$(document).ready(function(){

	$("#error").slideUp(0);

	$("#email").click(function(e){
		$(this).val('');
	});

	$("#submit").click(function(e){
		$("#submit").attr('disabled','disabled');

		e.stopPropagation();
		e.preventDefault();

		var listaVal = $("#lista").val();
				
		var loc = 'es';
		var locales_es = {
			"OK": 			"OK! Tu e-mail ha sido añadido correctamente!",
			"ERR_CONFIRM":	"Error. No se pudo enviar confirmacion",
			"ERR_SYSTEM":	"Error. Fallo conectando con el servidor de listas",
			"ERR_ALREADY":	"Error. El e-mail ya está en la lista",
			"ERR_BADEMAIL":	"Error. El e-mail tiene un formato erroneo o invalido",
			"ERR_BADLIST":	"Error. Lista erronea",
			
			"ERR_COND":		"Error. Debes aceptar y entender las condiciones de uso",
			"ERR_NOEMAIL":	"Error. Debes rellenar tu e-mail"
		};
		var locales = {
			"es": locales_es
		}
		var local = locales[loc];
		

		var hasError = false;
		var emailReg = /^[a-zA-Z0-9._-]+([+][a-zA-Z0-9._-]+){0,1}[@][a-zA-Z0-9._-]+[.][a-zA-Z]{2,6}$/;

		var emailVal = $("#email").val();
		if(emailVal == 'EMAIL' || emailVal == '') {
			$("#error").html(local['ERR_NOEMAIL']).slideDown('slow').animate({opacity: '+=0'}, 2000).slideUp('slow', function(){
				$("#submit").attr('disabled',false);
			});
			hasError = true;
		} else if(!emailReg.test(emailVal)) {	
			$("#error").html(local['ERR_BADEMAIL']).slideDown('slow').animate({opacity: '+=0'}, 2000).slideUp('slow', function(){
				$("#submit").attr('disabled',false);
			});
			hasError = true;
		}


		if(hasError == false) {
			//$("#maillist").append('<img src="/wp-content/themes/default/images/template/loading.gif" alt="Loading" id="loading" />');
			$.post(
				"/web/es/player/proxy",
				{ email: emailVal, lista: listaVal },
				function(data,status){
					switch(status){
						case 'success':
						case 'notmodified':
							texto = local[data];
							if(data=='OK'){
								$("#email").val('EMAIL');
							}
							break;
						case 'error':
						case 'timeout':
						case 'parsererror': 
						default:
							texto = 'Error de comunicación'
							break;
					}
					$("#error").html(texto).slideDown('slow').animate({opacity: '+=0'}, 3000).slideUp('slow', function(){
						$("#submit").attr('disabled',false);
					});
				}
			);
		}
	});


});