// JScript source code
// Feedback Form Validation Routine

function formvalidation(theform) {
	function GetSelectValue(ele)
	{
	    return ele.options[ele.selectedIndex].value;
	}
	if (GetSelectValue(theform.department) == "none") {
		alert("Please select a department to contact");
		theform.department.focus();
		return false;
	}	
	if (theform.name.value.length<2) {
	    alert('Please fill in your name');
	    theform.name.focus();
	    return false;
	}				
	if (!emailCheck(theform.email.value)) {
	    theform.email.focus();
	    return false;
	}
	//if (theform.phone.value == "") {
	//    alert('Please fill in your phone number');
	//    theform.phone.focus();
	//    return false;
	//}
	if (theform.comment.value == "") {
	    alert('Please fill in your comments or questions');
	    theform.comment.focus();
	    return false;
	}							
}

		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 + ")";
		// The following pattern describes the structure of the user
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
		var matchArray=emailStr.match(emailPat);
		if (matchArray==null) {
		        alert("Incorrect email address (check @ and .'s)");
		        return false;
		}
		var user=matchArray[1];
		var domain=matchArray[2];

		// See if "user" is valid 
		if (user.match(userPat)==null) {
		    // user is not valid
		    alert("The username doesn't seem to be valid.");
		    return false;
		}

		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!");
					return false;
				}
		    }
		    return true;
		}

		// Domain is symbolic name
		var domainArray=domain.match(domainPat);
		if (domainArray==null) {
		        alert("The domain name doesn't 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) {
		   // 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.");
		   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.)";
		}

		if (domArr[domArr.length-1].length==3 && len<2) {
		   var errStr="This address is missing a hostname!";
		   alert(errStr);
		   return false;
		}
		// If we've gotten this far, everything's valid!
		return true;
		}	

function validateTxt (sTextValue, iLength, sFieldName) 
{
	var iReturn = false;        
	 
	if (sTextValue.length > iLength)		
	{
		var sMsgText = new String()

		sMsgText	= "The length of \'" + sFieldName + "\' should be no greater than " + iLength + " characters.\n\n"
					+ "The length of \'" + sFieldName + "\' is " + sTextValue.length + " characters. \n"
					+ "Please reduce \'" + sFieldName + "\' by " + + (sTextValue.length - iLength) + " characters.";
		
		alert(sMsgText);    

		iReturn = false; 
	}
	else
	{
		iReturn = true;
	}
	
	return (iReturn);
}

//-->

function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else 
countfield.value = maxlimit - field.value.length;
}
