$().ready(function() {
	$('#loginsubscribe').alphanumeric({allow:" -_{}[]()+.&"});
	$('#emailsubscribe').alphanumeric({allow:" _.-@"});

	// Controle validite LOGIN
		$('#loginsubscribe').blur(function() {
			var id = $(this).attr("id"); 
			if ($(this).val().length > 0)
			{
				if ($(this).val().length >= 5 && $(this).val().length <= 20)
				{
					$("#ErrXMS" + id).hide();
					$("#ErrXMS2" + id).hide();
					$.ajax({
						type: "POST",
						async: false,
						url: "_Process/NewLoginExists.php",
						data: "Login=" + $("#loginsubscribe").val(),
						success: function(data, textStatus){
							if (data != 0) {
								$("#ErrXMS2" + id).html(data).show();
								$(this).val("");
							}
							else 
							{
								$("#ErrXMS" + id).hide();
								$("#ErrXMS2" + id).hide();
							}
						}
					});
				}
				else
					$("#ErrXMS" + id).show();
			}
			else
			{
				$("#ErrXMS" + id).hide();
				$("#ErrXMS2" + id).hide();
			}
		});


	// Controle validite EMAIL
		$('#emailsubscribe').blur(function() {
			var id = $(this).attr("id"); 
			if ($(this).val().length > 0)
			{
				if ($(this).val().length >= 7 && $(this).val().length <= 70)
				{
					$.ajax({
						type: "POST",
						async: false,
						url: "_Process/NewEmailExists.php",
						data: "Email=" + $(this).val(),
						success: function(data, textStatus){
							if (data != 0) {
								$("#ErrXMS" + id).html(data).show();
								$(this).val("");
							}
							else if (data == 2) {
								$("#ErrXMS" + id).html(data).show();
								$(this).val("");
							}
							else 
								$("#ErrXMS" + id).hide();
						}
					});
				}
				else
					$("#ErrXMS" + id).show();
			}
			else
				$("#ErrXMS" + id).hide();
		});
	


	// Controle validite PASSWORD
		$('#passwordVal').blur(function() {
			var id = $(this).attr("id"); 
			if ($(this).val().length > 0)
			{
				if ($(this).val().length < 5 || $(this).val().length > 15)
					$("#ErrXMS" + id).show();
				else
					$("#ErrXMS" + id).hide();
			}
			else
				$("#ErrXMS" + id).hide();
		});


	// Controle validite PASSWORD
		$('#password_confirm').blur(function() {
			var id = $(this).attr("id"); 
			if ($(this).val() != $('#passwordVal').val())
				$("#ErrXMS" + id).show();
			else
				$("#ErrXMS" + id).hide();
		});


	// Controle validite DATE ANNIVERSAIRE
		function ControlBirthDateGiven() {
			if ($("#BY option:selected").val() >= 1900 && $("#BM option:selected").val() >= 1 && $("#BD option:selected").val() >= 1)
				$("#ErrXMSbirthday").hide();
		};
		$('#BY').blur(ControlBirthDateGiven);
		$('#BM').blur(ControlBirthDateGiven);
		$('#BD').blur(ControlBirthDateGiven);

		$("#CGU").change(function () {
			if ($("#CGU:checked").length != 0) $("#ErrXMSMsgCGU").hide();
		});


	// Controle Data avant Submit
		$('#register_new_costumer').submit(function() {
			var errorForm = 0;
			
			if ($("#BY option:selected").val() == "" || $("#BM option:selected").val() == "" || $("#BD option:selected").val() == "")
			{
				$("#ErrXMSbirthday").show();
				errorForm = 1;
			}
			
			if ($("#CGU:checked").length == 0)
			{
				$("#ErrXMSMsgCGU").show();
				errorForm = 1;
			}

			if ($('#emailsubscribe').val().length == 0 || $('#emailsubscribe').val().length < 7 || $('#emailsubscribe').val().length > 70)
			{
				$("#ErrXMSemailsubscribe").show();
				errorForm = 1;
			}
			
			if ($('#loginsubscribe').val().length == 0 || $('#loginsubscribe').val().length < 5 || $('#loginsubscribe').val().length > 20)
			{
				$("#ErrXMSloginsubscribe").show();
				errorForm = 1;
			}
		
			if ($('#passwordVal').val().length == 0 || $('#passwordVal').val().length < 5 || $('#passwordVal').val().length > 15)
			{
				$("#ErrXMSpasswordVal").show();
				errorForm = 1;
			}

			if ($('#password_confirm').val() != $('#passwordVal').val())
			{
				$("#ErrXMSpassword_confirm").show();
				errorForm = 1;
			}
			
			if (errorForm == 1) return false;
		});


});

