/**
 * He, ho, commenting, here we go!
 */

var scriptlocation = "/includes/comments/comments.php";

function displayCommentCount(code) {

    // console.log('comment count voor', code);

    jQuery.ajax({
        type: "POST",
        url: scriptlocation,
        data: {'function':"commentCount", 'code': code },
        success: function(result){
            jQuery('#commentcount-'+code).html(result);
        }
    });

}


function displayComments(code) {

    jQuery.ajax({
        type: "POST",
        url: scriptlocation,
        data: {'function':"displayComments", 'code': code },
        success: function(result){
            jQuery("#comments").html(result);
        },
    });

}


function postComment() {


    var name = escape(jQuery("#commentform [name='name']").val());
    var email = escape(jQuery("#commentform [name='email']").val());
    var url = escape(jQuery("#commentform [name='url']").val());
    var comment = escape(jQuery("#commentform [name='comment']").val());
    var code = escape(jQuery("#commentform [name='code']").val());

    if (name.length<2) {
        alert("Please enter your name or alias!");
        return;
    }

    if (comment.length<4) {
        alert("Please enter a comment");
        return;
    }

    jQuery.ajax({
        type: "POST",
        url: scriptlocation,
        dataType: "json",
        data: {'function':"postComment", 'code': code, 'name':name, 'email':email, 'url':url, 'comment':comment },
        success: function(result){
            if (result[0]==0) {
                // Blacklist error
                alert(result[1]);
            }

            if (result[0]==1) {
                // Reactie is geplaatst..
                jQuery("#newcomment").hide().html(result[1]).slideDown('slow');
                jQuery("#commentform [name='comment']").val('');
            }

        },
    });

    return false;
}


function flag(uid) {

    jQuery.ajax({
        type: "POST",
        url: scriptlocation,
        data: {'function':"flagComment", 'comment_uid': escape(uid) },
        success: function(result){
            alert('De betreffende reactie is onder de aandacht van de redactie gebracht. Bedankt!');
        }
    });


}