var CYPRUSPARADISE = function() { };

CYPRUSPARADISE.helper = function () {
    return {
        getDocHeight: function () {
            return Math.max(
                $(document).height(),
                $(window).height(),
                document.documentElement.clientHeight
            );
        },

        getDocWidth: function () {
            return Math.max(
                $(document).width(),
                $(window).width(),
                document.documentElement.clientWidth
            );
        },

        setToCenter: function () {
            //this.css("position", "absolute");
            this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
            this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
            return this;
        },

        setCurrency: function (value) {
            var num = new Number(value);
            return "£" + CYPRUSPARADISE.helper.addCommas(num.toFixed(2));

        },

        addCommas: function (nStr) {
            nStr += '';
            var x = nStr.split('.');
            var x1 = x[0];
            var x2 = x.length > 1 ? '.' + x[1] : '';
            var rgx = /(\d+)(\d{3})/;
            while (rgx.test(x1)) {
                x1 = x1.replace(rgx, '$1' + ',' + '$2');
            }
            return x1 + x2;
        },


        getNoOfChildren: function (occupancy) {
            var rStr = occupancy.split('|');
            var pStr;
            var noOfChildren = 0;

            for (var i = 0; i < rStr.length; i++) {
                pStr = rStr[i].split(',');
                //or (var j = 0; j < pStr.length; j++) {
                noOfChildren += parseInt(pStr[1]);
                //}
            }
            return noOfChildren;
        },

        getNoOfPassengers: function (occupancy) {
            var rStr = occupancy.split('|');
            var pStr;
            var noOfPax = 0;

            for (var i = 0; i < rStr.length; i++) {
                pStr = rStr[i].split(',');
                for (var j = 0; j < pStr.length; j++) {
                    noOfPax += parseInt(pStr[j]);
                }
            }

            return noOfPax;

        },

        average: function (value, no) {
            return (value / no);
        },


        isDate: function (value) {
            try {
                var DayIndex = 0;
                var MonthIndex = 1;
                var YearIndex = 2;

                value = value.replace("-", "/").replace(".", "/");
                var SplitValue = value.split("/");
                var OK = true;
                if (!(SplitValue[DayIndex].length == 1 || SplitValue[DayIndex].length == 2)) {
                    OK = false;
                }
                if (OK && !(SplitValue[MonthIndex].length == 1 || SplitValue[MonthIndex].length == 2)) {
                    OK = false;
                }
                if (OK && SplitValue[YearIndex].length != 4) {
                    OK = false;
                }
                if (OK) {
                    var Day = parseInt(SplitValue[DayIndex], 10);
                    var Month = parseInt(SplitValue[MonthIndex], 10);
                    var Year = parseInt(SplitValue[YearIndex], 10);

                    if (OK = (Year > 1900) ) {
                        if (OK = (Month <= 12 && Month > 0)) {
                            var LeapYear = (((Year % 4) == 0) && ((Year % 100) != 0) || ((Year % 400) == 0));

                            if (Month == 2) {
                                OK = LeapYear ? Day <= 29 : Day <= 28;
                            }
                            else {
                                if ((Month == 4) || (Month == 6) || (Month == 9) || (Month == 11)) {
                                    OK = (Day > 0 && Day <= 30);
                                }
                                else {
                                    OK = (Day > 0 && Day <= 31);
                                }
                            }
                        }
                    }
                }
                return OK;
            }
            catch (e) {
                return false;
            }
        }


    }

} ();

CYPRUSPARADISE.searchBox = function () {
    return {}
} ();

CYPRUSPARADISE.hotelList = function () {
    return {
        filterResults: function () {
            //alert($('.filter_star').val());
            //alert($('.filter_hotel').val());
            //alert($('.filter_region').val());
            $('#hotelList').html('<img src="/_gfx/ajax-loader.gif" alt="loading..." />');
            $('#hotelList').load('/_ajax/hotelList.aspx?star=' + $('.filter_star').val() + '&hotel=' + $('.filter_hotel').val() + '&region=' + $('.filter_region').val());
        }
    }
} ();

