      //  Developed by Roshan Bhattarai
      //  Visit http://roshanbh.com.np for this script and more.
      //  This notice MUST stay intact for legal use
      ///
      var txtLogin= 'OK';
      var txtLogout ='Sair';
      var MsgErroLogin ='ATENÇÃO! <br/> O nome de utilizador ou a palavra-passe que introduziu está incorrecto.';
	  var MsgErroLogin_db ='ATENÇÃO! <br/> Ocorreu um erro no processo de autenticação. Se o problema persistir contacte o administrador de sistema.';
      var MsgSucessoLogin='Login realizado com sucesso!';
      var MsgSucessoLogout='Logout realizado com sucesso!';
      var MsgLogginEspera='Espere, por favor... ';


    function TrataLogin(login , txtLogin,data, btsub) {

	
    		document.getElementById('divlogout').innerHTML  =data;
    		btsub.value=txtLogin;
    		if (login == 0) {
    			 document.getElementById('username').value='nome do utilizador...';
    			 document.getElementById('password').value='';
    			 showdiv('divlogin');
				 expandloginarea('0');
    			 hidediv('divlogout');
				
    		}
    		else {
				
    			 showdiv('divlogout');
				 expandloginarea('1');
    			 hidediv('divlogin');
				 
				 
    		}
			
    	}

		function writediv(divname,texto) {

    		if (document.getElementById) { // DOM3 = IE5, NS6
    			
				document.getElementById(divname).innerHMTL=texto;
    		}
    		else {
    			if (document.layers) { // Netscape 4
    			
				document.divname.innerHMTL=texto;
    			}
    			else { // IE 4

				document.all.divname.innerHMTL=texto;
    			}
    		}
			
    	}
    	function hidediv(divname,texto) {

    		if (document.getElementById) { // DOM3 = IE5, NS6
    			document.getElementById(divname).style.display = 'none';
				
    		}
    		else {
    			if (document.layers) { // Netscape 4
    			document.divname.display = 'none';
				
    			}
    			else { // IE 4
    			document.all.divname.style.display = 'none';
				
    			}
    		}
			
    	}

    	function showdiv(divname,texto) {
    		if (document.getElementById) { // DOM3 = IE5, NS6
    		document.getElementById(divname).style.display = 'block';
			
    		}
    		else {
    			if (document.layers) { // Netscape 4
    			document.divname.display = 'block';
				
    			}
    			else { // IE 4
    			document.all.divname.style.display = 'block';

    			}
    		}
    	}
		function isdivhide(divname) {
    		
    		return document.getElementById(divname).style.display == 'block';

    	}
    	
		function expandloginarea(expand){
			
			var loginarea = document.getElementById('loginarea');
			var cssloginarea = loginarea.className;
			
			var btnLogin = document.getElementById('Submit');
			var cssbtnLogin = btnLogin.className;
			
			if (expand == '1')
			{
			  cssloginarea = cssloginarea + "exp";
			  cssbtnLogin = cssbtnLogin + "exp";
			}
			else
			{
			  cssloginarea = cssloginarea.replace(/exp/g,'');
			  cssbtnLogin = cssbtnLogin.replace(/exp/g,'');
			}
		   
			loginarea.className = cssloginarea;
			btnLogin.className = cssbtnLogin;
		}
		
		function isshowdiv(divname) {
    		if (document.getElementById) { // DOM3 = IE5, NS6

    			return (document.getElementById(divname).style.display == 'block' || document.getElementById(divname).style.display == '');
    		}
    		else {
    			if (document.layers) { // Netscape 4
    				return (document.divname.display == 'block' || document.divname.display == '');
    			}
    			else { // IE 4
    				return (document.all.divname.style.display == 'block' || document.all.divname.style.display == '');
    			}
    		}
    	}
    	
		
    jQuery(document).ready(function()
    {

	
	jQuery("#login_form").submit(function()
	{

		//remove all the class add the messagebox classes and start fading
		jQuery("#msgbox").removeClass().addClass('messagebox').text(MsgLogginEspera).fadeIn(1000);
		//check the username exists or not from ajax
		var btsub =document.getElementById('Submit');
		

		jQuery.post("ajax_login.php",{ username:jQuery('#username').val(),password:jQuery('#password').val(),rand:Math.random() } ,function(data)
		{
	if(data!='' && data.substring(0,1)=="1") //if correct login detail
			{
				
    			jQuery("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
    			{

    				
					
    				 if(btsub.value == txtLogout)
    				 {
							
						//alert('efectua logout...');	
    					 jQuery(this).html(MsgLogginEspera).addClass('messagebox').fadeTo(900,1,
    					  function()
    					  {
							
    						 TrataLogin(0, txtLogin,'', btsub) ;
    					  });
    					//add message and change the class of the box and start fading
    					jQuery(this).html( MsgErroLogin).addClass('messageboxerror').fadeTo(900,1);
    				 }
    				 else
    				 {
						
						if(data.substr(0,1)=='1')
						{
							 jQuery(this).html(MsgLogginEspera).addClass('messagebox').fadeTo(900,1,
							  function()
							  {
	
								 TrataLogin(1, txtLogout,data.substr(1,data.length-1), btsub) ;
							  });
							//add message and change the class of the box and start fading
							jQuery(this).html(MsgSucessoLogin).addClass('messageboxok').fadeTo(900,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
							 TrataLogin(0, txtLogin,'', btsub) ;
							 jQuery(this).html(MsgErroLogin).addClass('messageboxerror').fadeTo(900,1);
							   //$(this).html(data).addClass('messageboxerror').fadeTo(900,1);
							  
							});


						}

    				 }


    			});
    			//$(this).html('').$("#msgbox").removeClass().fadeTo(900,1);


			}
			else if(data!='' && data.substring(0,2)=='-1' && isshowdiv('divlogin'))
			{

				jQuery.post("ajax_clearsess.php",{ username:jQuery('#username').val(),password:jQuery('#password').val(),rand:Math.random() } ,function(data)
				{
				});
					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( MsgErroLogin ).addClass('messageboxerror').fadeTo(900,1);
				});
				 TrataLogin(0, txtLogin,'', btsub) ;
			}
			else if(data!='' && (data.substring(0,1)==0) && isshowdiv('divlogout'))
			{
			
				jQuery("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
				{
				
					jQuery.post("ajax_clearsess.php",{} ,function(data){});
				  //add message and change the class of the box and start fading
				  TrataLogin(0, txtLogin,'', btsub) ;
				  jQuery(this).html( MsgSucessoLogout ).addClass('messageboxok').fadeTo(900,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.post("ajax_clearsess.php",{ username:jQuery('#username').val(),password:jQuery('#password').val(),rand:Math.random() } ,function(data)
						{
						});

					  jQuery(this).html( MsgErroLogin_db ).addClass('messageboxerror').fadeTo(900,1);	
					});
									
			
			
			}
			
			/*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(data).addClass('messageboxerror').fadeTo(900,1);
							  
							  
							});*/

		});
		return false; //not to post the  form physical/ly

	});

});


 
