﻿var bedroomfilter = "";
var sleepfilter = "";
var catFilter = "";
var locFilter = "";
var accTypeFilter = "";
$(document).ready(function() {
    $("a[type=bedroom]").click(filterResult);
    $("a[type=sleeps]").click(filterSleep);
    $("a[type=cat]").click(filterCat);
    $("a[type=location]").click(filterloc);
    $("#lnkClear").click(clearfilters);
    $("a[type=acc]").click(filterAcc);
    if ($("#hdnArrDate").attr("value") != "" && $("#hdnDepDate").attr("value") != "") {
        if ($("#hdnSrchType").attr("value") == "basic") {
            $("#date1").val($("#hdnArrDate").attr("value"));
            $("#date2").val($("#hdnDepDate").attr("value"));
            $("#date5").val($("#hdnArrDate").attr("value"));
            $("#date6").val($("#hdnDepDate").attr("value"));
            $("select[@name='adults'] option[@value='" + $("#hdnAdult").attr("value") + "']").attr("selected", "selected");
            $("select[@name='children'] option[@value='" + $("#hdnChild").attr("value") + "']").attr("selected", "selected");
            $("select[@name='adults1'] option[@value='" + $("#hdnAdult").attr("value") + "']").attr("selected", "selected");
            $("select[@name='children1'] option[@value='" + $("#hdnChild").attr("value") + "']").attr("selected", "selected");
            $("select[@name='bedrooms'] option[@value='" + $("#hdnBedrooms").attr("value") + "']").attr("selected", "selected");
            Search($("#hdnSrchType").attr("value"), $("#hdnArrDate").attr("value"), $("#hdnDepDate").attr("value"), $("#hdnAdult").attr("value"), $("#hdnChild").attr("value"), $("#hdnBedrooms").val());
        }
        else if ($("#hdnSrchType").attr("value") == "advance") {
            $('#dvSearch').fadeOut("slow", function() { $('#dvAdSearch').fadeIn("slow"); });
            $("#date1").val($("#hdnArrDate").attr("value"));
            $("#date2").val($("#hdnDepDate").attr("value"));
            $("#date5").val($("#hdnArrDate").attr("value"));
            $("#date6").val($("#hdnDepDate").attr("value"));
            $("select[@name='adults'] option[@value='" + $("#hdnAdult").attr("value") + "']").attr("selected", "selected");
            $("select[@name='children'] option[@value='" + $("#hdnChild").attr("value") + "']").attr("selected", "selected");
            $("select[@name='adults1'] option[@value='" + $("#hdnAdult").attr("value") + "']").attr("selected", "selected");
            $("select[@name='children1'] option[@value='" + $("#hdnChild").attr("value") + "']").attr("selected", "selected");
            AdvSearch($("#hdnSrchType").attr("value"), $("#hdnArrDate").attr("value"), $("#hdnDepDate").attr("value"), $("#hdnLoc").attr("value"), $("#hdnRating").attr("value"), $("#hdnAdult").attr("value"), $("#hdnChild").attr("value"), $("#hdnBedrooms").val());
        }
    }
});
function Search(srchType, arrDate, DepartDate, adults, children, bedrooms) {
    $("#results").html("<div style='width:100%;text-align:center'><img src='/images/loading.gif' /><br /><br /><span class=\"Myrid18GB\">Searching...</span></div>");
    $.ajax({
        type: "GET",
        url: "/search.aspx",
        data: "searchType=" + srchType + "&ArrivalDate=" + arrDate + "&DepartureDate=" + DepartDate + "&adults=" + adults + "&children=" + children + "&beds=" + bedrooms,
        dataType: "text/xml",
        success: handleSearchResults
    });
}
function AdvSearch(srchType, arrDate, DepartDate, place, rating, adults, children, bedrooms) {
    $("#results").html("<div style='width:100%;text-align:center'><img src='/images/loading.gif' /></div>");
    $.ajax({
        type: "GET",
        url: "/search.aspx",
        data: "searchType=" + srchType + "&ArrivalDate=" + arrDate + "&DepartureDate=" + DepartDate + "&place=" + place + "&rating=" + rating + "&adults=" + adults + "&children=" + children + "&beds=" + bedrooms,
        dataType: "text/xml",
        success: handleSearchResults
    });
}
function handleSearchResults(ret) {
    $("#results").html(ret);
    var result = $("div[name*='SearchHeader']");

    $("a[type=location]").each(function() {
        var locno = $(this).attr("locno");
        var locCount = $("div[name][resort=" + locno + "]");
        if (locCount.length > 0) {
            $(this).text($(this).attr("locname") + " (" + locCount.length + ")");
        }
        else {
            $(this).text($(this).attr("locname"));
        }
    });

    $("a[type=bedroom]").each(function() {
        var num = $(this).attr("num");
        var locCount = $("div[name][bedroom=" + num + "]");
        if (locCount.length > 0) {
            $(this).text($(this).attr("text1") + " (" + locCount.length + ")");
        }
        else {
            $(this).text($(this).attr("text1"));
        }
    });
    $("a[type=sleeps]").each(function() {
        var num = $(this).attr("num");
        var locCount = $("div[name][sleeps=" + num + "]");
        if (locCount.length > 0) {
            $(this).text($(this).attr("text1") + " (" + locCount.length + ")");
        }
        else {
            $(this).text($(this).attr("text1"));
        }
    });

    $("a[type=cat]").each(function() {
        var num = $(this).attr("standard");
        var locCount = $("div[name][standard=" + num + "]");
        if (locCount.length > 0) {
            $(this).text($(this).attr("text1") + " (" + locCount.length + ")");
        }
        else {
            $(this).text($(this).attr("text1"));
        }
    });
    $("a[type=acc]").each(function() {
        var num = $(this).attr("accType");
        var locCount = $("div[name][accType=" + num + "]");
        if (locCount.length > 0) {
            $(this).text($(this).attr("text1") + " (" + locCount.length + ")");
        }
        else {
            $(this).text($(this).attr("text1"));
        }
    });
    bedroomfilter = "";
    sleepfilter = "";
    catFilter = "";
    locFilter = "";
    accTypeFilter = "";
    UpdateCurr();
}

