/* * jQuery UI Autocomplete HTML Extension * [IMPORTANTE] Para que funcione el autocompleter con la function "autoc" */ (function( $ ) { var proto = $.ui.autocomplete.prototype, initSource = proto._initSource; function filter( array, term ) { var matcher = new RegExp( $.ui.autocomplete.escapeRegex(term), "i" ); return $.grep( array, function(value) { return matcher.test( $( "
" ).html( value.label || value.value || value ).text() ); }); } $.extend( proto, { _initSource: function() { if ( this.options.html && $.isArray(this.options.source) ) { this.source = function( request, response ) { response( filter( this.options.source, request.term ) ); }; } else { initSource.call( this ); } }, _renderItem: function( ul, item) { return $( "
  • " ) .data( "item.autocomplete", item ) .append( $( "" )[ this.options.html ? "html" : "text" ]( item.label ) ) .appendTo( ul ); } }); })( jQuery ); /***** INICIACION DE LOS EVENTOS DE LOS AUTOCOMPLETADOS *****/ var TIMEOUT_AUTOCOMPLETER = null; function iniciarAutocompletadoHoteles(inputName, inputValue){ INPUT_NAME_AUTOC = inputName; INPUT_VALUE_AUTOC = inputValue; if( $(INPUT_NAME_AUTOC).attr('ini-event')==1 )return true; $(INPUT_NAME_AUTOC).attr('ini-event',1); // Evento al escribir $(document).on('keyup', INPUT_NAME_AUTOC, function(e){ if( [13,38,40].indexOf(e.keyCode)!=-1 ){ e.preventDefault(); if( e.keyCode==13 )$(INPUT_NAME_AUTOC).blur(); return false; }else if( e.keyCode==9 || e.keyCode==13 ){ $(INPUT_NAME_AUTOC).blur() return false; } if( $(INPUT_NAME_AUTOC).val()!='' && $(INPUT_NAME_AUTOC).val().length>1 ){ $('#scriptBuscador').remove(); if( TIMEOUT_AUTOCOMPLETER )clearTimeout(TIMEOUT_AUTOCOMPLETER); TIMEOUT_AUTOCOMPLETER = setTimeout(function(){ if( $('#scriptBuscador').length )return false; var script = document.createElement('script'); script.type = 'text/javascript'; script.id = 'scriptBuscador'; $("body").append(script); $('#scriptBuscador').attr('src','https://mvieiraviagens.pt/autocomplete.php?callback=autocH&term='+ encodeURIComponent($(INPUT_NAME_AUTOC).val()) +'&l=PT&_='+ (new Date()).getTime()); },200); }else{ $( INPUT_VALUE_AUTOC ).val(''); $( INPUT_VALUE_AUTOC.replace('_value','_type') ).val(''); $('#autocompleteHoteles').remove(); } }); // Evento al sacar el foco del input del autocompleter $(document).on('blur', INPUT_NAME_AUTOC, function(){ setTimeout(function(){ if( $(INPUT_VALUE_AUTOC).val()=='' ){ if( $('.ui-state-hover span').length==1 ) $('.ui-state-hover span').trigger('click'); else $('#autocompleteHoteles li span').first().trigger('click'); } },200); }); } // Establecemos el input del autocompletado (hoteles) $(document).on('focus', '#txtDestino', function(){ iniciarAutocompletadoHoteles('#txtDestino', '#list_id'); }); // Establecemos el input del autocompletado (transfer) if( TIPO_CABECERA_AGENCIA=='Transfer' ){ $(document).on('focus', '#origenIda_name', function(){ iniciarAutocompletadoHoteles('#origenIda_name', '#origenIda_value'); }); $(document).on('focus', '#destinoIda_name', function(){ iniciarAutocompletadoHoteles('#destinoIda_name', '#destinoIda_value'); }); $(document).on('focus', '#origenVuelta_name', function(){ iniciarAutocompletadoHoteles('#origenVuelta_name', '#origenVuelta_value'); }); $(document).on('focus', '#destinoVuelta_name', function(){ iniciarAutocompletadoHoteles('#destinoVuelta_name', '#destinoVuelta_value'); }); } /***** fin INICIACION DE LOS EVENTOS DE LOS AUTOCOMPLETADOS *****/ /** * Función que muestra los resultados del autocompletado de hoteles */ function autocH(arrayHoteles){ $(INPUT_VALUE_AUTOC).val(''); $( INPUT_VALUE_AUTOC.replace('_value','_type') ).val(''); var PESTANA_TRANSFER = ['Transfer'].indexOf(TIPO_CABECERA_AGENCIA)!=-1; var tipoYaSeleccionado = null; if( PESTANA_TRANSFER ){ switch( INPUT_NAME_AUTOC ){ case '#origenIda_name': tipoYaSeleccionado = $('#destinoIda_type').val(); break; case '#destinoIda_name': tipoYaSeleccionado = $('#origenIda_type').val(); break; case '#origenVuelta_name': tipoYaSeleccionado = $('#destinoVuelta_type').val(); break; case '#destinoVuelta_name': tipoYaSeleccionado = $('#origenVuelta_type').val(); break; } } var cadBuscar = normalize( $(INPUT_NAME_AUTOC).val().toLowerCase() ); var cadBuscarLength = cadBuscar.length; // Resultados de hoteles y destinos var htmlHoteles = '', htmlDestinos = ''; for (var i=0, n=arrayHoteles.length; i0 ){ htmlLabel = ( ini>0 ? htmlLabel.substring(0,ini) : '' ) + ""+ htmlLabel.substring(ini,ini+cadBuscarLength) +"" + htmlLabel.substring(ini+cadBuscarLength, htmlLabel.length) } if( tipoYaSeleccionado!="hotel" && tipoYaSeleccionado!="destino" ){ if( tipo=='hotel' ){ htmlHoteles += "
  • " + "" + htmlLabel + "" + "
  • "; }else{ htmlDestinos += "
  • " + "" + htmlLabel + "" + "
  • "; } } } var htmlPaises=''; if( tipoYaSeleccionado!='pais' && PESTANA_TRANSFER && typeof(PAISES_AUTOCOMPLETER)!='undefined' ){ var opcionesEncontradas = 0; $.each(PAISES_AUTOCOMPLETER, function(iPais, pais){ if( opcionesEncontradas>=5 )return true; var name = normalize( pais.nombre.toLowerCase() ); // Si ha escrito el nombre completo de la aerolinea --> la cargamos if( (ini=name.indexOf(cadBuscar))!=-1 ){ htmlPaises += htmlOptionAutocomplete({ tipo: 'pais', id: pais.codigoPais, nombre: pais.nombre.charAt(0).toUpperCase() + pais.nombre.slice(1), ini: ini },cadBuscarLength); opcionesEncontradas ++; } }); } var htmlAeropuertos = ''; if( tipoYaSeleccionado!='vuelo' && PESTANA_TRANSFER && typeof(IATA_AEROPUERTO)!='undefined' ){ var opcionesEncontradas = 0; var aeropuertoEncontradoIATA = null; var cadBuscarUpperCase = cadBuscar.toUpperCase() if( cadBuscar.length==3 && IATA_AEROPUERTO[ cadBuscarUpperCase ] ){ // En transfer no añadimos los IATAs de grupos if( !PESTANA_TRANSFER || !IATA_AEROPUERTO[ cadBuscarUpperCase ].grupo ){ opcionesEncontradas++; htmlAeropuertos += htmlOptionAutocomplete({ tipo: 'vuelo', id: IATA_AEROPUERTO[ cadBuscarUpperCase ].value[0], nombre: IATA_AEROPUERTO[ cadBuscarUpperCase ].name[0], ini: ini },cadBuscarLength); aeropuertoEncontradoIATA = IATA_AEROPUERTO[ cadBuscarUpperCase ]; } } $.each(IATA_AEROPUERTO, function(iAero, aero){ if( opcionesEncontradas>=5 )return true; // En transfer no añadimos los IATAs de grupos if( PESTANA_TRANSFER && aero.grupo )return true; var name = normalize( aero.name[0].toLowerCase() ); // Si ha escrito el nombre completo de la aerolinea --> la cargamos if( (ini=name.indexOf(cadBuscar))!=-1 && ( // FRON-2431 Para evitar meter el aeropuerto del IATA duplicado !aeropuertoEncontradoIATA || (aeropuertoEncontradoIATA.name[0]!=aero.name[0]) ) ){ htmlAeropuertos += htmlOptionAutocomplete({ tipo: 'vuelo', id: aero.value[0], nombre: aero.name[0], ini: ini },cadBuscarLength); opcionesEncontradas ++; } }); } var htmlPuerto = ''; if( tipoYaSeleccionado!='puerto' && PESTANA_TRANSFER && typeof(PUERTOS)!='undefined' ){ var opcionesEncontradas = 0; $.each(PUERTOS, function(iPuerto, puerto){ if( opcionesEncontradas>=5 )return true; var name = normalize( puerto.name.toLowerCase() ); // Si ha escrito el nombre completo de la aerolinea --> la cargamos if( (ini=name.indexOf(cadBuscar))!=-1 ){ htmlPuerto += htmlOptionAutocomplete({ tipo: 'puerto', id: iPuerto, nombre: puerto.name, ini: ini },cadBuscarLength); opcionesEncontradas ++; } }); } var htmlEstaciones = ''; if( tipoYaSeleccionado!='tren' && PESTANA_TRANSFER && typeof(ESTACIONES_TREN)!='undefined' ){ var opcionesEncontradas = 0; $.each(ESTACIONES_TREN, function(iEstacion, estacion){ if( opcionesEncontradas>=5 )return true; var name = normalize( estacion.name.toLowerCase() ); // Si ha escrito el nombre completo de la aerolinea --> la cargamos if( (ini=name.indexOf(cadBuscar))!=-1 ){ htmlEstaciones += htmlOptionAutocomplete({ tipo: 'tren', id: iEstacion, nombre: estacion.name, ini: ini },cadBuscarLength); opcionesEncontradas ++; } }); } var html = '
    ' + '
      ' + (PESTANA_TRANSFER && htmlAeropuertos!=''?'':'') + htmlAeropuertos + (PESTANA_TRANSFER && htmlPuerto!=''?'':'') + htmlPuerto + (PESTANA_TRANSFER && htmlEstaciones!=''?'':'') + htmlEstaciones + (PESTANA_TRANSFER && htmlDestinos!=''?'':'') + htmlDestinos + (PESTANA_TRANSFER && htmlHoteles!=''?'':'') + htmlHoteles; if( !PESTANA_TRANSFER ){ html += (PESTANA_TRANSFER && htmlPaises!=''?'':'') + htmlPaises; } html += "
    "; + "
    "; $('#autocompleteHoteles').remove(); $(INPUT_NAME_AUTOC).parent().append(html); // Parche para que ponga todas las primeras letras en mayusculas $('#autocompleteHoteles li.hoteles span').each( function(){ var elOrig = $(this); var hmtlOrig = elOrig.html(); var elEdit = $(this).clone(); var textOrig = elEdit.text(); if( textOrig.indexOf('(')!=-1 ){ textOrig = textOrig.substring(textOrig.indexOf('('), textOrig.indexOf(')')+1); var textCap = capitalizeAllWord( textOrig ); textCap = textCap.replace('( ', '('); textCap = textCap.replace(' )', ')'); $(this).html( hmtlOrig.split(textOrig).join(textCap) ) } }); } function getIconOptionAutocomplete(tipo){ switch(tipo){ case 'puerto': return 'fa-ship'; case 'vuelo': return 'fa-plane'; case 'pais': return 'fa-flag'; case 'tren': return 'fa-train'; } return ''; } function htmlOptionAutocomplete(opcion, cadBuscarLength){ cadBuscarLength = cadBuscarLength || false; htmlOpcion = "
  • " + "" + " "; // Si pasamos la cadena buscada --> la marcamos en negrita if( cadBuscarLength ){ htmlOpcion += ( opcion.ini>0?opcion.nombre.substring(0,opcion.ini):'' ) + ""+ opcion.nombre.substring(opcion.ini,opcion.ini+cadBuscarLength) +"" + opcion.nombre.substring(opcion.ini+cadBuscarLength, opcion.nombre.length); }else{ htmlOpcion += opcion['nombre'] } htmlOpcion += "" + "
  • "; return htmlOpcion; } /** * Funcíon que muestra todos los destinos de un país */ function mostrarDestinosPaisAutocompleter(iPais, tipo, idInput){ if( $('#cargandoBackground:visible').length )return true; if( typeof(tipo)=='undefined' )tipo='todo'; if( typeof(idInput)=='undefined' )idInput=''; $('#autocompleteHoteles, #autocompleter'+idInput).remove(); var pais = PAISES_AUTOCOMPLETER[iPais]; if( pais && ((tipo=='todo' && pais.destinos && getLengthObject(pais.destinos)) || (tipo=='aeropuertos' && pais.aeropuertos && getLengthObject(pais.aeropuertos))) ){ var html = ''; var html = ''; html += '
    ' + '' + '
    '; $("html").append(html); } } /* * Evento click de un elemento del autocomplete */ function clickAutocompleteHotel(name, value, tipo){ tipo = tipo || false; $(INPUT_NAME_AUTOC).val(name); $(INPUT_VALUE_AUTOC).val(value); $(INPUT_VALUE_AUTOC).trigger("change"); $('#autocompleteHoteles').remove(); $('#cargandoBackground').remove(); $('#cargandoContent').remove(); if( tipo && INPUT_VALUE_AUTOC.indexOf('_value')!=-1 ){ var inputTipo = INPUT_VALUE_AUTOC.replace('_value', '_type'); if( $(inputTipo).length )$(inputTipo).val(tipo) } // Para "Vuelo+Hotel" Comprobamos si el destino tiene descuento de residente if( TIPO_CABECERA_AGENCIA=='Vuelo+Hotel' ){ if( IDMinorista!=="17" )comprobarResidente(); setTimeout(mostrarIconAeropuertosCercanos,50); } } // Autocompletar Fernando: function resetIDs() { $('#txtDestino_id').val(''); if( false ){ $(INPUT_VALUE_AUTOC).val(''); } else { borraList=true; } if( $('#IDE').length ){ $('#IDE').val(''); } // tren $('#estTren_destino_name').val(''); $('#estTren_destino_value').val(''); $('#divEstacionTrenDestino').hide(); } //fin autocompletar