﻿// JScript File
function BuscadorAlo(nombre) {

    this.name = nombre
    this.clientID = "";
    this.funcCambiarZona = "";
    this.funcCopiarFecha = "";
    this.fechaIni = "";
    this.fechaFin = "";
    this.inicio = "";
    this.fin = "";
    this.idZon = "";
    this.nZona = "";
    this.nHabitaciones = "";
    this.nombreHotel = "";
    this.categoria = "";
    this.regimen = "";
    this.distancia = "";
    this.errorZone = "";
    this.errorArribalDate = "";
    this.errorDepartureDate = "";
    this.errorArribalDeparturaDate = "";
    this.errorLess = "";
    this.errorMore = "";
    this.msgMargen = "";
    this.msgChildren = "";
    this.msgAdults = "";
    this.session = "";
    this.errorCab = "";
    
    this.copiarFecha = function () {
//        if (this.inicio.value != this.fechaIni) {
//			this.fin.value = this.inicio.value;
//		}   
    }
    
    this.cambiarZona = function (id,nombre) {
        selectRadio(document.frmMain.idZon2,id);
	    this.idZon.value=id;
	    this.nZona.value=nombre;
	}
         
    
    this.CalcularDiasFecha = function (origen) {
    
         if (!CambiandoFecha) {
	        CambiandoFecha = 1;
	        if (origen == "fecha") {
	            var arrayFEnt = this.inicio.value.split("/");
		        var fEnt = new Date(arrayFEnt[2],arrayFEnt[1]-1,arrayFEnt[0]);
    		        
		        var arrayFSal = this.fin.value.split("/");
		        var fSal = new Date(arrayFSal[2],arrayFSal[1]-1,arrayFSal[0]);
    		        
                var tiempoRestante = fSal.getTime() - fEnt.getTime();
                var dias = Math.floor(tiempoRestante / (1000 * 60 * 60 * 24))-1;
		        document.frmMain.nDiasFiltro.selectedIndex = dias;
            } else if (origen == "dias") {
                var dias = document.frmMain.nDiasFiltro.selectedIndex;
		        var arrayFEnt = this.inicio.value.split("/");
		        var fEnt = new Date(arrayFEnt[2],arrayFEnt[1]-1,arrayFEnt[0]);
		        var fSal = new Date(fEnt.getTime() + (dias+1)*24*60*60*1000);
		        var d = fSal.getDate();
		        var year = fEnt.getYear();
		        if (year < 1900) {
		            year += 1900;
		       }       
		       this.fin.value = ((d < 10) ? '0' + d : d) + "/" + (fSal.getMonth()+1) + "/" + year;
            }  
            CambiandoFecha = 0;   
        }
        
    }
    
    
    this.comprobarCampos = function() {
    	
    	var error = "";
		if (document.getElementById('FechasHab_nZona')!= null){
		    if (document.getElementById('FechasHab_nZona').value==""){ error+="* " + this.errorZone + ".\n"}
		}
		
		this.CalcularDiasFecha("fecha");
		
		if (!validaFecha_ddmmyyyy (document.frmMain.FechasHab_fIni_fecha.value))
			error += "* " + this.errorArribalDate + ".\n";

		if (!validaFecha_ddmmyyyy (document.frmMain.FechasHab_fFin_fecha.value))
			error += "* " + this.errorDepartureDate + ".\n";

		if (error == "") {
			var arrayFEnt = document.frmMain.FechasHab_fIni_fecha.value.split("/");
			var fEnt = new Date(arrayFEnt[2],arrayFEnt[1]-1,arrayFEnt[0]);
			var arrayFSal = document.frmMain.FechasHab_fFin_fecha.value.split("/");
			var fSal = new Date(arrayFSal[2],arrayFSal[1]-1,arrayFSal[0]);
			var diaMg = new Date();
			/*diaMg.setDate(diaMg.getDate() + <%=margen%>);*/
			diaMg.setHours(0,0,0,0);


			if ((fEnt<diaMg)) {
				error += "* " + this.msgMargen + ".\n";
			}
			if (fSal<=fEnt) {
				error += "* " + this.errorArribalDeparturaDate + ".\n";
			}
			if ((fSal-fEnt)/60/60/24/1000>30) {
				error += "* " + this.errorLess + ".\n";
			}
		}

		if (error != "")
		{
			error = this.errorCab + "\n\n" + error;
			alert(error);
			return false;
		} else {
			return true;
		}
    
    }
    
    
    this.buscarAlojamientos = function()
	{
		if (this.comprobarCampos()) {
			panelBuscan.execu();
			//document.frmMain.accion.value="Buscar";
			document.frmMain.accionBuscador.value="Alo";
			document.frmMain.submit();
			//document.frmMain.accion.value="";
			return true;
		}
		return false;
	}
    
    
    this.getClearBuscador = function() {
        return this.session;
    }
    
    
    this.onOpcionesDesplegadas_handler = function() {
        this.nombreHotel.value = '';
        this.categoria.selectedIndex = 0;
        this.regimen.selectedIndex = 0;
        this.distancia.selectedIndex = 0;   
    }
    
    
    this.pintarTablaDatos = function () {
    	var objPanel;
		var str = "";
		var indice;
		var numeroHabitaciones;
		
		objPanel = document.getElementById("panelListboxesCombinaciones");
		numeroHabitaciones = this.nHabitaciones.value;
		if (numeroHabitaciones == "") {
			alert(this.msgMore);
			this.nHabitaciones.item(0).selected = true;
			numeroHabitaciones = 1;
		}
		indice = 1;
		
		str += "<table class='ninos' width='230px' cellSpacing='0' cellPadding='0' border='0'>";
	
		while (indice <= numeroHabitaciones)
		{
		    str += 	"<tr>";
		    str += 		"<td class='comb01' width='20px'>" + indice + ")</td>";		    
		    str += 		"<td class='comb02' width='70px'>" + this.msgAdults + "</td>";
		    str += 		"<td class='comb03' width='70px'>" + this.msgChildren + "</td>";
		    str += 		"<td class='comb04' width='70px'><div id='dvTitol" + indice + "' style='display:none; padding:3px; 0px 0px 0px'>Edad niños</div></td>";
		    str += 	"</tr>";
			str += 	"<tr>";
			str +=      "<td class='comb01'></td>";
			str += 		"<td class='comb02' valign='top'>";
			str += 			"<select name='nAdultosHab_" + indice + "_Filtro' id='nAdultosHab_" + indice + "_Filtro'>";
			str += 				"<option value='1'>1</option>";
			str += 				"<option value='2' selected>2</option>";
			str += 				"<option value='3'>3</option>";
			str += 				"<option value='4'>4</option>";
			str += 				"<option value='5'>5</option>";
			str += 				"<option value='6'>6</option>";
			str += 				"<option value='7'>7</option>";
			str += 				"<option value='8'>8</option>";
			str += 				"<option value='9'>9</option>";
			str += 				"<option value='10'>10</option>";
			str +=				"</select>";
			str += 		"</td>";
			str += 		"<td class='comb03' valign='top'>";
			str += 			"<select name='nNinosHab_" + indice + "_Filtro' id='nNinosHab_" + indice + "_Filtro' onchange='" + this.name + ".pintarNinos(" + indice + ");'>";
			str += 				"<option value='0'>0</option>";
			str += 				"<option value='1'>1</option>";
			str += 				"<option value='2'>2</option>";
			str += 				"<option value='3'>3</option>";
			str += 				"<option value='4'>4</option>";
			str +=			"</select>";
			str += 		"</td>";
			str += 		"<td class='comb04' valign='top'>";
			str +=          "<div class='comb04' id='dvninos" + indice + "'></div>";
			str += 		"</td>";			
			str += 	"</tr>";
			
			indice++;
		}
		str += "</table>";
		
		objPanel.innerHTML = str;					
		return true;   
    } 
    
    
	this.pintarNinos = function(i) {
		var objPanel;
		var str="";
		var indice;
		var nnins;
				
		objPanel = document.getElementById("dvninos" + i);
		nnins = document.frmMain.elements["nNinosHab_" + i + "_Filtro"].value;
		
		var tit = 0;
		var nhab = this.nHabitaciones.value;
		for (indice = 1; indice <= nhab; indice++){
			if (document.frmMain.elements["nNinosHab_" + indice + "_Filtro"].value>0) tit++;
		}
		if (tit>0)
			document.getElementById("dvTitol"+i).style.display = "block" ;
		else
			document.getElementById("dvTitol"+i).style.display = "none" ;
		indice = 1;
		
		while (indice <= nnins)		{
			str += 	"<select name='ed" +i+ indice + "'>";
			str += 		"<option value='0'>0</option>";
			str += 		"<option value='1'>1</option>";
			str += 		"<option value='2'>2</option>";
			str += 		"<option value='3'>3</option>";
			str += 		"<option value='4'>4</option>";
			str += 		"<option value='5' selected>5</option>";
			str += 		"<option value='6'>6</option>";
			str += 		"<option value='7'>7</option>";
			str += 		"<option value='8'>8</option>";
			str += 		"<option value='9'>9</option>";
			str += 		"<option value='10'>10</option>";
			str += 		"<option value='11'>11</option>";
			str += 		"<option value='12'>12</option>";
			str += 		"<option value='13'>13</option>";
			str += 		"<option value='14'>14</option>";
			str += 		"<option value='15'>15</option>";
			str += 		"<option value='16'>16</option>";
			str +=	"</select><br/>";
				
			indice++;
		}
		objPanel.innerHTML = str;
		return true;
	}
    
    this.pintarListboxesCombinaciones = function() {
		this.pintarTablaDatos(); //escribe las filas con 2ad y 0ni
		with (window.document.frmMain) {
			for (var j=1; j<thabits.length; j++) {
			    if(this.nHabitaciones.value>=j){ /**/
				    elements["nAdultosHab_" + j + "_Filtro"].value = thabits[j][0];
				    elements["nNinosHab_" + j + "_Filtro"].value = thabits[j][1];
				    this.pintarNinos(j);
				    for (var i=1; i<=thabits[j][1]; i++) { //i=1..nnins
					    elements["ed" + j+i].value = thabits[j][i+1];
				    }
				}
			}
		} //end with
    }
          
          
    this.verOpcionesAvanzadas = function() {
        
        var Opc = document.getElementById('OpcAv');
	    var disp = "none";
	    var dispInv = "block";
		        
	    if (Opc.style.display == "none") {
		  disp = "block";
		  dispInv = "none";
	    }
	    else
		   this.onOpcionesDesplegadas_handler();

	    Opc.style.display = disp;
	    document.getElementById('CerrarOpcAv').style.display = dispInv;
	    document.getElementById('tdOpcAv').style.display = dispInv;		     
    
    }
    
}

