	/** 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;
		};
	}


	function step1_populate_state()
	{
		var acceptable_country_franchising_statuses = ["active","inactive","not_franchising"];
		
		if ($("#step1_country").val() != "")
		{
			$("#ajax_errors").html("");
			
			$("#sold_out_country").hide();
			$("#sold_out_region").hide();
			
			$("#step1_region").hide();
			$("#RegionLabel").hide();
			
			$("#loading_image").show();
			
			$.ajax(
			{
				type: "GET",
				url: "region_ajax.php",
				data: "country_id=" + $("#step1_country").val(),
				timeout: 7000,
				dataType: 'json',
				success: function(data,status)
				{
					$("#loading_image").hide();
					
					if (acceptable_country_franchising_statuses.indexOf(data.franchising_status) >= 0)
					{
						if (data.choose_region == 'true')
						{
							$("#step1_region").html(data.dropdown_html);
							
							//select the first item "select one"
							$("#step1_region").attr("selectedIndex",0);

							$("#RegionLabel").show();
							$("#step1_region").show();	
						}
						else
						{
							$("#step1_form").submit();							
						}
					}
					else if (data.franchising_status == 'sold_out')
					{
						$("#sold_out_country").show();
					}
					else
					{
						$("#ajax_errors").html("<p class=\"error\">Sorry, but an unexpected error has occurred.  Please try again.  ERROR: C.F.S</p>");
					}
				},
				error: function(request,textstatus,error)
				{
					$("#loading_image").hide();
					
					//give them an error message		
					if (textstatus === 'timeout')
					{
						$("#ajax_errors").html("<p class=\"error\">Sorry, but this service is temporarily unavailable.  Please try again later.  ERROR: C.T.O.</p>");
					}
					else if (textstatus === 'error')
					{
						$("#ajax_errors").html("<p class=\"error\">Sorry, but this service is temporarily unavailable.  Please try again later.  ERROR: C.G.E.</p>");
					}
					else
					{
						$("#ajax_errors").html("<p class=\"error\">Sorry, but this service is temporarily unavailable.  Please try again later.  ERROR: C.U.E.</p>");
					}
				}
			});
		}
	}
	
	
	function step1_check_region()
	{
		var acceptable_region_franchising_statuses = ["active","inactive","not_franchising"];
		
		$("#ajax_errors").html("");
		
		$("#loading_image").show();
		
		$("#sold_out_region").hide();
		
		$.ajax(
		{
			type: "GET",
			url: "region_status.php",
			data: "region_id=" + $("#step1_region").val(),
			timeout: 7000,
			dataType: 'json',
			success: function(data,status)
			{
				$("#loading_image").hide();
				
				if (acceptable_region_franchising_statuses.indexOf(data.franchising_status) >= 0)
				{
					$("#step1_form").submit();							
				}
				else if (data.franchising_status == "sold_out")
				{
					$("#sold_out_region").show();
				}
				else
				{
					$("#ajax_errors").html("<p class=\"error\">Sorry, but an unexpected error has occurred.  Please try again.  ERROR: R.F.S</p>");
				}				
			},
			error: function(request,textstatus,error)
			{
				$("#loading_image").hide();
				
				//give them an error message		
				if (textstatus === 'timeout')
				{
					$("#ajax_errors").html("<p class=\"error\">Sorry, but this service is temporarily unavailable.  Please try again later.  ERROR: R.T.O.</p>");
				}
				else if (textstatus === 'error')
				{
					$("#ajax_errors").html("<p class=\"error\">Sorry, but this service is temporarily unavailable.  Please try again later.  ERROR: R.G.E.</p>");
				}
				else
				{
					$("#ajax_errors").html("<p class=\"error\">Sorry, but this service is temporarily unavailable.  Please try again later.  ERROR: R.U.E.</p>");
				}
			}
		});			
	}	
	
	
	
	function step3_choose_country()
	{
		var countries_with_states = ["US","CA"];
		
		if ($("#step3_country").val() != "")
		{
			if ( countries_with_states.indexOf($("#step3_country").val()) >= 0)
			{
				$("#step3_region_label").hide();
				$("#step3_region_input").hide();
				$("#step3_state_label").show();
				$("#step3_state_input").show();
			}
			else
			{
				$("#step3_region_label").show();
				$("#step3_region_input").show();
				$("#step3_state_label").hide();
				$("#step3_state_input").hide();
			}
		}
	}	

	
	$(document).ready(function()
	{
		$("#step1_country").bind("change",step1_populate_state);
		$("#step3_country").bind("change",step3_choose_country);
		
		$("#step1_region").bind("change",step1_check_region);
		
		step1_populate_state();
		step3_choose_country();		
	}); 