tlds=new Array();
tlds['ad']=['Andorra'];
tlds['ae']=['United Arab Emirates'];
tlds['aero']=['Aircraft-related'];
tlds['af']=['Afghanistan'];
tlds['ag']=['Antigua and Barbuda'];
tlds['ai']=['Anguilla'];
tlds['al']=['Albania'];
tlds['am']=['Armenia'];
tlds['an']=['Netherland Antilles'];
tlds['ao']=['Angola'];
tlds['aq']=['Antarctica'];
tlds['ar']=['Argentina'];
tlds['arpa']=['Address and Routing Parameter Area'];
tlds['as']=['American Samoa'];
tlds['at']=['Austria'];
tlds['au']=['Australia'];
tlds['aw']=['Aruba'];
tlds['az']=['Azerbaijan'];
tlds['ba']=['Bosnia-Herzegovina'];
tlds['bb']=['Barbados'];
tlds['be']=['Belgium'];
tlds['bf']=['Burkina Faso'];
tlds['bg']=['Bulgaria'];
tlds['bh']=['Bahrain'];
tlds['bi']=['Burundi'];
tlds['biz']=['Business'];
tlds['bj']=['Benin'];
tlds['bm']=['Bermuda'];
tlds['bn']=['Brunei Darussalam'];
tlds['bo']=['Bolivia'];
tlds['br']=['Brazil'];
tlds['bs']=['Bahamas'];
tlds['bt']=['Bhutan'];
tlds['bw']=['Botswana'];
tlds['by']=['Belarus'];
tlds['bz']=['Belize'];
tlds['ca']=['Canada'];
tlds['cc']=['Cocos (Keeling) Islands'];
tlds['cd']=['Democratic Republic of Congo'];
tlds['cf']=['Central African Republic'];
tlds['cg']=['Congo'];
tlds['ch']=['Switzerland'];
tlds['ci']=['Ivory Coast'];
tlds['ck']=['Cook Islands'];
tlds['cl']=['Chile'];
tlds['cm']=['Cameroon'];
tlds['cn']=['China'];
tlds['co']=['Colombia'];
tlds['com']=['Commercial'];
tlds['coop']=['Cooperative-related'];
tlds['cr']=['Costa Rica'];
tlds['cu']=['Cuba'];
tlds['cv']=['Cape Verde'];
tlds['cx']=['Christmas Island'];
tlds['cy']=['Cyprus'];
tlds['cz']=['Czech Republic'];
tlds['de']=['Germany'];
tlds['dj']=['Djibouti'];
tlds['dk']=['Denmark'];
tlds['dm']=['Dominica'];
tlds['do']=['Dominican Republic'];
tlds['dz']=['Algeria'];
tlds['ec']=['Ecuador'];
tlds['edu']=['Educational'];
tlds['ee']=['Estonia'];
tlds['eg']=['Egypt'];
tlds['es']=['Spain'];
tlds['et']=['Ethiopia'];
tlds['fi']=['Finland'];
tlds['fj']=['Fiji'];
tlds['fk']=['Falkland Islands (Malvinas)'];
tlds['fm']=['Micronesia'];
tlds['fo']=['Faroe Islands'];
tlds['fr']=['France'];
tlds['ga']=['Gabon'];
tlds['gd']=['Grenada'];
tlds['ge']=['Georgia'];
tlds['gf']=['French Guyana'];
tlds['gh']=['Ghana'];
tlds['gi']=['Gibraltar'];
tlds['gl']=['Greenland'];
tlds['gm']=['Gambia'];
tlds['gn']=['Guinea'];
tlds['gov']=['Government'];
tlds['gp']=['Guadeloupe (French)'];
tlds['gq']=['Equatorial Guinea'];
tlds['gr']=['Greece'];
tlds['gt']=['Guatemala'];
tlds['gu']=['Guam (US)'];
tlds['gy']=['Guyana'];
tlds['hk']=['Hong Kong'];
tlds['hm']=['Heard and McDonald Islands'];
tlds['hn']=['Honduras'];
tlds['hr']=['Croatia (Hrvatska)'];
tlds['hu']=['Hungary'];
tlds['id']=['Indonesia'];
tlds['ie']=['Ireland'];
tlds['il']=['Israel'];
tlds['in']=['India'];
tlds['info']=['General-purpose tlds'];
tlds['int']=['International'];
tlds['io']=['British Indian Ocean Territory'];
tlds['ir']=['Islamic Republic of Iran'];
tlds['is']=['Iceland'];
tlds['it']=['Italy'];
tlds['jm']=['Jamaica'];
tlds['jo']=['Jordan'];
tlds['jp']=['Japan'];
tlds['ke']=['Kenya'];
tlds['kg']=['Kyrgyzstan'];
tlds['kh']=['Cambodia'];
tlds['ki']=['Kiribati'];
tlds['km']=['Comoros'];
tlds['kn']=['Saint Kitts Nevis Anguilla'];
tlds['kr']=['South Korea'];
tlds['kw']=['Kuwait'];
tlds['ky']=['Cayman Islands'];
tlds['kz']=['Kazakhstan'];
tlds['la']=['Laos'];
tlds['lb']=['Lebanon'];
tlds['lc']=['Saint Lucia'];
tlds['li']=['Liechtenstein'];
tlds['lk']=['Sri Lanka'];
tlds['lr']=['Liberia'];
tlds['ls']=['Lesotho'];
tlds['lt']=['Lithuania'];
tlds['lu']=['Luxembourg'];
tlds['lv']=['Latvia'];
tlds['ly']=['Libya (Libyan Arab Jamahiriya)'];
tlds['ma']=['Morocco'];
tlds['mc']=['Monaco'];
tlds['md']=['Moldavia'];
tlds['mg']=['Madagascar'];
tlds['mh']=['Marshall Islands'];
tlds['mil']=['US Military'];
tlds['ml']=['Mali'];
tlds['mm']=['Myanmar'];
tlds['mn']=['Mongolia'];
tlds['mo']=['Macau'];
tlds['mp']=['Northern Mariana Islands'];
tlds['mq']=['Martinique (French)'];
tlds['mr']=['Mauritania'];
tlds['ms']=['Montserrat'];
tlds['mt']=['Malta'];
tlds['mu']=['Mauritius'];
tlds['museum']=['Museum-related'];
tlds['mv']=['Maldives'];
tlds['mw']=['Malawi'];
tlds['mx']=['Mexico'];
tlds['my']=['Malaysia'];
tlds['mz']=['Mozambique'];
tlds['na']=['Namibia'];
tlds['name']=['Personal name'];
tlds['nc']=['New Caledonia (French)'];
tlds['ne']=['Niger'];
tlds['net']=['Network Infrastructure'];
tlds['nf']=['Norfolk Island'];
tlds['ng']=['Nigeria'];
tlds['ni']=['Nicaragua'];
tlds['nl']=['Netherlands'];
tlds['no']=['Norway'];
tlds['np']=['Nepal'];
tlds['nr']=['Nauru'];
tlds['nu']=['Niue'];
tlds['nz']=['New Zealand'];
tlds['om']=['Oman'];
tlds['org']=['Nonprofit'];
tlds['pa']=['Panama'];
tlds['pe']=['Peru'];
tlds['pf']=['French Polynesia'];
tlds['pf']=['Polynesia (French)'];
tlds['pg']=['Papua New Guinea'];
tlds['ph']=['Philippines'];
tlds['pk']=['Pakistan'];
tlds['pl']=['Poland'];
tlds['pm']=['Saint Pierre and Miquelon'];
tlds['pn']=['Pitcairn'];
tlds['pr']=['Puerto Rico (US)'];
tlds['pro']=['Professional domain'];
tlds['ps']=['Palestina'];
tlds['pt']=['Portugal'];
tlds['pw']=['Palau'];
tlds['py']=['Paraguay'];
tlds['qa']=['Qatar'];
tlds['re']=['Reunion (French)'];
tlds['ro']=['Romania'];
tlds['ru']=['Russian Federation'];
tlds['rw']=['Rwanda'];
tlds['sa']=['Saudi Arabia'];
tlds['sb']=['Solomon Islands'];
tlds['sc']=['Seychelles'];
tlds['se']=['Sweden'];
tlds['sg']=['Singapore'];
tlds['sh']=['Saint Helena'];
tlds['si']=['Slovenia'];
tlds['sk']=['Slovak Republic (Slovakia)'];
tlds['sl']=['Sierra Leone'];
tlds['sm']=['San Marino'];
tlds['sn']=['Senegal'];
tlds['so']=['Somalia'];
tlds['sr']=['Surinam'];
tlds['st']=['Saint Tome and Principe'];
tlds['su']=['Soviet Union'];
tlds['sv']=['El Salvador'];
tlds['sz']=['Swaziland'];
tlds['tc']=['Turks and Caicos Islands'];
tlds['td']=['Chad'];
tlds['tf']=['French Southern Territories'];
tlds['tg']=['Togo'];
tlds['th']=['Thailand'];
tlds['tj']=['Tajikistan'];
tlds['tk']=['Tokelau'];
tlds['tm']=['Turkmenistan'];
tlds['tn']=['Tunisia'];
tlds['to']=['Tonga'];
tlds['tp']=['East Timor'];
tlds['tr']=['Turkey'];
tlds['tt']=['Trinidad and Tobago'];
tlds['tv']=['Tuvalu'];
tlds['tw']=['Taiwan'];
tlds['tz']=['Tanzania'];
tlds['ua']=['Ukraine'];
tlds['ug']=['Uganda'];
tlds['uk']=['United Kingdom'];
tlds['us']=['United States of America'];
tlds['uy']=['Uruguay'];
tlds['uz']=['Uzbekistan'];
tlds['va']=['Vatican City State'];
tlds['vc']=['Saint Vincent and the Grenadines'];
tlds['ve']=['Venezuela'];
tlds['vg']=['Virgin Islands (British)'];
tlds['vi']=['Virgin Islands (US)'];
tlds['vn']=['Vietnam'];
tlds['vu']=['Vanuatu'];
tlds['ws']=['Samoa'];
tlds['ye']=['Yemen'];
tlds['yu']=['Yugoslavia'];
tlds['za']=['South Africa'];
tlds['zm']=['Zambia'];
tlds['zr']=['Zaire'];
tlds['zw']=['Zimbabwe'];


