function IsAlphaNumeric(theField) { //Function to Check if a form field value contains alphanumeric input

   var strInvalidChars = "@¬!\"£$%^*()_+=}{[]|\\<>?/,";
   var strChar;
   var errorFlag = false;

   for (i=0; i < theField.length; i++) {
	  
      strChar = theField.charAt(i);
	  
      if (strInvalidChars.indexOf(strChar) != -1) {
			errorFlag = true;
      }
	  
    }
	 
	if(errorFlag) return false;
	
    return true;
}

function CheckFilled(theElement) {

	if(theElement.value == "") {
		return false;
	}
	
	return true;

}

function checkContactForm(theForm) {

	var errorMsg = "The following errors were encountered:\n\n";
	var errorFlag = false;


	
	// check for job	
	if(!IsAlphaNumeric(theForm.job.value) || !CheckFilled(theForm.job)) {
		errorMsg = errorMsg + "- Job Title is invalid\n";
		errorFlag = true;
	}

	// check for name	
	if(!IsAlphaNumeric(theForm.name.value) || !CheckFilled(theForm.name)) {
		errorMsg = errorMsg + "- Name is invalid\n";
		errorFlag = true;
	}	

	// check for org	
	if(!IsAlphaNumeric(theForm.organisation.value) || !CheckFilled(theForm.organisation)) {
		errorMsg = errorMsg + "- Organisation is invalid\n";
		errorFlag = true;
	}
	
	// check for phone	
	if(!IsAlphaNumeric(theForm.phone.value) || !CheckFilled(theForm.phone)) {
		errorMsg = errorMsg + "- Telephone is invalid\n";
		errorFlag = true;
	}

	// check for postcode	
	if(!IsAlphaNumeric(theForm.postcode.value) || !CheckFilled(theForm.postcode)) {
		errorMsg = errorMsg + "- postcode / zip is invalid\n";
		errorFlag = true;
	}
	
	// check for address	
	if(!IsAlphaNumeric(theForm.address.value) || !CheckFilled(theForm.address)) {
		errorMsg = errorMsg + "- Address is invalid\n";
		errorFlag = true;
	}
	
	
	if(!checkEmail(theForm.cemail.value) || !CheckFilled(theForm.EMAIL)) {
		errorMsg = errorMsg + "You must enter a valid contact email address. \n";
		errorFlag = true;	
	}
	
	if(errorFlag) { //Output any error messages
		alert(errorMsg);
		return false;
		}
	
	return true; //No errors encountered, submit the form

}




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("Email Address Check: Email 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) {
		alert("Email Address Check: This username contains invalid characters.");
		return false;
	}

}

for (i=0; i<domain.length; i++) {

	if (domain.charCodeAt(i)>127) {
		alert("Email Address Check: This domain name contains invalid characters.");
		return false;

	}

}



// See if "user" is valid 

if (user.match(userPat)==null) {

	// user is not valid

	alert("Email Address Check: 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("Email Address Check: Destination IP address is invalid!");

			return false;

		}

	}

	return true;

}



// Domain is symbolic name.  Check if it's valid.

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("Email Address Check: The domain name does not seem to be valid.");

		return false;

	}

}



// domain name seems valid, but now make sure that it ends in a known top-level domain (like com, edu, gov) or a two-letter word, representing country (uk, nl), and that there's a hostname preceding the domain or country. 

if (checkTLD && domArr[domArr.length-1].length!=2 && 

	domArr[domArr.length-1].search(knownDomsPat)==-1) {

	alert("Email Address Check: The address must end in a well-known domain or two letter " + "country.");

	return false;

}



// Make sure there's a host name preceding the domain.

if (len<2) {

	alert("Email Address Check: This address is missing a hostname!");

	return false;

}



// If we've gotten this far, everything's valid!

return true;

}