// JavaScript Document

var isIE = navigator.appName == 'Microsoft Internet Explorer';

// create instance of XMLHttp
var xmlReq = null;
if (isIE) {
    try {
        xmlReq = new ActiveXObject(
            (navigator.userAgent.toLowerCase().indexOf('msie 5') != -1) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP'
        );            
    }
    catch(e) {
        alert("You need to enable active scripting and activeX controls.")
    }
}
else {
    xmlReq = new XMLHttpRequest();
    if (! xmlReq) {
        alert("XMLHttpRequest is not supported on this browser.");
    }
}

activeSelectName = 'stateid';

function sendRequest(method, url, processCallback) 
{
	xmlReq.onreadystatechange = processCallback;    
    xmlReq.open(method, url);
    if (isIE) xmlReq.send();
    else xmlReq.send(null);
}


function processSelect()
{
	if ((xmlReq.readyState == 4) && (xmlReq.status == 200))
	{

		var response = xmlReq.responseText;		

		if (activeSelectName != null)
		{
			var selectName = activeSelectName; activeSelectName = null;
			
			selectName.options.length = 0;
			var option = document.createElement("option");
			option.value = 0;
			option.text = '[Any]';
			selectName.options.add(option);
			
			if (response.length > 0){
				
				var arr_items = response.split(';');										
				
				var i = 0;
				for (i=0;i<arr_items.length;i++){
					var option = document.createElement("option");
					var arr_item = arr_items[i].split(":");
					option.value = arr_item[0];
					option.text = arr_item[1];
					selectName.options.add(option);
				}
			}
			
			selectName.disabled = false;
			
		}
		if (! isIE) {
			xmlReq.onreadystatechange = void(0);
		}
		xmlReq.abort();
	}	
}

function selectCountry(countrySelect, stateSelectName)
{
	var countryID = countrySelect.options[countrySelect.selectedIndex].value;
	activeSelectName = stateSelectName;
	activeSelectName.disabled = true;
	sendRequest("GET", "index.php?do=db.printStates&countryid=" + countryID, processSelect);
}

function selectState(stateSelect, areaSelectName)
{
	var stateID = stateSelect.options[stateSelect.selectedIndex].value;
	activeSelectName = areaSelectName;
	activeSelectName.disabled = true;
	sendRequest("GET", "index.php?do=db.printAreas&stateid=" + stateID, processSelect);
}
