﻿///////////////////////////////////////////////////////////////////////////////////////////////////

function initComments(commentSection,action){
	comments = document.getElementById("comments_"+commentSection);
	comments.style.display = (action != 'off') ? 'block' : 'none';
}

///////////////////////////////////////////////////////////////////////////////////////////////////

function showCategory(activeCat){
    document.blogform.category_id.value = activeCat;
    document.blogform.page_number.value = 1;
    document.blogform.submit();
}

///////////////////////////////////////////////////////////////////////////////////////////////////

function changePage(pageNum){
    document.blogform.page_number.value = pageNum;
    document.blogform.submit();
}

///////////////////////////////////////////////////////////////////////////////////////////////////

function showTag(tagName){
}

///////////////////////////////////////////////////////////////////////////////////////////////////

function showAuthor(authName){
}

///////////////////////////////////////////////////////////////////////////////////////////////////

function selectMonth(monthName){
    document.blogform.archive_date.value = monthName;
    document.blogform.page_number.value = 1;
    document.blogform.submit();
}

///////////////////////////////////////////////////////////////////////////////////////////////////

function show(el){
    document.getElementById(el).style.display = 'block';
}

///////////////////////////////////////////////////////////////////////////////////////////////////

function hide(el){
    document.getElementById(el).style.display = 'none';
}

///////////////////////////////////////////////////////////////////////////////////////////////////

function processAjaxResponse(post_id, json, comments)
{
    if (json != null && 'SUCCESS' == json.status)
    {
        if (json.form == 'activate_form')
        {
            buildActivateCommentForm(_blog_id, post_id, json.message);
        }
        else if (json.form == 'comment_form')
        {
            buildCommentForm(_blog_id, post_id, '', json.username);
        }
        else if (json.form == 'logon_form')
        {
            buildCommentLogonForm(_blog_id, post_id, json.messages, json.message);
        }
        else if (json.form == 'account_form')
        {
            buildCreateAccountForm(_blog_id, post_id, json.messages,
                json.logonname, json.email, json.firstname, json.middlename, json.lastname);
        }
        else if (json.form == 'forgot_password')
        {
            buildForgotPassword(_blog_id, post_id, json.messages, json.email);
        }

        if (json.command == 'retrieve_comments')
        {
            retrieveComments(post_id);
        }
        else if (json.command == 'display_comments')
        {
            document.getElementById('comment_list_' + post_id).innerHTML = comments;
        }
    }
    else if (json != null)
    {
        document.getElementById('comments_form_' + post_id).innerHTML = json.messages;
    }
}

///////////////////////////////////////////////////////////////////////////////////////////////////

function activateCommentsForm(postId)
{
    $('activate_comments_' + postId).request({
        onComplete: function(transport, json)
              {
                  processAjaxResponse(postId, json);
              }
    })
}

///////////////////////////////////////////////////////////////////////////////////////////////////

function logon(postId)
{
    $('logon_form_' + postId).request({
        onComplete: function(transport, json)
              {
                  processAjaxResponse(postId, json);
              }
    })
}


///////////////////////////////////////////////////////////////////////////////////////////////////

function logonFormKeypress(e, post_id)
{
    var keynum;
    if(window.event) // IE
    {
        keynum = e.keyCode;
    }
    else if(e.which) // Netscape/Firefox/Opera
    {
        keynum = e.which;
    }

    if (keynum == 13)
    {
        logon(post_id);
        return false;
    }

    return true;
}

///////////////////////////////////////////////////////////////////////////////////////////////////

function postComment(postId)
{
    $('post_comment_form_' + postId).request({
        onComplete: function(transport, json)
              {
                  processAjaxResponse(postId, json);
              }
    })
}

///////////////////////////////////////////////////////////////////////////////////////////////////

function retrieveComments(postId)
{
    jQuery.ajax({
        url: '/core/controls/BlogFormHandler.ashx',
        type: 'POST',
        data: {
            comment_post_id: postId,
            blog_id: _blog_id,
            form_type: 'retrieve_comments'
        },
        complete: function(xhr, textStatus) {
            var json = eval( "(" + xhr.getResponseHeader('X-JSON') + ")" );
            processAjaxResponse(postId, json, xhr.responseText);
        }
    });
}

///////////////////////////////////////////////////////////////////////////////////////////////////

function createAccount(postId)
{
    $('account_form_' + postId).request({
        onComplete: function(transport, json)
              {
                  processAjaxResponse(postId, json);
              }
    })
}

function passwordReminder(postId)
{
    $('forgot_password_form_' + postId).request({
        onComplete: function(transport, json)
              {
                  processAjaxResponse(postId, json);
              }
    })
}

///////////////////////////////////////////////////////////////////////////////////////////////////

