﻿function showSubmit() {
    var btn = document.getElementById('submitComment');
    btn.style.display = 'block';
}

function submitArticlePostCommentForm() {
    var isValid = true;
    var frm = document.getElementById('postCommentsForm');

    var userId = frm.userId.value.trim();
    var articleId = frm.articleId.value.trim();
    var comment = frm.comment.value.trim();

    if (frm.comment.value.trim().length == 0) {
        isValid = false;
    }

    if (isValid == true) {
        insertArticleComment(userId, articleId, comment);
    }
}

function insertArticleComment(userId, articleId, comment) {
    $.ajax({
        type: "GET",
        url: "/Api/InsertArticleComment.ashx",
        data: "UserId=" + userId + "&ArticleId=" + articleId + "&Comment=" + escape(comment),
        //        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(response) {
            validateArticlePostComment(response);
        }
    });
}

function validateArticlePostComment(isValid) {
    var frm = document.getElementById('postCommentsForm');

    var articleCommentValidError = document.getElementById("articleCommentValidError");

    if (isValid == true) {
        articleCommentValidError.style.display = "none";
        // reload the current page
        window.location.href = window.location.href;
        //        frm.Submit();
    }
    else {
        articleCommentValidError.style.display = "block";
        isValid = false;
    }
}