function ValidEmail(email) 
{
  //minimum length 6: a@b.my
  if (email.length<7) {
    return false
  }
  
  //ensure only valid characters present
  valid_chars="@.-_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  for (i=0; i<email.length; i++) {
    c=email.charAt(i)
    if (valid_chars.indexOf(c,0)==-1) {
      return false
    }
  }
  
  //must contain excatly one @
  email_parts=email.split("@");
  if (email_parts.length!=2) {
    return false;
  }
  
  //@ sign not at beginning or end.
  if ((email_parts[0]=="") || (email_parts[1]=="")) {
    return false;
  }
    
  //if username contains a period, it cannot be at the beginning, end or next to another
  //period
  username_parts=email_parts[0].split(".");
  for (i=0; i<username_parts.length; i++) {
    if (username_parts[i]=="") {
      return false;
    }
  }
  
  //the domain name is at least 4 characters (a@b.my)
  if (email_parts[1].length<5) {
    return false;
  }
  
  //must be at least one period in the domain
  domain_parts=email_parts[1].split(".");
  if (domain_parts.length<2) {
    return false;
  }
  
  //period not at the beginning or end or next to another period
  for (i=0; i<domain_parts.length; i++) {
    if (domain_parts[i]=="") {
      return false;
    }
  }
  
  //match against all known tld's
  tld=domain_parts[domain_parts.length-1];
  if (!tlds[tld]) {
    return false;
  }
  
  return true
}
