function validateForm(theForm)
{

	if (!IsEmail(theForm.email.value))
	{
		alert("Please enter a valid email address.");
		theForm.email.focus();
		return false;
	}

	if (theForm.fullname.value == "")
	{
		alert("Please enter your name.");
		theForm.fullname.focus();
		return false;
	}

	if (theForm.primaryphone.value == "")
	{
		alert("Please enter your preferred phone number.");
		theForm.primaryphone.focus();
		return false;
	}

	if (theForm.referredby.value == "")
	{
		alert("Please tell us how you heard about us.");
		theForm.referredby.focus();
		return false;
	}

	if (theForm.referredby.value == "Other")
	{
    // idinline('physicalother');
	  if  (theForm.other.value == "")
	  {
		  alert("Please give us details of Other.");
		  theForm.other.focus();
		  return false;
		}
	}

	if (theForm.postalcode.value == "")
	{
		alert("Please provide your postal code.  It is for reference purposes only unless you request info by mail, in which case it is needed to ensure delivery.");
		theForm.postalcode.focus();
		return false;
	}

	if (theForm.country.value == "")
	{
  	if (!IsValidZipCode(theForm.postalcode.value))
  	{
  		alert("Postal code not in US zipcode format.  Please provide Country or correct zipcode.");
  		theForm.country.focus();
  		return false;
  	}
	}

  if (theForm.regmail.checked == true)
    {
    	if (theForm.address1.value == "")
    	{
    		alert("Please enter your Address.");
    		theForm.address1.focus();
    		return false;
    	}
    	if (theForm.city.value == "")
    	{
    		alert("Please enter your City.");
    		theForm.city.focus();
    		return false;
    	}
    	if (theForm.stprov.value == "")
    	{
    		alert("Please enter your State or Province.");
    		theForm.stprov.focus();
    		return false;
    	}
  }

  theForm.nothing.value=42;

  
// 	if (theForm.primaryPhone.value == "")
// 	{
// 		alert("Please enter your day time contact phone.");
// 		theForm.primaryPhone.focus();
// 		return false;
// 	}
// 	if (theForm.Referrer.value == "")
// 	{
// 		alert("Please let us know how you heard about us.");
// 		theForm.Referrer.focus();
// 		return false;
// 	}


}

function RegExSupported() {
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
  	var tempStr = "a";
  	var tempReg = new RegExp(tempStr);
  	if (tempReg.test(tempStr)) supported = 1;
   	}
  if (supported) { return true;   }
  else           { return false;  }
}

function IsEmail(str){
  var supported = 0;
  supported = RegExSupported();
	if (!supported)
	{
		return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	}
	else
	{
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)| ");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
 		return (!r1.test(str) && r2.test(str));
 	}
}

function IsValidZipCode(elementValue){
  var supported = 0;
  supported = RegExSupported();
	if (!supported)	{
		return true;
	}
	else
	{
    var zipCodePattern = /^\d{5}$|^\d{5}-\d{4}$/;
     return zipCodePattern.test(elementValue);
 	}
}

function idhide(id){
  var s=document.getElementById(id).style;
  if(s.display!='none'){
    s.display='none';
  }
}

function idinline(id){
  var s=document.getElementById(id).style;
  if(s.display!='inline'){
    s.display='inline';
  }
}

function showmail(v){
  if(v){
    idinline('physical1');
    idinline('physical2');
  }else{
    idhide('physical1');
    idhide('physical2');
  }
}

function displayotherifrefbyother(theField) {
	if (theField.value == "Other"){
    idinline('physicalother');
  }else{
    idhide('physicalother');
  }
}

function togglecountryprompt(theField) {
  if (theField.value == "")
  {
    idinline('physicalctryopt');
    idhide('physicalctryreq');
  }else if (IsValidZipCode(theField.value)) 
  {
    idinline('physicalctryopt');
    idhide('physicalctryreq');
  }else{
    idhide('physicalctryopt');
    idinline('physicalctryreq');
  }

}
