/* The blankField and fillField functions deal with the scenario where it is useful for displaying information in a textbox, but removing it once the user clicks on the box. If the user leaves the box without entering a new value, the box default back to its start value.

Example usage: <input type="text" name="fromDate" size="10" maxlength="10" value="#form.fromDate#" onFocus="blankField(this, 'm/d/yyyy');" onBlur="fillField(this, 'm/d/yyyy');">

*/
function blankField(theField, startValue) {
	if (theField.value == startValue) {
		theField.value = "";
	}
}

function fillField(theField, startValue) {
	if (theField.value == "") {
		theField.value = startValue;
	}
}

/* The checkAll function provides the ability to select or deselect a group of checkboxes with the same name. By passing in the form name, field name and check value (true or false), the associated checkboxes will then either check themselves or uncheck themselves.
*/
function checkAll(formName, fieldName, checkValue) {
	theObj = "window.document." + formName + "." + fieldName;
	
	if (eval("typeof(" + theObj + ".length)") == "undefined") {
		eval(theObj + ".checked = " + checkValue);
	}
	
	else {
		for (x=0; x < eval(theObj + ".length"); x++) {
			eval(theObj + "[" + x + "].checked = " + checkValue);
		}
	}
}

/* Trim functions used to trim strings */
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}


/* The sortSelectBox function sorts a SELECT drop down list */
function sortSelectBox(elementName, startPosition) { 
	//alert(startPosition);
	var selectBox = document.getElementById(elementName); 
	var startPos = startPosition;
	arrTexts = new Array(); 
	arrValues = new Array(); 
	arrOldTexts = new Array(); 

	//if (startPos == "undefined") {
	//	startPos = 0;
	//}
	
	for(i=0; i<selectBox.length; i++) { 
		arrTexts[i] = selectBox.options[i].text; 
		arrValues[i] = selectBox.options[i].value; 
		arrOldTexts[i] = selectBox.options[i].text; 
	} 

	arrTexts.sort(sortByName); 

	for(i=0; i<selectBox.length; i++) { 
		selectBox.options[i].text = arrTexts[i]; 
		for(j=0; j<selectBox.length; j++) { 
			if (arrTexts[i] == arrOldTexts[j]) { 
				selectBox.options[i].value = arrValues[j]; 
				j = selectBox.length; 
			} 
		} 
	} 
}

/* The sortByName function is a helper function to the sortSelectBox function to allow for sorting without worrying about case sensitivity */
function sortByName(a, b) {
  var anew = a.toLowerCase();
  var bnew = b.toLowerCase();
  if (anew < bnew) return -1;
  if (anew > bnew) return 1;
  return 0;
}

/* The addToSelectBox function adds an item to a drop down list */
function addToSelectBox(obj,text,value,selected) {
	if (obj !=null && obj.options!=null) {
		obj.options[obj.options.length] = new Option(text, value, false, selected);
	}
}

function editSelectBoxItem(obj, text, value) {
	for (var x=(obj.options.length-1); x>= 0; x--) {
		if (obj.options[x].value == value) {
			obj.options[x].text = text;
		}
	} 
}

function removeSelectBoxItem(obj, value) {
	for (var x=(obj.options.length-1); x>= 0; x--) {
		if (obj.options[x].value == value) {
			obj.options[x] = null;
		}
	} 
}

