
// Create XMLHttpRequest object
var http = createRequestObject();

// This is called when the user changes the country <select>
//Changes both the country and the state fields
function changeCountry()
{
	http.open('POST','handle.php');
	http.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
	var action = '&action=update_state';
	var country = '&country='+document.getElementById('country').value;
	var state = '&state='+document.getElementById('state').value;
	var stateElementTabIndex = '&stateTabIndex='+document.getElementById('state').getAttribute('tabindex');
	var qstring = action + country + state + stateElementTabIndex;

	http.onreadystatechange = handleCountryResponse;
	http.send(qstring);
}

// An event handler which awaits a response from the server
function handleCountryResponse()
{
	if (http.readyState == 4)
	{
		if(http.status == 200)
		{
			// Get HTTP Response
			var results = http.responseText.split('|');

			// Get div sections
			var r = document.getElementById('r');
			var s = document.getElementById('s');

			// Replace div section with appropriate input field
			r.innerHTML = results[0];
			s.innerHTML = results[1];

		}
	}
}