/*
This JavaScript file written by: Jon Penfield
Written for: TLC Caregivers Website:
  (http://www.tlccaregivers.com)
This file located at:
  (http://www.tlccaregivers.com/images/java.js)
*/

//Confirms the guest wants to reset the contact forms.
function restart()
{
  var choice = window.confirm("Are you sure you want to reset the form?")
  if (choice)
  {
    return true;
  }
    else
    {
      return false;
    }
}

//Validates the form.
function form_val()
{
  //Requires the first name field.
  if (document.contact.First.value == "")
  {
    alert("A first name is required.");
    document.contact.First.focus();
    return false;
  }

  //Requires the last name field.
  if (document.contact.Last.value == "")
  {
    alert("A last name is required.");
    document.contact.Last.focus();
    return false;
  }

  //Verfies that the telephone number is not less than 7 or greater than 10.
  if (document.contact.Telephone.value != "")
  {
    if (document.contact.Telephone.value.length < 7)
    {
      alert("Phone number must have a minimum of 7 numbers.");
      document.contact.Telephone.focus();
      document.contact.Telephone.select();
      return false;
    }

    if (document.contact.Telephone.value.length > 10)
    {
      alert("Phone number cannot exceed 10 numbers.");
      document.contact.Telephone.focus();
      document.contact.Telephone.select();
      return false;
    }
  }

  //Verfies that the zip code is not less than 5 or greater than 9.
  if (document.contact.Zip.value != "")
  {
    if (document.contact.Zip.value.length < 5)
    {
      alert("Zip code must have a minimum of 5 numbers.");
      document.contact.Zip.focus();
      document.contact.Zip.select();
      return false;
    }

    if (document.contact.Zip.value.length > 9)
    {
      alert("Zip code cannot exceed 9 numbers.");
      document.contact.Zip.focus();
      document.contact.Zip.select();
      return false;
    }
  }

  //Requires that all address fields are completed if the Brochure checkbox is checked
  if (document.contact.Brochure.checked)
  {
    //Requires the Address field.
    if (document.contact.Address.value == "")
    {
      alert("To recieve the brochure, an address is required.");
      document.contact.Address.focus();
      return false;
    }

    //Requires the City field.
    if (document.contact.City.value == "")
    {
      alert("To recieve the brochure, a city is required.");
      document.contact.City.focus();
      return false;
    }

    //Requires State.
    if (document.contact.State.value == "")
    {
      alert("To recieve the brochure, a state is required.");
      document.contact.State.focus();
      return false;
    }

    //Requires the Zip code.
    if (document.contact.Zip.value == "")
    {
      alert("To recieve the brochure, a zip code is required.");
      document.contact.Zip.focus();
      return false;
    }
  }

  //Requires a valid email address.
  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.contact.Email.value))
  {
    return true;
  }
    else
    {
      alert("E-mail must contain a valid e-mail address.");
      document.contact.Email.focus();
      document.contact.Email.select();
      return false;
    }
}

//Requires a valid zip code
function valzip()
{
  var valclass="0123456789-"
  var ok = "yes";
  var temp;
  for (var i=0; i<document.contact.Zip.value.length; i++)
  {
    temp = "" + document.contact.Zip.value.substring(i, i+1);
    if (valclass.indexOf(temp) == "-1") ok = "no";
  }
  if (ok == "no")
  {
    alert("Zip code can only contain numbers, no letters or symbols.");
    document.contact.Zip.focus();
    document.contact.Zip.select();
  }
}

//Requires a valid phone number
function valphone()
{
  var valclass="0123456789"
  var ok = "yes";
  var temp;
  for (var i=0; i<document.contact.Telephone.value.length; i++)
  {
    temp = "" + document.contact.Telephone.value.substring(i, i+1);
    if (valclass.indexOf(temp) == "-1") ok = "no";
  }
  if (ok == "no")
  {
    alert("Phone number can only contain numbers, no letters or symbols.");
    document.contact.Telephone.focus();
    document.contact.Telephone.select();
  }
}

