
function do_onError(form_object, input_object, object_value, error_message) {
	alert(error_message);
	return false;
}

function chk_hasValue(obj, obj_type) {
	if (obj_type == "TEXT") {
		if (obj.value.length == 0)
			return false;
		else
			return true;
	} else if (obj_type == "SELECT") {
		for (i=0; i < obj.length; i++) {
			if (obj.options[obj.selectedIndex].value == "--")
				return false;
			}
	   	return true;
	} else if (obj_type == "RADIO") {
		for (i=0; i < obj.length; i++) {
			if (obj[i].checked)
				return true;
			}
		return false;
	}
}

function do_checkinteger(object_value) {
	//Returns true if value is a number or is NULL
	//otherwise returns false

	if (object_value.length == 0)
		return true;

	//Returns true if value is an integer defined as
	//   having an optional leading + or -.
	//   otherwise containing only the characters 0-9.
	var decimal_format = ".";
	var check_char;

	//The first character can be + -  blank or a digit.
	check_char = object_value.indexOf(decimal_format)
	//Was it a decimal?
	if (check_char < 1)
	return chk_checkNumber(object_value);
	else
	return false;
	}


function chk_numberRange(object_value, min_value, max_value) {
	// check minimum
	if (min_value != null) {
		if (object_value < min_value)
			return false;
	}

	// check maximum
	if (max_value != null) {
		if (object_value > max_value)
			return false;
		}

	//All tests passed, so...
	return true;
}


function chk_checkNumber(object_value) {
	//Returns true if value is a number or is NULL
	//otherwise returns false

	if (object_value.length == 0)
		return true;

	//Returns true if value is a number defined as
	//   having an optional leading + or -.
	//   having at most 1 decimal point.
	//   otherwise containing only the characters 0-9.
	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

	//The first character can be + - .  blank or a digit.
	check_char = start_format.indexOf(object_value.charAt(0))
	//Was it a decimal?
	if (check_char == 1)
		decimal = true;
	else if (check_char < 1)
		return false;

	//Remaining characters can be only . or a digit, but only one decimal.
	for (var i = 1; i < object_value.length; i++) {
		check_char = number_format.indexOf(object_value.charAt(i))
		if (check_char < 0)
			return false;
		else if (check_char == 1) {
			if (decimal)		// Second decimal.
				return false;
			else
				decimal = true;
		} else if (check_char == 0) {
			if (decimal || digits)
				trailing_blank = true;
		// ignore leading blanks

		} else if (trailing_blank)
			return false;
		else
			digits = true;
	}
	//All tests passed, so...
	return true
}


function do_checkRange(object_value, min_value, max_value) {
	//if value is in range then return true else return false

	if (object_value.length == 0)
		return true;

	if (!chk_checkNumber(object_value)) {
		return false;
	} else {
		return (chk_numberRange((eval(object_value)), min_value, max_value));
	}

	//All tests passed, so...
	return true;
}


function do_checkPhone(object_value) {
	if (object_value.length == 0)
		return true;

	if (object_value.length != 12)
		return false;

	// check if first 3 characters represent a valid area code
	if (!chk_checkNumber(object_value.substring(0,3)))
		return false;
	else if (!chk_numberRange((eval(object_value.substring(0,3))), 100, 1000))
		return false;

	// check if area code/exchange separator is either a'-' or ' '
	if (object_value.charAt(3) != "-" && object_value.charAt(3) != " ")
		return false

	// check if  characters 5 - 7 represent a valid exchange
	if (!chk_checkNumber(object_value.substring(4,7)))
		return false;
	else if (!chk_numberRange((eval(object_value.substring(4,7))), 100, 1000))
		return false;

	// check if exchange/number separator is either a'-' or ' '
	if (object_value.charAt(7) != "-" && object_value.charAt(7) != " ")
		return false;

	// make sure last for digits are a valid integer
	if (object_value.charAt(8) == "-" || object_value.charAt(8) == "+")
		return false;
	else {
		return (do_checkinteger(object_value.substring(8,12)));
	}
}

function checkmyForm(theForm) {
	if  (!chk_hasValue(theForm.fname, "TEXT" )) {
		if  (!do_onError(theForm, theForm.fname, theForm.fname.value, "You must enter a FIRST NAME to continue.")) {
			theForm.fname.focus();
			return false;
		}
	}
	
	if  (!chk_hasValue(theForm.lname, "TEXT" )) {
		if  (!do_onError(theForm, theForm.lname, theForm.lname.value, "You must enter a LAST NAME to continue.")) {
			theForm.lname.focus();
			return false;
		}
	}
	
	if  (!chk_hasValue(theForm.address, "TEXT" )) {
		if  (!do_onError(theForm, theForm.address, theForm.address.value, "You must enter an ADDRESS to continue.")) {
			theForm.address.focus();
			return false;
		}
	}
	
	if  (!chk_hasValue(theForm.city, "TEXT" )) {
		if  (!do_onError(theForm, theForm.city, theForm.city.value, "You must enter a CITY to continue.")) {
			theForm.city.focus();
			return false;
		}
	}
	
	if  (!chk_hasValue(theForm.state, "TEXT" )) {
		if  (!do_onError(theForm, theForm.state, theForm.state.value, "You must enter a STATE to continue.")) {
			theForm.state.focus();
			return false;
		}
	}
	
	if  (!chk_hasValue(theForm.zip, "TEXT" )) {
		if  (!do_onError(theForm, theForm.zip, theForm.fname.zip, "You must enter a ZIP CODE to continue.")) {
			theForm.zip.focus();
			return false;
		}
	}
	
	if  (!chk_hasValue(theForm.phone_area_code, "TEXT" )) {
		if  (!do_onError(theForm, theForm.phone_area_code, theForm.phone_area_code.value, "You must enter an AREA CODE to continue.")) {
			theForm.phone_area_code.focus();
			return false;
		}
	}
	
	if  (!chk_hasValue(theForm.phone_number, "TEXT" )) {
		if  (!do_onError(theForm, theForm.phone_number, theForm.phone_number.value, "You must enter a PHONE NUMBER to continue.")) {
			theForm.phone_number.focus();
			return false;
		}
	}
	
	if  (!chk_hasValue(theForm.email, "TEXT" )) {
		if  (!do_onError(theForm, theForm.email, theForm.email.value, "You must enter an EMAIL to continue.")) {
			theForm.email.focus();
			return false;
		}
	}	

	return true;
}

