// Page is loaded, fill the comboboxes and set up their events
function onloadpage() {
    // Fill comboboxes with airports
    document.getElementById("departureAirport").parentNode.innerHTML = fillDestList("departureAirport");
    document.getElementById("arrivalAirport").parentNode.innerHTML = fillDestList("arrivalAirport");

    // need to set the selected cities again since the comboboxes were recreated
    if("$departureAirport"=="")
        {
        setCity("departureAirport", "xxx");
        }
    else
        {
    setCity("departureAirport", "$departureAirport");
        }
    if("$arrivalAirport"=="")
        {
        setCity("arrivalAirport", "xxx");
        }
    else
        {
    setCity("arrivalAirport", "$arrivalAirport");
        }
    // set the period
    if("$period"=="")
        {
        document.getElementById("period").value = 0;
        }
    else
        {
        document.getElementById("period").value = "$period";
        }
}

// Resetten van het form
function resetTimeTableForm()
    {
    setCity("departureAirport", "xxx");
    setCity("arrivalAirport", "xxx");
    document.getElementById("period").value = 0;
    document.forms.timeTableSearch.submit();
    }
function changeTimeTableForm(departureAirport,arrivalAirport)
    {
    setCity("departureAirport", departureAirport);
    setCity("arrivalAirport", arrivalAirport);
    document.forms.timeTableSearch.submit();
    }
// Opvullen combolijsten met bestemmingen
// args objId: id van de combobox die moet worden gevuld
// returns: string containing the html for the new combobox
function fillDestList(objId) {
    // We bouwen een nieuwe combobox
    var vDestList = "<select name='" + objId + "' id='" + objId + "' class='selInpSearchCity' onChange='onAirportSelChanged(this.id, this.value)'>";

    // Add an option for each value in the connection grid
    for(var i = 0; i < vCountDest; i++){
        vDestList = vDestList + "<option value='" + aDest[i][0] + "'>" + aDest[i][1] + "</option>";
    }

    vDestList = vDestList + "</select>";

    return vDestList;
}

// Called when user selects an airport from either combo
// arg objId: Id of the combo whose value has changed
// arg choiceId: The new value (question: doesn't this event come in before the value has changed?
function onAirportSelChanged(objId, choiceId){
    switch (objId) {
        case "departureAirport":
            if(choiceId!="xxx"){
                setCity(objId, choiceId);
                f1To = document.getElementById("arrivalAirport").options[document.getElementById("arrivalAirport").options.selectedIndex].value;
                document.getElementById("tdInputTo").innerHTML = updateDestList("arrivalAirport", choiceId);
                setCity("arrivalAirport", f1To);
            }
            else {
                setCity(objId, choiceId);
                f1To = document.getElementById("arrivalAirport").options[document.getElementById("arrivalAirport").options.selectedIndex].value;
                document.getElementById("tdInputTo").innerHTML = fillDestList("arrivalAirport");
                setCity("arrivalAirport", f1To);
            }
        break;
        case "arrivalAirport":
            if(choiceId!="xxx"){
                setCity(objId, choiceId);
                f1From = document.getElementById("departureAirport").options[document.getElementById("departureAirport").options.selectedIndex].value;
                document.getElementById("tdInputFrom").innerHTML = updateDestList("departureAirport", choiceId);
                setCity("departureAirport", f1From);
            }
            else {
                setCity(objId, choiceId);
                f1From = document.getElementById("departureAirport").options[document.getElementById("departureAirport").options.selectedIndex].value;
                document.getElementById("tdInputFrom").innerHTML = fillDestList("departureAirport");
                setCity("departureAirport", f1From);
            }
            break;
    }
}

function setCity(objId, choiceId){
    document.getElementById(objId).value = choiceId;
}

function fTakeSelObj(objId,choiceId){
/* donothing, than preventing a javascript error dunmp */
 }

 /* Update combolijsten met bestemmingen */
function updateDestList(objId, choiceId) {
    var vNewDestList = "<select name='" + objId + "' id='" + objId + "' class='selInpSearchCity' onChange='fTakeSelObj(this.id,this.value)'>";
    vNewDestList = vNewDestList + "<option value='"+aDest[0][0]+"'>"+aDest[0][1]+"</option>";
    var vCountFlichtSchedule = aFlichtSchedule[choiceId].length;

    for(var j=0;j<vCountFlichtSchedule;j++){
        if(aFlichtSchedule[choiceId][j]==1){
            vNewDestList = vNewDestList + "<option value='" + aDest[j][0] + "'>" + aDest[j][1] + "</option>";
        }
    }

    vNewDestList = vNewDestList + "</select>";

    return vNewDestList;
}
function getAirportName(airportCode)
{
    for(i=0;i<aDest.length;i++)
    {
        if(aDest[i][0]==airportCode)
        {
            var tmp=aDest[i][1].split(" - ");
            if (tmp.length == 3) {
                //special case...someone has placed extra '-' in the orginal name :/
                return tmp[0] + " - " + tmp[1];
            } else {
                return tmp[0];
            }
            //why break if we return ??? f*ck*ng crappy code..thank god he's fired...
            //didn't remove it just to have a laugh once in a while and our children can see how well we did in the old days :)
            break;
        }
    }
}
function formatDepDate(dateString)
{
    dateString=dateString.split("-");
    return dateString[0]+"/"+dateString[1]+"/20"+dateString[2];
}
