var panels = new Array();
var tabsOn = new Array();
var tabsOff = new Array();

function setupPanels()
{
	panels[0] = "cause_panel";
	panels[1] = "bills_panel";
	panels[2] = "student_debt_panel";
	panels[3] = "low_debt_panel";	
	panels[4] = "assets_panel";
	panels[5] = "income_panel";
	panels[6] = "family_panel";
	panels[7] = "contact_panel";
	
	tabsOn[0] = "causes_on";
	tabsOn[1] = "bills_on";
	tabsOn[2] = "assets_on";
	tabsOn[3] = "income_on";
	tabsOn[4] = "family_on";
	tabsOn[5] = "contact_on";	
	
	tabsOff[0] = "causes_off";
	tabsOff[1] = "bills_off";
	tabsOff[2] = "assets_off";
	tabsOff[3] = "income_off";
	tabsOff[4] = "family_off";
	tabsOff[5] = "contact_off";
}

function Navigate(requestedScreen)
{	
	var errormessage = "";
	
	var currentScreen = document.frm.currentPanel.value;
	
	if(requestedScreen == 'cause_panel') //case when displaying the form first time
    {
		//Parameters in the function: SetUpScreen (panel (that is requested), tabOn, tabOff, nextButton, previousButton)
		SetUpScreen ("cause_panel", "causes_on", "causes_off", "bills_panel", ""); // set up and show the next screen
  	}
  	else if(requestedScreen == 'bills_panel')
    {
		SetUpScreen ("bills_panel", "bills_on", "bills_off", "assets_panel", "cause_panel");
    }
	else if(requestedScreen == 'student_debt_panel')
	{
		SetUpScreen (requestedScreen, "bills_on", "bills_off", "", "bills_panel");
	}
	else if(requestedScreen == 'low_debt_panel')
	{
		SetUpScreen (requestedScreen, "bills_on", "bills_off", "", "bills_panel");
	}
	else if(requestedScreen == 'assets_panel')
    {				
		if(currentScreen == "income_panel") //no need to validate imput since it is the screen after this one 
		{
			SetUpScreen ("assets_panel", "assets_on", "assets_off", "income_panel", "bills_panel");
		}
		else //validate the input first
		{			
			errormessage = validateBillsEntries();
		  
			if (errormessage != "")
			{
				alert(errormessage);
				return false;
			}
			else
			{
				var studentdebtonlyandyoung = checkStudentLoanDebt();				
			  
				//check if bill section entries are as prefered, if they aren't interupt the evaluation
				//by showing a different screen
				if(studentdebtonlyandyoung)
				{
					SetUpScreen ("student_debt_panel", "bills_on", "bills_off", "", "bills_panel");
				}				
				else  
				{	
					var frm = document.forms[0];
					var debtamount = document.frm.totaldebt.value;
					
					if(debtamount == "< $5,000")
					{
						SetUpScreen ("low_debt_panel", "bills_on", "bills_off", "", "bills_panel");	
					}
					else
					{
						SetUpScreen ("assets_panel", "assets_on", "assets_off", "income_panel", "bills_panel");	 //if they are, take user to the next step in filling in the evaluation form
					}
				} 
			}
		}
    }
	else if(requestedScreen == 'income_panel')
    {	  
	  	if(currentScreen == "family_panel") //no need to validate imput since it is the screen after this one 
		{	
			SetUpScreen ("income_panel", "income_on", "income_off", "family_panel", "assets_panel");
		}
		else //validate the input first
		{		
			errormessage = validateAssetsEntries();
			
			if (errormessage != "")
			{
				alert(errormessage);
				return false;
			}
			else
			{				
				SetUpScreen ("income_panel", "income_on", "income_off", "family_panel", "assets_panel");
			}
		}
    }
	else if(requestedScreen == 'family_panel')
    {
		if(currentScreen == "contact_panel") //no need to validate imput since it is the screen after this one 
		{
			SetUpScreen ("family_panel", "family_on", "family_off", "contact_panel", "income_panel");
		}
		else 
		{	
			document.frm.nowage.value = "";
			document.frm.lowincome.value = "";	
			
			errormessage = validateIncomeEntries();
		  
			if (errormessage != "")
			{
				alert(errormessage);
				return false;
			}
			else
			{
				var nowage = checkIncomeSources();	  
				var lowincome = checkNetIncome();
			  
				//check if income section entries are as prefered, if they aren't interupt the process of filling in the evaluation form
				//by taking user to a different screen
				if(nowage == true)
				{
					document.frm.nowage.value = "yes";	
				}
				
				if(lowincome == true)
				{
					document.frm.lowincome.value = "yes";	
				}
				
				SetUpScreen ("family_panel", "family_on", "family_off", "contact_panel", "income_panel");
			}
		}
    }
	else if(requestedScreen == 'contact_panel')
    {
		SetUpScreen ("contact_panel", "contact_on", "contact_off", "", "family_panel");
    }
	
  	return true;
}

