﻿var XMLHttp = null;

function CountyOnChange() {
	var CountyList = document.getElementById("ctl00_ctl02_DropDownCounty");
	var CityList = document.getElementById("ctl00_ctl02_DropDownCity");
	
	// Make sure the default item isn't selected.
	if(CountyList.selectedIndex != 0) {
		CityList.disabled = true;
		
		var SelectedCounty = CountyList.options[CountyList.selectedIndex].value;
		var RequestUrl = "/!Controls/FindCity.aspx" + "?SelectedCounty=" + encodeURIComponent(SelectedCounty);
		
		var XMLHttp = GetHTTPObject();
		XMLHttp.onreadystatechange = CountyOnReadyDoSomething;
		
		XMLHttp.open("GET", RequestUrl, true);
		XMLHttp.send();
		
		
	}
	else {
		CityList.innerHTML = "";
		CityList.disabled = true;
	}
}

// Get the XML Http object.
function GetHTTPObject() {
	var _XMLHttp = null;
	if(window.XMLHttpRequest) {
		_XMLHttp = new XMLHttpRequest();
	}
	else if(window.ActiveXObject) {
		_XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return _XMLHttp;
}

// Check when the XML http object is ready.
function CountyOnReadyDoSomething() {
	var CityList = document.getElementById("ctl00_ctl02_DropDownCity");
	if (this.readyState == 4 && this.status == 200) {
		var list = json_parse(this.responseText).DDL;
		var DDL = document.getElementById("ctl00_ctl02_DropDownCity");
		
		DDL.options.length = 0;
		
		for (var i = 0; i < list.length; i++) {
			var option = new Option(list[i].text, list[i].value, false, false);
			DDL.options[DDL.length] = option;
		}
		CityList.disabled = false;
	}
	else if (this.readyState == 4 && this.status != 200) {
	}
}
function ContractingAuthorityShowResults(text, county, city) {
	if (text.length > 0) {
		window.location = "/FindContractingAuthorityResult.aspx?q=" + encodeURIComponent(text) + "&County=" + encodeURIComponent(county) + "&City=" + encodeURIComponent(city);
	}
	else if(county > 0) {
		window.location = "/FindContractingAuthorityResult.aspx?q=" + encodeURIComponent(text) + "&County=" + encodeURIComponent(county) + "&City=" + encodeURIComponent(city);
	}
}
