// JavaScript Document

function isValidEmail(str) 
{
   return (str.indexOf(".") > 0) && (str.indexOf("@") > 0 );
}
function isValidInput(checkOK, checkStr)
{
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  return  allValid; 
}

function doClear(theText) {
     if (theText.value == theText.defaultValue) {
         theText.value = ""
     }
 }
 
 function doFill(theText) {
     if (theText.value == "") {
         theText.value = theText.defaultValue
     }
 }

function FrontPage_Form1_Validator(theForm)
{
  //Validate First Name input -- required
  var FirstName
  FirstName = theForm.FirstName.value
  if (FirstName == "")
  {
    alert("Please enter a value for the \"First Name\" field.");
    theForm.FirstName.focus();
    return (false);
  }
  if (FirstName.length > 50)
  {
    alert("Please enter at most 50 characters in the \"First Name\" field.");
    theForm.FirstName.focus();
    return (false);
  }
  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzfSOZsozYÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ\t\r\n\f";
  var checkStr = theForm.FirstName.value;
  if (!isValidInput(checkOK,checkStr))
  {
    alert("Please enter only letters in the \"First Name\" field.");
    theForm.FirstName.focus();
    return (false);
  }
    //Validate Last Name input -- required
  var LastName
  LastName = theForm.LastName.value  
  if (LastName == "")
  {
    alert("Please enter a value for the \"Last Name\" field.");
    theForm.LastName.focus();
    return (false);
  }
  if (LastName.length > 50)
  {
    alert("Please enter at most 50 characters in the \"Last Name\" field.");
    theForm.LastName.focus();
    return (false);
  }
  var checkOK = "-ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzfSOZsozYÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ\t\r\n\f";
  var checkStr = theForm.LastName.value;
  if (!isValidInput(checkOK, checkStr))
  {
    alert("Please enter only letters or hyphens in the \"Last Name\" field.");
    theForm.LastName.focus();
    return (false);
  }   
  //Validate email input -- required
  var EmailAddress
  EmailAddress = theForm.EmailAddress.value
  if (EmailAddress == "")
  {
    alert("Please enter a value for the \"Email Address\" field.");
    theForm.EmailAddress.focus();
    return (false);
  }  
  if (!isValidEmail(theForm.EmailAddress.value))
  {
    alert("Please enter a valid email.");
    theForm.EmailAddress.focus();
    return (false);
  }
  if (EmailAddress.length >128)
  {
    alert("Please enter at most 128 characters in the \"Email Address\" field.");
    theForm.EmailAddress.focus();
    return (false);
  }
  //Validate Phone input -- required
  var Telephone
  Telephone = theForm.Telephone.value
  if (Telephone == "")
  {
    alert("Please enter a value in the \"Telephone Number\" field.");
    theForm.Telephone.focus();
    return (false);
  }
  if (Telephone.length != 10 )
  {
    alert("Please enter only 10 numbers in the \"Telephone Number\" field.");
    theForm.Telephone.focus();
    return (false);
  }
//  if (Telephone.length == 12 && Telephone.search(/^\d{3}-\d{3}-\d{4}$/)==-1)
//  {
//    alert("Please enter only 10 digits or add \"-\" with the format xxx-xxx-xxxx for Telephone.");
//    theForm.Telephone.focus();
//    return (false);
//  }
//  if (Telephone.length > 12)
//  {
//    alert("Please enter only 10 digits or add \"-\" with the format xxx-xxx-xxxx for Telephone.");
//    theForm.Telephone.focus();
//    return (false);
//  }
  var checkOK = "0123456789\t\r\n\f";
  var checkStr = theForm.Telephone.value;
  if (!isValidInput(checkOK, checkStr))
  {
    alert("Please enter only 10 numbers in the \"Telephone Number\" field.");
    theForm.Telephone.focus();
    return (false);
  }
    //Validate Zip Code input -- required for US and CA  
  var checkOK = "0123456789-";
  var checkStr = theForm.Zip.value;
  var zip = theForm.Zip.value
  if (zip == "")
  {
    alert("Please enter a value for the \"Zip/postal Code\" field.");
    theForm.Zip.focus();
    return (false);
  } 
  if ((!(zip.length == 5 || zip.length == 9 || zip.length == 10))&& isValidInput(checkOK, checkStr))
  {
	 alert("Please enter a valid \"Zip Code\".");
	 theForm.Zip.focus();
	 return false;
  }
  if ((zip.length == 5 || zip.length == 9) && !(isValidInput(checkOK, checkStr)))
  {
     alert("Please enter a\"Zip Code\"with the format 99999 or 99999-9999 for US.");
     theForm.Zip.focus();
	 return false;
  }
  if (zip.length == 9 && isValidInput(checkOK, checkStr)&& zip.search(/^\d{5}-\d{4}$/)== -1)
  {
     alert("Please enter a\"Zip Code\"with the format 99999 or 99999-9999 for US.");
     theForm.Zip.focus();
	 return false;
  }
  if (zip.length == 10 && zip.search(/^\d{5}-\d{4}$/)== -1) 
  {
	 alert("Please enter a \"Zip Code\"with the format 99999-9999 for US.");
	 theForm.Zip.focus();
	 return false;
  }
  if (isValidInput(checkOK, checkStr))
	 return true
  else
  {	
	    //Canadian Zip code validation
	 var allValid = false;
	 var zip = theForm.Zip.value;//removeSpaces(checkStr);
	 if (zip.length == 6 && zip.search(/^[a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d$/) != -1) 
	 { 
	   allValid = true; 
	 }
	 else if (zip.length == 7 && zip.search(/^[a-zA-Z]\d[a-zA-Z] \d[a-zA-Z]\d$/) != -1) 
	 {
	   allValid = true;
	 }
	 else allValid = false;    
	 if (!allValid)
	 {
		alert("Please enter a Postal Code with the format C2G9B3 OR C2G 9B3 for Canada.")
		theForm.Zip.focus();
		return (false);
	 }
   }
  return (true);
}
