//	Revision History
// -----------------------------------------------------------------------
//  WHO    DATE         DESCRIPTION
// -----------------------------------------------------------------------
//  WAF	  10-07-06	Initial Creation
//
// -----------------------------------------------------------------------
function ValidForm(form)
{
  var txt = "";

  form.fullname.value = Trim(form.fullname.value); 
  if (IsEmpty(form.fullname.value) || !IsCharacters(form.fullname.value))
  {
    alert("Please enter a valid name.");
    form.fullname.focus();
    form.fullname.select();
    return false;		
  }

  form.address.value = Trim(form.address.value);
  if (IsEmpty(form.address.value))
  {
    alert("Please enter a valid address.");
    form.address.focus();
    form.address.select();
    return false;		
  }

  form.city.value = Trim(form.city.value);
  if (IsEmpty(form.city.value) || !IsCharacters(form.city.value))
  {
    alert("Please enter a valid city.");
    form.city.focus();
    form.city.select();
    return false;		
  }
  
  form.state.value = Trim(form.state.value);
  txt = String(form.state.value);
  form.state.value = txt.toUpperCase();
  if (IsEmpty(form.state.value) || IsNumeric(form.state.value) || !IsValidState(form.state.value) || txt.length < 2)
  {
    alert("Please enter a valid state.");
    form.state.focus();
    form.state.select();
    return false;		
  }
  
  form.zip.value = Trim(form.zip.value); 
  txt = String(form.zip.value);
  if (IsEmpty(form.zip.value) || !IsNumeric(form.zip.value) || txt.length < 5)
  {
    alert("Please enter a valid zip code (5 digits).");
    form.zip.focus();
    form.zip.select();
    return false;		
  }

  // Figure out what the user selected for "Preferred Contact"
  // Radio Buttons:  [0]=Home, [1]=Work (default), [2]=Cell
  if (form.b_phone[0].checked)
  {
    form.Hparea.value = Trim(form.Hparea.value);
    txt = String(form.Hparea.value);
    if (IsEmpty(form.Hparea.value) || !IsNumeric(form.Hparea.value) || txt.length < 3)
    {
      alert("Please enter a valid home telephone area code.");
      form.Hparea.focus();
      form.Hparea.select();
      return false;		
    }

    form.Hprefix.value = Trim(form.Hprefix.value);
    txt = String(form.Hprefix.value);
    if (IsEmpty(form.Hprefix.value) || !IsNumeric(form.Hprefix.value) || txt.length < 3)
    {
      alert("Please enter a valid home telephone prefix.");
      form.Hprefix.focus();
      form.Hprefix.select();
      return false;		
    }

    form.Hpsuffix.value = Trim(form.Hpsuffix.value);
    txt = String(form.Hpsuffix.value);
    if (IsEmpty(form.Hpsuffix.value) || !IsNumeric(form.Hpsuffix.value) || txt.length < 4)
    {
      alert("Please enter a valid home telephone suffix.");
      form.Hpsuffix.focus();
      form.Hpsuffix.select();
      return false;		
    }
  }
  else if (form.b_phone[1].checked)  // Work Phone
  {
    form.Wparea.value = Trim(form.Wparea.value);
    txt = String(form.Wparea.value);
    if (IsEmpty(form.Wparea.value) || !IsNumeric(form.Wparea.value) || txt.length < 3)
    {
      alert("Please enter a valid work telephone area code.");
      form.Wparea.focus();
      form.Wparea.select();
      return false;		
    }
  
    form.Wprefix.value = Trim(form.Wprefix.value);
    txt = String(form.Wprefix.value);
    if (IsEmpty(form.Wprefix.value) || !IsNumeric(form.Wprefix.value) || txt.length < 3)
    {
      alert("Please enter a valid work telephone prefix.");
      form.Wprefix.focus();
      form.Wprefix.select();
      return false;		
    }

    form.Wpsuffix.value = Trim(form.Wpsuffix.value);
    txt = String(form.Wpsuffix.value);
    if (IsEmpty(form.Wpsuffix.value) || !IsNumeric(form.Wpsuffix.value) || txt.length < 4)
    {
      alert("Please enter a valid work telephone suffix.");
      form.Wpsuffix.focus();
      form.Wpsuffix.select();
      return false;		
    }
  }
  else if (form.b_phone[2].checked)  // Cell Phone
  {
    form.Cparea.value = Trim(form.Cparea.value);
    txt = String(form.Cparea.value);
    if (IsEmpty(form.Cparea.value) || !IsNumeric(form.Cparea.value) || txt.length < 3)
    {
      alert("Please enter a valid cell telephone area code.");
      form.Cparea.focus();
      form.Cparea.select();
      return false;		
    }
  
    form.Cprefix.value = Trim(form.Cprefix.value);
    txt = String(form.Cprefix.value);
    if (IsEmpty(form.Cprefix.value) || !IsNumeric(form.Cprefix.value) || txt.length < 3)
    {
      alert("Please enter a valid cell telephone prefix.");
      form.Cprefix.focus();
      form.Cprefix.select();
      return false;		
    }

    form.Cpsuffix.value = Trim(form.Cpsuffix.value);
    txt = String(form.Cpsuffix.value);
    if (IsEmpty(form.Cpsuffix.value) || !IsNumeric(form.Cpsuffix.value) || txt.length < 4)
    {
      alert("Please enter a valid cell telephone suffix.");
      form.Cpsuffix.focus();
      form.Cpsuffix.select();
      return false;		
    }
  }

  form.email.value = Trim(form.email.value);
  if (IsEmpty(form.email.value) || !isValidEmail(form.email.value))
  {
    alert("Please enter a valid email address.");
    form.idEmail.focus();
    return false;		
  }

  return true;
}
// -----------------------------------------------------------------------
function IsEmpty(sTextField)
{
  //alert("func IsEmpty(sTextField)");
  //alert("sTextField(" + sTextField.length + ") = [" + sTextField + "]");

  if (sTextField.length == 0 || sTextField == null)
  {
    //alert("func IsEmpty(sTextField) = true");
    return true;
  }
  else
  { 
    //alert("func IsEmpty(sTextField) = false");
    return false;
  }
}
// -----------------------------------------------------------------------
function IsNumeric(sNmbr)
{
  var ValidChars = "0123456789.";
  var IsNumber = true;
  var Char;
 
  //alert("func IsNumeric(sNmbr)");
  //alert("sNmbr(" + sNmbr.length + ") = [" + sNmbr + "]");

  for (i = 0; i < sNmbr.length && IsNumber == true; i++) 
  { 
    Char = sNmbr.charAt(i); 
    if (ValidChars.indexOf(Char) == -1) 
    {
      IsNumber = false;
    }
  }

  //alert("func IsNumeric(sNmbr) = " + IsNumber);
  return IsNumber;
}
// -----------------------------------------------------------------------
// Validate State Abbreviation is Valid
// -----------------------------------------------------------------------
function IsValidState(strState)
{
	str_States = "wa|or|ca|ak|nv|id|ut|az|hi|mt|wy|co|nm|nd|sd|ne|ks|ok|tx|mn|ia|mo|ar|la|wi|il" + 
	             "ms|mi|in|ky|tn|al|fl|ga|sc|nc|oh|wv|va|pa|ny|vt|me|nh|ma|ri|ct|nj|de|md|dc";
	
	if (str_States.indexOf(strState.toLowerCase() + "|") > -1)
	{
		return true;
	}
	
	return false;
}

