
sortitems = 1;  // Automatically sort items within lists? (1 or 0)
//  var System = java.lang.System;

  function print_vals(box) {
      for(var i=0; i<box.options.length; i++) {
        System.err.println("value = " + box.options[i].value);
        System.err.println("text = " + box.options[i].text);
  }
 }

  function remove_selected(box) {
    for(var i=0; i<box.options.length; i++) {
      if(box.options[i].selected) {
        box.options[i].value = "";
        box.options[i].text = "";
        box.options[i].selected = false;
      }
    }
    refresh_box(box);
  }

  function remove_all(box) {
    for(var i=0; i<box.options.length; i++) {
        box.options[i].value = "";
        box.options[i].text = "";
        box.options[i].selected = false;
    }
    refresh_box(box);
  }
  
  function selectall_and_submit(box,form,rm) {
    refresh_box(box);
    for(var i=0; i<box.options.length; i++) {
      box.options[i].selected = true;
    }
    form.rm.value=rm;
    form.submit();
  }

  function copy_selected(fbox, tbox) {
    var got_names = new Array();
    var changed = 0;

    for(var i=0; i<tbox.options.length; i++) {
  //System.err.println("setting val " + tbox.options[i].value + " = 1");
       got_names[tbox.options[i].value] = "1";
    }
    for (var j=0; j < fbox.options.length; j++) {
 // System.err.println("j = " + j + "index = " + fbox.options[j].value + "value = " + got_names[fbox.options[j].value]);
       if(fbox.options[j].selected  && fbox.options[j].value != "" 
          && got_names[fbox.options[j].value] != "1") {
          var opt = new Option();
          opt.value = fbox.options[j].value;
          opt.text = fbox.options[j].text;
          tbox.options[tbox.options.length] = opt;
          changed = 1;
        }
       fbox.options[j].selectd = 0;
    }
    if (sortitems && changed) qsort_box(tbox);     
  }


  function copy_all(fbox, tbox) {
    remove_all(tbox);   
    for (var j=0; j < fbox.options.length; j++) {
          var opt = new Option();
          opt.value = fbox.options[j].value;
          opt.text = fbox.options[j].text;
          tbox.options[tbox.options.length] = opt;
       fbox.options[j].selectd = false;
    }
  }


  function refresh_box(box) {
 // System.err.println("In refresh_box");
     var j = 0;
    for(var i=0; i<box.options.length; i++) {
       if(box.options[i].value != "") {
          if (j < i) {
//  System.err.println("RB: COPYING. j = " + j + " i = " + i);
 // System.err.println("RB: value = " + box.options[j].value + " text = " + box.options[j].text);
              box.options[j].value = box.options[i].value;
              box.options[j].text = box.options[i].text;
              box.options[i].value = "";
              box.options[i].text = "";
          }
          j++;
       }
    }
    box.options.length = j;
  }
  
  function SortD(box)  {
    var temp_opts = new Array();
    var temp = new Object();
    for(var i=0; i<box.options.length; i++)  {
      temp_opts[i] = box.options[i];
    }
    for(var x=0; x<temp_opts.length-1; x++)  {
      for(var y=(x+1); y<temp_opts.length; y++)  {
        if(temp_opts[x].text > temp_opts[y].text)  {
          temp = temp_opts[x].text;
          temp_opts[x].text = temp_opts[y].text;
          temp_opts[y].text = temp;
          temp = temp_opts[x].value;
          temp_opts[x].value = temp_opts[y].value;
          temp_opts[y].value = temp;
        }
      }
    }
    for(var i=0; i<box.options.length; i++)  {
      box.options[i].value = temp_opts[i].value;
      box.options[i].text = temp_opts[i].text;
    }
  }

  function _qsort(vec,lo,up){
    var i, j, t;
    while(up > lo){
      i = lo;
      j = up;
      t = new Option();
      t.text = new String(vec[lo].text);
      t.value = vec[lo].value;
      while(i < j){
          while (vec[j].text > t.text)
          j--;
        vec[i] = new Option();
        vec[i].text = new String(vec[j].text);
        vec[i].value = vec[j].value;
        while((i < j) && ((vec[i].text == t.text) || !(vec[i].text >
t.text)))
          i++;
        vec[j] = new Option();
        vec[j].text = new String(vec[i].text);
        vec[j].value = vec[i].value;
      }
      vec[i] = t;
      if(i - lo < up - i){
       _qsort(vec,lo,i-1); lo = i+1;
      } else {
       _qsort(vec,i+1,up); up = i-1;
      }
    }
  }


  function _text_cmp(a,b){
    return (a.text == b.text) ? 0 : (a.text > b.text) ? 1 : -1;
  }

  function qsort_box(box){
    if(box == null){
      return;
    }
    var hi = box.options.length - 1; 
     _qsort(box.options,0,hi,_text_cmp);


  }

