/*
  Javascript library (C) Tim Gerla <timg@rrv.net>
  Version 2002-09-02
 
  Helper utilities for various tasks.
*/

/* 
   Moves the currently selected option in a select box up or down.
 
   sel: the selection box.
   direction: -1 for up, 1 for down.
*/
function move(sel, direction) {
  var idx = sel.selectedIndex;

  if(idx >= sel.options.length-1 && direction > 0)
    return;
  if(idx < 1 && direction < 0)
    return;

  for(var i = 0; i < sel.options.length; i++) {
    if(i == idx) 
      sel.options[i].selected = true;
    else
      sel.options[i].selected = false;
  }
  
  for(var i = 0; i < sel.options.length; i++) {
    if (sel.options[i].selected && sel.options[i] != "" && sel.options[i+direction] != sel.options[sel.options.length]) {
      var tmpval = sel.options[i].value;
      var tmpval2 = sel.options[i].text;

      sel.options[i].value = sel.options[i+direction].value;
      sel.options[i].text = sel.options[i+direction].text;
      sel.options[i+direction].value = tmpval;
      sel.options[i+direction].text = tmpval2;
    }
  }
  // select moved item
  sel.options[idx].selected = false;
  sel.selectedIndex = idx+direction;
  sel.options[idx+direction].selected = true;
}

function sort (sel) {
   for (i = 0; i <sel.options.length; i++) {
      for (j = 0; j<sel.options.length-1; j++) {
         if (sel.options[j].text > sel.options[j+1].text) {
            tmpTxt = sel.options[j].text;
            tmpVal = sel.options[j].value;
            sel.options[j].text = sel.options[j+1].text ;
            sel.options[j].value = sel.options[j+1].value;
            sel.options[j+1].text = tmpTxt ;
            sel.options[j+1].value = tmpVal;
         }
      }
   }
}

function swap(target, source, bSort) {
  var source_len = source.length;

  for (var i=0; i<source_len ; i++){
    if (source.options[i].selected == true ) {
      target_len = target.length;
      target.options[target_len] = new Option(source.options[i].text,
					      source.options[i].value);
    }
  }

  for (var i = (source_len -1); i>=0; i--){
    if (source.options[i].selected == true ) {
      source.options[i] = null;
    }
  }
  if(bSort)
    sort(target);
}

function select_all(sel) {
  for (i=0; i < sel.length; i++) {
    sel.options[i].selected = true;
  }
}

function append(sel, text, value) {
  var len = sel.length;
  sel.options[len] = new Option(text, value);
}

function delete_selected(sel) {
  for (i=0; i < sel.length; i++) {
    if(sel.options[i].selected = true) {
	sel.options[i] = null;
    }
  }
}

function open_window(name,width, height) 
{
  attrbts = "width="+width+",height="+height+",resizable=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes";
  calwindow = window.open(name, "cal", attrbts);
  if(calwindow.opener == null)
    calwindow.opener = self;
}

