/*Header Begin

$Author: Hem.balla $

$Workfile: emailValidation.js $

$Modtime: 10/09/06 7:22a $

$Revision: 1 $

$NoKeywords: $

Change History: Put the issue ID and your description of the changes here

1) This file contains a function emailCheck  which validates the email address entered by a person.

Header Ends*/


function emailCheck (emailStr) {

var knownDomsList="";

for(i=0;i<tld_names.length;i++){
	if(knownDomsList.length>0){
		knownDomsList=knownDomsList + "|";	
	}
	knownDomsList= knownDomsList + tld_names[i];
}
knownDomsPat=new RegExp("^(" + knownDomsList + ")$");

/* The following pattern is used to check if the entered e-mail address fits the user@domain format.  It  is also used to separate the username
from the domain. */

var emailPat=/^(.+)@(.+)$/;

/* The following string represents the pattern for matching all special
characters.  We don't want to allow special characters in the address. 
These characters include ( ) < > @ , ; : \ " . [ ] */

var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

/* The following string represents the range of characters allowed in a 
username or domainname.  It really states which chars aren't allowed.*/

var validChars="\[^\\s" + specialChars + "\]";

/* The following pattern applies if the "user" is a quoted string (in which case, there are no rules about which characters are allowed
and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com is a legal e-mail address. */

var quotedUser="(\"[^\"]*\")";

/* The following string represents a series of non-special characters. */

var atom=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 + ")*$");



/* Check if the supplied address is valid. */

/* Break up user@domain into different pieces. */

var matchArray=emailStr.match(emailPat);

if (matchArray==null) {

// alert("The email address you entered seems incorrect. Please check @ and .'s");
return false;
}
var user=matchArray[1];
var domain=matchArray[2];

// Start by checking that only basic ASCII characters are in the strings (0-127).

for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
// alert("Ths username you entered in the email address contains invalid characters.");
return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
// alert("Ths domain name you entered in the email address contains invalid characters.");
return false;
   }
}

// See if "user" is valid 

if (user.match(userPat)==null) {
// user is not valid.
// alert("The username you entered in the email address doesn't seem to be valid.");
return false;
}

// Check if it's valid domain name.
 
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
 // alert("The domain name you entered in the email address does not seem to be valid.");
return false;
   }
}

/* Check if a known top-level domain (like com, edu, gov) present int the domain names array from domain_nameslist.js or a two-letter word,
representing country (uk, nl), and that there's a hostname preceding the domain or country. */


if (domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
 // alert("The email address you entered must end with a proper domain or two letter country name.");
return false;
} 

// Check if the there is a host name preceding the domain.
if (len<2) {
 // alert("The hostname is missing in the email address you entered.");
return false;
}
// The email address is valid.
return true;
}