function addOption(selectObject,optionText,optionValue) 
{
    var optionObject = new Option(optionText,optionValue)
    var optionRank = selectObject.options.length
    selectObject.options[optionRank]=optionObject
}

function removeAllObjects(selectObject)
{
	while (selectObject.options.length > 0)
	{
			selectObject.options[0] = null;
	} 
}
	
function getStatesForCountryId(iCountryId)
{
	var value = iCountryId;
	var url = 'getStatesForCountryId.php?countryId='+value;
	var AJAX = new CAJAX();

	AJAX.executeRequest('GET', url, AJAX.getToken(), 'includes/XML/');

	var oSelect = document.getElementById('stateSelectInput');
	removeAllObjects(oSelect);
	addOption(oSelect,'Loading',-1);
	oSelect.disabled = true;

	AJAX.request.onreadystatechange = function ()
	{
		if (AJAX.getReadyState() == 'OK')
		{
			//alert('OK');

			var oSelect = document.getElementById('stateSelectInput');
			var oSelectDiv = document.getElementById('stateSelectDiv');
			
			var oTextInput = document.getElementById('stateTextInput');
			var oTextDiv = document.getElementById('stateTextDiv');

			var states = AJAX.request.responseXML.getElementsByTagName('state');		
			removeAllObjects(oSelect);

			if(states.length == 0) //put up a text input
			{
				oSelect.disabled = true;
				oSelectDiv.style.display = 'none';

				oTextInput.disabled = false;
				oTextDiv.style.display = '';
			}

			else //put up the select input
			{
				oSelectDiv.style.display = '';

				oTextInput.disabled = true;
				oTextDiv.style.display = 'none';

				addOption(oSelect,'Choose a State . . .', 'null_select')
				for (i=0;i < states.length ;i++ )
				{
					addOption(oSelect,states[i].firstChild.nodeValue,states[i].firstChild.nodeValue);
				}

				oSelect.disabled = false;
			}
			
		}
	}
	
	AJAX.request.send(null);
}