function AbrirPopUp(sDireccion, sNombre, sCaracteristicas)
{
	window.open(sDireccion, sNombre, sCaracteristicas);
}

function abrirPagina(sPagina)
{
	window.location = sPagina;
}

function eliminarEspacios(sCadena)
{
	return sCadena.replace(/^\s+|\s+$/g, "");
}

function validarReservas()
{
	sNombre = eliminarEspacios(document.getElementById("sNombre").value);
	sApellido = eliminarEspacios(document.getElementById("sApellido").value);
	sMail = eliminarEspacios(document.getElementById("sMail").value);
	sTelefono = eliminarEspacios(document.getElementById("sTelefono").value);
	sPais = eliminarEspacios(document.getElementById("sPais").value);
	sProvincia = eliminarEspacios(document.getElementById("sProvincia").value);
	sComentarios = eliminarEspacios(document.getElementById("sComentarios").value);

	document.getElementById("sNombre").value = sNombre;
	document.getElementById("sApellido").value = sApellido;
	document.getElementById("sMail").value = sMail;
	document.getElementById("sTelefono").value = sTelefono;
	document.getElementById("sPais").value = sPais;
	document.getElementById("sProvincia").value = sProvincia;
	document.getElementById("sComentarios").value = sComentarios;

	if ((sNombre == "") || (sApellido == "") || (sMail == ""))
	{
		sMensaje = "Debe completar todos los campos obligatorios marcados\r\ncon asterisco (*) para enviar una solicitud de reserva.";
		alert(sMensaje);
		return false;
	}
	else
	{
		if (validarEgreso() == "NO")
		{
			sMensaje = "La fecha de egreso debe ser\r\nmayor a la fecha de ingreso.";
			alert(sMensaje);
			return false;
		}
		else
			return true;
	}
}

function reemplazar(sCadena, sReemplazado, sReemplazante)
{
	return sCadena.split(sReemplazado).join(sReemplazante);
}

function validarEgreso()
{
	var sEgresoValido = "SI";

	for (nHotel=1; nHotel<=5; nHotel++)
	{
		for (nHabitacion=1; nHabitacion<=5; nHabitacion++)
		{
			nId = nHotel.toString() + nHabitacion.toString();
			sHabitacionOculta = "sHabitacionOculta" + nId;

			if (document.getElementById(sHabitacionOculta).value == "NO")
			{
				cmbCheckInDia = "nCheckInDia" + nId;
				cmbCheckInMes = "sCheckInMes" + nId;
				cmbCheckInAno = "nCheckInAno" + nId;
				cmbCheckOutDia = "nCheckOutDia" + nId;
				cmbCheckOutMes = "sCheckOutMes" + nId;
				cmbCheckOutAno = "nCheckOutAno" + nId;

				//document.getElementById(cmbCheckInDia).options[document.getElementById(cmbCheckInDia).selectedIndex].value

				nCheckInDia = document.getElementById(cmbCheckInDia).value;
				nCheckInMes = document.getElementById(cmbCheckInMes).value;
				nCheckInAno = document.getElementById(cmbCheckInAno).value;
				nCheckOutDia = document.getElementById(cmbCheckOutDia).value;
				nCheckOutMes = document.getElementById(cmbCheckOutMes).value;
				nCheckOutAno = document.getElementById(cmbCheckOutAno).value;

				if (nCheckInAno <= nCheckOutAno)
				{
					if (nCheckInAno == nCheckOutAno)
					{
						if (nCheckInMes <= nCheckOutMes)
						{
							if (nCheckInMes == nCheckOutMes)
							{
								if (nCheckInDia <= nCheckOutDia)
								{
									if (nCheckInDia == nCheckOutDia)
										sEgresoValido = "NO";
								}
								else
									sEgresoValido = "NO";
							}
						}
						else
							sEgresoValido = "NO";
					}
				}
				else
					sEgresoValido = "NO";
			}
		}
	}

	return sEgresoValido;
}

function mensajeProximamente()
{
	sMensaje = "Próximamente.";
	alert(sMensaje);
}

