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 (nombre=="Edad") {
			if (isNaN(elcontenido)){
				mostrar_error(nombre + " solo puede contener números.");							
				return false;	
			} else {
				if (elcontenido>longitud){
					mostrar_error("Seguro que eres tan viejo.");							
					return false;	
				}
				if (elcontenido<0){
					mostrar_error(nombre + " incorrecta.");							
					return false;	
				}
			}
		} else {
			if ((elcontenido.length != longitud) || (isNaN(elcontenido))){
				mostrar_error(nombre + " es incorrecto.");							
				return false;	
			}				
		}
		if ((nombre=="Código postal") && ((elcontenido>=53000) || (elcontenido<1000))) {
			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 (/^\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(){
	
	// Validacion de Email 
	$("#email").blur(function () {		
		var resultado='';		
		resultado=check_fields("e","Email",$(this).val(),0);
		if (resultado != false) {
			ocultar_error();
			$(this).removeClass("input_formp1_error");
			$(this).addClass("input_formp1");	
			$(this).val(resultado);		  
		} else {
			$(this).removeClass("input_formp1");
			$(this).addClass("input_formp1_error");	
		}			
	});	

// Validacion de Asunto	
	$("#asunto").blur(function () {		
		var resultado='';		
		resultado=check_fields("t","Asunto",$(this).val(),50);
		if (resultado != false) {
			ocultar_error();
			$(this).removeClass("input_formp1_error");
			$(this).addClass("input_formp1");	
			$(this).val(resultado);
		} else {
			$(this).removeClass("input_formp1");
			$(this).addClass("input_formp1_error");	
		}			
	});	

// Validacion del formulario
	$("#continuar").click(function () {			
		var hayError=0;
		var camposObligatorios=['email','asunto'];
		
		$("form").each(function() {
			$("input").each(function() {
				if ($(this).attr("class")=='input_formp1_error') {
					hayError=1;
				}
				if ((jQuery.inArray($(this).attr("id"),camposObligatorios)>-1) && ($(this).val()=='')) {
					hayError=2;
					$(this).removeClass("input_formp1");
					$(this).addClass("input_formp1_error");
				}
			});			
		});
		if(hayError==1) {
			mostrar_error("El registro contiene errores, compruébelo antes de continuar.");
		} 
		if(hayError==2) {
			mostrar_error("Existen campos obligatorios por cumplimentar.");
		} 			
		if (hayError==0){			
			if ( !($("#acepto").is(":checked")) ) {
				hayError=3;
				$("#acepto_politica").css({'background-color' : '#f6f6cd'});
				mostrar_error("Debe aceptar la pol&iacute;tica de protecci&oacute;n de datos de Consupermiso.");				
			} else {
				$("#acepto_politica").css({'background-color' : 'white'});
			}
		}
// Si no hay error procedemos al submit		
		if (hayError==0){
			$("#form_contacto").attr("action","contacto.asp"); 
			$("#form_contacto").submit();
		}
	});	
			
});
