var NUMBERS = "1234567890";

function ValidateDirectoryForm() { 
	var strErrorMessage = '';
	var objForm = document.directory_form;

	if (objForm.first_name.value.length == 0) strErrorMessage += 'First name is missing.\n';
	if (objForm.last_name.value.length == 0) strErrorMessage += 'Last 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 == '' || !IsValidEmailAddress(objForm.email.value)) strErrorMessage += 'Valid email address is missing.\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 (getCheckedValue(objForm.personal_or_professional) == '') {
		strErrorMessage += 'Please select if your use for this site is personal or professional.\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));
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function IsValidEmailAddress(strEmailAddress) {
	intAtPosition = strEmailAddress.indexOf('@');
	intLastDotPosition = strEmailAddress.lastIndexOf('.');
	blnHasSpaces = (strEmailAddress.indexOf(' ') > -1);
	return !(blnHasSpaces || (strEmailAddress=='') || (intAtPosition == 0) || (intAtPosition == 1) || (intLastDotPosition == -1) || (intAtPosition > intLastDotPosition) || (intLastDotPosition+1 == strEmailAddress.length))
}


