var currEmailInvalid="Current e-mail entered is in an invalid format. Please re-enter."
var newEmailReq="Please enter your new e-mail address."
var newEmailMatches="New e-mail matches old email. Please re-enter."
var newEmailInvalid="New e-mail entered is in an invalid format. Please re-enter."
var confEmailReq="Confirmation e-mail required. Please enter."
var confEmailMismatch="Confirmation e-mail address must match new e-mail address. Please re-enter."
var confEmailInvalid="Confirmation e-mail is in an invalid format. Please re-enter."

function isValidEmail(fieldName) {
  var index = getIndex(fieldName);
  var emailString = document.webform.elements[index].value.rtrim();
 
  /*  Checks for email in user@domain format and also separates the username from the domain. */
  var emailPattern=/^(.+)@(.+)$/;
  /*  Pattern for matching all special characters which include ( ) < > @ , ; : \ " * . [ ]  */
  var specialChars="\\(\\)<>#$%^=|&{}`!*~@,;:\\\\\\\"\\.\\[\\]";
  /*  Characters not allowed in a username or domain name. */
  var validChars="\[^\\s" + specialChars + "\]";
  /*  Characters allowed as the first character in a valid username or domain.  */
  var firstChars=validChars;
  /*  Check for "user", when username is a quoted string. E.g. "jiminy cricket"@disney.com */
  var quotedUser="(\"[^\"]*\")";
  /*  Patterns for domains that are IP addresses . */
  var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
  /*  The following string represents an atom (basically a series of non-special characters.) */
  var atom="(" + firstChars + validChars + "*" + ")";
  /*  The following string represents one word in the typical username.  */
  var word="(" + atom + "|" + quotedUser + ")";
  /*  The following pattern describes the structure of the user */
  var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
  /*  Pattern for normal symbolic domain, as opposed to ipDomainPat. */
  var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
  /*  The course pattern to break up user@domain into different pieces . */
  var matchArray=emailString.match(emailPattern);
  if (emailString.length==0)  {
  //  callAlert(index,msg1014);
    return false;
  }
  if (matchArray==null) {
  /*  to alert if the address doesn't even fit the general mould of a valid e-mail address. */
   // callAlert(index,msg);
    return false;
  }
  var user=matchArray[1];
  var domain=matchArray[2];
    //  To check if "user" is valid
  if (user.match(userPat)==null) {
    //  user is not valid
   // callAlert(index,msg);
    return false;
  }
  /*  Check for valid IP address. */
  var IPArray=domain.match(ipDomainPat)
  if (IPArray!=null) {
    // this is an IP address
    for (var i=1;i<=4;i++) {
      if (IPArray[i]>255) {

  //  callAlert(index,msg);
    return false;
    }
    }
    return true;
  }
  // Domain is symbolic name
  var domainArray=domain.match(domainPat)
  if (domainArray==null) {
//  callAlert(index,msg);
    return false;
  }
  /*  Check for valid domain name. */
  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>4) {
     // the address must end in a two letter or three or four letter word.
   //  callAlert(index,msg);
    return false;
  }

    /*  Check for valid host name, if address just ends in .com, .gov, etc.*/
  if (domArr[domArr.length-1].length==3 && len<2) {
    var errStr="This address is missing a hostname!";
  //  callAlert(index,msg);
    return false;
  }
  //  If everything is valid!
  return true;
}

var submitted = false;

function validateForm()
{
	if(!submitted){
		
  if(document.webform.newEmail.value == "" && document.webform.confirmEmail.value != "")
  {
    alert(newEmailReq);
    return false;
  }
  else if (!isValidEmail("newEmail") && document.webform.newEmail.value != "")
  {
    alert(newEmailInvalid);
    return false;
  }
  
  if(document.webform.confirmEmail.value == "" && document.webform.newEmail.value != "")
  {
    alert(confEmailReq);
    return false;
  }
  else if (!isValidEmail("confirmEmail") && document.webform.confirmEmail.value != "")
  {
    alert(confEmailInvalid);
    return false;
  }
  else if (document.webform.newEmail.value.rtrim() != document.webform.confirmEmail.value.rtrim())
  {
    alert(confEmailMismatch);
    return false;
  }

  if(document.getElementById('submitbutton'))
  {
    document.getElementById('submitbutton').disabled = true;
  }
  submitted = true;
  return true;
  
	}else {
		alert("Your request is being processed. Please wait.");
		return false;
	}
  
}

function getIndex(fieldName){
  for(i=0; i< document.webform.length;i++){
    if(fieldName == document.webform.elements[i].name)
      return i;		
  }
}

String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
