/** 
 *    RouteChooser is a handler for html select elements, containing
 *    from_airports and a list of to_airports where the to_airports
 *    depends on the from_airports.
 *
 *    It works something like this.
 *    <pre>
 *    toAirports = new Array();
 *    toAirports[0] = new Airport(1, "Copenhagen", "CPH");
 *    toAirports[1] = new Airport(2, "London", "LDN");
 *    toAirports[2] = new Airport(3, "New York", "NYK");
 *
 *    routes = new Array();        
 *    routes[0] = new Route(new Airport(4, "Sweden", "SEA"), toAirports);
 *    routes[1] = new Route(new Airport(5, "Norway", "NWA"), toAirports);
 *                    
 *    routeChooser = new RouteChooser(document.all.fromAirport, document.all.toAirport);
 *    routeChooser.setRoutes(routes);
 *    </pre>
 *
 *    @param cmbFrom      a HTML select element for the from airports
 *    @param cmbTo        a HTML select element for the to airports
 *
 *    @see Airport.js, Route.js
 *
 **/

function RouteChooser(cmbFrom, cmbTo) {
    this.cmbFrom = cmbFrom;
    this.cmbFrom.handler = this;

    this.cmbTo = cmbTo;
    this.cmbTo.handler = this;
    
    this.routes = new Array();

    this.cmbFrom.onchange = this.fromChanged;    
    this.cmbTo.onchange = this.toChanged;    
}


/** Sets the routes and refreshes the select elements.
 *
 *    @param an array of Route Objects
 * 
 */

 RouteChooser.prototype.setRoutes = function(aRoutes) {
    var route;
    var airport;

    this.routes = aRoutes;

    this.cmbFrom.options.length = 0;

    for (var a = 0; a < this.routes.length; a++) {
        route = this.routes[a];
        airport = route.getFromAirport();    
        this.cmbFrom.options[a] = new Option(airport.getName(), airport.getId(),  false, false);
        this.cmbFrom.options[a].route = route; 
    }

    this.cmbFrom.onchange();
}


/** When the user changes the from list, this method gets called
 *    it only works if it gets called from the html element itself
 */ 

 
RouteChooser.prototype.fromChanged = function() {    
    var airports;

    // TODO: remove hardcode
    var form = document.forms["quickbook"];
    if (form == null) {
        form = document.forms["booking"];
    }
    if (form == null) {
        form = document.forms["timetable"];
    }
    if (form == null) {
        form = document.forms["pricelist"];
    }
    var fromAirport = form.fromAirport;
    var toAirport = form.toAirport;

    if (fromAirport != null && toAirport != null) {
	    airports = fromAirport.options[fromAirport.selectedIndex].route.getToAirports();            
	    toAirport.options.length = 0;

	    for (var a = 0; a < airports.length; a++) {
	    	toAirport.options[a] = new Option(airports[a].getName(), airports[a].getId(), false, false);
	    }
    }

    //this.handler.onFromChanged();
    //this.handler.onToChanged();
}

RouteChooser.prototype.toChanged = function() {
    this.handler.onToChanged();    
}

RouteChooser.prototype.setSelectedCode = function(fromCode, toCode) {
    for (var a = 0; a < this.routes.length; a++) {
        if (this.routes[a].getFromAirport().getCode() == fromCode) {
            this.cmbFrom.selectedIndex = a;
            this.cmbFrom.onchange();
            break;
        }
    }

    var destinations = this.routes[this.cmbFrom.selectedIndex].getToAirports();
    for (var a = 0; a < destinations.length; a++) {
        if (destinations[a].getCode() == toCode) {
            this.cmbTo.selectedIndex = a;
            this.cmbTo.onchange();
            break;
        }
    }
}


RouteChooser.prototype.setSelectedName = function(fromName, toName) {
    var fromCode = "";
    var toCode = "";
    for (var i = 0; i < this.routes.length; i++) {
        if (this.routes[i].getFromAirport().getName() == fromName) {
            fromCode = this.routes[i].getFromAirport().getCode();
            toAirports = this.routes[i].getToAirports();
            for (var j = 0; j < toAirports.length; j++) {
                if (toAirports[j].getName() == toName) {
                    toCode = toAirports[j].getCode();
                    break;
                }
            }
            break;
        }
    }
    this.setSelectedCode(fromCode, toCode);
}


RouteChooser.prototype.setSelectedId = function(fromId, toId) {
    for (var a = 0; a < this.routes.length; a++) {
        if (this.routes[a].getFromAirport().getId() == fromId) {
            this.cmbFrom.selectedIndex = a;
            this.cmbFrom.onchange();
            break;
        }
    }

    var destinations = this.routes[this.cmbFrom.selectedIndex].getToAirports();
    for (var a = 0; a < destinations.length; a++) {
        if (destinations[a].getId() == toId) {
            this.cmbTo.selectedIndex = a;
            this.cmbTo.onchange();
            break;
        }
    }
}



/**   Gets itself ???
 *
 *    TODO: find out what i was thinking when creating this method
 */

RouteChooser.prototype.getRoute = function() {
    return this.cmbFrom.options[this.cmbFrom.selectedIndex].route;
}

RouteChooser.prototype.getSelectedDestinationAirport = function() {
    var airportIndex;
    var route;
    var airports;
    
    airportIndex = this.cmbTo.selectedIndex;
    route = this.getRoute();
    airports = route.getToAirports();

    return airports[airportIndex];
}

/*    
    var a = 0;

    for (a = 0; a < this.routes && this.routes[a] != route; a++);

    return a == this.routes.length ? null : this.routes[a];
*/

RouteChooser.prototype.onFromChanged = function() {
}

RouteChooser.prototype.onToChanged = function() {
}

