function MAJ_bloc_commentaires(date,nom,message,rubrique) {
	$.ajax({ // mise à jour du bloc avec le nouveau commentaire
		type: "POST",
		url: "dernier_commentaire.php",
		success: function(resultat){
			num_cmt = resultat;
			$('#commentaire_holder_'+num_billet).append("<div id='commentaire_"+num_cmt+"' class='commentaire'><div id='en_tete_comment'><div id='date_comment'>"+date+"</div><div id='auteur_comment'><b>"+nom+"</b></div></div><div id='texte_comment'>"+message+"<div class='admin'><p class='bouton_suppr_commentaire' id='bouton_suppr_commentaire_"+num_cmt+"'>suppr</p></div>									</div></div><br />");
			$.ajax({ // On teste si le profil est admin	
					type: "POST",
					url: "quel_profil.php",
					success: function(resultat){
						if (resultat == "admin") {
							$(".admin").fadeIn(1500); // On fait apparaître la nouvelle partie admin si le profil est admin
						}
					}
			 	});			
			$("#commentaire_"+num_cmt).fadeIn(1500); // On fait apparaître le commentaire
		}
	});
	$.ajax({ //mise à jour du bouton avec le nombre de commentaires
		type: "POST",
		url: "nombre_commentaires_dans_billet.php",
		data: "billet="+num_billet+"&rubrique="+rubrique, 
		success: function(resultat){
			nb_cmt=resultat;
			if (nb_cmt > 1) { // Ici on met à jour le bouton Lire Commentaire avec le nouveau nombre de commentaires
				$("#bouton_affiche_commentaires_"+num_billet).replaceWith("<div id='bouton_affiche_commentaires_"+num_billet+"' class='bouton_affiche_commentaires'>Lire les "+nb_cmt+" commentaires</div>");
			} else {
			if (nb_cmt = 0) { 
				$("#bouton_affiche_commentaires_"+num_billet).replaceWith("<div id='bouton_affiche_commentaires_"+num_billet+"' class='bouton_affiche_commentaires'>Ecrire un commentaire</div>");
			}
			if (nb_cmt = 1) {
				$("#bouton_affiche_commentaires_"+num_billet).replaceWith("<div id='bouton_affiche_commentaires_"+num_billet+"' class='bouton_affiche_commentaires'>Lire le commentaire</div>");
			}}
		}
 	});
}

$(document).ready(function(){
	  $('.send_message')
	  	.livequery(function() {
			$(this).click(function(e){ 
		
			// On récupère le numéro du billet en cours
			var leBillet = $(this).attr('id'); 
			num_billet = leBillet.substring(13,leBillet.length);
            //stop the form from being submitted
            e.preventDefault();
            
            /* declare the variables, var error is the variable that we use on the end
            to determine if there was an error or not */
            var error = false;
			var rubrique = $('#rubrique').html();
            var nom = $('#nom_'+num_billet).val();
            var email = $('#email_'+num_billet).val();
            var message = $('#message_'+num_billet).val();
			var date = formate_date(new Date());
			var captcha = $('#captcha_'+num_billet).val();
			if (captcha == 42) {test=true;} else {test=false;} /* Captcha marche pas pour l'instant
			$.ajax({ // On teste le captcha
				type: "POST",
				url: "verif_code.php",
				data: "essai="+captcha,
				success: function(resultat){ alert(resultat);
					if(resultat == true){ 
        	        	$('#captcha_fail').fadeIn(500);
			        }else{
	        		var error = true;
	                $('#captcha_fail').fadeOut(500);
					}
				}
		 	});*/
			if(test == true){ 
        		$('#captcha_fail').fadeOut(500);
			    }else{
	        	var error = true;
	            $('#captcha_fail').fadeIn(500);
				}
            //now when the validation is done we check if the error variable is false (no errors)
            if(error == false){
                //disable the submit button to avoid spamming
                //and change the button text to Sending...
                $('#send_message_'+num_billet).attr({'disabled' : 'false', 'value' : 'Sending...' });
                data = $("#contact_form_"+num_billet).serialize(); /* On formate les données du POST */	
                $.post("rec_comment.php", data, function(result){ 
                    //and after the ajax request ends we check the text returned
                    if(result == 'sent'){	/* On met à jour le bloc des commentaires (on ajoute le nouveau) */
						MAJ_bloc_commentaires(date,nom,message,rubrique);
                        //if the mail is sent remove the submit paragraph
                        //$('#cf_submit_p').remove();
                        //and show the mail success div with fadeIn
                        //$('#mail_success').fadeIn(500);
						$("#contact_form_"+num_billet).each(function(){ // On vide le formulaire
							 this.reset(); 
						 	});
                        //reenable the submit button by removing attribute disabled and change the text back to Send The Message
                        $('#send_message_'+num_billet).removeAttr('disabled').attr('value', 'Enregistrer le commentaire');
                    }else{
                        //show the mail failed div
                        $('#mail_fail').fadeIn(500);
                        //reenable the submit button by removing attribute disabled and change the text back to Send The Message
                        $('#send_message_'+num_billet).removeAttr('disabled').attr('value', 'Enregistrer le commentaire');
                    }
                });
            }
        });    
   	});
	  
	  
	  
});		
