//  Developed by Roshan Bhattarai 
//  Visit http://roshanbh.com.np for this script and more.
//  This notice MUST stay intact for legal use

jQuery(document).ready(function()
{
	jQuery("#loginform").submit(function()
	{
		//remove all the class add the messagebox classes and start fading
		jQuery("#msgbox").removeClass().addClass('messagebox').text('A Validar....').fadeIn(1000);
		//check the username exists or not from ajax
		jQuery.post("ajaxlogin.php",{ user_name:jQuery('#log').val(),password:jQuery('#pwd').val(),cookie:jQuery('#setcookie').is(':checked'),rand:Math.random() } ,function(data)
        {
		  if(data=='yes') //if correct login detail
		  {
		  	jQuery("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  jQuery(this).html('A Aceder...').addClass('messageboxok').fadeTo(900,1,
              function()
			  { 
			  	 //redirect to secure page
				 document.location='index.php?mm=1';
			  });
			  
			});
		  }
		  else 
		  {
		  	jQuery("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  jQuery(this).html('Dados de acesso incorrectos...').addClass('messageboxerror').fadeTo(900,1);
			});		
          }
				
        });
 		return false; //not to post the  form physically
	});
	//now call the ajax also focus move from 
	jQuery("#pwd").blur(function()
	{
		jQuery("#loginform").trigger('submit');
	});
});