function SetUpScreen (panel, tabOn, tabOff, nextButton, previousButton)
{
	//show only the correct panel/tab and hide others
	controlPanelDisplay (panel, tabOn, tabOff);
	
	//show only those navigation buttons that should be visible on the current panel
	controlButtonDisplay(panel);
	
	//set the next and previous panel from this one, and the current panel
	document.frm.nextPanel.value = nextButton;
	document.frm.previousPanel.value = previousButton;
	document.frm.currentPanel.value = panel;
}

function controlPanelDisplay(panel, tabOn, tabOff)
{
	for (var i=0; i < panels.length; i++)
	{
		if (panels[i] !== panel)
		{
			hideControl(panels[i]);
		}
		else
		{
			showControl(panels[i]);	
		}
	}
	
	for (var i=0; i < tabsOn.length; i++)
	{
		if(tabsOn[i] == tabOn)
		{
			showControl(tabsOn[i]);
		}
		else
		{
			hideControl(tabsOn[i]);	
		}
	}
	
	for (var i=0; i < tabsOff.length; i++)
	{
		if(tabsOff[i] == tabOff)
		{
			hideControl(tabsOff[i]);
		}
		else
		{
			showControl(tabsOff[i]);	
		}
	}	
}

function controlButtonDisplay(panel)
{
	if(panel == 'cause_panel')
	{
		hideControl("previous_button");
		hideControl("submit_button");
		showControl("next_button");	
	}
	else if(panel == 'bills_panel' || panel == 'assets_panel' || panel == 'income_panel' || panel == 'family_panel')
	{
		hideControl("submit_button");
		showControl("previous_button");		
		showControl("next_button");	
	}
	else if(panel == 'student_debt_panel' || panel == 'low_debt_panel')
	{
		hideControl("next_button");			
		hideControl("submit_button");		
		showControl("previous_button");
	}
	else if(panel == 'contact_panel')
	{		
		hideControl("next_button");	
		showControl("previous_button");
		showControl("submit_button");
	}
}

function validateBillsEntries()
{	
	var numberofcheckeditems = 0;
	var studentloan = false;
	
	var errormessage = "";
	
	
	//checking if user either checked at least one checkbox or at least entered something into the "other debt" field.
	var frm = document.forms[0];
    var bills = document.getElementsByName('bills[]');
	
	for (var i=0; i < bills.length; i++)
	{
		if(bills[i].checked)
		{
			numberofcheckeditems += 1;
			
			if(bills[i].value == "student loans")
			{				
				studentloan = true;				
				break;
			}
		}				
	}
	
	if(numberofcheckeditems == 0 && trim(document.frm.billsother.value) == "")
	{
		errormessage = "Please tell us what bills you have.";
	}	
	//if student debt is checked, check if age of the debt is specified.
	else if(studentloan == true && (document.frm.studentdebtage.value == "" || document.frm.studentdebtage.value == "select"))
	{
		errormessage ="Please tell us how old your student loan is.";
	}
	else if(document.frm.totaldebt.value == "" || document.frm.totaldebt.value == "select")
	{
		 errormessage ="Please estimate the amount of your total debt.";		 
	}	  
	
	return errormessage;
}

function checkStudentLoanDebt()
{
	var numberofcheckeditems = 0;
	var studentloan = false;
	var otherdebts = "";
	var restricted = false;
	
	//checking if student loans is the only option that is checked
	
	var frm = document.forms[0];
    var bills = document.getElementsByName('bills[]')
	
	for (var i=0; i < bills.length; i++)
	{
		if(bills[i].checked)
		{
			numberofcheckeditems += 1;
			
			if(bills[i].value == "student loans")
			{				
				studentloan = true;				
				break;
			}
		}					
	}
	
	if(studentloan == true && numberofcheckeditems == 1)
	{
		//checking if there are other debts that user typed into the 'other debts' field
		otherdebts = trim(document.frm.billsother.value);	
		
		if(otherdebts == "")
		{
			//since the student debt is the only debt, check how old it is,
			//- based on the answer the next screen the user will see will be different.
			if(document.frm.studentdebtage.value == "less than 5 years")
			{
				restricted = true;
			}
		}
	}
	
	return restricted;
}

function validateAssetsEntries()
{
	var errormessage = "";	
	var isSelected = false;
	
	for (var i=0; i < document.frm.realestate.length; i++)
	{
		if(document.frm.realestate[i].checked)
		{
			isSelected = true;
			break;
		}
	}
	
	if(isSelected == false)
	{
		errormessage = "Please indicate whether or not you own any real estate.";	
	}
	
	return errormessage;
}


