var err;

function checkForm(formid)
{
  err = true;

  if (formid.name == 'registerform')
  {
    if (formid.confirm.checked == false)
    {
      error('You must indicate that you have read and agree to the Terms and Conditions.');
      formid.confirm.focus();
    }
    else if (formid.number_of_memberships.value.length < 1)
    {
      error('You haven\'t specified the required number of memberships.');
      formid.number_of_memberships.focus();
      formid.number_of_memberships.select();
    }
    else if (isNaN(formid.number_of_memberships.value))
    {
      error('You must specify the required number of memberships as a number.');
      formid.number_of_memberships.focus();
      formid.number_of_memberships.select();
    }
    else if (formid.contact_name.value.length < 1)
    {
      error('You haven\'t provided a contact name.');
      formid.contact_name.focus();
      formid.contact_name.select();
    }
    else if ((formid.contact_phone.value.length < 1) && (formid.email_address.value.length < 1))
    {
      error('You haven\'t provided a contact phone number or email address.');
      formid.contact_phone.focus();
      formid.contact_phone.select();
    }
    else if (formid.shirt_size.value.length < 1)
    {
      error('You haven\'t provided a shirt size.');
      formid.shirt_size.focus();
      formid.shirt_size.select();
    }
    else if ((formid.address_street.value.length < 1) || (formid.address_city.value.length < 1))
    {
      error('You haven\'t provided a delivery address for the shirt, cap, and certificate.');
      formid.address_street.focus();
      formid.address_street.select();
    }

    if (err && (formid.email_address.value.length > 0))
    {
      if (formid.email_address.value.length < 5)
      {
        error('The email address you have entered is not a valid email address.');
        formid.email_address.focus();
        formid.email_address.select();
      }
      else if ((formid.email_address.value.indexOf('@') == -1) || (formid.email_address.value.indexOf('.') == -1))
      {
        error('The email address you have entered is not a valid email address.');
        formid.email_address.focus();
        formid.email_address.select();
      }
      else if (formid.email_address.value.indexOf('@') > formid.email_address.value.lastIndexOf('.'))
      {
        error('The email address you have entered is not a valid email address.');
        formid.email_address.focus();
        formid.email_address.select();
      }
      else if (stringcontains(formid.email_address.value,'()[]<>;:," ') == true)
      {
        error('The email address you have entered is not a valid email address.');
        formid.email_address.focus();
        formid.email_address.select();
      }
    }
  }
  else if (formid.name == 'tournament')
  {
/*
    if (formid.confirm.checked == false)
    {
      error('You must indicate that you have read and agree to the Terms and Conditions.');
      formid.confirm.focus();
    }
    else 
    if (formid.number_of_players.value.length < 1)
    {
      error('You haven\'t specified the number of players.');
      formid.number_of_players.focus();
      formid.number_of_players.select();
    }
    else if (isNaN(formid.number_of_players.value))
    {
      error('You must specify the number of players as a number (ie. not in words).');
      formid.number_of_players.focus();
      formid.number_of_players.select();
    }
    else if (formid.number_of_players.value > 30)
    {
      error('No more than 30 players may be included in a single submission.');
      formid.number_of_players.focus();
      formid.number_of_players.select();
    }
    else 
*/
    if (formid.contact_name.value.length < 1)
    {
      error('You haven\'t provided a contact name.');
      formid.contact_name.focus();
      formid.contact_name.select();
    }
    else if ((formid.contact_phone.value.length < 1) && (formid.email_address.value.length < 1))
    {
      error('You haven\'t provided a contact phone number or email address.');
      formid.contact_phone.focus();
      formid.contact_phone.select();
    }
    else if (formid.transport.value.length < 1)
    {
      error('You haven\'t specified a mode of transport.');
      formid.transport.focus();
      formid.transport.select();
    }
    else if (!(formid.ceduna_dinner.checked || formid.tournament.checked || formid.kalgoorlie_dinner.checked))
    {
      error('You haven\'t indicated which functions you will be attending.');
      formid.tournament.focus();
      formid.tournament.select();
    }

    if (err && (formid.email_address.value.length > 0))
    {
      if (formid.email_address.value.length < 5)
      {
        error('The email address you have entered is not a valid email address.');
        formid.email_address.focus();
        formid.email_address.select();
      }
      else if ((formid.email_address.value.indexOf('@') == -1) || (formid.email_address.value.indexOf('.') == -1))
      {
        error('The email address you have entered is not a valid email address.');
        formid.email_address.focus();
        formid.email_address.select();
      }
      else if (formid.email_address.value.indexOf('@') > formid.email_address.value.lastIndexOf('.'))
      {
        error('The email address you have entered is not a valid email address.');
        formid.email_address.focus();
        formid.email_address.select();
      }
      else if (stringcontains(formid.email_address.value,'()[]<>;:," ') == true)
      {
        error('The email address you have entered is not a valid email address.');
        formid.email_address.focus();
        formid.email_address.select();
      }
    }
  }
  else if (formid.name == 'joinform')
  {
    if (formid.confirm.checked == false)
    {
      error('You must indicate that you have read and agree to the Terms and Conditions.');
      formid.confirm.focus();
    }
    else if (formid.Name_2.value.length < 1)
    {
      error('You haven\'t provided a contact name.');
      formid.Name_2.focus();
      formid.Name_2.select();
    }
    else if (formid.Shirt_size_4.value.length < 1)
    {
      error('You haven\'t provided a shirt size.');
      formid.Shirt_size_4.focus();
      formid.Shirt_size_4.select();
    }
/*
    else if (formid.Number_of_memberships_5.value.length < 1)
    {
      error('You haven\'t specified a number of memberships.');
      formid.Number_of_memberships_5.focus();
      formid.Number_of_memberships_5.select();
    }
*/
    else if (formid.Email_3.value.length < 1)
    {
      error('You haven\'t provided an email address.');
      formid.Email_3.focus();
      formid.Email_3.select();
    }

    if (err && (formid.Email_3.value.length > 0))
    {
      if (formid.Email_3.value.length < 5)
      {
        error('The email address you have entered is not a valid email address.');
        formid.Email_3.focus();
        formid.Email_3.select();
      }
      else if ((formid.Email_3.value.indexOf('@') == -1) || (formid.Email_3.value.indexOf('.') == -1))
      {
        error('The email address you have entered is not a valid email address.');
        formid.Email_3.focus();
        formid.Email_3.select();
      }
      else if (formid.Email_3.value.indexOf('@') > formid.Email_3.value.lastIndexOf('.'))
      {
        error('The email address you have entered is not a valid email address.');
        formid.Email_3.focus();
        formid.Email_3.select();
      }
      else if (stringcontains(formid.Email_3.value,'()[]<>;:," ') == true)
      {
        error('The email address you have entered is not a valid email address.');
        formid.Email_3.focus();
        formid.Email_3.select();
      }
    }
  }

  if (err)
  {
    err = window.confirm('Are you sure you have entered everything correctly?');
  }

  return err;
}

function stringcontains(haystack,needles)
{
  var result;
  result = false;

  for (i=0;i<needles.length;i++)
  {
    if (haystack.indexOf(needles.charAt(i))>0)
    {
      result = true;
    }
  }

  return result;
}

function error(txt)
{
  err = false;
  window.alert(txt);
}

