
var alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var numerals="1234567890"

function check(theForm, theField, displayName, min, max, type, special)
{
	/* COMMENTS ON HOW TO USE THIS FUNCTION
	   ====================================
	    theForm = Name OF Form
		theField = Name of field used in the HTML form
		displayName = Name of field to be displayed in message box
		min = minimum charactres required
		max = maximum characters required
		type = type of field .... 
				'characters' for string ( one line text box )
				'digits' for numeric ( one line text box )
				'dropdown' for combo box
				'checkbox' for a group of checkboxes
				'email' for email address
				'ccnumber' for credit card number
				'none' for all types of data
		special = variable used for various purposes in diff. cases like ...
					
					for textbox, special = special characters allowed in the field 
											  in addition to its actual datatype
										
			for checkbox group, special = total number of check boxes in the group
	
	*/

						
		if (theForm[theField].value.length =="") 
		{
			message=displayName + " is required."
			alert(message);
			theForm[theField].focus();
			return (false);
		}
		
		if (min!="null" && theForm[theField].value.length < min) 
		{
			
			if(type=="digits" || type=='ccnumber')
				message=displayName + " must contain at least " + min + " digits."
			else
				message=displayName + " must contain at least " + min + " characters."	
			
			alert(message);
			theForm[theField].focus();
			return (false);
		}
		
		if (max!="null" && theForm[theField].value.length > max) 
		{
			if(type=="digits" || type=='ccnumber')
				message=displayName + " must contain at most " + max + " digits."
			else
				message=displayName + " must contain at most " + max + " characters."
	
			alert(message);
			theForm[theField].focus();
			return (false);
		}
	
	
		if(!checkdatatype(theForm[theField].value, type, special))
		{
			message=displayName + " does not appear to be valid. Please check your format."
			alert(message);
			theForm[theField].focus();
			return (false);
		}

				
		if(type=="email")
			{
			
			if(!checkEmail(theForm[theField].value))
				{
				theForm[theField].focus()
				return(false)
				}
			else
				return(true)				

			}
		

	return(true);
}

function checkdatatype(string, type, special) {
		
		if(type=='none')
				return(true)
		
		if(special==null)
			special=''
		
		else if(type=='characters')
				special = special + alphabet
		
		else if(type=='digits')
				special = special + numerals
		
		else if(type=='email')
				special = special + numerals + alphabet + "@."
				
		else if(type=='ccnumber')
				special = special + numerals
		
				
		var result = true;
			
		for (i = 0;  i < string.length;  i++) 
			{
			ch = string.charAt(i);
			for (j = 0;  j < special.length;  j++)
				{
				 if (ch==special.charAt(j))
						{ 
						  break;
						}
				 if (j == special.length-1) 
						{
							result = false;
							break;
						}
				}
	
			}
		
		return(result);
			
}


function checkEmail (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("Invalid Email address ! Please 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) {
			alert("Invalid Email address ! The username contains invalid characters.");
			return false;
   			}
		}

	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			alert("Invalid Email address ! The domain name contains invalid characters.");
			return false;
   		}
	}

	if (user.match(userPat)==null) {
			alert("Invalid Email address ! The 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) {
						alert("Invalid Email address ! 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) {
			alert("Invalid Email address ! The 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) {
			alert("Invalid Email address ! The address must end in a well-known domain or two letter " + "country.");
			return false;
		}

	if (len<2) {
		alert("Invalid Email address ! This address is missing a hostname!");
		return false;
	}

	return true;
}


