var submitted = false;

//############################## FORM VALIDATOR ##############################//

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

	if (theForm.Access_code.selectedIndex == 0){
		alert("Please select your program of interest.");
		theForm.Access_code.focus();
		return (false);
	}

	if (theForm.First_Name.value == ""){
		alert("Please enter your First Name.");
		theForm.First_Name.focus();
		return (false);
	}

	if (theForm.Last_Name.value == ""){
		alert("Please enter your Last Name.");
		theForm.Last_Name.focus();
		return (false);
	}

	if (theForm.Highest_Degree_Earned.value == ""){
		alert("Please select your Highest Degree Earned.");
		theForm.Highest_Degree_Earned.focus();
		return (false);
	}

	if (theForm.Highest_Degree_Earned.selectedIndex < 3){
		alert("A Bachelor's Degree or higher is required for this program.");
		theForm.Highest_Degree_Earned.focus();
		return (false);
	}

	if (theForm.gpa.value == ""){
		alert("Please enter your GPA.");
		theForm.gpa.focus();
		return (false);
	}

	if ((theForm.GRE[0].checked == false) && (theForm.GRE[1].checked == false)){
		alert("Please indicate if you have taken a GRE test.");
		return (false);
	}

	if (theForm.GRE[1].checked == true){
		alert("PLEASE NOTE: You will required to take a GRE test to qualify for this program.");
	}

	if ((theForm.GMAT[0].checked == false) && (theForm.GMAT[1].checked == false)){
		alert("Please indicate if you have taken a GMAT.");
		return (false);
	}

	if (theForm.GMAT[1].checked == true){
		alert("PLEASE NOTE: You will required to take a GMAT test to qualify for this program.");
	}

	if (theForm.Country.value == "none"){
		alert("Please select your Country.");
		theForm.Country.focus();
		return (false);
	}

	if (theForm.Address.value == ""){
		alert("Please enter your Address.");
		theForm.Address.focus();
		return (false);
	}

	if (theForm.City.value == ""){
		alert("Please enter your City.");
		theForm.City.focus();
		return (false);
	}

	// UNITED STATES VALIDATION

	if (theForm.Country.value == "United States"){
		if (theForm.us_State.value == "none"){
			alert("Please select your State.");
			theForm.us_State.focus();
			return (false);
		}

		if (theForm.Zip1.value == ""){
			alert("Please enter your Zip Code.");
			theForm.Zip1.focus();
			return (false);
		}
	}

	// CANADA VALIDATION

	if (theForm.Country.value == "Canada"){
		if (theForm.Province.value == "none"){
			alert("Please select your Province.");
			theForm.Province.focus();
			return (false);
	  	}

		if (theForm.Postal_Code1.value == ""  || theForm.Postal_Code2.value == ""){
			alert("Please enter your Postal Code.");
			theForm.Postal_Code1.focus();
			return (false);
	  	}

		if (postalcode.search(/^[a-zA-Z]\d[a-zA-Z]$/) == -1 || postalcode2.search(/^\d[a-zA-Z]\d$/) == -1){
			alert("The Postal Code you entered is invalid.\nPlease enter a valid Postal Code.");
			theForm.Postal_Code1.focus();
			return false;
		}
	}

	// PRIMARY PHONE VALIDATION

	if (theForm.Country.value == "Canada")	{
		if (theForm.Can_Primary_Phone_1.value == "" || theForm.Can_Primary_Phone_2.value == "" || theForm.Can_Primary_Phone_3.value == ""){
			alert("Please enter your Primary Phone Number.");
			theForm.Can_Primary_Phone_1.focus();
			return (false);
		}
	}
	else if (theForm.Country.value == "United States")	{
		if (theForm.us_Primary_Phone_1.value == "" || theForm.us_Primary_Phone_2.value == "" || theForm.us_Primary_Phone_3.value == ""){
			alert("Please enter your Primary Phone Number.");
			theForm.us_Primary_Phone_1.focus();
			return (false);
		}
	}
	else {
		if (theForm.Int_Primary_Phone.value == ""){
			alert("Please enter your Primary Phone Number.");
			theForm.Int_Primary_Phone.focus();
			return (false);
		}
	}

var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i

	if (theForm.Email.value == ""){
		alert("Please enter your Email address.");
		theForm.Email.focus();
		return (false);
	}else{
		var returnval = emailfilter.test(theForm.Email.value)
		if (returnval == false){
			alert("The Email address you entered is invalid.\nPlease enter a valid Email address.")
			theForm.Email.focus();
			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.cellphone.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.cellphone.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.cellphone.value = theForm.Int_Cellular_phone.value;
		theForm.State.value = " ";
	}

submitted = true;

//end
}

