$(document).ready(function()
{
	$("#login_form").submit(function()
	{
		//remove all the class add the messagebox classes and start fading
		$("#msgbox").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
		//check the username exists or not from ajax
		$.post("data.php?action=login",{nim:$('#nim').val(),password:$('#password').val(),rand:Math.random() } ,function(data)
		{
		  if(data=='yes') //if correct login detail
		  {
			$("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $("#login").html('<center>Success. Please wait while redirecting.. <br>Or just clik <b><a href="index.php">here</a></b> if you dont want to wait.<br><img src="images/loading.gif"></center>').addClass('login').fadeTo(1500,1,
			  function()
			  { 
				 //redirect to secure page
				 document.location='index.php';
			  });
			  
			});
		  }
		  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(data+'Wrong combination between nim and password').addClass('messageboxerror').fadeTo(900,1);
			});		
		  }
				
		});
		return false; //not to post the  form physically
	});
	//now call the ajax also focus move from 
	$("#password").blur(function()
	{
		$("#login_form").trigger('submit');
	});
});