// Copyright, Ken McGinnis / Millennia Corporation
//
// see checkemail.js.readme.txt
//
// Place the below two lines in the <head> section of the calling page.
// <script type="text/javascript" src="email.js">
// </script> 
//
// Call it using the following statement from either the onblur event of the email
// field itself and / or the onclick or onsubmit event of the submission button or form: 
//
// validateEmail(email_field,mandatory,messages); 
//
// The email address to be validated needs to be passed in place of email_field in the above code.
// If the email field is mandatory then the mandatory field should contain '1' otherwise it should
// be '0'. The messages field can be set to '1' to return individual messages about which particular
// test that the email address failed to meet. The error messages will be suppressed if this is set
// to '0' and you can provide your own failure message. The value returned will be true if the email
// address is considered to be valid and false if it isn't. 
// 
// If you prefer you can incorporate the call into an if statement processing the appropriate code
// based directly on the value returned instead of having to test the valid_email field. For example
// you could place the following into your validation code to validate a mandatory email field: 
// 
// if (!CheckEmail(emailfield,1,1)) {
//    alert('email address is invalid or was not entered');
//    return false;
// } 
//
// This validation routine avoids the use of regular expressions and sticks with the most simple javascript code so that it can validate email addresses in all browsers. 


function CheckEmail(addr,man,db) {

//    alert('Ken, CheckEmail was called');

   if (addr == '' && man) {
       if (db) alert('Email address is mandatory.');
       return false;
    }
    var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
    for (i=0; i<invalidChars.length; i++) {
       if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
          if (db) alert('Email address contains invalid characters.');
          return false;
       }
    }
    for (i=0; i<addr.length; i++) {
       if (addr.charCodeAt(i)>127) {
         if (db) alert("Email address contains non ascii characters.");
         return false;
      }
   }

   var atPos = addr.indexOf('@',0);
   if (atPos == -1) {
      if (db) alert('Email address must contain an @ character.');
      return false;
   }
   if (atPos == 0) {
      if (db) alert('Email address must not start with an @ character.');
      return false;
   }
   if (addr.indexOf('@', atPos + 1) > - 1) {
      if (db) alert('Email address must contain only one @ character.');
      return false;
   }
   if (addr.indexOf('.', atPos) == -1) {
      if (db) alert('Email address must contain a period in the domain name.');
      return false;
   }
   if (addr.indexOf('@.',0) != -1) {
      if (db) alert('Period must not immediately follow @ in email address.');
      return false;
   }
   if (addr.indexOf('.@',0) != -1){
      if (db) alert('Period must not immediately precede @ in email address.');
      return false;
   }
   if (addr.indexOf('..',0) != -1) {
      if (db) alert('Two periods must not be adjacent in email address.');
      return false;
   }
   
   //var suffix = addr.substring(addr.lastIndexOf('.')+1);
   //if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum' && suffix != 'travel') {
   //   if (db) alert('invalid primary domain in email address');
   //   return false;
   //}
   
   var arr = new Array('.com','.net','.org','.biz','.coop','.info','.travel',
   '.museum','.name','.pro','.edu','.gov','.int','.mil','.arpa','.aero',
   '.ac','.ad','.ae','.af','.ag','.ai','.al','.am','.an','.ao','.aq','.ar',
   '.as','.at','.au','.aw','.az','.ba','.bb','.bd','.be','.bf','.bg','.bh',
   '.bi','.bj','.bm','.bn','.bo','.br','.bs','.bt','.bv','.bw','.by','.bz',
   '.ca','.cc','.cd','.cf','.cg','.ch','.ci','.ck','.cl','.cm','.cn','.co',
   '.cr','.cs','.cu','.cv','.cx','.cy','.cz','.de','.dj','.dk','.dm','.do',
   '.dz','.ec','.ee','.eg','.eh','.er','.es','.et','.eu','.fi','.fj','.fk',
   '.fm','.fo','.fr','.ga','.gd','.ge','.gf','.gg','.gh','.gi','.gl','.gm',
   '.gn','.gp','.gq','.gr','.gs','.gt','.gu','.gw','.gy','.hk','.hm','.hn',
   '.hr','.ht','.hu','.id','.ie','.il','.im','.in','.io','.iq','.ir','.is',
   '.it','.je','.jm','.jo','.jp','.ke','.kg','.kh','.ki','.km','.kn','.kp',
   '.kr','.kw','.ky','.kz','.la','.lb','.lc','.li','.lk','.lr','.ls','.lt',
   '.lu','.lv','.ly','.ma','.mc','.md','.me','.mg','.mh','.mk','.ml','.mm','.mn',
   '.mo','.mp','.mq','.mr','.ms','.mt','.mu','.mv','.mw','.mx','.my','.mz',
   '.na','.nc','.ne','.nf','.ng','.ni','.nl','.no','.np','.nr','.nt','.nu',
   '.nz','.om','.pa','.pe','.pf','.pg','.ph','.pk','.pl','.pm','.pn','.pr',
   '.ps','.pt','.pw','.py','.qa','.re','.ro','.ru','.rw','.sa','.sb','.sc',
   '.sd','.se','.sg','.sh','.si','.sj','.sk','.sl','.sm','.sn','.so','.sr',
   '.st','.sv','.sy','.sz','.tc','.td','.tf','.tg','.th','.tj','.tk','.tm',
   '.tn','.to','.tp','.tr','.tt','.tv','.tw','.tz','.ua','.ug','.uk','.um',
   '.us','.uy','.uz','.va','.vc','.ve','.vg','.vi','.vn','.vu','.wf','.ws',
   '.ye','.yt','.yu','.za','.zm','.zw'); 
   var dot = addr.lastIndexOf(".");
   var ext = addr.substring(dot,addr.length);
   var val = true;
   for(var i=0; i<arr.length; i++)
   {
      if(ext == arr[i]) 
      {
         val = true;
         break;
      } 
      else
      {
         val = false;
      }
   }
   if(val == false)
   {
      if (db) alert('Invalid primary domain in email address.');
      return false;
   }
   
   
   return true;
}
