var currentPage = 1;
var totalPages = 4;
var doValidation = true;


function toggleValidation(){
	if (doValidation){
		doValidation = false;
		alert ("valication OFF")
	} else {
		doValidation = true;
		alert ("valication ON")
	}
}
function nextPage(){
	if (currentPage < totalPages){
		if (validateCurrentPage()){
			currentPage++
			gotoPage(currentPage)
		} else {
			alert("Please fill out all required fields before submitting the form.\n Missing fields are highlighted in red.");
		}
	}
}
function previousPage(){
	if (currentPage >1){
		currentPage--
		gotoPage(currentPage)
	}
}

function validateCurrentPage()
{
	if (doValidation){
		theForm = document.applicationForm
		required = eval("required_"+currentPage)
		var error = ""; 
		for (i=0; i<required.length; i++){
			//alert (required[i] + "="+ eval("theForm."+required[i]+".value"))
			if (eval("theForm."+required[i]+".value") == "" || eval("theForm."+required[i]+".value") == "select answer" ){
				eval("theForm."+required[i]).className = "error"
				error += "   " + eval("theForm."+required[i]).id + "\n";
			} else {
				eval("theForm."+required[i]).className = ""
			}
		}
	
		if (error != "") {
			//error = "THE FOLLOWING FIELDS ARE REQUIRED: \n" + error;		
			return (false);
		} else {
			return (true);
		} 
	} else {
		return (true);
	}
}

function validateFinalPage(){
	theForm = document.applicationForm
	required = eval("required_"+totalPages)
	var error = ""; 
	for (i=0; i<required.length; i++){
		if (eval("theForm."+required[i]+".checked") == false){
   			error += "   " + eval("theForm."+required[i]).id + "\n";
		}
	}
	if (error != "") {	
		return (false);
	} else {
		return (true);
	} 
	
}


function gotoPage(p){
	for (var i=1; i<=totalPages;i++){
		// Turn ON the current page and torn OFF all of the others
		if (i == p){
			document.getElementById("page"+i).style.display = "block"
		} else {
			document.getElementById("page"+i).style.display = "none"
		}
	}		
	document.getElementById("stepImage").src = "images/step"+p+".gif"
	/*
	// disable the previous button if this is the first page
	if (p == 1){
		document.getElementById("prevBtn").disabled = true
	} else {
		document.getElementById("prevBtn").disabled = false
	}
	
	// disable the next button if this is the last page
	if (p == totalPages){
		document.getElementById("nextBtn").disabled = true
	} else {
		document.getElementById("nextBtn").disabled = false
	}
	*/
}


function saveBoxToggle(){
	if (document.getElementById("saveBox").style.display != "block"){
		document.getElementById("saveBox").style.display = "block"
	} else {
		document.getElementById("saveBox").style.display = "none"
	}
	/* if (window.pageYOffset)
        ScrollTop = window.pageYOffset;
    else
        ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;

	document.getElementById('light').style.display='block';
	document.getElementById('fade').style.display='block'

	if( window.innerHeight && window.scrollMaxY ){ // Firefox {
		pageWidth = window.innerWidth + window.scrollMaxX;
		pageHeight = window.innerHeight + window.scrollMaxY;
		scrollPos = window.scrollHeight;
	}
	else if( document.body.scrollHeight > document.body.offsetHeight ) // all but Explorer Mac
	{
	pageWidth = document.body.scrollWidth;
	pageHeight = document.body.scrollHeight;
	scrollPos = window.scrollHeight;
	}
	else // works in Explorer 6 Strict, Mozilla (not FF) and Safari
	{ 
	pageWidth = document.body.offsetWidth + document.body.offsetLeft; 
	pageHeight = document.body.offsetHeight + document.body.offsetTop; 
	scrollPos = document.body.offsetTop; 
	}
	
	document.getElementById('light').style.top = scrollPos + 50;
	document.getElementById('fade').style.height = pageHeight
	*/
	

}
function doSave(){
	
	if (document.applicationForm.pswd.value == ""){
		document.applicationForm.pswd.className = "error"
	} else if (document.applicationForm.email_address.value == ""){
		document.applicationForm.pswd.className = ""
		document.applicationForm.email_address.className = "error"
		alert ("you must enter in your email address in the form before saving. \nThis will help you retrieve your application information later.")
	} else {
		document.applicationForm.pswd.className = ""
		document.applicationForm.email_address.className = ""
		doPopup('appSave_waiting.php',300,200)           
		document.applicationForm.target='popWin'; // "myWin" is what the window is named in the "doPopup()" function
		document.applicationForm.action = "appSave.php";
		document.applicationForm.submit();
		document.getElementById("saveBox").style.display = "none"
	}
}
function doSubmit(){
	if (validateFinalPage()){
		document.applicationForm.target="_self";
		document.applicationForm.action = "application-submit.php";
   		document.applicationForm.submit()
		document.getElementById("saveBox").style.display = "none"
	} else {
		alert ("Please read and check all of the authorization boxes above before continuing.")
	}
}


function pulldownChange_HOW_DID_YOU_LEARN(thisPulldown){
	if (thisPulldown.value == "Other (fill in area)"){
		document.getElementById("otherBox").style.display = "inline"	
	} else {
		document.getElementById("otherBox").style.display = "none"	
	}
}




var required_1 = [  "first_name",
					"middle_initial",
					"last_name",
					"home_address",
					"city",
					"state",
					"zip",
					"years_at_home_address",
					"ssn",					
					"contact_phone_number",					
					"email_address",
					"email_address_confirm",
					"location_choice_1",
					"state_choice_1",
					"location_choice_2",
					"state_choice_2",
					"how_did_you_hear_about_franchise_opportunity",
					"when_do_you_anticipate_opening"
					];
var required_2 = ["reference_1_first_name",
				  "reference_1_last_name",
				  "reference_1_address",
				  "reference_1_city",
				  "reference_1_state",
				  "reference_1_zip",
				  "reference_1_phone",
				  
				  "reference_2_first_name",
				  "reference_2_last_name",
				  "reference_2_address",
				  "reference_2_city",
				  "reference_2_state",
				  "reference_2_zip",
				  "reference_2_phone",
				  
				  "reference_3_first_name",
				  "reference_3_last_name",
				  "reference_3_address",
				  "reference_3_city",
				  "reference_3_state",
				  "reference_3_zip",
				  "reference_3_phone",
				  
				  "do_you_have_experience_operating_a_restaurant"
				  
				  ];
var required_3 = ["type_of_business_entity",
					"name_of_business",
					"state_of_formation",
					"date_of_legal_existence",
					"primary_business_of_entity",
					"have_you_been_convicted",
					"assets_TOTAL",
					"liabilities_TOTAL",
					"income_TOTAL",
					"worth_NET_WORTH"];

var required_4 = ["authorize_1","authorize_2","authorize_3"];