function isDate_es(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/aaaa o mm-dd-aaaa.");
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 isDate_in(dateStr) {

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

if (matchArray == null) {
//alert("Please write the date with format: 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("Month between 1 and 12.");
return false;
}

if (day < 1 || day > 31) {
//alert("Day between 1 and 31.");
return false;
}

if ((month==4 || month==6 || month==9 || month==11) && day==31) {
//alert("The mont "+month+" has not 31 days!")
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("February " + year + " has not " + day + " days!");
return false;
}
}
return true; // date is valid
}

function replace(texto,s1,s2){
	return texto.split(s1).join(s2);
}

function validarEmail(valor) {
		  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){
		   return (true)
		  } else {
		      return (false);
		  }
		 }


function fedad_ninos1() {
with(document.forms['fbuscador']){
	if (ninos1.selectedIndex==0)
		{ 
		document.getElementById('edadninos1').style.display='none'; 
		document.getElementById('sed1ha1').style.display='none'; 
		document.getElementById('sed2ha1').style.display='none';
		document.getElementById('sed3ha1').style.display='none';
		document.getElementById('sed4ha1').style.display='none';
		}
	else if (ninos1.selectedIndex==1)
		{ 
		document.getElementById('formninos').style.display='block'; 
		document.getElementById('edadninos1').style.display='block'; 
		document.getElementById('sed1ha1').style.display='inline'; 
		document.getElementById('sed2ha1').style.display='none';
		document.getElementById('sed3ha1').style.display='none';
		document.getElementById('sed4ha1').style.display='none';
		edadnino1habitacion1.style.display='block';
		edadnino2habitacion1.style.display='none';
		edadnino3habitacion1.style.display='none';
		edadnino4habitacion1.style.display='none';
		}
	else if (ninos1.selectedIndex==2)
		{
		document.getElementById('formninos').style.display='block'; 
		document.getElementById('edadninos1').style.display='block'; 
		document.getElementById('sed1ha1').style.display='inline';
		document.getElementById('sed2ha1').style.display='inline';
		document.getElementById('sed3ha1').style.display='none';
		document.getElementById('sed4ha1').style.display='none';
		edadnino1habitacion1.style.display='block';
		edadnino2habitacion1.style.display='block';
		edadnino3habitacion1.style.display='none';
		edadnino4habitacion1.style.display='none';
		}
	else if (ninos1.selectedIndex==3)
		{
		document.getElementById('formninos').style.display='block'; 
		document.getElementById('edadninos1').style.display='block'; 
		document.getElementById('sed1ha1').style.display='inline';
		document.getElementById('sed2ha1').style.display='inline';
		document.getElementById('sed3ha1').style.display='inline';
		document.getElementById('sed4ha1').style.display='none';
		edadnino1habitacion1.style.display='block';
		edadnino2habitacion1.style.display='block';
		edadnino3habitacion1.style.display='block';
		edadnino4habitacion1.style.display='none';
		}
	else
		{
		document.getElementById('formninos').style.display='block'; 
		document.getElementById('edadninos1').style.display='block'; 
		document.getElementById('sed1ha1').style.display='inline';
		document.getElementById('sed2ha1').style.display='inline';
		document.getElementById('sed3ha1').style.display='inline';
		document.getElementById('sed4ha1').style.display='inline';
		edadnino1habitacion1.style.display='block';
		edadnino2habitacion1.style.display='block';
		edadnino3habitacion1.style.display='block';
		edadnino4habitacion1.style.display='block';
		}
	}
}

