//Unavailable routes that should be removed from default routes
var unavailableRoutes = new Array();
//adding existing unavailable routes
unavailableRoutes = unavailableRoutes.concat(['CPTJNB', 'JNBCPT']);
//Singapore
unavailableRoutes = unavailableRoutes.concat(['SINADL', 'SINBNE', 'SINMEL', 'SINPER', 'SINSYD', 'ADLSIN', 'BNESIN', 'MELSIN', 'PERSIN', 'SYDSIN']);
//Virgin Blue
unavailableRoutes = unavailableRoutes.concat(['SYDADL', 'SYDBNE', 'SYDCBR', 'SYDMEL', 'SYDOOL', 'ADLSYD', 'BNESYD', 'CBRSYD', 'MELSYD', 'OOLSYD']);
//British Midland
unavailableRoutes = unavailableRoutes.concat(['LONAMS', 'LONLHRAMS', 'LONDUB', 'LONLHRDUB', 'LONBRU', 'LONLHRBRU','LONPMI', 'LONLHRPMI', 'LONNAP', 'LONLHRNAP', 'LONVCE', 'LONLHRVCE', 'LONHAJ', 'LONLHRHAJ', 'AMSLON', 'AMSLONLHR', 'DUBLON', 'DUBLONLHR', 'BRULON', 'BRULONLHR', 'PMILON', 'PMILONLHR', 'NAPLON', 'NAPLONLHR', 'VCELON', 'VCELONLHR','HAJLON', 'HAJLONLHR']);
/*
 * Removes the unavailable routes from default routes.
 */
function removeUnavailableRoutes() {
    for (var i = 0; i < unavailableRoutes.length; i++) {
        var unavailableRoute = unavailableRoutes[i];
        for (var j = 0; j < ourRoutes.length; j++) {
            var route = ourRoutes[j];
            if (route.departCity + route.arriveCity == unavailableRoute) {
                //remove the current route
                ourRoutes.splice(j, 1);
                break;
            }
        }
    }
}

// this is the Main function called on entry to the page
function setUp(obDepartCity, obArriveCity, obCabin, ibDepartCity, ibArriveCity, ibCabin) {
    removeUnavailableRoutes();
    //check whether the Outbound and Inbound departure cities are in the dropdown list or not
    //if one of them are not available, set default departure cities as Gateway City and LON
    if (checkDepartCities(obDepartCity, ibDepartCity) || !isDepartCity(obDepartCity)) {
        obDepartCity = gatewayCityCode;
        defaultArrivalCity = 'LON';
        if (gatewayCityCode == 'LON') {//if Gateway Country is UK, set arrival city as NYC
            defaultArrivalCity = 'NYC';
        }
        obArriveCity = defaultArrivalCity;
        ibDepartCity = defaultArrivalCity;
        ibArriveCity = gatewayCityCode;
    }
    //If gateway city is not in the list, set defult to LON and NYC
    if (checkDepartCities(obDepartCity, ibDepartCity)) {
        obDepartCity = 'LON';
        obArriveCity = 'NYC';
        ibDepartCity = 'NYC';
        ibArriveCity = 'LON';
    }

    // do the outbound stuff
    doOutboundDeparture(obDepartCity);
    var obCabinList = doOutboundArrival(obDepartCity, obArriveCity);
    doOutboundCabin(obCabin);
}

// Populates the outbound departure city drop down
// Loops through all the cities, just removing the canDepart() = false ones
function doOutboundDeparture(departCity) {

    var selected = 0;
    var optionIndex = 0;

    document.main.departure.options.length = 0;
    for (var i = 0; i < cityNames.length; i++) {
        var city = cityNames[i];
        if (city.canDepart) {
            document.main.departure.options.length++;
            document.main.departure.options[optionIndex++] = new Option(city.name, city.code);
        }
        if (city.code == departCity) selected = optionIndex - 1;
    }
    // set the selected one
    document.main.departure.options[selected].selected = true;
}

// Populates the outbound arrival city drop down
function doOutboundArrival(departCity, arrivalCity) {
    // clear down the select box
    clearOptions(document.main.arrival);

    var optionIndex = 0;
    var cabinList = 0;
    var selected = 0;
    for (var i = 0; i < ourRoutes.length; i++) {
        var route = ourRoutes[i];
        // if this route departs from our departcity then add it
        if (route.departCity == departCity) {
            var cityName = getCityName(route.arriveCity)
            document.main.arrival.options[optionIndex++] = new Option(cityName, route.arriveCity);
            // check for the default and then get the cabin list from here
            if (route.arriveCity == arrivalCity) {
                selected = optionIndex - 1;
            }
        }
    }
    // set the selected one
    document.main.arrival.options[selected].selected = true;
}

