/* ===================================================================
** File:  petition.js
**
** Copyright © 2000 ICAMSR
**
** Validate form fields before submitting to CGI script.
**
** ===================================================================

/* -------------------------------------------------------------------
** Function:  isValidEmail(email)
**
** Validates the email address.  Rules applied:
** - email address must be alphanumeric ("_" and "-" allowed)
** - only one "@" symbol is allowed
** - prefix (after the last ".") must be 1 to 3 in length.
**
** Returns:  true if valid, otherwise false.
** ------------------------------------------------------------------- */
function isValidEmail(email)
{
   // Check for valid email. But can't check
   // email suffix in pattern match.
   var emailPattern = /[\w\-\.]+@[\w\-\.]+\.[\w\-]+/;
   var result = email.match(emailPattern);
   if (result == null)
      return false;
   if (result[0].length != email.length)
      return false;

   // patterm match can't test for double period!
   if (email.indexOf("..") >= 0)
      return false;

   // make sure the email suffix exists and is no
   // more than 3 characters.
   var j = email.length - email.lastIndexOf(".");
   if (j < 2 || j > 4)
      return false;

   return true;
}


/* -------------------------------------------------------------------
** Function:  checkFields(form)
**
** Check all required fields. Email address must be valid and all
** other fields must be non-blank.
**
** Returns:  true if all fields pass, otherwise false.
** ------------------------------------------------------------------- */
function checkForm(form)
{
   var realname   = form.realname.value;
   var email      = form.email.value;
   var address    = form.address.value;
   var city       = form.city.value;
   var stateprov  = form.stateprov.value;
   var country    = form.country.value;
   var postalzip  = form.postalzip.value;
   var occupation = form.occupation.value;
   
   if (parseInt(navigator.appVersion) >= 3)
   {
      if (realname.length == 0)
      {
         alert("Please enter your name.");
         form.realname.focus();
         return false;
      }
      else if (!isValidEmail(email))
      {
         alert("Please enter a valid e-mail address");
         form.email.focus();
         return false;
      }
      else if (address.length == 0)
      {
         alert("Please enter your address.");
         form.address.focus();        
         return false;
      }
      else if (city.length == 0)
      {
         alert("Please enter your city.");
         form.city.focus();        
         return false;
      }
      else if (stateprov.length == 0)
      {
         alert("Please enter your province/state.");
         form.stateprov.focus();        
         return false;
      }
      else if (country.length == 0)
      {
         alert("Please enter your country.");
         form.country.focus();        
         return false;
      }
      else if (postalzip.length == 0)
      {
         alert("Please enter your postal/zip code.");
         form.postalzip.focus();        
         return false;
      }
      else if (occupation.length == 0)
      {
         alert("Please enter your occupation.");
         form.occupation.focus();        
         return false;
      }
      else
         return true;
   }
}


/* -------------------------------------------------------------------
** Function:  resetForm(form)
**
** Clears the form and changes the focus to the first field in the
** form.
**
** Returns:  False to prevent submission to the CGI program.
** ------------------------------------------------------------------- */
function resetForm(form)
{
   form.reset();
   form.realname.focus();
   return false;
}

/* ====================== End Of File ================================ */