function buildActivateCommentForm(blog_id, post_id, message)
{
    var html = '';
    html = html + '<div class="error_message">' + message + '</div>';
    html = html + '<div class="comments_form" id="comments_form_' + post_id + '">';
    html = html + '<form id="activate_comments_' + post_id + '" name="activate_comments_' + post_id + '" action="/core/controls/BlogFormHandler.ashx" method="post" class="commentForm" >';
    html = html + '<input type="hidden" name="form_type" value="activate" />';
    html = html + '<input type="hidden" name="blog_id" value="' + blog_id + '" />';
    html = html + '<input type="hidden" name="comment_post_id" value="' + post_id + '" /></form>';
    html = html + '<input type="submit" class="comment_activate_button" value="Add Comment" onclick="activateCommentsForm(' + post_id + ');" /></div>';

    document.getElementById('comments_form_' + post_id).innerHTML = html;
}

///////////////////////////////////////////////////////////////////////////////////////////////////

function buildCommentForm(blog_id, post_id, message, username)
{
    var html = '';
    html = html + '<form name="post_comment_form_' + post_id + '" id="post_comment_form_' + post_id + '" action="/core/controls/BlogFormHandler.ashx" method="post" class="commentForm">';
    html = html + '<input type="hidden" name="form_type" value="comment" />';
    html = html + '<input type="hidden" name="blog_id" value="' + blog_id + '" />';
    html = html + '<input type="hidden" name="comment_post_id" value="' + post_id + '" />';
    html = html + '<div class="commentsSubHeading">Add a new comment</div>';
    html = html + '<table><tr><td>';
    html = html + '<label class="comment_logon_name_label">Name</label></td>';
    html = html + '<td><input class="comment_logon_name" type="text" disabled="disabled" value="' + username + '" /></td></tr>';
    html = html + '<tr><td><label>Comment</label></td>';
    html = html + '<td><textarea class="comment_message" name="comment_message" >' + message + '</textarea></td></tr>';
    html = html + '<tr><td colspan="2"><div class="captcha"><img id="captcha_' + post_id + '" class="comment_captcha" src="/core/img/spacer.gif" width="163" height="63" /><br />';
    html = html + '<label class="comment_captcha_label">Enter the characters shown above</label><br />';
    html = html + '<input class="comment_captcha" name="comment_captcha" type="text" /></div></td></tr></table></form>';
    html = html + '<input type="submit" class="comment_activate_button" value="Submit" onclick="postComment(' + post_id + ');" />';
    html = html + '<input type="submit" class="comment_activate_button" value="Cancel" onclick="buildActivateCommentForm(' + blog_id + ', ' + post_id + ', \'\');" />';

    document.getElementById('comments_form_' + post_id).innerHTML = html;
}

///////////////////////////////////////////////////////////////////////////////////////////////////

function buildCommentLogonForm(blog_id, post_id, messages, message)
{
    var html = '';

    html = html + '<div class="error_message">' + messages + '</div>';
    html = html + '<div class="error_message">' + message + '</div>';
    html = html + '<form name="logon_form_' + post_id + '" id="logon_form_' + post_id + '" action="/core/controls/BlogFormHandler.ashx" method="post" class="commentForm">';
    html = html + '<input type="hidden" name="form_type" value="logon" />';
    html = html + '<input type="hidden" name="blog_id" value="' + blog_id + '" />';
    html = html + '<input type="hidden" name="comment_post_id" value="' + post_id + '" />';
    html = html + '<div class="commentsSubHeading">You must be logged in to post a comment</div>';
    html = html + '<table><tr><td><label class="comment_logon_name_label">Logon</label></td>';
    html = html + '<td><input onkeydown="return logonFormKeypress(event,' + post_id + ')" type="text" class="comment_logon_name" name="logon_name" /></td></tr>';
    html = html + '<tr><td><label class="comment_logon_pass_label">Password</label></td>';
    html = html + '<td><input onkeydown="return logonFormKeypress(event,' + post_id + ')" type="password" class="comment_logon_pass" name="logon_pass" /></td></tr>';
    html = html + '<tr><td></td><td>';
    html = html + '<input type="button" class="comment_activate_button" value="Logon" onclick="logon(' + post_id + ');" />';
    html = html + '<input type="button" class="comment_activate_button" value="Cancel" onclick="buildActivateCommentForm(' + blog_id + ', ' + post_id + ', \'\');" />';
    html = html + '</td></tr></table>';
    html = html + '<div class="forgotPasswordLink"><a href="javascript:" onclick="buildForgotPassword(' + blog_id + ', ' + post_id + ', \'\', \'\'); return false;">I forgot my password</a></div>';
    html = html + '</form>';
    html = html + '<form name="create_account_' + post_id + '" id="create_account_' + post_id + '" action="/core/controls/BlogFormHandler.ashx" method="post" class="commentForm">';
    html = html + '<input type="hidden" name="form_type" value="create_account" />';
    html = html + '<input type="hidden" name="blog_id" value="' + blog_id + '" />';
    html = html + '<input type="hidden" name="comment_post_id" value="' + post_id + '" />';
    html = html + '<div class="commentsSubHeading">If you do not have an account but would like to create one, please click &quot;Create Account&quot; below.</div>';
    html = html + '<input type="button" class="comment_activate_button" value="Create Account" onclick="buildCreateAccountForm(' + blog_id + ', ' + post_id + ', \'\', \'\', \'\', \'\', \'\', \'\');" />';
    html = html + '</form>';

    document.getElementById('comments_form_' + post_id).innerHTML = html;
}

