/*
 ########################################################################
#                                                                        #
#   Ajax Register v1.0                                                   #
#              Copyright 2009 www.zer-o.eu                               #
#                             www.zer-o.eu / www.zer-o.at                #
#                                                                        #
 ########################################################################
*/

$(document).ready(function() {
	var checkUser = function (username) {
		//remove all the class add the messagebox classes and start fading
		$("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
		var username = $('#username').val();
		$.post(
			"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/icons/error.png" width="14" height="14">').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/icons/success.png" width="14" height="14">').addClass('messageboxok').fadeTo(900,1);	
					});
		  		}
			}			
		)
	}
		var checkNick = function (nickname) {
		//remove all the class add the messagebox classes and start fading
		$("#nmsgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
		var nickname = $('#nickname').val();
		$.post(
			"validateForm.php",
			{check: 'nickname', nick: nickname},
			function(data) {
				if(data=='no') { //if username not avaiable
				  	$("#nmsgbox").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/icons/error.png" width="14" height="14">').addClass('messageboxerror').fadeTo(900,1);
					});		
          		}
		  		else {
		  			$("#nmsgbox").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/icons/success.png" width="14" height="14">').addClass('messageboxok').fadeTo(900,1);	
					});
		  		}
			}			
		)
	}
	var checkPass = function (password) {
		$("#pmsgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("fast");
		var password = $('#password').val();
		var cpassword = $('#cpassword').val();
		if(password != cpassword) {
			$("#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/icons/error.png" width="14" height="14">').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/icons/success.png" width="14" height="14">').addClass('messageboxok').fadeTo(900,1);
			});	
		}		
	}
	var checkEmail = function (email) {
		$("#emsgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
		var email = $("#email").val();
		$.post(
			"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/icons/error.png" width="14" height="14">').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('Email Adresse ungültig!').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/icons/success.png" width="14" height="14">').addClass('messageboxok').fadeTo(900,1);	
					});
		  		}
			}			
		)
		
		
	}
	$("#username").blur(checkUser);
	$("#nickname").blur(checkNick);
	$("#cpassword").blur(checkPass);
	$("#email").blur(checkEmail);
});