CYPRUSPARADISE.accommodation = function () {
    return {
        filterCacheOffers: function () {
            $('#cachePackageHolder').html('<img src="/_gfx/ajax-loader.gif" alt="loading..." />');
            $('#cachePackageHolder').load('/_ajax/CachePackageList.aspx?month=' + $('.filter_month').val() + '&hotel=' + $('#filter_hotel').val() + '&duration=' + $('.filter_duration').val() + '&airport=' + $('.filter_airport').val());
        },

        setAccomOnSearchPanel: function (accomID) {
            $('#ddl_hotelName').val(accomID);
        }

    }
} ();

CYPRUSPARADISE.destination = function () {
    return {
        filterCacheOffers: function () {
            $('#cachePackageHolder').html('<img src="/_gfx/ajax-loader.gif" alt="loading..." />');
            $('#cachePackageHolder').load('/_ajax/CachePackageList.aspx?month=' + $('.filter_month').val() + '&limit=' + $('#filter_limit').val() +  '&locationID=' + $('#filter_location').val() + '&duration=' + $('.filter_duration').val() + '&airport=' + $('.filter_airport').val());
        },

        setResortsOnSearchPanel: function (locationID) {
            $('#ddl_hotelName').val(locationID);
        }

    }
} ();

CYPRUSPARADISE.searchAccom = function () {
    return {
        toggleRooms: function (indexID) {
            $('.div_roomType_' + indexID).slideToggle(function () {
                if ($(this).css('display') == "none") {
                    //alert('no show');
                    CYPRUSPARADISE.searchAccom.resetRoomSelected(indexID);
                    $('.showRoomWord_' + indexID).html('Room');
                }
                else {
                    $('.showRoomWord_' + indexID).html('Hide');
                }
            });
        },

        resetRoomSelected: function (indexID) {
            var div = $('.div_roomType_' + indexID);
            div.children().each(function () {
                var child = $(this);
                if (child.is('.div_content_col2_results_item_options_roomType')) {
                    //alert("!");
                    var input = child.find(":radio:first");
                    //alert(input.length);
                    input.attr('checked', true);
                }
            });
            CYPRUSPARADISE.searchAccom.roomSelected(indexID);
        },

        resetAllRoomSelected: function () {
            var resultsCount = parseInt($('.resultsCount input').val());
            for (var i = 0; i < resultsCount; i++) {
                CYPRUSPARADISE.searchAccom.resetRoomSelected(i);
            }
        },

        roomSelected: function (indexID) {
            var div = $('.div_roomType_' + indexID);
            var totalPrice = 0;
            var price = 0;
            var rStr;
            var roomIndex = 0;
            div.children().each(function () {
                //alert(this);
                var child = $(this);
                if (child.is('.div_content_col2_results_item_options_roomType')) {
                    //alert("!");
                    rStr = child.find(':radio:checked').val();
                    if (rStr.length > 0) {
                        totalPrice = totalPrice + Number(rStr.split('|')[1]);
                        roomTypeName = rStr.split('|')[2];
                        $('.spn_roomTypeName_' + indexID + '_' + roomIndex).html(roomTypeName);
                    }
                    roomIndex++;
                }
            });
            //alert(totalPrice);
            CYPRUSPARADISE.searchAccom.setPrices(indexID, totalPrice);
        },

        addToBasket: function (resultIndex, occupancy) {
            var div = $('.div_roomType_' + resultIndex);

            //var cacheIDs = '';
            var cacheID = '';

            var cDOBstr = '';

            var noOfChildren = CYPRUSPARADISE.helper.getNoOfChildren(occupancy);
            if (noOfChildren > 0) {
                cDOBstr = $('#hid_cDOBs').val();
            }

            div.children().each(function (index, item) {
                var child = $(this);
                if (child.is('.div_content_col2_results_item_options_roomType')) {
                    //alert("!");
                    cacheID = child.find(':radio:checked').val().split('|')[0];

                    var s = '/booking/basketadd.aspx'
                    s += '?cacheID=' + cacheID;
                    s += '&occupancy=' + occupancy;
                    s += '&unit=' + index;
                    if (cDOBstr.length > 0) {
                        s += '&cDOBs=' + cDOBstr;
                    }
                    s += '&ticks=' + getTicks();

                    $.ajax({ url: s, async:false, success: function () {
                    }
                    , error: function () { alert('error'); }
                    });
                };
            });

            document.location.href = '/booking/basketview.aspx';

        },


        getAccomSummary: function (accomID, indexID) {
            var selectedCacheID = $('.div_hid_selected_cacheID_' + indexID + ' input').val();
            var selectedPrice = $('.div_totalPrice_' + indexID).html();
            var occupancy = $('#hid_occupancy').val();

            $('.blackOut').height(CYPRUSPARADISE.helper.getDocHeight());
            $('.blackOut').width(CYPRUSPARADISE.helper.getDocWidth());

            $('.div_accomSummary').load("/_ajax/SearchAccomSummary.aspx?accommodationID=" + accomID + "&indexID=" + indexID + "&price=" + CYPRUSPARADISE.searchAccom.getTotalPrice(indexID) + "&avgPrice=" + CYPRUSPARADISE.helper.average(CYPRUSPARADISE.searchAccom.getTotalPrice(indexID), CYPRUSPARADISE.helper.getNoOfPassengers($('#hid_occupancy').val())) + "&searchMode=hotels&occupancy=" + occupancy, function () {
                $(this).show(function () {
                });
            });

            $('.div_accomSummary').css({ top: '50%', left: '50%', margin: '-' + ($('.div_accomSummary').height() / 2) + 'px 0 0 -' + ($('.div_accomSummary').width() / 2) + 'px' });

        },

        getCacheIDs: function (indexID) {
            var div = $('.div_roomType_' + indexID);
            var cacheIDs = '';
            var cacheID = '';
            div.children().each(function () {
                var child = $(this);
                if (child.is('.div_content_col2_results_item_options_roomType')) {
                    //alert("!");
                    cacheID = child.find(':radio:checked').val().split('|')[0];
                    if (cacheIDs.length > 0) cacheIDs = cacheIDs + ',';
                    cacheIDs = cacheIDs + cacheID;
                }
            });
            //alert(cacheIDs);
            return cacheIDs;
        },

        getTotalPrice: function (indexID) {
            var div = $('.div_roomType_' + indexID);
            var totalPrice = 0;
            var price = 0;
            div.children().each(function () {
                var child = $(this);
                if (child.is('.div_content_col2_results_item_options_roomType')) {
                    //alert("!");
                    price = child.find(':radio:checked').val().split('|')[1];
                    totalPrice = totalPrice + Number(price);
                }
            });
            //alert(totalPrice);
            return totalPrice;
        },

        setPrices: function (indexID, totalPrice) {
            $('#hid_totalPrice_' + indexID).val(totalPrice);
            $('.div_totalPrice_' + indexID).html(CYPRUSPARADISE.helper.setCurrency(totalPrice));
            var occupancy = $('#hid_occupancy').val();
            //alert(CYPRUSPARADISE.helper.getNoOfPassengers(occupancy));
            var noOfPassenger = CYPRUSPARADISE.helper.getNoOfPassengers(occupancy);
            $('.div_avgPrice_' + indexID).html(CYPRUSPARADISE.helper.setCurrency(CYPRUSPARADISE.helper.average(totalPrice, noOfPassenger)));
        },

        validateChildDOB: function (indexID) {
            var occupancy = $('#hid_occupancy').val();
            var noOfChildren = CYPRUSPARADISE.helper.getNoOfChildren(occupancy);
            var cDOBstr = '';
            var cDOBs;
            var OK;

            if (noOfChildren > 0) {
                for (var i = 1; i <= noOfChildren; i++) {
                    if (cDOBstr.length > 0) { cDOBstr += ','; }
                    cDOBstr += $('#ChildAges_' + i + '_Day').val() + "/" + $('#ChildAges_' + i + '_Month').val() + "/" + $('#ChildAges_' + i + '_Year').val();
                }
            }

            //alert(cDOBstr);

            cDOBs = cDOBstr.split(',');

            OK = true;

            for (var i = 0; i <= noOfChildren - 1; i++) {
                if (!CYPRUSPARADISE.helper.isDate(cDOBs[i])) {
                    OK = false;
                }
            }

            if (OK) {
                $('#hid_cDOBs').val(cDOBstr);
                CYPRUSPARADISE.searchAccom.addToBasket(indexID, occupancy);
            }
            else {
                alert('DOB not valid');
            }
        },

        getChildAges: function (indexID) {
            var occupancy = $('#hid_occupancy').val();

            if ($('.div_accomSummary').css('display') == 'none') {
                $('.blackOut').height(CYPRUSPARADISE.helper.getDocHeight());
                $('.blackOut').width(CYPRUSPARADISE.helper.getDocWidth());
            }
            else {
                $('.div_accomSummary').css('z-index', 900);
            }

            $('.div_childAges').load("/booking/ajax/ChildAges.aspx?searchMode=hotels&occupancy=" + occupancy + "&indexID=" + indexID, function () {
                $(this).show(function () {
                });
            });

            $('.div_childAges').css({ top: '50%', left: '50%', margin: '-' + ($('.div_childAges').height() / 2) + 'px 0 0 -' + ($('.div_childAges').width() / 2) + 'px' });
        },

        closeChildAges: function () {
            $('.div_childAges').html("");
            $('.div_childAges').hide();

            if ($('.div_accomSummary').css('display') == 'none') {
                $('.blackOut').height(0);
                $('.blackOut').width(0);
            }
            else {
                $('.div_accomSummary').css('z-index', 1002);
            }

        }

    }
} ();

