$(document).ready(function(){
      $('.mod_billet')
	  	.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(11,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 titre = $('#titre').val();
            var texte = $('#texte').val();
            var image = $('#image').val();
			var date = formate_date(new Date());
			var table = $('#table').val();
			$('#input_test').val(date);				
			$('#champ_date').val(date);
            if(error == false){
                $('#mod_billet').attr({'disabled' : 'true', 'value' : 'Sending...' });//disable the submit button to avoid spamming and change the button text to Sending...
                data = $("#billet_mod_"+num_billet).serialize(); /* On formate les données du POST */
                $.post("mod_form.php", data, function(result){
                    //and after the ajax request ends we check the text returned
                    if(result == 'sent'){
            			var texte = $('#texte_mod_'+num_billet).val(); // On récupère le nouveau texte
						//$("#texte_billet_"+num_billet).fadeOut();	
						$("#texte_billet_"+num_billet).html("<p><img class='insert_top' src='inserts/".$image."' align='left' hspace='10' vspace='5' width='200'>"+texte+"</p>"); // On met à jour le bloc billet avec le nouveau texte.
						//$("#texte_billet_"+num_billet).fadeIn();
                        $('#cf_submit_p').remove();//if the mail is sent remove the submit paragraph
                        $('#rec_success').fadeIn(500);//and show the mail success div with fadeIn
						$("#billet_mod"+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
                        $('#mod_billet').removeAttr('disabled').attr('value', 'Enregistrer le récit');
						$('#billet_mod_holder').slideUp('slow');
                    }else{
                        //show the mail failed div
                        $('#rec_fail').fadeIn(500);
                        //reenable the submit button by removing attribute disabled and change the text back to Send The Message
                        $('#mod_billet').removeAttr('disabled').attr('value', 'Send The Message');
                    }
                });
            }
        });    
	});
		
	$(".bouton_mod_billet")
		.livequery(function() {
			$(this).click(function(){
			$(".billet_mod_holder").slideToggle("slow");
		});
	});
});

