//script for open a pop up window for confidential callback
function openPopUpWindow(url,winName,features) {
  window.open(url,winName,features);
}
function validateblogquestion(form)
{
	if ( form.subject.value.length < 1 ) 
	{
		alert( "Please enter a subject of your question." );
		form.subject.focus();
		return false;
	}
	if ( form.message.value.length < 1 ) 
	{
		alert( "Please enter a question.  Our team is pleased to help." );
		form.message.focus();
		return false;
	}
	return true;
}
//validate city page form
function validate( form ){
  if ( form.contact.value.length < 1 ) {
    alert( "Please enter an email address or phone number so that we can get back to you with a response to your question." );
    return false;
  }
  if ( form.message.value.length < 1 ) {
    alert( "Please enter a message or question.  Our team is pleased to help." );
    return false;
  }
  return true;
}


//script used on bankruptcy 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] = "assets_panel";
	panels[3] = "income_panel";
	panels[4] = "contact_panel";
	
	tabsOn[0] = "causes_on";
	tabsOn[1] = "bills_on";
	tabsOn[2] = "assets_on";
	tabsOn[3] = "income_on";
	tabsOn[4] = "contact_on";
	
	tabsOff[0] = "causes_off";
	tabsOff[1] = "bills_off";
	tabsOff[2] = "assets_off";
	tabsOff[3] = "income_off";
	tabsOff[4] = "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 == '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
			{
				document.frm.studentdebtonlyandyoung.value = "";
				
				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)
				{
					document.frm.studentdebtonlyandyoung.value = "yes";
				}				
					
				SetUpScreen ("assets_panel", "assets_on", "assets_off", "income_panel", "bills_panel");				 
			}
		}
    }
	else if(requestedScreen == 'income_panel')
    {	  
	  	if(currentScreen == "contact_panel") //no need to validate imput since it is the screen after this one 
		{	
			SetUpScreen ("income_panel", "income_on", "income_off", "contact_panel", "assets_panel");
		}
		else //validate the input first
		{		
			errormessage = validateAssetsEntries();
			
			if (errormessage != "")
			{
				alert(errormessage);
				return false;
			}
			else
			{				
				SetUpScreen ("income_panel", "income_on", "income_off", "contact_panel", "assets_panel");
			}
		}
    }	

	else if(requestedScreen == 'contact_panel')
    {
		if(currentScreen == "contact_panel") //no need to validate imput since it is the screen after this one 
		{
			SetUpScreen ("contact_panel", "contact_on", "contact_off", "", "income_panel");
		}
		else //validate the input first
		{	
			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 ("contact_panel", "contact_on", "contact_off", "", "income_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 == 'bills_panel' || panel == 'assets_panel' || panel == 'income_panel')
	{
		hideControl("submit_button");
		showControl("previous_button");		
		showControl("next_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.ownestate.length; i++)
	{
		if(document.frm.ownestate[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.incometypeother.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.incometypeother.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 populate_cities()
{
	document.writeln("<select name=\"citiesselect\" class=\"quoteselect\">");
	document.writeln("<option value=\"Select a City\">Select a City</option>");
	document.writeln("<option value=\"Barrie\">Barrie</option>");
	document.writeln("<option value=\"Brampton\">Brampton</option>");
	document.writeln("<option value=\"Brantford\">Brantford</option>");
	document.writeln("<option value=\"Cambridge\">Cambridge</option>");
	document.writeln("<option value=\"Chatham\">Chatham</option>");
	document.writeln("<option value=\"Goderich\">Goderich</option>");
	document.writeln("<option value=\"Guelph\">Guelph</option>");
	document.writeln("<option value=\"Hamilton Downtown\">Hamilton Downtown</option>");
	document.writeln("<option value=\"Hamilton Mountain\">Hamilton Mountain</option>");
	document.writeln("<option value=\"Kitchener\">Kitchener</option>");
	document.writeln("<option value=\"Leamington\">Leamington</option>");
	document.writeln("<option value=\"London\">London</option>");
	document.writeln("<option value=\"Mississauga\">Mississauga</option>");
	document.writeln("<option value=\"North York\">North York</option>");
	document.writeln("<option value=\"Ottawa\">Ottawa</option>");
	document.writeln("<option value=\"Pembroke\">Pembroke</option>");
	document.writeln("<option value=\"Sarnia\">Sarnia</option>");
	document.writeln("<option value=\"Scarborough\">Scarborough</option>");
	document.writeln("<option value=\"St Catharines\">St. Catharines</option>");
	document.writeln("<option value=\"Thunder Bay\">Thunder Bay</option>");
	document.writeln("<option value=\"Vaughan\">Vaughan</option>");
	document.writeln("<option value=\"Windsor\">Windsor</option>");
	document.writeln("<option value=\"Yonge and Bloor\">Yonge and Bloor</option>");
	document.writeln("</select>");
}

//validate bankruptcy evaluation form
function validateevaluation(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.email.value.length < 1 && form.phone.value.length < 1) {
		alert( "Please enter your email or phone number for us to contact you." );
		form.phone.focus();
		return false;
	}
	if ( form.phone.value.length > 1 && form.bestcalltime.value.length < 1) {
		alert( "Please enter the best time for us to contact you." );
		form.bestcalltime.focus();
		return false;
	}
	if ( form.citiesselect.value == "Select a City")
	{
		alert( "Please select a city" );
		form.citiesselect.focus();
		return false;
	}
	if ( form.agreeterm.checked == false ) {
		alert( "Please check the terms and condition." );
		form.agreeterm.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; 
}

//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"; 
	}
}

function exitiframe(self)
{
	if (self != top) top.location = self.location;
}