/*The following function for searchFlight is not used as the page is changed to searchFlight_return*/

//CYPRUSPARADISE.searchFlight = function () {
//    return {
//        OBflightSelected: function (indexID, occupancy) {
//            var adults = occupancy.split(',')[0];
//            var children = occupancy.split(',')[1];
//            var infants = occupancy.split(',')[2];

//            var adultPrice = parseFloat($('.OB_adultPrice_' + indexID + ' input').val()) * parseInt(adults);
//            $('#div_Adult_OB').html(CYPRUSPARADISE.helper.setCurrency(adultPrice));
//            var childPrice = parseFloat($('.OB_childPrice_' + indexID + ' input').val()) * parseInt(children);
//            $('#div_Child_OB').html(CYPRUSPARADISE.helper.setCurrency(childPrice));

//            //$('#div_Infant_OB').html($('.OB_adultPrice_' + indexID ' input').val());
//            var tax = parseFloat($('.OB_adultTax_' + indexID + ' input').val()) * parseInt(adults) + parseFloat($('.OB_childTax_' + indexID + ' input').val()) * parseInt(children);
//            //alert(tax);
//            $('#div_Tax_OB').html(CYPRUSPARADISE.helper.setCurrency(tax));
//            CYPRUSPARADISE.searchFlight.OB_flightTotal(adultPrice, childPrice);
//            CYPRUSPARADISE.searchFlight.flightTotal();
//        },

