﻿
var fromCityCode = "";
var toCityCode = "";

function setUpDatePickerImageEvents(imgId, txtDateId) {
    $("#" + imgId).click(function() {
        $("#" + txtDateId).datepicker("show");
    });
}

function setupDestinationAutocomplete(txtId, onFindFunction) {


    $("#" + txtId).autocomplete(
            {
                url: "/Home/SearchDestinations",
                dataType: "json",

                delay: 10,
                minChars: 3,
                matchSubset: 1,
                matchContains: 1,
                cacheLength: 10,
                autoFill: true,
                parse: function(data) {
                    return $.map(data, function(row) {

                        return {

                            data: row,
                            value: row.Code,
                            result: row.Name
                        }
                    });
                },
                formatItem: function(item) {

                    return item.Name;
                }
            });
    $("#" + txtId).autocomplete("result", onFindFunction);
}

function setupPassengers(divContainerId, templateId) {
    $(divContainerId).find(".adults").change(function() {

        var childs = $(divContainerId).find(".childs");

        childs.attr("length", "0");

        var adults = parseInt($(this).val());
        var totalChildren = 10 - adults;

        for (i = 0; i < totalChildren; i++) {
            var option = document.createElement("option");
            option.appendChild(document.createTextNode(i));
            childs.append(option);
        }
        $(this).closest(divContainerId).find(".divChildAges").hide("slow");
        $(this).closest(divContainerId).find(".divChildAges").html("");
    });
    $(divContainerId).find(".childs").change(function() {
        var children = parseInt($(this).val());
        var childAgeList = new Array();
        for (i = 0; i < children; i++)
            childAgeList[i] = i + 1;

        if (children == 0)
            $(this).closest(divContainerId).find(".divChildAges").hide("slow");
        else {
            $(this).closest(divContainerId).find(".divChildAges").show("slow");

            var updateTarget = $(this).closest(divContainerId).find(".divChildAges");
            updateTarget.slideUp(function() {
                updateTarget.setTemplateElement(templateId);
                updateTarget.processTemplate(childAgeList);

                updateTarget.slideDown();
            });

        }
    });
}

function setupFlightDatePicker(id, correspondingId) {
    $(id).datepicker({
        numberOfMonths: 2,
        changeMonth: true,
        changeYear: true,
        dateFormat: 'yy-mm-dd',
        showButtonPanel: true,
        onSelect: function(dateFrom) {
            $(id).val(dateFrom);
            if (correspondingId != null) {
                var fromDate = new Date($(id).val().replace("-", "/").replace("-", "/"));
                var toDate = new Date($(correspondingId).val().replace("-", "/").replace("-", "/"));
                if (fromDate > toDate) {
                    $(correspondingId).val($(id).val());
                }
            }
        },
        yearRange: '-0:+1',
        firstDay: 1
    });
    $(id).datepicker('option', $.extend({ showMonthAfterYear: false }, $.datepicker.regional['sv']));

}

function syncInputValues(fromId, toId) {
    $("#" + toId).val($("#" + fromId).val());
}
