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

/* script used on Credit Counselling evaluation form */
var panels = new Array();
var tabsOn = new Array();
var tabsOff = new Array();

function setupPanels()
{
	panels[0] = "cause_panel";
	panels[1] = "bills_panel";
	panels[2] = "debt_info_panel";
	panels[3] = "assets_panel";
	panels[4] = "income_panel";
	panels[5] = "low_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 switchPanel(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 == 'debt_info_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
			{		  
				//check if bill section entries are as prefered, if they aren't interupt the evaluation
				//by showing a different screen
				if(checkTotalUnsecuredDebt())
				{
					SetUpScreen ("debt_info_panel", "bills_on", "bills_off", "", "bills_panel");
				}				
				else  //if they are, take user to the next step in filling in the evaluation form
				{	
					SetUpScreen ("assets_panel", "assets_on", "assets_off", "income_panel", "bills_panel");
				}  
			}
		}
    }
	else if(requestedScreen == 'income_panel')
    {	  
		SetUpScreen ("income_panel", "income_on", "income_off", "family_panel", "assets_panel");
    }
	else if(requestedScreen == 'low_income_panel')
	{
		SetUpScreen (requestedScreen, "income_on", "income_off", "", "income_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 //validate the input first
		{	
			errormessage = validateIncomeEntries();
		  
			if (errormessage != "")
			{
				alert(errormessage);
				return false;
			}
			else
			{
				var lowincome = checkNetIncome();
				var nowage = checkIncomeSources();
				var socialassistance = checkSocialAssistance();
			  
				//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 || socialassistance || lowincome)
				{
					SetUpScreen ("low_income_panel", "income_on", "income_off", "", "income_panel");
				}
				else //otherwise, take the user to the next step in filling in the evaluation form
				{	
					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)
		{
			hideDSControl(panels[i]);
		}
		else
		{
			showDSControl(panels[i]);	
		}
	}
	
	for (var i=0; i < tabsOn.length; i++)
	{
		if(tabsOn[i] == tabOn)
		{
			showDSControl(tabsOn[i]);
		}
		else
		{
			hideDSControl(tabsOn[i]);	
		}
	}
	
	for (var i=0; i < tabsOff.length; i++)
	{
		if(tabsOff[i] == tabOff)
		{
			hideDSControl(tabsOff[i]);
		}
		else
		{
			showDSControl(tabsOff[i]);	
		}
	}	
}

function controlButtonDisplay(panel)
{
	if(panel == 'cause_panel')
	{
		hideDSControl("previous_button");
		hideDSControl("submit_button");
		showDSControl("next_button");	
	}
	else if(panel == 'bills_panel' || panel == 'bills_panel' || panel == 'assets_panel' || panel == 'income_panel' || panel == 'family_panel')
	{
		hideDSControl("submit_button");
		showDSControl("previous_button");		
		showDSControl("next_button");	
	}
	else if(panel == 'debt_info_panel' || panel == 'low_income_panel')
	{
		hideDSControl("next_button");			
		hideDSControl("submit_button");		
		showDSControl("previous_button");
	}
	else if(panel == 'contact_panel')
	{		
		hideDSControl("next_button");	
		showDSControl("previous_button");
		showDSControl("submit_button");
	}
}

function validateBillsEntries()
{	
	var numberofcheckeditems = 0;
	var numberofcheckedunsecureitems = 0;
	var errormessage = "";
	var checkunsecureother = 0;
	var checksecureother = 0;
	
	//checking if user either checked at least one checkbox or at least entered something into the "other debt" field.
    var bills = document.getElementsByName('bills[]');
	
	for (var i=0; i < bills.length; i++)
	{
		if(bills[i].checked)
		{
			numberofcheckeditems += 1;
		}				
	}
	
	for (var i=0; i < bills.length; i++)
	{
		if(bills[i].checked)
		{
			if(bills[i].value == "Credit cards" || bills[i].value == "Personal loans" || bills[i].value == "Payday loans"
			   || bills[i].value == "Lines of credit" || bills[i].value == "Bank overdrafts" || bills[i].value == "Other Unsecure Debt" || document.frm.unsecureother.value.length > 0)
			{
				numberofcheckedunsecureitems += 1;
			}
			
		}				
	}
	
	for (var i=0; i < bills.length; i++)
	{
		if(bills[i].checked)
		{
			if(bills[i].value == "Other Unsecure Debt" && document.frm.unsecureother.value.length < 1)
			{
				checkunsecureother += 1;
			}
			
		}				
	}
	
	for (var i=0; i < bills.length; i++)
	{
		if(bills[i].checked)
		{
			if(bills[i].value == "Other Secure Debt" && document.frm.billsother.value.length < 1)
			{
				checksecureother += 1;
			}
			
		}				
	}
	
	if(numberofcheckeditems == 0 && trim(document.frm.billsother.value) == "")
	{
		errormessage = "Please tell us what debts you have.";
	}	
	else if(numberofcheckedunsecureitems == 0)
	{
		errormessage = "Please tell us what unsecured debts you have.";
	}	
	else if(checkunsecureother != 0)
	{
		errormessage = "Please specify what \"other\" unsecured debts you have.";
	}
	else if(checksecureother != 0)
	{
		errormessage = "Please specify what \"other\" secured debts you have.";
	}
	else if(document.frm.totaldebt.value == "" || document.frm.totaldebt.value == "select")
	{
		 errormessage ="Please estimate the amount of your total unsecured debt.";		 
	} 
	
	return errormessage;
}

function checkTotalUnsecuredDebt()
{
	
	var numberofcheckeditems = 0;
	var otherdebts = "";
	var restricted = false;
		
    var bills = document.getElementsByName('bills[]')
	

	if(document.frm.totaldebt.value == "< $5,000" || document.frm.totaldebt.value == "$5,000 - $10,000")
	{
		restricted = true;
	}
	
	return restricted;	
}

function validateIncomeEntries()
{	
	var errormessage = "";
	var numberofcheckeditems = 0;
	var checkotherincome = 0;
	var noneincomeselected = 0;
	var validnoneincomeselected = 0;
	var employeed = 0;
	
	//checking if user either checked at least one checkbox or at least entered something into the "other income" field.
    var incometypes = document.getElementsByName('incometypes[]');
	
	for (var i=0; i < incometypes.length; i++)
	{
		if(incometypes[i].checked)
		{
			numberofcheckeditems += 1;
		}				
	}
	
	for (var i=0; i < incometypes.length; i++)
	{
		if(incometypes[i].checked)
		{
			if(incometypes[i].value == "Other" && document.frm.incometypeother.value.length < 1)
			{
				checkotherincome += 1;
			}
			
		}				
	}
	
	for (var i=0; i < incometypes.length; i++)
	{
		if(incometypes[i].checked)
		{			
			if(incometypes[i].value == "None")
			{
				noneincomeselected += 1;
			}
			
		}				
	}
	
	for (var i=0; i < incometypes.length; i++)
	{
		if(incometypes[i].checked)
		{
			if(incometypes[i].value == "Full-time job" || incometypes[i].value == "Part-time job" || incometypes[i].value == "Other")
			{
				employeed += 1;
			}
			
		}				
	}
	
	if(noneincomeselected != numberofcheckeditems)
	{
		validnoneincomeselected = 1;
	}
	
	if(numberofcheckeditems == 0 && trim(document.frm.incometypeother.value) == "")
	{
		errormessage = "Please tell us what kinds of income you have.";
	}
	else if (checkotherincome != 0)
	{
		errormessage = "Please specify what \"other\" kinds of income you have.";		
	}	
	else if((document.frm.annualincome.value == "" || document.frm.annualincome.value == "select") && employeed != 0)
	{
		errormessage = "Please estimate the amount of your total annual net income.";
	}
	/*else if((document.frm.annualincome.value == "" || document.frm.annualincome.value == "select") && noneincomeselected == 0)
	{
		 errormessage = "Please estimate the amount of your total annual net income.";		 
	}
	else if((document.frm.annualincome.value == "" || document.frm.annualincome.value == "select") && validnoneincomeselected == 1)
	{
		 errormessage = "Please estimate the amount of your total annual net income.";		 
	}*/
	
	return errormessage;
}


function checkIncomeSources()
{
	var numberofcheckeditems = 0;
	var nowageincome = 0;
	var otherincome = "";
	
	var restricted = false;
	
    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" 
						   || incometypes[i].value == "Retirement pension"
						   || incometypes[i].value == "Child or spousal support"
						   || incometypes[i].value == "None")
			{
				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.incometypeother.value);	
		
		if(otherincome == "")
		{	
			restricted = true;
		}
	}
	
	return restricted;
}

