// JavaScript Document
function cambioTipoBusqueda(combo){
	var comboHab = document.getElementById('habitaciones');
	var divTexto = document.getElementById('tablaTextoHabitaciones');
	var seleccionado = 1;
	
	for(var x = 0; x < combo.length; x++){
		if(combo.options[x].selected){
			seleccionado = x+1;
		}
	}
	
	
	
	divTexto.style.visibility='visible';
	
	comboHab.style.visibility='visible';
	
	if(seleccionado==3){
		for(var x = 0; x < comboHab.length; x++){
			if(x==0){
				comboHab.options[x].selected = true;
			}else{
				comboHab.options[x].selected = false;
			}
		}
		recalcularCantidad(comboHab);
		comboHab.style.visibility='hidden';
		divTexto.style.visibility='hidden';
	}
	
	
	
}

function recalcularCantidad(combo){
	var seleccionado = 1;
	for(var x = 0; x < combo.length; x++){
		if(combo.options[x].selected){
			seleccionado = x+1;
		}
	}

	if(seleccionado==1){
		for(var y = combo.parentNode.parentNode.parentNode.rows.length -1; y >=2; y--){
			var nodo = combo.parentNode.parentNode.parentNode.rows[y];
			combo.parentNode.parentNode.parentNode.removeChild(nodo);
		}
	}
	
	if(seleccionado==2){
		for(var y = combo.parentNode.parentNode.parentNode.rows.length -1; y >=3; y--){
			var nodo = combo.parentNode.parentNode.parentNode.rows[y];
			combo.parentNode.parentNode.parentNode.removeChild(nodo);
		}
		
		for(var y = combo.parentNode.parentNode.parentNode.rows.length; y <3 ; y++){
			agregarCombosBusqueda(combo);
		}
	}
	
	if(seleccionado==3){
		for(var y = combo.parentNode.parentNode.parentNode.rows.length -1; y >=4; y--){
			var nodo = combo.parentNode.parentNode.parentNode.rows[y];
			combo.parentNode.parentNode.parentNode.removeChild(nodo);
		}
		for(var y = combo.parentNode.parentNode.parentNode.rows.length; y <4 ; y++){
			agregarCombosBusqueda(combo);
		}
	}
	
	if(seleccionado==4){
		for(var y = combo.parentNode.parentNode.parentNode.rows.length -1; y >=5; y--){
			var nodo = combo.parentNode.parentNode.parentNode.rows[y];
			combo.parentNode.parentNode.parentNode.removeChild(nodo);
		}
		for(var y = combo.parentNode.parentNode.parentNode.rows.length; y <5 ; y++){
			agregarCombosBusqueda(combo);
		}
	}
	
}

function agregarCombosBusqueda(combo){
	var tr = document.createElement('tr');
	var td1 = document.createElement('td');
	var td2 = document.createElement('td');
	var td3 = document.createElement('td');
	var td4 = document.createElement('td');	
	td1.valign ="middle";
	td2.valign ="middle";
	td3.valign ="middle";
	td4.valign ="middle";
	
	var sel1 = document.createElement('select');
	sel1.name = 'adultos';
	sel1.id = 'adultos';
	sel1.className = 'texto2';
	var sel2 = document.createElement('select');
	sel2.name = 'ninos';
	sel2.id = 'ninos';
	sel2.className = 'texto2';
	
	var option11 = document.createElement('option');
	var option12 = document.createElement('option');
	var option13 = document.createElement('option');
	var option14 = document.createElement('option');
	
	var option20 = document.createElement('option');
	var option21 = document.createElement('option');
	var option22 = document.createElement('option');
	var option23 = document.createElement('option');
	var option24 = document.createElement('option');
	
	option11.text = '1';
	option12.text = '2';
	option13.text = '3';
	option14.text = '4';
	
	option11.value = '1';
	option12.value = '2';
	option13.value = '3';
	option14.value = '4';
	
	option20.text = '0';
	option21.text = '1';
	option22.text = '2';
	option23.text = '3';
	option24.text = '4';
	
	option20.value = '0';
	option21.value = '1';
	option22.value = '2';
	option23.value = '3';
	option24.value = '4';
	
	try {
		sel1.add(option11, null); // standards compliant; doesn't work in IE
		sel1.add(option12, null);
		sel1.add(option13, null);
		sel1.add(option14, null);
	}catch(ex) {
		sel1.add(option11); // IE only
		sel1.add(option12);
		sel1.add(option13);
		sel1.add(option14);
	}
	
	try {
		sel2.add(option20, null);// standards compliant; doesn't work in IE
		sel2.add(option21, null); 
		sel2.add(option22, null);
		sel2.add(option23, null);
		sel2.add(option24, null);
	}catch(ex) {
		sel2.add(option20); // IE only
		sel2.add(option21); 
		sel2.add(option22);
		sel2.add(option23);
		sel2.add(option24);
	}

	td2.appendChild(sel1);
	td3.appendChild(sel2);
	
	tr.appendChild(td1);
	tr.appendChild(td2);
	tr.appendChild(td3);
	tr.appendChild(td4);
	
	combo.parentNode.parentNode.parentNode.appendChild(tr);
}



