﻿/************************************************************************  
VARS
**************************************************************************/
var _boardId = 0;
var _boardName = '';
var _boards = null;
var _deletedForumBoardId = 0;

var _threadTopicId = 0;
var _threadTopicName = '';
var _threadTopics = null;
var _deletedThreadTopicId = 0;

var _threadPostId = 0;
var _threadPosts = null;
var _deletedThreadPostId = 0;

var _forumTopicPageSize = 10;
var _forumTopicPageNumber = 1;
var _forumTopicCount = 0;

var _forumPostPageSize = 10;
var _forumPostPageNumber = 1;
var _forumPostCount = 0;

var _boardText = "Board";
var _threadTopicText = "ThreadTopic";
var _threadPostText = "ThreadPost";
var _riderText = "Court";

var _currentUserId = 0;
/************************************************************************ 
Page Element Enums
**************************************************************************/
var ForumElements =
{
    DivSecondIINoneForumError: 'divSecondIINoneForumError',
    DivForum: 'divForum',
    DivForumSubBoards: 'divForumSubBoards',
    DivBoards: 'divBoards',
    DivThreadTopics: 'divThreadTopics',
    DivThreadPosts: 'divThreadPosts'
}
/************************************************************************ 
Functions  
**************************************************************************/
$(document).ready(function() {
    isUserAuthenticated();
    isOfficer();
    forumBoardPageLoad();
});

function getBreadCrumb(level) {
    var breadCrumb = '';
    
    breadCrumb = breadCrumb + '<a href="#" onclick="javascript:getCurrentBoards();return false;">Second II None Forum</a>';
           
    switch (level) {
        case _boardText:
            break;
        case _threadTopicText:
            breadCrumb = breadCrumb + ' :: <a href="#" onclick="javascript:getCurrentTopics();return false;">' + _boardName + '</a>';
            break;
        case _threadPostText:
            breadCrumb = breadCrumb + ' :: <a href="#" onclick="javascript:getCurrentTopics();return false;">' + _boardName + '</a>';
            breadCrumb = breadCrumb + ' :: ' + _threadTopicName;
            break;
    }
    
    return breadCrumb;
}

function setBoardName() {
    for (var rowCount = 0; rowCount < _boards.length; rowCount++) {
        if (_boards[rowCount].Id == _boardId){
            _boardName = _boards[rowCount].Name;
        }
    }
}

function setThreadTopicName() {
    for (var rowCount = 0; rowCount < _threadTopics.length; rowCount++) {
        if (_threadTopics[rowCount].Id == _threadTopicId) {
            _threadTopicName = _threadTopics[rowCount].Subject;
        }
    }
}

function isUserAuthenticated() {
    SecondIINoneUIService.IsUserAuthenticated(onIsUserAuthenticatedComplete, onSecondIINoneCommonError);
}

function onIsUserAuthenticatedComplete(isUserAuthenticated) {
    _isUserAuthenticated = isUserAuthenticated;
}

function isOfficer() {
    SecondIINoneUIService.IsOfficer(onIsOfficerComplete, onSecondIINoneCommonError);
}

function onIsOfficerComplete(isOfficer) {
    _isOfficer = isOfficer;

    if (_isOfficer)
        showContent($get(ForumElements.DivBoards));
}

function populateForumBoards(element, forumBoards) {
    if (forumBoards.length > 0 && element.options.length ==0) {

        var optBoard = new Option(InformationMessages.Select, 0, false, false);
        element.options[element.options.length] = optBoard;

        for (var rowCount = 0; rowCount < forumBoards.length; rowCount++) {

            var optBoard = new Option(forumBoards[rowCount].Name, forumBoards[rowCount].Id, false, false);
            element.options[element.options.length] = optBoard;

        }
    }
}

