﻿/*
	populates the states ddl with a JSON call
*/
function getStatesByCountryID(countryID, crntStateID, elStateID, hfStateID)
{
	var elStateID = $(controlPrefix + elStateID);
	var elHfStateID = $(controlPrefix + hfStateID);

	//disable the state ddl and remove any exitings options
	elStateID.disabled = true;
	removeDdlOptions(elStateID);
	elHfStateID.value = '';
	
	//perform the search
	var request = new Request.JSON(
	{
		url: subPath + '_ajax/get_states_by_countryid.aspx',
		data: 'CountryID=' + countryID,
		onRequest: function()
		{
			//add the please wait message
			new Element('option', {'value': '', 'text': 'Please wait...'}).inject(elStateID);
		},
		onSuccess: function(objJSON)
		{
			//remove any existing options
			removeDdlOptions(elStateID);

			//if we've failed
			if (objJSON.Valid == 'False')
			{
				//add the message
				new Element('option', {'value': '', 'text': objJSON.Msg}).inject(elStateID);
			}
			else
			{
				//enable the ddl
				elStateID.disabled = false;
				
				//add the returns
				addDdlOptions(elStateID, objJSON.States);
				
				//if this is the initial selection
				if (crntStateID == 0)
				{
					//load the 1st value into the hidden field
					setStateID(elStateID.options[0].value, hfStateID);
				}
				else
				{
					//set the selected value of the state ddl and hidden field
					setDdlSelected(elStateID, crntStateID);
					setStateID(crntStateID, hfStateID);
				}
			}
		},
		onFailure: function(xhr)
		{
			//remove any existing options
			removeDdlOptions(elStateID);

			//set the error message
			new Element('option', {'value': '', 'text': 'Please check you are connected to the internet...'}).inject(elStateID);
		}
	}).send();
}

function setStateID(stateID, hfStateID)
{
	$(controlPrefix + hfStateID).value = stateID;
}