	/** country specific properties **/
	var countries_choose_state = ['AU','CAN','CA','MX','ENG','ES','FR','IE','NIR','NZ','SCT','UK','USA','US','WA','WLS','ZA'];
	var countries_that_have_counties = ['ENG','IE','NIR','SCT','UK','WA','WLS'];
	var countries_that_have_departments = ['FR'];
	var countries_enter_postal_code = ['AU','CAN','CA','ENG','MX','SCT','UK','USA','US','WLS'];
	var states_choose_city = ['CA','CAN','ES','MX','US','USA'];
	
	var show_postal_code_form = false;
	var show_state_form = false;
	
	var county_prompt = 'County';
	var state_prompt = 'State';
	//var department_prompt = 'Department';
	var department_prompt = 'City';
	
		/** IE 6 does not have the indexOf array function **/
	if (!Array.prototype.indexOf)
	{
	  Array.prototype.indexOf = function(elt /*, from*/)
	  {
	    var len = this.length;

	    var from = Number(arguments[1]) || 0;
	    from = (from < 0) 
	         ? Math.ceil(from)
	         : Math.floor(from);
	    if (from < 0)
	      from += len;

	    for (; from < len; from++)
	    {
	      if (from in this &&
	          this[from] === elt)
	        return from;
	    }
	    return -1;
	  };
	}

	/** update the hidden fields **/
	function set_hidden_fields()
	{
		/** set the country hidden fields **/
		hidden_country = $("#country").val();
		$("#hidden_postal_country").val(hidden_country);
		$("#hidden_state_country").val(hidden_country);
		$("#hidden_city_country").val(hidden_country);
		
		/** set the state hidden fields **/
		hidden_state = $("#state").val();
		$("#hidden_city_state").val(hidden_state);
	}
	

	/** hide fields **/
	function hide_everything_but_country()
	{
		$("#postal_code_right").hide();
		$("#state_right").hide();
		$("#city_right").hide();
		$("#state_right_or_postal_code_right").hide();					
		$("#state").hide();
		$("#city").hide();	
	}


	/** process data when they pick a country **/
	function set_country()
	{
		/** update the hidden fields within each form. **/
		set_hidden_fields();

		/** show/hide the dropdowns - they are populated via an AJAX request **/
		hide_everything_but_country();

		if ($("#country").val() !== '')
		{
			/** Determine if we should show the postal code input box**/
			if (countries_enter_postal_code.indexOf($("#country").val()) >= 0)
			{
				show_postal_code_form = true;
				$("#postal_code_right").show();
			}
			else
			{
				show_postal_code_form = false;
				$("#postal_code_right").hide();
			}

			/** Determine if we should show the state input box.  If so, ajax the list of states from the DB **/
			if (countries_choose_state.indexOf($("#country").val()) >= 0)
			{
				show_state_form = true;
				
				/** change the prompt to say county/state depending on the country **/
				if (countries_that_have_counties.indexOf($("#country").val()) >= 0)
				{
					$("#state_prompt").html(county_prompt);
				}
				else if (countries_that_have_departments.indexOf($("#country").val()) >= 0)
				{
					$("#state_prompt").html(department_prompt);
				}
				else 
				{
					$("#state_prompt").html(state_prompt);
				}
				
				$("#state_right").show();
				
				/** show the loader for the drop down **/
				$("#loading_state").show();
			}
			else
			{
				show_state_form = false;
				$("#state_right").hide();
			}

			/** Determine if we should show "OR" within the design of the page, or submit the form **/
			if (show_state_form && show_postal_code_form)
			{
				$("#state_right_or_postal_code_right").show();

				highlight_and_populate_state();
				highlight('postal_code_right',null);
			}
			else if (show_state_form)
			{
				$("#state_right_or_postal_code_right").hide();

				highlight_and_populate_state();
			}
			else if (show_postal_code_form)
			{
				$("#state_right_or_postal_code_right").hide();
				//animate the postal code area
				highlight('postal_code_right');
			}
			else
			{
				$("#state_right_or_postal_code_right").hide();
				$("#CountryLocationFinder").submit();
			}
		}
	}		


	/** process data when they pick a state/region **/
	function set_state()
	{
		/** update the hidden fields within each form. **/
		set_hidden_fields();

		if ($("#state").val() !== '')
		{
			/** Determine if we should show the cities drop down box **/
			if (states_choose_city.indexOf($("#country").val()) >= 0)
			{
				/** show the table **/
				$("#city_right").show();				

				/** hide the dropdown while we populate it **/
				$("#city").hide();

				/** show the loader for the dropdown **/
				$("#loading_city").show();

				highlight_and_populate_city('city_right');
			}
			else
			{
				$("#StateLocationFinder").submit();
			}
		}
		else
		{
			/** hide the city **/
			$("#city_right").hide();
		}
	}

	function populate_city()
	{
		/** populate the html **/
		$.ajax(
		{
			type: "GET",
			url: "ajax_get_cities.php",
			data: "country=" + $("#country").val() + "&club_state=" + $("#state").val(),
			timeout: 10000,
			success: function(locations)
			{
				$("#error_message").html('');
				$("#city").html(" " + locations);
				$("#city").val('');
				$("#loading_city").hide();
				$("#city").show();
			},
			error: function(request,textstatus,error)
			{
				/** hide the city **/
				$("#city_right").hide();

				/** give them an error message **/		
				if (textstatus === 'timeout')
				{
					$("#error_message").html("Sorry, but this service is temporarily unavailable.  Please try again later.  Error: T.O.");
				}
				else if (textstatus === 'error')
				{
					$("#error_message").html("Sorry, but this service is temporarily unavailable.  Please try again later.  ERROR: G.");
				}
			}				
		});
	}

	function populate_state()
	{
		$.ajax(
		{
			type: "GET",
			url: "ajax_get_states.php",
			data: "country=" + $("#country").val(),
			timeout: 10000,
			success: function(states,status)
			{
				$("#error_message").html('');
				$("#state").html(" " + states);
				$("#state").val('');
				$("#loading_state").hide();
				$("#state").show(); 

			},
			error: function(request,textstatus,error)
			{
				/** hide everything. **/
				hide_everything_but_country();

				/** give them an error message **/		
				if (textstatus === 'timeout')
				{
					$("#error_message").html("Sorry, but this service is temporarily unavailable.  Please try again later.  Error: T.O.");
				}
				else if (textstatus === 'error')
				{
					$("#error_message").html("Sorry, but this service is temporarily unavailable.  Please try again later.  ERROR: G.");
				}
			}
		});	
	}

	function set_city()
	{
		if ($("#city").val() !== '')
		{
			$("#CityLocationFinder").submit();
		}
	}

	function highlight_and_populate_state()
	{
		highlight('state_right',populate_state);
	}

	function highlight_and_populate_city()
	{
		highlight('city_right', populate_city);
	}
	
	function highlight(id,callback)
	{
	    $("#" + id).animate( { backgroundColor:"#FCF1F7" }, 1000, 'swing', callback).animate( { backgroundColor:"#FFFFFF" }, 1000, 'swing');
	}
	
	
	$(document).ready(function()
	{
		$("[name=country]").bind("change",set_country);
		$("[name=state]").bind("change",set_state);
		$("[name=city_letter]").bind("change",set_city);

		set_country();
	});	