// JavaScript Document
/* FORM validation functions


*/
//regular expressions
var email_reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/
var phone_reg = /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,3})|(\(?\d{2,3}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/

//validation functions
function hasValue(ff,msg){
	if(!ff.value){
		errorList[errorList.length] = msg;
	}
}
function isPhone(ff,msg){
	if(ff.value){
		if(!phone_reg.test(ff.value)){
			errorList[errorList.length] = msg;
		}
	} else {
		errorList[errorList.length] = msg;
	}
}
function checkExperience(ff,msg){
	if(ff.value){
		for (counter = 0; counter < ff.experience.length; counter++){
			if (ff.experience[counter].checked)
				experience = "yes"; 
			}
			if (!experience){
				errorList[errorList.length] = msg;
			} else {
			errorList[errorList.length] = msg;
		}
	}
}
function isEmail(ff,msg){
	if(ff.value){
		if(!email_reg.test(ff.value)){
			errorList[errorList.length] = msg;
		}
	} else {
		errorList[errorList.length] = msg;
	}
}
function checkState(ff,msg){
	if(ff.selectedIndex == 0){
		errorList[errorList.length] = msg;
	}
}
function checkEducation(ff,msg){
	if(ff.selectedIndex == 0){
		errorList[errorList.length] = msg;
	}
}
function checkMonthAvail(ff,msg){
	if(ff.selectedIndex == 0){
		errorList[errorList.length] = msg;
	}
}
function checkDayAvail(ff,msg){
	if(ff.selectedIndex == 0){
		errorList[errorList.length] = msg;
	}
}
function checkYearAvail(ff,msg){
	if(ff.selectedIndex == 0){
		errorList[errorList.length] = msg;
	}
}

function checkInterest(f,msg){
	for(i=1;i<=10;i++){
		if(f["interest" + i].checked){
			return;
		}
	}
	errorList[errorList.length] = msg;
}
function checkFileName(ff,msg){
	var RN = ff.value;
	var L = RN.length;
	var S = 0;
	while(RN.indexOf('\\',S) != -1){
		var TI = RN.indexOf('\\',S)
		if(TI != -1){
			S = TI+1;
		}
	}
	var LSlash = RN.indexOf('\\',S);
	var FileName = RN.substr(S,255);
	if(FileName.indexOf(' ') == -1){
		return;
	}
	errorList[errorList.length] = msg;
}
//check fields function
function validateForm(f){
	errorList = new Array();
	
	hasValue(f.first_name,"First Name is required");
	hasValue(f.last_name,"Last Name is required");
	isEmail(f.email,"Email is invalid or missing");
	hasValue(f.address,"Mailing Address is required");
	hasValue(f.city,"City is required");
	checkState(f.state,"Selecting a State is required");
	hasValue(f.zip_code,"Zip Code is required");
	hasValue(f.country,"Country is required");
	isPhone(f.phone1,"Primary Phone Number is invalid or missing\n---Use xxx-xxx-xxxx format for phones");
	checkExperience(f.experience,"Please indicate whether you have experience teaching or not.");
	checkEducation(f.hs_grad,"Please Select Your Level of Education");
	checkMonthAvail(f.monthAvail,"Please Select the month you are available.");
	checkDayAvail (f.dayAvail,"Please select the day you are available.");
	checkYearAvail (f.yearAvail,"Please select the year you are available.");
	checkInterest(f,"You must select at least one interest.");
	checkFileName(f.resume,"There should be no spaces in your resume file.");
	
	if(errorList.length > 0){
		var error_msg = "Errors:";
		L = errorList.length;
		for(i=0;i<L;i++){
			error_msg += "\n" + errorList[i];	
		}
		window.alert(error_msg);
		return false;
	}
	return true;
}

var isIE = (document.all) ? 1 : 0;
function keyFilter(e, strPattern){
	var chr = (isIE) ? e.keyCode : e.which;
	var ch = String.fromCharCode(chr);

	if (chr != 13 && chr != 8 && chr != 0){
		var re = new RegExp(strPattern);

		if (ch.search(re) == -1){
			if(isIE)
				e.returnValue = false;
			else
				e.preventDefault();
		}
	}
} 