// JavaScript Document

function validaForm(f) {
	
	if ( f.nombre.value == "" && f.apellidos.value == "") {
     alert("La razón social o el nombre y los apellidos son datos obligatorios.")
	 f.nombre.focus()
	 return false
	}
	
	if( f.email.value.length == 0 || !validamail(f.email.value) ) {
		alert("El e-mail parece incorrecto.");
	    f.email.focus();
		return false
    }
	
	var tamcp = 5
	if ( f.codigo_postal.value!= "" && (isNaN(f.codigo_postal.value) || f.codigo_postal.value.length != tamcp) ) {
		  alert("Por favor, escribe los " + tamcp + " dígitos del código postalm, sin espacios.")
		  f.codigo_postal.focus()
		  return false
	}
	
	if ( f.mensaje.value == "" ) {
     alert("El mensaje es obligatorio.")
	 f.mensaje.focus()
	 return false
	}
	
	//Comprueba que se introduzca uno de los dos los telefonos
	//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, sin espacios.")
		  f.telefono.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
}


