// ==================
// Updated 11/30/2011
// ==================

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 buyer's name (first and last name).");
    form.fullname.focus();
    form.fullname.select();
    return false;		
  }

  form.addr1.value = Trim(form.addr1.value);
  if (IsEmpty(form.addr1.value))
  {
    alert("Please enter a valid buyer's current address.");
    form.addr1.focus();
    form.addr1.select();
    return false;		
  }

  form.city.value = Trim(form.city.value);
  if (IsEmpty(form.city.value) || !IsCharacters(form.city.value))
  {
    alert("Please enter a valid buyer's current 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 buyer's current 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 buyer's current 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, [2]=Cell
  if (form.rad_buy_phone[0].checked)
  {
    // Home Phone Selected
    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 buyer's 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 buyer's 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 buyer's home telephone suffix.");
      form.Hpsuffix.focus();
      form.Hpsuffix.select();
      return false;		
    }
  }
  else if (form.rad_buy_phone[1].checked)
  {
    // Work Phone Selected
    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 buyer's 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 buyer's 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 buyer's work telephone suffix.");
      form.Wpsuffix.focus();
      form.Wpsuffix.select();
      return false;		
    }
  }
  else
  {
    // Cell Phone Selected
    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 buyer's 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 buyer's 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 buyer's 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 buyer's email address.");
    form.email.focus();
    form.email.select();
    return false;		
  }

  //=====================================================
  // Property Information
  //=====================================================
  form.propaddr1.value = Trim(form.propaddr1.value);
  if (IsEmpty(form.propaddr1.value))
  {
    alert("Please enter a valid property address.");
    form.propaddr1.focus();
    form.propaddr1.select();
    return false;		
  }

  form.propcity.value = Trim(form.propcity.value);
  if (IsEmpty(form.propcity.value) || !IsCharacters(form.propcity.value))
  {
    alert("Please enter a valid property city.");
    form.propcity.focus();
    form.propcity.select();
    return false;		
  }
  
  form.propzip.value = Trim(form.propzip.value);
  txt = String(form.propzip.value);
  if (IsEmpty(form.propzip.value) || !IsNumeric(form.propzip.value) || txt.length < 5)
  {
    alert("Please enter a valid property zip code of 5 digits.");
    form.propzip.focus();
    form.propzip.select();
    return false;		
  }

  if (form.structtype.selectedIndex == 0)
  {
    alert("Please enter a valid property type.");
    form.structtype.focus();
    return false;		
  }

  form.size.value = Trim(form.size.value);
  if (IsEmpty(form.size.value) || !IsNumeric(form.size.value))
  {
    alert("Please enter a valid square footage of the property.");
    form.size.focus();
    form.size.select();
    return false;		
  }

  if (form.stories.selectedIndex == 0)
  {
    alert("Please enter a valid number of stories for the property.");
    form.stories.focus();
    return false;		
  }

  form.year.value = Trim(form.year.value);
  txt = String(form.year.value);
  if (IsEmpty(form.year.value) || !IsNumeric(form.year.value) || txt.length != 4)
  {
    alert("Please enter a valid year the property was built.");
    form.year.focus();
    form.year.select();
    return false;		
  }

  if (form.garage.selectedIndex == 0)
  {
    alert("Please enter a valid number of car(s) garage for the property.");
    form.garage.focus();
    return false;		
  }

  //=====================================================
  // Buyer's Agent Information
  //=====================================================
  form.buyagentfullname.value = Trim(form.buyagentfullname.value); 
  if (IsEmpty(form.buyagentfullname.value) || !IsCharacters(form.buyagentfullname.value))
  {
    alert("Please enter a valid buyer's agent name (first and last name).");
    form.buyagentfullname.focus();
    form.buyagentfullname.select();
    return false;		
  }

  form.buyagentemail.value = Trim(form.buyagentemail.value);
  if (!IsEmpty(form.buyagentemail.value))
  {
    if (!isValidEmail(form.buyagentemail.value))
    {       
      alert("Please enter a valid buyer's agent email address.");
      form.buyagentemail.focus();
      form.buyagentemail.select();
      return false;
    }		
  }

  // Figure out what the buyer's agent selected for "Preferred Contact"
  // Radio Buttons:  [0]=Cell, [1]=Work
  if (form.ba_phone[0].checked)
  {
    // Cell Phone Selected
    form.BACparea.value = Trim(form.BACparea.value);
    form.BACprefix.value = Trim(form.BACprefix.value);
    form.BACpsuffix.value = Trim(form.BACpsuffix.value);
    if (!IsEmpty(form.BACparea.value) || !IsEmpty(form.BACprefix.value) || !IsEmpty(form.BACpsuffix.value))
    {
      txt = String(form.BACparea.value);
      if (!IsNumeric(form.BACparea.value) || txt.length < 3)
      {
        alert("Please enter a valid buyer's agent cell telephone area code.");
        form.BACparea.focus();
        form.BACparea.select();
        return false;		
      }

      txt = String(form.BACprefix.value);
      if (!IsNumeric(form.BACprefix.value) || txt.length < 3)
      {
        alert("Please enter a valid buyer's agent cell telephone prefix.");
        form.BACprefix.focus();
        form.BACprefix.select();
        return false;		
      }

      txt = String(form.BACpsuffix.value);
      if (!IsNumeric(form.BACpsuffix.value) || txt.length < 4)
      {
        alert("Please enter a valid buyer's agent cell telephone suffix.");
        form.BACpsuffix.focus();
        form.BACpsuffix.select();
        return false;		
      }
    }
  }  
  else
  {
    // Work Phone Selected
    form.BAWparea.value = Trim(form.BAWparea.value);
    form.BAWprefix.value = Trim(form.BAWprefix.value);
    form.BAWpsuffix.value = Trim(form.BAWpsuffix.value);
    if (!IsEmpty(form.BAWparea.value) || !IsEmpty(form.BAWprefix.value) || !IsEmpty(form.BAWpsuffix.value))
    {
      txt = String(form.BAWparea.value);
      if (!IsNumeric(form.BAWparea.value) || txt.length < 3)
      {
        alert("Please enter a valid buyer's agent work telephone area code.");
        form.BAWparea.focus();
        form.BAWparea.select();
        return false;		
      }

      txt = String(form.BAWprefix.value);
      if (!IsNumeric(form.BAWprefix.value) || txt.length < 3)
      {
        alert("Please enter a valid buyer's agent work telephone prefix.");
        form.BAWprefix.focus();
        form.BAWprefix.select();
        return false;		
      }

      txt = String(form.BAWpsuffix.value);
      if (!IsNumeric(form.BAWpsuffix.value) || txt.length < 4)
      {
        alert("Please enter a valid buyer's agent work telephone suffix.");
        form.BAWpsuffix.focus();
        form.BAWpsuffix.select();
        return false;		
      }
    }  
  }
  
  //=====================================================
  // Seller's Agent Information
  //=====================================================
  form.sellagentemail.value = Trim(form.sellagentemail.value);
  if (!IsEmpty(form.sellagentemail.value))
  {
    if (!isValidEmail(form.sellagentemail.value))
    {       
      alert("Please enter a valid seller's agent email address.");
      form.sellagentemail.focus();
      form.sellagentemail.select();
      return false;
    }		
  }

  //=====================================================
  // Inspection Information
  //=====================================================
  if (form.inspmonth.selectedIndex == 0)
  {
    alert("Please enter a valid inspection month.");
    form.inspmonth.focus();
    return false;		
  }

  if (form.inspday.selectedIndex == 0)
  {
    alert("Please enter a valid inspection day.");
    form.inspday.focus();
    return false;		
  }

  if (form.insptime.selectedIndex == 0)
  {
    alert("Please enter a valid inspection time.");
    form.insptime.focus();
    return false;		
  }

  if (form.insptype.selectedIndex == 0)
  {
    alert("Please enter a valid inspection type.");
    form.insptype.focus();
    return false;		
  }

  if (form.readdisclaimer.checked == false)
  {
    alert("Please read the LIMITATION & DISCLAIMERS and click the checkbox to continue.");
    form.readdisclaimer.focus();
    return false;		
  }

  return true;
};

// -----------------------------------------------------------------------
function ValidReport(form)
{
  form.username.value = Trim(form.username.value); 
  if (IsEmpty(form.username.value))
  {
    alert("Please enter a valid user name.");
    form.username.focus();
    return false;		
  }

  form.password.value = Trim(form.password.value);
  if (IsEmpty(form.password.value))
  {
    alert("Please enter a valid password.");
    form.password.focus();
    return false;		
  }
  
  var nStrg = null;
  nStrg = "ftp://" + form.username.value + ":" + form.password.value + "@wafenterprises.com";
  window.open(nStrg,"Report Download");
  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;
};

// -----------------------------------------------------------------------
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;
};

// -----------------------------------------------------------------------
// 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 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; 
};


