var RequestObject = false;

if (window.XMLHttpRequest) //
RequestObject = new XMLHttpRequest();

if (window.ActiveXObject)
RequestObject = new ActiveXObject("Microsoft.XMLHTTP");

function ReqChange()
{
	if (RequestObject.readyState==4)
	{
	if (RequestObject.responseText.indexOf('invalid') == -1)
		{
			document.getElementById("mostrar").innerHTML=RequestObject.responseText;
		} else {
			document.getElementById("mostrar").innerHTML = "Error llamando";
		}
	}
}

function prueba(p,h,d,tv)
{
	document.getElementById("horario-"+p).value=h;
	document.getElementById("dispon-"+p).value=d;
	document.getElementById("cboEnviarReser").disabled=false;
	var Capacidad = parseInt(document.getElementById("capacidad-"+p).value);
	if((parseInt(tv)+parseInt(d))<=Capacidad)
		{
			//document.getElementById("status-"+h).style.display="block";			
			//document.getElementById("resultado-"+h).src='imagenes/positivo.png';
		} else {
			//document.getElementById("resultado-"+h).src='imagenes/negativo.png';
		}
}


function MostrarDisponibilidad(producto,cupo)
{
	//alert("estoy aqui ");
	var aplicaHorario =document.getElementById("aplica"+producto).value;
	var custId =document.getElementById("idCust").value;
	if(custId == "")
	{
		alert("Debe iniciar sesion para disfrutar de nuestros servicios!! ");	
		return;
	
	}
	var CantNi, CantTe, CantAd, totalVisitantes
	var divMensaje=document.getElementById("ShowHorarios"+producto);
	divMensaje.innerHTML ="";
	
	document.getElementById("horario-"+producto).value="";
	
	CantAd = parseInt(document.getElementById("txtAd-"+producto).value);
	if (document.getElementById("txtNi-"+producto)==null)
		{
			CantNi =0;
		} 
	else
		{
			CantNi= parseInt(document.getElementById("txtNi-"+producto).value);	
		}
	if (document.getElementById("txtTe-"+producto)==null)
		{
			CantTe =0;
		} else {
			CantTe = parseInt(document.getElementById("txtTe-"+producto).value);
		}
		
	var MensajeCupoMaximo = "Su reserva excede el limite por reservación. Por favor para concretar su solicitud comuníquese \r\ncon el Departamento de Ventas y Atención al Cliente al 0424-3188665; 0244-4002554 ó 0244-4002550";
	totalVisitantes = CantAd + CantNi + CantTe;
	if (totalVisitantes==0)
		{
			alert("Debe indicar la cantidad de Adultos, Niños o personas de la Tercera Edad ultizarán los servicios indicados");
			return;
		} 	else {
			// La siguiente lineas hace referencia a PAINT BALL
			//if (producto==5)
			//	{
					//if (totalVisitantes>=11)
					//	{
					//	alert(MensajeCupoMaximo);					
					//	return;
					//	}
			//	}
			// La siguiente lineas aplica para limitar todos los productos a un cupo maximo			
			//if (totalVisitantes>=20)
			//	{
			//	//alert(MensajeCupoMaximo);					
			//	return;
			//	}
		}
	
	var txtDesde = document.getElementById("txtDesde").value;
	if(txtDesde == "")
	{
		alert("Debe Seleccionar una Fecha !! ");	
		return;
	
	}
	//var RequestObject = nuevoAjax();
	RequestObject.open("post", "ajax/reservacion/reserva.php", true);
	RequestObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	var URLparaEnviar = ("txtDesde="+txtDesde+"&txtIdProducto="+producto+"&totalvisita="+totalVisitantes+"&aplicaH="+aplicaHorario+"&custId="+custId);
	//alert(URLparaEnviar);
	divMensaje = "Buscando...";
	
	RequestObject.send(URLparaEnviar);
	
	RequestObject.onreadystatechange = function()
	{
	if(RequestObject.readyState==4)
		{
			//alert(RequestObject.responseText);			
			Datos = RequestObject.responseXML;
			
			t = Datos.getElementsByTagName("total");
			total = t[0].firstChild.nodeValue;
			p = Datos.getElementsByTagName("producto");
			h = Datos.getElementsByTagName("horario");		
			n = Datos.getElementsByTagName("nombrehora");
			v = Datos.getElementsByTagName("visitas");
			r = Datos.getElementsByTagName("reservaciones");
			vi= Datos.getElementsByTagName("campoVisitas");
			
			var totalr =r[0].firstChild.nodeValue; 
			var totalcupo = vi[0].firstChild.nodeValue; 
			if(totalr ==""){
				totalr=0;
				if(aplicaHorario!="N"){
					totalcupo =0;
				}
			}
			//alert(aplicaHorario);
			//alert(totalcupo);
			var totalFinalr = parseInt(totalr) + parseInt(totalcupo);
			if(totalcupo <= 20 ){
				if(totalFinalr <= 20 ){	
					
						i =0;
						valores="";
						var Capacidad = parseInt(document.getElementById("capacidad-"+producto).value);
				
						for(var i=0;i<total;i++)
						{
							
							if (v[i].firstChild.nodeValue==null)
								{
									visita=0;
								} else {
									visita=v[i].firstChild.nodeValue;
								}
								vis = parseInt(visita);
								tov = parseInt(totalVisitantes);
							//alert(vis+'-->'+ tov);
							if ((parseInt(visita)+parseInt(totalVisitantes)) <=parseInt(cupo))
								{
								valores = valores + "<tr><td class='texto_internas'><input name='"+producto+"' type='radio' name='prueba' value='' "+
												"onclick='prueba("+p[i].firstChild.nodeValue+","+h[i].firstChild.nodeValue+","+visita+","+totalVisitantes+");'></td><td>"+
												n[i].firstChild.nodeValue+"</td><td><div id='status-"+h[i].firstChild.nodeValue+"'><img border='0' src='imagenes/separador.gif' id='resultado-"+h[i].firstChild.nodeValue+"'></div></td></tr>" ;					
								} else {
								valores = valores + "<tr><td></td><td class='texto_internas'>"+n[i].firstChild.nodeValue+" (No Disponible) </td><td></td><td></td></tr>";
								}
						}
						document.getElementById("ShowHorarios"+producto).innerHTML = "<span class='texto_internas'>Horarios disponibles:</span><BR><table width='0%'  border='0' cellspacing='5' cellpadding='0'>"+valores+"</table>";
						document.getElementById("ShowHorarios"+producto).style.display="block";		
				}else{
						alert("Su reserva excede el limite por reservación. Por favor para concretar su solicitud comuníquese \r\ncon el Departamento de Ventas y Atención al Cliente al 0424-3188685; 0424-3188665; 0244-4002554 ó 0244-4002550");					
						document.getElementById("txtAd-"+producto).value=0;
						document.getElementById("txtNi-"+producto).value=0;	
						return;
						
					}
				
			}else{
						alert("Su reserva excede el limite por reservación. Por favor para concretar su solicitud comuníquese \r\ncon el Departamento de Ventas y Atención al Cliente al 0424-3188685; 0424-3188665; 0244-4002554 ó 0244-4002550");
						document.getElementById("txtAd-"+producto).value=0;
						document.getElementById("txtNi-"+producto).value=0;
						return;
					
				
				}
		}
	}
}