$(document).ready(function()
{
	$("#login_form").submit(function()
	{
		// Check if the username and password exist in the database
		$.post("authentication/login.php",{ username:$('#username').val(),password:$('#password').val(),rand:Math.random() } ,function(data)
        {
		
		  if(data=='yes') // If login.php returns 'yes' then the username and password have been found in a database row
		  {
		  	//$("div#panel").slideUp("slow");	
			
			$('#noaccount').slideUp("slow");					
			//$('#login_form').slideUp("slow");	
			$('#leftinfo').slideUp("slow");
			//$('#login_form').before('');
			
			
			$('#login_form').slideUp("slow", function () {
			
			if($('#username').val() == "Damian")
				$("#msg").html('<h2>Welcome back, ' + $('#username').val() + '</h2><p>You can access the Americatessen Control Panel <a href="controlpanel/">here</a>.</p>');
			else			
				$("#msg").html('<h2>Welcome back, ' + $('#username').val() + '</h2><p>You can now view the prices of our products in the Product Range section.</p><p>Click <a href="authentication/logout.php">here</a> to log out.</p>');
			});
			
			$("#guest").text('Welcome back!');
			$("#open").text('Logged in');

			
			//$("div#panel").slideDown("slow");	
		  }
		  else 
		  {
		  	$("#left").fadeTo(200,0.1,function() //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Your login detail sucks...').addClass('messageboxerror').fadeTo(900,1);
			});		
          }
				
        });
 		return false; //not to post the  form physically
	});
	//now call the ajax also focus move from 
	$("#left").blur(function()
	{
		$("#login_form").trigger('submit');
	});
});