function checkSocialAssistance()
{
	var numberofcheckeditems = 0;
	var socialassistance = false;
	var restricted = false;
	
	//checking if social assistance is the only option that is checked
    var incometypes = document.getElementsByName('incometypes[]')
	
	for (var i=0; i < incometypes.length; i++)
	{
		if(incometypes[i].checked)
		{
			numberofcheckeditems += 1;
			
			if(incometypes[i].value == "Social Assistance")
			{				
				socialassistance = true;				
				break;
			}
		}					
	}
	
	if(socialassistance == true && numberofcheckeditems == 1)
	{
		restricted = true;
	}
	
	return restricted;
}

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

function populate_provinces()
{
	document.writeln("<select name=\"provincesselect\" class=\"quoteselect\">");
	document.writeln("<option value=\"Select a Province\">Select a Province</option>");
	document.writeln("<option value=\"Alberta\">Alberta</option>");
	document.writeln("<option value=\"British Columbia\">British Columbia</option>");
	document.writeln("<option value=\"Manitoba\">Manitoba</option>");
	document.writeln("<option value=\"New Brunswick\">New Brunswick</option>");
	document.writeln("<option value=\"Newfoundland and Labrador\">Newfoundland and Labrador</option>");
	document.writeln("<option value=\"Nova Scotia\">Nova Scotia</option>");
	document.writeln("<option value=\"Ontario\">Ontario</option>");
	document.writeln("<option value=\"Prince Edward Island\">Prince Edward Island</option>");
	document.writeln("<option value=\"Saskatchewan\">Saskatchewan</option>");
	document.writeln("</select>");
}

