﻿
function selectorPredictivo(){
        /*
        PeriodoEntrePulsaciones
           Indica el periodo en milisegundos que debe pasar sin que se pulse una tecla
           para lanzar la búsqueda (así evitamos que si alguien escriba muy rápido lance
           muchas peticiones AJAX en poco tiempo)
        MinCaracteres
           Número mínimo de caracteres que se deben introducir en el buscador para que
           comience a buscar
        MostrarMensajeBuscando
           Indica si se debe mostrar un mensaje de "Buscando..." mientras se lanza la petición
           AJAX y se espera la respuesta. Si la búsqueda es muy rápida, quizás convenga desactivar
           el mensaje.
        */
    
    this.name = "";
    this.PeriodoEntrePulsaciones = 10;
    this.MinCaracteres = 3;
    this.MostrarMensajeBuscando = true; 
   
    this.ContenedorResultados = null; 
    this.ContenedorSelectorResultados = null;
    this.SelectorResultados = null; 
    this.InputNombre = null;
    this.InputValue = null;
    this.SpanSinResultados = null;
    this.SpanNoMinCar = null;
    this.SpanBuscando = null;
    this.xmlDocTextName = null;

   
    this.ValorSeleccionado = "";
    this.idioma = "es";
    this.onlyAirport = 0;
    this.onlyHotel = 0;
    this.onlyPackage = 0;
    this.onlyCar = 0;
    this.onlyCar = 0;
    this.onlyService = 0;
    this.onlyTransfer = 0;
    this.usarCLP = 1;
    this.subProduct = "";


    
    
    this.nombreValorSeleccionado = "";
    this.xmlDocText = null;
    this.numPulsaciones = 0;
    this.ResultsSel = false;

}



selectorPredictivo.prototype.nodeSelect = function(node) {

        selectorPredictivo.call
        this.SelectorResultados.value = node.ID.substr(2);
        var nodo = node;
        var str = "";
        while (nodo) {
            if (nodo.ID.length > 3) str = ", " + str + nodo.Text ;
            nodo = nodo.ParentNode;
        }
        this.InputNombre.value = str.substr(2);
        this.ocuDiv();
}
        // option:
        //  1 - Mostrar resultados
        //  2 - Mostrar sin resultados
        //  3 - Mostrar min caracteres
        //  4 - Mostrar buscando
        //  10 - Ocultar todo