//        IBflightSelected: function (indexID, occupancy) {
//            var adults = occupancy.split(',')[0];
//            var children = occupancy.split(',')[1];
//            var infants = occupancy.split(',')[2];

//            var adultPrice = parseFloat($('.IB_adultPrice_' + indexID + ' input').val()) * parseInt(adults);
//            $('#div_Adult_IB').html(CYPRUSPARADISE.helper.setCurrency(adultPrice));
//            var childPrice = parseFloat($('.IB_childPrice_' + indexID + ' input').val()) * parseInt(children);
//            $('#div_Child_IB').html(CYPRUSPARADISE.helper.setCurrency(childPrice));

//            var tax = parseFloat($('.IB_adultTax_' + indexID + ' input').val()) * parseInt(adults) + parseFloat($('.IB_childTax_' + indexID + ' input').val()) * parseInt(children);
//            $('#div_Tax_IB').html(CYPRUSPARADISE.helper.setCurrency(tax));
//            CYPRUSPARADISE.searchFlight.IB_flightTotal(adultPrice, childPrice);
//            CYPRUSPARADISE.searchFlight.flightTotal();
//        },

//        OB_flightTotal: function (adultPrice, childPrice) {
//            var OB_total = adultPrice + childPrice;
//            //alert(OB_total);
//            $('#hid_total_OB').val(OB_total);
//            $('#div_total_OB').html(CYPRUSPARADISE.helper.setCurrency(OB_total));
//        },