function agregarHabitacion(nHotel, nHabitacion)
{
	nId = nHotel.toString() + nHabitacion.toString();

	trAgregarHabitacionAnterior = "trAgregarHabitacion" + (nId - 1);
	trHabitacion = "trHabitacion" + nId;
	//trCantidad = "trCantidad" + nId;
	trIngreso = "trIngreso" + nId;
	trEgreso = "trEgreso" + nId;
	sHabitacionOculta = "sHabitacionOculta" + nId;

	document.getElementById(trAgregarHabitacionAnterior).style.display = "none";
	document.getElementById(trHabitacion).style.display = "";
	//document.getElementById(trCantidad).style.display = "";
	document.getElementById(trIngreso).style.display = "";
	document.getElementById(trEgreso).style.display = "";

	if (nHabitacion < 5)
	{
		trAgregarHabitacion = "trAgregarHabitacion" + nId;
		document.getElementById(trAgregarHabitacion).style.display = "";
	}

	document.getElementById(sHabitacionOculta).value = "NO";

	llenarComboFecha(nHotel, nHabitacion);

	if (nHabitacion > 1)
	{
		nCheckInDia = "nCheckInDia" + nId;
		sCheckInMes = "sCheckInMes" + nId;
		nCheckInAno = "nCheckInAno" + nId;
		nCheckOutDia = "nCheckOutDia" + nId;
		sCheckOutMes = "sCheckOutMes" + nId;
		nCheckOutAno = "nCheckOutAno" + nId;

		nId = nHotel.toString() + "1";

		nCheckInDia1 = "nCheckInDia" + nId;
		sCheckInMes1 = "sCheckInMes" + nId;
		nCheckInAno1 = "nCheckInAno" + nId;
		nCheckOutDia1 = "nCheckOutDia" + nId;
		sCheckOutMes1 = "sCheckOutMes" + nId;
		nCheckOutAno1 = "nCheckOutAno" + nId;

		document.getElementById(nCheckInDia).value = document.getElementById(nCheckInDia1).value;
		document.getElementById(sCheckInMes).value = document.getElementById(sCheckInMes1).value;
		document.getElementById(nCheckInAno).value = document.getElementById(nCheckInAno1).value;
		document.getElementById(nCheckOutDia).value = document.getElementById(nCheckOutDia1).value;
		document.getElementById(sCheckOutMes).value = document.getElementById(sCheckOutMes1).value;
		document.getElementById(nCheckOutAno).value = document.getElementById(nCheckOutAno1).value;
	}
}

function agregarHotel(nHotel)
{
	nId = nHotel + "1";

	trAgregarHotelAnterior = "trAgregarHotel" + (nHotel - 1);
	trHotel = "trHotel" + nHotel;
	trHabitacion = "trHabitacion" + nId;
	//trCantidad = "trCantidad" + nId;
	trIngreso = "trIngreso" + nId;
	trEgreso = "trEgreso" + nId;
	trAgregarHabitacion = "trAgregarHabitacion" + nId;
	trhr = "trhr" + nHotel;
	sHabitacionOculta = "sHabitacionOculta" + nId;

	document.getElementById(trAgregarHotelAnterior).style.display = "none";
	document.getElementById(trHotel).style.display = "";
	document.getElementById(trHabitacion).style.display = "";
	//document.getElementById(trCantidad).style.display = "";
	document.getElementById(trIngreso).style.display = "";
	document.getElementById(trEgreso).style.display = "";
	document.getElementById(trAgregarHabitacion).style.display = "";
	document.getElementById(trhr).style.display = "";

	if (nHotel < 5)
	{
		trAgregarHotel = "trAgregarHotel" + nHotel;
		document.getElementById(trAgregarHotel).style.display = "";
	}

	document.getElementById(sHabitacionOculta).value = "NO";

	llenarComboFecha(nHotel, 1);

	if (nHotel > 1)
	{
		sHabitacion = "sHabitacion" + nId;
		sHabitacion1 = "sHabitacion" + "11";
		document.getElementById(sHabitacion).value = document.getElementById(sHabitacion1).value;
	}
}

function validarMail()
{
	document.getElementById("sMail").value = eliminarEspacios(document.getElementById("sMail").value);
	sMail = eliminarEspacios(document.getElementById("sMail").value);
	
	if (sMail != "")
	{
		if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(sMail)))
		{
			alert("La dirección de e-mail " + sMail + " es incorrecta.");
			document.getElementById("sMail").focus();
		}
	}
}

function llenarComboFecha(nHotel, nHabitacion)
{
	llenarComboDia(nHotel, nHabitacion);
	llenarComboMes(nHotel, nHabitacion);
	llenarComboAno(nHotel, nHabitacion);
}

