$(document).ready(function(){
	
	// Primer clic en la caja de texto
	$("#alertas input.text").click(function(){
		if( !$(this).attr("rel") ) {
			$(this).val("");
			$(this).attr("rel", 1);
		}
	});
	
	// Si se hace un Enter, emular un clic en el botón
	$("#alertas input.text").keypress(function(e){
		if( e.which == 13 )
		{
			$("div#alertas :button").click();
			e.preventDefault();
		}
	});
	
	// Clic en el enlace
    $("#alertas a").click(function(){
    	$(this).attr("href", "javascript:void(0)");
    	$(this).parent().parent().css("height", "102px");
    });
    
    // Clic en el botón
    $("div#alertas :button").click(function(){
    	$(this).attr("disabled", "disabled");
    	$("div#alertas :input[type=text]").attr("disabled", "disabled").removeClass("campo_error");
    	$("div#alertas div#msg_estado").removeClass("error").removeClass("ok").html("Procesando...");
    	$("div#alertas").css("height", "120px");
		$.post("/marcar/alertasEmail", {email: $("div#alertas :input[type=text]").val(),
									categoria: $("div#alertas #id_categoria").val(),
									subcategoria: $("div#alertas #id_subcategoria").val(),
									zona: $("div#alertas #id_zona").val()},
				function(data){
					error = false;
					if( data == 1 )
					{
						$("div#alertas div#msg_estado").addClass("error").html("Por favor, introduce tu e-mail.");
						error = true;
					}
					else if( data == 2 )
					{
						$("div#alertas div#msg_estado").addClass("error").html("E-mail no válido.");
						error = true;
					}
					else if( data == 3 )
					{
						$("div#alertas div#msg_estado").addClass("ok").html("Tu e-mail ya estaba registrado.");
					}
					else
					{
						$("div#alertas div#msg_estado").addClass("ok").html("Tu e-mail ha sido registrado.");
					}
					
					if( error )
					{
						$("div#alertas :button").removeAttr("disabled");
						$("div#alertas :input[type=text]").addClass("campo_error").removeAttr("disabled").focus();
					}
		});
    });
});