/**
 * FUNCTION:	val_form
 *
 *	onSubmit = "this..notOptional=true; 
 *				this..isInt=true; 
 *				this..allowNegatives=(true|false);
 *				return validateForm(this);"
 *
 * @param Form form pointer to the form to be validated
 * @param bool submit_check if true will throw error if form has been submitted twice
 * @return bool true if form passes tests, false if it does not
 */
var submitcount = 0;
function validateForm(form, submit_check) {
	var msg = "", value = "";

	//Prevent submitting the form when submit is clicked more than once.
	if (submit_check) {
		if (submitcount == 0) {
			submitcount++;
		}
		else {
			alert("You have already submitted the form. Thank you!");
			return false;
		}
	}
	
	for (var i = 0; i < form.length; i++) {
		var lF = form.elements[i];	  // get input fields from form

		if (lF.type == "checkbox")
			value = lF.checked;
		else if (lF.type == "radio")
			value = getRadioValue(lF);
		else if (lF.type == "select")
			value = getSelectValue(lF);
		else
			value = lF.value;

		// Check for required field.
		if ( lF.notOptional && isBlank(value) ) {
			if (lF.helpText)
				msg = "The form was not submited because '"+ lF.helpText +"' is empty.\n";
			else
				msg = "The form was not submited because a required field is empty.\n";
			msg += "\nPlease correct the error and re-submit.\n";
			alert(msg);
			submitcount = 0;
			focusOnField(lF);
			selectField(lF);
			return false;
		}
		
		// Perform remaining checks if field is not blank 
		if (!isBlank(value)) {
			// Check integer fields.
			if ( lF.isInt && !isInt(value, lF.allowNegatives) ) {
				if (lF.helpText)
					msg += "The form was not submited because '"+ lF.helpText +"' contains\n";
				else
					msg += "The form was not submited because an Integer field contains\n";
				msg += "an invalid character or is negative.\n";
				msg += "\nPlease correct the error and re-submit.\n";
				alert(msg);
				submitcount = 0;
				focusOnField(lF);
				selectField(lF);
				return false;
			}
		}
	}

	return true; //will return true if no input errors by user and form will be submitted
}


function focusOnField(obj) {
	if (obj.type == "text" || obj.type == "textarea" || obj.type == "password")
		obj.focus();
}

function selectField(obj) {
	if (obj.type == "text" || obj.type == "textarea" || obj.type == "password")
		obj.focus();
}

/* ======================================================================
	FUNCTION:	isNull

	INPUT:		val - the value to be tested

	RETURN:		true, if the value is null;
				false, otherwise.
====================================================================== */
function isNull( val ) {
	var isValid = false;

	if (val+"" == "null")
	isValid = true;

	return isValid;
}  // end isNull

/* ======================================================================
	FUNCTION:	isUndef

	INPUT:		val - the value to be tested

	RETURN:		true, if the value is undefined
				false, otherwise.
====================================================================== */
function isUndef( val ) {
	var isValid = false;

	if (val+"" == "undefined")
	isValid = true;

	return isValid;
}  // end isUndef

/* ======================================================================
	FUNCTION:	isBlank

	INPUT:		val - the value to be tested

	RETURN:		true, if the string is null, undefined or an empty string
				false, otherwise.

	CALLS:		isNull(), isUndef() which are defined elsewhere in the 
				Script Library
====================================================================== */
function isBlank( str ) {
	var isValid = false;

	if ( isNull(str) || isUndef(str) || (str+"" == "") )
	isValid = true;

	return isValid;
}  // end isBlank

/* ======================================================================
	FUNCTION:	isInt

	INPUT:		numstr (string/number)	 - the string that will be tested 
					to ensure that each character is a digit
				allowNegatives (boolean) - (optional) when true, allows 
					numstr to be negative (contain a '-').	When false, 
					any negative number or a string starting with a '-' 
					will be considered invalid.

	RETURN:		true, if all characters in the string are a character from 
					0-9, regardless of value for allowNegatives
				true, if allowNegatives is true and the string starts 
					with a '-', and all other characters are 0-9.
				false, otherwise.
====================================================================== */
function isInt( numstr, allowNegatives ) {
	// Return immediately if an invalid value was passed in
	if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "") 
		return false;

	// Default allowNegatives to true when undefined or null
	if (allowNegatives+"" == "undefined" || allowNegatives+"" == "null") 
		allowNegatives = true;

	var isValid = true;

	// convert to a string for performing string comparisons.
	numstr += "";  

	// Loop through string and test each character. If any
	// character is not a number, return a false result.
	// Include special case for negative numbers (first char == '-').	
	for (i = 0; i < numstr.length; i++) {
		if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9") || (numstr.charAt(i) == "-"))) {
			isValid = false;
			break;
		}
		else if ((numstr.charAt(i) == "-" && i != 0) || (numstr.charAt(i) == "-" && !allowNegatives)) {
			isValid = false;
			break;
		}
	} // END for   

	return isValid;
}  // end isInt

/* ======================================================================
	FUNCTION:	stripNonNumeric

	INPUT:  	str (string) - a string to be altered

	RETURN: 	a string containing only numeric characters 0-9;
				returns null if invalid arguments were passed

	DESC:		This function removes all non-numeric characters 
				from a given string.  It is useful for removing 
				dashes, parentheses, etc. from input strings such 
				as credit card numbers or phone nubmers.
====================================================================== */
function stripNonNumeric( str ) {
	var 	resultStr = "";

	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str == null)	
		return null;

	// Make sure the argument is a string
	str += "";

	// Loop through entire string, adding each character from the original
	// string if it is a number
	for (var i=0; i < str.length; i++) {
		if ( (str.charAt(i) >= "0") && (str.charAt(i) <= "9") )
			resultStr = resultStr + str.charAt(i);

	} // end for loop      

	return resultStr;
}  // end StripNonNumeric

function getRadioValue( radioObject ) { 
	var value = null;
	
	// Validate parameter value
	if (radioObject+"" == "undefined" || radioObject == null)
		return null;
	
	for (var i=0; i < radioObject.length; i++) { 
		if (radioObject[i].checked) { 
			value = radioObject[i].value;
			break;
		} 
	} // end for loop 
	
	return value;
}

function getSelectValue( selectObject ) {
	// Validate parameter value
	if (selectObject == null)
		return null;

	if (selectObject.selectedIndex+"" == "undefined" || 
			selectObject.selectedIndex == null ||
			selectObject.selectedIndex < 0)
		return null;

	return selectObject.options[selectObject.selectedIndex].value;
}
