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

$(document).ready(function()
{
	$("#login_form").submit(function()
	{
		//remove all the class add the messagebox classes and start fading
		$("#msgbox").removeClass().addClass('messagebox').text('Verificando....').fadeIn(500);
		//check the username exists or not from ajax
		
		$.post("controladores/controlador_acceso.php",{ user_name:$('#username').val(),password:$('#password').val(),rand:Math.random() } ,function(data)
        {
			
			var noesp = data; // pasar la respuesta auna variable para quitarle espacios
			noesp = noesp.replace(/^\s*|\s*$/g,""); //quito espacios
		  if(noesp=='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
			  $(this).html('Ingresando.....').addClass('messageboxok').fadeTo(900,1,
              function()
			  { 
			  	 //redirect to secure page - si existe mande a menu
				 document.location='inicio.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('Datos Incorrectos.').addClass('messageboxerror').fadeTo(900,1);document.forms[0].elements[1].focus();
			});		
          }
				
        });
 		return false; //not to post the  form physically
	});
	//now call the ajax also focus move from 
	$("#Submit").click(function()
	{
		$("#login_form").trigger('submit');
	});
});