function autotab(field,fieldLen) {
if (!/^\d+$/.test(field.value)) { //no alpha only numeric digits for studentnum
field.value = '';
return false;
} 
if (field.value.length<fieldLen) return true; //max char is 8 - still room
var el, e = 0, f = field.form;
while (el = f.elements[e++]) if (el == field) break; //find field position
if(event.keyCode != 9){
f.elements[e].focus(); //move focus
f.elements[e].select();//highlight the field
}
return false;
}	

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0); 
}

function isValidDate(day,month,year){
/*
Purpose: return true if the date is valid, false otherwise

Arguments: day integer representing day of month
month integer representing month of year
year integer representing year

Variables: dteDate - date object

*/
var dteDate;

//set up a Date object based on the day, month and year arguments
//javascript months start at 0 (0-11 instead of 1-12)
dteDate=new Date(year,month,day);

/*
Javascript Dates are a little too forgiving and will change the date to a reasonable guess if it's invalid. We'll use this to our advantage by creating the date object and then comparing it to the details we put it. If the Date object is different, then it must have been an invalid date to start with...
*/

return ((day==dteDate.getDate()) && (month==dteDate.getMonth()) && (year==dteDate.getFullYear()));
}

/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Alan Gruskoff | http://www.performantsystems.com/ */

function anyCheck(form,fieldname) {
var total = 0;
var max = eval("form." + fieldname + ".length");
for (var idx = 0; idx < max; idx++) {
if (eval("document.frmVolunteer." + fieldname + "[" + idx + "].checked") == true) {
    total += 1;
   }
}
return total;
}

function validateForm(){
	theForm = document.frmVolunteer;
	
	//check all fields are entered
	if (trim(theForm.lastname.value) == ''){
		alert('Last Name is required.');
		return false;
	}
	else if (trim(theForm.firstname.value) == ''){
		alert('First Name is required.');
		return false;
	}
	else if (
		(trim(theForm.bdayMM.value) == '') ||
		(trim(theForm.bdayDD.value) == '') ||
		(trim(theForm.bdayYY.value) == '')
	){
		alert('Birthdate is required.');
		return false;
	}
	else if (
		(trim(theForm.phoneareacode.value) == '') ||
		(trim(theForm.phone3.value) == '') ||
		(trim(theForm.phone4.value) == '')
	){
		alert('Phone is required.');
		return false;
	}
	else if (trim(theForm.email.value) == ''){
		alert('Email is required.');
		return false;
	}
	else if (anyCheck(theForm,'voltype') == 0){
		alert('"Are you?" must be selected');
		return false;
	}
	else if (anyCheck(theForm,'training') == 0){
		alert('Training must be selected.');
		return false;
	}
			
	//validate date
	theMonth = theForm.bdayMM.value - 1;
	if(!(isValidDate(theForm.bdayDD.value,theMonth,theForm.bdayYY.value))){
		alert('Birthdate must be a valid date.');
		return false;
	}
		
	//check email has @
	if(!(isValidEmail(theForm.email.value))){
		alert('Email must be valid.');
		return false;
	}
	
	//school selected if student/teacher/parent
	if(
		(theForm.voltype[0].checked || 
		 theForm.voltype[2].checked || 
		 theForm.voltype[3].checked) && (theForm.site.value == "")){
		alert('School must be selected for Parent/Student/Teacher');
		return false;
	}
	
	if((theForm.voltype[4].checked) && (trim(theForm.otherdescr.value) == "")){
		alert('"Other" option requires additional information.');
		return false;
	}
	
	return true;
}