function checkemail(strmail, mailtype) 
{
var checkTLD=1;
var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
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=strmail.match(emailPat);

	if (matchArray==null) 
	{
		if (mailtype == 1) //Email
			{alert("The EMail address seems incorrect (check @ and .'s)");
			return false;}
		else  //wap mail
			{alert("The WAP Mail address seems incorrect (check @ and .'s)");
			return false;}
	}

	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++) 
	{
		if (user.charCodeAt(i)>127) 
		{
			if (mailtype == 1) //Email
				{alert("Ths Email username contains invalid characters.");
				return false;}
			else  // WAP Mail
				{alert("Ths WAP Mail username contains invalid characters.");
				return false;}
		}
	}
	for (i=0; i<domain.length; i++) 
	{
		if (domain.charCodeAt(i)>127) 
		{
			if (mailtype == 1) //Email
				{alert("Ths Email domain name contains invalid characters.");
				return false;}
			else  // WAP Mail
				{alert("Ths WAP Mail domain name contains invalid characters.");
				return false;}
		}
	}
	if (user.match(userPat)==null) 
	{
		if (mailtype == 1) //Email
			{alert("The Email username doesn't seem to be valid.");
			return false;}
		else  // WAP Mail
			{alert("The WAP Mail username doesn't seem to be valid.");
			return false;}
	}
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) 
	{
		for (var i=1;i<=4;i++) 
		{
			if (IPArray[i]>255) 
			{
				if (mailtype == 1) //Email
					{alert("The Email destination IP address is invalid!");
					return false;}
				else  // WAP Mail
					{alert("The WAP Mail 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) 
		{
			if (mailtype == 1) //Email
				{alert("The Email domain name does not seem to be valid.");
				return false;}
			else  // WAP Mail
				{alert("The WAP Mail domain name does not seem to be valid.");
				return false;}
		}
	}
	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) 
	{
		if (mailtype == 1) //Email
			{alert("The Email address must end in a well-known domain or two letter " + "country.");
			return false;}
		else  // WAP Mail
			{alert("The WAP Mail address must end in a well-known domain or two letter " + "country.");
			return false;}
	}
	if (len<2) 
	{
		if (mailtype == 1) //Email
			{alert("This Email address is missing a hostname!");
			return false;}
		else  // WAP Mail
			{alert("This WAP Mail address is missing a hostname!");
			return false;}
	}
	if (mailtype == 1) //Email
	{
		var firstchar = checkfirstcharacter(strmail) ;
		if (firstchar == false)  { 	return false;	}
	}
	return true;
}

function trim(strfield) 
{
var loop;
var space;
var strtmp;
	
	strtmp=strfield;
	space=0;
	for(loop=0;strtmp.substr(loop,1)==" " && loop < strtmp.length; loop++, space++);
		strtmp=strtmp.substr(space,(strtmp.length-space))
	
	space=0;
	for(loop=strtmp.length;strtmp.substr(loop-1,1)==" " && loop>0;loop--,space++);
		strtmp=strtmp.substr(0,strtmp.length-space)
	return strtmp;
	
}

function checkfirstcharacter(varstring)
{
	var txtnumber = /^[0-9]/;
	var spchar = /^[@#~!%&*()^-_$=+{}[]|\/:;"'<,>.?]/;
	var firstchar = varstring.substr(0,1);

	if (firstchar.match(txtnumber) != null) 
	{
		alert("Invalid entry. Numbers are not allowed as a first character.");
		return false;
	}
		
	if (firstchar.match(spchar) != null)
	{
		alert("Invalid entry. Special characters are not allowed as a first character.");
		return false;
	}

	return true;

}

function allowfloatnumber()
{ 
	if (event.keyCode >= 48 && event.keyCode <= 57)
	   { event.keyCode = event.keyCode; }
	else
	   {
	   if (event.keyCode == 46) 
		  {	  event.keyCode = event.keyCode; }
	   else
	      {   event.keyCode = 0;      }
	   }
}

function allowwholenumber()
{ 
	if (event.keyCode >= 48 && event.keyCode <= 57)
	   {event.keyCode = event.keyCode; }
	else
	   {event.keyCode = 0;}
}

function allowalphabetonly()
{
	if ((event.keyCode >= 65 && event.keyCode <= 90) || (event.keyCode >= 97 && event.keyCode <= 122))
		{event.keyCode = event.keyCode; }
	else
		{event.keyCode = 0;}
}

function printWindow(){
	bV = parseInt(navigator.appVersion);
	if (bV >= 4) window.print();
}

function dateDifference(strDate1,strDate2){
     datDate1= Date.parse(strDate1);
     datDate2= Date.parse(strDate2);
     return ((datDate2-datDate1)/(24*60*60*1000));
     
}
