/* author: Christian Bolz */
/* Creation date: 28.10.2003 */


/* Funtionen um die einzelnen Listboxen neu aufzubauen. Wenn eine Listbox geändert wurde,
   werden jeweils die unter ihr liegenden Listboxen neu aufgebaut.  Das wird von buildAll()
   gesteuert.
   
   Das geschieht durch löschen der alten Listboxeinträge (länge auf 0 setzen) und erneutem 
   hinzufügen aller  Einträge. Beim Hinzufugen wird das Array ausgelesen das alle Kombinationen
   enthalt. Z.B:
   
   ar[country]   ar[region]     city          airport   courstype
   
   [1]spain         costa brava    barcelona     girona    beginner
   [2]spain         madrid         madrid        madrid    advanced
    
   Natürlich werden dann nur die Einträge hinzugefügt die auch zu der Selektion
   in den oberen Listboxen passen.
   
   Variablen:
   
   changed: welche Auswahlbox wurde verandert (z.B country)
   newValue: der neue Wert, den die Auswahlbox hat
   
*/

function buildTempArray(changed, newValue, toChange, tempArray) {
    for (var i=0; i<ar[toChange].length;i++){
        if ((ar[changed][i]==newValue) || (newValue=="")){
            tempArray.push(ar[toChange][i]);
        }
    }
    tempArray.sort();
}

function buildRegion(changed, newValue) {
    tempArray = new Array();
    if (document.selector.region.selectedIndex != 0) {
        var temp_selected = document.selector.region.options[document.selector.region.selectedIndex].value;
    }
    else var temp_selected = 0;
    buildTempArray(changed, newValue, "region", tempArray);
    var temptext="";
    document.selector.region.length=0;
    neueOption = new Option("Select Region", "");
    document.selector.region.add(neueOption);
    for (var i=0; i<tempArray.length; i++) {
        // im sortierten tempArray steht z.B. 5 mal Spanien - das wird hiermit dedupliziert:
        if (temptext != tempArray[i]) {
            temptext = tempArray[i];
            neueOption = new Option(temptext, temptext);
            document.selector.region.add(neueOption);
        }
    }
}

function buildCity(changed, newValue) {
    tempArray = new Array();
    if (document.selector.nextcity.selectedIndex != 0) {
        var temp_selected = document.selector.nextcity.options[document.selector.nextcity.selectedIndex].value;
    }
    else var temp_selected = 0;
    buildTempArray(changed, newValue, "city", tempArray);
    var temptext="";
    document.selector.nextcity.length=0;
    neueOption = new Option("Select City", "");
    document.selector.nextcity.add(neueOption);
    for (var i=0; i<tempArray.length; i++) {
        if (temptext != tempArray[i]) {
            temptext = tempArray[i];
            neueOption = new Option(temptext, temptext);
            document.selector.nextcity.add(neueOption);
        }
    }
}
function buildAirport(changed, newValue) {
    tempArray = new Array();
    if (document.selector.airport.selectedIndex != 0) {
        var temp_selected = document.selector.airport.options[document.selector.airport.selectedIndex].value;
    }
    else var temp_selected = 0;
    buildTempArray(changed, newValue, "airport", tempArray);
    var temptext="";
    document.selector.airport.length=0;
    neueOption = new Option("Select Airport", "");
    document.selector.airport.add(neueOption);
    for (var i=0; i<tempArray.length; i++) {
        if (temptext != tempArray[i]) {
            temptext = tempArray[i];
            neueOption = new Option(temptext, temptext);
            document.selector.airport.add(neueOption);
        }
    }
}
function buildType(changed, newValue) {
    tempArray = new Array();
    if (document.selector.l_type.selectedIndex != 0) {
        var temp_selected = document.selector.l_type.options[document.selector.l_type.selectedIndex].value;
    }
    else var temp_selected = 0;
    buildTempArray(changed, newValue, "type", tempArray);
    var temptext="";
    document.selector.l_type.length=0;
    neueOption = new Option("Select Course Type", "");
    document.selector.l_type.add(neueOption);
    for (var i=0; i<tempArray.length; i++) {
        if (temptext != tempArray[i]) {
            temptext = tempArray[i];
            neueOption = new Option(temptext, temptext);
            document.selector.l_type.add(neueOption);
        }
    }
}


function buildAll(changed) {
    if (changed == "country") {
        // auslesen des aktuellen Wertes der geaenderten Listbox:
        newValue=document.selector.country.options[document.selector.country.selectedIndex].value
        // Neuaufbau der anderen Listboxen:
        buildRegion(changed, newValue);
        buildCity(changed, newValue);
        buildAirport(changed, newValue);
        buildType(changed, newValue);
    }
    else if (changed == "region") {
        newValue=document.selector.region.options[document.selector.region.selectedIndex].value
        buildCity(changed, newValue);
        buildAirport(changed, newValue);
        buildType(changed, newValue);
    }
    else if (changed == "city") {
        newValue=document.selector.nextcity.options[document.selector.nextcity.selectedIndex].value
        buildAirport(changed, newValue);
        buildType(changed, newValue);
    }
    else if (changed == "airport") {
        newValue=document.selector.airport.options[document.selector.airport.selectedIndex].value
        buildType(changed, newValue);
    }
}
