﻿/*
=======================================================================================
 HOTELES RH                                                     Site produced by KOALAB        
 Copyright 2007                                                   http://www.koalab.com
=======================================================================================
*/
$(document).ready(function() {

    var destinationsAll = $("#ctl00_Content_cblPromoDestinations tr input:checkbox");
    var catAll = $("#ctl00_Content_cblPromoTags tr input:checkbox");
    var url = document.location.href;

    url.match(/\?(.+)$/);
    var params = RegExp.$1;
    params = params.split("&");
    var queryStringList = {};

    if (params.length > 1) {


        for (var i = 0; i < params.length; i++) {
            var tmp = params[i].split("=");
            queryStringList[tmp[0]] = unescape(tmp[1]);
        }
        for (var i in queryStringList) {

            if (i == "destinations") {
                var _destinations = ["0", "1", "2"];
                for (var j = 0; j < _destinations.length; j++) {
                    if (queryStringList[i].indexOf(_destinations[j]) > -1) {
                        destinationsAll[j].checked = true;
                    }
                }
            }
            if (i == "tags") {
                
                var _tags = new Array();
                catAll.each(function(i, obj) {
                    _tags[i] = $(obj).parent().attr('rel');
                });
 
                //var _tags = ["Spa", "Golf", "Playa", "Familia", "Fines de Semana"];

                for (var j = 0; j < _tags.length; j++) {
                    if (queryStringList[i].indexOf(_tags[j]) > -1) {
                        catAll[j].checked = true;
                    }
                }


            }
        }
    }
    else {
        for (var i = 0; i < destinationsAll.length; i++) {
            destinationsAll[i].checked = true;
        }
        for (var i = 0; i < catAll.length; i++) {
            catAll[i].checked = true;
        }
    }

});
function clickOffersFilter() {

        var destination = null;
        var destinationsArray =$("#ctl00_Content_cblPromoDestinations tr input:checked");
        
        if(destinationsArray.length <= 0)
        {
            alert('Debe seleccionar al menos un destino, por favor.');
            return false;
        }
        
        for (var i=0; i< destinationsArray.length; i++)
        {
              destination = destination + ',' + ($(destinationsArray[i]).attr("id").replace("ctl00_Content_cblPromoDestinations_",""));
        }

        var cat = null;
 
        $("#ctl00_Content_cblPromoTags td input:checked").each(function(obj, i) {
            cat += ',' + $(i).parent().attr('rel');
        });
         
        if (destination == null)
            destination = "null,";
        if (cat == null)
            cat = "null,";

        document.location.href="/content/promotions/default.aspx?startson=" + $("#ctl00_Content_tbSincePreference").val().replace('/','-').replace('/','-') 
                                + "&expireson=" + $("#ctl00_Content_tbForPreference").val().replace('/','-').replace('/','-')
                                + "&destinations=" + destination.replace("null,","") + "&tags=" + cat.replace("null,","");
                                
    }
