// JavaScript Document


var answerKeyArray = new Array();
answerKeyArray[0] = /--/; //double dashes
answerKeyArray[1] = /^[a-zA-Z0-9 .#-]+$/; //alphaNum1
answerKeyArray[2] = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/; //email
answerKeyArray[3] = /^[a-zA-Z0-9 -]+$/; //alphaNum
answerKeyArray[4] = /^[a-zA-Z0-9 .()-]+$/; //numericPhone
answerKeyArray[5] = /^[a-zA-Z0-9 ._-]+$/; //alphaNum2
answerKeyArray[6] = /^[a-zA-Z0-9_-]+$/; //alphaNum3
answerKeyArray[7] = /^[0-9 ]+$/; //numeric
answerKeyArray[8] = /^[a-zA-Z0-9 ]+$/ //alphaNumStrict
answerKeyArray[9] = /^[a-zA-Z0-9 !@#$.,-_\r]+$/ //varchar (almost)
answerKeyArray[10] = /^[ ]/;//spaces

var testTypeArray = new Array();
testTypeArray[0] = "testerName";
testTypeArray[1] = "testerDropDown"; //if your calling this tester, see NOTE (below) at function
testTypeArray[2] = "testerPhone"; //this is for validating a phone number thats divided up into 3 input fields
testTypeArray[3] = "testerPwCompare"; //this function is for comparing a passord input and a re-type password input
testTypeArray[4] = "testerState"; //this function compares the state drop-down to the foreign input field

var failMessageArray = new Array();
failMessageArray[0] = "Invalid Character(s)";
failMessageArray[1] = "Cannot contain two or more consecutive dashes";
failMessageArray[2] = "Required selection from drop-down";
failMessageArray[3] = "Required field";
failMessageArray[4] = "Invalid e-mail address";
failMessageArray[5] = "Entry exceeds maximum length";
failMessageArray[6] = "Does not meet minimum required length";
failMessageArray[7] = "Area Code: ";
failMessageArray[8] = "Prefix: ";
failMessageArray[9] = "Line Number: ";
failMessageArray[10] = "Extention: ";
failMessageArray[11] = "Passwords do not match";
failMessageArray[12] = "Required if 'Not U.S. or Canada' is selected in State/Province drop-down";
failMessageArray[13] = "Clear Foreign Province if a state is selected in drop-down";


//this is a function template. copy/paste (and modify) it to each page you're validating
//this function should be duplicated for each page that you want to validate.
//duplicate this form, and give it a new name, 
//then call that specific name on the form's onsubmit

/*

function ____Exam() {
	
	// teacher(1,2,3,4,5,6,7) params are as follows: 
	//--------------------------------
	// 1. element ID name,
	// 2. type of validation (refer to testTypeArray),
	// 3. param option
	// 4. param option
	// 5. param option
	// 6. param option
	// 7. param option	(typically passed as 'yes' or 'no'-- yes being onSubmit, no being onBlur)
	//--------------------------------	
	// "params options" are dependant on which 'type of validation' is called (param 2)	
	
	q3 = teacher('school',0,1,0,50,0,'yes');
	q2 = teacher('title',0,1,1,30,0,'yes');
	q1 = teacher('name',0,1,1,30,0,'yes');
	
	//set this variable to the numnber of validations you do (above). IE, if you have up to q7, set qnum to 7
	var qnum = 3;
	//this var is initially set to pass, if there is an error in one of the validations, it gets set to fail
	var finalScore = "pass";
	//this loops through all the validations (IE. q1, q2, etc), if it finds 1 failure, it immediately stops and sets finalScore as "fail"
	var s = 1;		
	for (s = 1; s<=qnum; s++) {		
		if (window["q"+s] == "fail") {			
			finalScore = "fail";			
			break;
		}
	}
	//finalScore in this IF statement determins whether the form is stopped or not
	if (finalScore == "fail") {
		return false;
	}else {
		return true;
	}	
}

*/

//this function gets called onblur of each element, and it also gets called when the page's form gets submitted.
function teacher(textField,type,keyType,required,maxLength,minLength,finalExam) {	
	//this var gets the string name of of the appropriate test from the testTypeArray
	var callTester = testTypeArray[type];
	//this var dynamically calls the proper test function and gets set at "pass" or "fail" 
	var grade = window[callTester](textField,keyType,required,maxLength,minLength,finalExam);
	return grade;
}

//_________________________________________________________________________________________________
//											BEGIN Testers										   
//_________________________________________________________________________________________________

//this tester can handle any text input field. including email inputs 
//give it the correct keyType param (based off of answerKeyArray) to set the correct level of validation strictness
function testerName(textField,keyType,required,maxLength,minLength,finalExam) {	

	var student = document.getElementById(textField);	
	var studentFail = document.getElementById(textField+"_error");
	var answer = answerKeyArray[keyType];
	var rawspace = answerKeyArray[10];
	var final = "";	
	var noDashes = student.value.search(answerKeyArray[0]);
	
	
	if (student.value == "") {
		if (required == 0){
			final = "pass";				
		}else {
			final = "fail";
			failMessage = failMessageArray[3];
		}
	}else if (noDashes != -1) { //test for double dahes
		final = "fail"; 
		failMessage = failMessageArray[1];		
	}else if (student.value.match(answer)) { //test that it matches the reg expression
		final = "pass"; 
	}else {
		final = "fail";
		if (keyType == 2) { //if testing email, dislay correct error message
			failMessage = failMessageArray[4];
		}else {
			failMessage = failMessageArray[0];
		}
	}
	
	//testing max length
	if (maxLength != 0 ) {	//if maxLength is 0, dont test for max length	
		if (student.value.length > maxLength) {
			final = "fail";
			failMessage = failMessageArray[5];
		}
	}
	
	//test min length
	if (minLength != 0) { //if minLength is 0, dont test for min length
		if (student.value.length < minLength && student.value.length > 0) {
			final = "fail";
			failMessage = failMessageArray[6];
		}
	}
	
	//test for a bunch of spaces (any/all spaces = failure(if the field is required))
	if (student.value.match(rawspace) && required == 1) {
		final = "fail";
		failMessage = failMessageArray[3];
	}
	
	if (final == "fail") {
		student.className = 'text-box-red-error';
		studentFail.firstChild.data = failMessage;
		studentFail.style.display = "inline"
		if (finalExam == "yes") {
			student.focus();
			student.select();
		}
		return final;		
	}else {
		student.className = 'text-box-gray-border';
		studentFail.style.display = "none";
		return final;		
	}					
}

//this function only NEEDS the following params: field,required,finalExam.  all other params can be entered as 0 (zero).
function testerDropDown(field,keyType,required,maxLength,minLength,finalExam) {
	var student = document.getElementById(field);
	var studentFail = document.getElementById(field+"_error");
	var final = "";
	
	if (student.value == -1 && required == 1) {
		final = "fail";
		failMessage = failMessageArray[2];
	}else {
		final = "pass";		
	}
	
	if (final == "fail") {
		student.className = 'text-box-red-error';
		studentFail.firstChild.data = failMessage;
		studentFail.style.display = "inline";
		if (finalExam == "yes") {
			student.focus();
		}
		return final;
	}else {
		student.className = 'drop-down';
		studentFail.style.display = "none";
		return final;
	}
}

//this 
function testerPhone(textField,keyType,required,phonePiece,minLength,finalExam) {
	var student = document.getElementById(textField);
	var studentFail = document.getElementById(textField+"_error");
	var answer = answerKeyArray[keyType];
	var final = "";
		
	switch(phonePiece) {
		case 1:
			failMessage = failMessageArray[7];
			break;
		case 2:
			failMessage = failMessageArray[8];
			break;
		case 3:
			failMessage = failMessageArray[9];
			break;
		case 4:
			failMessage = failMessageArray[10];
			break;
		default:
			failMessage = '';
	}
		
	if (student.value == '' && required == 1) {		
		final = 'fail';
		failMessage = failMessage + failMessageArray[3];
	}else if (required == 1 && minLength != 0 && student.value.length < minLength) {
		final = 'fail';
		failMessage = failMessage + failMessageArray[6];
	}else if (required != 1 && student.value != '' && student.value.length < minLength) {
		final = 'fail';
		failMessage = failMessage + failMessageArray[6];		
	}else if (student.value.match(answer) || student.value == '') {		
		final = 'pass';
	}else {		
		final = 'fail';
		failMessage = failMessage + failMessageArray[0];
	}
	
	if (final == 'fail') {
		student.className = 'text-box-red-error';
		studentFail.firstChild.data = failMessage;
		studentFail.style.display = 'inline';
		if (finalExam == 'yes'){
			student.focus();
			student.select();
		}
		return final;		
	}else {
		student.className = 'text-box-gray-border';
		studentFail.style.display = 'none';
		return final;
	}	
}

function testerPwCompare(textField,partner,required,maxLength,minLength,finalExam) {
	var student = document.getElementById(textField);
	var studentFail = document.getElementById(textField+"_error");
	var studentPartner = document.getElementById(partner);
	var final = "";
	
	if (student.value == '' && required == 1) {
		final = 'fail';
		failMessage = failMessageArray[3];
		var redBoth = 0;
	}else if(student.value != studentPartner.value) {
		final = 'fail';
		failMessage = failMessageArray[11];
		var redBoth = 1;
	}else {
		final = 'pass';
	}
	
	if (final == "fail") {
		student.className = 'text-box-red-error';
		if (redBoth == 1) {
			studentPartner.className = 'text-box-red-error';
		}
		studentFail.firstChild.data = failMessage;
		studentFail.style.display = 'inline';
		if (finalExam == 'yes') {
			if (redBoth == 1) {
				studentPartner.focus();
				studentPartner.select();
			}else {				
				student.focus();
				student.select();
			}
		}
		return final;
	}else {
		student.className = 'text-box-gray-border';
		studentPartner.className = 'text-box-gray-border';		
		studentFail.style.display = 'none';
		return final;
	}
}

function testerState(textField,colleague,keyType,maxLength,minLength,finalExam) {
	var student = document.getElementById(textField);
	var studentFail = document.getElementById(textField+"_error");
	var studentColleague = document.getElementById(colleague);
	var studentSpecial = document.getElementById(colleague+"_special");
	var answer = answerKeyArray[keyType];
	var final = '';
	
	switch(textField) {
		case "state":			
			if (student.value == -1) {
				final = 'fail';
				failMessage = failMessageArray[2];
				studentSpecial.style.display = 'none';				
				studentColleague.value = '';
			}else if(student.value == 'NA') {
				final = 'pass';
				studentSpecial.style.display = 'inline';
				studentColleague.focus();
			}else {
				final = 'pass';
				studentSpecial.style.display = 'none';				
				studentColleague.value = '';
			}
			
			if (final == 'fail') {
				student.className = 'text-box-red-error';
				studentFail.firstChild.data = failMessage;
				studentFail.style.display = 'inline';
				if (finalExam == 'yes') {
					student.focus();					
				}
				return final;
			}else {
				student.className = 'drop-down';
				studentFail.style.display = 'none';
				return final;
			}
			break;
			
		case "f":			
			if (student.value == '' && studentColleague.value == 'NA') {
				final = 'fail';
				failMessage = failMessageArray[3];
			}else if(student.value.match(answer) || student.value == '') {
				final = 'pass';	
			}else {
				final = 'fail';
				failMessage = failMessageArray[0];
			}
			
			if (final == 'fail') {
				student.className = 'text-box-red-error';
				studentFail.firstChild.data = failMessage;
				studentFail.style.display = 'inline';
				if (finalExam == 'yes') {
					student.focus();
					student.select();
				}
				return final;
			}else {
				student.className = 'text-box-gray-border';
				studentFail.style.display = 'none';
				return final;
			}
			break;
			
		//this is the same as "f" case
		default:
			if (student.value == '' && studentColleague.value == 'NA') {
				final = 'fail';
				failMessage = failMessageArray[3];
			}else if(student.value.match(answer) || student.value == '') {
				final = 'pass';	
			}else {
				final = 'fail';
				failMessage = failMessageArray[0];
			}
			
			if (final == 'fail') {
				student.className = 'text-box-red-error';
				studentFail.firstChild.data = failMessage;
				studentFail.style.display = 'inline';
				if (finalExam == 'yes') {
					student.focus();
					student.select();
				}
				return final;
			}else {
				student.className = 'text-box-gray-border';
				studentFail.style.display = 'none';
				return final;
			}
			break;
			
	}
	
	/*if (student.value == "" && studentColleague.value == 'NA') {
		final = 'fail';
		failMessage = failMessageArray[12];
	}else if (student.value != "" && studentColleague.value != 'NA') {
		final = 'fail';
		failMessage = failMessageArray[13];
	}else if (student.value.match(answer)) {
		final = 'pass';
	}else {
		final = 'fail';
		failMessage = failMessageArray[0];
	}
	
	if (final == 'fail') {
		student.className = 'text-box-red-error';
		studentFail.firstChild.data = failMessage;
		studentFail.style.display = 'inline';
		if (finalExam == 'yes') {
			student.focus();
			student.select();
		}
		return final;
	}else {
		student.className = 'text-box-gray-border';
		studentFail.style.display = 'none';
		return final;
	}*/
}
//_________________________________________________________________________________________________
//											END Testers										   
//_________________________________________________________________________________________________