function liste() {
  var param = "" ;
  $.ajax({
    type:"POST",
    url:$("#cf_url").val()+"/acte/liste",
    data:param,
    success:function(msg){
      $("#div_acte").html(msg) ;
      var palettes = [
          ["#000000", "#434343", "#666666", "#999999", "#b7b7b7", "#cccccc", "#d9d9d9", "#efefef", "#f3f3f3", "#ffffff"],
          ["#980000", "#ff0000", "#ff9900", "#ffff00", "#00ff00", "#00ffff", "#4a86e8", "#0000ff", "#9900ff", "#ff00ff"],
          ["#e6b8af", "#f4cccc", "#fce5cd", "#fff2cc", "#d9ead3", "#d9ead3", "#c9daf8", "#cfe2f3", "#d9d2e9", "#ead1dc"],
          ["#dd7e6b", "#ea9999", "#f9cb9c", "#ffe599", "#b6d7a8", "#a2c4c9", "#a4c2f4", "#9fc5e8", "#b4a7d6", "#d5a6bd"],
          ["#cc4125", "#e06666", "#f6b26b", "#ffd966", "#93c47d", "#76a5af", "#6d9eeb", "#6fa8dc", "#8e7cc3", "#c27ba0"],
          ["#a61c00", "#cc0000", "#e69138", "#f1c232", "#6aa84f", "#45818e", "#3c78d8", "#3d85c6", "#674ea7", "#a64d79"],
          ["#85200c", "#990000", "#b45f06", "#bf9000", "#38761d", "#134f5c", "#1155cc", "#0b5394", "#351c75", "#741b47"],
          ["#5b0f00", "#660000", "#783f04", "#7f6000", "#274e13", "#0c343d", "#1c4587", "#073763", "#20124d", "#4c1130"]
        ];
      $(".color_picker").spectrum({
        flat: false,
  		  showPalette: true,
  		  preferredFormat: "hex",
  		  // palette:palettes,
        showInput: true,
        });
    }
  }) ;
}

function maj(obj) {
  var champ = obj.name ;
  var id = obj.getAttribute("rel") ;
  var type = obj.type ;
  if(type=="checkbox"){
    var val = 0 ;
    if($("#"+champ+"_"+id).is(":checked")) val = 1 ;
  }
  else{
    var val = $("#"+champ+"_"+id).val() ;
  }
  var param = "id="+id+"&champ="+champ+"&val="+encodeURIComponent(val) ;
  $.ajax({
    type:"POST",
    url:$("#cf_url").val()+"/acte/maj",
    data:param,
    success:function(msg){
      if(msg!="") alert(msg) ;
    }
  }) ;
}

function supprime(id) {
  if(confirm('Etes vous sûr ?')){
    var param = "id="+id ;
    $.ajax({
      type:"POST",
      url:$("#cf_url").val()+"/acte/supprime",
      data:param,
      success:function(msg){
        liste() ;
      }
    }) ;
  }
}

function form(id) {
  if(id==0){
    var param = "" ;
    // alert($("#cf_url").val()+"/acte/ajouter") ;
    $.ajax({
      type:"POST",
      url:$("#cf_url").val()+"/acte/ajouter",
      data:param,
      success:function(msg){
        liste() ;
      }
    }) ;
  }
  else{
    var param = "id="+id ;
    // alert($("#cf_url").val()+"/acte/form?"+param) ;
    $.ajax({
      type:"POST",
      url:$("#cf_url").val()+"/acte/form",
      data:param,
      success:function(msg){
        $("#div_acte").html(msg) ;
      }
    }) ;
  }

}

function traitForm() {
  if($("#libel").val()==""){
    alert("Nom obligatoire...") ;
    $("#libel").focus() ;
  }
  else if($("#abrev").val()==""){
    alert("Abreviation obligatoire...") ;
    $("#abrev").focus() ;
  }
  else if($("#duree").val()==""){
    alert("Durée obligatoire...") ;
    $("#duree").focus() ;
  }
  else{
    var param = "id="+$("#id").val() ;
    param+="&libel="+encodeURIComponent($("#libel").val()) ;
    param+="&abrev="+encodeURIComponent($("#abrev").val()) ;
    param+="&duree="+encodeURIComponent($("#duree").val()) ;
    // alert($("#cf_url").val()+"/acte/trait_form?"+param) ;
    $.ajax({
      type:"POST",
      url:$("#cf_url").val()+"/acte/trait_form",
      data:param,
      success:function(msg){
        // $("#div_acte").html(msg) ;
        alert("MODIFICATIONS ENREGISTREES") ;
        form(msg) ;
      }
    }) ;
  }
}

$(document).ready(function(){
  liste() ;
}) ;
