function find_stations(route_id) {
	if (route_id>0) {
		fill_select_start(route_id,'startsted_id');
	}
}
/*
function fill_select(route_id,type,select_id) {
if (route_id>0) {
stations = routes[route_id].get_stations(type);
select_box = document.getElementById(select_id);
select_box.options.length = 0;

for (i=0;i<stations.length;i++) {
option = new Option(stations[i].name,stations[i].id);
select_box.options[select_box.options.length] = option;
}
}
}
*/
function compare_option(a,b) {
	return true;
}

function preselect_stations(route_id,start_station_id,stop_station_id) {
	find_stations(route_id);
	fill_select_start(route_id,'startsted_id',start_station_id);
	fill_select_stop(start_station_id,'route_id','stoppested_id',stop_station_id);
}

function printElt(element, id, array) {
	option = new Option(element['station'].name,id);
	this.options[this.options.length] = option;
}

function fill_select_start(master_route_id,select_id,pre_selected_station_id) {
	pre_selected_station_id = (pre_selected_station_id===undefined) ? -1 : pre_selected_station_id;
	
	if (master_route_id>0) {
		stations = master_routes[master_route_id].get_stations('start');
		select_box = document.getElementById(select_id);
		select_box.options.length = 0;

		//stations.forEach(printElt,select_box);

		/*		for (key in stations)   {
		if (selected_station_id==-1) {
		selected_station_id = key;
		}
		element = stations[key];
		option = new Option(element['station'].name,key);
		select_box.options[select_box.options.length] = option;
		}*/
		for (i = 0; i < stations.length; i++) {
			if (master_routes[master_route_id].reverse) {
				my_i = stations.length-i-1;
			} else {
				my_i = i;
			}
			if (stations[my_i]!==undefined) {
				element = stations[my_i];
				option = new Option(element['station'].name,element['station'].id);
				select_box.options[select_box.options.length] = option;
				if (pre_selected_station_id==-1) {
					pre_selected_station_id = element['station'].id;
				}
				if (pre_selected_station_id==element['station'].id) {
					pre_selected_index = select_box.options.length-1;
				}
			}
		}
		if (pre_selected_index!==undefined) {
			select_box.selectedIndex = pre_selected_index;
		}
	}
	fill_select_stop(pre_selected_station_id,'route_id','stoppested_id');
}

function fill_select_stop(start_station_id,master_route_select_box_id,select_id,pre_selected_station_id) {
	pre_selected_station_id = (pre_selected_station_id===undefined) ? -1 : pre_selected_station_id;
	master_route_select_box = document.getElementById(master_route_select_box_id);
	master_route_id = master_route_select_box.value;

	if (master_route_id>0) {
		stations = master_routes[master_route_id].get_stop_stations(start_station_id);
		select_box = document.getElementById(select_id);
		select_box.options.length = 0;

		for (i = 0; i < stations.length; i++) {
			if (master_routes[master_route_id].reverse) {
				my_i = stations.length-i-1;
			} else {
				my_i = i;
			}

			if (stations[my_i]!==undefined) {
				element = stations[my_i];
				option = new Option(element.name,element.id);
				select_box.options[select_box.options.length] = option;
				if (pre_selected_station_id==-1) {
					pre_selected_station_id = element.id;
				}
				if (pre_selected_station_id==element.id) {
					pre_selected_index = select_box.options.length-1;
				}
			}
		}
		if (pre_selected_index!==undefined) {
			select_box.selectedIndex = pre_selected_index;
		}
	}
	
	fill_hidden_round_trip('startsted_id',pre_selected_station_id,'round_trip_valid');
}

function fill_hidden_round_trip(start_select_id,stop_station_id,round_trip_hidden_id) {
	start_select_box = document.getElementById(start_select_id);
	start_station_id = start_select_box.value;

	round_trip_hidden = document.getElementById(round_trip_hidden_id);
	round_trip_hidden.value = 0;

	if (check_round_trip(start_station_id,stop_station_id)) {
		round_trip_hidden.value = 1;
	}
}

function check_round_trip(start_station_id,stop_station_id) {
	if (stop_station_id!==undefined && start_station_id!==undefined) {
		for (key in master_routes)   {
			if (master_routes[key].is_valid_trip(stop_station_id,start_station_id)) {
				return true;
			}
		}
	}
	return false;
}

function station(id,name) {
	this.id = id;
	this.name = name;
}

function route(id,name) {
	this.name = name;
	this.id = id;
	this.start_stations = new Array();
	this.stop_stations = new Array();

	this.add_station = function(type,id,name) {
		switch(type) {
			case 'start':
			this.start_stations.push(new station(id,name));
			break;
			case 'stop':
			this.stop_stations.push(new station(id,name));
			break;
		}
	}

	this.get_stations = function(type) {
		switch(type) {
			case 'start':
			return this.start_stations;
			break;
			case 'stop':
			return this.stop_stations;
			break;
		}
	}
}





function master_route(id,name,reverse) {
	this.name = name;
	this.id = id;
	this.reverse = reverse;
	this.routes = new Array();
	this.start_stations = new Array();

	this.add_route = function(tmp_route) {
		this.routes.push(tmp_route);
		route_start_stations = tmp_route.get_stations('start');
		route_stop_stations = tmp_route.get_stations('stop');

		for (i=0;i<route_start_stations.length;i++) {
			if (this.start_stations[route_start_stations[i].id]===undefined) {
				this.start_stations[route_start_stations[i].id] = new Array();
				this.start_stations[route_start_stations[i].id]['station'] = route_start_stations[i];
				this.start_stations[route_start_stations[i].id]['stop_stations'] = new Array();
			}
			for (j=0;j<route_stop_stations.length;j++) {
				tmp_station = route_stop_stations[j];
				this.start_stations[route_start_stations[i].id]['stop_stations'][tmp_station.id] = tmp_station;
			}
		}
	}
	this.get_stop_stations = function(start_station_id) {
		return this.start_stations[start_station_id]['stop_stations'];
	}
	this.get_stations = function(type) {
		switch(type) {
			case 'start':
			return this.start_stations;
			break;
			case 'stop':
			return this.stop_stations;
			break;
		}
	}
	this.is_valid_trip = function(start_station_id,stop_station_id) {
		if (this.start_stations[start_station_id]!==undefined) {
			if (this.start_stations[start_station_id]['stop_stations'][stop_station_id]!==undefined) {
				return true;
			}
		}
		return false;
	}
}


/**
* Function : dump()
* Arguments: The data - array,hash(associative array),object
*    The level - OPTIONAL
* Returns  : The textual representation of the array.
* This function was inspired by the print_r function of PHP.
* This will accept some data as the argument and return a
* text that will be a more readable version of the
* array/hash/object that is given.
*/
function dump(arr,level,max_level) {
	var dumped_text = "";
	if(!level) level = 0;

	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";

	if(typeof(arr) == 'object') { //Array/Hashes/Objects
		for(var item in arr) {
			var value = arr[item];

			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				if (level<=max_level) {
					dumped_text += dump(value,level+1,max_level);
				}
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}