function validateIncomeEntries()
{	
	var errormessage = "";
	var numberofcheckeditems = 0;	
	
	//checking if user either checked at least one checkbox or at least entered something into the "other income" field.
	var frm = document.forms[0];
    var incometypes = document.getElementsByName('incometypes[]');
	
	for (var i=0; i < incometypes.length; i++)
	{
		if(incometypes[i].checked)
		{
			numberofcheckeditems += 1;
			break;
		}				
	}
	
	if(numberofcheckeditems == 0 && trim(document.frm.otherincome.value) == "")
	{
		errormessage = "Please tell us what kinds of income you have.";
	}
	else if(document.frm.monthlyincome.value == "" || document.frm.monthlyincome.value == "select")
	{
		 errormessage = "Please estimate the amount of your total monthly net income.";		 
	}	  
	
	return errormessage;
}


function checkIncomeSources()
{
	var numberofcheckeditems = 0;
	var nowageincome = 0;
	var otherincome = "";
	
	var restricted = false;
	
	var frm = document.forms[0];
    var incometypes = document.getElementsByName('incometypes[]')
	
	//checking if "no-wage" income sources are the only options that are checked
	for (var i=0; i < incometypes.length; i++)
	{
		if(incometypes[i].checked)
		{
			numberofcheckeditems += 1;
			
			if(incometypes[i].value == "social assistance or welfare" 
						   || incometypes[i].value == "retirement pension"
						   || incometypes[i].value == "child or spousal support")
			{
				nowageincome += 1;
			}
		}				
	}
	
	if(nowageincome == numberofcheckeditems)
	{
		//checking if there are other types of income that user typed into the 'other income' field
		otherincome = trim(document.frm.otherincome.value);	
		
		if(otherincome == "")
		{	
			restricted = true;
		}
	}
	
	return restricted;
}

function checkNetIncome()
{
			
	var restricted = false;
	var netincome = document.frm.monthlyincome.value;
	
	//checking if net income is in the prefered range
	if(netincome == "$0 - $1,000")
	{
	 	restricted = true;
	}
	
	return restricted;
}

/*function load_cities()
{
	var cities = new Array('Calgary', 'Edmonton', 'Grande Prairie', 'Medicine Hat', 'Red Deer');
	
	
	document.frm.city.length = cities.length + 1;
	
	document.frm.city.options[0].text = "Select";
    document.frm.city.options[0].value = "Select";
	
	for (var i=0; i < cities.length; i++) 
	{
	  document.frm.city.options[i+1].text = cities[i];
	  document.frm.city.options[i+1].value = cities[i];	  
	}
	
	document.frm.city.selectedIndex = 0;
}*/

function validateEvaluationRequestInput(form)
{
  if ( trim(form.firstname.value).length < 1 ) 
  {
    alert( "Please enter your first name." );
	form.firstname.focus();
    return false;
  } 
  
  if ( trim(form.lastname.value).length < 1 ) 
  {
    alert( "Please enter your last name." );
	form.lastname.focus();
    return false;
  } 
  
  if (trim(frm.phone.value).length < 1)
  {
	alert ("Please enter your phone number so that we can get back to you.");
	frm.phone.focus();
	return false;
  }	
  
  /*var mprovince = form.province.value;
 
  if(mprovince == "" || mprovince == "Select")
  {
	 alert( "Please select a province." );
	 form.province.focus();
	 return false;
  }*/
  
  var city = form.city.value;

   if(city == "" || city == "Select")
  {
	 alert( "Please select the city that is closest to you." );
	 form.city.focus();
	 return false;
  }
  
  if(!form.privacyagreement.checked)
  {
		alert("Before you submit your request for the evaluation, \nyou need to accept our Terms and Conditions.");
		form.privacyagreement.focus();
		return false;
  }
  
  return true;
}

//removes leading and trailing spaces in the string passed in.
function trim( string_to_trim )
{ 
  while (string_to_trim.charAt(0) == " ")
  { 
    // remove leading spaces 
    string_to_trim = string_to_trim.substring(1); 
  } 
  
  while (string_to_trim.charAt(string_to_trim.length - 1) == " ")
  { 
    // remove trailing spaces 
    string_to_trim = string_to_trim.substring(0,string_to_trim.length - 1); 
  } 
  
  return string_to_trim; 
}

function displayOnTop(self)
{
	if (self != top) top.location = self.location;
}

function openInNewWindow(pageurl) 
{	
	window.open(pageurl, 'new_page_window', 'menubar=no, toolbar=no, location=no, directories=no, status=no, scrollbars, resizable=no, dependent, width=640, height=480, left=0, top=0')
}


