/**************************************************************************************************************************
**
**				Formulario contacto - Webmet (Contacto)
**
**************************************************************************************************************************/
/* funciones eventos formulario 
	Observación: reestructurar esta parte (más corta)
*/
 // Nombre
 function controlBlurNombre(){
 	if(this.value=='') {
    	this.value="Nombre";
    }
 }
 function controlFocusNombre(){
    if(this.value=="Nombre") {
        this.value='';
    }
 }
// Email
 function controlBlurEmail(){
 	if(this.value=='') {
    	this.value="Email";
  	}
 }
 function controlFocusEmail(){
    if(this.value=="Email") {
       this.value='';
	}
 } 
 // Telefono
 function controlBlurTelefono(){
	if(this.value=='') {
       this.value="Tel\u00E9fono";
    }
                                       
 }
 function controlFocusTelefono(){
    if(this.value=="Tel\u00E9fono") {
       this.value='';
    }
 }
//Empresa
 function controlBlurEmpresa(){
   if(this.value=='') {
      this.value="Empresa";
   }
 }
 function controlFocusEmpresa(){
   if(this.value=="Empresa") {
      this.value='';
   }
                                        
 }
// Web
 function controlBlurWeb(){
   if(this.value=='') {
      this.value="Web";
   }
                                       
 }
 function controlFocusWeb(){
   if(this.value=="Web") {
      this.value='';
 	}
                                        
 }
// Mensaje
 function controlBlurMensaje(){
   if(this.value=='') {
      this.value="Escribe aqu\xed";
   }
   this.style.overflow="hidden";
   this.style.fontSize="19px";
 }
 function controlFocusMensaje(){
  if(this.value=="Escribe aqu\xed") {
  this.value='';
 }
  this.style.overflow="scroll";
  this.style.fontSize="12px";					
 }
/** 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    ----- */
function validohome() {
	var error=false;
	var form_contacto = document.getElementById('IDform_contacto');
	
		with (form_contacto) {
			with(nombre){

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

				if ((value.length==0) || (value=="Tel\u00E9fono")){
					// Añadimos el error
					mostrarError('Falta tel\u00E9fono.','telefono');
					error=true;
				} else if (value.length>25){
					// Añadimos el error
					mostrarError('Max 25 cifras.','telefono');
					error=true;
				} else {
					borrarError("telefono");
										
				}
				
			}
		}
		
		with (form_contacto) {
			with(empresa){

				if ((value.length==0) || (value=="Empresa")) {
					// Añadimos el error
					mostrarError('Falta empresa.','empresa');
					error=true;
				}else{
					borrarError("empresa");	
				}
			}
		}
		
		with (form_contacto) {
			with(mensaje){

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

function inicializa() {
	/* Eventos onblur y onfocus
		--- Observación: reestructurar esta parte (hacerla más corta)
	*/
	
                                  
   textoNombre = document.getElementById('nombre'); 
                                    
   if (textoNombre){
                                        
   		textoNombre.onblur=controlBlurNombre;
   		textoNombre.onfocus=controlFocusNombre;
   }
									

									
   textoEmail = document.getElementById('email'); 
                                    
   if (textoEmail){
                                        
       textoEmail.onblur=controlBlurEmail;
       textoEmail.onfocus=controlFocusEmail;
   }
	
									
   textoTelefono = document.getElementById('telefono'); 
                                    
    if (textoTelefono){
                                        
       textoTelefono.onblur=controlBlurTelefono;
       textoTelefono.onfocus=controlFocusTelefono;
    }
									
	
									
   textoEmpresa = document.getElementById('empresa'); 
                                   
   if (textoEmpresa){
                                        
      textoEmpresa.onblur=controlBlurEmpresa;
      textoEmpresa.onfocus=controlFocusEmpresa;
   }
									
									
									
   textoWeb = document.getElementById('web'); 
                                    
   if (textoWeb){
                                        
      textoWeb.onblur=controlBlurWeb;
      textoWeb.onfocus=controlFocusWeb;
   }
									
									
   textoMensaje = document.getElementById('mensaje'); 
                                    
   if (textoMensaje){
                                        
      textoMensaje.onblur=controlBlurMensaje;
      textoMensaje.onfocus=controlFocusMensaje;
   }
									
	/* ----- FORMULARIO CONTACTO   ---------------------------------- */
	/* -------------------------------------------------------------- */
	
	FormularioContacto=document.getElementById('IDform_contacto');
	// Si no esta definido, no hay evento submit
	if (FormularioContacto){
		FormularioContacto.onsubmit= validohome; 
	}
   			
}

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