var submitted = false;

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

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

	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 Applicant does not have Bachelor's Degree or higher, they are prompted to Undergraduate Program
	if(theForm.Highest_Degree_Earned.value == "High School" || theForm.Highest_Degree_Earned.value == "Associates"){
		checkDegree();
		theForm.Highest_Degree_Earned.focus();
		return false;
	}

	if (theForm.Highest_Degree_Earned.selectedIndex == 0){
		alert("Please select your Highest Degree Earned.");
		theForm.Highest_Degree_Earned.focus();
		return (false);
	}
	if (theForm.Country.selectedIndex == 0){
		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.selectedIndex == 0)		{
			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("We're sorry, but the Postal Code you entered is invalid. Please 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);
		}
	}

	if (theForm.Email.value == ""){
		alert("Please enter your Email Address.");
		theForm.Email.focus();
		return (false);
	}else{
		var emailfilter = /^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
		var returnval = emailfilter.test(theForm.Email.value);
		
		if (returnval == false){
			alert("We're sorry but the Email Address you entered is invalid. Please 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 = " ";
	}

	if(theForm.EKGLeadsAntiSpamInput){
		if(theForm.EKGLeadsAntiSpamInput.value == ""){
			alert("Please enter the Authenticity Code displayed.");
			theForm.EKGLeadsAntiSpamInput.focus();
			return false;
		}
	}


submitted = true;
}


//############################## CHECK DEGREE ##############################//

function checkDegree(){
	if(document.forms[0].Highest_Degree_Earned.value == "High School" || document.forms[0].Highest_Degree_Earned.value == "Associates"){
		if(confirm("A Bachelor's degree or higher is required to apply to this program.\n\nBoston University offers an Online Undergraduate Degree Completion Program.\n\nPlease click OK if you want to learn more about this program.")){

			submitted = true;
			var goTo = "http://www.budegree.info/burf/index.htm?"
			var theForm = document.forms[0];
			for(x=0; x<theForm.length; x++){
				if(theForm[x].value != "" && theForm[x].value != "none" && theForm[x].value != "Please Select"){
					goTo = goTo + theForm[x].name + "=" + theForm[x].value + "&";
				}
			}

			//var newWin = window.open(goTo,"BU","height=600,width=800,top=10,left=10,scrollbars=yes,menubar=yes,toolbar=yes,status=yes,resizeable=yes,location=yes");

			location = goTo;

			return false;
		}else{
			document.forms[0].Highest_Degree_Earned.focus();
			return false;
		}
	}
}

//############################## POP-UNDER IF FORM CLOSED/REFERESHED BEFORE FORM SUBMISSION ##############################//

function popExit(){
	if(submitted == false){
		window.open('http://www.cjdegree.info','','width=700,height=500,location=yes,scrollbars=yes,resizable=yes,menubar=yes,status=yes,toolbar=yes,top=25,left=25');
	}
}


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


//############################## EMAIL VALIDATOR ##############################//

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

function checkmail(e){
	var returnval=emailfilter.test(e.value)
	if (returnval==false){
		alert("Email invalid. Please enter a valid Email address.")
		e.select()
	}
	return returnval
}


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