function llenarComboDia(nHotel, nHabitacion)
{
	nId = nHotel.toString() + nHabitacion.toString();

	nCheckInDia = "nCheckInDia" + nId;
	nCheckOutDia = "nCheckOutDia" + nId;

	for (nDia=1; nDia<=31; nDia++)
	{
		var eOpcionCheckInDia = document.getElementById(nCheckInDia).options;
		var eOpcionCheckOutDia = document.getElementById(nCheckOutDia).options;

		if (nDia < 10)
		{
			eOpcionCheckInDia[eOpcionCheckInDia.length] = new Option("0" + nDia.toString(), "0" + nDia.toString());
			eOpcionCheckOutDia[eOpcionCheckOutDia.length] = new Option("0" + nDia.toString(), "0" + nDia.toString());
		}
		else
		{
			eOpcionCheckInDia[eOpcionCheckInDia.length] = new Option(nDia, nDia);
			eOpcionCheckOutDia[eOpcionCheckOutDia.length] = new Option(nDia, nDia);
		}
	}
}

function llenarComboMes(nHotel, nHabitacion)
{
	nId = nHotel.toString() + nHabitacion.toString();

	// Check In

	sCheckInMes = "sCheckInMes" + nId;

	var eOpcion = document.getElementById(sCheckInMes).options;
	eOpcion[eOpcion.length] = new Option("Enero", "01");

	var eOpcion = document.getElementById(sCheckInMes).options;
	eOpcion[eOpcion.length] = new Option("Febrero", "02");

	var eOpcion = document.getElementById(sCheckInMes).options;
	eOpcion[eOpcion.length] = new Option("Marzo", "03");

	var eOpcion = document.getElementById(sCheckInMes).options;
	eOpcion[eOpcion.length] = new Option("Abril", "04");

	var eOpcion = document.getElementById(sCheckInMes).options;
	eOpcion[eOpcion.length] = new Option("Mayo", "05");

	var eOpcion = document.getElementById(sCheckInMes).options;
	eOpcion[eOpcion.length] = new Option("Junio", "06");

	var eOpcion = document.getElementById(sCheckInMes).options;
	eOpcion[eOpcion.length] = new Option("Julio", "07");

	var eOpcion = document.getElementById(sCheckInMes).options;
	eOpcion[eOpcion.length] = new Option("Agosto", "08");

	var eOpcion = document.getElementById(sCheckInMes).options;
	eOpcion[eOpcion.length] = new Option("Septiembre", "09");

	var eOpcion = document.getElementById(sCheckInMes).options;
	eOpcion[eOpcion.length] = new Option("Octubre", "10");

	var eOpcion = document.getElementById(sCheckInMes).options;
	eOpcion[eOpcion.length] = new Option("Noviembre", "11");

	var eOpcion = document.getElementById(sCheckInMes).options;
	eOpcion[eOpcion.length] = new Option("Diciembre", "12");

	// Check Out

	sCheckOutMes = "sCheckOutMes" + nId;

	var eOpcion = document.getElementById(sCheckOutMes).options;
	eOpcion[eOpcion.length] = new Option("Enero", "01");

	var eOpcion = document.getElementById(sCheckOutMes).options;
	eOpcion[eOpcion.length] = new Option("Febrero", "02");

	var eOpcion = document.getElementById(sCheckOutMes).options;
	eOpcion[eOpcion.length] = new Option("Marzo", "03");

	var eOpcion = document.getElementById(sCheckOutMes).options;
	eOpcion[eOpcion.length] = new Option("Abril", "04");

	var eOpcion = document.getElementById(sCheckOutMes).options;
	eOpcion[eOpcion.length] = new Option("Mayo", "05");

	var eOpcion = document.getElementById(sCheckOutMes).options;
	eOpcion[eOpcion.length] = new Option("Junio", "06");

	var eOpcion = document.getElementById(sCheckOutMes).options;
	eOpcion[eOpcion.length] = new Option("Julio", "07");

	var eOpcion = document.getElementById(sCheckOutMes).options;
	eOpcion[eOpcion.length] = new Option("Agosto", "08");

	var eOpcion = document.getElementById(sCheckOutMes).options;
	eOpcion[eOpcion.length] = new Option("Septiembre", "09");

	var eOpcion = document.getElementById(sCheckOutMes).options;
	eOpcion[eOpcion.length] = new Option("Octubre", "10");

	var eOpcion = document.getElementById(sCheckOutMes).options;
	eOpcion[eOpcion.length] = new Option("Noviembre", "11");

	var eOpcion = document.getElementById(sCheckOutMes).options;
	eOpcion[eOpcion.length] = new Option("Diciembre", "12");
}

