//-----------------------------------------------------------
// 
// isEmail
//
// Minimal address: a@b.cd
//
//-----------------------------------------------------------
function isEmail(str)
{
   pattern = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9-])+(\.[a-zA-Z0-9_-]+)+$/;

   return pattern.test(str);
}

//-----------------------------------------------------------
// 
// isZip
//
// Minimal zipcode: 12345 OR 12345-6789
//
//-----------------------------------------------------------
function isZip(str)
{
   pattern = /(^\d{5}$)|(^\d{5}-\d{4}$)/;

   return pattern.test(str);
}

//-----------------------------------------------------------
// 
// isPhone
//
// Minimal phone: 111 2222
//
//-----------------------------------------------------------
function isPhone(str)
{
   str = str.replace(/([\(\)\-\.# ])/g, ""); // Remove letters, (, ), and -

   pattern = /(^\d{10,15}$)|(^\d{10,15}[a-zA-Z]+\d+$)/;

   return pattern.test(str);
}

//-----------------------------------------------------------
// 
// isComment
//
// Minimal address: a@b.cd
//
//-----------------------------------------------------------
function isComment(str)
{
   pattern = /(https?)/;

   ret = pattern.test(str);
   
   if (ret)
      return(false);
      
   return(true);
}

//-----------------------------------------------------------
// 
// verify
//
//-----------------------------------------------------------
function verify(passForm)
{
   var error = 0;
   var msg;
   var str;
   var idx;

   //----------------------------------------------------
   // Required fields used when displaying error messages
   //----------------------------------------------------
   var reqFields = new Array 
   (
      "name, Name",
      "email, Email Address",
      "answer, Security Answer",
      "EOL"
   );

   msg = "\nThe form cannot be submitted because:\n\n";
   
   for (var i = 0; reqFields[i] != "EOL"; i++)
   {
      field_arr = reqFields[i].split(", ");
      field = field_arr[0];
      str = field_arr[1];
      
      value = passForm.elements[field].value;
      
      // Check if any required text fields are empty
      if (value.length == 0)
      {
         msg+= " - the " + str + " field is empty.\n";
         error++;
      }
   }

   //------------------------------------
   // Check if zipcode is valid
   //------------------------------------
   str = passForm.elements["zip"].value;
   if (str.length > 0 && !isZip(str))
   {
      msg+= " - the Zipcode is invalid. Please enter the zip code in the form: XXXXX or XXXXX-XXXX.\n";
      error++;
   }

   //------------------------------------
   // Check if email address is valid
   //------------------------------------
   str = passForm.elements["email"].value;
   if (str.length > 0 && !isEmail(str))
   {
      msg+= " - the E-mail Address is invalid. Please enter your e-mail address in the form: name@host.com.\n";
      error++;
   }

   //------------------------------------
   // Check if phone is valid
   //------------------------------------
   str = passForm.elements["phone"].value;
   if (str.length > 0 && !isPhone(str))
   {
      msg+= " - the Phone Number is invalid. Please enter your phone number including the area code.\n";
      error++;
   }

   //------------------------------------
   // Check if Comments is valid
   //------------------------------------
   str = passForm.elements["comments"].value;
   if (str.length > 0 && !isComment(str))
   {
      msg+= " - the Comments field is invalid. No URL's are allowed.\n";
      error++;
   }

   // Display error message
   if (error)
   {
      msg+="\nPlease correct the error(s) and resubmit.";
      alert(msg);
      return(false);
   }
}
