//
// Description: Check form email (or null) and list of required fields before form submit is allowed
// Version: 1.0
// Date: 27Aug2004
//
//  1.0  27Aug2004  Initial writing
//  1.1  20Sep2004  Altered the call to make the first parameter the email object and the rest required field objects
//                  If there is no email field then send the string 'null' for the first parameter
//

function CheckFields()
{
  // Define a list of arguments and an argument count
  var args = CheckFields.arguments;
  var argc = CheckFields.arguments.length;

  // Check that all given arguments (except the email or first argument) are not blank here
  for (var i = 1; i < argc; i++)
  {
    if (args[i].value == "")
    {
      alert("Please enter a value for " + args[i].name + " and then re-submit this form.");
      return false;
    }
  }

  // No email in this form - then return true
  if(args[0].value == "null")
  {
    return true;
  }

  // Get the value of the email string
  var emailStr = args[0].value;

  // Validate the email address entered

  var badChars='#$%^&*()[]{}:;<>/?+=~,!|\\" ';
  var badFound='';

  for(i=0;i<emailStr.length;i++)
  {
    for(j=0;j<badChars.length;j++)
    {
      if(emailStr.charAt(i)==badChars.charAt(j))
      {
        badFound += emailStr.charAt(i);
      }
    }
  }
  if(badFound != '')
  {
    alert('Please do not include "' + badFound + '" in the email address.');
    return false;
  }

  var emailPat=/^(.+)@(.+)$/;
  var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
  var validChars="\[^\\s" + specialChars + "\]";
  var quotedUser="(\"[^\"]*\")";
  var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
  var atom=validChars + '+';
  var word="(" + atom + "|" + quotedUser + ")";
  var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
  var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
  var matchArray=emailStr.match(emailPat);

  if (matchArray==null)
  {
    alert("Email address seems incorrect (check @ and .'s)");
    return false;
  }

  var user=matchArray[1];
  var domain=matchArray[2];

  if (user.match(userPat)==null)
  {
    alert("The username doesn't seem to be valid.");
    return false;
  }

  var IPArray=domain.match(ipDomainPat);

  if (IPArray!=null)
  {
    for (var i=1;i<=4;i++)
    {
      if (IPArray[i]>255)
      {
        alert("Destination IP address is invalid!");
        return false;
      }
    }
    return true;
  }

  var domainArray=domain.match(domainPat);

  if (domainArray==null)
  {
    alert("The domain name doesn't seem to be valid.");
    return false;
  }

  var atomPat=new RegExp(atom,"g");
  var domArr=domain.match(atomPat);
  var len=domArr.length;

  if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3)
  {
    alert("The address must end in a three-letter domain, or two letter country.");
    return false;
  }
  if (len<2)
  {
    var errStr="This address is missing a hostname!";
    alert(errStr);
    return false;
  }

  return true;
}

// End

