﻿$(document).ready(function () {

    function isBrowserAccepted() {
        if (navigator.userAgent.toLowerCase().search("firefox") > -1) {
            index = navigator.userAgent.toLowerCase().indexOf("firefox");
            if (index > 0) {
                var version = parseFloat(navigator.userAgent.toLowerCase().substring(index + 8));
                if (version >= 4)
                    return false;
            }
        }
        if (navigator.userAgent.toLowerCase().search("iphone") > -1)
            return false;
        if (navigator.userAgent.toLowerCase().search("ipad") > -1)
            return false;
        if (navigator.userAgent.toLowerCase().search("palm") > -1)
            return false;
        if (navigator.userAgent.toLowerCase().search("blackberry") > -1)
            return false;
        if (navigator.userAgent.toLowerCase().search("windows ce") > -1)
            return false;
        if (navigator.userAgent.toLowerCase().search("windows ce") > -1)
            return false;
        if (navigator.userAgent.toLowerCase().search("android") > -1)
            return false;
        if (navigator.userAgent.toLowerCase().search("webkit") > -1 && (navigator.userAgent.toLowerCase().search("series60") > -1 || navigator.userAgent.toLowerCase().search("symbian") > -1))
            return false;

        //True = enable, False = disable
        return true;
    }

    function showOrHide(from) {

        if (!isBrowserAccepted()) {
            $('#cbxExpedia').attr('checked', false);
            $('#cbxHotwire').attr('checked', false);
            $('#cbxTravelocity').attr('checked', false);
            $('#cbxHotels').attr('checked', false);
            $("#compare-to").hide();
            return;
        }

        //Enable just Expedia, no checkbox
        if ($("#ctc-hotelscheap-search-form-destination").length > 0 && $("#ctc-hotelscheap-search-form-destination").length > 0 && $("#ctc-hotelscheap-search-form-destination").val().length > 0 && from == "check-in") {
            $('#cbxExpedia').attr('checked', true);
            $('#cbxHotwire').attr('checked', false);
            $('#cbxTravelocity').attr('checked', false);
            $('#cbxHotels').attr('checked', false);
            $("#compare-to").hide();
            return;
        }


        if ($("#ctc-hotelscheap-search-form-destination").length > 0 && $("#ctc-hotelscheap-search-form-destination").val().length > 0 && from == "check-in")
            $("#compare-to").show();
        else
            $("#compare-to").hide();
    }

    function launchCompareTo(url) {

        var months = {
            "01": "Jan",
            "02": "Feb",
            "03": "Mar",
            "04": "Apr",
            "05": "May",
            "06": "Jun",
            "07": "Jul",
            "08": "Aug",
            "09": "Sep",
            "10": "Oct",
            "11": "Nov",
            "12": "Dec"
        };

        var address = { "Hotels.com": "http://www.jdoqocy.com/click-4397609-10780389?url=http://www.hotels.com/PPCSearch?",
            "Expedia": "http://www.expedia.com/pubspec/scripts/eap.asp?GOTO=HOTSEARCH&mdpcid=meta.hotelhotline.Hotel.Frontdoor&",
            "Travelocity": "http://travel.travelocity.com/hotel/HotelSearch.do?Service=CJUS&searchMode=city&expr_path=Y&",
            "Hotwire": "http://click.linksynergy.com/fs-bin/statform?id=B3nlcpZnGOQ&offerid=120349&bnid=1334&subid=0&bid=B378456&inputId=index&"
        };

        url = url.replace("http://reservations.hotelscheap.org/search-bridge/?", "");
        var urlTokens = url.split("&");

        for (i = 0; i < urlTokens.length; i++) {
            var key = urlTokens[i].substring(0, urlTokens[i].indexOf("="));
            var value = urlTokens[i].substring(urlTokens[i].indexOf("=") + 1, urlTokens[i].length);

            switch (key) {
                case "destination":
                    var destinationTokens = value.split("%2C%20");

                    var city = destinationTokens[0];
                    var state = '';
                    var country = '';

                    if (destinationTokens.length == 2) {
                        country = destinationTokens[1];
                    }
                    if (destinationTokens.length == 3) {
                        state = destinationTokens[1];
                        country = destinationTokens[2];
                    }

                    if (city.length > 0) {
                        address["Hotels.com"] += "city=" + city + "&";
                        address["Expedia"] += "CityName=" + city + "&";
                        if (state.length > 0)
                            address["Hotwire"] += "destCity=" + city + ", " + state + "&";
                        else
                            address["Hotwire"] += "destCity=" + city + "&";
                    }

                    if (country.length > 0)
                        address["Hotels.com"] += "country=" + country + "&";

                    address["Travelocity"] += "city=" + city + "&cityCountryCode=" + country + "&";
                    if (state.length > 0)
                        address["Travelocity"] += "state=" + state + "&";

                    break;
                case "destination-id":
                    break;
                case "check-in": //mm dd yyyy
                    var dateTokens = value.split("/");
                    var dmyDate = dateTokens[1] + "/" + dateTokens[0] + "/" + dateTokens[2];

                    address["Hotels.com"] += "arrivalDate=" + dmyDate + "&";
                    address["Expedia"] += "InDate=" + value + "&";
                    address["Hotwire"] += "startMonth=" + dateTokens[0] + "&startDay=" + dateTokens[1] + "&";
                    address["Travelocity"] += "dateLeavingMonth=" + months[dateTokens[0]] + "&dateLeavingDay=" + dateTokens[1] + "&";

                    break;
                case "check-out":
                    var dateTokens = value.split("/");
                    var dmyDate = dateTokens[1] + "/" + dateTokens[0] + "/" + dateTokens[2];

                    address["Hotels.com"] += "departureDate=" + dmyDate + "&";
                    address["Expedia"] += "OutDate=" + value + "&";
                    address["Hotwire"] += "endMonth=" + dateTokens[0] + "&endDay=" + dateTokens[1] + "&";
                    address["Travelocity"] += "dateReturningMonth=" + months[dateTokens[0]] + "&dateReturningDay=" + dateTokens[1] + "&";

                    break;
                case "rooms": //4

                    address["Hotels.com"] += "numberOfRooms=" + value + "&";
                    address["Expedia"] += "NumRoom=" + value + "&";

                    break;
                case "room-breakdown": //2,1|3,0|1,1|2,2 or 2,0|2,0
                    var breakdownTokens = value.split("|");

                    var apr = "adultsPerRoom=";
                    var cpr = "childrenPerRoom=";
                    var ta = 0;
                    var tc = 0;

                    var tot = 0;

                    for (j = 0; j < breakdownTokens.length; j++) {
                        apr += breakdownTokens[j].substring(0, 1) + ",";
                        cpr += breakdownTokens[j].substring(2, 3) + ",";
                        ta += parseInt(breakdownTokens[j].substring(0, 1), 10);
                        tc += parseInt(breakdownTokens[j].substring(2, 3), 10);

                        address["Expedia"] += "NumAdult" + (j + 1) + "=" + breakdownTokens[j].substring(0, 1) + "&";
                    }

                    apr = apr.slice(0, apr.length - 1);
                    cpr = cpr.slice(0, cpr.length - 1);

                    address["Hotels.com"] += apr + "&";
                    address["Hotels.com"] += cpr + "&";
                    address["Travelocity"] += "adults=" + ta + "&children=" + tc + "&";

                    break;
                case "children-breakdown": //2||2|2,1
                    var cb = value;

                    while (cb.indexOf("||") > -1)
                        cb = cb.replace("||", "|");

                    cb = cb.replace("|", ",");

                    if (cb.length > 0)
                        address["Hotels.com"] += "childAgesPerRoom=" + cb + "&";

                    break;
                default:
                    break;
            }
        }

        address["Hotels.com"] = address["Hotels.com"].slice(0, address["Hotels.com"].length - 1);
        address["Expedia"] = address["Expedia"].slice(0, address["Expedia"].length - 1);
        address["Hotwire"] = address["Hotwire"].slice(0, address["Hotwire"].length - 1);
        address["Travelocity"] = address["Travelocity"].slice(0, address["Travelocity"].length - 1);

        //encoding
        address["Travelocity"] = "http://www.kqzyfj.com/click-4397609-10595024?sid=&url=" + encodeURIComponent(address["Travelocity"]);

        var numberOfCheckedWindows = (($("#cbxHotels:checked").val() !== undefined) ? 1 : 0) + (($("#cbxTravelocity:checked").val() !== undefined) ? 1 : 0) + (($("#cbxExpedia:checked").val() !== undefined) ? 1 : 0) + (($("#cbxHotwire:checked").val() !== undefined) ? 1 : 0);
        var space = 10;
        var left = 0;
        var width = screen.width / numberOfCheckedWindows - space;

        var maxWidth = screen.width * .5;
        if (maxWidth < width)
            width = maxWidth;

        var height = screen.height * .95;
        var otherOptions = ',resizable=yes,scrollbars=yes,toolbar=yes,location=yes,status=yes';
        var top = 0;

        if ($("#cbxExpedia:checked").val() !== undefined) {
            //var winExpedia = window.open(address["Expedia"], 'winExpedia', 'left=' + left + ',top=' + top + ',width=' + width + ',height=' + height + otherOptions).blur();
            //This is for the behind popup only
            var winExpedia = window.open(address["Expedia"], 'winExpedia', 'left=' + (maxWidth * 1.55) + ',top=' + (height * .2) + ',width=' + (maxWidth * .35) + ',height=' + (height * .65) + otherOptions).blur();
            window.focus();
            left = left + width + space;
        }
        if ($("#cbxHotwire:checked").val() !== undefined) {
            var winHotwire = window.open(address["Hotwire"], 'winHotwire', 'left=' + left + ',top=' + top + ',width=' + width + ',height=' + height + otherOptions).blur();
            window.focus();
            left = left + width + space;
        }
        if ($("#cbxTravelocity:checked").val() !== undefined) {
            var winTravelocity = window.open(address["Travelocity"], 'winTravelocity', 'left=' + left + ',top=' + top + ',width=' + width + ',height=' + height + otherOptions).blur();
            window.focus();
            left = left + width + space;
        }
        if ($("#cbxHotels:checked").val() !== undefined) {
            var winHotels = window.open(address["Hotels.com"], 'winHotels', 'left=' + left + ',top=' + top + ',width=' + width + ',height=' + height + otherOptions).blur();
            window.focus();
            left = left + width + space;
        }
    }



    $("#ctc-hotelscheap-search-form-destination").blur(function () {
        showOrHide("destination");
    });
    $("#ctc-hotelscheap-search-form-check-in").blur(function () {
        showOrHide("check-in");
    });

    showOrHide("main");


    $.ctcCalendar({
        calendarCount: 2,
        inputType: "text",
        dateSelector: "#ctc-hotelscheap-search-form-check-in",
        expanderSelector: "#ctc-hotelscheap-search-form-check-in-expander",
        endDateSelector: "#ctc-hotelscheap-search-form-check-out",
        endExpanderSelector: "#ctc-hotelscheap-search-form-check-out-expander",
        allowRange: true,
        allowBlankDates: true,
        defaultDate: "blank",
        defaultRange: 1,
        maximumRange: 28,
        autoSetEndDate: true
    });

    setTimeout(function () {
        $("#ctc-hotelscheap-search-form-children-1,#ctc-hotelscheap-search-form-children-2,#ctc-hotelscheap-search-form-children-3,#ctc-hotelscheap-search-form-children-4,#ctc-hotelscheap-search-form-children-5,#ctc-hotelscheap-search-form-children-6,#ctc-hotelscheap-search-form-children-7,#ctc-hotelscheap-search-form-children-8").val("0").change();
        $("#ctc-hotelscheap-search-form-adults-1,#ctc-hotelscheap-search-form-adults-2,#ctc-hotelscheap-search-form-adults-3,#ctc-hotelscheap-search-form-adults-4,#ctc-hotelscheap-search-form-adults-5,#ctc-hotelscheap-search-form-adults-6,#ctc-hotelscheap-search-form-adults-7,#ctc-hotelscheap-search-form-adults-8").val("2").change();
        $("#ctc-hotelscheap-search-form-rooms").val("1").change();
    }, 100);

    $("#ctc-hotelscheap-search-form-destination").autocomplete("/get-autosuggest-results/", {
        delay: 0,
        minChars: 3,
        matchContains: true,
        matchSubset: false,
        max: 100,
        scrollHeight: 450,
        width: 450,
        formatItem: function (row) {
            var result_array = row[0].split("^");
            if (result_array[0] == "city")
                return '<small class="city">City</small> ' + result_array[result_array.length - 1];
            else if (result_array[0] == "airport")
                return '<small class="airport">Airport</small> ' + result_array[result_array.length - 1];
            else if (result_array[0] == "landmark")
                return '<small class="landmark">Landmark</small> ' + result_array[result_array.length - 1];
            else if (result_array[0] == "hotel")
                return '<small class="hotel">Hotel</small> ' + result_array[result_array.length - 1];
            else
                return result_array[result_array.length - 1];
        },
        formatMatch: function (row) {
            var result_array = row[0].split("^");
            return result_array[result_array.length - 1];
        },
        formatResult: function (row) {
            var result_array = row[0].split("^");
            return result_array[result_array.length - 1];
        }
    }).result(function (event, row) {
        $("#ctc-hotelscheap-search-form-autosuggest-city-id,#ctc-hotelscheap-search-form-autosuggest-landmark-id,#ctc-hotelscheap-search-form-autosuggest-hotel-id,#ctc-hotelscheap-search-form-autosuggest-original-text").val();

        var result_array = row[0].split("^");
        if (result_array[0] == "city")
            $("#ctc-hotelscheap-search-form-autosuggest-city-id").val(result_array[1]);
        else if (result_array[0] == "landmark" || result_array[0] == "airport") {
            $("#ctc-hotelscheap-search-form-autosuggest-city-id").val(result_array[1]);
            $("#ctc-hotelscheap-search-form-autosuggest-landmark-id").val(result_array[2]);
        }
        else if (result_array[0] == "hotel")
            $("#ctc-hotelscheap-search-form-autosuggest-hotel-id").val(result_array[1]);

        showOrHide("destination-list");

        $("#ctc-hotelscheap-search-form-autosuggest-original-text").val(result_array[result_array.length - 1]);
    });

    $("#ctc-hotelscheap-search-form-rooms").change(function () {
        var children_position, number_of_children;
        var number_of_rooms = parseInt($(this).val(), 10);
        $("#ctc-hotelscheap-search-form-room-2-row").css("display", number_of_rooms >= 2 ? "block" : "none");
        $("#ctc-hotelscheap-search-form-room-3-row").css("display", number_of_rooms >= 3 ? "block" : "none");
        $("#ctc-hotelscheap-search-form-room-4-row").css("display", number_of_rooms >= 4 ? "block" : "none");
        $("#ctc-hotelscheap-search-form-room-5-row").css("display", number_of_rooms >= 5 ? "block" : "none");
        $("#ctc-hotelscheap-search-form-room-6-row").css("display", number_of_rooms >= 6 ? "block" : "none");
        $("#ctc-hotelscheap-search-form-room-7-row").css("display", number_of_rooms >= 7 ? "block" : "none");
        $("#ctc-hotelscheap-search-form-room-8-row").css("display", number_of_rooms >= 8 ? "block" : "none");
        if (number_of_rooms == "9")
            GroupBooking();
    });

    $("#ctc-hotelscheap-search-form-children-1,#ctc-hotelscheap-search-form-children-2,#ctc-hotelscheap-search-form-children-3,#ctc-hotelscheap-search-form-children-4,#ctc-hotelscheap-search-form-children-5,#ctc-hotelscheap-search-form-children-6,#ctc-hotelscheap-search-form-children-7,#ctc-hotelscheap-search-form-children-8").change(function () {
        var children_position, number_of_children;
        children_position = this.id.substring(this.id.length - 1);
        number_of_children = $(this).val().length == 1 ? parseInt($(this).val(), 10) : 0;
        $("#ctc-hotelscheap-search-form-child-" + children_position + "-1").css("display", number_of_children >= 1 ? "block" : "none");
        $("#ctc-hotelscheap-search-form-child-" + children_position + "-2").css("display", number_of_children >= 2 ? "block" : "none");
        $("#ctc-hotelscheap-search-form-child-" + children_position + "-3").css("display", number_of_children >= 3 ? "block" : "none");
    });

    function submit_to_priceline() {
        var new_url = "";
        if ($("#ctc-hotelscheap-search-form-autosuggest-hotel-id").val().length > 0 && $("#ctc-hotelscheap-search-form-autosuggest-original-text").val() == $("#ctc-hotelscheap-search-form-destination").val())
            new_url = "http://secure.hotelscheap.org/hotel/";
        else {
            new_url = "http://secure.hotelscheap.org/city/";
            //City Specific
            new_url += "?rs_city=Chicago, IL";
        }

        new_url += "&rs_chk_in=" + escape($("#ctc-hotelscheap-search-form-check-in").val());
        new_url += "&rs_chk_out=" + escape($("#ctc-hotelscheap-search-form-check-out").val());
        new_url += "&rs_rooms=" + (1 + $("#ctc-hotelscheap-search-form-room-2-row:visible,#ctc-hotelscheap-search-form-room-3-row:visible,#ctc-hotelscheap-search-form-room-4-row:visible,#ctc-hotelscheap-search-form-room-5-row:visible,#ctc-hotelscheap-search-form-room-6-row:visible,#ctc-hotelscheap-search-form-room-7-row:visible,#ctc-hotelscheap-search-form-room-8-row:visible").length)

        //launchCompareTo(new_url);

        document.location = new_url;
    };

    function submit_to_travelocity() {
        var new_url = "";
        if ($("#ctc-hotelscheap-search-form-autosuggest-hotel-id").val().length > 0 && $("#ctc-hotelscheap-search-form-autosuggest-original-text").val() == $("#ctc-hotelscheap-search-form-destination").val())
            new_url = "http://reserve.hotelscheap.org/nexres/search/search_results.cgi";
        else {
            new_url = "http://reserve.hotelscheap.org/nexres/search/search_results.cgi";
            //City Specific
            new_url += "?src=10028877&sort_order=TopPicks&avail=Y&tab=tab0&t1_addr=&t3_landmark=&city=San+Francisco&state=CA&country=US";
        }

        var doa_mm; var doa_dd; var doa_yy; var dod_mm; var dod_dd; var dod_yy;
        doa_mm = escape($("#ctc-hotelscheap-search-form-check-in").val()).split("/")[0];
        doa_dd = escape($("#ctc-hotelscheap-search-form-check-in").val()).split("/")[1];
        doa_yy = escape($("#ctc-hotelscheap-search-form-check-in").val()).split("/")[2];
        dod_mm = escape($("#ctc-hotelscheap-search-form-check-out").val()).split("/")[0];
        dod_dd = escape($("#ctc-hotelscheap-search-form-check-out").val()).split("/")[1];
        dod_yy = escape($("#ctc-hotelscheap-search-form-check-out").val()).split("/")[2];


        new_url += "&doa_mm=" + doa_mm;
        new_url += "&doa_dd=" + doa_dd;
        new_url += "&doa_yy=" + doa_yy;
        new_url += "&dod_mm=" + dod_mm;
        new_url += "&dod_dd=" + dod_dd;
        new_url += "&dod_yy=" + dod_yy;

        new_url += "&num_rooms=" + (1 + $("#ctc-hotelscheap-search-form-room-2-row:visible,#ctc-hotelscheap-search-form-room-3-row:visible,#ctc-hotelscheap-search-form-room-4-row:visible,#ctc-hotelscheap-search-form-room-5-row:visible,#ctc-hotelscheap-search-form-room-6-row:visible,#ctc-hotelscheap-search-form-room-7-row:visible,#ctc-hotelscheap-search-form-room-8-row:visible").length)

        //hard coded for now

        new_url += "&num_adults=2&num_children=0";
        
        //launchCompareTo(new_url);

        document.location = new_url;
    };

    submit_search_form = function () {
        if ($("#ctc-hotelscheap-search-form-destination").val().length == 0) {
            alert("Please enter a city, airport or landmark");
            return false;
        }

        if ($("#ctc-hotelscheap-search-form-destination").val().toLowerCase() == "chicago, il, usDISABLED") {
            //submit_to_priceline();
            //return false;
        }

        if ($("#ctc-hotelscheap-search-form-destination").val().toLowerCase() == "san francisco, ca, usDISABLED") {
            //submit_to_travelocity();
            //return false;
        }

        var children_are_valid = true;
        $("#ctc-hotelscheap-search-form-child-1-1,#ctc-hotelscheap-search-form-child-1-2,#ctc-hotelscheap-search-form-child-1-3,#ctc-hotelscheap-search-form-child-2-1,#ctc-hotelscheap-search-form-child-2-2,#ctc-hotelscheap-search-form-child-2-3,#ctc-hotelscheap-search-form-child-3-1,#ctc-hotelscheap-search-form-child-3-2,#ctc-hotelscheap-search-form-child-3-3,#ctc-hotelscheap-search-form-child-4-1,#ctc-hotelscheap-search-form-child-4-2,#ctc-hotelscheap-search-form-child-4-3,#ctc-hotelscheap-search-form-child-5-1,#ctc-hotelscheap-search-form-child-5-2,#ctc-hotelscheap-search-form-child-5-3,#ctc-hotelscheap-search-form-child-6-1,#ctc-hotelscheap-search-form-child-6-2,#ctc-hotelscheap-search-form-child-6-3,#ctc-hotelscheap-search-form-child-7-1,#ctc-hotelscheap-search-form-child-7-2,#ctc-hotelscheap-search-form-child-7-3,#ctc-hotelscheap-search-form-child-8-1,#ctc-hotelscheap-search-form-child-8-2,#ctc-hotelscheap-search-form-child-8-3").each(function () {
            if ($(this).is(":visible") && $(this).val().length == 0) {
                alert("You must enter the age of child " + this.id.substring(36, 37) + " in room " + this.id.substring(34, 35));
                children_are_valid = false;
                return false;
            }
        });
        if (!children_are_valid)
            return false;

        var new_url = "";
        if ($("#ctc-hotelscheap-search-form-autosuggest-hotel-id").val().length > 0 && $("#ctc-hotelscheap-search-form-autosuggest-original-text").val() == $("#ctc-hotelscheap-search-form-destination").val())
            new_url = "http://reservations.hotelscheap.org/hotel-bridge/" + String($("#ctc-hotelscheap-search-form-autosuggest-hotel-id").val()) + "/?";
        else {
            new_url = "http://reservations.hotelscheap.org/search-bridge/";
            new_url += "?destination=" + escape($("#ctc-hotelscheap-search-form-destination").val());
            if ($("#ctc-hotelscheap-search-form-autosuggest-city-id").val().length > 0 && $("#ctc-hotelscheap-search-form-autosuggest-original-text").val() == $("#ctc-hotelscheap-search-form-destination").val())
                new_url += "&destination-id=" + escape($("#ctc-hotelscheap-search-form-autosuggest-city-id").val());
            if ($("#ctc-hotelscheap-search-form-autosuggest-landmark-id").val().length > 0 && $("#ctc-hotelscheap-search-form-autosuggest-original-text").val() == $("#ctc-hotelscheap-search-form-destination").val())
                new_url += "&sort=distance&nearby-landmarks=" + escape($("#ctc-hotelscheap-search-form-autosuggest-landmark-id").val());
            else if ($("#LandmarkDestId").val() != null)
                new_url += "&sort=distance&nearby-landmarks=" + escape($("#LandmarkDestId").val());


            //temp ipad fix
            if (navigator.userAgent.toLowerCase().search("ipad") > -1)
                new_url += "&m=0";
        }
        new_url += "&check-in=" + escape($("#ctc-hotelscheap-search-form-check-in").val());
        new_url += "&check-out=" + escape($("#ctc-hotelscheap-search-form-check-out").val());
        new_url += "&rooms=" + (1 + $("#ctc-hotelscheap-search-form-room-2-row:visible,#ctc-hotelscheap-search-form-room-3-row:visible,#ctc-hotelscheap-search-form-room-4-row:visible,#ctc-hotelscheap-search-form-room-5-row:visible,#ctc-hotelscheap-search-form-room-6-row:visible,#ctc-hotelscheap-search-form-room-7-row:visible,#ctc-hotelscheap-search-form-room-8-row:visible").length)
        new_url += "&room-breakdown=";
        $("#ctc-hotelscheap-search-form-adults-1,#ctc-hotelscheap-search-form-adults-2,#ctc-hotelscheap-search-form-adults-3,#ctc-hotelscheap-search-form-adults-4,#ctc-hotelscheap-search-form-adults-5,#ctc-hotelscheap-search-form-adults-6,#ctc-hotelscheap-search-form-adults-7,#ctc-hotelscheap-search-form-adults-8").each(function () {
            if ($(this).is(":visible")) {
                if (this.id.substring(35, 36) != "1")
                    new_url += "|";
                new_url += $("#ctc-hotelscheap-search-form-adults-" + this.id.substring(35, 36)).val();
                new_url += ",";
                new_url += $("#ctc-hotelscheap-search-form-children-" + this.id.substring(35, 36)).val();
            }
        });
        new_url += "&children-breakdown=";
        $("#ctc-hotelscheap-search-form-children-1,#ctc-hotelscheap-search-form-children-2,#ctc-hotelscheap-search-form-children-3,#ctc-hotelscheap-search-form-children-4,#ctc-hotelscheap-search-form-children-5,#ctc-hotelscheap-search-form-children-6,#ctc-hotelscheap-search-form-children-7,#ctc-hotelscheap-search-form-children-8").each(function () {
            if ($(this).is(":visible")) {
                if (this.id.substring(37, 38) != "1")
                    new_url += "|";
                if ($("#ctc-hotelscheap-search-form-child-" + this.id.substring(37, 38) + "-1").is(":visible"))
                    new_url += $("#ctc-hotelscheap-search-form-child-" + this.id.substring(37, 38) + "-1").val();
                if ($("#ctc-hotelscheap-search-form-child-" + this.id.substring(37, 38) + "-2").is(":visible")) {
                    new_url += ",";
                    new_url += $("#ctc-hotelscheap-search-form-child-" + this.id.substring(37, 38) + "-2").val();
                }
                if ($("#ctc-hotelscheap-search-form-child-" + this.id.substring(37, 38) + "-3").is(":visible")) {
                    new_url += ",";
                    new_url += $("#ctc-hotelscheap-search-form-child-" + this.id.substring(37, 38) + "-3").val();
                }
            }
        });

        launchCompareTo(new_url);

        document.location = new_url;

        return false;
    };

    $("#ctc-hotelscheap-search-form-submit").click(submit_search_form);
    $("#ctc-hotelscheap-search-form").submit(submit_search_form);
});

