function effacerLogin(prenom, nom){
	
	$("#loginform").children().hide();
	$("#bonjour").html("Bonjour " + prenom + " " + nom).show();
	$("<button/>").attr("id", "boutonlogout").button({label: "déconnexion"}).click(function(){            	
    	$.getJSON("ajax/logout.php",{} ,function(data){
    		if(data.reponse=='ok'){
    			$("#boutonlogout").button( "destroy" );
    			$("#boutonlogout").remove();
    			//$("#boutonlogout").detach();
       			$("#loginform").children().show();
    			$("#bonjour").hide();
    			$("#submit").button( "option", {disabled: false, label: "Login"} );

    		}
    			
    	});
    }).appendTo("#loginform");



}



function initialiserLogin() {
			$("#submit").button();
			$("#login_form").submit(function() {
        
        		$("#submit").button( "option", {disabled: true, label: "Vérification"} );
        
        		//check the username exists or not from ajax
    		    $.getJSON("ajax/identification.php",{ mail:$('#username').val(),password:$('#password').val(),rand:Math.random() } ,function(data)
   			    {
        	
         		 if(data.reponse=='ok') //if correct login detail
        		 {
        		 	effacerLogin(data.prenom, data.nom);
   
                

          }
          else
          {
                alert(data.reponse);
                //$("<div/>").val(data.reponse).dialog().appendTo("body");
                /*
                $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
                {
                  //add message and change the class of the box and start fading
                  $(this).html('Adresse email ou mot de passe inconnu.').addClass('messageboxerror').fadeTo(900,1);
                });
                */
                
                
                
          	 $("#submit").button( "option", {disabled: false, label: "Login"} );      
          }
       });
       return false;//not to post the  form physically
});
						
		

					
}
