var destinations_map_ts = {"destinations": []};
var destinations_map_hash_ts = {};
var destinations_pairs_map_ts = {};
var imp_city_ids = [];
var first_origin = destinations_map_ts.destinations[0][1];
function load_origins_ts(destinations_map_ts, selected_origin){
	start_length = 0;
	destinations = destinations_map_ts.destinations;
	field_obj = document.getElementById("searchbus_from");	
	org_ids = new Array();
	for(i=0;i<destinations.length;i++){
	  org_ids.push(destinations[i][1]);
	}
	field_obj = appendImpCities(field_obj, org_ids);
	j = field_obj.options.length;
	for(i=j; i<(destinations.length+j); i++){
		default_selected = false;
		field_obj.options[start_length+i] = new Option(destinations[i-j][0], destinations[i-j][1], false, false);
		if (destinations[i-j][1] == selected_origin){
			field_obj.options[start_length+i].selected = true;
			}
			else {
			field_obj.options[start_length+i].selected = false;
		}
	}
	count = 0;
	// change_destinations_by_origin(120) //TODO: Need to put this by thte user based. Now it is by Bangalore.
}
function appendImpCities(field_obj, city_ids){
    j = 0;
	for(i=0; i<city_ids.length; i++){
		if (imp_city_ids.include(city_ids[i])){
		   field_obj.options[j] = new Option(destinations_map_hash_ts[city_ids[i]], city_ids[i], false, false);
		   j += 1;
		}
	}
	if (j>0){
		field_obj.options[j] = new Option('-----------', '', false, false);
	}
	return field_obj;
}
function change_destinations_by_origin_ts(origin, selected_destination){
	if ((document.getElementById("searchbus_from").value == '0') || (origin == "")) {
		alert("Invalid Origin!");
		return;
	}
	field_obj = document.getElementById("searchbus_to");
	field_obj.options.length = 0;
	start_length = 0;
    destinations_arr_ts = destinations_pairs_map_ts[origin];
    if (destinations_arr_ts == undefined){
      origin = first_origin;
      destinations_arr_ts = destinations_pairs_map_ts[origin]; 
    } 
    destinations_arr_ts = getSortedCitys(destinations_arr_ts);
    field_obj = appendImpCities(field_obj, destinations_arr_ts);
    j = field_obj.options.length; 
    for (i = j; i < (destinations_arr_ts.length +j);  i++) {
    chk_val = i - j;
    if (destinations_arr_ts[chk_val].include("-")) {
    city	= destinations_arr_ts[chk_val].split("-")[0];
    city_name = destinations_arr_ts[chk_val].split("-")[1];
			}
			else {
				city = destinations_arr_ts[chk_val];
				city_name = destinations_map_hash_ts[city];
			}
			field_obj.options[start_length + i] = new Option(city_name, city, false, false);
			if (city == selected_destination) {
					field_obj.options[start_length + i].selected = true;
			}
		}
		//sort_select_options("searchbus_to", selected_destination);
	}
	
	
 function sort_select_options(select_id, selected_destination) {
	var lb = document.getElementById(select_id);
	arrTexts = new Array();
	arrTextsOrig = new Array();
	arrVals = new Array();

	for(i=0; i<lb.length; i++)  {
	  arrTexts[i] = lb.options[i].text;
	  arrTextsOrig[i] = lb.options[i].text;
	  arrVals[i] = lb.options[i].value;
	}

	arrTexts.sort();
	
	for(i=0; i<lb.length; i++)  {
	  lb.options[i].text = arrTexts[i];
	  lb.options[i].value = arrVals[arrTextsOrig.indexOf(arrTexts[i])];
	  if (lb.options[i].value == selected_destination)
	  	lb.options[i].selected = true;
	}
} 	

function load_origin_destinations_str(destinations_map_ts, destinations_pairs_map_ts, field_name){
	var org_dest_str = "";
	dests = destinations_map_ts.destinations;
	for (i = 0; i < dests.length;  i++) {
		dests_arr = destinations_pairs_map_ts[dests[i][1]];
		if (dests_arr != "undefined"){
			for (j = 0; j < dests_arr.length; j++) {
				org_dest_str += "<div class='size12 margintop5'>" + dests[i][0] + ' &raquo; ' + destinations_map_hash_ts[dests_arr[j]] + '</div>';
			}
		}
	}
	$(field_name).innerHTML = org_dest_str;
}
function getSortedCitys(destinations_arr_ts){
	arrTexts = new Array();
	arrTextsOrig = new Array();
	arrVals = new Array();
	for(i=0;i<destinations_arr_ts.length;i++){
	  arrTexts[i] = destinations_map_hash_ts[destinations_arr_ts[i]];
	  arrTextsOrig[i] = destinations_map_hash_ts[destinations_arr_ts[i]];
	  arrVals[i] = destinations_arr_ts[i];
	}
	arrTexts.sort();
	sort_ids = new Array();
	for(i=0;i<arrTexts.length;i++){
	  sort_ids.push(arrVals[arrTextsOrig.indexOf(arrTexts[i])]);
	}
	return sort_ids;
}

var all_routes_arr = [];
function load_all_routes_arr(field_name){
	var org_dest_str = "";
	for (i = 0; i < all_routes_arr.length;  i++) {
		origin = destinations_map_hash_ts[all_routes_arr[i][0]];
		destination = destinations_map_hash_ts[all_routes_arr[i][1]];
		org_dest_str += "<div class='all_routes_content'><span class='size14px'>" + origin + ' &raquo; ' + destination + "<br></span><small>D: " + all_routes_arr[i][2] + " / " + all_routes_arr[i][3]  + "</small>" + '</div>';
	}
	$(field_name).innerHTML = org_dest_str;
}

