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.age.value.length > 0){
		if (!isAllCharsInDomain(objForm.age.value, NUMBERS)){
			strErrorMessage += 'Please enter a number for Age.\n';
		}
	}
	
	if (objForm.referral_2.value.length == 0) {
	    if (objForm.referral_1.value == 'other') strErrorMessage += "Please specify where you heard about New LifeStyles Online.\n";
	    if (objForm.referral_1.value == 'newspaper') strErrorMessage += "Pleaset specify the newspaper where you heard about New LifeStyles Online.\n";
	    if (objForm.referral_1.value == 'link') strErrorMessage += "Please specify the site where you heard about New LifeStyles Online.\n";
  	}
	
	var pPrimaryLocation = objForm.primary_location.options[objForm.primary_location.selectedIndex].value
	
	if (pPrimaryLocation.charAt(0) == "*") {
		strErrorMessage += 'Please select a specific region within ' + pPrimaryLocation.slice(1) + '.\n';
	}
	
	if (pPrimaryLocation == "") {
		strErrorMessage += 'A primary location must be chosen.\n';
	}else{
		if (objForm.primary_quantity.value == "0"){
			strErrorMessage += 'A quantity must be entered for primary location.\n';
		}
	}
	
	var pSecondLocation = objForm.secondary_location.options[objForm.secondary_location.selectedIndex].value
	if (pSecondLocation.charAt(0) == "*") {
		strErrorMessage += 'Please select a specific region within ' + pSecondLocation.slice(1) + '.\n';
	}
	
	if (pSecondLocation != "") {
		if (objForm.secondary_quantity.value == "0"){
			strErrorMessage += 'A quantity must be entered for second location.\n';
		}
	}
  
  	var pThirdLocation = objForm.third_location.options[objForm.third_location.selectedIndex].value
	if (pThirdLocation.charAt(0) == "*") {
		strErrorMessage += 'Please select a specific region within ' + pThirdLocation.slice(1) + '.\n';
	}
	
	if (pThirdLocation != "") {
		if (objForm.third_quantity.value == "0"){
			strErrorMessage += 'A quantity must be entered for third location.\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));
}



 