function displayForumBoards(forumBoards) {

    _boards = forumBoards;    

    var divError = $get(ForumElements.DivSecondIINoneForumError);
    hideContent(divError);

    var forumBoardsDiv = $get(ForumElements.DivForum);
    showContent(forumBoardsDiv);

    var forumBoardsString = ''

    if (forumBoards.length > 0) {

        _isUserAuthenticated = forumBoards[0].IsUserAuthenticated;
        _isOfficer = forumBoards[0].IsOfficer;

        forumBoardsString = forumBoardsString + '<table cellpadding="0" cellspacing="0" align="center" class="forumTable">';
        forumBoardsString = forumBoardsString + '<tr class="blueBorder">';
        forumBoardsString = forumBoardsString + '<th>';
        forumBoardsString = forumBoardsString + '&nbsp;Author&nbsp;';
        forumBoardsString = forumBoardsString + '</th>';
        forumBoardsString = forumBoardsString + '<th>';
        forumBoardsString = forumBoardsString + '&nbsp;Board Name&nbsp;';
        forumBoardsString = forumBoardsString + '</th>';
        forumBoardsString = forumBoardsString + '<th>';
        forumBoardsString = forumBoardsString + '&nbsp;Topics&nbsp;';
        forumBoardsString = forumBoardsString + '</th>';
        forumBoardsString = forumBoardsString + '<th>';
        forumBoardsString = forumBoardsString + '&nbsp;Posts&nbsp;';
        forumBoardsString = forumBoardsString + '</th>';
        forumBoardsString = forumBoardsString + '<th>';
        forumBoardsString = forumBoardsString + '&nbsp;Last Post&nbsp;';
        forumBoardsString = forumBoardsString + '</th>';
        forumBoardsString = forumBoardsString + '</tr>';

        var forumBoardTypeId = 0;
        for (var forumBoardPostCount = 0; forumBoardPostCount < forumBoards.length; forumBoardPostCount++) {

            if (forumBoards[forumBoardPostCount].TypeId != forumBoardTypeId) {
                forumBoardsString = forumBoardsString + '<tr class="blueBorder" style="background-color: #CCCCCC;">';
                forumBoardsString = forumBoardsString + '<td colspan="5" class="forumBoardTypeName">';
                forumBoardsString = forumBoardsString + forumBoards[forumBoardPostCount].TypeName;
                forumBoardsString = forumBoardsString + '</td>';
                forumBoardsString = forumBoardsString + '</tr>';
            }

            forumBoardTypeId = forumBoards[forumBoardPostCount].TypeId

            forumBoardsString = forumBoardsString + '<tr>';
            forumBoardsString = forumBoardsString + '<td class="createdBy">';
            forumBoardsString = forumBoardsString + '<img class="picture" src="' + forumBoards[forumBoardPostCount].UserPicture + '" />';
            forumBoardsString = forumBoardsString + '<br /><span class="username">' + forumBoards[forumBoardPostCount].CreatedByUsername + '</span>';
            
            if(forumBoards[forumBoardPostCount].CreatedByOffice !=_riderText){
                forumBoardsString = forumBoardsString + '<br /><span class="office">' + forumBoards[forumBoardPostCount].CreatedByOffice + '</span>';
            }
            
            forumBoardsString = forumBoardsString + '<br /><span class="date">&nbsp;on&nbsp;' + forumBoards[forumBoardPostCount].CreatedDate + '</span>';
            forumBoardsString = forumBoardsString + '</td>';
            forumBoardsString = forumBoardsString + '<td class="content">';
            
            forumBoardsString = forumBoardsString + '<div class="forumTableMessage">';
            
            forumBoardsString = forumBoardsString + '<a href="#" class="topic" onclick="javascript:getThreadTopicsByForumBoardId(' + forumBoards[forumBoardPostCount].Id + ');return false;">';
            forumBoardsString = forumBoardsString + forumBoards[forumBoardPostCount].Name;
            forumBoardsString = forumBoardsString + '</a>';
            forumBoardsString = forumBoardsString + '<br/>';
            forumBoardsString = forumBoardsString + '<br/>';
            forumBoardsString = forumBoardsString + forumBoards[forumBoardPostCount].Description.replace(/\n/g, '<br />');
            
            forumBoardsString = forumBoardsString + '</div>';
            
            forumBoardsString = forumBoardsString + '<div class="forumTableLinks">';
            
            forumBoardsString = forumBoardsString + '<a href="#" class="topic" onclick="javascript:getThreadTopicsByForumBoardId(' + forumBoards[forumBoardPostCount].Id + ');return false;">';
            forumBoardsString = forumBoardsString + 'View Topic(s)';
            forumBoardsString = forumBoardsString + '</a>';
            
            if (_isOfficer || (forumBoards[forumBoardPostCount].CreatedBy == forumBoards[forumBoardPostCount].CurrentUserId)) {
                forumBoardsString = forumBoardsString + '&nbsp;|&nbsp;';
                forumBoardsString = forumBoardsString + '<a href="#" class="topic" onclick="javascript:updateForumBoard(' + forumBoards[forumBoardPostCount].Id + ');return false;">';
                forumBoardsString = forumBoardsString + 'Board';
                forumBoardsString = forumBoardsString + '</a>';
                
                forumBoardsString = forumBoardsString + '&nbsp;|&nbsp;';
                forumBoardsString = forumBoardsString + '<a href="#" class="topic" onclick="javascript:deleteForumBoard(' + forumBoards[forumBoardPostCount].Id + ');return false;">';
                forumBoardsString = forumBoardsString + 'Board';
                forumBoardsString = forumBoardsString + '</a>';
            }
          
            forumBoardsString = forumBoardsString + '</div>';
            
            forumBoardsString = forumBoardsString + '</td>';
            forumBoardsString = forumBoardsString + '<td class="count">';
            forumBoardsString = forumBoardsString + forumBoards[forumBoardPostCount].TopicCount;
            forumBoardsString = forumBoardsString + '</td>';
            forumBoardsString = forumBoardsString + '<td class="count">';
            forumBoardsString = forumBoardsString + forumBoards[forumBoardPostCount].PostCount;
            forumBoardsString = forumBoardsString + '</td>';
            forumBoardsString = forumBoardsString + '<td class="createdBy">';

            if (forumBoards[forumBoardPostCount].LastPostCreatedByUsername != undefined) {
                forumBoardsString = forumBoardsString + '<img class="picture" src="' + forumBoards[forumBoardPostCount].LastPostUserPicture + '" />';
                forumBoardsString = forumBoardsString + '<br /><span class="username">' + forumBoards[forumBoardPostCount].LastPostCreatedByUsername + '</span>';

                if (forumBoards[forumBoardPostCount].LastPostCreatedByOffice != _riderText) {
                    forumBoardsString = forumBoardsString + '<br /><span class="office">' + forumBoards[forumBoardPostCount].LastPostCreatedByOffice + '</span>';
                }

                forumBoardsString = forumBoardsString + '<br /><span class="date">&nbsp;on&nbsp;' + forumBoards[forumBoardPostCount].LastPostCreatedDate + '</span>';
            }
            else {
                forumBoardsString = forumBoardsString + '<p>&nbsp;</p>';
            }

            forumBoardsString = forumBoardsString + '</td>';
            forumBoardsString = forumBoardsString + '</tr>';
        }

        forumBoardsString = forumBoardsString + '</table>';
    }
    else {
        forumBoardsString = '<div class="whiteMessage">There are no boards in the Second II None Fourum at this time.</div>';
    }

    forumBoardsDiv.innerHTML = forumBoardsString;

    hideWaitDialog();
}

