/****************************************************
*This section contains functions for sorting the options in a select box
*Note: I found this but don't know the author and can't remember the page
****************************************************/
//Compares the text property of the options
function compareText (option1, option2) {
  return option1.text < option2.text ? -1 :
    option1.text > option2.text ? 1 : 0;
}
//Compares the value property of the options
function compareValue (option1, option2) {
  return option1.value < option2.value ? -1 :
    option1.value > option2.value ? 1 : 0;
}
//Compares the text property of the options as floating point numbers
function compareTextAsFloat (option1, option2) {
  var value1 = parseFloat(option1.text);
  var value2 = parseFloat(option2.text);
  return value1 < value2 ? -1 :
    value1 > value2 ? 1 : 0;
}
//Compares the value property of the options as floating point numbers
function compareValueAsFloat (option1, option2) {
  var value1 = parseFloat(option1.value);
  var value2 = parseFloat(option2.value);
  return value1 < value2 ? -1 :
    value1 > value2 ? 1 : 0;
}
//Sorts the options based on the chosen compare function. See above functions
function sortSelect (select, compareFunction) {
  if (!compareFunction)
    compareFunction = compareText;
  var options = new Array (select.options.length);
  for (var i = 0; i < options.length; i++)
    options[i] =
      new Option (
        select.options[i].text,
        select.options[i].value,
        select.options[i].defaultSelected,
        select.options[i].selected
      );
  options.sort(compareFunction);
  select.options.length = 0;
  for (var i = 0; i < options.length; i++)
    select.options[i] = options[i];
}
/**************************************************************
*End option sorting functions
*************************************************************/

/**************************************************************
*This section interacts with a set of two select boxes and two
*buttons that allow items to be moved between the select boxes.
*
*Note: I would really like to know what the real name for this
*	type of control is
**************************************************************/
var twinBoxes = {

	//This function takes 2 select box inputs and moves the selected item from the
	//	first box to the second box.
  moveData: function(selectOne, selectTwo)
  {
		//Get index of selected value
  	var selected = selectOne.selectedIndex;
		//next is the current length of selectTwo because selectTwo [0] indexed
  	var next = selectTwo.length;
		//Create a new option with the values from selectOne
  	selectTwo.options[next] = new Option(selectOne.options[selected].text,selectOne.options[selected].value);
		//Set the selected option of selectOne to null
  	selectOne.options[selected] = null;
		//sort the select that got the new option
		//sortSelect(selectTwo,compareText);
  }
};
/**************************************************************
*End Section
**************************************************************/

//Collect all values from a select box and return them semi-colon delimited.
function gatherAllFromSelect(mySelect)
{
	var collectedData = new String();
	for(var i=0; i < mySelect.length; i++)
	{
		collectedData = collectedData.concat(mySelect.options[i].value,";");
	}
	return collectedData;
}

//Moves selected options up or down
function moveSelected (select, direction) {
	//Check that an option is actually selected (-1 means nothing is selected)
	if (select.selectedIndex != -1) {
		if (direction == 'down') {
			//Check to make sure the option isn't the bottom option
			if (select.selectedIndex != select.options.length - 1)
				var x = select.selectedIndex + 1;
			else
				return;
		}
		else if(direction == 'up'){
			//Check to make sure the option isn't the top option
			if (select.selectedIndex != 0)
				var x = select.selectedIndex - 1;
			else
				return;
		}
		var swapOption = new Object();

		//Move options
		swapOption.text = select.options[select.selectedIndex].text;
		swapOption.value = select.options[select.selectedIndex].value;
		swapOption.selected = select.options[select.selectedIndex].selected;

		select.options[select.selectedIndex].text = select.options[x].text;
		select.options[select.selectedIndex].value = select.options[x].value;
		select.options[select.selectedIndex].selected = select.options[x].selected;

		select.options[x].text = swapOption.text;
		select.options[x].value = swapOption.value;
		select.options[x].selected = swapOption.selected;

	}
}

/**
 * checkBoxes.	Ensure at	least	one	checkbox is	selected.
 * @param		theForm				The	form which contains	the	checkboxes.
 * @param		theCheckbox		The	checkbox name.
 * @param		alertText			Text to	be displayed in	popup	if required.
 * @return		Return bool	to indicate	if at	least	one	checkbox
 *						is selected.
 */
function checkBoxes( theForm,	theCheckbox, alertText )
{
	for( j = 0;	j	<	document.forms[theForm].elements[theCheckbox].length;
		j++	)
	{
		if(	document.forms[theForm].elements[theCheckbox][j].checked )
		{
			return true;
		}
	}

	if(	alertText	!= ""	)
	{
		alert( "Please select " + alertText	+	"."	);
	}
	return false;
}
