﻿
// Tillfälligt bortkommenterat 2011-12-09
/*

function enableChildrensAges(count) {
    if (count == 0) {
        $("ChildrensAges").hide();
    } else {
        $("ChildrensAges").show();
    }
    $$("#ChildrensAges select").each(function(item, index) {

        if (count > index) {
            item.enable();
            item.show();
        } else {
            item.disable();
            item.hide();
        }

    });
}
document.observe('dom:loaded', function() {
var maxNumberOfChildren = 8;

    $('City1').focus();
    
    $$('input.City').each(function(item) {
        item.observe('focus', function(event) {
            Event.element(event).activate();
        });
        //new Autocompleter.Local(item.id, "airports", airports, {});
        new Ajax.Autocompleter(item.id, "airports", "/tinyMCE/Data/Destinations/", {});
    });

    $("Date1").value = new Date().addDays(7).clearTime().toString('yyyy-MM-dd');
    $("Date2").value = new Date().addDays(14).clearTime().toString('yyyy-MM-dd');

    $("AdultsContainer").insert(new Element("select", { id: "Adults", name: "Adults" }));
    $("ChildrenContainer").insert(new Element("select", { id: "Children", name: "Children" }));
    for (var i = 0; i <= maxNumberOfChildren; i++) {
        if (i > 0) {
            $("Adults").insert(new Element("option").update(i));
        }
        $("Children").insert(new Element("option").update(i));
    }

    $("ChildrensAges").hide();
    for (var i = 1; i <= maxNumberOfChildren; i++) {
        var sel = $("ChildrensAges").insert(new Element("select", { name: "ChildrensAges" }));
        //        for (var y = 0; y <= 11; y++) {
        //            sel.insert(new Element("option").update(y));

        //        };
    }
    $$("#ChildrensAges select").each(function(item, index) {
        for (var y = 0; y <= 11; y++) {
            item.insert(new Element("option").update(y));
        };
    });

    enableChildrensAges($("Children").getValue());

    $("Children").observe("change", function() {
        enableChildrensAges(this.getValue());
    });

    $("OneWaySelector").observe("click", function(event) {
        $("Date2").setValue("");
    });


    var a = new Impleo.Calendar({
        container: "Calendar1",
        inline: true,
        source: "Date1",
        disabledDates: [new Impleo.Calendar.DateRange(0, Date.today().addDays(2))],
        onDateSelection: function(calendar) {
            b.disabledDates.clear();
            b.addDisabledDateRange(new Impleo.Calendar.DateRange(0, new Date(calendar.getSelectedDate()).addDays(-1)));
            if (calendar.getSelectedDate().compareTo(b.getSelectedDate()) != -1) {
                Impleo.Calendar.browseTo(b, calendar.getVisibleDate());
            }
        }
    });
    var b = new Impleo.Calendar({
        container: "Calendar2",
        inline: true,
        source: "Date2",
        disabledDates: [new Impleo.Calendar.DateRange(0, Date.today().addDays(2))],
        onDateSelection: function(calendar) {
        },
        onDisable: function() {
            this.source.disable();
        },
        onEnable: function() {
            this.source.enable();
        }
    });

    $("Search").observe("click", function() {
        var valid = true;
        var qs = $H();
        if (extractIATA($("City1").getValue()).length == 3) {
            qs.set("City1", extractIATA($("City1").getValue()));
        } else {
            Effect.Pulsate("City1");
            valid = false;
        }
        if (extractIATA($("City2").getValue()).length == 3) {
            qs.set("City2", extractIATA($("City2").getValue()));
        } else {
            valid = false;
            Effect.Pulsate("City2");
        }
        if (Date.parse($("Date1").getValue())) {
            qs.set("Date1", Date.parse($("Date1").getValue()).toString("d/M/yyyy"));
        } else {
            valid = false;
            Effect.Pulsate("Date1");
        }
        if (Date.parse($("Date2").getValue())) {
            qs.set("Date2", Date.parse($("Date2").getValue()).toString("d/M/yyyy"));
        }
        qs.set("Adults", $("Adults").getValue());
        if (getChildrensAges() > "") {
            qs.set("Non_Adult_Age", getChildrensAges());
        }
        if ($("NonStop").checked) {
            qs.set("Only_Non_Stop_Flights", "1");
        }
        qs.set("Ref", window.location.href);
        if (valid) {
            window.location = "/SpecialAds/Flygtorget/Resor/SearchTransfer/?TargetDomain=flygtorget.se&" + qs.toQueryString()
        }
    });
});
function extractIATA(input) {
    if (input.lastIndexOf("(") != -1 && input.lastIndexOf(")") != -1) {
        return input.substring(input.lastIndexOf("(") + 1, input.lastIndexOf(")"))
    } else {
        return input
    }
}
function getChildrensAges() {
    var value = "";
    $$("#ChildrensAges select").each(function(item, index) {
        if (item.disabled != true) {
            value += item.getValue() + "|";
        }
    });

    return value.substr(0, value.length - 1);
}
*/
