// Affichage form comentaire + liste commentaires
$(function(){
    $(".form_comment").hide();
    $("a.add_comment").click(function(){
        $(".form_comment").toggle();
        return false;
    });
});

// liste des commentaires
function list_commentaires(type,id_item){
    $.ajax({
        type: 'POST',
        url: "ajax/read_commentaire.php",
        data: "&type="+type+"&id_item="+id_item+"",
        success: function(datas) {
            if(datas != ""){
                $(".list_commentaires").html(datas);
            }
        }
    });    
}

// nb de commentaires
function afficher_nb_commentaires(type,id_item){
    $.ajax({
        type: 'POST',
        url: "ajax/nb_commentaires.php",
        data: "&type="+type+"&id_item="+id_item+"",
        success: function(data) {
            if(data != ""){
                $("a.read_comment").html(data);
            }
        }
    });
}

// Enregistrement commentaires
$(function(){
    
    $(".msg").hide();
    
    $("#add_commentaires").submit(function(){
        
        if($("#pseudo").val() == ""){
            $(".msg").show().html("Vous devez associer à votre commentaire un pseudo, un nom ou un prénom");
        }else if($("#email").val() == ""){
            $(".msg").show().html("Vous devez insérer une adresse email valide");
        }else if($("#comment").val() == ""){
            $(".msg").show().html("Vous devez insérer un commentaire");
        }else{
            $.ajax({
                type: 'POST',
                url: "ajax/add_commentaire.php",
                data: "&pseudo="+$("#pseudo").val()+"&email="+$("#email").val()+"&comment="+$("#comment").val()+"&type="+$("#type").val()+"&id_item="+$("#id_item").val()+"",
                success: function(data) {
                    if(data == 1){
                        $(".msg").show().html("Votre commentaire a été correctement enregistré.");
                        $("#add_commentaires").hide();
                        $(".list_commentaires").show();
                        list_commentaires($("#type").val(), $("#id_item").val());
                    }else{
                        $(".msg").show().html("Votre commentaire n'a pas été correctement enregistré suite à un problème technique.");
                        $("#add_commentaires").hide();
                    }
                }
            });       
        }
        
        return false;
     
    });
    
});
