// Trims leading and trailing spaces (only spaces, not other whitespace)
function trim(s)
{
  var str = s;
  var rtrimmed = str.replace(/[ ]*$/gi, "");
  var trimmed = rtrimmed.replace(/^[ ]*/gi, "");
  return trimmed;
}

// All digits in the string s numbers?
function isNumeric(s)
{
  return (s.search(/[^0|1|2|3|4|5|6|7|8|9]/) == -1);
}

function verifyCMA(f)
{
  var sellerTimeFrame  = f["SellerTimeFrame"].selectedIndex;
  var propertyCity     = trim(f["PropertyCity"].value); 
  var propertyAddress  = trim(f["PropertyAddress"].value); 
  var propertyState    = trim(f["PropertyState"].value); 
  var propertyZip      = trim(f["PropertyZip"].value); 
  var firstName = trim(f["FirstName"].value);
  var lastName = trim(f["LastName"].value);
  var email = trim(f["Email"].value);
  var timeToCall = trim(f["TimeToCall"].value); // not = "unselected";
  var dayPhonea     = trim(f["DayPhonea"].value);
  var dayPhoneb     = trim(f["DayPhoneb"].value);
  var dayPhonec     = trim(f["DayPhonec"].value);
  var eveningPhonea = trim(f["EveningPhonea"].value);
  var eveningPhoneb = trim(f["EveningPhoneb"].value);
  var eveningPhonec = trim(f["EveningPhonec"].value);

  if (propertyAddress.length == 0)
  {
    f["PropertyAddress"].focus();
    alert("Please enter the address (or legal description) of the property.");
    return false;
  }
  if (sellerTimeFrame == 0)
  {
    alert("Please enter your time frame by selecting an option from the dropdown.");
    f["TimeToCall"].focus();
    return false;
  }

  if (propertyCity.length == 0)
  {
    f["PropertyCity"].focus();
    alert("Please enter the city where the property is located");
    return false;
  }
  if (propertyState.length == 0)
  {
    f["PropertyCity"].focus();
    alert("Please enter the State where the property is located.");
    return false;
  }
  if (propertyZip.length == 0)
  {
    f["PropertyZip"].focus();
    alert("Please enter the zip code of the property");
    return false;
  }


  if (firstName.length == 0)
  {
    f["FirstName"].focus();
    alert("First name missing");
    return false;
  }

  if (lastName.length == 0)
  {
    f["LastName"].focus();
    alert("Last name missing");
    return false;
  }

  if (timeToCall == "unselected")
  {
    alert("Please enter the best time to call by selecting an option from the dropdown.");
    f["TimeToCall"].focus();
    return false;
  }
  
  if ((timeToCall == "Morning") || (timeToCall == "Afternoon"))
  {
    if ((dayPhonea.length != 3) || 
        (dayPhoneb.length != 3) || 
        (dayPhonec.length != 4) ||
        (! isNumeric(dayPhonea)) ||
        (! isNumeric(dayPhoneb)) || 
        (! isNumeric(dayPhonec)) )
    {
      alert("Day phone value missing or invalid.  Please enter a complete phone number including area code, or select a different call time.");
      f["DayPhonea"].focus();
      return false;
    }
  }

  else if (timeToCall == "Evening")
  {
    if ((eveningPhonea.length != 3) || 
      (eveningPhoneb.length != 3) || 
      (eveningPhonec.length != 4) ||
      (! isNumeric(eveningPhonea)) ||
      (! isNumeric(eveningPhoneb)) || 
      (! isNumeric(eveningPhonec)) )
    {
      alert("Evening phone value missing or invalid.  Please enter a complete phone number including area code, or select a different call time.");
      f["EveningPhonea"].focus();
      return false;
    }
  }

  if (email.indexOf("@") == -1 ||
    email.indexOf(".") == -1 ||
    email.indexOf(".") < email.indexOf("@"))
  {
    alert("Invalid e-mail address entered.  E-mail address should be in the form:  \"myusername@somewhere.com\" (no quotes)\.");
    f["Email"].focus();
    return false;
  }

  return true;
}