/*function load_provinces()
{
	var provinces = new Array('Alberta', 'British Columbia', 'Manitoba', 'New Brunswick', 'Newfoundland', 'Nova Scotia',
						   'Ontario', 'Quebec', 'Saskatchewan');
	
	document.frm.province.length = provinces.length ;
	
	for (var i=0; i < provinces.length; i++) 
	{
	  document.frm.province.options[i].text = provinces[i];
	  document.frm.province.options[i].value = provinces[i];	  
	}
	
	document.frm.province.selectedIndex = 0; //pre-selected province
}*/

function load_cities_per_province()
{		
	var cities_per_province = new Array('Airdrie', 'Brooks', 'Camrose', 'Drumheller', 'Edmonton', 'Medicine Hat', 'Okotoks', 'Olds', 'Fort McMurray', 
										'Grande Prairie', 'Innisfail', 'Leduc', 'Lloydminster','Red Deer', 'Sherwood Park', 'Spruce Grove', 'Stony Plain', 
										'St. Albert');
	
	document.frm.city.length = cities_per_province.length + 1;
	
	document.frm.city.options[0].text = "Select";
	document.frm.city.options[0].value = "Select";

	for (var i = 0; i < cities_per_province.length; i++) 
	{
	  document.frm.city.options[i+1].text = cities_per_province[i];
	  document.frm.city.options[i+1].value = cities_per_province[i];
	}	
		
	document.frm.city.selectedIndex = 0;
}

/*function load_offices_per_city()
{
	var city_selected = document.frm.citiesperprovince.options[document.frm.citiesperprovince.options.selectedIndex].value;

	var cities_with_multiple_offices = new Array("Toronto Area", "Hamilton");
	var multiple_offices = 0;
	
	for (var i=0; i < cities_with_multiple_offices.length; i++)
	{
		if(city_selected == cities_with_multiple_offices[i])
		{
			multiple_offices = 1;			
			break;
		}
	}
	
	if(multiple_offices == 1)
	{
		showControl("offices_per_city");
		
		if(city_selected == "Toronto Area")
		{
			var offices_per_city = new Array("Toronto","Brampton", "Mississauga", "North York", "Scarborough", "Vaughan");				
		}
		else if(city_selected == "Hamilton")
		{
			var offices_per_city = new Array("Hamilton", "Hamilton Mountain");
		}

		document.frm.officespercity.length = offices_per_city.length + 1;
		
		document.frm.officespercity.options[0].text = "Select";
		document.frm.officespercity.options[0].value = "Select";

		for (var i = 0; i < offices_per_city.length; i++) 
		{
		  document.frm.officespercity.options[i+1].text = offices_per_city[i];
		  document.frm.officespercity.options[i+1].value = offices_per_city[i];
		}	
		
		document.frm.officespercity.selectedIndex = 0;
	}
	else
	{
		document.frm.officespercity.selectedIndex = -1;
		hideControl("offices_per_city");
	}
}*/

//removes leading and trailing spaces in the string passed in.
function trim( string_to_trim )
{ 
  while (string_to_trim.charAt(0) == " ")
  { 
    // remove leading spaces 
    string_to_trim = string_to_trim.substring(1); 
  } 
  
  while (string_to_trim.charAt(string_to_trim.length - 1) == " ")
  { 
    // remove trailing spaces 
    string_to_trim = string_to_trim.substring(0,string_to_trim.length - 1); 
  } 
  
  return string_to_trim; 
} 

//show div passed in
function showControl(controlId)
{
	if (document.getElementById)
	{
		// this is the way the standards work
		document.getElementById(controlId).style.visibility = "visible";
		document.getElementById(controlId).style.display = "block"; 
	}
	else if (document.all)
	{
		// this is the way old msie versions work
		document.all[controlId].style.visibility = "visible";
		document.getElementById(controlId).style.display = "block"; 
	}
	else if (document.layers)
	{
		// this is the way nn4 works
		document.layers[controlId].visibility = "visible";
		document.getElementById(controlId).style.display = "block"; 
	}
}

//hide div passed in
function hideControl(controlId)
{
	if (document.getElementById)
	{
		// this is the way the standards work
		document.getElementById(controlId).style.visibility = "hidden";
		document.getElementById(controlId).style.display = "none"; 
	}
	else if (document.all)
	{
		// this is the way old msie versions work
		document.all[controlId].style.visibility = "hidden";
		document.getElementById(controlId).style.display = "none"; 
	}
	else if (document.layers)
	{
		// this is the way nn4 works
		document.layers[controlId].visibility = "hidden";
		document.getElementById(controlId).style.display = "none"; 
	}
}
