﻿function setStoreValue(id, text){
	var hdnStoreID = $get(hdnStoreIDClientID);
	var hdnStoreName = $get(hdnStoreNameClientID);
	hdnStoreID.value = id;
	hdnStoreName.value = text;
}

function getStoresByState() {
	//alert('getStoresByState');
	try {
		var ddlState = $get(ddlStateClientID);
		//alert('ddlState = ' + ddlState);
		if (ddlState){
			var state = ddlState.options[ddlState.selectedIndex].value;
			//alert('state = ' + state);
			// If the user selected the "State" option, reset the stores
			if (state == ''){
				// Get the store dropdownlist
				var ddlStore = $get(ddlStoreClientID);
				resetStoreDropDownList(ddlStore, 'Location');
			}
			else{
				UnoWS.GetStoresByState(state, OnGetStoresByStateRequestComplete);
			}
		}
	}
	catch(err) {
	}
}

function OnGetStoresByStateRequestComplete(results) {
	//alert('OnGetStoresByStateRequestComplete');
	//alert('results = ' + results);
	if (results != null) {
		loadStores(results);
	}
}

function loadStores(results) {
	//alert('loadStores');
	var ddlStore = $get(ddlStoreClientID);

	// Clear out the existing items
	ddlStore.length = 0;

	// Add the first item
	objOption = new Option('Location', '', false, false);

	//	Add the option to the destination SELECT
	ddlStore.options[0] = objOption;
	
	// Create a variable to store the index of the previously selected item
	var index = -1;
	
	// Add the new items
	for (var i = 0; i < results.length;  i++) {
		//	Create a new option with the value and text of the current option
		objOption = new Option(results[i].Text, results[i].Value, false, false);

		//	Add the option to the destination SELECT
		ddlStore.options[ddlStore.options.length] = objOption;
	}
	
	ddlStore.disabled = false;
}

function resetStoreDropDownList(ddlDropDownList, strFirstOptionText){
	// Clear out the existing stores
	ddlDropDownList.length = 0;
	// Disable the store dropdownlist
	ddlDropDownList.disabled = true;
	// Add the first item
	objOption = new Option(strFirstOptionText, '', false, false);
	ddlDropDownList.options[0] = objOption;
}

