// ****************************************************
// *** Script By John Romano Rafol | March 13, 2001 ***
// ****************************************************
// ---------------------------------------------------------------------------
// Function to check if field is valid e-mail format
//
// emailStr  - email string
// 
// Created : Adapted from Sandeep V. Tamhankar (stamhankar@hotmail.com) 
// Source  : http://javascript.internet.com/ (The Javascript source)
//
// ---------------------------------------------------------------------------
function emailCheck (emailStr) {

	var emailPat = /^(.+)@(.+)$/;
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var firstChars=validChars;
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom="(" + firstChars + validChars + "*" + ")";
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null) {
		alert("Email address incorrect (check (@) and (.)s).");
		return false;
	}
	
	var user=matchArray[1];
	var domain=matchArray[2];

	if (user.match(userPat)==null) {
	    alert("Username portion does not 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) {
		        alert("Destination IP address is invalid!");
				return false;
		    }
	    }
	    return true;
	}

	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		alert("The domain name does not seem to be valid.");
    	return false;
	}

	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) {
   		alert("The address must end in a three-letter domain, or two letter country.");
   		return false;
	}


	if (domArr[domArr.length-1].length==2 && len<3) {
   		var errStr="This address ends in two characters, which is a country";
   		errStr+=" code.  Country codes must be preceded by ";
   		errStr+="a hostname and category (like com, co, pub, pu, etc.)";
   		alert(errStr);
   		return false;
	}


	if (domArr[domArr.length-1].length==3 && len<2) {
   		var errStr="This address is missing a hostname!";
   		alert(errStr);
   		return false;
	}

	return true;
}

function isDecimal(strValue)
{
	if (isNaN(parseFloat(strValue)))
		return(false)
	else
		return(true);
}

function isInteger(strValue)
{
	if (isNaN(parseInt(strValue)))
		return(false)
	else
		return(true);
}


function isValidExt(strValue)
{
	if (strValue.length < 3)
		return(false)
	else if (isNaN(parseInt(strValue)))
		return(false)
	else
		return(true);
}

function isTelephone(strValue)
{
	var intLen, intCtr, intAscCode;

	intLen=(strValue.length);
	for (intCtr=0; intLen > intCtr; intCtr++) 
	{
		intAscCode = strValue.charCodeAt(intCtr);
		if ( !(((intAscCode >= 48) && (intAscCode <= 57))  ||
		       ((intAscCode == 45) || (intAscCode == 32))) )	//*** 45 is a hypen while 32 is a space. 48-57 is the 0-9 numbers
		{	
			return(false);
		}
	}

	if (intLen < 6)
		return(false);
	else
		return(true);
}	

function isAlphaNumeric(strValue)
{
	var intCtr;
	intLen = strValue.length;
	
	for (intCtr=0; intLen > intCtr; intCtr++) 
	{
		intAscCode = strValue.charCodeAt(intCtr);
		
		if ( !(((intAscCode >= 48) && (intAscCode <= 57))    || //*** 48-57  are the 0-9 numbers
		       ((intAscCode >= 65) && (intAscCode <= 90))    || //*** 65-90 are the A-Z capital letters		
		       ((intAscCode >= 97) && (intAscCode <= 122))) )   //*** 97-122 are the a-z small letters	
		{	
			return(false);
		}
	}
	return(true);
}

function empty_string(strValue)
{
	var ctr, int_length, empty = true;
	int_length = strValue.length;

	for (ctr=0;ctr<int_length;ctr++)
	{
		if (strValue.charAt(ctr) != "" && strValue.charAt(ctr) != " " && strValue.charCodeAt(ctr) != 13  && strValue.charCodeAt(ctr) != 10)
			empty = false;
	}

	if ((strValue.length == 0) || (empty))
		return(true);

	return(false);
}

function ValidateForm(objForm)
{
	var strValidationMsg = "";
	var i = objForm.tags("INPUT");
	var i2 = objForm.tags("TEXTAREA");	
	var ctr=0, ctr2=0, intIndex=0;
	var boolSubmit = false;

	for (ctr=0; ctr < i.length; ctr++)
	{
		if ((i.item(ctr).type == "text" || i.item(ctr).type == "password") && !(i.item(ctr).disabled) && i.item(ctr).propRequired != "no")
		{
			if ( (empty_string(i.item(ctr).value)) || (i.item(ctr).propExtField == "yes" && !isValidExt(i.item(ctr).value)) || (i.item(ctr).propTelField == "yes" && !isTelephone(i.item(ctr).value)) )
				intIndex ++;

			strValidationMsg = strValidationMsg + ((empty_string(i.item(ctr).value) || (i.item(ctr).propExtField == "yes" && !isValidExt(i.item(ctr).value)) || (i.item(ctr).propTelField == "yes" && !isTelephone(i.item(ctr).value)) ) ? intIndex.toString() + ". " + i.item(ctr).name.replace(/_/g," ") + "\n" : "");
		}
	}
	
	
	for (ctr2=0; ctr2 < i2.length; ctr2++)
	{
		if (!(i2.item(ctr2).disabled) && i2.item(ctr2).propRequired != "no")
		{
			if ( empty_string(i2.item(ctr2).value) )
				intIndex ++;

			strValidationMsg = strValidationMsg + (empty_string(i2.item(ctr2).value ) ? intIndex.toString() + ". " + i2.item(ctr2).name.replace(/_/g," ") + "\n" : "");
		}
	}	

	if (strValidationMsg=="")
	{
		return (true); //*** Validation Criterions Passed, go forth and process the form ***
	}
	else
	{
		alert( "The following fields either has an invalid entry or empty  : \n\n" + strValidationMsg); //*** Oops, I think there's an error ***
		return (false)
	}	
}	


function InValidEmail(emailStr)
{
	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=emailStr.match(emailPat);

	if (matchArray==null)
	   {
	    alert("Email address seems incorrect (check @ and .'s)");
	    return true;
	   }

	var user=matchArray[1];
	var domain=matchArray[2];

	for (i=0; i<user.length; i++)
	   {
	    if (user.charCodeAt(i)>127)
	       {
		alert("This username contains invalid characters.");
		return true;
	       }
	   }

	for (i=0; i<domain.length; i++)
	    {
	     if (domain.charCodeAt(i)>127)
		{
		 alert("Ths domain name contains invalid characters.");
		 return true;
		}
	    }

	if (user.match(userPat)==null)
	   {
	    alert("The username doesn't seem to be valid.");
	    return true;
	   }

	var IPArray=domain.match(ipDomainPat);

	if (IPArray!=null)
	   {
	    for (var i=1;i<=4;i++)
		{
		 if (IPArray[i]>255)
		    {
		     alert("Destination IP address is invalid!");
		     return true;
		    }
		}
	    return false;
	   }

	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 does not seem to be valid.");
		 return true;
		}
	    }

	if (checkTLD && domArr[domArr.length-1].length!=2 &&
	    domArr[domArr.length-1].search(knownDomsPat)==-1)
	   {
	    alert("The address must end in a well-known domain or two letter " + "country.");
	    return true;
	   }

	if (len<2)
	   {
	    alert("This address is missing a hostname!");
	    return true;
	   }

	return false;

}