function validaciones(formulario){
	if(validaNulos(formulario)){
		return true;
	}
	return false;
}
function esDigito(sChr){  
    var sCod = sChr.charCodeAt(0);  
    return ((sCod > 47) && (sCod < 58));  
   }  
   function valSep(oTxt){  
    var bOk = false;  
    bOk = bOk || ((oTxt.value.charAt(2) == "-") && (oTxt.value.charAt(5) == "-"));  
    bOk = bOk || ((oTxt.value.charAt(2) == "/") && (oTxt.value.charAt(5) == "/"));  
    return bOk;  
   }  
   function finMes(oTxt){  
    var nMes = parseInt(oTxt.value.substr(3, 2), 10);  
    var nAno = parseInt(oTxt.value.substr(6), 10);  
    var nRes = 0;  
    switch (nMes){  
     case 1: nRes = 31; break;  
     case 2: nRes = 28; break;  
     case 3: nRes = 31; break;  
     case 4: nRes = 30; break;  
     case 5: nRes = 31; break;  
     case 6: nRes = 30; break;  
     case 7: nRes = 31; break;  
     case 8: nRes = 31; break;  
     case 9: nRes = 30; break;  
     case 10: nRes = 31; break;  
     case 11: nRes = 30; break;  
     case 12: nRes = 31; break;  
    }  
    return nRes + (((nMes == 2) && (nAno % 4) == 0)? 1: 0);  
   }  
   function valDia(oTxt){  
    var bOk = false;  
    var nDia = parseInt(oTxt.value.substr(0, 2), 10);  
    bOk = bOk || ((nDia >= 1) && (nDia <= finMes(oTxt)));  
    return bOk;  
   }  
   function valMes(oTxt){  
    var bOk = false;  
    var nMes = parseInt(oTxt.value.substr(3, 2), 10);  
    bOk = bOk || ((nMes >= 1) && (nMes <= 12));  
    return bOk;  
   }  
   function valAno(oTxt){  
    var bOk = true;  
    var nAno = oTxt.value.substr(6);  
    bOk = bOk && ((nAno.length == 2) || (nAno.length == 4));  
    if (bOk){  
     for (var i = 0; i < nAno.length; i++){  
      bOk = bOk && esDigito(nAno.charAt(i));  
     }  
    }  
    return bOk;  
   }  
   function valFecha(oTxt){  
    var bOk = true;  
    if (oTxt.value != ""){  
     bOk = bOk && (valAno(oTxt));  
     bOk = bOk && (valMes(oTxt));  
     bOk = bOk && (valDia(oTxt));  
     bOk = bOk && (valSep(oTxt));  
     return bOk;  
    }  
   }  
   function fechaMayorOIgualQue(fec0, fec1, igual){  
    var bRes = false;  
    var sDia0 = fec0.substr(0, 2);  
    var sMes0 = fec0.substr(3, 2);  
    var sAno0 = fec0.substr(6, 4);  
    var sDia1 = fec1.substr(0, 2);  
    var sMes1 = fec1.substr(3, 2);  
    var sAno1 = fec1.substr(6, 4);  
    if (sAno0 > sAno1) {
		bRes = true;
	}else {
		if (sAno0 == sAno1){
			if (sMes0 > sMes1){
				bRes = true;
			}else {
				if (sMes0 == sMes1){
					if(igual){
						if (sDia0 >= sDia1){
							bRes = true;
						}
					}else{
						if (sDia0 > sDia1){
							bRes = true;
						}
      				}  
     			}
    		}
		}
	}
    return bRes;  
   }  
function validaNulos(formulario){
	var error = "Se encontraron los siguientes errores: \n";
	var fecha1 = formulario.theDate1.value;
	var fecha2 = formulario.theDate2.value;
	if(formulario.theDate1.value==""){
		error+= " - Debe seleccionar la fecha de llegada\n";
	}
	if(formulario.theDate2.value==""){
		error+= " - Debe seleccionar la fecha de salida\n";
	}
	
	if(fecha1.length>0 && fecha2.length>0){
		var fecha=new Date();
		var diames=fecha.getDate();
		var mes=fecha.getMonth() +1 ;
		var ano=fecha.getFullYear();
		var fechaActual= (diames>9?diames:("0"+""+diames))+"/"+((mes>9)?mes:("0"+""+mes))+"/"+ano;
		if(!fechaMayorOIgualQue(fecha1, fechaActual, true)){
			error+= " - La fecha de entrada debe ser igual o mayor que la fecha actual\n";
		}
		
		if(!fechaMayorOIgualQue(fecha2, fecha1, false)){
			error+= " - La fecha de salida debe ser mayor que la fecha de entrada\n";
		}
	}
	if(error != "Se encontraron los siguientes errores: \n"){
		alert(error);
		return false;
	}
	return true;
}

