/* This email validation script file checks the following:
1. There should be exactly one "@" and atleast one "." characters
2. The last index of the "." character should be either 2 or 3 places preceeding 
the last character of the email address
3. There should not be any special character after @ except for "." ,"_", "-"
4. No special characters like "-" , "." or "_" are allowed after or before the "@" and "."
5. All characters are allowed before the character "@"
6. The email address cannot begin with "@" or "."
7. The part of the  email address before "@" should not contain the following characters:
   ';', '"' , ',' , ':', '<' , '>' , '(' , ')'
*/ 

function isEmail(email_Id)
{
	var email=fnTrim(email_Id);
	var iAtCount=0;
	var iDotCount=0;

	for(var iCnt=0; iCnt< email.length;iCnt++)
	{
if(email.charAt(iCnt) == " "||email.charAt(iCnt) == ";" ||email.charAt(iCnt) == "\"" || email.charAt(iCnt) == ":" || email.charAt(iCnt) == "," || email.charAt(iCnt) == "<" || email.charAt(iCnt) == ">" || email.charAt(iCnt) == "(" || email.charAt(iCnt) == ")") 
		{
			return false;	
		}
	}
	for(var iCnt=0;iCnt < email.length;iCnt++)
	{
		if(email.charAt(iCnt)=="@")
			iAtCount++;
		if(email.charAt(iCnt)==".")
			iDotCount++;
	}
	if((iAtCount==1)&&(iDotCount!=0))
	{
		//Checks that the email address is not beginning with "@" or "."
		//Also checks that the index of the last "." precedes the index of the last character
		// by 2 or 3		
		//if((email.indexOf("@",0)==0)||(email.indexOf(".",0)==0)||((email.lastIndexOf(".")!=email.length-4)&&(email.lastIndexOf(".")!=email.length-3)))
		if((email.indexOf("@",0)==0)||(email.indexOf(".",0)==0)||((email.length-email.lastIndexOf(".") < 3)||(email.length-email.lastIndexOf(".") > 5)))//CR_10081
		{
			return false;
		}
		else
		{
			if(!(validEmailAddress(email)))
			{
				return false;
			}
			else
			{
				return true;
			}
		}
	}
	else
	{
		return false;
	}
}	

function validEmailAddress(email)
{
   var ch=email.charAt(email.indexOf("@")+1);
   var ch3=email.charAt(email.length-1);
   var ch4;

   // checks that there are no special characters after "@" in the 
   //email address except '-' , '_' and '.'
   for(var iCnt=email.indexOf("@")+1;iCnt<email.length ;iCnt++)
   {
		ch4=email.charAt(iCnt);
		if((!isAlphaNumeric(ch4))&&(ch4!=".")&&(ch4!="-")&&(ch4!="_"))
		{
			return false;
		}	
   }
   //Checks that all the characters which are just preceding and just succeding 
   // the "." character are alphanumeric
   for(var iCnt=email.indexOf("@")+1;iCnt<email.length ;iCnt++)
   {
		if(email.charAt(iCnt)==".")
		{
			if((!isAlphaNumeric(email.charAt(iCnt-1)))||(!isAlphaNumeric(email.charAt(iCnt+1))))
			{
				return false;
			}	
		}
	}	

   //Checks that the character just after '@' is alphanumeric 
   //and the last character is not a special character
   if(isAlphaNumeric(ch)&&isAlphaNumeric(ch3))
   {
		return true;
   }
   else
   {
		return false;
   }     
}		

function isAlphaNumeric(emailch)
{
   var email_char=emailch;
   if(((email_char >= "0")&&(email_char <= "9" ))||((email_char >= "A")&&(email_char <= "Z" ))||((email_char >= "a")&&(email_char <= "z" )))
   {
		return true;
   }
   else
   {
		return false;
   }
}	