/*******************************************************************
*
* File    : admin.js
*
*
* Created : 02/10/2001
*
* Author  : Don Hauri
*
* Purpose : 
*
* History
*
* Date         Version        Description
* 2001-02-10     1.0          Original Release
*
* Program Notes:
*
*******************************************************************/

/*** Create some global variables ***/
var FIELD_DELIMITER = "~";

function moveSelection(src, direction, dirtyFlag){
   if (src.selectedIndex > -1){
      
      var pos = src.selectedIndex;
      
      if ((direction == "up") && (pos > 0)) {         
         var o = src.options[pos];
   	   var selectedOpt = new Option(o.text,o.value);
         
         var temp = src.options[pos - 1];
         var tempOpt = new Option(temp.text, temp.value);
         
         src.options[pos - 1] = selectedOpt;
         src.options[pos] = tempOpt;
         
         src.selectedIndex = pos - 1;
         dirtyFlag.value = -1;
      }
      
      if ((direction == "down") && (pos < (src.length - 1))){      
         var o = src.options[pos];
   	   var selectedOpt = new Option(o.text,o.value);
         
         var temp = src.options[pos + 1];
         var tempOpt = new Option(temp.text, temp.value);
         
         src.options[pos + 1] = selectedOpt;
         src.options[pos] = tempOpt;
         
         src.selectedIndex = pos + 1;
         dirtyFlag.value = -1;
      }
   }
}

/***********************************************************
* Function   : moveListItems
*
* Parameters : from - the name of the source list
*              to   - the name of the target list
*              
* Description : Moves selected items in a list for the source
*               list to the target list in alphabetical order.
***********************************************************/
function moveListItems(from, to, maintainOrder, dirtyFlag){
   for(var i=from.options.length-1; i>=0; i--){
   	if (from.options[i].selected) {
         /*  
         ** Create a new option object for the 'to list' based 
         **  on the properties of the 'from list' option.  Remove 
         **  the 'from list' option by setting it to null.
         */
   		var o = from.options[i];
   		var newOpt = new Option(o.text,o.value);
   		from.options[i] = null;
   		
         //  Find the position in the 'to list to place the new option.
   		if (to.options.length == 0)
   			to.options[0] = newOpt;
   		else{
            if (maintainOrder){
      			for(var j=to.options.length-1; j>=0; j--){
      				if(newOpt.text < to.options[j].text){
      					var curOpt = new Option(to.options[j].text, to.options[j].value);
      					to.options[j+1] = curOpt;
      					if (j==0)
      						to.options[0] = newOpt;
      				}
      				else{
      					to.options[j+1] = newOpt;
      					break;
      				}
      			}
            } else {
               to.options[to.options.length] = newOpt;
            }
   		}																
			// Set the form's dirty flag (0 = false; -1 = true)
         dirtyFlag.value = -1;
   	}
   }
}

/***********************************************************
* Function   : saveListItems
*
* Parameters : from - the name of the source list
*              target   - the name of the target field
*              
* Description : Saves the values of the items in the 'from list' 
*               to the target field.  Values are delimited by 
*               the value of the FIELD_DELIMITER.
***********************************************************/
function saveListItems(tagName){
	var from = document.getElementById(tagName + "_ListBox2");
	var target = document.getElementById(tagName);
	
	var sValue = "";			
	for(var i=0; i < from.options.length; i++)
	{
		sValue += from.options[i].value;
		if (i < from.options.length - 1)
			sValue += FIELD_DELIMITER;
	}	
	target.value = sValue;
}

