$(function() {
	var truePicture = '<img src="/images/correct.gif" width="16" height="16" alt="Correct" />';
	var falsePicture = '<img src="/images/incorrect.gif" width="16" height="16" alt="Correct" />';
	
	$("input[name=user[first_name]]").keyup(function(){
		if($(this).val().length > 1){
			$(this).next(".error_first_name").empty();
			$(this).next(".error_first_name").append(truePicture);
		} else {
			$(this).next(".error_first_name").empty();
			$(this).next(".error_first_name").append(falsePicture);
	
		}
	});
	$("input[name=user[last_name]]").keyup(function(){
		if($(this).val().length > 1){
			$(this).next(".error_last_name").empty();
			$(this).next(".error_last_name").append(truePicture);
		} else {
			$(this).next(".error_last_name").empty();
			$(this).next(".error_last_name").append(falsePicture);
		}
	});
	$("input[name=user[email]]").keyup(function(){
		if($(this).val().match(/^[a-z0-9\-_.]+@[a-z0-9\-_.]+\.[a-z]{2,4}$/i)){
			$(this).next(".error_email").empty();
			$(this).next(".error_email").append(truePicture);
		} else {
			$(this).next(".error_email").empty();
			$(this).next(".error_email").append(falsePicture);
		}
	});
	$("input[name=user[login]]").keyup(function(){
		if($(this).val().length > 1){
			$(this).next(".error_login").empty();
			$(this).next(".error_login").append(truePicture);
		} else {
			$(this).next(".error_login").empty();
			$(this).next(".error_login").append(falsePicture);
		}
	});
	$("input[name=user[password]]").keyup(function(){
		if($(this).val().length > 3 ){
			$(this).next(".error_password").empty();
			$(this).next(".error_password").append(truePicture);
		} else {
			$(this).next(".error_password").empty();
			$(this).next(".error_password").append(falsePicture);
		}
	});
	$("input[name=user[confirm_password]]").keyup(function(){
		if($(this).val().length > 3 && $(this).val() == $("input[name=user[password]]").val() ){
			$(this).next(".error_confirm_password").empty();
			$(this).next(".error_confirm_password").append(truePicture);
			$(".error_password").empty();
			$(".error_password").append(truePicture);
		} else {
			$(this).next(".error_confirm_password").empty();
			$(this).next(".error_confirm_password").append(falsePicture);
			$(".error_password").empty();
			$(".error_password").append(falsePicture);
		}
	});
	
		

	$(" #formsubscribe").submit(function() {

		$(this).find(" .submit1").fadeOut();
		
		first_name			= $(this).find("input[name=user[first_name]]").val();
		last_name			= $(this).find("input[name=user[last_name]]").val();
		email				= $(this).find("input[name=user[email]]").val();
		login				= $(this).find("input[name=user[login]]").val();
		password			= $(this).find("input[name=user[password]]").val();
		confirm_password	= $(this).find("input[name=user[confirm_password]]").val();

		var valid = true;
		if(first_name == "" ){
			$(" .error_first_name").fadeIn().text("Veuillez entrer votre nom");
			valid = false;
		}
		if(last_name == "" ){
			$(" .error_last_name").fadeIn().text("Veuillez entrer votre prénom");
			valid = false;
		}
		 if (!email.match(/^[a-z0-9\-_.]+@[a-z0-9\-_.]+\.[a-z]{2,4}$/i)) {
			$(" .error_email").fadeIn().text("Veuillez entrer un email valide");
			valid = false;
		}
		if(login == "" ){
			$(" .error_login").fadeIn().text("Veuillez entrer votre login");
			valid = false;
		}
		if(password == "" ){
			$(" .error_password").fadeIn().text("Veuillez entrer un mot de passe");
			valid = false;
		}
		if(confirm_password == "" ){
			$(" .error_confirm_password").fadeIn().text("Veuillez confirmer votre mot de passe");
			valid = false;
		}
		if(confirm_password != "" && confirm_password != password ){
			$(" .error_confirm_password").fadeIn().text("le mot de passe et sa confirmation ne correspondent pas.");
			valid = false;
		}
		
		if(valid == true){
			$.post($(this).attr('action'), {
				'user[first_name]' : first_name,
				'user[last_name]' : last_name,
				'user[email]' : email,
				'user[login]' : login,
				'user[password]' : password,
				'user[confirm_password]' : confirm_password,
			}, function(data){
				$(" #form_content").empty();
				$(" #form_content").append(data);
			});
		} else {
			$(this).find(" .submit1").fadeIn();
		}
		
		return false;
	});
});
