﻿var _previousValue;
var _continueSearch = true;
$(function() {
    LoadSearch();
    $("#CheckInDate").datepicker({
        numberOfMonths: 2,
        minDate: 0,
        maxDate: '+1Y',
        dateFormat: 'dd-mm-yy',
        onSelect: SetCheckOutDate,
        showAnim: 'slideDown'
    });
    $("#CheckOutDate").datepicker({
        numberOfMonths: 2,
        maxDate: '+1Y',
        dateFormat: 'dd-mm-yy',
        showAnim: 'slideDown'
    });
    if ($("#IBEHotelSearchBox").length > 0) {
        $("#CheckInDate").datepicker("setDate", "+1d");
        $("#CheckOutDate").datepicker("setDate", "+3d");
    }
    var checkInDate = $("#CheckInDate").datepicker("getDate");
    $("#CheckOutDate").datepicker("option", "minDate", new Date(checkInDate.getTime() + 1 * 24 * 60 * 60 * 1000));
    $("#CheckOutDate").datepicker("option", "maxDate", new Date(checkInDate.getTime() + 30 * 24 * 60 * 60 * 1000));
    $("#Search").removeAttr("disabled");
});
function LoadSearch() {
    $("#DestinationName").focus();
    $("#CheckInDate, #CheckOutDate, #SearchHotels select").focus(function() { HideError(); });
    $("#NoOfRms").change(function() { ChangeNoOfRooms($(this).val()); });
    $("#Rm1NoOfChdren").change(function() { ChangeNoOfChildren(1, $(this).val()) });
    $("#Rm2NoOfChdren").change(function() { ChangeNoOfChildren(2, $(this).val()) });
    $("#Rm3NoOfChdren").change(function() { ChangeNoOfChildren(3, $(this).val()) });
    $("#DestinationName").keyup(function(e) { SearchDestination(e); });
    $("#DestinationName").submit(function(e) { e.preventDefault(); });
    $("#SearchForm").bind("keypress", function(e) {
        if (e.keyCode == 13) { return false; }
    });
    $("#SearchForm").submit(function(e) { ValidateSearch(e); });
}
function HideDestinations() {
    $("select").css("visibility", "visible");
    if ($("#DestinationsContainer").css("display") != "none") $("#DestinationsContainer").hide();
    $("#LoadingDestinations").css("visibility", "hidden");
    _previousValue = "";
}
function ShowDestinations() {
    $("select").css("visibility", "hidden");
    var destinationsContainer = $("#DestinationsContainer");
    if (destinationsContainer.css("display") == "none") destinationsContainer.show();
    $("#LoadingDestinations").css("visibility", "hidden");
}
function FilterDestinations() {
    var prefix = $.trim($("input#DestinationName").val().toLowerCase());
    if (prefix.length == 0) HideDestinations();
    else {
        if (_previousValue != prefix) {
            if (prefix.length > 1 && _continueSearch) {
                $("#LoadingDestinations").css("visibility", "visible");
                var link = "/SearchHotels/SearchDestinations?prefix=" + prefix;
                $.get(link, function(response) {
                    $("#DestinationsContainer").html(response);
                    if ($("div[type='city']").length == 0 && $("div[type='landmark']").length == 0 &&
                        $("div[type='hotel']").length == 0 && $("#DoYouMeanHeader").length == 0) {
                        HideDestinations(); return;
                    } 
                    $("#DestinationsContainer span").highlight(prefix);
                    $($("#DestinationsContainer div")[0]).attr("class", "browsing");
                    $("#DestinationsContainer div").click(function() { ClickDestination(this); });
                    ShowDestinations();
                });
            }
            else {
                _continueSearch = true;
                HideDestinations();
            }
        }
        _previousValue = prefix;
    }    
}
function SearchDestination(e) {
    HideError();
    if (e.which == 32 || (65 <= e.which && e.which <= 65 + 25)
            || (97 <= e.which && e.which <= 97 + 25) || e.which == 8) {
        setTimeout("FilterDestinations();", 600);
    }
    else if (e.which == 8) {
        setTimeout("FilterDestinations();", 600);
    }
    else if (e.which == 38 || e.which == 40) {
        BrowseDestinations(e.which);
    }
    else if (e.which == 13) {
        SelectDestination();
    }
    else if (e.which == 27) {
        HideDestinations();
    }
}
function BrowseDestinations(keycode) {
    if ($("#DestinationsContainer").css("display") != "none") {
        if ($("#DestinationsContainer div").length > 0) {
            var browsing = $("#DestinationsContainer div.browsing");
            var scrollTop;
            if (keycode == 38) {
                var prev = browsing.prevAll("div");
                if (prev.length > 0) {
                    var prevOne = $(prev[0]);
                    prevOne.attr("class", "browsing");
                    browsing.removeAttr("class");
                    scrollTop = prevOne[0].offsetTop;
                    $("#DestinationsContainer").scrollTop(scrollTop - prevOne.height() - 20);
                }
            }
            else {
                var next = browsing.nextAll("div");
                if (next.length > 0) {
                    var nextOne = $(next[0]);
                    nextOne.attr("class", "browsing");
                    browsing.removeAttr("class");
                    scrollTop = nextOne[0].offsetTop;
                    $("#DestinationsContainer").scrollTop(scrollTop - nextOne.height() - 20);
                }
            }
        }
    }
}
function SelectDestination() {
    _continueSearch = false;
    var browsing = $("#DestinationsContainer .browsing").removeHighlight();
    if (browsing.length > 0) {
        $("#DestinationName").val($.trim(browsing.find("span").text()));
        browsing.removeAttr("class");
        $("#DestinationType").val(browsing.attr("type"));
        if (browsing.attr("type") != "hotel") {
            $("#HotelId").val("");
        }
    }
    HideDestinations();
}
function ClickDestination(div) {
    var destinationName = $(div).removeHighlight();
    $("#DestinationName").val($.trim(destinationName.find("span").text()));
    $("#DestinationType").val($(div).attr("type"));
    if ($(div).attr("type") != "hotel") {
        $("#HotelId").val("");
    }
    HideDestinations();
}
function SetCheckOutDate() {
    var checkInDate = $("#CheckInDate").datepicker("getDate");
    var minDate = new Date(checkInDate.getTime() + 1 * 24 * 60 * 60 * 1000);
    var maxDate = new Date(checkInDate.getTime() + 30 * 24 * 60 * 60 * 1000);
    var checkOutDate = new Date(checkInDate.getTime() + 3 * 24 * 60 * 60 * 1000);
    $("#CheckOutDate").datepicker("option", "minDate", minDate);
    $("#CheckOutDate").datepicker("option", "maxDate", maxDate);
    $("#CheckOutDate").datepicker("setDate", checkOutDate);
}
function ChangeNoOfRooms(noOfRooms) {
    var divRoom1 = $("#Rm1");
    var divRoom2 = $("#Rm2");
    var divRoom3 = $("#Rm3");
    switch (noOfRooms) {
        case "1":
            if (divRoom1.css("display") == "none") divRoom1.show();
            if (divRoom2.css("display") != "none") divRoom2.hide();
            if (divRoom3.css("display") != "none") divRoom3.hide();
            break;
        case "2":
            if (divRoom1.css("display") == "none") divRoom1.show();
            if (divRoom2.css("display") == "none") divRoom2.show();
            if (divRoom3.css("display") != "none") divRoom3.hide();
            break;
        case "3":
            if (divRoom1.css("display") == "none") divRoom1.show();
            if (divRoom2.css("display") == "none") divRoom2.show();
            if (divRoom3.css("display") == "none") divRoom3.show();
            break;
        default:
            break;
    }
}
function ChangeNoOfChildren(roomno, noOfChildren) {
    var divRoomChildrenAge = $("#Rm" + roomno + "Chdren");
    var divRoomChild1Age = $("#Rm" + roomno + "Chd1");
    var divRoomChild2Age = $("#Rm" + roomno + "Chd2");
    var ddlRoomChild1Age = $("#Rm" + roomno + "Chd1Age");
    var ddlRoomChild2Age = $("#Rm" + roomno + "Chd2Age");
    switch (noOfChildren) {
        case "0":
            if (divRoomChildrenAge.css("display") != "none") divRoomChildrenAge.hide();
            if (divRoomChild1Age.css("display") != "none") divRoomChild1Age.hide();
            if (divRoomChild2Age.css("display") != "none") divRoomChild2Age.hide();
            ddlRoomChild1Age.val("<1");
            ddlRoomChild2Age.val("<1");
            break;
        case "1":
            if (divRoomChildrenAge.css("display") == "none") divRoomChildrenAge.show();
            if (divRoomChild1Age.css("display") == "none") divRoomChild1Age.show();
            if (divRoomChild2Age.css("display") != "none") divRoomChild2Age.hide();
            ddlRoomChild2Age.val("<1");
            break;
        case "2":
            if (divRoomChildrenAge.css("display") == "none") divRoomChildrenAge.show();
            if (divRoomChild1Age.css("display") == "none") divRoomChild1Age.show();
            if (divRoomChild2Age.css("display") == "none") divRoomChild2Age.show();
            break;
    }
}
function ValidateSearch(e) {
    var errorCount = 0;
    if ($("#DestinationName").val() == "") {
        ShowError("#DestinationName", "Please enter a destination", e);
        errorCount++;
    }
    if (errorCount == 0 && $("#CheckInDate").val() == "") {
        ShowError("#CheckInDate", "Please select a check in date", e);
        errorCount++;
    }
    if (errorCount == 0 && $("#CheckOutDate").val() == "") {
        ShowError("#CheckOutDate", "Please select a check out date", e);
        errorCount++;
    }
    if (errorCount == 0 && !$("#CheckInDate").val().match(/^\d\d?\-\d\d?\-\d\d\d\d$/)) {
        ShowError("#CheckInDate", "Date must be in dd-MM-yyyy format", e);
        errorCount++;
    }
    if (errorCount == 0 && !$("#CheckOutDate").val().match(/^\d\d?\-\d\d?\-\d\d\d\d$/)) {
        ShowError("#CheckOutDate", "Date must be in dd-MM-yyyy format", e);
        errorCount++;
    }
    if (errorCount == 0) {
        var CheckInDate = $("#CheckInDate").datepicker('getDate');
        var now = new Date();
        var today = new Date(now.getFullYear(), now.getMonth(),now.getDate());
        if (CheckInDate < today) {
            ShowError("#CheckInDate", "Check in date must be today or later", e);
            errorCount++;
        }
    }
    if (errorCount == 0) {
        var CheckInDate = $("#CheckInDate").datepicker('getDate');
        var CheckOutDate = $("#CheckOutDate").datepicker('getDate');
        if (CheckOutDate <= CheckInDate) {
            ShowError("#CheckOutDate", "Invalid check out date", e);
            errorCount++;
        }
        else if (((CheckOutDate - CheckInDate) / 1000 / 60 / 60 / 24) > 30) {
            ShowError("#CheckOutDate", "Maximum period of stay is 30 days", e);
            errorCount++;
        }
    }
    if (errorCount == 0 && ($("#NoOfRms").val() > 3 || $("#NoOfRms") == 0)) {
        ShowError("#NoOfRooms", "Number of rooms must be between 1 and 3", e);
        errorCount++;
    }
    if (errorCount > 0) {
        e.preventDefault();
        return false;
    }
    else return true;
}
function ShowError(elementId, text, event) {
    var element = $(elementId);
    var top = element.position().top;
    var left = element.position().left;
    $("#ErrorMessage").html(text);
    var divError = $("#Error");
    divError.css({ "top": top - divError.height(), "left": left });
    $("#Error").show();
    event.preventDefault();
}
function HideError() {
    if ($("#Error").css("display") != "none") $("#Error").hide();
}