//############################## 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;
}


//############################## FLASH DETECTOR ##############################//

var detectableWithVB = false;
var pluginFound = false;

function goURL(daURL){
    window.location = daURL;
}
function redirectCheck(pluginFound, redirectURL, redirectIfFound) {
    if(redirectURL && ((pluginFound && redirectIfFound) || (!pluginFound && !redirectIfFound))){
		goURL(redirectURL);
		return pluginFound;
    }else{
		return pluginFound;
    }	
}
function canDetectPlugins(){
    if(detectableWithVB || (navigator.plugins && navigator.plugins.length > 0)){
		return true;
    }else{
		return false;
    }
}
function detectFlash(redirectURL, redirectIfFound){
    pluginFound = detectPlugin('Shockwave','Flash'); 
    if(!pluginFound && detectableWithVB){
		pluginFound = detectActiveXControl('ShockwaveFlash.ShockwaveFlash.1');
    }
    return redirectCheck(pluginFound, redirectURL, redirectIfFound);
}
function detectPlugin() {
    var daPlugins = detectPlugin.arguments;
    var pluginFound = false;
    if (navigator.plugins && navigator.plugins.length > 0){
		var pluginsArrayLength = navigator.plugins.length;
			for (pluginsArrayCounter=0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++ ){
	    		var numFound = 0;
	    		for(namesCounter=0; namesCounter < daPlugins.length; namesCounter++){
					if((navigator.plugins[pluginsArrayCounter].name.indexOf(daPlugins[namesCounter]) >= 0) || (navigator.plugins[pluginsArrayCounter].description.indexOf(daPlugins[namesCounter]) >= 0)){
						numFound++;
					}   
	    		}
	    		if(numFound == daPlugins.length){
					pluginFound = true;
					break;
	    		}
			}
    }
    return pluginFound;
}

// VBSCRIPT FOR IE/WINDOWS

if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1)){
    document.writeln('<script language="VBscript">');
    document.writeln('\'do a one-time test for a version of VBScript that can handle this code');
    document.writeln('detectableWithVB = False');
    document.writeln('If ScriptEngineMajorVersion >= 2 then');
    document.writeln('  detectableWithVB = True');
    document.writeln('End If');
    document.writeln('\'this next function will detect most plugins');
    document.writeln('Function detectActiveXControl(activeXControlName)');
    document.writeln('  on error resume next');
    document.writeln('  detectActiveXControl = False');
    document.writeln('  If detectableWithVB Then');
    document.writeln('     detectActiveXControl = IsObject(CreateObject(activeXControlName))');
    document.writeln('  End If');
    document.writeln('End Function');
    document.writeln('\'and the following function handles QuickTime');
    document.writeln('Function detectQuickTimeActiveXControl()');
    document.writeln('  on error resume next');
    document.writeln('  detectQuickTimeActiveXControl = False');
    document.writeln('  If detectableWithVB Then');
    document.writeln('    detectQuickTimeActiveXControl = False');
    document.writeln('    hasQuickTimeChecker = false');
    document.writeln('    Set hasQuickTimeChecker = CreateObject("QuickTimeCheckObject.QuickTimeCheck.1")');
    document.writeln('    If IsObject(hasQuickTimeChecker) Then');
    document.writeln('      If hasQuickTimeChecker.IsQuickTimeAvailable(0) Then ');
    document.writeln('        detectQuickTimeActiveXControl = True');
    document.writeln('      End If');
    document.writeln('    End If');
    document.writeln('  End If');
    document.writeln('End Function');
    document.writeln('</scr' + 'ipt>');
}

//############################## SEMESTER START ##############################//

		function getToday(){
			var daDate = new Date();
			var daYear = daDate.getYear();
				if(daYear < 1000){
					daYear = daYear + 1900;
				}
			var daMonth = daDate.getMonth();
			var daDay = daDate.getDate();
			var wsaycs_y = daYear;

			if(daMonth < 4){
				wsaycs_s = new Array("Summer","Fall","Spring");
			}
			if(daMonth >= 4 && daMonth < 8){
				wsaycs_s = new Array("Fall","Spring","Summer");
			}
			if(daMonth >= 8){
				wsaycs_s = new Array("Spring","Summer","Fall");
			}

			for(x=0; x<wsaycs_s.length; x++){
				if(wsaycs_s[x] == "Spring"){
					wsaycs_y = daYear+1;
				}

				document.forms[0].What_semester_are_you_considering_starting.options[1+x].value = wsaycs_s[x] + " " + wsaycs_y;
				document.forms[0].What_semester_are_you_considering_starting.options[1+x].text = wsaycs_s[x] + " " + wsaycs_y;
			}
		//end
		}