// populates the outbound cabin drop down
function doOutboundCabin(cabinId) {

    var cabinList = getCabinList(true);

    // clear down the select box
    clearOptions(document.main.classType);

    var selected = 0;
    // get the array of cabins
    var cabins = cabinGroups[cabinList];
    // loop through these populating the class drop down
    for (var i = 0; i < cabins.length; i++){
        var cabin = cabins[i];
        document.main.classType.options[i] = new Option(cabin.myFriendlyName, cabin.myId);
        if (cabin.myId == cabinId) selected = i;
    }
    // now set the default
    document.main.classType.options[selected].selected = true;
}

// gets the city name from the city code
function getCityName(cityCode) {
    var city;
    // loop through the cities till we find this one
    for (var i = 0; i < cityNames.length; i++) {
        city = cityNames[i];
        if (city.code == cityCode) break;
    }
    return city.name;
}

// returns the route object with this departCity and arriveCity
function getRoute(departCity, arriveCity) {
    var route;

    // this will occur on first entry into the main booking page as the inbound cities are not set
    // when this method is first called
    if (departCity == '' || arriveCity == '') {
        route = -1;
    }
    else {
        for (var i = 0; i < ourRoutes.length; i++) {
            route = ourRoutes[i];
            if (route.departCity == departCity && route.arriveCity == arriveCity) break;
        }
    }
    return route;
}

// gets the cabin list number from the selected departure and arrival options
function getCabinList(outbound) {
    // get the route
    var route;

    if (outbound) {
        route = getRoute(document.main.departure.options[document.main.departure.selectedIndex].value,
                document.main.arrival.options[document.main.arrival.selectedIndex].value);
    }
    else {
        route = getRoute(document.main.departureReturn.options[document.main.departureReturn.selectedIndex].value,
                document.main.arrivalReturn.options[document.main.arrivalReturn.selectedIndex].value);
    }

    var cabinList;

    if (route != -1) {
        cabinList = route.cabinList;
    }
    else {
        cabinList = -1;
    }

    // need to return the cabin list number
    return cabinList;
}

// --------DROP DOWN EVENT HANDLERS-------

// Outbound Departure drop down changed
function obDepChanged()
{
    // do outbound arrival cities
    var departCity = document.main.departure.options[document.main.departure.selectedIndex].value;
    doOutboundArrival(departCity, getDefaultArrival(departCity));

    // do outbound classes, inbound cities and classes if necessary
    obArrivalChanged();
}

// Outbound arrival drop down changed
function obArrivalChanged() {
    // do outbound classes
    doOutboundCabin(document.main.classType.options[document.main.classType.selectedIndex].value);
}

// gets the default arrival city for this departure city
function getDefaultArrival(departCity) {
    var arrivalCity;
    if (departCity.substring(0, 3) == 'LON') {
        arrivalCity = 'NYC';
    }
    else if (departCity == 'MAN') {
        arrivalCity = 'MCO';
    }
    else {
        arrivalCity = 'LON';
    }
    return arrivalCity;
}

// Clears the select box
function clearOptions(theSelect) {
    if (theSelect.type == 'select-one')
        while (theSelect.length > 0)
            theSelect.options[0] = null;
}

// -------------------------- JS OBJECTS --------------------------------

// represents a city for the drop downs
function City(cd, nm, cdp)
{
    this.code = cd;
    this.name = nm;
    this.canDepart = cdp;
}

// The CabinDetails class represents info on a cabin such as cabin ID 
// (for submission to the backend) and a friendly name for display.
function CabinDetails(id, fn, flx, nei, sk)
{
    this.myId = id;
    this.myFriendlyName = fn;
    this.flexible = flx;
    this.neither = nei;
    this.sortKey = sk;
}
// The Route class represents an individiual route
function Route(dc, ac, cl, ms)
{
    this.departCity = dc;
    this.arriveCity = ac;
    this.cabinList = cl;
    this.multiSector = ms;
}
//Function to check whether the cities are in the allowed routes
function checkDepartCities(outBoundDepart, inBoundDepart) {

    if (!checkDepartCity(outBoundDepart)) {
        return true;
    }

    if (!checkDepartCity(inBoundDepart)) {
        return true;
    }
    return false;
}
//function to check city is in the departures list
function checkDepartCity(departureCityName) {

    for (var i = 0; i < ourRoutes.length; i++)
    {
        var route = ourRoutes[i];
        // if this route departs from our departcity then true
        if (route.departCity == departureCityName)
        {
            return true;
        }
    }
    return false;
}
//function to return whether city is in the list of can depart cities
function isDepartCity(cityToDepart) {
    for (var i = 0; i < cityNames.length; i++)
    {
        var city = cityNames[i];
        if (city.code == cityToDepart) {
            if (city.canDepart) return true;
        }
    }
    return false;
}



