/* 
 * Author: Isaac Rowntree (Zack Design)
 * URL: http://www.zackdesign.biz
 * Email: isaac@zackdesign.biz
 * Date: 26/04/2005
 * 
 * Description: This form checker validates the obtain-a-quote (index.php) and making-a-claim (how-to-apply.php) forms.
 * 
 */
 
function vQuote()
{
  var bool = true;
  var counter = 0;
    
  // Test for applicant's name and daytime phone number as well as their email.
  counter = both (document.quote.applicantname.value, document.quote.phonedaytime.value, document.quote.email.value);
  if (counter == 1)
  {
    bool = false;
  }
  
  return bool;
}

function vApply()
{
  var bool = true;
  var counter = 0;
  
  // Test for applicant's name and daytime phone number as well as their email
  counter = both (document.apply.applicantname.value, document.apply.phonedaytime.value, document.apply.email.value);
  if (counter == 1)
  {
    bool = false;
  }
  
  // Test that the first address line has been filled in
  member = document.apply.membernum.value;
  if (member == "")
  {
    if (counter == 0)
    {
      counter++;
      alert("You must fill in your AAPB membership number.");
      bool = false;
    }
  }
  
  // Test that the first address line has been filled in
  addr = document.apply.addressline1.value;
  if (addr == "")
  {
    if (counter == 0)
    {
      counter++;
      alert("You must fill in the first address line.");
      bool = false;
    }
  }
  
  // Test for suburb
  suburb = document.apply.suburb.value;
  if (suburb == "")
  {
    if (counter == 0)
    {
      counter++;
      alert("You must fill in your suburb.");
      bool = false;
    }
  }
  
  // Test for state
  state = document.apply.state.value;
  if (state == "")
  {
    if (counter == 0)
    {
      counter++;
      alert("You must fill in your state.");
      bool = false;
    }
  }
  
  // Test for post code
  postcode = document.apply.postcode.value;
  if (postcode == "")
  {
    if (counter == 0)
    {
      counter++;
      alert("You must fill in your postcode.");
      bool = false;
    }
  }
  
  // Test to make sure the policies are agreed to
  if (counter == 0)
  {
    if ((!document.apply.profindem.checked) || (!document.apply.publicliability.checked)) 
    {
      alert("You must have read and understood both policies before continuing.");
      counter++
      bool = false;
    }
  }
  
  return bool;
}

function both (name, number, email)
{
  // Check the email address for correct formatting
  pos = email.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/);

  if (name == "")
  {
    alert("You forgot to fill in your name");
    return 1;
  }
  else if (number == "")
  {
    alert("You must provide a daytime contact number");
    return 1;
  }
  else if (email == "")
  {
    alert("You must provide your email address");
    return 1;
  }
  else if  (pos)
  {
    alert("The email address may only have these characters: a-z, A-Z, @, period, underscore. It may not begin with an @ or a period.");
    return 1;
  }
  else
  {
    return 0;
  }
}
