/*
	function 			: 	to validate an email address
	created by		: 	hong lim, ykchoong
	modified by		: 	jason wong
	date created	: 	2002
	date modified	:		07 May 2003
	
	syntax				: 	isOKemail ( frmObj, msg )
										chkEmail ( frmObj, msg )
										isEmail ( str )
									
	parameters		:		frmObj	(required)	indicates the form element
										msg			(optional) 	indicates the alert message to be prompted if email is invalid
										str			(required)	indicates the string value 
	
	examples			:		if ( !isOKemail(frm.txtTest,"Please enter a proper email address.") ) { return false; }
										<input type="text" name="txtEmail" value="" onBlur="chkEmail(this,'Please enter a PROPER email.');">
										if ( !isEmail(frm.txtEmail.value) )
	
	prerequisite	: 	fn_trim.js
										val_empty.js
*/
var def = false;

function isOKemail ( frmObj, msg )
{
	if ( frmObj.type == "text" )
	{
		if ( isEmpty(frmObj.value) )
		{
			if ( msg ) 
				{ alert(msg); }
			else 
				{ alert("Email is compulsory.\n\nPlease re-enter."); }
			frmObj.focus();
			return false;
		}
		else if ( !isEmail( frmObj.value ) )
		{
			if ( msg )
				{ alert(msg); }
			else 
				{ alert("Please enter a correct email address."); }
			frmObj.focus();
			frmObj.select();
			return false;
		}
		else
			{ return true; }
	}
	else
		{ alert("Error:\n\nWrong form element type"); return false; }
}

function chkEmail( frmObj, msg )
{
	if ( isEmpty( frmObj.value ) )
  	{ return false; }
	else if ( isEmail( frmObj.value ) == false )
	{
		if ( msg )
			{ alert(msg); }
		else 
			{ alert("Please enter a correct email address."); }
		frmObj.focus();
		frmObj.select();
		return false;
	}
	else
		{ return true; }
}

function isEmail ( str )
{ 
	if ( isEmpty( str ) )
  	{ return false; }
			
	var checkTLD = 0; 
	var knownDomsPat = / ^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	// Check e-mail address fits user@domain format, separate the username from the domain.
	var emailPat = /^(.+)@(.+)$/; 
	// No special characters in the address. These characters include ( ) < > @ , ; : \ " . [ ]
	var specialChars = "\\(\\)><@,;:\\\\\\\"\\.\\[\\]"; 
	// Range of characters allowed in a username or domainname
	var validChars = "\[^\\s" + specialChars + "\]"; 
	// If "user" is a quoted string (in which case there is no rules) "jiminy cricket"@disney.com is a legal e-mail address
	var quotedUser = "(\"[^\"]*\")"; 
	// Check for domains that are IP addresses, joe@[123.124.233.4] is a legal e-mail address
	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 = validChars + '+'; 
	// The following string represents one word in the typical username.
   // For example, in john.doe@somewhere.com, john and doe are words.
   // Basically, a word is either an atom or quoted string.
	var word = "(" + atom + "|" + quotedUser + ")"; 
	// The following pattern describes the structure of the user
	var userPat = new RegExp("^" + word + "(\\." + word + ")*$"); 
	// The following pattern describes the structure of a normal symbolic domain, as opposed to ipDomainPat, shown above
	var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$"); 

	// Begin with the coarse pattern to simply break up user@domain into different pieces that are easy to analyze
	var matchArray = str.match(emailPat); 
	if ( matchArray == null )
	{ 
		//alert("The Email Address Is Invalid"); 
		return false; 
	} 
	
	var user=matchArray[1]; 
	var domain=matchArray[2]; 
	for ( i = 0; i < user.length; i++ )
	{ 
		if (user.charCodeAt(i)>127)
		{ 
			//alert("The Username Contains Invalid Characters."); 
			return false; 
		} 
	} 
	
	for (i=0; i<domain.length; i++)
	{ 
		if (domain.charCodeAt(i) > 127)
		{ 
			//alert("Ths Domain Name Contains Invalid Characters."); 
			return false; 
		} 
	}

	if ( user.match(userPat) == null )
	{ 
		//alert("The Username Is Invalid."); 
		return false; 
	}
	
	var IPArray = domain.match(ipDomainPat); 
	if ( IPArray != null )
	{ 
		for ( var i = 1; i <= 4; i++ )
		{ 
			if ( IPArray > 255)
			{ 
				//alert("The Destination IP Address Is Invalid."); 
				return false; 
			} 
		} 
		return true; 
	} 
	
	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 Is Invalid."); 
			return false; 
		} 
	} 

	if ( checkTLD && domArr[domArr.length-1].length != 2 && domArr[domArr.length-1].search(knownDomsPat) == -1 )
	{ 
		//alert("The Domain Name Extension Is Invalid"); 
		return false; 
	} 

	if ( len < 2 )
	{ 
		//alert("The Address Is Missing A Hostname."); 
		return false;
	}
	return true;
} 

// archived

function isEmailOld3( str )
{ 
 	return( /^[\w~][\w\.\-~]*[\w~]@[\w~][\w\-~]*[\.][\w~][\w\.\-~]*[\w]$|^$/.test(str) && !/[\.]{2}/.test(str)); 
}

function isEmailOld2( emailAddress )
{
	var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  return re.test(emailAddress);
}

function isEmailOld1( str )
{
  // are regular expressions supported?
  var supported = 0;
  if ( window.RegExp )
	{
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if ( !supported ) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}