function llenarComboAno(nHotel, nHabitacion)
{
	nId = nHotel.toString() + nHabitacion.toString();

	nCheckInAno = "nCheckInAno" + nId;
	nCheckOutAno = "nCheckOutAno" + nId;

	for (nAno=2011; nAno<=2012; nAno++)
	{
		var eOpcionCheckInAno = document.getElementById(nCheckInAno).options;
		var eOpcionCheckOutAno = document.getElementById(nCheckOutAno).options;

		eOpcionCheckInAno[eOpcionCheckInAno.length] = new Option(nAno, nAno);
		eOpcionCheckOutAno[eOpcionCheckOutAno.length] = new Option(nAno, nAno);
	}
}

/*
function llenarComboCantidad(nHotel, nHabitacion)
{
	nId = nHotel.toString() + nHabitacion.toString();

	nCantidad = "nCantidad" + nId;

	for (nIndice=1; nIndice<=5; nIndice++)
	{
		var eOpcion = document.getElementById(nCantidad).options;
		eOpcion[eOpcion.length] = new Option(nIndice, nIndice);
	}
}
*/

function borrarHabitacion(nHotel, nHabitacion)
{
	nIndiceHabitacion = 5;

	while (nIndiceHabitacion >= 1)
	{
		nId = nHotel.toString() + nIndiceHabitacion.toString();
		sHabitacionOculta = "sHabitacionOculta" + nId;

		if (document.getElementById(sHabitacionOculta).value == "NO")
		{
			trHabitacion = "trHabitacion" + nId;
			//trCantidad = "trCantidad" + nId;
			trIngreso = "trIngreso" + nId;
			trEgreso = "trEgreso" + nId;

			document.getElementById(trHabitacion).style.display = "none";
			//document.getElementById(trCantidad).style.display = "none";
			document.getElementById(trIngreso).style.display = "none";
			document.getElementById(trEgreso).style.display = "none";

			if (nIndiceHabitacion < 5)
			{
				trAgregarHabitacion = "trAgregarHabitacion" + nId;
				document.getElementById(trAgregarHabitacion).style.display = "none";
			}

			if (nIndiceHabitacion > 1)
			{
				trAgregarHabitacionAnterior = "trAgregarHabitacion" + (nId - 1);
				document.getElementById(trAgregarHabitacionAnterior).style.display = "";
			}
			else
			{
				trHotel = "trHotel" + nHotel;
				trhr = "trhr" + (nHotel - 1);

				document.getElementById(trHotel).style.display = "none";
				document.getElementById(trhr).style.display = "none";

				if (nHotel < 5)
				{
					trAgregarHotel = "trAgregarHotel" + nHotel;
					document.getElementById(trAgregarHotel).style.display = "none";
				}
			}

			document.getElementById(sHabitacionOculta).value = "SI";

			vaciarComboFecha(nHotel, nIndiceHabitacion);

			nIndiceHabitacion = 1;
		}

		nIndiceHabitacion = nIndiceHabitacion - 1;

		nIndiceHotel = 4;

		while (nIndiceHotel >= 1)
		{
			nId = nIndiceHotel.toString() + "1";
			sHabitacionOculta = "sHabitacionOculta" + nId;

			if (document.getElementById(sHabitacionOculta).value == "NO")
			{
				trAgregarHotelAnterior = "trAgregarHotel" + nIndiceHotel;
				document.getElementById(trAgregarHotelAnterior).style.display = "";
				nIndiceHotel = 1;
			}
			
			nIndiceHotel = nIndiceHotel - 1;
		}
	}
}

function vaciarComboFecha(nHotel, nHabitacion)
{
	nId = nHotel.toString() + nHabitacion.toString();

	nCheckInDia = "nCheckInDia" + nId;
	nCheckOutDia = "nCheckOutDia" + nId;
	sCheckInMes = "sCheckInMes" + nId;
	sCheckOutMes = "sCheckOutMes" + nId;
	nCheckInAno = "nCheckInAno" + nId;
	nCheckOutAno = "nCheckOutAno" + nId;

	vaciarCombo(nCheckInDia);
	vaciarCombo(nCheckOutDia);
	vaciarCombo(sCheckInMes);
	vaciarCombo(sCheckOutMes);
	vaciarCombo(nCheckInAno);
	vaciarCombo(nCheckOutAno);
}

function vaciarCombo(sControl)
{
	var sCombo = document.getElementById(sControl);

	while (sCombo.length > 0)
	{
		sCombo.remove(sCombo.length-1);
	}
}
