//
//	For use with two Select lists (both of which have the multiple flag set)
//
//	Take the selected options from one menu and stick them in the other, keeping both lists in alphabetical display order...
//
function SelectSwap(strFromMenu, strToMenu)
{
	var arrRemoved     = new Array();
	
	var objFromMenu    = eval("document."+strFromMenu);
	var arrFromOptions = objFromMenu.options;

	var objToMenu      = eval("document."+strToMenu);
	var arrToOptions   = objToMenu.options;
	
	for(var intLoop=0; intLoop < arrFromOptions.length; intLoop++)
	{
		var objElement = arrFromOptions[intLoop];
		if(objElement.selected == true)
		{
			arrToOptions[arrToOptions.length] = new Option(objElement.text, objElement.value);
			arrRemoved[arrRemoved.length]     = intLoop;
		}
	}
	
	//	This section deals with the removal of the elements. 
	//	The maths surrounding intElement are due to the element positions changing in line with prior removal of elements
	//
	var intRemoved = 0;
	for(var intLoop=0; intLoop < arrRemoved.length; intLoop++)
	{
		var intElement = arrRemoved[intLoop] - intRemoved;
		
		arrFromOptions[intElement] = null;
		++intRemoved;
	}
	
	SelectSort(strToMenu);
}


//
//	Sort a Select List into Alphabetical order
//
function SelectSort(strMenu)
{
	if(strMenu.substring(0,11) != 'ObjectForm.')
	{
		strMenu = 'ObjectForm.'+strMenu;
	}

	var objMenu    = eval('document.'+strMenu);
	var arrOptions = objMenu.options;
	var arrSort    = new Array();
	
	for(var intLoop=0; intLoop<arrOptions.length; intLoop++)
	{
		arrSort[arrSort.length] = arrOptions[intLoop].text + '|' + arrOptions[intLoop].value;
	}
	
	// Without emptying it, strange things happen...
	objMenu.options.length = 0;

	arrSort.sort();
	for(var intLoop=0; intLoop<arrSort.length; intLoop++)
	{
		var arrElement            = arrSort[intLoop].split('|');
		objMenu.options[objMenu.options.length] = new Option(arrElement[0], arrElement[1]);
	}
}



function SelectPopulate(arrOptions, strMenu, strNotInMenu, bolEmpty)
{
	var objMenu     = eval('document.' + strMenu);
	var arrNotThese = new Array();

	if(bolEmpty == true)
	{
		objMenu.options.length = 0;
	}
	
	if(strNotInMenu)
	{
		var objNotInMenu = eval('document.' + strNotInMenu);
		for(var intLoop=0; intLoop < objNotInMenu.options.length; intLoop++)
		{
			arrNotThese[objNotInMenu.options[intLoop].value] = true;
		}
	}
	
	for(var intLoop=0; intLoop < arrOptions.length; intLoop++)
	{
		if(arrOptions[intLoop] != null && arrNotThese[intLoop] == null)
		{
			objMenu.options[objMenu.options.length] = new Option(arrOptions[intLoop], intLoop);
		}
	}
	//SelectSort(strMenu);
}



function SelectPopulateParentArray(arrOptions, arrParents, strMenu, intParentID, bolEmpty, bolDefaultSelect)
{
	var objMenu     = eval('document.ObjectForm.' + strMenu);
	if(bolEmpty == true)
	{
		objMenu.options.length = 0;
	}
	
	if(bolDefaultSelect == true)
	{
		objMenu.options[objMenu.options.length] = new Option('-- Select --', -1);
	}

	for(var intLoop=0; intLoop < arrOptions.length; intLoop++)
	{
		if(arrOptions[intLoop] != null)
		{
			if(arrParents[intLoop] == intParentID)
			{
				objMenu.options[objMenu.options.length] = new Option(arrOptions[intLoop], intLoop);
			}
		}
	}
	SelectSort(strMenu);
	//alert(objMenu.options.length);
	return true;
}



