/* 
  This file contains the needed javascript to create functionality for a 
  split-panel multiple item selector. It should be included with any page
  that uses the smarty plugin 
  	{html_select_multi ...}
  	
  ALSO NOTE, any form with such an element should wrap it's submit function
  with selectMultiCall(..), see function docs for details.
  
  Greg
*/

var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);

function moveOptions(source, target, limit, callback)
{
   if ((typeof limit == "undefined") || (limit == 0)) {
      limit = source.length + target.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 source Select.
  for(i=source.length-1; i>=0; i--)
  {
    if(source.options[i].selected)
    {
      if ( (selectedCount+target.length) >= limit){
      	break;
      }
      selectedText[selectedCount] = source.options[i].text;
      selectedValues[selectedCount] = source.options[i].value;
      deleteOption(source, i);
      selectedCount++;
    }
  }

  // Add the selected text/values in reverse order.
  // This will add the Options to the target Select
  // in the same order as they were in the 'from' Select.
  for(i=selectedCount-1; i>=0; i--)
  {
    addOption(target, selectedText[i], selectedValues[i]);
  }
alert("callback is " + callback);
	sortOptions(target);
 try{
  	eval(callback + "()");
  }catch(e){
  	//do nothing
  }
  
  if(NS4) history.go(0);

}
/***************************************************************************
* move selection to textarea
*/
function moveValues(source, target)
{

  var selectedText = new Array();
  var selectedValues = new Array();
  var selectedCount = 0;
  
  var i;
  var selection='';
  var values='';
  // Find the selected Options in reverse order
  // and delete them from the source Select.
  for(i=source.length-1; i>=0; i--)
  {
    if(source.options[i].selected)
    {
      selectedText[selectedCount] = source.options[i].text;
      
      if(target.value.length > 0)
      	target.value += "\n" + source.options[i].text;
      else
      	target.value =source.options[i].text;
      	
     /* selection = selection +',' + source.options[i].text;
      values = values +','+ source.options[i].value;
     */ 
      selectedValues[selectedCount] = source.options[i].value;
      //deleteOption(source, i);
      selectedCount++;
    }
  }

  //alert('selection '+ selection + ' values '+ values);
  
  // Add the selected text/values in reverse order.
  // This will add the Options to the target Select
  // in the same order as they were in the 'from' Select.
  /*for(i=selectedCount-1; i>=0; i--)
  {
    addOption(target, selectedText[i], selectedValues[i]);
  }*/
  //sortOptions(target);
  
  if(NS4) history.go(0);
}

function deleteOption(object,index) {
    object.options[index] = null;
}

function addOption(object,text,value) {
    var defaultSelected = false;
    var selected = false;
    var optionName = new Option(text, value, defaultSelected, selected)
    object.options[object.length] = optionName;
    object.options[object.length-1].selected = false;
    
}

function sortOptions(what) {
	
    var copyOption = new Array();
    for (var i=0;i<what.options.length;i++)
        copyOption[i] = new Array(what[i].value, what[i].text);

    copyOption.sort();

    for (var i=what.options.length-1;i>-1;i--)
        deleteOption(what,i);

    for (var i=0;i<copyOption.length;i++)
        addOption(what,copyOption[i][1],copyOption[i][0])
	return true;
}


function selectAll(){
	var args = $A(arguments);
	args.each(function(item) {
	    for (var i=0; i<item.options.length; i++){
	        item.options[i].selected=true;
	    }
	});
}

function selectNone(selectObj){
    var args = $A(arguments);
	args.each(function(item) {
	    for (var i=0; i<item.options.length; i++){
	        item.options[i].selected=false;
	    }
	});
}

/* selectMultiCall must wrap the function that submits the form.
(it causes all selected elements to become highlighted and included
in form data.)

This function and the example depend on prototype.js, for the select
functions $('elementID') and $$('css').

e.g. If using an image to submit the form, one would use:

<img alt="[submit]" src="/img/submit.png" 
	onclick="selectMultiCall('$(\'form_id\').submit()');">

Note that the argument to selectMultiCall is an escaped string.
*/

function selectMultiCall(callback){
	
	var selects = $$('.right_select');
	for (var i = 0; i<selects.length; i++){
		selectAll(selects[i]);
	}
	
	if (typeof(callback) == 'string') {
		eval(callback);
	}else {
		callback();
	}
	
	for (var i = 0; i<selects.length; i++){
		selectNone(selects[i]);
	}		
}
