
/*--------------------------------------------------------------------
SelectAddRemoveOption(objForm, objButton, objAddSelect, objRemoveSelect)
Usage:	Add and remove options to and from the selAppAdd and the selAppRemove select boxes.
		Used to add and remove Applications for the Organization

Input:	objForm - The document form object containing the select elements.
		objButton - The calling button object.  
		objAddSelect - The select object the option is added from.
		objRemoveSelect - The select object the option is added to.		
		
Notes:	The value of the 'Add' button requires an '>' character in its value property.

--------------------------------------------------------------------*/
function SelectAddRemoveOption(objForm, objButton, objAddSelect, objRemoveSelect)
{
   var i = 0;
   var blnIsAdd = blnAddApp = false;

   with (objForm)
   {
      blnIsAdd = (objButton.value.indexOf('>') != -1) ? true : false;     
	  
      with ( ((blnIsAdd)? objAddSelect : objRemoveSelect) )
      {
         for (i = 0; i < length; i++)
         {
            if (blnAddApp = (options[i].selected)? true : false)
            {
               with (options[i])
               { 
                  if (blnIsAdd)
                  {
                     objRemoveSelect.options[objRemoveSelect.length] = new Option( text, value );
                  }
                  else
                  {
                     objAddSelect.options[objAddSelect.length] = new Option( text, value );
                  }
               } 
               
               options[i] = null;
               i--;
            }                  
         } 
         
         if (options[0] != null)
            options[0].selected = true;
      } 
   } 
}

/*--------------------------------------------------------------------
SelectAddRemoveOptionBy2(objForm, objButton, objAddSelect, objRemoveSelect, objRemoveSelect2)
Usage:	Add and remove options to and from the selAppAdd and the selAppRemove/selAppRemove2 select boxes.
		Used to add and remove Applications for the Organization

Input:	objForm - The document form object containing the select elements.
		objButton - The calling button object.  
		objAddSelect - The select object the option is added from.
		objRemoveSelect - The select object the option is added to.
		objRemoveSelect2 - The second select object the option is added to.				
		
Notes:	The value of the 'Add' button requires an '>' character in its value property.

--------------------------------------------------------------------*/
function SelectAddRemoveOptionBy2(objForm, objButton, objAddSelect, objRemoveSelect, objRemoveSelect2)
{
   var i = 0;
   var blnIsAdd = blnAddApp = false;

   with (objForm)
   {
      blnIsAdd = (objButton.value.indexOf('>') != -1) ? true : false;     
			with ( ((blnIsAdd)? objAddSelect : objRemoveSelect) )
      {
         for (i = 0; i < length; i++)
         {
            if (blnAddApp = (options[i].selected)? true : false)
            {
               with (options[i])
               { 
                  if (blnIsAdd)
                  {
                     objRemoveSelect.options[objRemoveSelect.length] = new Option( text, value );
                     objRemoveSelect2.options[objRemoveSelect2.length] = new Option( text, value );
                  }
                  else
                  {
                     objAddSelect.options[objAddSelect.length] = new Option( text, value );
                  }
               } 
							
							 if (blnIsAdd == false){
								objRemoveSelect2.options[i] = null;
							 }	         
               options[i] = null;
               i--;
            } 
            
            if(navigator.appName == "Netscape" ) 
              history.go(0);
         } 
         
         if (options[0] != null)
            options[0].selected = true;
      } 
   } 
}

/*--------------------------------------------------------------------
SortListAlpha(objList)
Usage:	Sorts the destination list alphabetically by option text
		
Input:	objList - The list to sort
	
--------------------------------------------------------------------*/

function SortListAlpha(objList)
{
	var items = objList.options.length;
	var i;
	
	// create array and make copies of options in list
	var tmpArray = new Array(items);
	
	for ( i=0; i<items; i++ )
    {
        tmpArray[i] = new Option(objList.options[i].text, objList.options[i].value);
	}	
		
	// sort options using given function
	tmpArray.sort(CompareOptionTextAlpha);
	
	// make copies of sorted options back to list
	for ( i=0; i<items; i++ )
	{
		objList.options[i] = new Option(tmpArray[i].text, tmpArray[i].value);
	}
}

function CompareOptionTextAlpha(a,b) 
{
	/*
	 * return >0 if a>b
	 *         0 if a=b
	 *        <0 if a<b
	 */
	// textual comparison
	return a.text!=b.text ? a.text<b.text ? -1 : 1 : 0; 
}


function OrderOption(obj, inc)
{
	if (obj==null) return;
	if (obj.disabled) return;
	
	var i, j;
	var o1, o2;
	var tVal, tText, bSel;
	var bAll;
	
	// first ensure that not ALL the options are selected
	for (i=0; i<obj.options.length; i++)
	{
		if (!obj.options[i].selected)
		{
			bAll = false;
			break;
		}	
	}
		
	if (inc<0) // move items up
		i = 0;
	else // move down
		i = obj.options.length - 1;
		
	while (i>=0 && i<obj.options.length)
	{
		// ensure the remaining items in the direction we're moving are not selected
		bAll = true;
		j = i+inc;
		while (j>-1 && j<obj.options.length)
		{		
			if (!obj.options[j].selected)
			{
				bAll = false;
				break;
			}
			j+=inc;
		}
		
		if (!bAll)
		{
		
			bSel = false;
			if (obj.options[i].selected && i+inc>=0 && i+inc<obj.options.length)
			{

				o1 = obj.options[i]; // get the current option
				o2 = obj.options[i+inc]; // get the option above or below the current
														
				bSel = o2.selected;
				tVal = o2.value;
				tText = o2.text;	
					
				// swap the 2nd option with the current one
				o2.value = o1.value;
				o2.text = o1.text;
				o2.selected = true; 
					
				// assign the current option with the temp values
				o1.value = tVal;
				o1.text = tText;
				o1.selected = bSel;
			}
			else
			{
				i -= inc;
			}
		}
		else
			i-=inc;
	}
	
}