///////////////////////////////////////////////////////////////////////////////////////////////////

function buildCreateAccountForm(blog_id, post_id, message, logonname, email, firstname, middlename, lastname)
{
    var html = '';

    if (message)
    {
        html = html + '<div class="error_message">' + message + '</div>';
    }
    html = html + '<form id="account_form_' + post_id + '" name="account_form_' + post_id + '" action="/core/controls/BlogFormHandler.ashx" method="post" class="commentForm" >';
    html = html + '<input type="hidden" name="form_type" value="account_form" />';
    html = html + '<input type="hidden" name="blog_id" value="' + blog_id + '" />';
    html = html + '<input type="hidden" name="comment_post_id" value="' + post_id + '" />';

    html = html + '<div class="commentsSubHeading">Account Information</div>';
    html = html + '<table>';
    html = html + '<tr><td><label class="req">Username</label></td>';
    html = html + '<td><input type="text" class="account_username" name="account_username" value="' + logonname + '"/></td></tr>';
    html = html + '<tr><td><label class="req">Password</label></td>';
    html = html + '<td><input type="password" class="account_password" name="account_password" /></td></tr>';
    html = html + '<tr><td><label class="req">Verify Password</label></td>';
    html = html + '<td><input type="password" class="account_verify_password" name="account_verify_password" /></td></tr>';
    html = html + '<tr><td><label class="req">Email Address</label></td>';
    html = html + '<td><input type="text" class="account_email" name="email" value="' + email + '"/></td></tr>';
    html = html + '<tr><td><label class="req">First Name</label></td>';
    html = html + '<td><input type="text" class="account_firstname" name="firstname" value="' + firstname + '"/></td></tr>';
    html = html + '<tr><td><label>Middle Name</label></td>';
    html = html + '<td><input type="text" class="account_middlename" name="middlename" value="' + middlename + '"/></td></tr>';
    html = html + '<tr><td><label class="req">Last Name</label></td>';
    html = html + '<td><input type="text" class="account_lastname" name="lastname" value="' + lastname + '"/></td></tr>';
    html = html + '</table><br />';

    html = html + '<input type="button" class="comment_activate_button" value="Create Account" onclick="createAccount(' + post_id + '); return false;" />';
    html = html + '<input type="button" class="comment_activate_button" value="Cancel" onclick="buildActivateCommentForm(' + blog_id + ', ' + post_id + ', \'\');" /></form>';

    document.getElementById('comments_form_' + post_id).innerHTML = html;
}

///////////////////////////////////////////////////////////////////////////////////////////////////

function buildForgotPassword(blog_id, post_id, message, email)
{
    var html = '';

    html = html + '<div class="error_message">' + message + '</div>';
    html = html + '<form name="forgot_password_form_' + post_id + '" id="forgot_password_form_' + post_id + '" action="/core/controls/BlogFormHandler.ashx" method="post" class="commentForm">';
    html = html + '<input type="hidden" name="form_type" value="forgot_password" />';
    html = html + '<input type="hidden" name="blog_id" value="' + blog_id + '" />';
    html = html + '<input type="hidden" name="comment_post_id" value="' + post_id + '" />';
    html = html + '<div class="commentsSubHeading">Password Reset Form</div>';
    html = html + '<div class="password_reset_text">If you forgot your password, enter the e-mail address you used to register and we will e-mail you a new password.</div>';
    html = html + '<table><tr><td><label class="req">Email Address</label></td>';
    html = html + '<td><input type="text" class="text" name="email" value="' + email + '"/></td></tr>';
    html = html + '<tr><td></td><td>';
    html = html + '<input type="button" class="comment_activate_button" value="Reset Password" onclick="passwordReminder(' + post_id + '); return false;" />';
    html = html + '<input type="button" class="comment_activate_button" value="Cancel" onclick="buildActivateCommentForm(' + blog_id + ', ' + post_id + ', \'\');" />';
    html = html + '</td></tr></table>';
    html = html + '</form>';

    document.getElementById('comments_form_' + post_id).innerHTML = html;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
