﻿

var loadingHtml = '<h3>Searching...</h3><div class="loading"><img src="/_assets/images/icons/ajax-loader-longer.gif" /></div>';

$(document).ready(function() {
    $(".price").hide();
    $(".search").hide();

    $("#checkbtnTop, #checkbtnBottom, #research").click(ShowSearch);
    $("#ajaxBookButton").click(AjaxSearch);
});
 

function ShowSearch() {
    $(".form").show();
    $(".price").hide();
    $(".search").hide();
    $(".FlyOutBookingForm").removeClass("small");

    if ($(".FlyOutBookingForm").length > 1) {
        $("#FlyOutBookingForm_Bottom").html("");
    }
    
    //re-bind click events.
    $("#ajaxBookButton").click(AjaxSearch);

    //init calender
    VH.CAL.Init(null);
}

function ClonedSummaryShowSearch() {
    $(".form").show();
    $(".price").hide();
    $(".search").hide();
    $(".FlyOutBookingForm").removeClass("small");

    if ($(".FlyOutBookingForm").length > 1) {
        var flyOutForm = $("#FlyOutBookingForm_Top .FlyOutBookingForm");
        $("#FlyOutBookingForm_Bottom").html(flyOutForm);
    }

    //re-bind click events.
    $("#ajaxBookButton").click(AjaxSearch);
    
    //init calender
    VH.CAL.Init(null);
}


function AjaxSearch() {
    var depair = $(".AirportDD :selected").val();
    var ad = $(".adult :selected").val();
    var ch = $(".children :selected").val();
    var inf = $(".infants :selected").val();
    if (parseInt(ad) + parseInt(ch) + parseInt(inf) > 8) { $("#paxerror").text("Please select a maximum of 8 passengers"); return false; }
    if (depair == "-") { $("#noAirport").text("Please select a departure airport"); return false; }
    var ages = "";
    $("#Pax1ChildAgesPanel input").each(function() {
        if ($(this).val() != "") ages += $(this).val() + ",";
    });

    var url = GetPageName() + "?";
    url += "Key=" + HotelKEY;
    url += "&DepAirport=" + depair;
    url += "&DepDate=" + $(".departureDate").val();
    url += "&Duration=" + $(".durationFs :selected").val();
    url += "&AdultPax=" + ad;
    url += "&ChildPax=" + ch;
    url += "&InfantPax=" + inf;
    //            	var ages = "";
    //            	$("#Pax1ChildAgesPanel :not(.hide) input").each(function() {
    //            		if ($(this).val() < 2 || $(this).val() > 12) { $("#paxerror").text("Please enter the child ages correctly"); return false; }
    //            		ages += $(this).val() + ",";
    //            	});
    url += "&ChildAges=" + ages.substring(0, ages.length - 1)

    $(".FlyOutBookingForm").addClass("small");
    $(".form").hide();
    $(".price").hide();
    $(".search").html(loadingHtml).show();

    $.getJSON(url, null, ShowPrice);
}

function ShowPrice(data, textStatus) {

    if (!VH.UTIL.isUndefinedOrNull(HotelType)) {
        if (HotelType == "AccomOnlyHotel")
            ShowPriceAccomOnly(data);
        else
            ShowHotelPrice(data, textStatus);

        cloneSummary();
    }
}

