$(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('User unavailable for registration!').addClass('messageboxerror').fadeTo(900,1);
						$('input[type=button]').attr('disabled', 'disabled');
					});
          			}
		  		else if(data=='yes') {
		  			$("#msgbox").fadeTo(200,0.1,function() {  //start fading the messagebox
			  			//add message and change the class of the box and start fading
			 		 	$(this).html('User available for registration!').addClass('messageboxok').fadeTo(900,1);
						$('input[type=button]').attr('disabled', false);	
					});
		  		}
				else if(data=='no_db_server_connection') {
					$("#msgbox").fadeTo(200,0.1,function() {  //start fading the messagebox
			  			//add message and change the class of the box and start fading
			 		 	$(this).html('The mySQL server is down!').addClass('messageboxerror').fadeTo(900,1);
						$('input[type=button]').attr('disabled', 'disabled');
					});
				}
				else if(data=='no_db_connection') {
					$("#msgbox").fadeTo(200,0.1,function() {  //start fading the messagebox
			  			//add message and change the class of the box and start fading
			 		 	$(this).html('The specified database does not exist!').addClass('messageboxerror').fadeTo(900,1);
						$('input[type=button]').attr('disabled', 'disabled');
					});
				}
			}			
		)
	}
	var checkPass = function (password) {
		$("#pmsgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
		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('Passwords do no match!').addClass('messageboxerror').fadeTo(900,1);
				$('input[type=button]').attr('disabled', 'disabled');
			});
		}
		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('Passwords match!').addClass('messageboxok').fadeTo(900,1);
				$('input[type=button]').attr('disabled', false);
			});	
		}		
	}
	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('Email address unavailable for registration!').addClass('messageboxerror').fadeTo(900,1);
						$('input[type=button]').attr('disabled', 'disabled');
					});		
          			}
		  		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 address provided is invalid!').addClass('messageboxerror').fadeTo(900,1);
					});		
          			}
		  		else if(data=='yes'){
		  			$("#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 address available for registration!').addClass('messageboxok').fadeTo(900,1);
						$('input[type=button]').attr('disabled', false);	
					});
		  		}
				else if(data=='no_db_server_connection') {
					$("#emsgbox").fadeTo(200,0.1,function() {  //start fading the messagebox
			  			//add message and change the class of the box and start fading
			 		 	$(this).html('The mySQL server is down!').addClass('messageboxerror').fadeTo(900,1);
						$('input[type=button]').attr('disabled', 'disabled');
					});
				}
				else if(data=='no_db_connection') {
					$("#emsgbox").fadeTo(200,0.1,function() {  //start fading the messagebox
			  			//add message and change the class of the box and start fading
			 		 	$(this).html('The specified database does not exist!').addClass('messageboxerror').fadeTo(900,1);
						$('input[type=button]').attr('disabled', 'disabled');
					});
				}	
			}			
		)
	}
	$("#username").blur(checkUser);
	$("#cpassword").blur(checkPass);
	$("#email").blur(checkEmail);
});

