// -----------------------------------------
// -- tools.js (c) terrr@kanalattacke.org --
// -----------------------------------------

// cross browser scripts

// ----------------------------------------------------------------------------------
// this function adds key/value pairs as option elements
// to a selectbox denoted by obj. the csvdata is a string
// which has following format:
// CSVDATA ::= LINE  
// LINE ::= KEY SEMICOLON VALUE BR LINE | epsilon
// KEY  ::= seq of char
// VALUE ::= sql of value
// SEMICOLON ::= ";"
// BR   ::= "\n";
function addOptions(obj,csvdata)
{
  // first, we have to tokenize the string
  lines = csvdata.split("\n");
  for(i=0; i<lines.length-1; i++)
    {
      keyValuePair = lines[i].split(";");
      //      alert("key: " + keyValuePair[0] + "\n value:" + keyValuePair[1]);
      obj.options[obj.options.length] = new Option(keyValuePair[1],keyValuePair[0])
    }
}

// ----------------------------------------------------------------------------------
// adds an option to a selectbox 
// obj ... selectbox which is to be changed
// text .. text which is displayed in the selectboxentry
// value . value which is used
// defaultSelected .. determines which option is preselected
// selected . determines which option is selected
function addOption(obj,text,value,defaultSelected,selected)
{
  obj.options[obj.options.length] = new Option(text,value,defaultSelected,selected);
}


// ----------------------------------------------------------------------------------
// deletes all options from a selectbox
// parameters: obj ... selectbox which is affected
function deleteOptions(obj)
{
  while (obj.options.length > 0)
    {
      obj.options[0] = null;
    }
}

// ----------------------------------------------------------------------------------
// deletes all selected options from a selectbox
// parameters: obj ... selectbox which is affected
function deleteSelectedOptions(obj)
{
  for (i=0; i< obj.options.length; i++)
    {
      if (obj.options[i].selected == true)
	{
	  obj.options[i] = null;
	  i--; 
	}
    }
}

// ----------------------------------------------------------------------------------
// opens a window
function popup(url, width, height) 
{
  window.open(url,
	      'popup'+ width + '_' + height,
	      'toolbar=no,location=yes,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+ width +
	      ',height='+ height );
}


// ----------------------------------------------------------------------------------
// selects all entries of a selectbox, except those which have a key of -1
// parameters: obj ... selectbox 
function selectAll(obj)
{
  for (i=0; i< obj.options.length; i++)
    {
      if (obj.options[i].value != -1)
	{
	  obj.options[i].selected = true;
	}
    }
}

// ----------------------------------------------------------------------------------
// this helper function selects all elements of the appropriate selectboxes
function selectComponents(mode)
{
  switch (mode)
    {
    case "articles":
      selectAll(document.articles.elements['categoriesSB[]']);
      selectAll(document.articles.elements['linksSB[]']);
      selectAll(document.articles.elements['objectsSB[]']);
      selectAll(document.articles.elements['articlesSB[]']);
      break;
    case "links":
      selectAll(document.forms['links'].elements['categoriesSB[]']);
      break;
    }
}

// ----------------------------------------------------------------------------------
// this function copies option entries from one selectbox to another
// parameters: src ... source selectbox
//             dest .. destination selectbox
function copySelectedOptions(src,dest) 
{
  if (src.selectedIndex == -1) {
    alert("Sie müssen eine Auswahl treffen!");
    return;
  }

  for (o=0;o<src.options.length;o++) 
    {
      if (src.options[o].selected) 
	{
	  value = src.options[o].value;
	  text  = src.options[o].text;
	  
	  add = true;
	  
	  // determine if an equal entry is already in dest
	  for (i=0;i<dest.options.length;i++) { 
	    if (dest.options[i].value == value) 
	      {
		add = false;
	      }
	  }
	      
	  if (add) {
	    opt = new Option(src.options[o].text,
			     src.options[o].value,
			     false,
			     true);
	    dest.options[dest.options.length] = opt;
		
	  }
	}
    }
} 



