﻿var BlogPost = Class.create({
    initialize: function()
    {
    }
});

BlogPost.getArticleComments = function(articleId)
{
    new Ajax.Request('Utilities/cmMethods.aspx', {
        method: 'post',
        parameters: { Command: 'GetArticleComments', ArticleID: articleId },
        onSuccess: function(xhrResponse)
        {
            var d = new Element('div').update(xhrResponse.responseText);
            $('dArticleComments_' + articleId).innerHTML = $(d).select('div[id="dArticleComments"]')[0].innerHTML;
        },
        onFailure: function(xhrResponse)
        {
            $('dArticleComments_' + articleId).innerHTML = xhrResponse.responseText;
        }
    });
}

BlogPost.showAddCommentContainer = function(articleId)
{
    $('dAddNewCommentContainer_' + articleId).style.display = 'block';
    $('dAddNewCommentContainer_' + articleId).style.visibility = 'visible';
    $('dArticleCommentButtons_' + articleId).style.display = 'block';
    $('dArticleCommentButtons_' + articleId).style.visibility = 'visible';
    $('txtCommentText_' + articleId).style.color = '#000000';
    $('txtCommentText_' + articleId).value = '';
    $('txtCommentText_' + articleId).setAttribute('onclick', '');
}

BlogPost.hideAddCommentContainer = function(articleId)
{
    $('dAddNewCommentContainer_' + articleId).style.display = 'none';
    $('dAddNewCommentContainer_' + articleId).style.visibility = 'hidden';
    $('dArticleCommentButtons_' + articleId).style.display = 'none';
    $('dArticleCommentButtons_' + articleId).style.visibility = 'hidden';
    $('txtCommentText_' + articleId).style.color = '#ccc';
    $('txtCommentText_' + articleId).value = 'Post a comment...';
    $('txtCommentText_' + articleId).setAttribute('onclick', 'BlogPost.showAddCommentContainer(\'' + articleId + '\')');
}

BlogPost.addArticleComment = function(articleId)
{
    new Ajax.Request('Utilities/cmMethods.aspx', {
        method: 'post',
        parameters: { Command: 'AddArticleComment', ArticleID: articleId, CommentText: $F('txtCommentText_' + articleId), PostedByEmail: $F('txtEmailAddress_' + articleId), PostedByFirstName: $F('txtFirstName_' + articleId), PostedByLastName: $F('txtLastName_' + articleId) },
        onSuccess: function(xhrResponse)
        {
            BlogPost.getArticleComments(articleId);
            
            $('txtCommentText_' + articleId).value = '';
            $('txtEmailAddress_' + articleId).value = '';
            $('txtFirstName_' + articleId).value = '';
            $('txtLastName_' + articleId).value = '';
            BlogPost.hideAddCommentContainer(articleId);
        },
        onFailure: function(xhrResponse)
        {
        }
    });
}