function getCurrentBoards() {

    hideForumSubBoards();
    hideThreadTopics();
    hideThreadPosts();
    
    var divBoards = $get(ForumElements.DivBoards);

    if (_isOfficer)
        showContent(divBoards);
    
    getForumBoardsUsingService();
}

function getCurrentTopics() {

    hideForumBoards();
    hideThreadPosts();
    
    var divThreadTopics = $get(ForumElements.DivThreadTopics);

    if (_isUserAuthenticated)
        showContent(divThreadTopics);

    _threadTopicBoardId = _boardId;
    setBoardName();

    threadTopicsPageLoad();
}

function getCurrentPosts() {

    hideForumBoards();
    hideThreadPosts();

    var divThreadPosts = $get(ForumElements.DivThreadPosts);

    if (_isUserAuthenticated)
        showContent(divThreadPosts);

    _threadTopicBoardId = _boardId;
    setBoardName();

    threadPostsPageLoad();
}

function getThreadTopicsByForumSubBoardId(boardId) {
    
    hideForumSubBoards();
    hideForumBoards();

    var divThreadTopics = $get(ForumElements.DivThreadTopics);

    if (_isUserAuthenticated)
        showContent(divThreadTopics);

    _boardId = boardId;
    _threadTopicBoardId = _boardId;

    setBoardName();

    threadTopicsPageLoad();
}
    