function setSelectionRange(input, selectionStart, selectionEnd) {
  if (input.setSelectionRange) {
    input.focus();
    input.setSelectionRange(selectionStart, selectionEnd);
  }
  else if (input.createTextRange) {
    var range = input.createTextRange();
    range.collapse(true);
    range.moveEnd('character', selectionEnd);
    range.moveStart('character', selectionStart);
    range.select();
  }
}
function setCaretToEnd (input) {
  setSelectionRange(input, input.value.length, input.value.length);
}
function setCaretToBegin (input) {
  setSelectionRange(input, 0, 0);
}
function setCaretToPos (input, pos) {
  setSelectionRange(input, pos, pos);
}
function selectString (input, string) {
  var match = new RegExp(string, "i").exec(input.value);
  if (match) {
    setSelectionRange (input, match.index, match.index + match
[0].length);
  }
}
function replaceSelection (input, replaceString) {
  if (input.setSelectionRange) {
    var selectionStart = input.selectionStart;
    var selectionEnd = input.selectionEnd;
    input.value = input.value.substring(0, selectionStart)
                  + replaceString
                  + input.value.substring(selectionEnd);
    if (selectionStart != selectionEnd) // has there been a selection
      setSelectionRange(input, selectionStart, selectionStart + 
replaceString.length);
    else // set caret
      setCaretToPos(input, selectionStart + replaceString.length);
  }
  else if (document.selection) {
    var range = document.selection.createRange();
    if (range.parentElement() == input) {
      var isCollapsed = range.text == '';
      range.text = replaceString;
      if (!isCollapsed)  { // there has been a selection
        //it appears range.select() should select the newly 
        //inserted text but that fails with IE
        range.moveStart('character', -replaceString.length);
        range.select();
      }
    }
  }
}



var sbproxy;

function addCategory(obj,aCat,bCat,id,entity)
{
  if (id != -1)
    {
      sbproxy.add(aCat,bCat,id,entity);
      sbproxy.refresh(obj,
		      'ARTICLE_REFRESH_CATEGORY',
		      aCat);
    }
  else
    {
      alert("Artikel muß zuerst abgespeichert werden, \nbevor er kategorisiert werden kann.");
    }
}

function subCategory(obj,aCat,bCat,id,entity)
{
  sbproxy.sub(aCat,bCat,id,entity);
  sbproxy.refresh(obj,
		  'ARTICLE_REFRESH_CATEGORY',
		  aCat);
}

function upCategory(obj,aCat,bCat,id,entity)
{
  sbproxy.up(aCat,bCat,id,entity);
  sbproxy.refresh(obj,
		  'ARTICLE_REFRESH_CATEGORY',
		  aCat);
}


function downCategory(obj,aCat,bCat,id,entity)
{
  sbproxy.down(aCat,bCat,id,entity);
  sbproxy.refresh(obj,
		  'ARTICLE_REFRESH_CATEGORY',
		  aCat);
}

function setCSSProperty (name, property, value) {
  if (document.all) {
    var lastStyleSheet = 
      document.styleSheets[document.styleSheets.length - 1];
    var selector = '#' + name;
    var css = property + ': ' + value + ';';
    lastStyleSheet.addRule(selector, css);
  }
  else if (document.getElementById) {
    var lastStyleSheet = 
      document.styleSheets[document.styleSheets.length - 1];
    var ruleText = '#' + name + ' { ' + property + ': ' + value 
+ '}';
    lastStyleSheet.insertRule(ruleText, lastStyleSheet.cssRules.length);
  }
}


function hideDOM(name) {  setCSSProperty(name,'display','none'); }
function showDOM(name) {  setCSSProperty(name,'display','block'); }

function popUpProperties(inobj) {
	op = window.open();
	op.document.open('text/plain');
	for (objprop in inobj) {
	op.document.write(objprop + ' => ' + inobj[objprop] + '\n');
	}
	op.document.close();
}
