﻿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 ddlProvince = gE('ddlProvince');
    ddlProvince.length = 0;
    if (PROVINCE_LIST.provinces.length > 0)
    {
        ddlProvince.options[ddlProvince.length]=new Option(PROVINCE_SELECTION,"");
        for (var i=0; i < PROVINCE_LIST.provinces.length; i++ )
            ddlProvince.options[ddlProvince.length]=new Option(PROVINCE_LIST.provinces[i].provincename,PROVINCE_LIST.provinces[i].provinceid);		
        ddlProvince.options[0].selected=true;
        
        if (PROVINCE_LIST.provinces.length == 1)
        {
            ddlProvince.options[1].selected=true;
            loadCities();
        }
    }
}

function loadCities()
{
    var ddlProvince = gE('ddlProvince');
    var url = 'cached_pages/GetGeoData.ashx?';
            url += 'view=CITY';
            url += '&provinceid=' + ddlProvince[ddlProvince.selectedIndex].value;
            url += '&languageid=' + LANGUAGE_ID;
            url += '&bannerid=' + BANNER_ID;
           
            queueRequest(url, 'updateCityContent');   // queue the AJAX request
}

function updateCityContent(json)
{
    var CITY_LIST = json;
    var ddlCity = gE('ddlCity');
    ddlCity.length = 0;
//    hide(gE('pnlSelectStore'));
    if (CITY_LIST.cities.length > 0)
    {
        ddlCity.disabled=false;
    
        ddlCity.options[ddlCity.length]=new Option(CITY_SELECTION,"");
        for (var i=0; i < CITY_LIST.cities.length; i++ )
            ddlCity.options[ddlCity.length]=new Option(CITY_LIST.cities[i].cityname,CITY_LIST.cities[i].cityid);		
        ddlCity.options[0].selected=true;

        if (CITY_LIST.cities.length == 1) {
            ddlCity.options[1].selected = true;
            loadStores();
        }
        else {
            showSubmitButton(false);
        }
    }
}

function loadStores()
{
    var ddlCity = gE('ddlCity');
    var url = 'cached_pages/GetGeoData.ashx?';
            url += 'view=STORE';
            url += '&cityid=' + ddlCity[ddlCity.selectedIndex].value;
            url += '&languageid=' + LANGUAGE_ID;
            url += '&bannerid=' + BANNER_ID;
           
            queueRequest(url, 'updateStoreContent');   // queue the AJAX request
}

function updateStoreContent(json)
{
    var STORE_LIST = json;
    var ddlStore = gE('ddlStore');
	ddlStore.length = 0;
//    hide(gE('pnlSelectStore'));
    if (STORE_LIST.stores.length > 0)
    {
        ddlStore.disabled=false;
        
        ddlStore.options[ddlStore.length]=new Option(STORE_SELECTION,"");
        for (var i=0; i < STORE_LIST.stores.length; i++ )
        {
            ddlStore.options[ddlStore.length]=new Option(STORE_LIST.stores[i].storename,STORE_LIST.stores[i].storeid);		
        }
        ddlStore.options[0].selected=true;

        if (STORE_LIST.stores.length == 1) {
            ddlStore.options[1].selected = true;
            showSubmitButton(true);
        }
        else
            showSubmitButton(false);
    }
}

function ddlProvince_SelectedIndexChanged()
{
    var ddlStore = gE('ddlStore');
	ddlStore.length = 0;
    ddlStore.disabled=true;

    var ddlProvince = gE('ddlProvince');
    if (ddlProvince[ddlProvince.selectedIndex].value != 0)
        loadCities();
    else
    {
        var ddlCity = gE('ddlCity');
	    ddlCity.length = 0;
	    ddlCity.disabled = true;
	    showSubmitButton(false);
	    ddlStore.length = 0;
	    ddlStore.disabled = true;
	    showSubmitButton(false);
    }
}

function ddlCity_SelectedIndexChanged()
{
   var ddlCity = gE('ddlCity');
   if (ddlCity[ddlCity.selectedIndex].value != 0) {
       loadStores();
   }
   else {
       var ddlStore = gE('ddlStore');
       ddlStore.length = 0;
       ddlStore.disabled = true;
       showSubmitButton(false);
   }
}

function ddlStore_SelectedIndexChanged()
{
    var ddlStore = gE('ddlStore');
    if (ddlStore[ddlStore.selectedIndex].value != 0)
        showSubmitButton(true);
    else
        showSubmitButton(false);
}

function showSubmitButton(display) 
{
    if (display)
        show(gE('btnSelectStore'));
    else
        hide(gE('btnSelectStore'));
}

function btnSelectStore_Click() {

    
    var redirectUrl = "PublicationDirector.ashx?" + window.location.search.substring(1);
    redirectUrl += "&storeid=" + gE('ddlStore')[ gE('ddlStore').selectedIndex].value;

    window.location.href = redirectUrl.replace("&changestore=true", "");
}