//validate credit counselling evaluation form
function validateccevaluation(form)
{
	if ( form.firstname.value.length < 1 ) {
		alert( "Please enter your first name." );
		form.firstname.focus();
		return false;
	}
	if ( form.lastname.value.length < 1 ) {
		alert( "Please enter your last name." );
		form.lastname.focus();
		return false;
	}
	if ( form.phone.value.length < 1) {
		alert( "Please enter your phone number for us to contact you." );
		form.phone.focus();
		return false;
	}
	if (!checkPhoneNumber(form.phone.value))
	{	
		alert( "Please enter a valid 10 digit phone number for us to contact you." );
		form.phone.focus();
		return false;
	}
	if ( form.provincesselect.value == "Select a Province")
	{
		alert( "Please select a Province" );
		form.provincesselect.focus();
		return false;
	}
	if ( form.agreeterm.checked == false ) {
		alert( "Please check the terms and condition." );
		form.agreeterm.focus();
		return false;
	}
	return true;
}

function checkPhoneNumber(phonenumberStr)
{	
	var Char;
	var numbercount=0;
	var trimphonenumber;
	trimphonenumber= phonenumberStr.split(' ').join('');
 
	for (i = 0; i < trimphonenumber.length; i++) 
	{ 
		Char = trimphonenumber.charAt(i); 
		if (!isNaN(Char)) //a number
		{
			 numbercount +=1;
		}
	}
	
	if(numbercount == 10)
	{
		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; 
}

//show div passed in
function showDSControl(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 hideDSControl(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"; 
	}
}