function ShowHotelPrice(data, textStatus) {
    if (data.error) {
        $("#bookingForm .search").html("<h" + "3>Sorry</h" + "3><p>There has been a problem with your search: </p><p><b>" +
             data.error.message + "</b></p>" +
             "<a id='re_search_error' href='javascript://'>&lt; Change search details</a>"
             );
    }
    else if (data.AlternativeFlights) {
        document.location.href = "/book/start";
    }
    else {
        $("#priceimage").attr("src", "/dynamichtag.aspx?w=150&s=28&c=000000&b=f8f8f8&hoffset=4&caption=%c2%a3" + (data.Availability.PerAdult.replace(/\.../, "")));
        $(".price ul li").eq(0).text("Leaving on " + $(".departureDate").val() + " for " + $(".durationFs :selected").text());

        if (data.Availability.PricedAsAccomOnlyFlyDrive) {
            $(".price ul li").eq(1).html("<b>Accommodation only (flight not included)</b>");
            $(".price ul li").eq(2).html("<b>Total cost with flights will be confirmed in the next step.<b/>");
            $(".price ul li").eq(3).hide();
            $(".price ul li").eq(4).hide();
        }
        else {
            var ac = data.Availability.Accommodation;
            var fl = data.Availability.Flights;
            $(".price ul li").eq(1).text(ac.Rooms + "x" + ac.Description + (ac.Rooms != "1" ? "s" : "") + ", " + ac.BoardBasis.Description);
            $(".price ul li").eq(2).text(getPaxString());
            $(".price ul li").eq(3).html("Flights and taxes included from " + $(".AirportDD :selected").text() + "(" + $(".AirportDD :selected").val() + ")");
            $(".price ul li").eq(4).html(fl.ClassOut + " outbound and " + fl.ClassReturn + " return.");
        }
        $(".search").hide();
        $(".price").show();
    }
}


var ShowPriceAccomOnly = function(data) {
    if (data.error) {
        $("#bookingForm .search").html("<h" + "3>Sorry</h" + "3><p>There has been a problem with your search; <br /><b>" +
                 data.error.message + "</b></p>" +
                 "<a id='re_search_error' href='javascript://'>&lt; Change search details</a>"
                 ).find("a").click(ShowSearch);
    }
    else if (data.AlternativeFlights) {
        document.location.href = "/book/start";
    }
    else {
        $("#priceimage").attr("src", "/dynamichtag.aspx?w=150&s=28&c=000000&b=f8f8f8&hoffset=4&caption=%c2%a3" + (data.Availability.PerAdult.replace(/\.../, "")));
        $(".price ul li").eq(0).text("Leaving on " + $(".departureDate").val() + " for " + $(".durationFs :selected").text());
        var ac = data.Availability.Accommodation;
        var fl = data.Availability.Flights;
        $(".price ul li").eq(1).text(ac.Rooms + "x" + ac.Description + (ac.Rooms != "1" ? "s" : "") + ", " + ac.BoardBasis.Description);
        $(".price ul li").eq(2).text(getPaxString());

        $(".search").hide();
        $(".price").show();
    }
} 


function getPaxString() {
    var ad = $(".adult :selected").val();
    var ch = $(".children :selected").val();
    var inf = $(".infants :selected").val();
    var str = ad + " Adult" + (ad == 1 ? "" : "s");
    str += ", " + ch + " Child" + (ch == 1 ? "" : "ren");
    str += ", " + inf + " Infant" + (inf == 1 ? "" : "s");
    return str;
}


function cloneSummary() {

    var topSummary = $("#FlyOutBookingForm_Top");
    var bottomSummary = $("#FlyOutBookingForm_Bottom");

    var flyOutForm = $(".FlyOutBookingForm");

    if (flyOutForm.length > 1) {
        $("#FlyOutBookingForm_Bottom").html("");
    }

    //always move form to top
    topSummary.html(flyOutForm);

    var html = topSummary.html();
    html = html.replace("research", "research2");
    html = html.replace("re_search_error", "re_search_error2");
    html = html.replace("bookingForm", "bookingFormBottom");
    
    bottomSummary.html(html);

    $("#research, #re_search_error").click(ShowSearch);
    $("#research2, #re_search_error2").click(ClonedSummaryShowSearch);

    $("#FlyOutBookingForm_Bottom .close").click(CloseFlyOutForm);
    
    $("#FlyOutBookingForm_Bottom .form").remove();
}

function GetPageName() {

    if (!VH.UTIL.isUndefinedOrNull(HotelType)) {
        if (HotelType == "Hotel")
            return "/book/checkprice.aspx";
        else if (HotelType == "TourHotel")
            return "/book/checkprice_fixedtour.aspx";
        else if (HotelType == "AccomOnlyHotel")
            return "/book/checkprice_accomonly.aspx";
    }

    return null;
}
        