﻿var dates;
var currentInboundDate = null;
var currentOutboundDate = null;
var ddlDestinationsId = "";

$(document).ready(function () {

    ddlDestinationsId = $("#hd_destination").val();

    dates = $("#inbound, #outbound").datepicker
        ({
            changeMonth: true,
            numberOfMonths: 1,
            dateFormat: "dd/mm/yy",
            onSelect: function (selectedDate) {
                var option = this.id == "outbound" ? "minDate" : "maxDate";
                var instance = $(this).data("datepicker");
                var date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
                dates.not(this).datepicker("option", option, date);
            }
        });

    if ($("#return").attr("checked")) {
        ToggleReturn();
    }

    $("#ddlDestinations").change(function () {
        currentOutboundDate = null;
        currentInboundDate = null;        
    });
});


function Search() {

    var destination = $("#" + ddlDestinationsId + "").val();
    var departureDate = $("#outbound").val();
    $("#result").html(destination);
    DoOutbound(destination, departureDate);

    var inboundDate = $("#inbound").val();
    if ($("#return").attr("checked") && outbounded) {        
        DoInbound(destination, inboundDate);
    }
    else {
        $("#inbound-info").html("");
        $("#holder-inbound a").css("display", "none");
        $("#holder-inbound").css("display", "none");
    }
    return false;
}

function StepBackOutbound() {
    if (currentOutboundDate == null)
        currentOutboundDate = $("#outbound").val();

    var d = Date.parse(currentOutboundDate);

    currentOutboundDate = d.add(-3).days().toString("dd/MM/yyyy");
    DoOutbound($("#" + ddlDestinationsId + "").val(), currentOutboundDate);
}

function StepForwardOutbound() {

    if (currentOutboundDate == null)
        currentOutboundDate = $("#outbound").val();

    var d = Date.parse(currentOutboundDate);
    currentOutboundDate = d.add(3).days().toString("dd/MM/yyyy");
    DoOutbound($("#" + ddlDestinationsId + "").val(), currentOutboundDate);
}

function StepBackInbound() {
    if ($("#inbound").val().length > 0 && outbounded) {
        if (currentInboundDate == null)
            currentInboundDate = $("#inbound").val();

        var d = Date.parse(currentInboundDate);

        currentInboundDate = d.add(-3).days().toString("dd/MM/yyyy");
        DoInbound($("#" + ddlDestinationsId + "").val(), currentInboundDate);
    }
}

function StepForwardInbound() {
    if ($("#inbound").val().length > 0 && outbounded) {
        if (currentInboundDate == null)
            currentInboundDate = $("#inbound").val();

        var d = Date.parse(currentInboundDate);

        currentInboundDate = d.add(3).days().toString("dd/MM/yyyy");
        DoInbound($("#" + ddlDestinationsId + "").val(), currentInboundDate);
    }
}
var outbounded = false;
function DoOutbound(destination, departureDate) {

    outbounded = false;

    if (destination == "-select-") {
        alert("Please select a destination.");
        return;
    }

    if (departureDate.length == 0) {
        alert("Please enter an outbound date.");
        return;
    }

    outbounded = true;
    
    $("#holder-outbound").css("display", "block");
    $("#loading-outbound").html("<img src='/common/images/ajax-loading.gif' alt='Loading...' title='Loading...' />");

    var url = "/common/handlers/outbound.ashx?dest=" + destination + "&date=" + departureDate;
    $.get(url, function (data) {
        $("#outbound-info").html(data);
        $("#loading-outbound").html("");
        $("#holder-outbound a").css("display", "block");
    });
}

function DoInbound(origin, inboundDate) {

    if (inboundDate.length == 0) {
        alert("Please enter a return date.");
        return;
    }

    var url = "/common/handlers/inbound.ashx?origin=" + origin + "&date=" + inboundDate;
    $("#holder-inbound").css("display", "block");
    $("#loading-inbound").html("<img src='/common/images/ajax-loading.gif' alt='Loading...' title='Loading...' />");
    $.get(url, function (data) {
        $("#inbound-info").html(data);
        $("#loading-inbound").html("");
        $("#holder-inbound a").css("display", "block");
    });
}

function ToggleReturn() {
    if ($("#return").attr("checked")) {
        $("#returnFlights").css("display", "block");
    }
    else {
        $("#returnFlights").css("display", "none");
    }


}