function fedad_ninos2() {
with(document.forms['fbuscador']){
if (ninos2.selectedIndex==0)
		{ 
		document.getElementById('edadninos2').style.display='none'; 
		document.getElementById('sed1ha2').style.display='none'; 
		document.getElementById('sed2ha2').style.display='none';
		document.getElementById('sed3ha2').style.display='none';
		document.getElementById('sed4ha2').style.display='none';
		}
	else if (ninos2.selectedIndex==1)
		{ 
		document.getElementById('formninos').style.display='block'; 
		document.getElementById('edadninos2').style.display='block'; 
		document.getElementById('sed1ha2').style.display='inline'; 
		document.getElementById('sed2ha2').style.display='none';
		document.getElementById('sed3ha2').style.display='none';
		document.getElementById('sed4ha2').style.display='none';
		edadnino1habitacion2.style.display='block';
		edadnino2habitacion2.style.display='none';
		edadnino3habitacion2.style.display='none';
		edadnino4habitacion2.style.display='none';
		}
	else if (ninos2.selectedIndex==2)
		{
		document.getElementById('formninos').style.display='block'; 
		document.getElementById('edadninos2').style.display='block'; 
		document.getElementById('sed1ha2').style.display='inline';
		document.getElementById('sed2ha2').style.display='inline';
		document.getElementById('sed3ha2').style.display='none';
		document.getElementById('sed4ha2').style.display='none';
		edadnino1habitacion2.style.display='block';
		edadnino2habitacion2.style.display='block';
		edadnino3habitacion2.style.display='none';
		edadnino4habitacion2.style.display='none';
		}
	else if (ninos2.selectedIndex==3)
		{
		document.getElementById('formninos').style.display='block'; 
		document.getElementById('edadninos2').style.display='block'; 
		document.getElementById('sed1ha2').style.display='inline';
		document.getElementById('sed2ha2').style.display='inline';
		document.getElementById('sed3ha2').style.display='inline';
		document.getElementById('sed4ha2').style.display='none';
		edadnino1habitacion2.style.display='block';
		edadnino2habitacion2.style.display='block';
		edadnino3habitacion2.style.display='block';
		edadnino4habitacion2.style.display='none';
		}
	else
		{
		document.getElementById('formninos').style.display='block'; 
		document.getElementById('edadninos2').style.display='block'; 
		document.getElementById('sed1ha2').style.display='inline';
		document.getElementById('sed2ha2').style.display='inline';
		document.getElementById('sed3ha2').style.display='inline';
		document.getElementById('sed4ha2').style.display='inline';
		edadnino1habitacion2.style.display='block';
		edadnino2habitacion2.style.display='block';
		edadnino3habitacion2.style.display='block';
		edadnino4habitacion2.style.display='block';
		}
	}
}

function fedad_ninos3() {
with(document.forms['fbuscador']){
if (ninos3.selectedIndex==0)
		{ 
		document.getElementById('edadninos3').style.display='none'; 
		document.getElementById('sed1ha3').style.display='none'; 
		document.getElementById('sed2ha3').style.display='none';
		document.getElementById('sed3ha3').style.display='none';
		document.getElementById('sed4ha3').style.display='none';
		}
	else if (ninos3.selectedIndex==1)
		{ 
		document.getElementById('formninos').style.display='block'; 
		document.getElementById('edadninos3').style.display='block'; 
		document.getElementById('sed1ha3').style.display='inline'; 
		document.getElementById('sed2ha3').style.display='none';
		document.getElementById('sed3ha3').style.display='none';
		document.getElementById('sed4ha3').style.display='none';
		edadnino1habitacion3.style.display='block';
		edadnino2habitacion3.style.display='none';
		edadnino3habitacion3.style.display='none';
		edadnino4habitacion3.style.display='none';
		}
	else if (ninos3.selectedIndex==2)
		{
		document.getElementById('formninos').style.display='block'; 
		document.getElementById('edadninos3').style.display='block'; 
		document.getElementById('sed1ha3').style.display='inline';
		document.getElementById('sed2ha3').style.display='inline';
		document.getElementById('sed3ha3').style.display='none';
		document.getElementById('sed4ha3').style.display='none';
		edadnino1habitacion3.style.display='block';
		edadnino2habitacion3.style.display='block';
		edadnino3habitacion3.style.display='none';
		edadnino4habitacion3.style.display='none';
		}
	else if (ninos3.selectedIndex==3)
		{
		document.getElementById('formninos').style.display='block'; 
		document.getElementById('edadninos3').style.display='block'; 
		document.getElementById('sed1ha3').style.display='inline';
		document.getElementById('sed2ha3').style.display='inline';
		document.getElementById('sed3ha3').style.display='inline';
		document.getElementById('sed4ha3').style.display='none';
		edadnino1habitacion3.style.display='block';
		edadnino2habitacion3.style.display='block';
		edadnino3habitacion3.style.display='block';
		edadnino4habitacion3.style.display='none';
		}
	else
		{
		document.getElementById('formninos').style.display='block'; 
		document.getElementById('edadninos3').style.display='block'; 
		document.getElementById('sed1ha3').style.display='inline';
		document.getElementById('sed2ha3').style.display='inline';
		document.getElementById('sed3ha3').style.display='inline';
		document.getElementById('sed4ha3').style.display='inline';
		edadnino1habitacion3.style.display='block';
		edadnino2habitacion3.style.display='block';
		edadnino3habitacion3.style.display='block';
		edadnino4habitacion3.style.display='block';
		}
	}
}

