var sURL = "/inc/storeMenu.php";
var country = "";
var loc = "";
var userLang = "";

function findCities(uCountry) {
	$("fieldset#consumer").hide("slow");
	$("fieldset#business").hide("slow");
	
	userLang = $("input[name='lang']").val();

	if(uCountry) {
		country = uCountry;
		$("select#country").val(""+country+"").attr("selected", true);
	}
	else { country = $("select#country option:selected").val(); }
	
	if(country == "") {
		$("select#location").hide();
	}
	else {
		$("select#location").show("slow");
		// Make an asynchronous post call. This is done in order to make the rest of the code "wait" for the callback before executing. We need to wait to be able to chose our cookie-loaded city from the not yet generated drop-down list.
		$.ajax({
			type:		"POST",
			url:		""+sURL+"",
			data:		({country: ""+country+"", lang: ""+userLang+""}),
			success:	function(data){
							$("select#location").html(""+data+"")
						},
			async:		false
		});
	}
} // function findCities()

function findStores(uLoc) {
	if(uLoc) {
		loc = uLoc
		$("select#location").val(""+loc+"").attr("selected", true);
	}
	else { loc = $("select#location option:selected").val(); }

	if(loc == "") {
		$("fieldset#consumer").hide();
		$("fieldset#business").hide();
	}
	else {
		$("fieldset#consumer").show("slow");
		$("fieldset#business").show("slow");
		$.post(""+sURL+"", {location: ""+loc+"", country: ""+country+"", consumer: "1"}, function(data){
			$("#store_info").html(""+data+"")
		});
		$.post(""+sURL+"", {location: ""+loc+"", country: ""+country+"", reseller: "1"}, function(data){
			$("#reseller_info").html(""+data+"")
		});
		$.post(""+sURL+"", {location: ""+loc+"", country: ""+country+"", agent: "1"}, function(data){
			$("#agent_info").html(""+data+"")
		});
	}
} // function findStores()

$(document).ready(function(){
	// Hide everything we don't need right now.
	$("fieldset#consumer").hide();
	$("fieldset#business").hide();
	$("#store_info").hide();
	$("#agent_info").hide();
	$("#reseller_info").hide();

	// Look for stored location info in a cookie
	var cookie = GetCookie("location");
	if (cookie) {
		var stop = cookie.indexOf("+");
		country = cookie.substring(0, stop);
		loc = cookie.substring(stop+1);
		
		findCities(country);
		findStores(loc);
	} else {
		$("select#location").hide();
	}

	$("fieldset#consumer").toggle(function (event) {
		$("fieldset#consumer").children("legend").addClass("active");
		$("#store_info").show("slow");
	}, function (event) {
		$("fieldset#consumer").children("legend").removeClass("active");
		$("#store_info").hide("slow");
	});
	$("fieldset#business").toggle(function (event) {
		$("fieldset#business").children("legend").addClass("active");
		$("#agent_info").show("slow");
		$("#reseller_info").show("slow");
	}, function (event) {
		$("fieldset#business").children("legend").removeClass("active");
		$("#agent_info").hide("slow");
		$("#reseller_info").hide("slow");
	});
});
