$(document).ready(function()
{
   /* $("#submit").click(function(){
       $("#login_form").trigger('submit'); 
    });*/
	$("#login_form").submit(function()
	{
		//remove all the class add the msgbox classes and start fading
		$("#msgbox").removeClass().addClass('msgbox').text('Validating....').fadeIn(1000);
		
		if($('#username').val()=="")
		{
			$("#msgbox").fadeTo(200,0.1,function() //start fading the msgbox
			{ 
			  //add msg and change the class of the box and start fading
			  $(this).html('Please Enter Username(Email)').addClass('msgboxerror').fadeTo(900,1);
			});	
		}
		else if($('#password').val()=="")
		{
			$("#msgbox").fadeTo(200,0.1,function() //start fading the msgbox
			{ 
			  //add msg and change the class of the box and start fading
			  $(this).html('Please Enter password').addClass('msgboxerror').fadeTo(900,1);
			});
		}
		else
		{
		//check the username exists or not from ajax
			$.post("Includes/ajax_login.php",{ user_name:$('#username').val(),password:$('#password').val(),rand:Math.random() } ,function(data)
			{
			  if(data=='NO') //if correct login detail
			  {
				$("#msgbox").fadeTo(200,0.1,function() //start fading the msgbox
				{ 
				  //add msg and change the class of the box and start fading
				  $(this).html("Invalid username or password").addClass('msgboxerror').fadeTo(900,1);
				});	
			  }
			  else 
			  {
				  
				$("#msgbox").fadeTo(200,0.1,function()  //start fading the msgbox
				{ 
				  //add msg and change the class of the box and start fading
				  $(this).html('Logging in.....').addClass('msgboxok').fadeTo(900,1,
				  function()
				  { 
					 //redirect to secure page
					 document.location=data;
				  });
				  
				});
				/**/	
			  }
					
			});
		}
 		return false; //not to post the form physically
	});
	//now call the ajax also focus move from 
	/*$("#password").blur(function()
	{
		$("#login_form").trigger('submit');
	});*/
});
