// Declare Global Variables

	var formAlertMessage='There were Errors!';
	var formSubmit=1;

//called each time there is an error in the validation routines.
function throwError (theField,theMessage) {
	formAlertMessage=formAlertMessage + "\n - " + theMessage;
	formSubmit=0;
	switchStyle(theField);
}

// highlights the errored field
function switchStyle (theField) {
	theSwitch=new Function("this.style.backgroundColor='FFFFFF';this.style.color='000000';if(this.type=='text') this.select();")
	theField.style.backgroundColor='#f5deb3';
	theField.style.color='red';
	theField.onfocus=theSwitch;
}

// formAlert displays the alert message and clears necessary variables to start the validation over.
function formAlert() {
	alert(formAlertMessage);
	formAlertMessage='There were Errors!';
	formSubmit=1;
}

// form field required. select list empty default value must be 0
function formRequired(theField,theMessage) {
	theField.value = theField.value.replace(/\s+$|^\s*/gi, "");
	if(theField.value == '' || theField.value == '-1' || theField.value == '0') {
		throwError(theField,theMessage);
	}
}

// form field must be composed only of numbers and letters.
function formIsAlphaNumeric(theField,theMessage) {
	// alert(theField.value + ", " + theMessage);
	theField.value = theField.value.replace(/\s+$|^\s*/gi, "");
	isAlphaNum = new RegExp ("[^0-9a-zA-Z \(\)\-\.\/]");
	// isAlphaNum = new RegExp ("[^0-9a-zA-Z]");
	if (isAlphaNum.test(theField.value)) {
		throwError(theField,theMessage);
	}
}

// multiple form fields must match.
function formIsSame(field1,field2,theMessage) {
	if(field1.value != field2.value) {
		throwError(field2,theMessage);
		switchStyle(field1);
	}
}

// multiple form fields must Not match.
function formIsNotSame(field1,field2,theMessage) {
	if(field1.value == field2.value) {
		throwError(field2,theMessage);
		switchStyle(field1);
	}
}

// form field must be a valid email address.
function formIsEmail (theField,theMessage) {
	var email = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
 	if(!email.test(theField.value)) {
		throwError(theField,theMessage);
	}
 }