//        IB_flightTotal: function (adultPrice, childPrice) {
//            var IB_total = adultPrice + childPrice;
//            //alert(IB_total);
//            $('#hid_total_IB').val(IB_total);
//            $('#div_total_IB').html(CYPRUSPARADISE.helper.setCurrency(IB_total));
//        },

//        flightTotal: function () {
//            var total = parseInt($('#hid_total_OB').val()) + parseInt($('#hid_total_IB').val());
//            //alert(total);
//            $('#div_total').html(CYPRUSPARADISE.helper.setCurrency(total));
//        },

//        addToBasket: function (occupancy) {
//            var s = '/booking/basketadd.aspx';
//            var c = '';

//            $('.resultsOB').find('input:radio:checked').each(function (index, item) {
//                c = $(item).val();
//            });

//            if (c.length > 0) {
//                s += '?cacheID=' + c;
//                s += '&occupancy=' + occupancy;
//                $.ajax({ url: s, async: false });
//            }

//            s = '/booking/basketadd.aspx';

//            $('.resultsIB').find('input:radio:checked').each(function (index, item) {
//                c = $(item).val();
//            });

//            if (c.length > 0) {
//                s += '?cacheID=' + c;
//                s += '&occupancy=' + occupancy;
//                $.ajax({ url: s, async: false });
//            }

//            document.location.href = '/booking/basketview.aspx';

//        }
//    }
//} ();

CYPRUSPARADISE.searchFlight = function () {
    return {
        addToBasket: function (indexID) {
            var s = '/booking/basketadd.aspx';
            var c = '';
            var occupancy = $('#hid_occupancy').val();
            var cDOBstr = '';
            var noOfChildren = CYPRUSPARADISE.helper.getNoOfChildren(occupancy);
            if (noOfChildren > 0) {
                cDOBstr = $('#hid_cDOBs').val();
            }

            c = $('#hid_cacheID_' + indexID).val();

            if (c.length > 0) {
                s += '?cacheID=' + c;
                s += '&occupancy=' + occupancy;
                if (cDOBstr.length > 0) {
                    s += '&cDOBs=' + cDOBstr;
                }
                $.ajax({ url: s, success: function () {
                    document.location.href = '/booking/basketview.aspx';
                }
                    , error: function () { alert('error'); }
                });
            }


            

        },

        validateChildDOB: function (indexID) {
            var occupancy = $('#hid_occupancy').val();
            var noOfChildren = CYPRUSPARADISE.helper.getNoOfChildren(occupancy);
            var cDOBstr = '';
            var cDOBs;
            var OK;

            if (noOfChildren > 0) {
                for (var i = 1; i <= noOfChildren; i++) {
                    if (cDOBstr.length > 0) { cDOBstr += ','; }
                    cDOBstr += $('#ChildAges_' + i + '_Day').val() + "/" + $('#ChildAges_' + i + '_Month').val() + "/" + $('#ChildAges_' + i + '_Year').val();
                }
            }

            //alert(cDOBstr);

            cDOBs = cDOBstr.split(',');

            OK = true;

            for (var i = 0; i <= noOfChildren - 1; i++) {
                if (!CYPRUSPARADISE.helper.isDate(cDOBs[i])) {
                    OK = false;
                }
            }

            if (OK) {
                $('#hid_cDOBs').val(cDOBstr);
                CYPRUSPARADISE.searchFlight.addToBasket(indexID);
            }
            else {
                alert('DOB not valid');
            }
        },

        getChildAges: function (indexID) {
            var occupancy = $('#hid_occupancy').val();

            if ($('.div_accomSummary').css('display') == 'none') {
                $('.blackOut').height(CYPRUSPARADISE.helper.getDocHeight());
                $('.blackOut').width(CYPRUSPARADISE.helper.getDocWidth());
            }
            else {
                $('.div_accomSummary').css('z-index', 900);
            }

            $('.div_childAges').load("/booking/ajax/ChildAges.aspx?searchMode=flights&occupancy=" + occupancy + "&indexID=" + indexID, function () {
                $(this).show(function () {
                });
            });

            $('.div_childAges').css({ top: '50%', left: '50%', margin: '-' + ($('.div_childAges').height() / 2) + 'px 0 0 -' + ($('.div_childAges').width() / 2) + 'px' });
        },

        closeChildAges: function () {
            $('.div_childAges').html("");
            $('.div_childAges').hide();

            if ($('.div_accomSummary').css('display') == 'none') {
                $('.blackOut').height(0);
                $('.blackOut').width(0);
            }
            else {
                $('.div_accomSummary').css('z-index', 1002);
            }

        }
    }

} ();

