/** Validar E-Mail
 * 
 * @param {Object} valor: valor del e-mail
 */
function validoEmail(valor)	{
	return /^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/.test(valor);
}

/* ----- FUNCION DE BORRAR ERRORES - FORMULARIO CONTACTO ---- */
/* -------------------------------------------------------------- */
/** borra la capa de error de los errores presentes en el formulario registrarse
 * 
 * @param {Object} mensaje
 */

function borrarElemento() {
	
	this.parentNode.removeChild(this);
	
}

function borrarError(idInput){
	
	var capaError	=	document.getElementById("IdError"+idInput);
		
	// Si existe el elemento lo quitamos 
	if (capaError){
		$fx(capaError).fxAdd({type: 'opacity', to: 0, step: -10, delay: 25}).fxRun(borrarElemento);			
	}
	// Volvemos a renombrar el input con el atributo original caja
	document.getElementById(idInput).setAttribute("class", "");
}

/* ----- FUNCION DE MOSTRAR ERRORES - FORMULARIO CONTACTO    ---- */
/* -------------------------------------------------------------- */ 
/** presenta los errores del formulario registrarse
 * 
 * @param {Object} mensaje
 * @param {object} idImput
 */

function mostrarError(mensaje,idInput){
	
	// Elementos
	var capaError       =	document.createElement("div");
	var capaErrorStart  =	document.createElement("div");
	var capaErrorContent=	document.createElement("div");
	var capaErrorEnd    =	document.createElement("div");
	var capaPadre		=   document.getElementById(idInput).parentNode;
	var mensajeError	=	mensaje;
	
	//Si no está creado el mensaje, se crea
	if (!(document.getElementById("IdError" + idInput))) {
		
		// añadimos red para que tome estilos
		document.getElementById(idInput).setAttribute("class", "red");
		
		// Atributos
		capaError.setAttribute("class", "error");
		capaError.setAttribute("id", "IdError" + idInput);
		capaErrorStart.setAttribute("class", "error_start");
		capaErrorContent.setAttribute("class", "error_content");
		capaErrorEnd.setAttribute("class", "error_end");
		
		
		// Construcción del mensaje
		capaErrorContent.innerHTML = '<p>' + mensajeError + '</p>';
		capaError.appendChild(capaErrorStart);
		capaError.appendChild(capaErrorContent);
		capaError.appendChild(capaErrorEnd);
		
		// Localización en DOM
		capaPadre.appendChild(capaError);
		
		$fx(capaError).fxAdd({
			type: 'opacity',
			from: 0,
			to: 100,
			delay: 25
		}).fxRun();
			
	}
		
}

/* ----- FUNCION DE COMPROBACION DEL FORMULARIO CONTACTO    ----- */
/* -------------------CURSOS ESPECÍFICOS------------------------- */

/** 
 *   CURSO-FUTBOL, CURSO-PERIODISMO-DEPORTIVO, MASTER-ÁSTRONOMIA 
 */
function validocontacto() {
	var error=false;
	var form_contacto = document.getElementById('IDformulario');
	
		with (form_contacto) {
			with(nombre){

				if (value.length==0) {
					// Añadimos el error
					mostrarError('Falta nombre.','nombre');
					error=true;
				}else{
					borrarError("nombre");	
				}
			}
		}
			
		with (form_contacto) {
			with(email){
				
				if (!validoEmail(value)) {
				
					// añadimos el error
					mostrarError("Falta email.","email");
					error=true;
				}
				else {
					 borrarError("email");
				}
				
			}
		}	
		
		with (form_contacto) {
			with(mensaje){

				if (value.length==0) {
					// Añadimos el error
					mostrarError('Falta mensaje.','mensaje');
					error=true;
				}else{
					borrarError("mensaje");	
				}
			}
		}
		
	return !error;
	
}

function inicializa() {
	
	/* ----- FORMULARIO CONTACTO   ---------------------------------- */
	/* -------------------------------------------------------------- */
	
	FormularioContacto=document.getElementById('IDformulario');
	// Si no esta definido, no hay evento submit
	if (FormularioContacto){
		FormularioContacto.onsubmit= validocontacto; 
	}
   			
}

 // Enlazamos el evento onload con la función
 window.onload = inicializa; 
