/*
 *-----------------------------------------------------------------------------
 * File		: ValidateEmail.js
 * Author      	: Andy Millard
 * Created     	: January 2001
 *-----------------------------------------------------------------------------
 * Description 	: validate that email address entered in a form is valid, 
 *		  or at least conforms to the general facts about an email address
 *-----------------------------------------------------------------------------
 * Functions   	: funcValidEmail()
 *-----------------------------------------------------------------------------
 */

/*
 *------------------------------------------------------
 * Name     	: funcValidEmail()
 * Author   	: Matt Neild (original)
 * Amended  	: Andy Millard - added extra parameters,
 *       	: and modified to allow single function to
 *       	: be used across whole site
 * Created  	: December 2000
 *------------------------------------------------------
 * Inputs   	: EmailAddress (name of field on form),
 *       	: FormName (name of form being checked),
 *       	: DoAlert (1/0 decides whether error message
 *       	: is called from this function, or its parent)
 * Process  	: Find the actual value of the email field,
 *       	: and check that has all the standard
 *       	: characteristics of an email address
 *       	: if not, either return false (if DoAlert is 0)
 *       	: or create an alert and return false
 * Outputs  	: true/false
 *------------------------------------------------------
 * Modifications:
 * Name		: Andy Millard, 3rd September 2001
 * Desc		: Changed the email validtion to allow
 *		  2, 3 or 4 characters at the end (for new
 *		  top level domains)
 *------------------------------------------------------
 */
function funcValidEmail(EmailAddress, FormName, DoAlert)
{
var str = document.forms[FormName].elements[EmailAddress].value;
var atsign = str.indexOf('@') // get position of @ sign in string
var dot = str.lastIndexOf('.');
var emailCheck = /^([a-zA-Z0-9\-\.\_]+)\@([a-zA-Z0-9\-\.\_]+)\.([a-zA-Z]{2,4})$/;
var result = str.match(emailCheck);
if ((!result) || ((atsign < 1) ||                    // '@' cannot be in first position
    (dot <= atsign + 1) ||             // Must be at least one valid char btwn '@' and '.'
    (str.charAt(dot - 1) == '.') ||   // Two dots can not appear in consecutive positions
    (dot == (str.length - 1)) ||       // Must be at least one valid char after '.'
    (str.indexOf(' ')  != -1) ||       // No empty spaces permitted
    (str.indexOf(',')  != -1) ||       // No commas permitted
    (str.indexOf('&quot;')  != -1) ||  // No double quotes permitted
    (str.indexOf('\'')  != -1)))        // No single quotes permitted
  {
  if (DoAlert)
  {
   alert('Please enter a valid email address');
   document.forms[FormName].elements[EmailAddress].focus();
  }
  return false;
}
return true;
}