// -----------------------------------------------------------------------
function IsCharacters(sStr)
{
  var inValidChars = "#/$%^&*()+=!?<>'[]{}|`@~\-_:;1234567890"; 
  var IsChar = true;
  var Char;
 
  // alert("func IsCharacters(sStr)");
  // alert("sStr(" + sStr.length + ") = [" + sStr + "]");

  for (i = 0; i < sStr.length && IsChar == true; i++) 
  { 
    Char = sStr.charAt(i); 
    if (inValidChars.indexOf(Char) != -1) 
    {
      IsChar = false;
    }
  }

  // alert("func IsCharacters(sStr) = " + IsChar);
  return IsChar;
}

// -----------------------------------------------------------------------
function isValidEmail(strEmail)
{
  var sTmpStr = strEmail;

  //alert("func isValidEmail(strEmail)");
  //alert("strEmail(" + sTmpStr.length + ") = [" + sTmpStr + "]");

  //declare a character string containing all invalid characters for email address fields
  var invalidChars = "#/$%^&*()+=!?<>'[]{}|`~\-_:,;"; 
  //alert("invalidChars = [" + invalidChars + "]");

  //Next we are going to check for invalid characters in the email using 
  //the invalidChars string we defined at the beginning of this function. 
  //We need a predefined loop to iterate through the structure to check for 
  //ALL invalid characters, since JavaScript knows how many characters is 
  //contained within the invalidChars string, we will use the .length method 
  //as our ending loop value.  We will iterate through, the email object, 
  //comparing each character contained within the invalidChars string to 
  //verify no invalid characters exist within the email object. In otherwords,
  //we will compare string a, to string b, and see if any characters match. 
  //If any characters match then we will exit with false indicating the email 
  //address submitted was not typed correctly.

  var Char;
  for (i = 0; i < sTmpStr.length; i++) 
  { 
    Char = sTmpStr.charAt(i); 
    //alert("invalidChars.indexOf(Char) = " + invalidChars.indexOf(Char) );
    
    if (invalidChars.indexOf(Char) != -1) 
    {
      //alert("invalid char [" + Char + "] found at pos [" + i + "+1]");
      return false;
    }
  }

  //the next phase of the verification process is to determine if the email address 
  //submitted contains one and only one "@" symbol.
  AtSign = "@";
  var nCnt = 0;
  var cAt;

  for (i = 0; i < sTmpStr.length; i++) 
  { 
    cAt = sTmpStr.charAt(i); 
    if (AtSign.indexOf(cAt) != -1)
    {
      nCnt++;
      //alert("@ sign found at pos [" + i + "+1]");
    }
  }

  //alert("counted [" + nCnt + "] @ signs");
  if (nCnt == 0 || nCnt > 1)
  {
    //alert("func isValidEmail(strEmail) = false (more than 1 - @)");
    return false;
  }

  //now we need to check and make sure the . operator is present for the .com, .net or .us extensions
  //after the @ symbol.
  ATsign = "@";
  Period = ".";
  var cAT;
  var cPeriod;
  var nCNt = 0;
  var nCntr = 0;

  for (i = 0; i < sTmpStr.length; i++) 
  { 
    cAT = sTmpStr.charAt(i); 
    if (ATsign.indexOf(cAT) != -1)
    {
      nCNt++;
      //alert("@ sign found at pos [" + i + "+1]");
      //now find the . operator
      for (i = nCNt+1; i < sTmpStr.length; i++) 
      { 
        cPeriod = sTmpStr.charAt(i); 
        if (Period.indexOf(cPeriod) != -1) 
        {
           nCntr++;
           //alert(". found at pos [" + i + "+1]");
        }
      }
    }
  }

  //alert("counted [" + nCntr + "] periods ");
  //if (nCntr == 0 || nCntr > 1)
  if (nCntr == 0)
  {
    //alert("func isValidEmail(strEmail) = false (did not find any .)");
    return false;
  }

  //Check to see if the period position is less than 2 from end
  xPeriod = ".";
  var iCnt = 0;
  var cLoc;
  for (i = 0; i < sTmpStr.length; i++) 
  { 
    cLoc = sTmpStr.charAt(i); 
    if (xPeriod.indexOf(cLoc) != -1) 
    {
      iCnt = i;
    }
  }

  //alert(". found at pos [" + iCnt + "] + 3 <= len [" + sTmpStr.length + "]");
  if (iCnt+3 > strEmail.length)
  {
    //alert("func isValidEmail(strEmail) = false (. pos less than 2)");
    return false;
  }
return true;
}
// -----------------------------------------------------------------------
function Trim(inputString)
{
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string")
   {
     //alert("func Trim(inputString) - input was not a string");
     return false;
   }

   var retValue = inputString;
   var ch = retValue.substring(0, 1);

   while (ch == " ")
   {
      // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }

   ch = retValue.substring(retValue.length-1, retValue.length);

   while (ch == " ")
   { 
      // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }

   while (retValue.indexOf("  ") != -1)
   { 
     // Note that there are two spaces in the string - look for multiple spaces within the string
     // Again, there are two spaces in each of the strings
     retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length);
   }
   
   // Return the trimmed string back to the user
   return retValue; 
}
