/*********************************************************************************
***************    FUNCIONES DE VALIDACIÓN     ***********************************
**********************************************************************************/


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
  return false;
}



function validarFormulario(form) {

// Obtenemos la información de la fecha que ha seleccionado un usuario
var fEstancia= document.getElementById("fecha").value;

	if (form.nombre.value == ""){
		alert ('Por favor introduzca el nombre');
		form.nombre.focus();
		return false;
		}


	if (form.apellidos.value == ""){
		alert ('Por favor introduzca los apellidos');
		form.apellidos.focus();
		return false;
		}
	
	if( !(/\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(form.email.value)) ) {
		  	alert('Por favor introduzca un valor correcto de E-mail');
		  	form.email.focus();
		  	return false;
	  	}
	
		
		
	/*	
	if (!/^\d{15}$/.test(form.telefono.value)){
			alert ('El teléfono no es correcto');
			form.telefono.focus();
			return false;	 
		}
	
	*/
	// Se elimina la ecpresion regular a petición de teléfono internacional	
	if  (form.telefono.value == ""){
			alert ('Debe introducir un telefono');
			form.telefono.focus();
			return false;	 
		}
	
	
	
	if (form.apellidos.value == ""){
		alert ('Por favor introduzca la fecha de la estancia en el Hotel');
		form.fecha.focus();
		return false;
		}
	
	if (form.fecha.value == ""){
		alert ('Por favor introduzca la fecha de la estancia en el Hotel');
		form.fecha.focus();
		return false;
		}
	
	
	// Comprueba si la fecha introducida es correcta
	var valorFecha = compruebaFecha(fEstancia);
	
	if (valorFecha == false){
		alert ('La fecha que ha seleccionado no es válida ya que es una fecha anterior a la actual');
		form.fecha.focus();
		return false;
		}
		
	
	if (form.noches.selectedIndex==0){
		alert ('Por favor introduzca el número de noches');
		form.noches.focus();
		return false;
		}

	if ((form.num_h_doble.selectedIndex==0) && (form.num_h_doble_ind.selectedIndex==0) && (form.suite.selectedIndex==0)){
		alert ('Por favor debe seleccionar como mínimo una cantidad y un número de habitación');
		form.num_h_doble.focus();
		return false;
		}
	
	for (var i = 0; i < form.length; i++){
			if (form.condiciones.checked == false) {
			alert ('Debe de aceptar las condiciones');
			return false;
			}	
		}




return true;

}




function compruebaFecha (fechaEstancia){
	
	var coincide = false;
	var fecha = new String(fechaEstancia);
	
	var dia = fecha.substr(0,2);
	var mes = fecha.substr(3,2);
	var ano = fecha.substr(6);
	
// Este código lo he tenido que introducir ya que la función parseInt no me convertia correctamente el mes 08 y el 09 a 		enteros por ese motivo lo fuerzo a mano.
	
	if (mes == '08' || mes == '09'){
		if (mes == '08'){ numMes = 8; }
		else{ numMes = 9;	}
	} else {	
	var numMes = parseInt(mes); // convierto el mes a entero
	}
	
	// fin del bug
	
//	var numMes = parseInt(mes); // convierto el mes a entero

	var mesFinal = numMes -1;  // resyo uno ya que los meses con la funcion getMonth van del 0 al 11 siendo el 0-> Enero y 11-> Diciembre
	
	// Creamos el objeto fecha con la fecha del usuario
	var fechaSeleccionada = new Date();
	fechaSeleccionada.setFullYear(ano);
	fechaSeleccionada.setDate(dia);
	fechaSeleccionada.setMonth(mesFinal);
	
	// creamos la fecha de hoy
	var today = new Date();
	var diaHoy = today.getDate();
	var mesHoy = today.getMonth();
	var anoHoy = today.getFullYear();
	
	// Comparamos si la fecha seleccionada corresponde con la de hoy:
	if (dia == diaHoy){ 
		if (mesFinal == mesHoy){
			if (ano == anoHoy){
				var coincide = true;
				return coincide;
			}	
		}
	}
	
	if (coincide != true){ // Marcamos si la fecha es correcta
	
		if (fechaSeleccionada < today){ 
	  		  coincide = false;
			  return coincide; 
			}

		else{ coincide = true; 
			return coincide;
			
		}
	
	} // fin del if coincide
	
	

}