/************************************
  E-mail-validation script v1.3

  Subsero A/S (subsero.dk)
*************************************

v1.3 (Torben Rohde, 13/12-2007)
-------------------------------
Added an overload with a nicer name (isEmailValid). This version is fully compatible with 1.2


v1.2 (Torben Rohde, 28/7-2006)
-------------------------------
Added 4 letter TLD as valid domain name (e.g. .info)

v1.1 (Torben Rohde, 9/5-2006)
--------------------------------
Fixed bug: Address in the form myaddress@.dk was accepted as valid
The function was almost completely rewritten

v1.0 (Torben Rohde, 25/10-2001)
--------------------------------
Initial version
*/

function isEmailValid(email)
{
  return validateEmail(email)
}

//Validate the e-mail address (returns true if all ok, else possibly the character that is not allowed or simply false)
function validateEmail(email) 
{
  //invalidChars = "   /:,;�$�{[]}|�!\"#�%&()=?`��\\*+'<>���^";
  invalidChars = "\t \x2F\x3A\x2C\x3B\xA3\x24\x20AC\x7B\x5B\x5D\x7D\x7C\xB4\x21\x5C\x22\x23\xA4\x25\x26\x28\x29\x3D\x3F\x60\xBD\xA7\x5C\x5C\x2A\x2B\x27\x3C\x3E\xE6\xF8\xE5\x5E";

  //Any invalid chars?
    for (i = 0; i < invalidChars.length; i++) {
      checkChar = invalidChars.charAt(i)
      if (email.indexOf(checkChar, 0) > - 1) {
        return false;
      }
    }

  //Check the occurence of the @ char
    var emailParts = email.split('@');
    if (emailParts.length == 1) {
      //string did not contain @
      return false;
    }
    else if (emailParts.length > 2) {
      //string did not contain more than one @
      return false;
    }
    else if (emailParts.length != 2) {
      //unknown error, should not occur
      return false;
    }

  var emailAccountName = emailParts[0];
  var emailDomain = emailParts[1];

  //Correct account name?
    if (emailAccountName.length == 0) {
      //no account name
      return false;
    }
    var emailAccountNameFirstChar = emailAccountName.charAt(0);
    var emailAccountNameLastChar = emailAccountName.charAt(emailAccountName.length-1);

    if (emailAccountNameFirstChar == ".") {
      //account name must not start with .
      return false;
    }
    if (emailAccountNameLastChar == ".") {
      //account name must not end with .
      return false;
    }

  //Correct domain
    if (emailDomain.length == 0) {
      //no account name
      return false;
    }
    //domain must end in two or three letters after .
      if (emailDomain.indexOf(".") == -1) {
        //no . in domain
        return false;
      }
      var topLevelDomainName = emailDomain.substring(emailDomain.lastIndexOf(".")+1, emailDomain.length);
      if (topLevelDomainName.length != 2 && topLevelDomainName.length != 3 && topLevelDomainName.length != 4) {
        //invalid top-level-domain (must be e.g. dk or com)
        return false;
      }
      //check that there are not two dots next to each other
        if (emailDomain.indexOf("..") != -1) {
          //invalid domain, contains double .
          return false;
        }

      var subdomain = emailDomain.substring(0, emailDomain.lastIndexOf("."));
      if (subdomain.length == 0) {
        //no subdomain specified (e.g. myaccount@.com)
        return false;
      }
    var emailDomainFirstChar = emailDomain.charAt(0);
    if (emailDomainFirstChar == ".") {
      //account name must not start with .
      return false;
    }
  //All ok
  return true;
}

