	
$(document).ready(function(){
	var locales = {
		'es': {
			"LOADING":		"CARGANDO...espere por favor",
			"SEND":			"ENVIAR",
			"OK": 			"OK! Tus datos se han enviado!",
			"ERR_CONFIRM":	"OK! Tus datos se han enviado!", //"Error. No se pudo enviar confirmacion",
			"ERR_SYSTEM":	"Error. Fallo conectando con el servidor de listas",
			"ERR_ALREADY":	"OK! Pero tu 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_FORM":		"Error. Hay un error en el formulario",
			
			"ERR_COND":		"Error. Debes aceptar las condiciones de uso",
			"ERR_NOEMAIL":	"Error. Debes rellenar tu e-mail",
			"ERR_NONAME":	"Error. Debes rellenar tu nombre",
			"ERR_NOSURNAME":"Error. Debes rellenar tus apellidos",
			"ERR_NONAT":	"Error. Debes rellenar tus nacionalidad"
		},
		'en': {
			"LOADING":		"LOADING...please wait",
			"SEND":			"SEND",
			"OK": 			"OK! Your data has been sent!",
			"ERR_CONFIRM":	"OK! Your data has been sent!",
			"ERR_SYSTEM":	"Error. Error connecting with the lists server",
			"ERR_ALREADY":	'OK! But your email is already on the list!',
			"ERR_BADEMAIL":	"Error. E-mail address format is not valid",
			"ERR_BADLIST":	"Error. Erroneous maillist",
			"ERR_FORM":		"Error. Form Error",
			
			"ERR_COND":		"Error. You must accept the privacy policy",
			"ERR_NOEMAIL":	"Error. E-mail address is required",
			"ERR_NONAME":	"Error. Your name is required",
			"ERR_NOSURNAME":"Error. Your surname is required",
			"ERR_NONAT":	"Error. Your nationality is required"
		}
	}
	var locale = 'en';
	var local = locales[locale];
	
	

	$("#error").fadeOut();


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

	$(".submit").click(function(e){
		if( $(this).attr('disabled') == 'disabled' ) return;
		$(this).attr('disabled','disabled').css({opacity:'0.5'}); // = true;
/*		e.stopPropagation();*/
		e.preventDefault();

		var	btn = $(this);
		var form = $(this).parent();
		var errorHolder = form.find('.error');
		
		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 = form.find('[name="email"]').val();
		var condicionesVal = (form.find('[name="condiciones"]').attr('checked') == undefined) ? false : true;
		var listaVal = form.find('[name="lst"]').val();
		var wait = 2000;
		
		if(condicionesVal != true) {
			errorHolder.html(local['ERR_COND']).slideDown('slow').animate({opacity: '+=0'}, wait).slideUp('slow', function(){
				btn.attr('disabled',false).css({opacity:1});
			});
			hasError = true;
		} else if(emailVal == 'EMAIL' || emailVal == '') {
			errorHolder.html(local['ERR_NOEMAIL']).slideDown('slow').animate({opacity: '+=0'}, wait).slideUp('slow', function(){
				btn.attr('disabled',false).css({opacity:1});
			});
			hasError = true;
		} else if(!emailReg.test(emailVal)) {	
			errorHolder.html(local['ERR_BADEMAIL']).slideDown('slow').animate({opacity: '+=0'}, wait).slideUp('slow', function(){
				btn.attr('disabled',false).css({opacity:1});
			});
			hasError = true;
		}	

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

});

