var NUMBERS = "1234567890";

function ValidateDirectoryForm() { 
	var strErrorMessage = '';
	var objForm = document.directory_form;

	if (objForm.last_name.value.length == 0) strErrorMessage += 'Last name is missing.\n';
	if (objForm.first_name.value.length == 0) strErrorMessage += 'First name is missing.\n';
	if (objForm.address.value.length == 0) strErrorMessage += 'Street address is missing.\n';
	if (objForm.city.value.length == 0) strErrorMessage += 'City is missing.\n';
	if (objForm.state.value == "") strErrorMessage += 'State must be chosen.\n';
	if (!IsValidZip(objForm.zip.value)) strErrorMessage += 'Zip is missing or invalid.\n';
	if ((!objForm.email.value) || (objForm.email.value.indexOf("@") == -1)) strErrorMessage += 'Email address is missing or invalid.\n';
	if ((!objForm.email_confirm.value) || (objForm.email_confirm.value.indexOf("@") == -1)) strErrorMessage += 'Email address confirmation is missing or invalid.\n';
	if (objForm.email.value != objForm.email_confirm.value) strErrorMessage += 'Email and Email Confirm must match.\n';

	if (objForm.contact_method.value == 'Phone') {
		if (objForm.phone1.value.length < 3 || objForm.phone2.value.length < 3 || objForm.phone3.value.length < 4 || (!isAllCharsInDomain(objForm.phone1.value, NUMBERS)) || (!isAllCharsInDomain(objForm.phone2.value, NUMBERS)) || (!isAllCharsInDomain(objForm.phone3.value, NUMBERS))){
			strErrorMessage += 'You chose Phone as the best method to contact you. Please supply a valid phone number.\n';
		}
	}else{
		if (objForm.phone1.value != '' || objForm.phone2.value != '' || objForm.phone3.value != ''){
			if (objForm.phone1.value.length < 3 || objForm.phone2.value.length < 3 || objForm.phone3.value.length < 4 || (!isAllCharsInDomain(objForm.phone1.value, NUMBERS)) || (!isAllCharsInDomain(objForm.phone2.value, NUMBERS)) || (!isAllCharsInDomain(objForm.phone3.value, NUMBERS))){
				strErrorMessage += 'Phone number is invalid.\n';
			}
		}
	}
	
	if (objForm.pref_area.value.length == 0) strErrorMessage += 'Specific Geographic Area is missing.\n';
	
	if (strErrorMessage != '') {
		alert('Validation Error:\n\n' + strErrorMessage);
		return false;
	} 
	else{
		return true;
	}
}

function isAllCharsInDomain(strValue, strDomain){
	var bAllValuesInDomain = true;
	for (var i=0; i<strValue.length; i++) {
		temp = strValue.substring(i, i+1);
		if (strDomain.indexOf(temp) == "-1") bAllValuesInDomain = false;
	}
	return bAllValuesInDomain;
}

function IsValidZip(strZip) {
	return (IsValidUSZip(strZip) || IsValidCanadianZip(strZip));
}

function IsValidUSZip(strZip) {
	var regex = /(^\d{5}$)|(^\d{5}-\d{4}$)/
	return (regex.test(strZip));
}

function IsValidCanadianZip(strZip) {
    var regex = /^[abceghjklmnprstvxyABCEGHJKLMNPRSTVXY]\d[a-zA-Z] \d[a-zA-Z]\d$/;
	return (regex.test(strZip));
}



 