function fedad_ninos4() {
with(document.forms['fbuscador']){
if (ninos4.selectedIndex==0)
		{ 
		document.getElementById('edadninos4').style.display='none'; 
		document.getElementById('sed1ha4').style.display='none'; 
		document.getElementById('sed2ha4').style.display='none';
		document.getElementById('sed3ha4').style.display='none';
		document.getElementById('sed4ha4').style.display='none';
		}
	else if (ninos4.selectedIndex==1)
		{ 
		document.getElementById('formninos').style.display='block'; 
		document.getElementById('edadninos4').style.display='block'; 
		document.getElementById('sed1ha4').style.display='inline'; 
		document.getElementById('sed2ha4').style.display='none';
		document.getElementById('sed3ha4').style.display='none';
		document.getElementById('sed4ha4').style.display='none';
		edadnino1habitacion4.style.display='block';
		edadnino2habitacion4.style.display='none';
		edadnino3habitacion4.style.display='none';
		edadnino4habitacion4.style.display='none';
		}
	else if (ninos4.selectedIndex==2)
		{
		document.getElementById('formninos').style.display='block'; 
		document.getElementById('edadninos4').style.display='block'; 
		document.getElementById('sed1ha4').style.display='inline';
		document.getElementById('sed2ha4').style.display='inline';
		document.getElementById('sed3ha4').style.display='none';
		document.getElementById('sed4ha4').style.display='none';
		edadnino1habitacion4.style.display='block';
		edadnino2habitacion4.style.display='block';
		edadnino3habitacion4.style.display='none';
		edadnino4habitacion4.style.display='none';
		}
	else if (ninos4.selectedIndex==3)
		{
		document.getElementById('formninos').style.display='block'; 
		document.getElementById('edadninos4').style.display='block'; 
		document.getElementById('sed1ha4').style.display='inline';
		document.getElementById('sed2ha4').style.display='inline';
		document.getElementById('sed3ha4').style.display='inline';
		document.getElementById('sed4ha4').style.display='none';
		edadnino1habitacion4.style.display='block';
		edadnino2habitacion4.style.display='block';
		edadnino3habitacion4.style.display='block';
		edadnino4habitacion4.style.display='none';
		}
	else
		{
		document.getElementById('formninos').style.display='block'; 
		document.getElementById('edadninos4').style.display='block'; 
		document.getElementById('sed1ha4').style.display='inline';
		document.getElementById('sed2ha4').style.display='inline';
		document.getElementById('sed3ha4').style.display='inline';
		document.getElementById('sed4ha4').style.display='inline';
		edadnino1habitacion4.style.display='block';
		edadnino2habitacion4.style.display='block';
		edadnino3habitacion4.style.display='block';
		edadnino4habitacion4.style.display='block';
		}
	}
}

