// JavaScript Document

function check_empty(x,i){
	if (x.elements[i].value =="" || x.elements[i].value =="NULL"){
		//----------------------------- if validation fails, then generate alert box message and reset focus
		alert("there is no value for the " + x.elements[i].id+" field");
		return false;
	}
	else {return true;}
}

function check_email(email,email2){
with(email){

	apos=value.indexOf("@");
	dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2){
		alert("this is not a valid Email address.");
		return false;
		}
}
//-- if you do not have a re-entry of email address, disable this section
		if (email.value != email2.value){                             //-
		alert("The Email addresses do not match.");                   //-
		return false;                                                 //-
		}                                                             //-
//------------------------------------------------------------------------
	else {return true;}
}

function check_phone(thisField){
with(thisField){
var checkOK = "0123456789-()- \t\r\n\f";
var checkStr = thisField.value;
var allValid = true;

	if (checkStr.length < 10){
	alert("you must provide a Phone Number including Area Code");
	return false;
	}
	//-- check each character in the field one by one
	for (i = 0;  i < checkStr.length;  i++){
    ch = checkStr.charAt(i);
	//-- loop through each of the approved characters compaiting them to the present field character
	for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j)){
		break;  //-- stop the loop when a match is made
		}
		if (j == checkOK.length){
		allValid = false;
		break;  //-- stop the loop when the end when you reach the end of the characters in the field
		}
	}

	if (!allValid){
		alert("Please enter only digit, whitespace, \"()\", and \"-\" characters in the "+thisField.id+" field.");
		return false;
	}
	else{return true;}
}//-- end with
}//-- end function check_phone()


function check_form(thisForm){

//with(thisForm){
	
var x=document.getElementById("formHM");
	
	//----------------------------------------- validate the form
	for (var i=0;i<x.length;i++){
		//------------------------------------- check to see if the field needs to be validated
		if (x.elements[i].alt == "validate" || x.elements[i].alt == "validate_email" || x.elements[i].alt == "validate_phone"){
			//--------------------------------- valadte the field
			if (check_empty(x,i)==false){
			x.elements[i].focus();
			return false;
			}
			//--------------------------------- validate email and compare to email re-entry
			if (x.elements[i].alt == "validate_email"){
				j=i+1;
				if (check_email(x.elements[i],x.elements[j])==false){
				x.elements[i].focus();
				return false;
				}
			}
			if (x.elements[i].alt == "validate_phone"){
				if (check_phone(x.elements[i])==false){
				x.elements[i].focus();
				return false;
				}
			}
		}
	}
	//--if all validation passes, then return true and allow the form to be submitted
	//--  alert("all fields have passed validation");
	return true;
//}

}

