var submitted = false;

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

function FrontPage_Form1_Validator(theForm){

	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.selectedIndex == 0){
		alert("Please select your Highest Degree Earned.");
		theForm.Highest_Degree_Earned.focus();
		return (false);
	}
	if(theForm.Highest_Degree_Earned.value == "High School" || theForm.Highest_Degree_Earned.value == "Associates"){
		alert("Sorry, you must have at least a Bachelors degree to qualify for this program.");
		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.selectedIndex == 0){
			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 (theForm.Postal_Code1.value.search(/^[a-zA-Z]\d[a-zA-Z]$/) == -1 || theForm.Postal_Code2.value.search(/^\d[a-zA-Z]\d$/) == -1) {
			alert("Postal Code 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("Please enter a valid Email address.")
			theForm.Email.focus();
			return false;
		}
	}

	if (theForm.designation_name[0].checked == false && theForm.designation_name[1].checked == false){
  		alert("Please indicate if you are certified by the ABMS.");
		theForm.designation_name[0].focus();
  		return (false);
	}
	if(theForm.designation_name[0].checked == true && theForm.Specialization.selectedIndex == 0){
		alert("Please select your ABMS Specialty.");
		theForm.Specialization.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.First_Name.value == "embtest"){
	alert("test: " + theForm.Access_Code.value);
	return false;
}
submitted = true;
}

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


//############################## SHOW DEGREES ##############################//

var degreeList = new Array("Please Select","Abdominal Surgery","Addiction Medicine","Adolescent Medicine (Pediatrics)","Adult Reconstructive Orthopedics","Aerospace Medicine","Allergy And Immunology","Allergy","Anatomic Pathology","Anatomic/Clinical Pathology","Anesthesiology","Blood Banking/Transfusion Medicine","Cardiovascular Disease","Child &amp; Adolescent Psychiatry","Child Neurology","Clinical Cardiac Electrophysiology","Clinical Cytogenetics","Clinical Genetics","Clinical Laboratory Immunology (Allergy &amp; Immunology)","Clinical Molecular Genetics","Clinical Neurophysiology","Clinical Pathology","Clinical Pharmacology","Colon And Rectal Surgery","Cosmetic Surgery","Critical Care Medicine (Anesthesiology)","Critical Care Medicine (Internal Medicine)","Cytopathology","Dermatology","Dermatopathology (Pathology)","Diabetes","Diagnostic Radiology","Emergency Medicine","Endocrinology, Diabetes &amp; Metabolism","Facial Plastic Surgery","Family Medicine","Family Medicine","Family Practice/Psychiatry","Forensic Pathology","Forensic Psychiatry","Gastroenterology","General Practice","General Preventive Medicine","General Surgery","Geriatric Medicine (Family Practice)","Geriatric Medicine (Internal Medicine)","Geriatric Psychiatry","Gynecological Oncology","Gynecology","Hand Surgery (Orthopedics)","Hand Surgery","Head And Neck Surgery","Hematology (Internal Medicine)","Hematology (Pathology)","Hematology/Oncology","Hepatology","Immunology","Infectious Disease","Internal Medicine","Internal Medicine/Pediatrics","Interventional Cardiology","Legal Medicine","Maternal And Fetal Medicine","Medical Genetics","Medical Management","Medical Microbiology","Medical Oncology","Medical Toxicology (Preventive Medicine)","Neonatal-Perinatal Medicine","Nephrology","Neurological Surgery","Neurology","Neuropathology","Neuroradiology","Nuclear Medicine","Nuclear Radiology","Nutrition","Obstetrics &amp; Gynecology","Obstetrics","Occupational Medicine","Ophthalmology","Orthopedic Surgery Of The Spine","Orthopedic Surgery","Osteopathic Manipulative Medicine","Other Specialty","Otolaryngology","Otology-Neurotology","Pain Management","Pain Medicine","Palliative Medicine","Pediatric Allergy","Pediatric Anesthesiology (Anesthesiology)","Pediatric Cardiology","Pediatric Critical Care Medicine","Pediatric Emergency Med (Emergency Med)","Pediatric Emergency Medicine (Emergency Medicine)","Pediatric Emergency Medicine (Pediatrics)","Pediatric Endocrinology","Pediatric Gastroenterology","Pediatric Hematology/Oncology","Pediatric Infectious Disease","Pediatric Nephrology","Pediatric Orthopedics","Pediatric Otolaryngology","Pediatric Pulmonology","Pediatric Radiology","Pediatric Rheumatology","Pediatric Surgery (Surgery)","Pediatric Urology","Pediatrics","Pharmaceutical Medicine","Physical Medicine And Rehabilitation","Plastic Surgery","Psychiatry","Psychoanalysis","Public Health And General Preventive Medicine","Pulmonary &amp; Critical Care Medicine","Pulmonary Disease","Radiation Oncology","Radiology","Reproductive Endocrinology And Infertility","Reproductive Endocrinology","Rheumatology","Selective Pathology","Sleep Medicine","Sports Medicine (Emergency Medicine)","Sports Medicine (Family Practice)","Sports Medicine (Internal Medicine)","Sports Medicine (Orthopedic Surgery)","Surgical Critical Care (Surgery)","Surgical Oncology","Thoracic Surgery","Transitional Year","Traumatic Surgery","Unspecified","Urgent Care Medicine","Urology","Vascular And Interventional Radiology","Vascular Surgery");


function showDegrees(yn){

var daDiv;
var degreeDiv = "";

if(document.all){
	daDiv = document.all['degrees'];
}
if(document.getElementById){
	daDiv = document.getElementById('degrees');
}

if(yn == "y"){
	for(x=0; x<degreeList.length; x++){
		degreeDiv = degreeDiv + "<option value=";
			if(degreeList[x] != "Please Select"){
				degreeDiv = degreeDiv + degreeList[x];
			}
		degreeDiv = degreeDiv + ">" + degreeList[x] + "</option>";
	}
	
	daDiv.innerHTML = "<font size='2' face='Arial' color='#666666'><b>Please Note:</b> If you have a medical degree and are<br>board-certified in the U.S., you are part of a select group<br>that is <b>exempt</b> from taking the GMAT.<hr size='1' noshade><b>What is your Specialty?</b></font><br><select name='Specialization' class='selecta'>" + degreeDiv + "</select>";
}else{
	daDiv.innerHTML = "";
}

//end
}