// JavaScript Document
//  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()
{ $("input#username").bind("click", function(e) {
	$("#msgbox").removeClass().html('');
});
	$("#login_form").submit(function(){
		//remove all the class add the messagebox classes and start fading	
		if(($('input#username').val())==""){
			$("#msgbox").removeClass().addClass('messageboxfalta');
			$("#msgbox").html('Escriba el usuario').fadeIn('slow');
			return false;
		}
		$("#msgbox").removeClass().addClass('messageboxtest').text('Comprobando....').fadeIn('slow');
		//check the username exists or not from ajax
		$.post("login.php",{ username:$('#username').val(),password:$('#password').val(),rand:Math.random() } ,function(data)
        {
		  if(data=='yes'){ //if correct login detail
		  	$("#msgbox").fadeTo(1200,0.1,function()  //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Entrando.....').addClass('messageboxok').fadeTo(900,1,
              function()
			  { $('#wrapper').fadeOut('medium');
			  	 //redirect to secure page
				 document.location='main.php';
			  });  
			});
		  }
		  else {//alert(data);
		  	$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Acceso no permitido...').removeClass().addClass('messageboxerror').fadeTo(900,1);
			  document.forms['login_form'].reset();
			});		
          }			
        });
 		return false; //not to post the  form physically
	});
	//envio el formulario tambien al perder el foto la contraseņa
	$("#password").blur(function()
	{
		//$("#login_form").trigger('submit');
	});
});
