function mail(texto){
	var mailres = true;            
	var cadena = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890@._-";
	var arroba = texto.indexOf("@",0);
	var punto = texto.lastIndexOf(".");
	if ((texto.lastIndexOf("@")) != arroba){
		arroba = -1;
	}
	var contador=0;
	while (contador<texto.length){
		if (cadena.indexOf(texto.substr(contador, 1),0) == -1){
			mailres = false;
		} else contador++;
	}
	if ((arroba > 1) && (arroba + 1 < punto) && (punto + 1 < (texto.length)) && (mailres == true) && (texto.indexOf("..",0) == -1)){
		mailres = true;
	} else mailres = false;
	return mailres;
}

function validar() {
	/*
	dias=new Array("domingo", "lunes", "martes", "miércoles", "jueves", "viernes", "sábado");
	
	document.getElementById('aviso').style.display = 'none';
	document.getElementById('aviso2').style.display = 'none';
	document.getElementById('aviso3').style.display = 'none';
	document.getElementById('aviso4').style.display = 'none';
	document.getElementById('aviso5').style.display = 'none';
	
	fecha=document.getElementById('fecha_desde').value;
	fecha_cortada=fecha.split('/');
	mes=fecha_cortada[1];
	fecha_objeto=new Date(fecha_cortada[2],fecha_cortada[1]-1,fecha_cortada[0]);
	dia=dias[fecha_objeto.getDay()];
	*/
	
	/* sólo se permiten reservas entre marzo y agosto, ambos inclusive, de la forma:
	 	a) de marzo a junio (ambos incluídos) -> la fecha de entrada sólo puede ser viernes o domingo
	 	b) de julio a agosto (ambos incluídos) -> la fecha de entrada sólo puede ser sábado 
	*/
	
	/*
	if ((mes<3)||(mes>9)||(fecha=='')){
		document.getElementById('aviso3').style.display = 'block';
		return false;
	}
	if (((mes>=3)&&(mes<=6))||(mes==9)){
		if ((dia!='viernes')&&(dia!='sábado')&&(dia!='domingo')){
			document.getElementById('aviso3').style.display = 'block';
			return false;
		}
		if ((document.getElementById('tipo_alojamiento').value=='finde')&&(dia!="viernes")&&(dia!="sábado")){
			document.getElementById('aviso3').style.display = 'block';
			return false;
		}
		if ((document.getElementById('tipo_alojamiento').value=='5dias')&&(dia!="sábado")&&(dia!="domingo")){
			document.getElementById('aviso3').style.display = 'block';
			return false;
		}
		if ((document.getElementById('tipo_alojamiento').value=='7dias')&&(dia!="sábado")&&(dia!="domingo")){
			document.getElementById('aviso3').style.display = 'block';
			return false;
		}
	}
	if ((mes>=7)&&(mes<=8)){
		if (dia!='sabado'){
			document.getElementById('aviso3').style.display = 'block';
			return false;
		}
		if ((document.getElementById('tipo_alojamiento').value=='finde') || (document.getElementById('tipo_alojamiento').value=='5dias') || ((document.getElementById('tipo_alojamiento').value=='7diasfull')&&(document.getElementById('habitacion').value!='Cubo'))){
			document.getElementById('aviso3').style.display = 'block';
			return false;
		}
	}
	if (((mes==7)||(mes==8)) &&	((document.getElementById('tipo_alojamiento').value=='finde') || (document.getElementById('tipo_alojamiento').value=='5dias') || ((document.getElementById('tipo_alojamiento').value=='7diasfull') && (document.getElementById('habitacion').value!='Cubo')))){
		document.getElementById('aviso4').style.display = 'block';
		return false;
	} else {
		if ((document.getElementById('tipo_alojamiento').value=='7diasfull')&&(document.getElementById('habitacion').value!='Cubo')){
			document.getElementById('aviso5').style.display = 'block';
			return false;
		} else {
			/*if (document.getElementById('email').value != (document.getElementById('email2').value)) {
				document.getElementById('aviso').style.display = 'block';
				return false;
			} else {*/
	/*
				if ((document.getElementById('email').value == "") && (document.getElementById('telefono').value == "")) {
					document.getElementById('aviso2').style.display = 'block';
					return false;
				} else {
					document.getElementById('formulario').action = '#';
					document.getElementById('formulario').submit();
					return true;
				}
			//}
		}
	}
	*/
	n=document.getElementById('nombre').value;
	a=document.getElementById('apellidos').value;
	e=document.getElementById('edad').value;
	nif=document.getElementById('nif').value;
	c=document.getElementById('ciudad').value;
	t=document.getElementById('telefono').value;
	em=document.getElementById('email').value;
	num=document.getElementById('numero').value;
	if ((n=='')||(a=='')||(e=='')||(nif=='')||(c=='')||(t=='')||(em=='')||(n=='')){
		alert("Todos los campos marcados son obligatorios.");
	} else {
		if (!mail(em)){
			alert("El email debe tener un formato válido.");
		} else {
			if ((num>0)&&(num<=20)){
				document.getElementById('formulario').action = '#';
				document.getElementById('formulario').submit();
			} else alert("El número de personas debe estar entre 0 y 20.");
		}
	}
}

function validar_formulario(f,direccion){
	var filters = {
		obligatorio: function(el) {return ($(el).val() != '' && $(el).val() != -1);},
		email: function(el) {return /^[A-Za-z_\.][A-Za-z0-9_\.]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/.test($(el).val());},
		telefono: function(el){return /^[0-9]*$/.test($(el).val());}
	};

	$.extend({
		stop: function(e){
		   if (e.preventDefault) e.preventDefault();
		   if (e.stopPropagation) e.stopPropagation();
		}
	});
	
	if (typeof filters == 'undefined') return;
    
	$('#'+f).find("input, textarea, select").each(function(x,el){
        if ($(el).attr("className") != 'undefined') {
        	$.each(new String($(el).attr("className")).split(" "), function(x, klass){
        		if ($.isFunction(filters[klass]))
        			if (!filters[klass](el)){
        				$(el).addClass("error");
        			} else $(el).removeClass("error");
        	});
        }
    });
	
	if ($('#'+f).find(".error").size() > 0) {
		//$.stop(e || window.event);
		alert('Los campos marcados con (*) son obligatorios.');
		return false;
	} else {
		$('#'+f).attr('action',direccion);
		$('#'+f).submit();
	}
}

function isEmailAddress(theElement, nombre_del_elemento ) {
	var s = theElement.value;
	var  filter=/^[A-Za-z][A-Za-z0-9_\.-]*@[A-Za-z0-9_]+.[A-Za-z0-9_.]+[A-za-z]$/;
	if (s.length == 0 ) {
		return true;
	}
	if (filter.test(s)) {
		return true;
	} else alert("Ingrese una dirección de correo válida");
	theElement.focus();
	return false;
}
