function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
	try {
	  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	  try {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (E) {
		xmlhttp = false;
	  }
	}
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
	try {
	  xmlhttp = new XMLHttpRequest();
	} catch (e) {
	  xmlhttp = false;
	}
  }
  return xmlhttp;
}

var http = getHTTPObject();

var isWorking = false;

function updateSearchOptions(which) {
	if(!isWorking && http) {
		//var county = document.getElementById('county').options[document.getElementById('county').selectedIndex].value;
		var county = document.formnew.county.options[document.formnew.county.selectedIndex].value;
		//var town = document.getElementById('town').options[document.getElementById('town').selectedIndex].value;
		var town = document.formnew.town.options[document.formnew.town.selectedIndex].value;
		http.open("GET", "ajax.asp?updateSearchOptions=1&which=" + escape(which) + "&county=" + escape(county) + "&town=" + escape(town), true);
		http.onreadystatechange = handleUpdateSearchOptionsHttpResponse;
		isWorking = true;
		http.send(null);
	}
}

function handleUpdateSearchOptionsHttpResponse() {
	if(http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {

			var results = http.responseText.split("|");

			var countyObj = document.formnew.county;
			var countySelected = countyObj.options[countyObj.selectedIndex].value;
			
			var townObj = document.formnew.town;
			var townSelected = townObj.options[townObj.selectedIndex].value;

/*
			if(results[0] != 'county') {
				countys = results[1].split(",");
				countyObj.options.length = 0;
				countyObj.options[0] = new Option('Choose county','');
				var c = 1;
				for(i=0;i<countys.length-1;i+=2) {
					countyObj.options[c] = new Option(countys[i],countys[i+1]);
					if(countySelected == countys[i+1])
						countyObj.options[c].selected = true;
					c+=1;
				}
			}
*/			
//			if(results[0] != 'town') {
				towns = results[2].split(",");
				townObj.options.length = 0;
				townObj.options[0] = new Option('Choose a town or city','');
				var c = 1;
				for(i=0;i<towns.length-1;i+=2) {
					townObj.options[c] = new Option(towns[i],towns[i+1]);
					if(townSelected == towns[i+1])
						townObj.options[c].selected = true;					
					c+=1;
				}
//			}

			isWorking = false;
		}
	}
}
