var which_page = new String();
counties = new ajaxObject('/inc_code/select_counties.inc.php');
counties.callback = function(responseText) {
	if (responseText != '') {
		var arCounties	= responseText.split("	");
		var form_elem	= document.getElementById('County');
		// populate the select box
		county_option = new Option('2. Select Your County:', '');
		form_elem.options[0] = county_option;
		county_option = new Option('------------------------', '');
		form_elem.options[1] = county_option;
		for (i=2; i<(arCounties.length+2); i++) {
			county_option = new Option(arCounties[i-2], arCounties[i-2]);
			form_elem.options[i] = county_option;
		}
		// select/highlight already selected options
		if (select_options_v_query('County', 'County'))
			select_county();
		// display the box
		document.getElementById('County').style.display = '';
	}
}

cities = new ajaxObject('/inc_code/select_cities.inc.php');
cities.callback = function(responseText) {
	if (responseText != '') {
		var arCities	= responseText.split("	");
		var form_elem	= document.getElementById('City');
		// populate the select box
		city_option = new Option('3. Select City:', '');
		form_elem.options[0] = city_option;
		city_option = new Option('------------------------', '');
		form_elem.options[1] = city_option;
		for (i=2; i<(arCities.length+2); i++) {
			city_option = new Option(arCities[i-2], arCities[i-2]);
			form_elem.options[i] = city_option;
		}
		// select/highlight already selected options
		select_options_v_query('City', 'City');
		// display the box
		document.getElementById('City').style.display = '';
	}
}

function select_options_v_query(query_key, which_select) {
	// select/highlight the options already selected in the query string
	var selectBox = document.getElementById(which_select);
	// get the query string
	var query = get_query_variable(query_key);
	if (query != 'null') {
		for (i=0; i<selectBox.length; i++) {
			if (is_array(query)) { // there are multiple selected options
				for (j=0; j<query.length; j++) {
					if (selectBox.options[i].value == query[j]) {
						selectBox.options[i].selected = true;
						continue;
					}
				}
			} else { // there is a single selected option
				if (selectBox.options[i].value == query) {
					selectBox.options[i].selected = true;
					break;
				}
			}
		}
	} else {
		return false;
	}
	return true;
}

function select_state() {
	// display the counties for the selected states
	clearSelect('County');
	document.getElementById('County').style.display = 'none';
	clearSelect('City');
	document.getElementById('City').style.display = 'none';
	var states = get_select_options('State');
	if (states != '')
		counties.update('State='+states+'&page='+which_page);
}

function select_county() {
	// display the cities for the selected counties
	clearSelect('City');
	document.getElementById('City').style.display = 'none';
	var states		= get_select_options('State');
	var counties	= get_select_options('County');
	if (counties != '')
		cities.update('State='+states+'&County='+counties+'&page='+which_page);
}

function get_select_options(which_select) {
	// get all selected/highlighted options from the specified select box
	var selectBox		= document.getElementById(which_select);
	var selectOptions	= new String();
	var reselect		= new Array();
	while (selectBox.selectedIndex != -1) {
			reselect.push(selectBox.selectedIndex);
			if (selectOptions != '')
				selectOptions	= selectOptions+',';
			selectOptions		= selectOptions+selectBox.options[selectBox.selectedIndex].value;
			selectBox.options[selectBox.selectedIndex].selected = false;
	}
	for (i=0; i<reselect.length; i++) {
		selectBox.options[reselect[i]].selected = true;
	}
	return selectOptions;
}

function clearSelect(which_select) {
	// clear the specified select box's contents
	var selectBox = document.getElementById(which_select);
	for (i=selectBox.length-1; i>=0; i--) {
		selectBox.remove(i);
	}
}

function is_array(mixed_var) {
	// check if the specified variable is an array or not
	return (mixed_var instanceof Array);
}

function uunescape(string) {
	// ultimate unescape a query string value (works with '+' signs too)
	string = ""+string;
	while (true) {
		var i = string.indexOf('+');
		if (i < 0)
			break;
		string = string.substring(0, i)+'%20'+string.substring(i+1, string.length);
	}
	
	return unescape(string);
}

function get_query_variable(key) {
	// parse the query string and get the values for the specified key
	var query		= window.location.search.substring(1);
	var vars		= query.split("&");
	var result		= new Array();
	for (i=0; i<vars.length; i++) {
		var pair	= vars[i].split("=");
		if (pair[0] == key)
			result.push(uunescape(pair[1]));
	}
	// return the single value, the array of values, or null
	return ((result.length == 1) ? result[0] : ((result.length > 1) ? result : null));
}