//	When the form is submitted, we actually want to select all options in this selction, otherwise they wont actually be returned!
//
function SubmitMultiple(strSelectMenu, strActualMenu, strInput)
{
	SelectMenuDeselectAll(strSelectMenu);
	SelectMenuDeselectAll(strActualMenu);

	var objActualMenu = eval("document."+strActualMenu);
	var objInput      = eval("document."+strInput);

	var arrValues  = new Array();
	var arrOptions = objActualMenu.options;
	for(var intLoop=0; intLoop < arrOptions.length; intLoop++)
	{
		arrValues[arrValues.length]  = arrOptions[intLoop].value;
	}
	objInput.value = arrValues;
}


function SelectMenuDeselectAll(strMenu)
{
	//	Unset original select menu, we don't want these being returned
	var objMenu    = eval('document.'+strMenu);
	var arrOptions = objMenu.options;
	for(var intLoop=0; intLoop < arrOptions.length; intLoop++)
	{
		arrOptions[intLoop].selected = false;
	}
}


function SelectSmartMenus(strFromMenu, strToMenu, bolAddToToMenu, bolRemoveFromFromMenu, bolOrderToMenu)
{

	var objFromMenu    = eval("document."+strFromMenu);
	var arrFromOptions = objFromMenu.options;

	var objToMenu      = eval("document."+strToMenu);
	var arrToOptions   = objToMenu.options;

	var arrRemoved     = new Array();

	for(var intLoop=0; intLoop < arrFromOptions.length; intLoop++)
	{
		var objElement = arrFromOptions[intLoop];
		if(objElement.selected == true)
		{
			if(bolAddToToMenu == 1)
			{
				arrToOptions[arrToOptions.length] = new Option(objElement.text, objElement.value);
			}

			if(bolRemoveFromFromMenu == 1)
			{
				arrRemoved[arrRemoved.length]     = intLoop;
			}
		}
	}


	// Now actually remove any form elements that we were supposed to
	var intRemoved = 0;
	for(var intLoop=0; intLoop < arrRemoved.length; intLoop++)
	{
		var intElement = arrRemoved[intLoop] - intRemoved;

		arrFromOptions[intElement] = null;
		++intRemoved;
	}

	if(bolOrderToMenu == 1)
	{
		SelectSort(strToMenu);
	}
}


function SelectMoveUp(strMenu)
{
	SelectMove(strMenu, -1);
}


function SelectMoveDown(strMenu)
{
	SelectMove(strMenu, 1);
}


function SelectMove(strMenu, intDirection)
{

	var objMenu    = eval("document."+strMenu);
	var arrOptions = objMenu.options;

	var arrMove     = new Array();
	if(intDirection < 0)
	{
		for(var intLoop=0; intLoop < arrOptions.length; intLoop++)
		{
			if(arrOptions[intLoop].selected == true)
			{
				arrMove[arrMove.length] = intLoop;
			}
		}
	}
	else
	{
		for(var intLoop=arrOptions.length - 1; intLoop > -1; intLoop--)
		{
			if(arrOptions[intLoop].selected == true)
			{
				arrMove[arrMove.length] = intLoop;
			}
		}
	}



	for(var intLoop=0; intLoop < arrMove.length; intLoop++)
	{
		intPos = arrMove[intLoop];
		var objElement = arrOptions[intPos];

		var intSwapPosition = intPos + intDirection;
		if(intSwapPosition < 0)
		{
			intSwapPosition = 0;
		}
		if(intSwapPosition == arrOptions.length)
		{
			intSwapPosition = arrOptions.length - 1;
		}
		var objSwap = arrOptions[intSwapPosition];

		var strValue   = objSwap.value;
		var strDisplay = objSwap.text;

		objSwap.value       = objElement.value;
		objSwap.text        = objElement.text;
		objSwap.selected    = true;

		objElement.value    = strValue;
		objElement.text     = strDisplay;
		objElement.selected = false;
	}
}

