var initially_checked_stations;
var initially_checked_areas;
var current_tab;
var checked_station_values = $A([]);
var checked_area_values = $A([]);

function setStations() {
	for (i = 0; true; i++) {
		var e = $('station_' + i);
		if (e == null) {
			break;
		}
		if (initially_checked_stations.include(e.value)) {
			e.checked = true;
		}
	};
}

function setAreas() {
	for (i = 0; true; i++) {
		var e = $('area_' + i);
		if (e == null) {
			break;
		}
		if (initially_checked_areas.include(e.value)) {
			e.checked = true;
		}
	}
}

function updateStations() {
	var route_select = $('route');
	if (route_select == null) {
		return;
	}
	args = {
		method: 'get',
		parameters: { id: $('route').value, tab: current_tab },
		onComplete: function() { setStations(); }
	};
	new Ajax.Updater('station_checks', 'update_stations', args);
}

function updateAreas() {
	var area_select = $('area_select');
	if (area_select == null) {
		return;
	}
	args = {
		method: 'get',
		parameters: { id: area_select.value, tab: current_tab },
		onComplete: function() { setAreas(); }
	};
	new Ajax.Updater('area_checks', 'update_areas', args);
}

function enableRouteSelect() {
	for (i = 0; true; i++) {
		var e = $('station_' + i);
		if (e == null) {
			break;
		}
		e.checked = checked_station_values.include(e.value);
		var l = $('label_for_station_' + i);
		e.disabled = l == null || l.innerHTML.match(/\(0\)/);
	}
	$('route').disabled = false;
	$('area_select').disabled = true;
}

function enableAreaSelect() {
	checked_station_values.clear();
	for (i = 0; true; i++) {
		var e = $('station_' + i);
		if (e == null) {
			break;
		}
		if (e.checked) {
			checked_station_values.push(e.value);
		}
		e.disabled = true;
	}
	$('route').disabled = true;
	$('area_select').disabled = false;
}
