$(document).ready(function()
{
	$("#top_login").submit(function()
	{
		//remove all the class add the messagebox classes and start fading
		$("#loginbox").removeClass().addClass('messagebox').text('Controllo dati...').fadeIn(1000);
		//check the username exists or not from ajax
		$.post("/index.php/ajax/login/",{ email:$('#email').val(),password:$('#password').val(),posizione:$('#posizione').val(),remember:$('#remember').val(),rand:Math.random() } ,function(data)
        {
		  if(data=='no') //if correct login detail
		  {
			  	$("#loginbox").fadeTo(200,0.1,function() //start fading the messagebox
				{ 
				  //add message and change the class of the box and start fading
				 $(this).html("E-mail o password errati").addClass('messageboxerror').fadeTo(900,1);
				});	
		  } 
		  else 
		  {
	  	$("#loginbox").fadeTo(200,0.1,function()  //start fading the messagebox
				{ 
				  //add message and change the class of the box and start fading
				  $(this).html('Login in corso...').addClass('messageboxok').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');
	});
});
