﻿function buscadorPaquetes(name) {
  if (name){
    this.name = name;
  }else{
    this.name = "xmlDoc";
  }
  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.msgCualquiera = "Cualquiera"
  this.ClaseProducto = "";
  this.idPaquete = "";
  this.tipoPaquete = "";
  this.Destino = "";
  this.margen = 0;
  this.datosCynsa = true;
  this.datosSoltour = true;
  this.datosSpecialTours = true;
  this.datos5Estrellas = true;
  this.datosLocalPre = true;
  this.datosLocalDin = true;
  this.datosLocalEst = true;
}


buscadorPaquetes.prototype.datosProveedores = function() {
    var str = "";
    if (this.datosSoltour == false) {
        str += "&dsoltour=0";
    }
    if (this.datosCynsa == false) {
        str += "&dcynsa=0";
    }
    if (this.datosSpecialTours == false) {
        str += "&dspecialtour=0";
    }
    if (this.datos5Estrellas == false) {
        str += "&d5estrellas=0";
    }
    if (this.datosLocalPre == false) {
        str += "&dlocalpre=0";
    }
    if (this.datosLocalDin == false) {
        str += "&dlocaldin=0";
    }
    if (this.datosLocalEst == false) {
        str += "&dlocalest=0";
    }
    return str;
}

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('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;
  var url = "/include/xmlRequest.ashx?";
  url += "action=epdest";
  url += "&clp=" + this.ClaseProducto;
  url += "&id=" + this.idPaquete;
  url += this.datosProveedores();
  url += "&y=" + Math.random();
  //alert(url)
  this.xmlDocDestinos.getRemoteDocument(url);
  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);
    var url = "/include/xmlRequest.ashx?";
    url += "action=epori";
    url += "&clp=" + this.ClaseProducto;
    url += "&id=" + this.idPaquete;
        url += this.datosProveedores();
    url += "&y=" + Math.random();
    //alert(url);
    this.xmlDocZonas.getRemoteDocument(url);
    var list = this.xmlDocZonas.selectNodes("xml/zon");
    //Lo primero es comprobar si nos devuelve sinVuelo, asi lo pintamos en primer lugar  
    for (var 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); 
        cmbSelectValue('cmbOrigen', '-1');
      }
    }
    if (list.length > 0) {
        for (var 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);
            cmbSelectValue('cmbOrigen', '-1');
          }
        }
        document.getElementById('lblSinVuelo').style.display = 'block'
        cmb.style.display = 'block'
        // Solo hay uno (miramos si es sin vuelo)
        if(list.length >= 1){
          if(cmb.options[cmb.selectedIndex].value == "-1"){
            //Es sin vuelo
            cmbSelectValue('cmbOrigen', '-1');
            if (list.length == 1){
              cmb.style.display = 'none'
              document.getElementById('lblSinVuelo').style.display = 'none'
            }
            this.actualizarFechas();
          }
        }
    }else{
      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 = 'none'
      this.actualizarFechas();
    }
    document.getElementById('cmbOrigen').disabled = false;
  }
}
    
buscadorPaquetes.prototype.actualizarFechas = function (){
  var ori = cmbGetValue('cmbOrigen');
  var dest = cmbGetValue('cmbDestino');
  document.getElementById('cmbEstancia').disabled = true;
  if (ori == '?') ori = "-1"
  if (dest != '?') {
    this.resetFecini();
    var url = "/include/xmlRequest.ashx?"
    url += "action=epfec";
    url += "&dest=" + dest;
    url += "&ori=" + ori;
    url += "&clp=" + this.ClaseProducto;
    url += "&mrg=" + this.margen;
    url += "&id=" + this.idPaquete;
    url += "&tipo=" + this.tipoPaquete;
        url += this.datosProveedores();
    url += "&y=" + Math.random();
    //alert(url);
    this.xmlDocFechas.getRemoteDocument(url);
    var list = this.xmlDocFechas.selectNodes("xml/fec")
    if (list.length == 0) {
      //
    }else{
      for (var 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;
  }
  document.getElementById('cmbDestino').disabled = false;
  document.getElementById('cmbOrigen').disabled = false;
}
    
 buscadorPaquetes.prototype.actualizarEstancias = function (){ 
  // Estancias
  var ori = cmbGetValue('cmbOrigen');
  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);
  if (ori == '?') ori = "-1"
  if (dest != "?") {
    var url = "/include/xmlRequest.ashx?";
    url += "action=epest";
    url += "&dest=" + dest;
    url += "&ori=" + ori;
    url += "&fsal=" + fsalida;
    url += "&clp=" + this.ClaseProducto;
    url += "&id=" + this.idPaquete;
    url += this.datosProveedores();
    url += "&y=" + Math.random();
    //alert(url);
    this.xmlDocFechas.getRemoteDocument(url);
    var list = this.xmlDocFechas.selectNodes("xml/est")
    if (list.length > 0) {
      //Si el primero de la lista es 0, quiere decir que vale cualquiera
      for (var i = 0; i < list.length; i++){
       opt = document.createElement('option');
       opt.value = list[i].getAttribute('dia');
       // Si viene el cero es que vale cualquier duración.
       if (opt.value == "0"){
        opt.innerHTML =  this.msgCualquiera;
       }else{
        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.resetFecini = function (){
  this.diaSalidaStr  = new Array();
  this.diaSalidaDate = new Array();
}

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 = 'PaqTour';
  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;
        this.actualizarEstancias();
        if (preloadEstancia && preloadEstancia!="-1"){
          cmbSelectValue('cmbEstancia', preloadEstancia);
        }
      }
    }else{
      //sin vuelo
      this.actualizarFechas();
      if (preloadFecini && preloadFecini!="-1"){
        //document.getElementById('fecini').value = preloadFecini; // Lo he comentado porque da problemas con la ficha
        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);
  }
  return;
}   