$(document).ready(function() {
	var checkUser = function (username) {
		//remove all the class add the messagebox classes and start fading
		$("#msgbox").removeClass().addClass('messagebox').text('').fadeIn("slow");
		var username = $('#username').val();
		$.post(
			"public/Users/validateForm.php",
			{check: 'username', user: username},
			function(data) {
				if(data=='no') { //if username not avaiable
				  	$("#msgbox").fadeTo(200,0.1,function() { //start fading the messagebox
						//add message and change the class of the box and start fading
			  			$(this).html('<img src="images/Varies/icon_ajax_false.gif" /> Username gi&agrave; in uso.').addClass('messageboxerror').fadeTo(900,1);
					});		
          		}
		    else if((username.length < 3 || username.length > 20)) { //if username not avaiable
				  	$("#msgbox").fadeTo(200,0.1,function() { //start fading the messagebox
						//add message and change the class of the box and start fading
			  			$(this).html('<img src="images/Varies/icon_ajax_false.gif" /> L\'Username &egrave; troppo corta o troppo lunga.').addClass('messageboxerror').fadeTo(900,1);
					});		
          		}
		  		else {
		  			$("#msgbox").fadeTo(200,0.1,function() {  //start fading the messagebox
			  			//add message and change the class of the box and start fading
			 		 	$(this).html('<img src="images/Varies/icon_ajax_true.gif" />').addClass('messageboxok').fadeTo(900,1);	
					});
		  		}
			}			
		)
	}
	var checkPass = function (password) {
		$("#pmsgbox").removeClass().addClass('messagebox').text('').fadeIn("slow");
		var password = $('#password').val();
		var passwordc = $('#passwordc').val();
		if(password != passwordc) {
			$("#pmsgbox").fadeTo(200,0.1,function() { //start fading the messagebox
				//add message and change the class of the box and start fading
				$(this).html('<img src="images/Varies/icon_ajax_false.gif" /> Le password non corrispondono').addClass('messageboxerror').fadeTo(900,1);
			});
		}
		else if((password.length < 6 || password.length > 30) || (passwordc.length < 6 || passwordc.length > 30)) {
			$("#pmsgbox").fadeTo(200,0.1,function() { //start fading the messagebox
				//add message and change the class of the box and start fading
				$(this).html('<img src="images/Varies/icon_ajax_false.gif" /> La password &egrave; troppo corta o troppo lunga.').addClass('messageboxerror').fadeTo(900,1);
			});
		}
		else {
			$("#pmsgbox").fadeTo(200,0.1,function() { //start fading the messagebox
				//add message and change the class of the box and start fading
				$(this).html('<img src="images/Varies/icon_ajax_true.gif" />').addClass('messageboxok').fadeTo(900,1);
			});	
		}	

	}
	var checkEmail = function (email) {
		$("#emsgbox").removeClass().addClass('messagebox').text('').fadeIn("slow");
		var email = $("#email").val();
		$.post(
			"public/Users/validateForm.php",
			{check: 'email', emailAddress: email},
			function(data) {
				if(data=='no') { //if username not avaiable
				  	$("#emsgbox").fadeTo(200,0.1,function() { //start fading the messagebox
						//add message and change the class of the box and start fading
			  			$(this).html('<img src="images/Varies/icon_ajax_false.gif" /> L\'indirizzo inserito &egrave; gi&agrave; in uso.').addClass('messageboxerror').fadeTo(900,1);
					});		
          		}
		  		else if(data=='invalid') { //if username not avaiable
				  	$("#emsgbox").fadeTo(200,0.1,function() { //start fading the messagebox
						//add message and change the class of the box and start fading
			  			$(this).html('<img src="images/Varies/icon_ajax_false.gif" /> L\'indirizzo inserito non &egrave; valido.').addClass('messageboxerror').fadeTo(900,1);
					});		
          		}
		  		else {
		  			$("#emsgbox").fadeTo(200,0.1,function() {  //start fading the messagebox
			  			//add message and change the class of the box and start fading
			 		 	$(this).html('<img src="images/Varies/icon_ajax_true.gif" />').addClass('messageboxok').fadeTo(900,1);	
					});
		  		}
			}			
		)
		
		
	}
	$("#username").blur(checkUser);
	$("#passwordc").blur(checkPass);
	$("#email").blur(checkEmail);
});