﻿function submitPostalCode()
{
    var url = 'AccessibleStoreSelector.aspx?sessionID=' + SESSION_ID;
           var postalCode = document.forms[0].txtPostalCode.value;
            // Start building the URL            
            url += '&banner=' + BANNER_NAME;
            url += '&language=' + LANGUAGE;          
            url += '&postalcode=' + postalCode.replace(' ', '');
            
            // Validation
            if (postalCodeValidate(postalCode))
                window.location.href = url;
            else
                alert("Please enter in a valid postal code value.");
}

function loadProvinces()
{
    var url = '../cached_pages/GetGeoData.ashx?';
    url += 'view=PROVINCE';
    url += '&languageid=' + LANGUAGE_ID;
    url += '&bannerid=' + BANNER_ID;

    queueRequest(url, 'updateProvinceContent');   // queue the AJAX request
}

function updateProvinceContent(json)
{
    var PROVINCE_LIST = json;
    var provinceListHtml = "";
    
    if (PROVINCE_LIST.provinces.length > 1)
    {
        for (var i=0; i < PROVINCE_LIST.provinces.length; i++ )
	        provinceListHtml = provinceListHtml + "<a href='AccessibleCitySelector.aspx?" + window.location.search.substring(1) + "&province=" + PROVINCE_LIST.provinces[i].provinceid + "'>" + PROVINCE_LIST.provinces[i].provincename + "</a><br>";
		
        var oElement;

		oElement = document.getElementById("ProvinceList");
		oElement.innerHTML = provinceListHtml;
		    
	    document.getElementsByTagName("a")[0].focus();
    } 
    else
    {
        var targetUrl = "AccessibleCitySelector.aspx?" + window.location.search.substring(1); ;
        var params = '';
        if (PROVINCE_LIST.provinces.length == 1)
			    params+= "&province=" + PROVINCE_LIST.provinces[0].provinceid;
		
	    window.location.href = targetUrl + params;
    }
       
}

function loadCities()
{
    var url = '../cached_pages/GetGeoData.ashx?';
            url += 'view=CITY';
            url += '&provinceid=' + PROVINCE_ID;
            url += '&languageid=' + LANGUAGE_ID;
            url += '&bannerid=' + BANNER_ID;
           
            queueRequest(url, 'updateCityContent');   // queue the AJAX request
}

function updateCityContent(json)
{
    var CITY_LIST = json;
    var cityListHtml = "";

    if (CITY_LIST.cities.length > 1) 
    {
        for (i = 0; i < CITY_LIST.cities.length; i++) {
            cityListHtml = cityListHtml + "<a href='AccessibleStoreSelector.aspx?" + window.location.search.substring(1) + "&city=" + CITY_LIST.cities[i].cityid + "'>" + CITY_LIST.cities[i].cityname + "</a><br>";
        }
        var oElement;

        oElement = document.getElementById("CityList");
        oElement.innerHTML = cityListHtml;

        document.getElementsByTagName("a")[0].focus();        
    }
    else
    {
        var targetUrl = "AccessibleStoreSelector.aspx?" + window.location.search.substring(1);
        var params = '';
        if (CITY_LIST.cities.length == 1)
            params += "&city=" + CITY_LIST.cities[0].cityid;

        window.location.href = targetUrl + params;
    }
}

function loadStores()
{
    var url = "";
    if (POSTAL_CODE != '')
        loadClosestStores();
    else
    {
        url = '../cached_pages/GetGeoData.ashx?';
            url += 'view=STORE';
            url += '&cityid=' + CITY_ID;
            url += '&languageid=' + LANGUAGE_ID;
            url += '&bannerid=' + BANNER_ID;
           
        queueRequest(url, 'updateStoreContent');   // queue the AJAX request
    }
}

function updateStoreContent(json)
{
    var STORE_LIST = json;
    var storeListHtml = "";
	  
    if (STORE_LIST.stores.length > 1)
    {
        for (var i=0; i < STORE_LIST.stores.length; i++ )
            storeListHtml += "<a href='../publicationdirector.ashx?" + window.location.search.substring(1) + "&storeid=" + STORE_LIST.stores[i].storeid + "'>" + STORE_LIST.stores[i].storename + "</a><br>";
		var oElement;

		oElement = document.getElementById("StoreList");
		oElement.innerHTML = storeListHtml;
		    
	    document.getElementsByTagName("a")[0].focus();
    }
    else
    {
        var targetUrl = "../publicationdirector.ashx?" + window.location.search.substring(1);
        var params = '';
		    if (STORE_LIST.stores.length == 1)
		        params += "&storeid=" + STORE_LIST.stores[0].storeid;
		
		    window.location.href = targetUrl + params;
    }
}