<!--
var objXMLHTTP;

// function to initialize the ajax
function Init_XMLHTTP() {
	
	
	if (window.XMLHttpRequest) { objXMLHTTP = new XMLHttpRequest(); }
	else if (window.ActiveXObject) { objXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP"); }
	
	return objXMLHTTP;
}

function validateEmail(EmailElementId,DisplayMessageDivID,SubmitButton,OptionalField,DivCheckList,EmailFieldValue,divEmailValidationNote) {
	//first remove all space from the email address.
	var strEmailAdd  = document.getElementById(EmailElementId).value.replace(/^\s*|\s*$/g,'');
	var strInValidEmailPattern = "";
	var blnBlankField = strEmailAdd  == "";
	// a boolean to indicate whether the email contain multiple email or just one email.
	// if the email contain ',' and ';' at the start/end of the string then treat it as one email(regular expression will validate it and return invalid format)
	var blnMultipleEmail = (((strEmailAdd .indexOf(";") >= 0)||(strEmailAdd .indexOf(",") >= 0)) &&(strEmailAdd .charAt(strEmailAdd .length-1) != ",")&&(strEmailAdd .charAt(strEmailAdd .length-1) != ";")&&(strEmailAdd .charAt(0) != ";")&&(strEmailAdd .charAt(0) != ","));
	var strSubmitButtonList = "";
	var blnOptionalField = OptionalField == 1;
	var blnMultipleDivCheck = DivCheckList != ""; 
	
	// strSubmitButtonList to indicate which button to be disable if the email field is invalid, it may contain few button.
	strSubmitButtonList = SubmitButton.split(",");	

	if(!blnBlankField)
	{
		if (blnMultipleEmail)
			strInValidEmailPattern = ValidMultipleEmailPattern(strEmailAdd );
		else 
			strInValidEmailPattern = ValidOneEmailPattern(strEmailAdd );
	}
	
	if(blnBlankField)
	{
		//Optional Email field and the email field is blank, no action taken
		if (blnOptionalField)
		{
			document.getElementById(DisplayMessageDivID).innerHTML = "";
			document.getElementById(divEmailValidationNote).innerHTML = "";
			
			disableButton(strSubmitButtonList,false);
			if(blnMultipleDivCheck)
				disableButton(strSubmitButtonList,checkMultipleEmail(DivCheckList));
		}
		else
		{
			//if the field is blank but it is not optional then display error message.
			document.getElementById(DisplayMessageDivID).innerHTML = "<img src='/_pics/arrow1.gif'><font style='FONT: 7.5pt verdana' color=red><b>Invalid Email Address. Please check.<br>Please correct the Email Address before you click on " + getButtonValue(strSubmitButtonList)  + ".</b></font>";				
			document.getElementById(divEmailValidationNote).innerHTML = "<font style='FONT: 7.5pt verdana' color=red><b>NOTE: Please correct the <a href='' onclick='javascript:setEmailFocus(\""+ EmailElementId +"\");return false;'> \"" + EmailFieldValue + "\"</a> before you click on " + getButtonValue(strSubmitButtonList)  + ".</b></font>";			
			disableButton(strSubmitButtonList,true);
			if(blnMultipleDivCheck)
				disableButton(strSubmitButtonList,checkMultipleEmail(DivCheckList));
		}
	}
	else if(strInValidEmailPattern == null)
	{
		//strInValidEmailPattern = null mean all the email address pass the regular expression. So now proceed to call ajax and check the domain/email
		var objXMLHTTP = Init_XMLHTTP();
		var responseText;
		
		if (objXMLHTTP != null ) {	
			
			objXMLHTTP.open("GET", "/_general/mailValidatorJX.asp?emailToVal=" + strEmailAdd , true);
			objXMLHTTP.onreadystatechange = function Output() {
				if (objXMLHTTP.readyState == 1)
				{
					disableUpdateAdCheckBox(true);
					//if status = loading then display loading message and disable all submit button if this is not an optional field
					document.getElementById(DisplayMessageDivID).innerHTML = "<img src='/_pics/arrow1.gif'><font style='FONT: 7.5pt verdana' color=#666666>Email Address is being validated.</font>";					
					document.getElementById(divEmailValidationNote).innerHTML = "";
					disableButton(strSubmitButtonList,true);
					if(blnMultipleDivCheck)
						disableButton(strSubmitButtonList,checkMultipleEmail(DivCheckList));
				}
				else if (objXMLHTTP.readyState == 4) {				
					//if status = complete
					disableUpdateAdCheckBox(false);
					responseText = objXMLHTTP.responseText;
					if (responseText == "Valid")
					{						
						//if status = valid mean all the email is valid, unlock the submit button and display valid message
						document.getElementById(DisplayMessageDivID).innerHTML = "<img src='/_pics/arrow1.gif'><font style='FONT: 7.5pt verdana' color=green><b>The Email Address is valid.</b></font>";
						document.getElementById(divEmailValidationNote).innerHTML = "";
						disableButton(strSubmitButtonList,false);
						if(blnMultipleDivCheck)
							disableButton(strSubmitButtonList,checkMultipleEmail(DivCheckList));
					}
					else if(responseText != "Valid")
					{
						//if the email is invalid then display error message and disable all the submit button.		
						document.getElementById(DisplayMessageDivID).innerHTML = "<img src='/_pics/arrow1.gif'><font style='FONT: 7.5pt verdana' color=red><b>Invalid Email Address: \"" + responseText + "\". Please check.<br>Please correct the Email Address before you click on " + getButtonValue(strSubmitButtonList)  + ".</b></font>";				
						document.getElementById(divEmailValidationNote).innerHTML = "<font style='FONT: 7.5pt verdana' color=red><b>NOTE: Please correct the <a href='' onclick='javascript:setEmailFocus(\""+ EmailElementId +"\");return false;'> \"" + EmailFieldValue + "\"</a> before you click on " + getButtonValue(strSubmitButtonList)  + ".</b></font>";
						
						if (!blnOptionalField)
						{
							disableButton(strSubmitButtonList,true);
							if(blnMultipleDivCheck)
								disableButton(strSubmitButtonList,checkMultipleEmail(DivCheckList));
						}						
					}
				}
			}
			objXMLHTTP.send(null);
		}
	}
	else
	{	
		//if the regular expression found the email having invalid pattern then will disable all submit button and display error message.	
		document.getElementById(DisplayMessageDivID).innerHTML = "<img src='/_pics/arrow1.gif'><font style='FONT: 7.5pt verdana' color=red><b>Invalid Email Address: \"" + strInValidEmailPattern + "\". Please check.<br>Please correct the Email Address before you click on " + getButtonValue(strSubmitButtonList)  + ".</b></font>";				
		document.getElementById(divEmailValidationNote).innerHTML = "<font style='FONT: 7.5pt verdana' color=red><b>NOTE: Please correct the <a href='' onclick='javascript:setEmailFocus(\""+ EmailElementId +"\");return false;'> \"" + EmailFieldValue + "\"</a> before you click on " + getButtonValue(strSubmitButtonList)  + ".</b></font>";
		disableButton(strSubmitButtonList,true);
		if(blnMultipleDivCheck)
			disableButton(strSubmitButtonList,checkMultipleEmail(DivCheckList));
	}
}

function setEmailFocus(EmailElementId)
{
	setTimeout("document.getElementById('" + EmailElementId + "').focus()", 50);
	//alert(EmailElementId);
	//return false;
}

function ValidOneEmailPattern(vl)
{
	// Common typo error for common domains :
	//	 - hotmail.com
	//   - yahoo.com, yahoo.co.uk, yahoo.co.in
	//   - rediffmail.com
	//   - tm.net.my
	var arrTypo = new Array();
	var i = 0;
	
	arrTypo[0] = "[\\w-\\.]{1,}\\@[\\w-\\.\\@]{0,}(rediff|htomail|htomil|htomai|htomal|htomial|hotmil)s*.co[m]{0,1}[\\w\\.\\@]{0,}"
	arrTypo[1] = "[\\w-\\.]{1,}\\@[\\w-\\.]{1,}(rediff|rediffmail|hotmail|hotm[ail]s?).com[\\w-\\.\\@]{0,}"
	arrTypo[2] = "[\\w-\\.]{1,}\\@[\\w-\\.]{1,}(rediffmail|hotmail|hotm[ail]s?)[\\w-\\.]{1,}.com[\\w\\.\\@]{0,}"
	arrTypo[3] = "[\\w-\\.]{1,}\\@(rediffmail|hotmail)[\\w-\\.]{1,}.co[m]{0,1}[\\w-\\.\\@]{0,}"
	arrTypo[4] = "[\\w-\\.]{1,}\\@(rediffmail|hotmail).com[\\w-\\.\\@]{1,}"
	arrTypo[5] = "[\\w-\\.]{1,}\\@[\\w-\\.\\@]{1,}yahoo[\\w\\.]{1,}.(com|co.uk|co.in)"
	arrTypo[6] = "[\\w-\\.]{1,}\\@[\\w-\\.\\@]{1,}yahoo.(com|co.uk|co.in)"
	arrTypo[7] = "[\\w\\.\\@]{1,}\\@yahoo[\\w-\\.]{1,}.(com|co.uk|co.in)"
	arrTypo[8] = "[\\w-\\.]{1,}\\@[\\w-\\.\\@]{0,}yahoo[\\w]{0,}.com.in"
	arrTypo[9] = "[\\w-\\.]{1,}\\@[\\w-\\.\\@]{0,}yahoo[\\w]{0,}.com.uk"
	arrTypo[10] = "[\\w-\\.]{1,}\\@[\\w-\\.\\@]{0,}y(o|a)*ho{3,}[\\w]{0,}.(com|co.uk|co.in)"
	arrTypo[11] = "[\\w-\\.]{1,}\\@[\\w-\\.\\@]{0,}yahoo.uk.co[\\w-\\.]{0,}"
	arrTypo[12] = "[\\w-\\.]{1,}\\@[\\w-\\.\\@]{1,}tm[.,]net[.,]my[\\w]{1,}"
	arrTypo[13] = "[\\w-\\.]{1,}\\@[\\w-\\.\\@]{1,}tm[.,]net[.,]my"
	arrTypo[14] = "[\\w-\\.]{1,}\\@tm[.,]net[.,]my[\\w]{1,}"
	arrTypo[15] = "[\\w-\\.]{1,}\\@[\\w-\\.\\@]{1,}tm[.,]com[.,]my[\\w]{1,}"
	arrTypo[16] = "[\\w-\\.]{1,}\\@tm[.,]com[.,]my[\\w]{1,}"
	arrTypo[17] = "[\\w-\\.]{1,}\\@usa[.,]net[\\w-\\.\\@]{1,}"
	arrTypo[18] = "[\\w-\\.\\@]{1,}\\@[\\w-\\.\\@]{1,}usa[.,]net[\\w-\\.\\@]{1,}"
	arrTypo[19] = "[\\w-\\.\\@]{1,}\\@[\\w-\\.\\@]{1,}usa[.,]net"
	arrTypo[20] = "[\\w-\\.]{1,}\\@pacific[.,]net[.,]sg[\\w-\\.\\@]{1,}"
	arrTypo[21] = "[\\w-\\.]{1,}\\@[\\w-\\.\\@]{1,}pacific[.,]net[.,]sg"
	arrTypo[22] = "[\\w-\\.]{1,}\\@[\\w-\\.\\@]{1,}pacific[.,]net[.,]sg[\\w-\\.\\@]{1,}"
	//arrTypo[23] = "[\\w-\\.\\@]{0,}www.[\\w-\\.\\@]{0,}"
	
	// check valid email format
	if (rv=validPattern(vl,null,
		"('|[\\w-_])+((\\.|\\'|\\`)[\\w-_]+)*\\@[\\w-_]+(\\.[\\w-_]+)+") ) {
		// Valid domains for exclusion : i. @yahoo-inc.com
		if (!(validPattern(vl, null, "[\\w-_]*[\\w-\\.]*\\@yahoo\\-inc.com"))) {
			for (i = 0; i < arrTypo.length; i++) {
				// if can match expression, means contains typo error
				if (validPattern(vl, null, arrTypo[i])) {
					rv = false;
					break;
				}
			}
		}
	}
	if(rv)
		return null;
	else
		return vl;
}

function ValidMultipleEmailPattern(emails) {
	if (emails.indexOf(";") >= 0){
		var rSemi = /;/g;
		emails=emails.replace(rSemi,",")
	}
	
	emails = emails.split(",");	
	var res = /^\s+/ig;
	var ree = /\s+$/ig;

	for (e = 0; e < emails.length; e++) {
		emails[e] = emails[e].replace(res,"").replace(ree,""); 
		if (ValidOneEmailPattern(emails[e]) != null) {
			return emails[e];			
		} 
	}
	return null;
}

function validPattern(vl,errm) // varying number of arguments
{
	var intCount;
	validated=true;
	// scan regular expressions
	for (intCount=2; intCount<validPattern.arguments.length; intCount++) 
	{
		var rx;
		//alert(valid.arguments[intCount]);
		rx=new RegExp(validPattern.arguments[intCount]);
		if ((a=rx.exec(vl))!=null && a[0].length==vl.length) {
			return true;  // ok
		}
	}
	if (errm != null) { alert(errm); }
	return false;
}

function resetEmailValResult(DisplayMessageDivID,divEmailValidationNote)
{
	//function to clear the email validation result when user press on Reset button
	DisplayMessageDivID = DisplayMessageDivID.split(",");
	divEmailValidationNote = divEmailValidationNote.split(",");
	for (e = 0; e < DisplayMessageDivID.length; e++) {	
		if(document.getElementById(DisplayMessageDivID[e]))	
			document.getElementById(DisplayMessageDivID[e]).innerHTML="";
		if(document.getElementById(divEmailValidationNote[e]))	
			document.getElementById(divEmailValidationNote[e]).innerHTML="";		
	}
}

function disableUpdateAdCheckBox(Disable)
{
	if (document.getElementById("FormUpdateAd"))
	{
		var frm = document.getElementById("FormUpdateAd");
		if(Disable)
		{
			if(frm.RcvResume[0])
				frm.RcvResume[0].disabled = true;
			if(frm.RcvResume[1])
				frm.RcvResume[1].disabled = true;
		}
		else
		{
			if(frm.RcvResume[0])
				frm.RcvResume[0].disabled = false;
			if(frm.RcvResume[1])
				frm.RcvResume[1].disabled = false;
		}	
	}
}

function checkMultipleEmail(DisplayMessageDivID)
{
	DisplayMessageDivID = DisplayMessageDivID.split(",");
	for (e = 0; e < DisplayMessageDivID.length; e++) {
		if(document.getElementById(DisplayMessageDivID[e]))
		{
			if(document.getElementById(DisplayMessageDivID[e]).innerHTML.indexOf("The Email Address is valid.") == -1 && document.getElementById(DisplayMessageDivID[e]).innerHTML != "")
			{
				return true;
				break;
			}
		}
	}
	return false;
}

function disableButton(SubmitButtonList,Disable)
{
	for (e = 0; e < SubmitButtonList.length; e++) {
		if(document.getElementById(SubmitButtonList[e]))	
			document.getElementById(SubmitButtonList[e]).disabled = Disable;
	}	

}

function getButtonValue(SubmitButtonList)
{
	var strTempButton = "";
	for (e = 0; e < SubmitButtonList.length; e++) {		
		if(document.getElementById(SubmitButtonList[e]))
		{	
			if(strTempButton!="")
				strTempButton = strTempButton + ", ";
			strTempButton = strTempButton + document.getElementById(SubmitButtonList[e]).value;
		}
	}	
	return strTempButton;
}

//-->
