// updated: 14.07.2009

function getOptions(url, select, func_name) {
	if (!is_object(select)) {
		select = document.getElementById(select);
	}
	if ( (select.options.length < 2) || (func_name == null) ) {
		ajaxRequest(url, null, function(response) { setOptions(select, response.options, func_name); });
	}
}

function getRoutesOptions(select) {
	getOptions('/not_routes/index.ajax', select, getRouteOptionText);
}

function getStreetsOptions(select) {
	getOptions('/streets/index.ajax', select, getStreetOptionText);
}

function getKsOptions(select) {
	getOptions('/not_routes_ks/index.ajax', select, getKsOptionText);
}

function getSubwayStationsOptions(select) {
	getOptions('/subway/stations/index.ajax', select, getSubwayStationOptionText);
}

function setOptions(select_object, options, func_name) {
	if (options) {
		select_object.innerHTML = '';
		for(var i in options) {
			option = document.createElement('OPTION');
			if (!empty(options[i].id)) {
				option.setAttribute('value', options[i].id);
			} else {
				option.setAttribute('value', i);
			}
			if (func_name != null) {
				option.innerHTML = func_name(options[i]);
			} else {
				option.innerHTML = options[i];
			}
			select_object.appendChild(option);
		}
		/*
		for(i=0; i < options.length; i++) {
			option = document.createElement('OPTION');
			option.setAttribute('value', options[i].id);
			if (func_name != null) {
				option.innerHTML = func_name(options[i]);
			} else {
				option.innerHTML = options[i];
			}
			select_object.appendChild(option);
		}
		*/
		select_object.selectedIndex = 0;
	} else {
		return false;
	}
}

function getRouteOptionText(option) {
	if (option) {
		text = option.type.name + ' ¹' + option.name;
		return text;
	} else {
		return false;
	}

}

function getStreetOptionText(option) {
	if (option) {
		text = option.name + ', ' + option.type.full_name + ' (' + option.routes_count +')';
		return text;
	} else {
		return false;
	}

}

function getKsOptionText(option) {
	if (option) {
		text = option.name + ' (' + option.routes_count +')';
		return text;
	} else {
		return false;
	}

}

function getSubwayStationOptionText(option) {
	if (option) {
		text = option.name + ' (' + option.not_routes_count +')';
		return text;
	} else {
		return false;
	}

}
