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] = "family_panel";
	panels[5] = "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";
	
	//hideControl("offices_per_city");
	load_offices_per_city();
}

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

function switchPanel(activePanelId) 
{	
	var previousPanel;
	var nextPanel;
	
	for (var i=0; i < panels.length; i++ ) 
	{
		if(panels[i] == activePanelId)
		{
			showControl(panels[i]);
			showControl(tabsOn[i]);
			hideControl(tabsOff[i]);	
			
			if(i > 0)
			{
				previousPanel = panels[i - 1];
				
				if(i < panels.length - 1)
				{
					nextPanel = panels[i + 1];
				}
				else
				{
					nextPanel = "";
				}
			}
			else
			{
				previousPanel = "";
				nextPanel = panels[i + 1];
			}
						
			if(previousPanel == "")
			{
				hideControl("previous_button");
				hideControl("submit_button");
				showControl("next_button");
			}
			
			if(nextPanel == "")
			{
				hideControl("next_button");	
				showControl("previous_button");
				showControl("submit_button");
			}
			
			if(previousPanel != "" && nextPanel != "")
			{
				hideControl("submit_button");	
				showControl("previous_button");
				showControl("next_button");
			}			
		}
		else
		{
			hideControl(panels[i]);
			hideControl(tabsOn[i]);
			showControl(tabsOff[i]);
		}
	}
	
	document.frm.previousPanel.value = previousPanel;
	document.frm.nextPanel.value = nextPanel;	
}

function load_cities()
{
	var cities = new Array('Campbell River', 'Comox', 'Courtenay', 'Cranbrook', 'Duncan', 'East Kootenays', 'Nanaimo', 'Parksville', 'Port Alberni', 'Qualicum', 'Victoria');
	
	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 load_offices_per_city()
{
	var city_selected = document.frm.city.options[document.frm.city.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", "Etobicoke", "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");
	}
}

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(form.phone.value).length < 1 && trim(form.email.value).length < 1 ) 
  {
    alert( "Please enter either your phone number or your email so that we can get back to you." );
	form.phone.focus();
    return false;
  }
  
  var city = form.city.value;
 
  if(city == "" || city == "Select")
  {
	 alert( "Please select a city." );
	 form.city.focus();
	 return false;
  }
  else
  {
	  var nearest_office = form.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." );
				form.officespercity.focus();
				return false;
			}
	  	}
	  }
  }
  
  if(form.privacyagreement.checked == false)
  {
		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; 
}

//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"; 
	}
}
