var message = ""
function onsubmitForm()
{  
  if(document.pressed == 'page_causes')
  {
   document.frm.action ="bankruptcy-evaluation.htm";
  }
  else if(document.pressed == 'page_bills')
  {
	  document.frm.action ="bankruptcy-evaluation-bills.htm";	
  }
  else if(document.pressed == 'page_assets')
  {	 
	  var errormessage = "";
	  errormessage = validateBillsEntries();
	  
	  if (errormessage != "")
	  {
		alert(errormessage);
	  }
	  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)
		  {
			document.frm.action = "bankruptcy-evaluation-studentloans.htm";
		  }
		  //if they are, take user to the next step in filling in the evaluation form
		  else
		  {		
			document.frm.action ="bankruptcy-evaluation-assets.htm";
		  }  
	  }
  }	
  else if(document.pressed == 'page_assets_back')
  {
	  document.frm.action ="bankruptcy-evaluation-assets.htm";
  }
  else if(document.pressed == 'page_income')
  {
	  var errormessage = validateAssetsEntries();
	  
	  if (errormessage != "")
	  {
		alert(errormessage);
	  }
	  else
	  {
		  document.frm.action ="bankruptcy-evaluation-income.htm";
	  }
  }
  else if(document.pressed == 'page_income_back')
  {
	document.frm.action ="bankruptcy-evaluation-income.htm";
  }
  else if(document.pressed == 'page_family')
  {	  	  	  
	  var errormessage = validateIncomeEntries();
	  
	  if (errormessage != "")
	  {
		alert(errormessage);
	  }
	  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)
		  {
			document.frm.action = "bankruptcy-evaluation-nowage.htm";
		  }
		  else if(lowincome)
		  {
			document.frm.action = "bankruptcy-evaluation-lowincome.htm";
		  }	  
		  //otherwise, take the user to the next step in filling in the evaluation form
		  else
		  {		
			document.frm.action ="bankruptcy-evaluation-family.htm";
		  }
	  }
  }	
  else if(document.pressed == 'page_family_back')
  {
	  document.frm.action ="bankruptcy-evaluation-family.htm";
  }
  else if(document.pressed == 'page_contact')
  {
	document.frm.action ="bankruptcy-evaluation-contact.htm";
  } 
  else if(document.pressed == 'page_submit')
  {
	document.frm.action ="evaluation-email.php";
	//document.frm.action ="evaluation-email-test.php";
  }
	
  return true;
}

function validateBillsEntries()
{	
	var numberofcheckeditems = 0;
	var studentloan = false;
	
	//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) == "")
	{
		message = "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"))
	{
		message ="Please tell us how old your student loan is.";
	}
	else if(document.frm.totaldebt.value == "" || document.frm.totaldebt.value == "select")
	{
		 message ="Please estimate the amount of your total debt.";		 
	}	  
	
	return message;
}

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 message = "";
	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)
	{
		message = "Please indicate whether or not you own any real estate.";	
	}
	
	return message;
}


function validateIncomeEntries()
{	
	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) == "")
	{
		message = "Please tell us what kinds of income you have.";
	}
	else if(document.frm.monthlyincome.value == "" || document.frm.monthlyincome.value == "select")
	{
		 message = "Please estimate the amount of your total monthly net income.";		 
	}	  
	
	return message;
}


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('Barrie','Brampton', 'Brantford', 'Cambridge', 'Chatham', 'Goderich', 'Guelph', 'Hamilton', 
								  'Kitchener', 'Leamington', 'London', 'Mississauga', 'North York', 'Sarnia', 'Scarborough', 
								  'St Catharines', 'Toronto Area', 'Vaughan', 'Waterloo', 'Windsor');

    var currentcity ="";						

	currentcity =document.frm.selectedcity.value
	document.frm.city[0] =new Option("Select", "Select", false, false);  

	for (var i=0; i < cities.length; i++) 
	{
		document.frm.city[i+1] = new Option(cities[i],cities[i]);	 
		if (currentcity == cities[i])
		{
			document.frm.city.selectedIndex = i+1	 // current selected city
		}
			 
	}
		
	if (currentcity == "Toronto Area")
		{
			showControl("offices_per_city");
		}		
	else
		{
			hideControl("offices_per_city");
		}	
}

function load_offices_per_city()
{
	
	var city_selected = document.frm.city.options[document.frm.city.options.selectedIndex].value;
  var currentoffice = "";

	currentoffice = document.frm.selectedoffice.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];
		  
		  if (currentoffice == offices_per_city[i])
		  {
		   	document.frm.officespercity.selectedIndex = i+1	 // current selected city
		  }	 		  
		}		
	}
	else
	{
		document.frm.officespercity.selectedIndex = -1;
		hideControl("offices_per_city");
	}
}

function validateEvaluationRequestInput(frm)
{
  if ( trim(frm.firstname.value).length < 1 ) 
  {
    alert( "Please enter your first name." );
	frm.firstname.focus();
    return false;
  } 
  	
	var contact_type = 0;
	
	for (i = 0; i < document.frm.preferredcontact.length; i++)
	{
		if (document.frm.preferredcontact[i].checked == true)
		{
			contact_type = document.frm.preferredcontact[i].value;
			break;
		}		
	}
	
	if (contact_type == 0)
	{
		alert ("Please select prefered type of contact.");
		return false;
	}
	else if (contact_type == "e_mail")
	{
		if (trim(frm.email.value).length < 1)
		{	
			alert ("Please enter your email address so that we can get back to you.");
			frm.email.focus();
			return false;
		}
	}
	else if (contact_type == "telephone")
	{
		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 city = frm.city.value;
 
  if(city == "" || city == "Select")
  {
	 alert( "Please select a city." );
	 frm.city.focus();
	 return false;
  }
  else
  {
	  var nearest_office = frm.officespercity.value;
	  var cities_with_multiple_offices = new Array ("Toronto Area", "Hamilton");
	  
	  for(var i=0; i < cities_with_multiple_offices.length; i++)
	  {
	  	if(city == cities_with_multiple_offices[i])
	  	{
			if(nearest_office == "" || nearest_office == "Select")
			{
				alert( "We have more than one office in the city you have selected. \nPlease select the office that is closest to you." );
				frm.officespercity.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; 
}