

function updateCityDropdown(jsonData) {

	var optionListHtml = "<option value=\"\">" + lcloGlobalMessages["store.selector.menu"] + "</option>";
	
	for (x = 0; x < jsonData.cities.length; x++) {
		optionListHtml += "<option value=\"";
		optionListHtml += jsonData.cities[x];
		optionListHtml += "\">";
		optionListHtml += jsonData.cities[x];
		optionListHtml += "</option>"
	}

 	jQuery("select#dd_banner_cities option").remove();
	jQuery("select#dd_banner_cities").append(optionListHtml);
	jQuery("select#dd_banner_cities").removeAttr("disabled");
	
	// disable the stores list
	jQuery("select#dd_banner_stores").attr("disabled", "disabled");

	// disable the submit button
	jQuery("#button_select_store").hide();
}

function updateStoreDropdown(jsonData) {

	var optionListHtml = "<option value=\"\">" + lcloGlobalMessages["store.selector.menu"] + "</option>";
	
	for (x = 0; x < jsonData.stores.length; x++) {
		optionListHtml += "<option value=\"";
		optionListHtml += jsonData.stores[x].storeId;
		optionListHtml += "\"";
		if (jsonData.stores[x].storeId == "5000008") {
			optionListHtml += " style=\"font-weight:bold;\"";
		}
		optionListHtml += ">";
		optionListHtml += jsonData.stores[x].storeName;
		optionListHtml += "</option>"
	}
	
	if (jsonData.stores.length == 0) {
		optionListHtml += "<option value=\"";
		optionListHtml += "\">";
		optionListHtml += "No stores available";
		optionListHtml += "</option>"
	}
	
 	jQuery("select#dd_banner_stores option").remove();
	jQuery("select#dd_banner_stores").append(optionListHtml);
	jQuery("select#dd_banner_stores").removeAttr("disabled");

	// disable the submit button
	jQuery("#button_select_store").hide();
}

jQuery(document).ready(function() {

	if (jQuery.browser.msie == true && jQuery.browser.version < 7) {
		jQuery.ajaxSetup({
			contentType: "application/x-www-form-urlencoded"
		});
	} else {
		jQuery.ajaxSetup({
			scriptCharset: "utf-8" ,
			contentType: "application/x-www-form-urlencoded; charset=utf-8"
		});
	}

	// The provinces dropdown needs to reload the city choices when a selection is made
    jQuery("select#dd_banner_province").change(function () {

		var selectedProvinceCode = jQuery("select#dd_banner_province option:selected").val();
		if (selectedProvinceCode != "") {

				jQuery.post('store_selector.jsp', 
					{
						form_action:'storeSelector',
						provinceCode:selectedProvinceCode,
						city:'',
						storeId:''
			         },
					 function (data) { updateCityDropdown(data);  }, 
					 "json"); 
		}
	}).change();


	// The city dropdown needs to reload the stores choices when a selection is made
    jQuery("select#dd_banner_cities").change(function () {

		var selectedProvinceCode = jQuery("select#dd_banner_province option:selected").val();
		var selectedCity = jQuery("select#dd_banner_cities option:selected").val();
		if (selectedProvinceCode != "" && selectedCity != "") {

				jQuery.post('store_selector.jsp', 
					{
						form_action:'storeSelector',
						provinceCode:selectedProvinceCode,
						city:selectedCity,
						storeId:''
			         },
					 function (data) { updateStoreDropdown(data);  }, 
					 "json"); 
		}
	}).change();


	// default the select button to hidden
	jQuery("#button_select_store").hide();

	// Once something is chosen from the store dropdown, enable the submit button
    jQuery("select#dd_banner_stores").change(function () {

		var value = jQuery("select#dd_banner_stores option:selected").val();
		if (value != "") {

			jQuery("#button_select_store").show();
			jQuery("#hidden_store_id").val(value);
			
		} else {
			jQuery("#button_select_store").hide();
		}

	}).change();
});

