console.log("Loader loaded."); var request_url = "https://ctescullos.bookinweb.roomonline.es/bookin-web/box-content/?lang=es&design=inline&target=_blank"; $("#bookinweb").html("Loading Bookinweb..."); console.log("Requesting " + request_url); var jqxhr = $.ajax(request_url) .done(function(data) { $("#bookinweb").html(data); $(".bookinweb_form").addClass("design-inline"); $(".bookinweb_form form").attr('target', '_blank'); $(document).ready(function(){ ajax_success(); }); }) .fail(function(jqXHR, textStatus, error) { $("#bookinweb").html("Error: " + error + textStatus); }) .always(function() { //alert( "complete" ); }); function ajax_success(){ /* Si la carga del html via ajax ha ido bien, se ejecuta esta función */ //Mover css $('.bookinweb_form link, .bookinweb_form script').appendTo("head"); update_allocations(); $(".bookinweb_form select").on("change", update_allocations); // Bind destination_hotel select to silently modify hidden fields hotel and destination $('.bookinweb_form select[name="destination_hotel"]').change(function() { var option = $(this).find('option:selected').eq(0); var destination = $(option).attr('data-destination'); var hotel = option.attr('data-hotel'); $('.bookinweb_form input[name="dest"]').val(destination); $('.bookinweb_form input[name="hotel"]').val(hotel); }); /* Controlar que date_to no sea inferior a date_from */ $(".bookinweb_form form").submit(function(){ console.log("submit"); var error_msg = null; var date_from = $(".bookinweb_form input[name=date_from]").val(); var date_to = $(".bookinweb_form input[name=date_to]").val(); if (date_to <= date_from) { error_msg = "La fecha desde no puede ser inferior a la fecha hasta"; } if (error_msg) { alert(error_msg); } }); } //Puede ser que al modificar la lista de posibles options, el valor previamente seleccionado no esté dentro de la lista. En ese caso seleccionaremos un valor más cercano. function ensure_value_is_on_the_list(selector) { $.each($(selector), function (index, value) { var v = parseInt($(this).find('option:selected').val()); var option_exists = $(this).find('option[value=' + v + ']').not(':disabled').length; if (!option_exists) { var last_value = parseInt($(this).find('option').not(':disabled').last().val()); var first_value = parseInt($(this).find('option').not(':disabled').first().val()); if (v > last_value) { $(this).val(last_value); //Si el valor no es valido porque es demasiado alto, seleccionar el mas alto disponible $(this).trigger('change'); } else if (v < first_value) { $(this).val(first_value); //Si el valor no es valido porque es demasiado bajo, seleccionar el mas bajo disponible $(this).trigger('change'); } } }); } function set_max_adults(max) { // console.log('set_max_adults('+max+')'); $.each($('.adults_selection select option'), function (index, value) { var v = parseInt($(this).attr('value')); if (v <= max) $(this).removeAttr('disabled').show(); else $(this).attr('disabled', 'disabled').hide(); }); ensure_value_is_on_the_list('.select_adults'); } function set_max_children(max) { // console.log('set_max_children('+max+')'); $.each($('.children_selection select option'), function (index, value) { var v = parseInt($(this).attr('value')); if (v <= max) $(this).attr('disabled', false).show(); else $(this).attr('disabled', true).hide(); }); ensure_value_is_on_the_list('.select_children'); } function set_max_babies(max) { $.each($('.select_babies option'), function (index, value) { var v = parseInt($(this).attr('value')); if (v <= max) $(this).attr('disabled', false).show(); else $(this).attr('disabled', true).hide(); }); ensure_value_is_on_the_list('.select_babies'); } function set_children_ages_range(min, max) { // console.log('set_children_ages_range('+min+','+max+')'); $.each($('.select_children_age option'), function (index, value) { var v = parseInt($(this).attr('value')); if ((min <= v) && (v <= max)) $(this).attr('disabled', false).show(); else $(this).attr('disabled', true).hide(); }); ensure_value_is_on_the_list('.select_children_age'); } function update_allocations() { // console.log("update_allocations"); /* // Script encargado de la gestión de los allocation del formulario de búsqueda de booking de CBEDS: Fragments Booking.LineForm, Booking.BoxForm, Booking.Step0 y calendario // Como Hotel y Destino pueden tener maximo de adultos, de children, min y max de edad de niño, etc. hay que modificarlo dinamicamente por javascript, segun se seleccione un hotel/destino u otro. */ var o = $(".bookinweb_form select[name=destination_hotel]").find("option:selected"); var max_adults = parseInt(o.data('max-adults')); var max_children = parseInt(o.data('max-childs')); var max_babies = parseInt(o.data('max-babies')); var children_min_age = parseInt(o.data('children-min-age')); var children_max_age = parseInt(o.data('children-max-age')); set_max_adults(max_adults); set_max_children(max_children); set_max_babies(max_babies); set_children_ages_range(children_min_age, children_max_age); //Mostrar solamente el numero de habitaciones seleccionadas var no_hab = parseInt($(".bookinweb_form select[name=no_hab]").val()); $(".bookinweb_form .room").hide(); $(".bookinweb_form .room").slice(0,no_hab).show(); //Mostrar solamente los campos de edad niño segun el numero de niños seleccionado $(".bookinweb_form .room").each(function(){ $(this).find(".age_selection").hide(); var no_ch = parseInt($(this).find(".children_selection select").val()); $(this).find(".age_selection").slice(0,no_ch).show(); }); }