//http://timothypowell.net/blog/?p=28
//by Timothy Powell 
//modified by Kaarel Lumi

//http://www.mredkj.com/tutorials/tutorial_mixed2b.html
//Author: Keith Jenci

function sortlist(select) {
arrTexts = new Array();

for(i=0; i<select.length; i++)  {
  arrTexts[i] = select.options[i].text;
}

arrTexts.sort();

  for(i=0; i<select.length; i++)  {
    select.options[i].text = arrTexts[i];
    select.options[i].value = arrTexts[i];
  }
}

var listbak;

/**
  * Filters a dropdown list based on a search string pattern.
  * @param pattern: a string of zero or more characters by 
  * which to filter the list
  * @param list: reference to a form object of type 'select'
  */
function filter(pattern, list) {
 	//search by regular expression, case-insensitive
 	pattern = new RegExp(pattern,'i');
 
 	// if the dropdown list passed in hasn't already been 
       // backed up, do that now 
 	if (!listbak){
  	    // Attach an array to the select object that is a 
           // backup of the original dropdown list
	    var options = list.getElementsByTagName('option');
            listbak = $A(options);
 	}

 	// Iterate through the backed up dropdown list and create 
        // a list of matches.
 	var matchOptions = listbak.findAll( 
		function(node){ return pattern.test(node.text); }
	);	
 
 	// Replace the dropdown list with the list of matches.
 	var listDelArray = $A(list.getElementsByTagName('option'));
 	listDelArray.each(function(node){
 	    Element.remove(node);
 	});
 
 	matchOptions.each(function(node) {
 	    Element.insert(list, node);
 	});
 
 	//Select the first item so the new options are apparent
 	list.selectedIndex=0;
}

var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);

function addOption(theSel, theText, theValue)
{
  var newOpt = new Option(theText, theValue);
  var selLength = theSel.length;
  theSel.options[selLength] = newOpt;
}

function deleteOption(theSel, theIndex)
{ 
  var selLength = theSel.length;
  if(selLength>0)
  {
    theSel.options[theIndex] = null;
  }
}

function moveOptions(theSelFrom, theSelTo, addtobak, list)
{
  if (!listbak){
    var options = list.getElementsByTagName('option');
    listbak = $A(options);
  }
  var selLength = theSelFrom.length;
  var selectedText = new Array();
  var selectedValues = new Array();
  var selectedCount = 0;
  
  var i;
  
  // Find the selected Options in reverse order
  // and delete them from the 'from' Select.
  for(i=selLength-1; i>=0; i--)
  {
    if(theSelFrom.options[i].selected)
    {
      selectedText[selectedCount] = theSelFrom.options[i].text;
      selectedValues[selectedCount] = theSelFrom.options[i].value;
      
      if (!addtobak)
      {
        for (a=listbak.length-1; a>=0; a--)
        {
	  if (listbak[a].value == theSelFrom.options[i].value)
	  {
	    listbak.splice(a, 1);
	    break;
          }
        }
      }
      deleteOption(theSelFrom, i);
      selectedCount++;
    }
  }
  
  // Add the selected text/values in reverse order.
  // This will add the Options to the 'to' Select
  // in the same order as they were in the 'from' Select.
  for(i=selectedCount-1; i>=0; i--)
  {
    addOption(theSelTo, selectedText[i], selectedValues[i]);

    if (addtobak)
    {
      var newOpt = new Option(selectedText[i], selectedValues[i]);
      listbak[listbak.length] = newOpt;
    }
    
  }
  
  if(NS4) history.go(0);
}