CYPRUSPARADISE.searchPackage = function () {
    return {

        swapAccomSummaryTab: function (tab) {
            $('.div_accomSummary_body_tabs_content').hide();
            $('.div_accomSummary_body_tabs_content_' + tab).show();
            $('.accomSummary_tabs').removeClass('active');
            $('.accomSummary_tabs_' + tab).addClass('active');
        },

        getAccomSummary: function (accomID, indexID) {
            var selectedCacheID = $('.div_hid_selected_cacheID_' + indexID + ' input').val();
            var selectedPrice = $('#hid_totalPrice_' + indexID).val();
            var occupancy = $('#hid_occupancy').val();

            $('.blackOut').height(CYPRUSPARADISE.helper.getDocHeight());
            $('.blackOut').width(CYPRUSPARADISE.helper.getDocWidth());


            $('.div_accomSummary').load("/_ajax/SearchAccomSummary.aspx?accommodationID=" + accomID + "&indexID=" + indexID + "&price=" + selectedPrice + "&avgPrice=" + CYPRUSPARADISE.helper.average(selectedPrice, CYPRUSPARADISE.helper.getNoOfPassengers($('#hid_occupancy').val())) + "&searchMode=packages&occupancy=" + occupancy, function () {
                $(this).show(function () {
                });
            });

            $('.div_accomSummary').css({ top: '50%', left: '50%', margin: '-' + ($('.div_accomSummary').height() / 2) + 'px 0 0 -' + ($('.div_accomSummary').width() / 2) + 'px' });

        },

        swapAccomSummaryImage: function (indexID) {
            $('.div_accomSummary_body_tabs_content_photos_large').hide();
            $('.photo_large_' + indexID).show();
        },

        closeAccomSummary: function () {
            $('.div_accomSummary').html("");
            $('.div_accomSummary').hide();
            $('.blackOut').height(0);
            $('.blackOut').width(0);
        },

        toggleRooms: function (indexID) {
            $('.div_roomType_' + indexID).slideToggle(function () {
                if ($(this).css('display') == "none") {
                    //alert('no show');
                    CYPRUSPARADISE.searchPackage.resetRoomSelected(indexID);
                    $('.showRoomWord_' + indexID).html('Room');
                }
                else {
                    $('.showRoomWord_' + indexID).html('Hide');
                }
            });
        },

        resetRoomSelected: function (indexID) {
            var div = $('.div_roomType_' + indexID);
            var input = $(div).find(":radio:first");
            //alert(input.length);
            input.attr('checked', true);
            var defaultCacheID = $('.div_hid_default_cacheID_' + indexID + ' input').val();
            $('.div_hid_selected_cacheID_' + indexID + ' input').val(defaultCacheID);
            var defaultPrice = $('.div_hid_default_totalPrice_' + indexID + ' input').val();
            CYPRUSPARADISE.searchPackage.setPrices(indexID, defaultPrice);
            var defaultRoomType = $('.div_hid_default_roomTypeName_' + indexID + ' input').val();
            CYPRUSPARADISE.searchPackage.setRoomTypes(indexID, defaultRoomType);
            var defaultBasis = $('.div_hid_default_roomBasis_' + indexID + ' input').val();
            CYPRUSPARADISE.searchPackage.setRoomBasis(indexID, defaultBasis);
            $('.showFlight_' + indexID + ' input').val(defaultPrice);
            if ($('.div_flightTable_' + indexID + '_content').html() != "") {
                // if no value then expand
                CYPRUSPARADISE.searchPackage.getAltFlights(indexID);
            }
        },

        roomSelected: function (indexID, cacheID, priceToUpdate, roomTypeToUpdate,boardbasis) {
            $('.div_hid_selected_cacheID_' + indexID + ' input').val(cacheID);
            CYPRUSPARADISE.searchPackage.setPrices(indexID, priceToUpdate);
            CYPRUSPARADISE.searchPackage.setRoomTypes(indexID, roomTypeToUpdate);
            CYPRUSPARADISE.searchPackage.setRoomBasis(indexID, boardbasis);
            $('.showFlight_' + indexID + ' input').val(priceToUpdate);
            if ($('.div_flightTable_' + indexID + '_content').html() != "") {
                $('.div_flightTable_' + indexID + '_content').slideUp();
                $('.showFlightWord_' + indexID).html('Change');
                CYPRUSPARADISE.searchPackage.getAltFlights(indexID);
            }
        },

        toggleAltFlights: function (indexID) {
            if ($('.div_flightTable_' + indexID + '_content').html() == "") {
                // if no value then expand
                CYPRUSPARADISE.searchPackage.getAltFlights(indexID);
            }
            else {
                // if has value then collaspe
                $('.div_flightTable_' + indexID + '_content').slideUp();
                $('.showFlightWord_' + indexID).html('Change');
                $('.div_flightTable_' + indexID + '_content').html("");
                $('.div_hid_selected_cacheID_' + indexID + ' input').val(cacheID);
                CYPRUSPARADISE.searchPackage.setPrices(indexID, $('.showFlight_' + indexID + ' input').val());
            }
            //$('.showFlight_' + indexID).toggleClass("flightExpanded");
        },

        getAltFlights: function (indexID) {
            //alert($('.div_hid_selected_cacheID_' + indexID + ' input').val());
            cacheID = $('.div_hid_selected_cacheID_' + indexID + ' input').val();
            $('.div_flightTable_' + indexID + '_content').load("/_ajax/SearchPackagesAltFlights.aspx?cacheID=" + cacheID + "&indexID=" + indexID, function () {
                $('.div_flightTable_' + indexID + '_content').slideDown();
                $('.showFlightWord_' + indexID).html('Hide');
            });

        },

        flightSelected: function (indexID, cacheID, priceToUpdate) {
            $('.div_hid_selected_cacheID_' + indexID + ' input').val(cacheID);
            CYPRUSPARADISE.searchPackage.setPrices(indexID, priceToUpdate);
        },

        addToBasket: function (indexID, occupancy) {
            var noOfChildren = CYPRUSPARADISE.helper.getNoOfChildren(occupancy);
            var cDOBstr = '';
            if (noOfChildren > 0) {
                cDOBstr = $('#hid_cDOBs').val();
            }

            var c = '',
            c = $('.div_hid_selected_cacheID_' + indexID + ' input').val();
            $('.blackOut').height(CYPRUSPARADISE.helper.getDocHeight());
            $('.blackOut').width(CYPRUSPARADISE.helper.getDocWidth());
            $('.div_content_col2_searchResults').html("<div class='addToBasket'><img src='/_gfx/ajax-loader2.gif' /></div>");
            $("img[src='/_gfx/ajax-loader2.gif']").first().attr('src', '/_gfx/ajax-loader2.gif');

            var s = '/booking/basketadd.aspx'
            s += '?cacheID=' + c;
            s += '&occupancy=' + occupancy;

            if (cDOBstr.length > 0) {
                s += '&cDOBs=' + cDOBstr;
            }
            //  s += '&unit=' + index;


            $.ajax({ url: s, success: function () {
                document.location.href = '/booking/basketview.aspx';
            }
                    , error: function () { alert('error'); }
            });



        

        },

        setPrices: function (indexID, totalPrice) {
            $('#hid_totalPrice_' + indexID).val(totalPrice);
            $('.div_totalPrice_' + indexID).html(CYPRUSPARADISE.helper.setCurrency(totalPrice));
            var occupancy = $('#hid_occupancy').val();
            //alert(CYPRUSPARADISE.helper.getNoOfPassengers(occupancy));
            var noOfPassenger = CYPRUSPARADISE.helper.getNoOfPassengers(occupancy);
            $('.div_avgPrice_' + indexID).html(CYPRUSPARADISE.helper.setCurrency(CYPRUSPARADISE.helper.average(totalPrice, noOfPassenger)));
        },

        setRoomTypes: function (indexID, roomTypeName) {
            $('.spn_RoomTypeName_' + indexID).html(roomTypeName);
        },
        setRoomBasis: function (indexID, basis) {
            $('.spn_BoardBasis_' + indexID).html(basis);
        },

        validateChildDOB: function (indexID) {
            var occupancy = $('#hid_occupancy').val();
            var noOfChildren = CYPRUSPARADISE.helper.getNoOfChildren(occupancy);
            var cDOBstr = '';
            var cDOBs;
            var OK;

            if (noOfChildren > 0) {
                for (var i = 1; i <= noOfChildren; i++) {
                    if (cDOBstr.length > 0) { cDOBstr += ','; }
                    cDOBstr += $('#ChildAges_' + i + '_Day').val() + "/" + $('#ChildAges_' + i + '_Month').val() + "/" + $('#ChildAges_' + i + '_Year').val();
                }
            }

            //alert(cDOBstr);

            cDOBs = cDOBstr.split(',');

            OK = true;

            for (var i = 0; i <= noOfChildren - 1; i++) {
                if (!CYPRUSPARADISE.helper.isDate(cDOBs[i])) {
                    OK = false;
                }
            }

            if (OK) {
                $('#hid_cDOBs').val(cDOBstr);
                CYPRUSPARADISE.searchPackage.addToBasket(indexID, occupancy);
            }
            else {
                alert('DOB not valid');
            }
        },

        getChildAges: function (indexID) {
            var occupancy = $('#hid_occupancy').val();

            if ($('.div_accomSummary').css('display') == 'none') {
                $('.blackOut').height(CYPRUSPARADISE.helper.getDocHeight());
                $('.blackOut').width(CYPRUSPARADISE.helper.getDocWidth());
            }
            else {
                $('.div_accomSummary').css('z-index', 900);
            }

            $('.div_childAges').load("/booking/ajax/ChildAges.aspx?searchMode=packages&occupancy=" + occupancy + "&indexID=" + indexID, function () {
                $(this).show(function () {
                });
            });

            $('.div_childAges').css({ top: '50%', left: '50%', margin: '-' + ($('.div_childAges').height() / 2) + 'px 0 0 -' + ($('.div_childAges').width() / 2) + 'px' });
        },

        closeChildAges: function () {
            $('.div_childAges').html("");
            $('.div_childAges').hide();

            if ($('.div_accomSummary').css('display') == 'none') {
                $('.blackOut').height(0);
                $('.blackOut').width(0);
            }
            else {
                $('.div_accomSummary').css('z-index', 1002);
            }

        }

    };
} ();

$.getDocHeight = function () {
    return Math.max(
        $(document).height(),
        $(window).height(),
    /* For opera: */
        document.documentElement.clientHeight
    );
};

function getTicks() {
    return new Date().getTime();
};
