//////////////////////////////////////////////////////////////////
//	FORM UTILITIES
//////////////////////////////////////////////////////////////////
/**
	Select option sorting
*/
function compareText (option1, option2) {
	return option1.text < option2.text ? -1 :
		option1.text > option2.text ? 1 : 0;
}

function compareValue (option1, option2) {
	return option1.value < option2.value ? -1 :
		option1.value > option2.value ? 1 : 0;
}

function compareTextAsFloat (option1, option2) {
	var value1 = parseFloat(option1.text);
	var value2 = parseFloat(option2.text);
	return value1 < value2 ? -1 :
		value1 > value2 ? 1 : 0;
}

function compareValueAsFloat (option1, option2) {
	var value1 = parseFloat(option1.value);
	var value2 = parseFloat(option2.value);
	return value1 < value2 ? -1 :
		value1 > value2 ? 1 : 0;
}

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];
}

//////////////////////////////////////////////////////////////////
//	CHECKBOX TOGGLER
//////////////////////////////////////////////////////////////////
function toggleSelection(toggle, whichForm, whichElement) {
	if (toggle)	selectAll(whichForm, whichElement);
	else		unselectAll(whichForm, whichElement);
}

function selectAll(whichForm, whichElement) {
	var selected = document[whichForm][whichElement];
	if (selected.length)
		for (var i = 0; i < selected.length; i++)
			selected[i].checked = true;
	else
		selected.checked = true;
}

function unselectAll(whichForm, whichElement) {
	var selected = document[whichForm][whichElement];
	if (selected.length)
		for (var i = 0; i < selected.length; i++)
			selected[i].checked = false;
	else
		selected.checked = false;
}

//////////////////////////////////////////////////////////////////
//	FORM VERIFICATION
//////////////////////////////////////////////////////////////////
/**
	Tests for user inputed white space characters.
*/
function isBlank (string) {
	for (var i = 0; i < string.length; i++) {
		var c = string.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
	}
	return true;
}

/**
	Receives a string of required elements
	split by commas and a reference to the form and test
	to see if they are filled out.
	
	*** really hacked together...
*/
function verifyForm(required, whichForm) {
	var msg			= "";
	var error		= false;
	var errorMsg	= "";
	
	//	Split string of required fields into an array
	//var reqList = required.split(/\s*,\s*/);
	var reqList = required;
	
	//	Test for empty required fields and list name if empty
	for (var i = 0; i < reqList.length; i++) {
		var whichElement = eval("whichForm." + reqList[i][0]);
		if ((whichElement.value == null) || (whichElement.value == "") || isBlank(whichElement.value))
			errorMsg += reqList[i][1] + "\n";
	}
	
	//	If everything is ok, return true and submit the form already...
	if ( errorMsg == "" ) return true;
	
	//	Shucks, we have to create an error message
	msg += "_________________________________________________________\n\n";
	msg += "The form was not submitted because some required fields\n"
		+  "were left blank.\n\n"
	msg += "Please correct these error(s) and re-submit.\n";
	msg += "_________________________________________________________\n\n";
	msg += "Some required fields are missing.\n";
	msg += errorMsg;
	
	//	Show error message
	alert(msg);
	
	//	Prevent form from submitting
	return false;
}