selectorPredictivo.prototype.ToggleAutoComplete = function(option) {
    this.ContenedorResultados.style.display = '';

    var x = this.InputNombre.offsetLeft; //coloca el div
    var y = this.InputNombre.offsetTop + this.InputNombre.offsetHeight;
    var parent = this.InputNombre;
    while (parent.offsetParent) {
        parent = parent.offsetParent;
        x += parent.offsetLeft;
        y += parent.offsetTop;
    }

    y = y + 1;
    this.ContenedorResultados.style.left = x + 'px';
    this.ContenedorResultados.style.top = y + 'px';
    this.ContenedorResultados.style.display = '';

    this.SelectorResultados.style.display = 'none';
    this.SpanSinResultados.style.display = 'none';
    this.SpanNoMinCar.style.display = 'none';
    this.SpanBuscando.style.display = 'none';
    if (1 == option) {
        this.SelectorResultados.style.display = '';
    }
    else if (2 == option) {
        this.SpanSinResultados.style.display = '';
    }
    else if (3 == option) {
        this.SpanNoMinCar.style.display = '';
    }
    else if (4 == option) {
        this.SpanBuscando.style.display = '';
    }
    else if (10 == option) {
        this.ContenedorResultados.style.display = 'none';
    }
}

    selectorPredictivo.prototype.actualizar = function() {
        var list = this.xmlDocText.selectNodes("/results/result");
        var i;
        this.SelectorResultados.options.length = 0;
        for (i = 0;i < list.length;i++) {
            this.SelectorResultados.options[i] = new Option(list[i].childNodes[0].nodeValue, list[i].attributes[0].value)
        }
        if (this.SelectorResultados.options.length == 0) {
            this.ToggleAutoComplete(2);
        }
        else {
            this.ToggleAutoComplete(1);
        }
    }


    selectorPredictivo.prototype.verDiv = function(Obj) {
        if (!Obj || Obj.value.length >= this.MinCaracteres) {
            this.numPulsaciones += 1;
            setTimeout(this.name + ".lanzarPet()", this.PeriodoEntrePulsaciones);
        }
    }

    selectorPredictivo.prototype.lanzarPet = function()  {
        if ( this.numPulsaciones == 1) {
           this.numPulsaciones = 0;
            this.mostrarDiv()
        }else {
            this.numPulsaciones -= 1;
        }
    }





    selectorPredictivo.prototype.mostrarDiv = function(objTxt) {
        var val;
        if (this.xmlDocText) {
            this.xmlDocText = null;
            if (typeof CollectGarbage != 'undefined') {
                CollectGarbage();
            }
        }
        this.xmlDocText = new XMLRemoteRequest(this.name + ".xmlDocText");
        val = this.InputNombre.value
        if (val == " ") {
            this.SelectorResultados.style.display = 'None';
            this.cargarDatos();
        }
        else {
            if (this.nombreValorSeleccionado != this.InputNombre.value) {
                if (this.InputNombre.value.length >= this.MinCaracteres) {
                    this.xmlDocText.async = true;
                    if (this.MostrarMensajeBuscando) this.ToggleAutoComplete(4);
                    this.xmlDocText.onreadystatechange = new Function(this.name + ".lanzarCargarDatos()");
                    var url = "/include/ctlSelectorPredictivoZona/xmlRequestSelectorZona.ashx?val=" + encodeURI(val.replace(/^\s*|\s*$/g, ""))
                    var now = new Date();
                    url += "&f=" + now.getDay + "_" + now.getMonth + "_" + now.getTime;
                    url += "&idioma=" + this.idioma;
                    url += "&onlyAirport=" + this.onlyAirport;
                    url += "&onlyHotel=" + this.onlyHotel;
                    url += "&onlyPackage=" + this.onlyPackage;
                    url += "&onlyCar=" + this.onlyCar;
                    url += "&onlyService=" + this.onlyService;
                    url += "&onlyTransfer=" + this.onlyTransfer;
                    url += "&subProducto=" + this.subProduct;
                    url += "&usarCLP=" + this.usarCLP;
                    this.xmlDocText.getRemoteDocument(url);
                }
                else {
                    this.ContenedorSelectorResultados.style.display = '';
                    this.cargarDatos();
                }
            }
            else {
                if (objTxt) {
                    if (objTxt.value.length >= 0) {
                        this.ToggleAutoComplete(3);
                    } else {
                        this.ocuDiv();
                    }
                }
            }
        }
    }

   selectorPredictivo.prototype.lanzarCargarDatos = function() {
        this.nombreValorSeleccionado = this.InputNombre.value;
        this.actualizar();
        this.ContenedorSelectorResultados.style.display = '';
        this.cargarDatos();
    }

    selectorPredictivo.prototype.cargarDatos = function() {

        //        var eL = 0;
        //        var eT = 0;
        //        for (p = this.InputNombre; p && p.tagName != 'BODY'; p = p.offsetParent) {
        //            eL += p.offsetLeft;
        //            eT += p.offsetTop;
        //        }
        //        var eH = this.InputNombre.offsetHeight;
        //        var dH = this.ContenedorResultados.style.pixelHeight;
        //        var sT = document.body.scrollTop;
        //        this.ContenedorResultados.style.left = eL;
        //        if (eT - dH >= sT && eT + eH + dH > document.body.clientHeight + sT)
        //            this.ContenedorResultados.style.top = eT - dH;
        //        else
        //            this.ContenedorResultados.style.top = eT + eH;
        //        this.ContenedorResultados.style.display = '';
    }


    
    selectorPredictivo.prototype.ocuDiv = function() {
        this.ContenedorResultados.style.display = 'none';
        this.ContenedorSelectorResultados.style.display = 'none';
    }

    selectorPredictivo.prototype.camDiv = function () {
        if (this.ContenedorResultados.style.display != '') {
            this.verDiv(null);
        }
        else {
           this.ocuDiv();
        }
    }

    selectorPredictivo.prototype.detectaTecla = function(objTxt, e) {
        var tecla = (document.all) ? e.keyCode : e.which;
        if (tecla == 40) { // abajo
            var obj = this.SelectorResultados;
            if (obj != undefined && obj.options != undefined && obj.options.selectedIndex < obj.options.length - 1) {
                var IndSel = obj.options.selectedIndex;
                if (obj.options.selectedIndex > 0 && obj.options[obj.options.selectedIndex].selected) {
                    obj.options[obj.options.selectedIndex].selected = false;
                }
                obj.options[IndSel + 1].selected = true;
            }
        }
        else if (tecla == 38) { // arriba
            var obj = this.SelectorResultados;
            if (obj != undefined && obj.options != undefined && obj.options.selectedIndex > 0) {
                var IndSel = obj.options.selectedIndex;
                if (obj.options.selectedIndex < obj.options.length - 1 && obj.options[obj.options.selectedIndex].selected) {
                    obj.options[obj.options.selectedIndex].selected = false;
                }
                obj.options[IndSel - 1].selected = true;
            }
        }
        else if (tecla == 13) { // intro
            // Nada que hacer, lo controla la función controlaTecla()
        }
        else if (tecla == 27) { // escape
            this.ocuDiv();
        }
        else {
            this.ValorSeleccionado = '';
            this.InputValue.value = '';
            if (objTxt.value.length >= this.MinCaracteres) {
                this.verDiv(objTxt);
            }
            else if (objTxt.value.length >= 0) {
                this.ToggleAutoComplete(3);
            }
            else {
                this.ocuDiv();
            }
        }
        return false;
    }

    selectorPredictivo.prototype.controlaTecla = function(e) {
        var tecla = (document.all) ? e.keyCode : e.which;
        if (tecla == 13) { // intro
            var obj = this.SelectorResultados;
            if (obj != undefined && obj.options != undefined && obj.options.selectedIndex > -1) {
                this.establecerValor();
                tecla = 0;
                return false;
            }
        }
        return true;
    }

    selectorPredictivo.prototype.establecerValor = function() {
        
        var obj = this.SelectorResultados;
        this.ValorSeleccionado = obj.options[obj.selectedIndex].value;
        this.InputValue.value = this.ValorSeleccionado;
        this.InputNombre.value = obj.options[obj.selectedIndex].text;
        this.ocuDiv();
    }

    // Si hacemos click en los resultados no desaparece el autocomplete
    // Si hacemos click fuera de los resultados desaparece el autocomplete

   selectorPredictivo.prototype.selectInput = function(objTxt) {
        if (objTxt.value.length < this.MinCaracteres)
            this.ToggleAutoComplete(3);
        else  {
            this.nombreValorSeleccionado = '';
            this.mostrarDiv(objTxt);
        }
    }

    selectorPredictivo.prototype.deselectInput = function(objTxt) {
        if (! this.ResultsSel) {
            this.ToggleAutoComplete(10)
        }
    }

    selectorPredictivo.prototype.OnResults = function() {
        this.ResultsSel = true;
    }

    selectorPredictivo.prototype.OutResults = function() {
       this.ResultsSel = false;
    }