function fedad_ninos5() {
with(document.forms['fbuscador']){
if (ninos5.selectedIndex==0)
		{ 
		document.getElementById('edadninos5').style.display='none'; 
		document.getElementById('sed1ha5').style.display='none'; 
		document.getElementById('sed2ha5').style.display='none';
		document.getElementById('sed3ha5').style.display='none';
		document.getElementById('sed4ha5').style.display='none';
		}
	else if (ninos5.selectedIndex==1)
		{ 
		document.getElementById('formninos').style.display='block'; 
		document.getElementById('edadninos5').style.display='block'; 
		document.getElementById('sed1ha5').style.display='inline'; 
		document.getElementById('sed2ha5').style.display='none';
		document.getElementById('sed3ha5').style.display='none';
		document.getElementById('sed4ha5').style.display='none';
		edadnino1habitacion5.style.display='block';
		edadnino2habitacion5.style.display='none';
		edadnino3habitacion5.style.display='none';
		edadnino4habitacion5.style.display='none';
		}
	else if (ninos5.selectedIndex==2)
		{
		document.getElementById('formninos').style.display='block'; 
		document.getElementById('edadninos5').style.display='block'; 
		document.getElementById('sed1ha5').style.display='inline';
		document.getElementById('sed2ha5').style.display='inline';
		document.getElementById('sed3ha5').style.display='none';
		document.getElementById('sed4ha5').style.display='none';
		edadnino1habitacion5.style.display='block';
		edadnino2habitacion5.style.display='block';
		edadnino3habitacion5.style.display='none';
		edadnino4habitacion5.style.display='none';
		}
	else if (ninos5.selectedIndex==3)
		{
		document.getElementById('formninos').style.display='block'; 
		document.getElementById('edadninos5').style.display='block'; 
		document.getElementById('sed1ha5').style.display='inline';
		document.getElementById('sed2ha5').style.display='inline';
		document.getElementById('sed3ha5').style.display='inline';
		document.getElementById('sed4ha5').style.display='none';
		edadnino1habitacion5.style.display='block';
		edadnino2habitacion5.style.display='block';
		edadnino3habitacion5.style.display='block';
		edadnino4habitacion5.style.display='none';
		}
	else
		{
		document.getElementById('formninos').style.display='block'; 
		document.getElementById('edadninos5').style.display='block'; 
		document.getElementById('sed1ha5').style.display='inline';
		document.getElementById('sed2ha5').style.display='inline';
		document.getElementById('sed3ha5').style.display='inline';
		document.getElementById('sed4ha5').style.display='inline';
		edadnino1habitacion5.style.display='block';
		edadnino2habitacion5.style.display='block';
		edadnino3habitacion5.style.display='block';
		edadnino4habitacion5.style.display='block';
		}
	}
}


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(){
	var numero = document.getElementById('habitaciones').selectedIndex + 1;
	var m=2;
	for (m=2;m<=numero;m++)
		{
		document.getElementById('buscadorhotelestr'+m).style.display="block";
		}
		numero++;
	for (m=numero;m<=5;m++)
		{ 	document.getElementById('buscadorhotelestr'+m).style.display="none";
			document.getElementById('ninos'+m).selectedIndex=0;
			if (document.getElementById('edadninos'+m)!=null)
				document.getElementById('edadninos'+m).style.display="none";
		}
		
}

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

function GuardarReserva_CAS(){
var msgerror='';

with(document.forms['fr']){
	
	if ((holdername_prin.value=="")){ msgerror = '* Nombre\n'; }
	if ((holderlastname_prin.value=="")){ msgerror = '* Apellidos\n'; }
	if ((email.value=="")){ msgerror = msgerror +'* Email\n'; }
	else if (! validarEmail(email.value)){ msgerror = msgerror +'* Email incorrecto\n'; }
	if ((email2.value=="")){ msgerror = msgerror +'* Repetir email\n'; }
	if ((email.value!=email2.value)){ msgerror = msgerror +'* Emails distintos\n'; }
	if ((telefono.value=="")){ msgerror = msgerror +'* Teléfono\n'; }
	
	
	if (msgerror!=''){
		alert('Estos campos son obligatorios:\n\n'+msgerror);
		return false;
		}
	else{
		document.forms['fr'].submit();
		}
	}
}

function GuardarReserva_ENG(){
var msgerror='';


with(document.forms['fr']){
	var total=totalclientes.value;

	if ((holdername_prin.value=="")){ msgerror = '* Name\n'; }
	if ((holderlastname_prin.value=="")){ msgerror = '* Last Name\n'; }
	if ((email.value=="")){ msgerror = msgerror +'* Email\n'; }
	else if (! validarEmail(email.value)){ msgerror = msgerror +'* Incorrect Email\n'; }
	if ((email2.value=="")){ msgerror = msgerror +'* Repeat email\n'; }
	if ((email.value!=email2.value)){ msgerror = msgerror +'* Distinct emails\n'; }
	if ((telefono.value=="")){ msgerror = msgerror +'* Telephone (Mobile)\n'; }

	for (m=1;m<=total;m++)
	{
		var habi= document.getElementById('holdername'+m).value;
		var habi2= document.getElementById('holderlastname'+m).value;
		if (habi=="Guest First Name"){ msgerror = msgerror +'* Name from room '+m+' is not correct \n'; }
		if (habi=="Guest Last Name"){ msgerror = msgerror +'* lastname from room '+m+' is not correct \n'; }
	}
	
	if (msgerror!=''){
		alert('This fields are obligatory:\n\n'+msgerror);
		return false;
		}
	else{
		document.forms['fr'].submit();
		}
	}
}



