$(document).ready(function()
{
	var lastMail = -1;
	var tmtMail = -1;
	var lastTeam = -1;
	var tmtTeam = -1;
	var lastPass = -1;
	var tmtPass = -1;
	var tmtCity = -1;
	var lastCode = -1;
	var tmtCode = -1;

	function checkMail()
	{
		value = $("#email").attr('value');
		if(lastMail != value)
		{
			lastMail = value;

			var pattern = self.MAIL_PATTERN;
			if(!pattern.test(lastMail))
			{
				$("#email-helper").attr('class', 'helper helper-error').html('email non valida');
				return false;
			}

			$.getJSON(BASE_PATH + '/ajax/check-mail/mail/' + escape(lastMail), function(response)
			{
				$("#email-helper").html(response.text);

				if(response.valid)
					$("#email-helper").attr('class', 'helper helper-ok');
				else
					$("#email-helper").attr('class', 'helper helper-error');
			});

		}
	}

	function checkTeam()
	{
		value = $("#squadra").attr('value');
		if(lastTeam != value)
		{
			lastTeam = value;

			var pattern = self.REGEXP_PATTERN;
			if(!pattern.test(lastTeam))
			{
				$("#squadra-helper").attr('class', 'helper helper-error').html(self.REGEXP_MESSAGE);
				return false;
			}

			$.getJSON(BASE_PATH + '/ajax/check-team/team/' + escape(lastTeam), function(response)
			{
				$("#squadra-helper").html(response.text);

				if(response.valid)
					$("#squadra-helper").attr('class', 'helper helper-ok');
				else
					$("#squadra-helper").attr('class', 'helper helper-error');
			});
		}
	}

	function checkPass()
	{
		value = $("#pass").attr('value');

		if(value.length >= 6)
		{
			$("#pass-helper").attr('class', 'helper helper-ok').html('password valida');
		}
		else
		{
			$("#pass-helper").attr('class', 'helper helper-error').html('la password deve essere di almeno 6 caratteri');
		}
	}

	function checkCity()
	{
		value = $("#citta").attr('value');

		var pattern = self.REGEXP_PATTERN;
		if(!pattern.test(value))
		{
			$("#citta-helper").html(self.REGEXP_MESSAGE).attr('class', 'helper helper-error');
		}
		else
		{
			$("#citta-helper").html('citt&agrave; valida').attr('class', 'helper helper-ok');
		}
	}

	function checkCode()
	{
		value = $("#codice").attr('value');
		if(lastCode != value)
		{
			lastCode = value;

			var pattern = /^[0-9A-Fa-f]+$/;
			if(!pattern.test(lastCode) || lastCode.length != 6)
			{
				$("#codice-helper").html('codice non valido').attr('class', 'helper helper-error');
				return false;
			}

			$.getJSON(BASE_PATH + '/ajax/check-code/codice/' + escape(lastCode), function(response)
			{
				if(response.valid)
				{
					$("#codice-helper").attr('class', 'helper helper-ok').html('codice valido');
				}
				else
				{
					$("#codice-helper").html('codice non valido').attr('class', 'helper helper-error');
				}
			});
		}
	}




	// campo email
	$("#email").focus(function()
	{
		$("#email-helper").show();
	});
	$("#email").blur(function()
	{
		if( $("#email-helper").hasClass('helper-info') )
		{
		    $("#email-helper").hide();
		}
	});
	$("#email").keypress(function(e)
	{
		if(e.which > 0)
		{
			clearTimeout(tmtMail);
			$("#email-helper").html('controllo in corso...').attr('class', 'helper helper-check');
			tmtMail = setTimeout(checkMail, 1500);
		}
	});




	// campo password
	$("#pass").focus(function()
	{
		$("#pass-helper").show();
	});
	$("#pass").blur(function()
	{
		if( $("#pass-helper").hasClass('helper-info') )
		{
		    $("#pass-helper").hide();
		}
	});
	$("#pass").keypress(function(e)
	{
		if(e.which > 0)
		{
			clearTimeout(tmtPass);
			$("#pass-helper").html('controllo in corso...').attr('class', 'helper helper-check');
			tmtPass = setTimeout(checkPass, 1500);
		}
	});




	// campo citta
	$("#citta").focus(function()
	{
		$("#citta-helper").show();
	});
	$("#citta").blur(function()
	{
		if( $("#citta-helper").hasClass('helper-info') )
		{
		    $("#citta-helper").hide();
		}
	});
	$("#citta").keypress(function(e)
	{
		if(e.which > 0)
		{
			clearTimeout(tmtCity);
			$("#citta-helper").html('controllo in corso...').attr('class', 'helper helper-check');
			tmtCity = setTimeout(checkCity, 1500);
		}
	});




	// campo squadra
	$("#squadra").focus(function()
	{
		$("#squadra-helper").show();
	});
	$("#squadra").blur(function()
	{
		if( $("#squadra-helper").hasClass('helper-info') )
		{
		    $("#squadra-helper").hide();
		}
	});
	$("#squadra").keypress(function(e)
	{
		if(e.which > 0)
		{
			clearTimeout(tmtTeam);
			$("#squadra-helper").html('controllo in corso...').attr('class', 'helper helper-check');
			tmtTeam = setTimeout(checkTeam, 1500);
		}
	});




	// campo codice
	$("#codice").focus(function()
	{
		$("#codice-helper").show();
	});
	$("#codice").blur(function()
	{
		if( $("#codice-helper").hasClass('helper-info') )
		{
		    $("#codice-helper").hide();
		}
	});
	$("#codice").keypress(function(e)
	{
		if(e.which > 0)
		{
			clearTimeout(tmtCode);
			$("#codice-helper").html('controllo in corso...').attr('class', 'helper helper-check');
			tmtCode = setTimeout(checkCode, 1500);
		}
	});




	// campo provincia
	$("#provincia").focus(function()
	{
		$("#provincia-helper").show();
	});
	$("#provincia").blur(function()
	{
		if( $("#provincia-helper").hasClass('helper-info') )
		{
		    $("#provincia-helper").hide();
		}
	});
	$("#provincia").change(function()
	{
		if($("#provincia option:selected").attr('value') != '-')
		{
			$("#provincia-helper").html('provincia valida').attr('class', 'helper helper-ok');
		}
		else
		{
			$("#provincia-helper").html('seleziona una provincia').attr('class', 'helper helper-error');
		}
	});




	// seleziona automaticamente il campo email
	$("#email").focus().select();




	// invio dei dati
	$("#form-iscrizione").submit(function()
	{
		$("#form-iscrizione").block();

		$.ajax({
			cache: false,
			url: $(this).attr('action'),
			type: $(this).attr('method'),
			data: $(this).serialize(),
			dataType: "json",
			success: function(response) {
				if(response.valid) {
					$("#form-iscrizione").empty();
					var code = "<div class=\"Verde\">Iscrizione effettuata con successo!</div>" +
							   "<div class=\"spazio10\"></div>" +
					           "Hai ricevuto una mail contenente un link per l'attivazione della tua squadra";
					$("#form-iscrizione").html(code);
					//growlUI_ok('Congratulazioni!', 'La tua squadra &egrave; stata iscritta con successo');
					$("#form-iscrizione").unblock();
				} else {
					if(response.field != undefined)	{
						$("#"+response.field+"-helper").attr('class', 'helper helper-error').html(response.text);
					}
					//growlUI_error('Errore!',array['text']);
					$("#form-iscrizione").unblock();
				}
			}
		});

		return false;


 	});

});
