document.write(
	'<div id="catDropDowns" style="display:none">' +
	'	<select id="navLevel1" onchange="f_onOptionSelect(this, 1)"></select>'+
	'	<select id="navLevel2" onchange="f_onOptionSelect(this, 2)" style="display:none;"></select>' +
	'	<select id="navLevel3" onchange="f_onOptionSelect(this, 3)" style="display:none;"></select>' +
	'	<input type="button" onclick="f_goToOption()" id="goToOption" value="GO" />' +
	'</div>'
);

function f_updateOptions(n_parentId, n_level) {
	
	if (!window.o_http)
		return;

	if (window.o_http.readyState != 4) return;
	if (window.o_http.status != 200 && o_http.status != 304)
		throw 'AJAX response error';
		
	var e_select = document.getElementById('navLevel' + n_level);
	if (!e_select) return;

	var s_options = this.o_http.responseText;
	if (s_options.indexOf('|') == -1) {
		if (n_level == 1)
			document.getElementById('catDropDowns').style.display = 'none';
		return;
	}

	var a_options = s_options.split("\n");
	var s_option, a_option;
	
	e_select.options.length = 0;
	e_select.options[0] = new Option('-- Select --');
	
	for (var i = 0; i < a_options.length; i++) {
		s_option = a_options[i];
		if (!s_option || s_option.indexOf('|') == -1)
			continue;
		a_option = s_option.split('|');
		e_select.options[e_select.options.length] = new Option(a_option[2], a_option[0] + "|" + a_option[1]);
	}
	e_select.style.display = 'inline';
	document.getElementById('catDropDowns').style.display = 'block';
	e_select.disabled = false;
}

function f_requestOptions(n_parentId, n_level) {

	var e_select = document.getElementById('navLevel' + n_level);
	if (!e_select) return;

	for (l = n_level; e_select = document.getElementById('navLevel' + l); l++) {
		if (l == 1) {
			e_select.options.length = 0;
			e_select.options[0] = new Option ('Loading...');
			e_select.disabled = true;
		}
		else
			e_select.style.display = 'none';
	}

	if (n_parentId == null)
		return;

	if (window.o_http)
		window.o_http.abort();

	if (window.ActiveXObject) 
		window.o_http = new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest) 
		window.o_http = new XMLHttpRequest();
	else
		return;
		
	window.o_http.onreadystatechange = function () { f_updateOptions(n_parentId, n_level) };
	window.o_http.open("GET", 'http://www.ridetheworld.com/cgi-bin/search/build/get-options.cgi?cat=' + n_parentId, true);
	window.o_http.send(null);
}

function f_onOptionSelect (e_select, n_level) {
	var s_value = e_select.options[e_select.selectedIndex].value;
	if (s_value && s_value.indexOf('|') != -1) {
		var a_value = s_value.split('|');
		f_requestOptions(a_value[0], n_level + 1);
	}
	f_requestOptions(null, n_level + 1);
}

function f_goToOption () {
	var l, e_select;
	for (l = 3; l > 0; l--) {
		if (!(e_select = document.getElementById('navLevel' + l)) || e_select.style.display == 'none')
			continue;
		var s_value  = e_select.options[e_select.options.selectedIndex].value;
		if (!s_value || s_value.indexOf('|') == -1)
			continue;
		var a_value = s_value.split('|');
		window.location = a_value[1];
		return;
	}
}

