// ********************** VALIDATION MANAGER TOOL ********************** //
// **************************** Rev 2/12/05 **************************** //



//--------------------------------------------------------------------------
// *************** FORM VALIDATION VARIABLE DECLARATIONS *************** //
var regex_whitespace = /^\s+$/
var regex_alphabetic = /^[a-zA-Z]+$/
var regex_alphanumeric = /^[a-zA-Z0-9]+$/
var regex_integer = /^\d+$/

// remove all the spaces to restore the original regexp

var regex_email = /^.+$/
var error_location = "Pick a location";
var error_telephone = "Enter a telephone number";
var error_street1 = "Enter a street address";
var error_address2 = "";
var error_city = "Enter a city";
var error_state = "Enter a state";
var error_zip = "Enter a zip";
var error_city_state_zip = "Enter a city and state and/or a zip"
var error_email = "Enter an email address";



// ******************* FORM VALIDATION FUNCTIONS ******************* //
// Return TRUE if "text" is empty
function isEmptyString(text) {
	return ((text == null) || (text.length == 0));
}

// Return TRUE if "text" is empty or contains whitespace characters
function isWhitespace(text) {
	return (validateEmptyString(text) || regex_whitespace.test(text));
}

// Return TRUE if "text" is comprised of integers
function isIntegerString(text) {
	return (regex_integer.test(text));
}

// Return TRUE if one element in a Radio Button group is selected
function isRadioSelected(radioGroup) {
	var length = radioGroup.length;
	if (length == 0)
		return radioGroup.checked;
	for (var i=0; i<length; i++) {
		if (radioGroup[i].checked)
			return true;
	}
	return false
}

// Return TRUE if no elements in an array var group are checked
function isCheckNotSelected(theForm, varName) {
	var checkFound = false;
	for (var counter=0; counter < theForm.length; counter++) {
		//alert (theForm.elements[counter].name);
		if ((theForm.elements[counter].name == varName) && (theForm.elements[counter].checked == true)) {
			checkFound = true;
			return false;
		}
	}
	if (checkFound != true) {
		return true;
	}
}

// Return TRUE is the element is an array
function isArray(obj){return(typeof(obj.length)=="undefined")?false:true;}

// does not check zip+4
function isZipCode(text) {
	if (text.length < 5)
		return false;
	return isIntegerString(text.substring(0, 5));
}

function isStreetAddress(text) {
	return (!isEmptyString(text))
}

function isCity(text) {
	return (!isEmptyString(text))
}

function isState(select) {
	return (select.selectedIndex != 0)
}

function isPhoneAreaCode(text) {
	if(! (text.length == 3))
		return false;
	return isIntegerString(text)
}

// Prefix - the first 3 digits of a 7 digit phone number.
function isPhonePrefix(text){
	if(! (text.length == 3))
		return false;
	return isIntegerString(text)
}

// Extension is the last 4 digits of a 7 digit phone number
function isPhoneExtension(text) {
	if(! (text.length == 4))
		return false;
	return isIntegerString(text)
}



// ************************** VALIDATION ERRORS ************************ //
// Do the correct action for a validation error
function validationError(anErrorMsg, aValidationManager) {
	aValidationManager.addError(anErrorMsg);
	return false;
}



// ********************** MAIN VALIDATION FUNCTION ********************* //
// Object for management of error messages.
function ValidationManager() {
	this.errors = [];
	this.addError = function (aString) {
		if(this.errors.length == 0){ this.errors[0] = "Please review the following fields\n to make sure they are complete: \n"; }
		this.errors[this.errors.length] = aString;
	};
	this.displayError = function() {
		alert(this.errors.join("\n"));
	}
	this.hasError = function() {
		return this.errors.length > 0;
	}
}