﻿function buscadorPaquetes(name) {
	if (name){
		this.name=name;
	}else{
		this.name="xmlDoc";
	}
	//this.XMLHttpComponent = this.getXMLHttpComponentInstance();
	//this.idioma="es";
	this.diaSalidaStr  = new Array();
    this.diaSalidaDate = new Array();
    this.xmlDocSinVuelo = new XMLRemoteRequest("xmlDocSinVuelo");
    this.xmlDocZonas    = new XMLRemoteRequest("xmlDocZonas");
    this.xmlDocFechas   = new XMLRemoteRequest("xmlDocFechas");
    this.xmlDocEstancias= new XMLRemoteRequest("xmlDocEstancias");
    this.xmlDocDestinos = new XMLRemoteRequest("xmlDocDestinos");
    this.msgSeleccione = "Seleccione";
    this.msgSinVuelo = "Sin Vuelo";
    this.msgMargen = "Mensaje margen";
    this.msgFaltaDest = "Falta destino";
    this.msgFaltaOri = "Falta origen";
    this.msgFaltaFecha = "Falta fecha";
    this.msgFaltaEst = "Falta estancia";
    this.ClaseProducto = "";
    this.Destino = "";
    this.margen = 0;
}
      
     buscadorPaquetes.prototype.disableAll = function (){
        this.diaSalidaStr  = new Array();
        this.diaSalidaDate = new Array();
        document.getElementById('lblSinVuelo').style.display = 'none'
        document.getElementById('cmbOrigen').style.display = 'block'
        nodeRemoveAllChilds('cmbOrigen')
        //document.getElementById('cmbDestino').disabled = true;
        document.getElementById('cmbOrigen').disabled = true;
        document.getElementById('fecini').disabled = true;
        document.getElementById('cmbEstancia').disabled = true;
     }
     
     buscadorPaquetes.prototype.ObtenerDestinos = function (){
       this.disableAll();
       var cmb = document.getElementById('cmbDestino');
       nodeRemoveAllChilds('cmbDestino')
        var opt;
        this.xmlDocDestinos.getRemoteDocument("/include/xmlRequest.ashx?action=epdest&clp=" + this.ClaseProducto + "&y=" + Math.random());
        //<option value="?"><%=Mensaje("Seleccione") %></option>
        opt = document.createElement('option');
        opt.value = "?"; 
        opt.innerHTML = this.msgSeleccione;
        cmb.appendChild(opt);
        var list = this.xmlDocDestinos.selectNodes("xml/zon")
        for (i = 0; i < list.length; i++){
            opt = document.createElement('option');
            opt.value = list[i].getAttribute('cod'); 
            opt.innerHTML = list[i].getAttribute('nom');
            cmb.appendChild(opt);
        }
        document.getElementById('cmbDestino').style.display = 'block';
    }
     
      buscadorPaquetes.prototype.actualizarOrigen = function (){
        var dest = cmbGetValue('cmbDestino');

        this.disableAll();
        nodeRemoveAllChilds('cmbOrigen')
        nodeRemoveAllChilds('cmbEstancia')

        var cmb = document.getElementById('cmbOrigen');

        if (dest != '?') {
            var opt;
            opt = document.createElement('option');
            opt.value = '?'; 
            opt.innerHTML = this.msgSeleccione;
            cmb.appendChild(opt);
            /*this.xmlDocSinVuelo.getRemoteDocument("/include/xmlRequest.ashx?action=epwof&dest=" + dest + "&clp=" + this.ClaseProducto + "&y=" + Math.random());
            
            var sinVueloNode = this.xmlDocSinVuelo.selectSingleNode("xml/result");
            if (sinVueloNode.getAttribute("sinVuelo") == "1") {
                opt = document.createElement('option');
                opt.value = '-1'; 
                opt.innerHTML = this.msgSinVuelo;
                cmb.appendChild(opt);           
            }*/
            this.xmlDocZonas.getRemoteDocument("/include/xmlRequest.ashx?action=epori&dest=" + dest + "&clp=" + this.ClaseProducto + "&y=" + Math.random());
            var list = this.xmlDocZonas.selectNodes("xml/zon");
            //Lo primero es comprobar si nos devuelve sinVuelo, asi lo pintamos en primer lugar            
            for (i = 0; i < list.length; i++){
                if(list[i].getAttribute("cod") == "-1"){
                    opt = document.createElement('option');
                    opt.value = '-1'; 
                    opt.innerHTML = this.msgSinVuelo;
                    cmb.appendChild(opt); 
                }
            }
            if (list.length > 0) {
                for (i = 0; i < list.length; i++){
                    //Aquí ya hemos añadido sinVuelo no hay que volverlo a sacar.
                    if(list[i].getAttribute("cod") != "-1"){
                        opt = document.createElement('option');
                        opt.value = list[i].getAttribute('cod'); 
                        opt.innerHTML = list[i].getAttribute('nom');
                        cmb.appendChild(opt);
                    }
                }
                document.getElementById('lblSinVuelo').style.display = 'none'
                document.getElementById('cmbOrigen').style.display = 'block'
                // Solo hay uno (miramos si es sin vuelo)
                if(list.length == 1){
                    if(list[0].getAttribute('cod') == "-1"){
                        //Es sin vuelo
                        cmbSelectValue('cmbOrigen', '-1');
                        document.getElementById('cmbOrigen').style.display = 'none'
                        document.getElementById('lblSinVuelo').style.display = 'block'
                        this.actualizarFechas();
                    }
                }
            }else{
                /*if (sinVueloNode.getAttribute("sinVuelo") == "0") {
                    opt = document.createElement('option');
                    opt.value = '-1'; 
                    opt.innerHTML = this.msgSinVuelo;
                    cmb.appendChild(opt);           
                }*/
                cmbSelectValue('cmbOrigen', '-1');
                document.getElementById('cmbOrigen').style.display = 'none'
                document.getElementById('lblSinVuelo').style.display = 'block'
                this.actualizarFechas();
            }

            document.getElementById('cmbOrigen').disabled = false;
        }
        
        /*************si solo esta la opcion sin vuelo, no muestra el origen******************/
        if(list[0].getAttribute('cod') == "-1"){
           document.getElementById('cmbOrigen').style.display = 'none'
           document.getElementById('lblSinVuelo').style.display = 'none' 
           document.getElementById('origenNombre').style.display = 'none'
         }else{ 
            document.getElementById('origenNombre').style.display = 'block'
        }
        /*******************************/

        //document.getElementById('cmbDestino').disabled = false;
    }
    
     buscadorPaquetes.prototype.actualizarFechas = function (){
        var ori = cmbGetValue('cmbOrigen');
        var dest = cmbGetValue('cmbDestino');

        //this.disableAll();
        document.getElementById('cmbEstancia').disabled = true;
        
        if (ori != '?') {
            //if (ori == '*') { ori = -1; }
            // Fechas
            this.xmlDocFechas.getRemoteDocument("/include/xmlRequest.ashx?action=epfec&dest=" + dest + "&ori=" + ori + "&clp=" + this.ClaseProducto + "&y=" + Math.random());

            var list = this.xmlDocFechas.selectNodes("xml/fec")

            if (list.length == 0) {
       
            }else{
                for (i = 0; i < list.length; i++) {
                    this.diaSalidaStr[i] = list[i].getAttribute('txt');
                    this.diaSalidaDate[i] = new Date(this.diaSalidaStr[i]);
                }
                this.diaSalidaStr = this.diaSalidaStr.sort();
                this.inicializarFecini();
            }
            this.actualizarEstancias();
            document.getElementById('fecini').disabled = false;
        } else {
        }

        document.getElementById('cmbDestino').disabled = false;
        document.getElementById('cmbOrigen').disabled = false;
    }
    
     buscadorPaquetes.prototype.actualizarEstancias = function (){ 
     // Estancias
         var ori = cmbGetValue('cmbOrigen');
         //if (ori == '*') { ori = -1; }
        var dest = cmbGetValue('cmbDestino');
        var fsalida = document.getElementById('fecini').value;
        
        nodeRemoveAllChilds('cmbEstancia')
        var cmb = document.getElementById('cmbEstancia');
        var opt;
        opt = document.createElement('option');
        opt.value = '?'; 
        opt.innerHTML = this.msgSeleccione;
        cmb.appendChild(opt);
                 
            this.xmlDocFechas.getRemoteDocument("/include/xmlRequest.ashx?action=epest&dest=" + dest + "&ori=" + ori + "&fsal=" + fsalida + "&clp=" + this.ClaseProducto + "&y=" + Math.random());
            var list = this.xmlDocFechas.selectNodes("xml/est")
            for (i = 0; i < list.length; i++){
                opt = document.createElement('option');
                opt.value = list[i].getAttribute('dia');
                opt.innerHTML = list[i].getAttribute('dia');
                cmb.appendChild(opt);
            }
             document.getElementById('cmbEstancia').disabled = false;
     }
    
    
      buscadorPaquetes.prototype.validarFecini = function (){
        var d = this.inputDate('fecini');
        if (!this.isValidDate(d)) {
            this.inicializarFecini();
        }
    }

    buscadorPaquetes.prototype.inicializarFecini = function (){
        var selDay = new Date(this.diaSalidaStr[0]);
        var inpFecini = document.getElementById('fecini');

        inpFecini.value = selDay.getDate() + "/" + (selDay.getMonth()+1) + "/" + selDay.getFullYear();
    }
    
     buscadorPaquetes.prototype.mostrarCalendario = function (id){
        if (this.diaSalidaStr.length == 0) return;
        var inpFecini = document.getElementById('fecini');
        ShowCalendar(inpFecini, inpFecini, null, this.diaSalidaStr[0], this.diaSalidaStr[this.diaSalidaStr.length - 1], this.diaSalidaDate);
    }

     buscadorPaquetes.prototype.isValidDate = function (d){
        if (d == null) return false;
        for (i = 0; i < this.diaSalidaDate.length; i++) {
            if (d.getTime() == this.diaSalidaDate[i].getTime()) return true;
        }
        return false;
    }

   buscadorPaquetes.prototype.validarFormulario = function (){
        var errmsg = '';
        if (cmbGetValue('cmbDestino') == '?') errmsg += '\n ' + this.msgFaltaDest;
        if (cmbGetValue('cmbOrigen') == '?') errmsg += '\n ' + this.msgFaltaOri;
        if (document.getElementById('fecini').value == '') errmsg += '\n ' + this.msgFaltaFecha;
        if (cmbGetValue('cmbEstancia') == '?') errmsg += '\n ' + this.msgFaltaEst;
                
        if (errmsg == ''){
			var arrayFEnt = document.getElementById('fecini').value.split("/");
			var fEnt = new Date(arrayFEnt[2],arrayFEnt[1]-1,arrayFEnt[0]);
			var diaMg = new Date();
			diaMg.setDate(diaMg.getDate() + this.margen);
			diaMg.setHours(0,0,0,0);
			if ((fEnt<diaMg)) {
				errmsg += "* " + this.msgMargen + ".\n";
			}
	    }

        if (errmsg == ''){
            return true;
        }else{
            alert(errmsg);
            return false;
        }
    }

    buscadorPaquetes.prototype.inputDate = function (id){
        var t = document.getElementById(id).value;
	    if(!t.length) return null;
	    t=t.replace(/\s+/g,"");
	    if(t.match(/[^-|\d|\.|\/]/)) return null;
	    var rgt=t.split(/-|\.|\//);
	    for(var i=0;i<rgt.length;i++) rgt[i]=parseInt(rgt[i],10);
	    if(!rgt[1]) return null;
	    var m,d,y;
	    var fmt=GetDateFmt();
	    if(fmt=="yymmdd")
	    {
	    if(!rgt[2]) return null;
	    m=rgt[1];d=rgt[2];y=rgt[0];
	    }
	    else
	    {
	    if(fmt=="mmddyy"){m=rgt[0];d=rgt[1];}
	    else{m=rgt[1];d=rgt[0];}//fmt=="ddmmyy"
	    if(rgt[2])y=rgt[2];
	    else y=DefYr(m-1,d);
	    }
	    m-=1;if(y<100)y+=2000;
	    if(y<1601||y>4500||m<0||m>11||d<1||d>GetMonthCount(m,y))return null;
	    return new Date(y,m,d);
    }

    buscadorPaquetes.prototype.doFecSubmit = function (){
        panelBuscando();
        document.frmMain.accionBuscador.value = 'PaqConf';
        document.frmMain.submit();
    }

    buscadorPaquetes.prototype.preloadFormData = function() {
        if (preloadZonDestino && preloadZonDestino != "-1") {
            cmbSelectValue('cmbDestino', preloadZonDestino);
            this.actualizarOrigen();
            if (preloadZonOrigen && preloadZonOrigen != "-1") {
                cmbSelectValue('cmbOrigen', preloadZonOrigen);
                this.actualizarFechas();
                if (preloadFecini && preloadFecini != "-1") document.getElementById('fecini').value = preloadFecini;
                if (preloadEstancia && preloadEstancia != "-1") cmbSelectValue('cmbEstancia', preloadEstancia);
            } else {
                this.actualizarFechas();
                if (preloadFecini && preloadFecini != "-1") {
                    document.getElementById('fecini').value = preloadFecini;
                    this.actualizarEstancias();
                    if (preloadEstancia && preloadEstancia != "-1") {
                        cmbSelectValue('cmbEstancia', preloadEstancia);
                    }
                }

            }
        }
    }
    
    buscadorPaquetes.prototype.buscarVuelosGdsAlo = function (){
	    if (this.validarFormulario()) {
	        this.doFecSubmit()
		}
		return;
	}
    
     buscadorPaquetes.prototype.modificarDest = function (){
        if ((this.Destino + '')!=''){
            cmbSelectValue('cmbDestino',this.Destino);
            /*document.getElementById("divDestino").style.display = 'none';*/
        }
        return;
    }   
    