var tempError;

/*
function mostrar_error(texto) {	
	clearTimeout(tempError);
	$("#texto_error").html(texto);
	$("#ventana_error").animate({ 
      top: "0px"
    }, 500 );
  tempError=setTimeout(ocultar_error, 7000);  
}

function ocultar_error() {  
  $("#ventana_error").animate({ 
      top: "-170px"
    }, 500 );    
    clearTimeout(tempError);
}		
*/

function check_fields(tipo,nombre,valor,longitud) {
	var elcontenido=jQuery.trim(valor);
	//elcontenido=elcontenido.replace(/^\s*|\s*$/g,"");	

//	Control campos alfanumericos
	if (tipo=="t") {		
		if (elcontenido == '') {
			mostrar_error(nombre + " es obligatorio.");							
			return false;					
		}
		if (elcontenido.length > longitud) {
			mostrar_error(nombre + " debe tener menos de " + longitud + " carácteres.");							
			return false;							
		}		
		ocultar_error();
		return elcontenido;
	}
//	Control campos numericos	
	if (tipo=="n") {
		if (elcontenido == '') {
			mostrar_error(nombre + " es obligatorio.");							
			return false;
		}		
		if ((elcontenido.length != longitud) || (isNaN(elcontenido))){
				mostrar_error(nombre + " es incorrecto.");							
				return false;								
		}		
		ocultar_error();
		return elcontenido;
	}
	
//	Control campos email
	if (tipo=="e") {		
		if (elcontenido == '') {
			mostrar_error(nombre + " es obligatorio.");							
			return false;
		}		
		
		if (elcontenido.length > longitud) {
			mostrar_error(nombre + " debe tener menos de " + longitud + " carácteres.");							
			return false;							
		}		
		
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(elcontenido)){				
			ocultar_error();		  
	  } else {
	  	mostrar_error(nombre + " tiene un formato incorrecto.");							
			return false;
	  }	
	  ocultar_error();	
	  return elcontenido;
	}
}
	
$("#close_message").click(function() {
  ocultar_error();
});

// Validacion de campos del formulario

$(document).ready(function(){
	
	$('form').keypress(function(e){    
    if(e == 13){ 
      $("#continuar").click(); 
    } 
  }); 
 
  $('input').keypress(function(e){ 
    if(e.which == 13){ 
      $("#continuar").click();; 
    } 
  }); 
	
// Validacion de usuario	
	$("#email").blur(function () {		
		var resultado='';		
		resultado=check_fields("e","Email",$(this).val(),50);
		if (resultado != false) {
			$.get("check_email.asp", { id: resultado },
		  	function(data){
		  		var result = data;
		    	if (result.indexOf('invalid')==-1) {
		    		mostrar_error(resultado + " no existe en la base de datos.");							
						$("#email").removeClass("inputacceso");
						$("#email").addClass("inputacceso_error");			    		
		    	} else {
		    		ocultar_error();
						$("#email").removeClass("inputacceso_error");
						$("#email").addClass("inputacceso");	
						$("#email").val(resultado);		    		
		    	}		    			    	
		  },"text");
		} else {
			$(this).removeClass("inputacceso");
			$(this).addClass("inputacceso_error");	
		}			
	});
		
// Validacion del formulario
	$("#continuar").click(function () {					
		var hayError=0;
		var camposObligatorios=['email'];		
		$("form").each(function() {
			$("input").each(function() {
				if ($(this).attr("class")=='inputacceso_error') {
					hayError=1;
				}
				if ((jQuery.inArray($(this).attr("id"),camposObligatorios)>-1) && ($(this).val()=='')) {
					hayError=2;
					$(this).removeClass("inputacceso");
					$(this).addClass("inputacceso_error");
				}				
			});
		});
		if(hayError==1) {
			mostrar_error("El formulario de acceso contiene errores, compruébelo antes de continuar.");
		} 
		if(hayError==2) {
			mostrar_error("Existen campos obligatorios por cumplimentar.");
		} 
		// Si no hay error procedemos al submit		
		if (hayError==0){			
			$("#form_olvida").attr("action","olvida.asp"); 
			$("#form_olvida").submit();
		}
	});	
			
});
