// JavaScript Document

// Load required classes
AppGlobal.loadClass("Ajax");

var Stores;
if (!Stores) Stores = {};

// Declare properties
Stores.ajaxURL = "/ajax/stores.aspx";

Stores.setLocalStore = function(num) {
	var imgProgress = document.getElementById("imgProgress");
	if (parseInt(num) > 0) {
		var set = document.getElementById("store-set");
		var notfound = document.getElementById("store-notfound");
		// Call ajax script synchronously
		if (imgProgress) imgProgress.style.visibility = '';
		var request = Ajax.newRequest();
		request.open("POST", Stores.ajaxURL, false);
		request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		request.send("a=s&n=" + encodeURIComponent(num));
		if (request.status == 200) {
			// Set new store cookie for 60 days (first number in the equation)
			var vExpireDate = new Date(new Date().getTime() + (60 * 1000 * 60 * 60 * 24) );
			var xmlDoc = request.responseXML;
			var vLocationName = AppGlobal.getXMLValue(xmlDoc.getElementsByTagName("LocationName")[0]);
			var vDomain = AppGlobal.getXMLValue(xmlDoc.getElementsByTagName("Domain")[0]);
			Cookie.set("Local", num, vExpireDate, "/", vDomain);
			// Set panel display
			if (AppGlobal.overlay == null) AppGlobal.overlay = new Overlay("store-popupobj");
			set.style.display = '';
			notfound.style.display = 'none';
			// Track event in Google Analytics
			try { pageTracker._trackEvent('Store Locator', 'Set Local Store', vLocationName); } catch(err) {}
		} else {
			if (AppGlobal.overlay == null) AppGlobal.overlay = new Overlay("store-popupobj");
			// Display failed message
			set.style.display = 'none';
			notfound.style.display = '';
	    }
		if (imgProgress) imgProgress.style.visibility = 'hidden';
	} else throw new Error("Variables missing in AJAX request.");
	return false;
}

// Closes the store selector popup
Stores.closeStoreSelector = function() {
	AppGlobal.overlay.remove();
	AppGlobal.overlay = null;
}