// JavaScript Document

function validaForm(f) {
	
	if ( f.nombre.value == "" ) {
		alert("El nombre es obligatorio.")
		f.nombre.focus()
		return false
	}
	
	if ( f.apellidos.value == "" ) {
		alert("los apellidos son obligatorios.")
		f.apellidos.focus()
		return false
	}
	
	if ( f.direccion.value == "" ) {
		alert("La direccion es obligatorio.")
		f.direccion.focus()
		return false
	}
	
	if ( f.poblacion.value == "" ) {
		alert("La poblacion es obligatoria.")
		f.poblacion.focus()
		return false
	}	
	
	
	if ( f.codigo_postal.value == "" ) {
		alert("El código postal es obligatorio.")
		f.codigo_postal.focus()
		return false
	} else {
		if ( f.codigo_postal.value.length < 5 || isNaN(f.codigo_postal.value) || 
	     	 f.codigo_postal.value.substring(0,2) < 1 || f.codigo_postal.value.substring(0,2) > 52 ) {
			alert("El código postal no es correcto.")
			f.codigo_postal.focus()
			return false
		}	 
	}
	
	//Comprueba que se introduzca uno de los dos los telefonos
	if(f.telefono.value=="" && f.movil.value=="") {
		alert("Introduce uno de los dos teléfonos.")
		f.telefono.focus()
		return false
	}
	
	//Comprueba que se introduzcan los telefonos
		
	var tamTelf = 9
	if ( f.telefono.value!="" && (isNaN(f.telefono.value) || f.telefono.value.length != tamTelf) ) {
		  alert("Por favor, escribe los " + tamTelf + " dígitos de tu teléfono fijo.")
		  f.telefono.focus()
		  return false
	}
	if ( f.movil.value!="" && (isNaN(f.movil.value) || f.movil.value.length != tamTelf) ) {
		  alert("Por favor, escribe los " + tamTelf + " dígitos de tu teléfono móvil.")
		  f.txtMovil.focus()
		  return false
	}

	if ( f.email.value == "" ) {
		alert("El Email de contacto es obligatorio.")
		f.txtEmail.focus()
		return false
	}	
	
	
	if( f.email.value.length == 0 || !validamail(f.email.value) ) {
		alert("El E-mail parece incorrecto.");
	    f.email.focus();
		return false
    }

	
	// Todo OK, enviamos el formulario
	return true
}





function validamail(email){

	// Mínimo de 5 caracteres
	if (email.length < 5)
		return false
		
	// Cadena de caracteres no permitidos
	var iChars = "+*|,\":<>[]{}`';()&$#% ";	
	
	// Primero comprobamos que en el email no haya algún 
	// caracter no permitido
	var eLength = email.length;	
	for (var i=0; i < eLength; i++)	{		
		if (iChars.indexOf(email.charAt(i)) != -1)
			return false
	}	
	
	// Comprobamos que la @ tenga algún caracter delante y alguno detrás
	var atIndex = email.lastIndexOf("@");	
	if(atIndex < 1 || (atIndex == eLength - 1))
		return false

	// Comprobamos que exista '.' a partir del cuarto carácter, pero
	// que no acabé en '.'
	var pIndex = email.lastIndexOf(".");	
	if(pIndex < 3 || (pIndex == eLength - 1))	
		return false;	

	// Por último, comprobamos que el punto esté detrás de la @
	if(atIndex > pIndex)	
		return false	

	return true
}

