﻿function populateCounties(selectedState) {
	if (selectedState != '') {
		$('indicator').toggleClass('hide');
		var myAjax = new Ajax(
			"/xml/counties-xml.asp?StateAbbreviation=" + selectedState, {
				method: 'get',
				async: 'false',
				onComplete: populateCountiesSelect
			}
		).request();
	}
	else
		$('Counties').disabled = true;
}

function populateCountiesSelect(ajaxResponse) {

	var doc, elements;

//alert(ajaxResponse);
	if (window.ActiveXObject) { // Internet Explorer
		doc = new ActiveXObject("Microsoft.XMLDOM");
		doc.async = false;
		doc.loadXML(ajaxResponse);
	}
	else { // All other browsers
		var parser = new DOMParser();
		doc = parser.parseFromString(ajaxResponse,"text/xml");
	}

	elements = doc.documentElement.getElementsByTagName('county');

	$('Counties').options.length = 0; // Clear any existing options

	for (var i = 0; i < elements.length; i++) {
		$('Counties').options[i] = new Option(elements[i].firstChild.nodeValue + ' (' + elements[i].getAttribute('Properties') + ')', elements[i].getAttribute('CountyID'));
	}
	  
	$('Counties').disabled = false;
	$('indicator').toggleClass('hide');
}