function getThreadTopicsByForumBoardId(boardId) {

    hideForumBoards();

    var divThreadTopics = $get(ForumElements.DivThreadTopics);

    var divBoards = $get(ForumElements.DivBoards);
    if (_isUserAuthenticated)
        showContent(divThreadTopics);

    _boardId = boardId;
    _threadTopicBoardId = _boardId;
    
    setBoardName();

    threadTopicsPageLoad();
}

function getThreadPostsByThreadTopicId(threadTopicId) {
    
    hideForumSubBoards();
    hideThreadTopics();

    var divThreadPosts = $get(ForumElements.DivThreadPosts);

    if (_isUserAuthenticated)
        showContent(divThreadPosts);

    _threadTopicId = threadTopicId;
    _threadTopicBoardId = _boardId;

    setBoardName();
    setThreadTopicName();

    threadPostsPageLoad();
}

function getThreadPostReplysByThreadPostId(threadPostId) {
    _threadPostId = threadPostId;
    _threadTopicBoardId = _threadPostId;
    threadPostsPageLoad();
}

function hideForumBoards() {
    hideContent($get(ForumElements.DivBoards));
}

function hideForumSubBoards() {
    var divSecondIIForumSubBoard = $get(ForumElements.DivForumSubBoards);
    divSecondIIForumSubBoard.innerHTML = '';

    hideContent(divSecondIIForumSubBoard);
}

function hideThreadTopics() {
    hideContent($get(ForumElements.DivThreadTopics));
}

function hideThreadPosts() {
    hideContent($get(ForumElements.DivThreadPosts));
}

function deleteForumBoard(boardId)
{
    _deletedForumBoardId = boardId;
    showConfirm(InformationMessages.ForumBoardDeleteConfirm, onConfirmDeleteForumBoard);
}

function onConfirmDeleteForumBoard() {
    removeConfirm();
    showWaitDialog(InformationMessages.ForumBoardDelete);
    ForumUIService.DeleteForumBoard(_deletedForumBoardId, onForumBoardDeletionComplete, onSecondIINoneForumError);
}

function onForumBoardDeletionComplete()
{
    hideWaitDialog();
    getCurrentBoards();
}

function deleteThreadTopic(threadTopicId)
{
    _deletedThreadTopicId = threadTopicId;
    showConfirm(InformationMessages.ForumThreadTopicDeleteConfirm, onConfirmDeleteThreadTopic);
}

function onConfirmDeleteThreadTopic()
{
    removeConfirm();
    showWaitDialog(InformationMessages.ForumThreadTopicDelete);
    ForumUIService.DeleteThreadTopic(_deletedThreadTopicId, onForumThreadTopicDeletionComplete, onSecondIINoneForumError);
}

function onForumThreadTopicDeletionComplete()
{
    hideWaitDialog();
    getCurrentTopics();
}

function deleteThreadPost(threadPostId)
{
    _deletedThreadPostId = threadPostId;
    showConfirm(InformationMessages.ForumThreadPostDeleteConfirm, onConfirmDeleteThreadPost);
}

function onConfirmDeleteThreadPost()
{
    removeConfirm();
    showWaitDialog(InformationMessages.ForumThreadPostDelete);
    ForumUIService.DeleteThreadPost(_deletedThreadPostId, onForumDeleteThreadPostComplete, onSecondIINoneForumError);
}

function onForumDeleteThreadPostComplete()
{
    getCurrentPosts();
    hideWaitDialog();
}

function onSecondIINoneForumError(result) {
    var problems = new Array();
    problems[problems.length] = result.get_message();
    setErrorInformation($get(ForumElements.DivSecondIINoneForumError), problems);

    hideWaitDialog();
}


