﻿function buscadorAlojamientos(name) {
  if (name){
    this.nombre=name;
  }else{
    this.nombre="xmlDoc";
  }
  this.diaSalidaStr  = new Array();
  this.diaSalidaDate = new Array();
  this.xmlDocFechas   = new XMLRemoteRequest("xmlDocFechas");
  this.xmlDocEstancias = new XMLRemoteRequest("xmlDocEstancias");
  this.SelectorFecha = "";
  this.SelectorNoches = "";
  this.SelectorHabitaciones = "";
  this.ClientId = "";
  this.Form = "frmMain";
  this.FormatoFecha = "dd/MM/yyyy";
  this.fechaIni = "";
  this.msgSeleccione = "-";
}
buscadorAlojamientos.prototype.setFechaIni = function(f) {
  this.fechaIni = f;
}

buscadorAlojamientos.prototype.disableAll = function (){
  this.diaSalidaStr  = new Array();
  this.diaSalidaDate = new Array();
  document.getElementById(this.ClientId + '_lblSinVuelo').style.display = 'none'
  document.getElementById(this.ClientId + '_cmbOrigen').style.display = 'block'
  nodeRemoveAllChilds('cmbOrigen')
  document.getElementById(this.ClientId + '_cmbOrigen').disabled = true;
  eval(this.SelectorFecha + '_ActivarDesactivar(false)');
  document.getElementById(this.ClientId + '_cmbEstancia').disabled = true;
}

buscadorAlojamientos.prototype.actualizarFechas = function (){
  this.resetFecini();
  this.xmlDocEstancias.getRemoteDocument("/rAlojamientos/include/ctlBuscadorAlojamiento/xmlRequest.ashx?action=fechasTurisferr&idAlo=" + document.getElementById(this.ClientId + '_codATF').value);
  var list = this.xmlDocEstancias.selectNodes("xml/fec")
  if (list.length == 0) {
      eval(this.SelectorFecha + "_ActualizarDiasActivos(new Array())");   
  }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();
    document.getElementById(this.SelectorFecha + "_campoFecha").value = '';
    eval(this.SelectorFecha + "_ActualizarDiasActivos(this.diaSalidaDate)");   
    eval(this.SelectorFecha + '_ActivarDesactivar(true)');
    document.getElementById(this.SelectorFecha + "_functionOnClose").value = this.nombre + ".actualizarEstancias()";
    this.inicializarFecini('');
  }
}
    
buscadorAlojamientos.prototype.actualizarEstancias = function (){ 
  var fsalida = document.getElementById(this.SelectorFecha + "_campoFecha").value;
  //if (!this.isValidDate(fsalida)) return false;
  nodeRemoveAllChilds(this.SelectorNoches + '_noches');
  var cmb = document.getElementById(this.SelectorNoches + "_noches");
  var opt;
  opt = document.createElement('option');
  opt.value = '?'; 
  opt.innerHTML = this.msgSeleccione;
  cmb.appendChild(opt);
  opt = document.createElement('option');
  this.xmlDocFechas.getRemoteDocument("/rAlojamientos/include/ctlBuscadorAlojamiento/xmlRequest.ashx?action=estanciasTurisferr&idAlo=" + document.getElementById(this.ClientId + '_codATF').value + "&fsal=" + fsalida + "&y=" + Math.random());
  var list = this.xmlDocFechas.selectNodes("xml/est")
  for (var 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);
  }
  if (cmb.length > 1 ) cmb.selectedIndex = 1;
  cmb.disabled = false;
  eval(this.SelectorNoches + "_selFec.CalcularDiasFecha('dias',true)");
}

//*********************************************************************//
buscadorAlojamientos.prototype.preloadFormData = function (){
  this.actualizarFechas();
  if (this.fechaIni != '') document.getElementById(this.SelectorFecha + "_campoFecha").value = this.fechaIni;
  this.actualizarEstancias();
}

buscadorAlojamientos.prototype.resetFecini = function (){
  this.diaSalidaStr  = new Array();
  this.diaSalidaDate = new Array();
}

buscadorAlojamientos.prototype.inicializarFecini = function (){
  var selDay = new Date(this.diaSalidaStr[0]);
  var inpFecini = document.getElementById(this.SelectorFecha + "_campoFecha");
  inpFecini.value = selDay.getDate() + "/" + (selDay.getMonth()+1) + "/" + selDay.getFullYear();
}

    
buscadorAlojamientos.prototype.validarFecini = function (){
  var d = document.getElementById(this.SelectorFecha + "_campoFecha").value;
  if (!this.isValidDate(d)) {
    this.inicializarFecini();
  }
}

buscadorAlojamientos.prototype.isValidDate = function (d){
  if (d == null || d == '') return false;
  var df = this.convertirFecha(d);
  for (i = 0; i < this.diaSalidaDate.length; i++) {
    if (df.getTime() == this.diaSalidaDate[i].getTime()) return true;
  }
  return false;
}

buscadorAlojamientos.prototype.convertirFecha = function (fecha){
    return StringToDate(fecha,this.FormatoFecha);
}
