$(document).ready(function() {
	var checkUser = function (username) {
		//remove all the class add the messagebox classes and start fading
		$("#msgbox").removeClass().addClass('messagebox').text('Loading...').fadeIn("slow");
		var username = $('#username').val();
		$.post(
			"validateForm.php",
			{check: 'username', user: username},
			function(data) {
				if(data=='no') { //if username sudah dipakai
				  	$("#msgbox").fadeTo(200,0.1,function() { //start fading the messagebox
						//add message and change the class of the box and start fading
			  			$(this).html('Username telah terpakai!').addClass('messageboxerror').fadeTo(900,1);
					});		
          		}
				
		  		
				
				if(username == "") {
			$("#msgbox").fadeTo(200,0.1,function() { //start fading the messagebox
				//add message and change the class of the box and start fading
				$(this).html('Username belum diisi!').addClass('messageboxerror').fadeTo(900,1).innerHTML = '<img src="images/loading.gif">';
			});
		}
		
		else if(data=='yes') { //if belum ada
				  	$("#msgbox").fadeTo(200,0.1,function() { //start fading the messagebox
						//add message and change the class of the box and start fading
			  			$(this).html('Username tersedia!').addClass('messageboxok').fadeTo(900,1);
					});		
          		}
		
		
			}			
		)
	}
	
	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('Password tidak cocok!').addClass('messageboxerror').fadeTo(900,1);
			});
		}
		
		else 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('Password belum diisi!').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('Password Cocok!').addClass('messageboxok').fadeTo(900,1);
			});	
		}		
		
		
	}
	
	
	var checkPassKosong = function (cekpassword) {
		$("#kmsgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
		var kosong = $('#password').val();
	if(kosong == "") {
			$("#kmsgbox").fadeTo(200,0.1,function() { //start fading the messagebox
				//add message and change the class of the box and start fading
				$(this).html('Password belum diisi!').addClass('messageboxerror').fadeTo(900,1);
			});
		}
		
		else {
			$("#kmsgbox").fadeTo(200,0.1,function() { //start fading the messagebox
				//add message and change the class of the box and start fading
				$(this).html('Thanks..').addClass('messageboxok').fadeTo(900,1);
			});	
		}		
		
	}
		
		
	var checkNama = function (nama) {
		$("#nmsgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
		var nama = $('#nama_lengkap').val();
	
		
		if(nama == "") {
			$("#nmsgbox").fadeTo(200,0.1,function() { //start fading the messagebox
				//add message and change the class of the box and start fading
				$(this).html('Nama lengkap belum diisi!').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('Thanks..').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('Email address pernah digunakan!').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 address tidak valid!').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('Email address tersedia!').addClass('messageboxok').fadeTo(900,1);	
					});
		  		}
			}			
		)
		
		
	}
	$("#username").blur(checkUser);
	$("#cpassword").blur(checkPass);
	$("#password").blur(checkPassKosong);
	$("#nama_lengkap").blur(checkNama);
	$("#email").blur(checkEmail);
});