function filterResult() {
    var filter = $(this).attr("num");
    if (filter == bedroomfilter) {
        bedroomfilter = "";
        $(this).attr("class", "filterlink");
    }
    else {
        $("a[type=bedroom][num=" + bedroomfilter + "]").attr("class", "filterlink");
        bedroomfilter = filter;
        $(this).attr("class", "filterlink1");

    }
    UpdateFilters();
    return false;
}
function filterSleep() {
    var filter = $(this).attr("num");
    if (filter == sleepfilter) {
        sleepfilter = "";
        $(this).attr("class", "filterlink");
    }
    else {
        $("a[type=sleeps][num=" + sleepfilter + "]").attr("class", "filterlink");
        sleepfilter = filter;
        $(this).attr("class", "filterlink1");
    }
    UpdateFilters();
    return false;
}

function filterCat() {
    var filter = $(this).attr("standard");
    if (filter == catFilter) {
        catFilter = "";
        $(this).attr("class", "filterlink");
    }
    else {
        $("a[type=cat][standard=" + catFilter + "]").attr("class", "filterlink");
        catFilter = filter;
        $(this).attr("class", "filterlink1");
    }
    UpdateFilters();
    return false;
}
function filterloc() {
    var filter = $(this).attr("locno");
    if (filter == locFilter) {
        locFilter = "";
        $(this).attr("class", "filterlink");
    }
    else {
        $("a[type=location][locno=" + locFilter + "]").attr("class", "filterlink");
        locFilter = filter;
        $(this).attr("class", "filterlink1");
    }
    UpdateFilters();
    return false;
}
function filterAcc() {
    var filter = $(this).attr("accType");
    if (filter == accTypeFilter) {
        accTypeFilter = "";
        $(this).attr("class", "filterlink");
    }
    else {
        $("a[type=acc][accType=" + accTypeFilter + "]").attr("class", "filterlink");
        accTypeFilter = filter;
        $(this).attr("class", "filterlink1");
    }
    UpdateFilters();
    return false;
}
function clearfilters() {
    bedroomfilter = "";
    sleepfilter = "";
    catFilter = "";
    locFilter = "";
    accTypeFilter = "";
    $("a[type=location]").each(function() {
        $(this).attr("class", "filterlink");
    });
    $("a[type=bedroom]").each(function() {
        $(this).attr("class", "filterlink");
    });
    $("a[type=sleeps]").each(function() {
        $(this).attr("class", "filterlink");
    });
    $("a[type=cat]").each(function() {
        $(this).attr("class", "filterlink");
    });
    $("a[type=acc]").each(function() {
        $(this).attr("class", "filterlink");
    });
    UpdateFilters();
    return false;
}
function UpdateFilters() {
    var filterString = "";
    if (bedroomfilter != "") {
        filterString = "[bedroom$='" + bedroomfilter + "']";
    }
    if (sleepfilter != "") {
        filterString += "[sleeps$='" + sleepfilter + "']";
    }
    if (catFilter != "") {
        filterString += "[standard$='" + catFilter + "']";
    }
    if (locFilter != "") {
        filterString += "[resort$='" + locFilter + "']";
    }
    if (accTypeFilter != "") {
        filterString += "[accType$='" + accTypeFilter + "']";
    }
    
    $("div[name*='SearchHeader']").attr("class", "invisibleDiv");
    $("div[name*='SearchHeader']" + filterString + "").attr("class", "visibleDiv");

    $("a[type=location]").each(function() {
        $(this).text("");
        var locno = $(this).attr("locno");
        var locCount = $("div[name][resort=" + locno + "][class=visibleDiv]");
        if (locCount.length > 0) {
            $(this).text($(this).attr("locname") + " (" + locCount.length + ")");
        }
        else {
            $(this).text($(this).attr("locname"));
        }
    });
    $("a[type=bedroom]").each(function() {
        $(this).text("");
        var num = $(this).attr("num");
        var locCount = $("div[name][bedroom=" + num + "][class=visibleDiv]");
        if (locCount.length > 0) {
            $(this).text($(this).attr("text1") + " (" + locCount.length + ")");
        }
        else {
            $(this).text($(this).attr("text1"));
        }
    });
    $("a[type=sleeps]").each(function() {
        $(this).text("");       
        var num = $(this).attr("num");
        var locCount = $("div[name][sleeps=" + num + "][class=visibleDiv]");
        if (locCount.length > 0) {
            $(this).text($(this).attr("text1") + " (" + locCount.length + ")");
        }
        else {
            $(this).text($(this).attr("text1"));
        }
    });
    $("a[type=acc]").each(function() {
        var num = $(this).attr("accType");
        var locCount = $("div[name][accType=" + num + "][class=visibleDiv]");
        if (locCount.length > 0) {
            $(this).text($(this).attr("text1") + " (" + locCount.length + ")");
        }
        else {
            $(this).text($(this).attr("text1"));
        }
    });
    $("a[type=cat]").each(function() {
        $(this).text("");
        var num = $(this).attr("standard");
        var locCount = $("div[name][standard=" + num + "][class=visibleDiv]");
        if (locCount.length > 0) {
            $(this).text($(this).attr("text1") + " (" + locCount.length + ")");
        }
        else {
            $(this).text($(this).attr("text1"));
        }
    });
}