
function checkMail(str,fl)
{ 
		var flg=0;		
		var count=0;
		var field2="\"From:\"";
		var field1="\"To:\"";
		//count1=0;
		var s1=str.indexOf('@');
		var s2=str.indexOf('.');
		var s3=str.indexOf(' ');
		var s4=str.indexOf(';');
		var s5=str.indexOf(',');
		var s6=str.indexOf('?');
		var s7=str.indexOf('/');
		var s8=str.indexOf('%');
		var s9=str.indexOf('*');
		var s10=str.indexOf('&');
		var s11=str.indexOf('^');
		var s12=str.indexOf('$');
		var s13=str.indexOf('!');
		var s14=str.indexOf('#');
		var s15=str.indexOf('+');
		var s16=str.indexOf('=');
		var s17=str.indexOf('<');
		var s18=str.indexOf('>');
		var s19=str.indexOf('}');
		var s20=str.indexOf('{');
		var s21=str.indexOf('|');
		var s22=str.indexOf(')');
		var s23=str.indexOf('(');
		//var returnValue = false;
		
		if(fl==4)
		{
			
			field1="recipient";
			fl=1;
			
		}
		else if(fl==3)
		{
			field2="your e-mail";
			fl=0;
		}
		
		if (str == "")
		{  
			if(fl==0)
			{
				alert("Please enter an address in the " +field2+ " field \n");
				return;
			}
			else if(fl==1)
			{
				alert("Please enter an address in the " +field1+ " field \n");
				return;
			}
			else if(fl==2)
			{
				alert("Please enter an address in your e-mail field \n");
				return;
			}
			flg++;
			return;
		}
		if((s1==-1 || s2==-1)||(s1==0 || s2==str.length-1))
		{ 
			if(fl==0)
				alert("This email address seems wrong, check the prefix and '@' sign in the "+field2+ " field \n");
			else if(fl==1)
				alert("This: " +str+ " email address seems wrong, check the prefix and '@' sign in the " +field1+ " field \n");
			else if(fl==2)
				alert("This: " +str+ " email address seems wrong, check the prefix and '@' sign in  your e-mail field \n");	
			flg++;
		 return;
		}
		if((s3!=-1)||(s4!=-1)||(s5!=-1)||(s6!=-1)||(s7!=-1)||(s8!=-1)||(s9!=-1)||(s10!=-1)||(s11!=-1)||(s12!=-1)||(s13!=-1)||(s14!=-1)||(s15!=-1)||(s16!=-1)||(s17!=-1)||(s18!=-1)||(s19!=-1)||(s20!=-1)||(s21!=-1)||(s22!=-1)||(s23!=-1))
		{
			if(fl==0)
				alert("Please check that your e-mail contains nothing but letters (A-Z), numbers (0-9), the underscore (_), '@' sign or '.' \n in the " +field2+ " field \n");
			else if(fl==1)
				alert("Please check that your e-mail: " +str+ " contains nothing but letters (A-Z), numbers (0-9), the underscore (_), '@' sign or '.'  " +field1+ " field \n");
			else if(fl==2)
				alert("Please check that your e-mail: " +str+ " contains nothing but letters (A-Z), numbers (0-9), the underscore (_), '@' sign or '.'   \n");
			flg++;
			return;
		}
		if((str.lastIndexOf('.')+1)==str.length)
		{ 
			
			if(fl==0)
					alert("Check the '.' sign in the " +field2+ " field \n");
				else if(fl==1)
					alert("E-mail: " +str+ ", please check the '.' sign in the " +field1+ " field \n"); 
				else if(fl==2)
					alert("E-mail: " +str+ ", please check the '.' sign in  your e-mail field \n");
				flg++;
				return;
		}
		
		for (var i=0;i<str.length;i++)
  		{
   			var tst=str.substring(i,i+1)
   			if (tst=='@')
   			{
    			count++;	
   			}
   			if(count>1)
			{
				if(fl==0)
					alert("Check the '@' sign in the " +field2+ " field \n");
				else if(fl==1)
					alert("E-mail: " +str+ ", please check the '@' sign in the " +field1+ " field \n");
				else if(fl==2)
					alert("E-mail: " +str+ ", please check the '@' sign in  your e-mail field \n");
				flg++;
				count=0;
				return;
			}
			if ((tst=='.')&&(str.substring(i-1,i))==(str.substring(i,i+1)))
   			{
				if(fl==0)
					alert("Check the '.' sign in the " +field2+ " field \n");
				else if(fl==1)
					alert("E-mail: " +str+ ", please check the '.' sign in the " +field1+ " field \n"); 
				else if(fl==2)
					alert("E-mail: " +str+ ", please check the '.' sign in your e-mail field \n");
				flg++;
				return;
			}
 		}
		//-------------------------------------------//
		var emailPat=/^(.+)@(.+)$/

		var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
		
		var validChars="\[^\\s" + specialChars + "\]"
		
		var quotedUser="(\"[^\"]*\")"
		
		var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
		
		var atom=validChars + '+'
		
		var word="(" + atom + "|" + quotedUser + ")"
		
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
		
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
		
		var matchArray=str.match(emailPat)
		
		if (matchArray==null) {
  
			//alert("Email address seems incorrect (check @ and .'s)")
			return false
		}
		var user=matchArray[1]
		var domain=matchArray[2]
		
		if (user.match(userPat)==null) {
		    // user is not valid
		    //alert("The username doesn't seem to be valid.")
			if(fl==0)
				alert("This email address seems wrong, check the prefix and '@' .sign in the " +field2+ " field \n");
			else if(fl==1)
				alert("This: " +str+ " email address seems wrong, check the prefix and '@' sign in the " +field1+ " field \n");
			else if(fl==2)
				alert("This: " +str+ " email address seems wrong, check the prefix and '@' sign in your e-mail field \n");	
			flg++;
			 return;
		    
		}
		
		var IPArray=domain.match(ipDomainPat)
		if (IPArray!=null) {
		    // this is an IP address
			  for (var i=1;i<=4;i++) {
			    if (IPArray[i]>255) {
			        //alert("Destination IP address is invalid!")
					if(fl==0)
						alert("Destination IP address is invalid in the " +field2+ " field \n");
					else if(fl==1)
						alert("Destination IP address is invalid in the in the " +field1+ " field \n");
					else if(fl==2)
						alert("Destination IP address is invalid in your e-mail  field \n");	
						flg++;
			 			return;
				
			    }
		    }
		    
		}
		
		// Domain is symbolic name
		var domainArray=domain.match(domainPat)
		if (domainArray==null) {
			//alert("The domain name doesn't seem to be valid.")
			if(fl==0)
				alert("This email address seems wrong, check the prefix and '@' .sign in the " +field2+ " field \n");
			else if(fl==1)
				alert("This: " +str+ " email address seems wrong, check the prefix and '@' sign in the " +field1+ " field \n");
			else if(fl==2)
				alert("This: " +str+ " email address seems wrong, check the prefix and '@' sign in  your e-mail field \n");	
			flg++;
			 return;
		    
		}
		
		var atomPat=new RegExp(atom,"g")
		var domArr=domain.match(atomPat)
		var len=domArr.length
		if (domArr[domArr.length-1].length<2 || 
		    domArr[domArr.length-1].length>3) {
		   // the address must end in a two letter or three letter word.
		   //alert("The address must end in a three-letter domain, or two letter country.")
		   if(fl==0)
				alert("This email address seems wrong, check the prefix and '@' .sign in the " +field2+ " field \n");
			else if(fl==1)
				alert("This: " +str+ " email address seems wrong, check the prefix and '@' sign in the " +field1+ " field \n");
			else if(fl==2)
				alert("This: " +str+ " email address seems wrong, check the prefix and '@' sign in your e-mail field \n");	
			flg++;
			 return;
		   
		}
		if (len<2) {
		   //var errStr="This address is missing a hostname!"
		   //alert(errStr)
		   if(fl==0)
				alert("This email address is missing a hostname, check this address in the " +field2+ " field \n");
			else if(fl==1)
				alert("This: " +str+ " email address is missing a hostname, check this address in the " +field1+ " field \n");
			else if(fl==2)
				alert("This: " +str+ " email address is missing a hostname, check this address in your e-mail field \n");	
			flg++;
			 return;
		   
		}
		
		
		//}
		if (flg == 0)
		{
			returnValue = true;
		}
		else
		{
			returnValue = false;
		} 
		return returnValue;
}

function ignoreSpaces(string) {
	var temp = "";
	string = '' + string;
	splitstring = string.split(" ");
		for(k = 0; k < splitstring.length; k++){
			temp += splitstring[k];
		}
return temp;
}