/* The functions in the file are called from the email trustee form */
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');
	
	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("email_offices_per_city");
		
		/*if(city_selected == "Toronto Area")
		{
			var offices_per_city = new Array("Toronto", "Mississauga", "North York", "Scarborough", "Vaughan");
			
			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(city_selected == "Toronto Area")
		{
			var offices_per_city = new Array("Toronto", "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("email_offices_per_city");
	}
}

function controlFormDisplay(officeSelectionRequired)
{
	//alert("officeSelectionRequired: " + officeSelectionRequired);
	if (officeSelectionRequired == 0)
	{
		hideControl("email_office");
		//showControl("spacing");
	}
	else if (officeSelectionRequired == 1)
	{
		showControl("email_office");
		load_offices_per_city();
		//hideControl("email_offices_per_city");	
		//hideControl("spacing");
	}
}

function validateEmailForm(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;
		}		
	}  
  
  if(isVisible("email_office"))	// city selection is needed
  {
	  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;
				}
			}
		  }
	  }
  }
  
  if ( trim(frm.question.value).length < 1 ) 
  {
    alert( "Please enter your question." );
	frm.question.focus();
    return false;
  }
    
  return true;
}

function validateEmailFormStrict(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 ) 
  {
    alert( "Please enter your telephone number." );
	form.lastname.focus();
    return false;
  }
  
  if(isVisible("email_office"))	// city selection is needed
  {
	  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");
		  
		  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 ( trim(form.question.value).length < 1 ) 
  {
    alert( "Please enter your question." );
	form.question.focus();
    return false;
  }
    
  return true;
}

function isVisible(controlId)
{
	isControlVisible = false;
	
	if (document.getElementById)
	{
		// this is the way the standards work
		if(document.getElementById(controlId).style.visibility == "visible")
		{
			isControlVisible = true;
		}		
	}
	else if (document.all)
	{
		// this is the way old msie versions work
		if(document.all[controlId].style.visibility == "visible")
		{
			isControlVisible = true;
		}	
	}
	else if (document.layers)
	{
		// this is the way nn4 works
		if(document.layers[controlId].visibility == "visible")
		{
			isControlVisible = true;
		}
	}
	
	return isControlVisible;
}

//called from the freedom.html
function validateEmailFormStrict2(form)
{
	if ( trim(form.name.value).length < 1 ) 
	{
	alert( "Please enter your name." );
	form.name.focus();
	return false;
	}

	if ( trim(form.phone.value).length < 1 ) 
	{
	alert( "Please enter your phone number." );
	form.name.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");
		  
		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;
				}
			}
		}
	}
	
	return true;
}	