function Buscador_CAS(){

var msgerror='';

if((document.getElementsByName('consumerName')[0].value=='Carretera a Sierra Nevada')||(document.getElementsByName('consumerName')[0].value=='Prado Llano')){
		 window.location.href='http://www.malagaholidays.es/hoteles/sierra-nevada.php';
	}else{
		with(document.forms['fbuscador']){
			if ((iddestino.value=="")){ msgerror = '* Destino\n'; }
			if (!isDate_es(checkin.value)){ msgerror = msgerror + '* Entrada\n'; }
			if (!isDate_es(checkout.value)){ msgerror = msgerror + '* Salida\n'; }
			if (msgerror!=''){
				alert('Los siguientes campos son obligatorios:\n\n'+msgerror);
				return false;
				}
			else{
				if (ninos1.selectedIndex==0 && ninos2.selectedIndex==0 && ninos3.selectedIndex==0 && ninos4.selectedIndex==0 && ninos5.selectedIndex==0)
					{
					document.forms['fbuscador'].enviar.disabled=false;
					//document.forms['fbuscador'].action='hoteles.php';
					document.forms['fbuscador'].submit();
					}
				else
					{
					document.forms['fbuscador'].enviar.disabled=true;
					document.getElementById('formninos').style.display='none'; 
					fedad_ninos1();
					fedad_ninos2();
					fedad_ninos3();
					fedad_ninos4();
					fedad_ninos5();
					}
				}
		}	
	}
}


function Buscador2_CAS(){

	if((document.getElementsByName('consumerName')[0].value=='Carretera a Sierra Nevada')||(document.getElementsByName('consumerName')[0].value=='Prado Llano')){
		 window.location.href='http://www.malagaholidays.es/hoteles/sierra-nevada.php';
	}else{
		with(document.forms['fbuscador']){
			//document.forms['fbuscador'].action='hoteles.php';
			document.forms['fbuscador'].submit();
		}
	}
}


function Buscador_ENG(){
var msgerror='';

	if((document.getElementsByName('consumerName')[0].value=='Carretera a Sierra Nevada')||(document.getElementsByName('consumerName')[0].value=='Prado Llano')){
		 window.location.href='http://www.malagaholidays.com/hotels/sierra-nevada.php';
	}else{
		with(document.forms['fbuscador']){
		if ((iddestino.value=="")){ msgerror = '* Destination\n'; }
		if (!isDate_in(checkin.value)){ msgerror = msgerror + '* Check in\n'; }
		if (!isDate_in(checkout.value)){ msgerror = msgerror + '* Check out\n'; }
		if (msgerror!=''){
			alert('These fields are obligatory:\n\n'+msgerror);
			return false;
			}
		else{
			if (ninos1.selectedIndex==0 && ninos2.selectedIndex==0 && ninos3.selectedIndex==0 && ninos4.selectedIndex==0 && ninos5.selectedIndex==0)
				{
				document.forms['fbuscador'].enviar.disabled=false;
				//document.forms['fbuscador'].action='hoteles.php';
				document.forms['fbuscador'].submit();
				}
			else
				{
				document.forms['fbuscador'].enviar.disabled=true;
				document.getElementById('formninos').style.display='none'; 
				fedad_ninos1();
				fedad_ninos2();
				fedad_ninos3();
				fedad_ninos4();
				fedad_ninos5();
				}
			}
		}
	}	
}


function Buscador2_ENG(){
	if((document.getElementsByName('consumerName')[0].value=='Carretera a Sierra Nevada')||(document.getElementsByName('consumerName')[0].value=='Prado Llano')){
		 window.location.href='http://www.malagaholidays.com/hotels/sierra-nevada.php';
	}else{	
		with(document.forms['fbuscador']){
		//document.forms['fbuscador'].action='hoteles.php';
		document.forms['fbuscador'].submit();
		}
	}
}

