// JavaScript Document
function verifyEmail(email) {
/*	new version by Laurence 2004 Nov 26; old version commented-out below
	RegExp is a Javascript1.2 feature; tested successfully in WinIE 5.0+, MacIE 5.1, Safari 1.0, WinNN 4.7, WinNN 6.2, WinMoz 1.7
	The pattern matches: 
	- at least one word character ( a-zA-Z0-9_ )
	- followed by any number more including periods
	- then a single @
	- then at least one alphanumeric character plus any number more including hyphens and periods
	- plus an ending of an alphanumeric char, a single period, and at least 2 more letters. 
	There can be nothing before or after the pattern; all whitespace, punctuation, etc. not explicitly allowed is forbidden.
*/
	var myRegExp = /^[\w][\w\.]*@[a-zA-Z0-9]+[\w\.\-]*[a-zA-Z0-9]\.[a-zA-Z]{2,}$/;
	var sPos = email.search(myRegExp);
	if (sPos >= 0) {
		return true;
	} else {
		return false;
	}
}

function validatePhone(number) {
/*	new version by Laurence 2004 Nov 26; old version commented-out below 
	RegExp is a Javascript1.2 feature; tested successfully in WinIE 5.0+, MacIE 5.1, Safari 1.0, WinNN 4.7, WinNN 6.2, WinMoz 1.7
	The pattern matches groups of 3, 3 and 4 digits found in that order anywhere in string 
*/
	var myRegExp = /(\d{3}).*(\d{3}).*(\d{4})/;	
	var sPos = number.search(myRegExp);
	if ( sPos >= 0 ) {
		number = "(" + RegExp.$1 + ")" + RegExp.$2 + "-" + RegExp.$3;
		return true;
	} else {
		return false;
	}
}


function validateEmail(thisForm) {
	var theForm = thisForm;
	var emptyFields = "";
	
   if (theForm.emailAddress.value == ""){
		emptyFields += "The email address cannot be blank." + "\n" ;
	} else if (!verifyEmail(theForm.emailAddress.value)) {
		emptyFields += "The email address must be correctly formatted." + "\n" ;
	}
	
	if (emptyFields.length >= 1) {
		alert("Please ensure that the following fields are completed correctly:" + "\n" + emptyFields);
		return false;
	} else {
		return true;
	}
}

function validateSample(thisForm) {
	var theForm = thisForm;
	var emptyFields = "";
	
	 if (theForm.name.value == ""){
		emptyFields += "The name cannot be blank." + "\n" ;
	}
	
	 if (theForm.organization.value == ""){
		emptyFields += "The organization cannot be blank." + "\n" ;
	}
	
	 if (theForm.phone.value == ""){
		emptyFields += "The phone number cannot be blank." + "\n" ;
	} else if (!validatePhone(theForm.phone.value)) {
		emptyFields += "The phone number must be correctly formatted." + "\n" ;
	}
	
   if (theForm.emailAddress.value == ""){
		emptyFields += "The email address cannot be blank." + "\n" ;
	} else if (!verifyEmail(theForm.emailAddress.value)) {
		emptyFields += "The email address must be correctly formatted." + "\n" ;
	}
	
	if (emptyFields.length >= 1) {
		alert("Please ensure that the following fields are completed correctly:" + "\n" + emptyFields);
		return false;
	} else {
		return true;
	}
}
