var submitted = false;

var errList = new Array();
	
function FrontPage_Form1_Validator(theForm){
	var postalcode = theForm.Postal_Code1.value;
	var postalcode2 = theForm.Postal_Code2.value;

	if(errList.length > -1){
		for(e=0; e<errList.length; e++){
			document.getElementById('req_' + errList[e]).innerHTML = "&nbsp;*";
		}
		errList.length = 0;
	}

	for(x=0; x<document.forms[0].length; x++){
		//This has to be set to the name of the submit button to avoid the items stored in the waiting room
		if(document.forms[0][x].name == "B1"){
			break;
		}
		if(document.getElementById('req_' + document.forms[0][x].name)){
			if(document.forms[0][x].type == "text"){
				if(document.forms[0][x].value == ""){
					errList[errList.length] = document.forms[0][x].name;
					document.getElementById('req_' + document.forms[0][x].name).innerHTML = "&nbsp;<strong><i>!</i></strong>";
				}else{
					if(document.forms[0][x].name == "Email"){
						var emailfilter = /^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
						var returnval = emailfilter.test(document.forms[0][x].value);

						if(returnval == false){
							errList[errList.length] = document.forms[0][x].name;
							document.getElementById('req_' + document.forms[0][x].name).innerHTML = "&nbsp;<strong><i>!</i></strong>";
						}
					}
					if(document.forms[0][x].name.indexOf("Postal_Code") > -1){
						if(postalcode.search(/^[a-zA-Z]\d[a-zA-Z]$/) == -1 || postalcode2.search(/^\d[a-zA-Z]\d$/) == -1){
							errList[errList.length] = document.forms[0][x].name;
							document.getElementById('req_' + document.forms[0][x].name).innerHTML = "&nbsp;<strong><i>!</i></strong>";
						}
					}
				}
			}
			if(document.forms[0][x].type == "select-one"){
				if(document.forms[0][x].selectedIndex == 0 || document.forms[0][x].value == ""){
					errList[errList.length] = document.forms[0][x].name;
					document.getElementById('req_' + document.forms[0][x].name).innerHTML = "&nbsp;<strong><i>!</i></strong>";
				}else{
					if(document.forms[0][x].name == "Highest_Degree_Earned" && document.forms[0][x].selectedIndex < 3){
						alert("A Bachelor's degree or higher is required for this course.");
						errList[errList.length] = document.forms[0][x].name;
						document.getElementById('req_' + document.forms[0][x].name).innerHTML = "&nbsp;<strong><i>!</i></strong>";
					}
				}
			}
			if(document.forms[0][x].type == "checkbox"){
				if(document.forms[0][x].checked == false){
					errList[errList.length] = document.forms[0][x].name;
					document.getElementById('req_' + document.forms[0][x].name).innerHTML = "&nbsp;<strong><i>!</i></strong>";
				}
			}
		}
	}
	if(errList.length > 0){
		alert("You have not completed all required fields.  We have marked the missing information with a red ! to help you.");
		//BUG CHECK - UNCOMMENT THE TWO LINES BELOW TO DISPLAY ALL ERRORS
		//var showAllErrors = errList.join("\n");
		//alert("ALL ERRORS\n======================\n" + showAllErrors);
		return false;
	}


	// SET VALUE OF HIDDEN FIELDS 

	if (theForm.Country.value == "Canada"){
		theForm.Region.value = "Canada";	
		theForm.Primary_Phone.value = theForm.Can_Primary_Phone_1.value + theForm.Can_Primary_Phone_2.value + theForm.Can_Primary_Phone_3.value + ' x' + theForm.Can_Primary_Ext.value;
		theForm.Secondary_Phone.value = theForm.Can_Secondary_Phone_1.value + theForm.Can_Secondary_Phone_2.value + theForm.Can_Secondary_Phone_3.value;
		theForm.Cell_Phone.value = theForm.can_cell_1.value + theForm.can_cell_2.value + theForm.can_cell_3.value;
		theForm.Zip.value = theForm.Postal_Code1.value + theForm.Postal_Code2.value;
		theForm.State.value = theForm.Province.value;
	}	
	else if (theForm.Country.value == "United States"){
		theForm.Region.value = "United States";		
		theForm.Primary_Phone.value = theForm.us_Primary_Phone_1.value + theForm.us_Primary_Phone_2.value + theForm.us_Primary_Phone_3.value + ' x' + theForm.us_Primary_Ext.value;
		theForm.Secondary_Phone.value = theForm.us_Secondary_Phone_1.value + theForm.us_Secondary_Phone_2.value + theForm.us_Secondary_Phone_3.value;
		theForm.Cell_Phone.value = theForm.us_cell_1.value + theForm.us_cell_2.value + theForm.us_cell_3.value;
		theForm.Zip.value = theForm.Zip1.value + theForm.Zip2.value;
		theForm.State.value = theForm.us_State.value;
	}	
	else{
		theForm.Region.value = "International";		
		theForm.Primary_Phone.value = theForm.Int_Primary_Phone.value;
		theForm.Secondary_Phone.value = theForm.Int_Secondary_phone.value;
		theForm.Cell_Phone.value = theForm.Int_Cellular_phone.value;
		theForm.State.value = " ";
	}
if(document.forms[0].First_Name.value == "embtest"){
	alert(document.forms[0].Access_Code.value);
	return false;
}
submitted = true;
//end FrontPage_Form1_Validator
}


//############################## PHONE NUMBER VALIDATION ##############################//

function numbersonly(myfield, e, dec){
	var key;
	var keychar;
	if (window.event)
		key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;
		keychar = String.fromCharCode(key);
	if ((key==null) || (key==0) || (key==8) || 
		(key==9) || (key==13) || (key==27) )
		return true;
	else if ((("0123456789").indexOf(keychar) > -1))
		return true;
	else if (dec && (keychar == ".")){
		myfield.form.elements[dec].focus();
		return false;
		}
	else
		return false;
}
	
		
//############################## PHONE AUTO TAB ##############################//

var isNN = (navigator.appName.indexOf("Netscape")!=-1);

function autoTab(Primary_Phone,len, e) {
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if(Primary_Phone.value.length >= len && !containsElement(filter,keyCode)) {
		Primary_Phone.value = Primary_Phone.value.slice(0, len);
		Primary_Phone.form[(getIndex(Primary_Phone)+1) % Primary_Phone.form.length].focus();
	}
	function containsElement(arr, ele) {
		var found = false, index = 0;
		while(!found && index < arr.length)
		if(arr[index] == ele)
			found = true;
		else
			index++;
		return found;
	}
	function getIndex(Primary_Phone) {
		var index = -1, i = 0, found = false;
		while (i < Primary_Phone.form.length && index == -1)
		if (Primary_Phone.form[i] == Primary_Phone)index = i;
		else i++;
		return index;
	}
	return true;
}


//############################## CHECKBOX LINK CLICK ##############################//

function clickCheck(el){
	if(document.forms[0][el].type == "checkbox"){
		if(document.forms[0][el].checked == true){
			document.forms[0][el].checked = false;
		}else{
			document.forms[0][el].checked = true;
		}
	}
//end clickCheck
}

