var abiertollegadas="no";
var abiertosalidas="no";
function clickhotelesllegada(){
		abiertollegadas="yes";
		document.f.hotelllegada.style.width='350px';

}

function clickhotelessalida(){
	
		abiertosalidas="yes";
		document.f.hotelsalida.style.width='350px';
	
}
function isDate(dateStr) {

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
var matchArray = dateStr.match(datePat); // is the format ok?

if (matchArray == null) {
alert("Por favor introduzca la fecha en el formato: mm/dd/yyyy o mm-dd-yyyy.");
return false;
}

month = matchArray[3]; // p@rse date into variables
day = matchArray[1];
year = matchArray[5];

//alert ("dia:"+day+"\nMes:"+month+"\naño:" + year);


if (month < 1 || month > 12) { // check month range
alert("El mes debe ser entre 1 y 12.");
return false;
}

if (day < 1 || day > 31) {
alert("El día debe ser entre 1 y 31.");
return false;
}

if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("El mes "+month+" no tienen 31 días!")
return false;
}

if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)) {
alert("Febrero " + year + " no tiene " + day + " días!");
return false;
}
}
return true; // date is valid
}


function expandir(){
	if (abierto=="no")
	{
		document.getElementById("hotel").style.width="500px";
		abierto=="yes";
	}
	else
	{
		document.getElementById("hotel").style.width="160px";
		abierto=="no";
	}
}
function mostrarsalida(){
	if (document.getElementById("salida1").checked)
	{
		document.getElementById("salidadiv").style.display='block';
		
		abierto=="yes";
	}
	else
	{
		document.getElementById("salidadiv").style.display='none';
		abierto=="no";
	}
}
function clonar(nombre){
	document.getElementById(nombre+"salida").selectedIndex =document.getElementById(nombre+"llegada").selectedIndex;
}
function replace(texto,s1,s2){
	return texto.split(s1).join(s2);
}
function comprobarformularioreserva(){
	var error="";
	//FUNCIONES PARA COMPROBAR LLEGADA
	if (document.f.llegada.checked)
	{
		if (document.f.hotelllegada.value=='')
			error='Debe seleccionar su hotel de llegada.\n';
		
		if (isDate(document.f.fechallegada.value)=='dd/mm/yyyy')
			error+='La fecha de llegada es errónea.\n';
		else
		{
			var fecha = document.f.fechallegada.value;
			fecha = replace(fecha,"-","/");
			fecha = fecha.split("/");
			var fechallegada=new Date();
			fechallegada.setFullYear(fecha[2],(fecha[1]-1),fecha[0]);
			var hoy=new Date();
			var anohoy=hoy.getYear();
			if (anohoy< 1000)
				anohoy+=1900
			var meshoy=hoy.getMonth();
			var diahoy=hoy.getDate()+3;
			hoy.setFullYear(anohoy,meshoy,diahoy);
			if (fechallegada<hoy)
				error+='La fecha de llegada debe ser 36 horas superior a la de hoy.\n';
		}	
		
	}	

	//FUNCIONES PARA COMPROBAR SALIDA
	if (document.f.salida.checked)
	{
		if (document.f.hotelsalida.value=='')
			error+='Debe seleccionar su hotel de salida.\n';
			
			if (isDate(document.f.fechasalida.value)=='dd/mm/yyyy')
				error+='La fecha de salida es errónea.\n';
			else
			{
				var fecha = document.f.fechasalida.value;
				fecha = replace(fecha,"-","/");
				fecha = fecha.split("/");
				var fechallegada=new Date();
				fechallegada.setFullYear(fecha[2],(fecha[1]-1),fecha[0]);
				var hoy=new Date();
				var anohoy=hoy.getYear();
				if (anohoy< 1000)
					anohoy+=1900
				var meshoy=hoy.getMonth();
				var diahoy=hoy.getDate()+3;
				hoy.setFullYear(anohoy,meshoy,diahoy);
				if (fechallegada<hoy)
					error+='La fecha de salida debe ser 36 horas superior a la de hoy.\n';
			}
	}
	
	//CONTROL DE QUE LA FECHA DE SALIDA SEA SUPERIOR A LA DE ENTRADA SI NO HAY ERROR ANTERIOR
	if ((error=='') & (document.f.llegada.checked) & (document.f.salida.checked))
	{
		var fecha = document.f.fechasalida.value;
		fecha = replace(fecha,"-","/");
		fecha = fecha.split("/");
		var fechasalida=new Date();
		fechasalida.setFullYear(fecha[2],(fecha[1]-1),fecha[0]);
		
		fecha = document.f.fechallegada.value;
		fecha = replace(fecha,"-","/");
		fecha = fecha.split("/");
		var fechallegada=new Date();
		fechallegada.setFullYear(fecha[2],(fecha[1]-1),fecha[0]);
		//alert (fechallegada+"\n"+fechasalida);
		
		if (fechasalida<=fechallegada)
			error+='La fecha de salida debe ser superior a la fecha de llegada.\n';
	}
	if ((error=='') & (!document.f.llegada.checked) & (!document.f.salida.checked))
	{
					error+='Debe seleccionar un traslado de llegada o salida.\n';
	}
	//CONTROL FINAL
	if (error=='')
		return true;
	else
		{
			alert('Se han producido los siguientes errores:\n'+error);
			return false;
		}
	
}
	function validarEmail(valor) {
		  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){
		   return (true)
		  } else {
		      return (false);
		  }
		 }
function comprobar2(){
	var error="";
	var erroring="";
	
	var msgerror="";
	if (document.f2.nombre=='')
		{
		error+='Debe escribir un nombre.\n';
		erroring+='Your name.\n';
		}
		
		if (document.f2.teléfono==''){
			error+='Debe escribir un teléfono.\n';
			erroring+='Your telephone number.\n';
			}
			if (document.f2.vuelo==''){
				error+='Debe escribir número de vuelo.\n';
				erroring+='Flight number.\n';
			}
		
		if (document.f2.email.value=='') 
			{
			error += '* Email\n';
			erroring += '* The Email seems wrong\n';
			}
		else
		{
		if (validarEmail(document.f2.email.value)==false){
			error +='* El Email parece incorrecto\n';
			erroring += '* The Email seems wrong\n';
			}
		}
		if (error=='')
			return true;
		else
			{
				alert('Se han producido los siguientes errores:\n'+error+'\n\nThey have produced the following errors:\n'+ erroring);
				return false;
			}
}

function calc() { 
//vent=window.open('','tpv','width=725,height=600,scrollbars=no,resizable=yes,status=yes,menubar=no,location=no');
document.compra.submit();}

function autocompletar(texto,pagina,campo){
	var lista = document.getElementById('listadoautocomplete');
	lista.style.left=document.getElementById(campo).style.left;
	
	if (texto.length<=3)
		lista.style.display="block";
	else
		lista.style.display="none";
}

function visualizar(numero){
	for (m=1;m<=numero;m++)
		{
		document.getElementById('buscadorhotelestr'+m).style.display="inline";
		}
		numero++;
	for (m=numero;m<=5;m++)
		{
		document.getElementById('buscadorhotelestr'+m).style.display="none";
		document.getElementById('edadninos'+m).style.display="none";
		}
		
}

function mostrarninos(valor,fila)
{
	if (valor>0)
		document.getElementById('edadninos'+fila).style.display="inline";
	else
		document.getElementById('edadninos'+fila).style.display="none";
}
