﻿/************************************************************************  
                                VARS
**************************************************************************/
var _selectedThreadTopicId = 0;
var _riderText = "Rider";

var _deletedFilePath = '';
var _currentAttachedFiles = '';

/************************************************************************ 
                        Page Element Enums
**************************************************************************/
var ForumThreadTopicElements =
{
    TxtThreadTopicSubject: 'txtThreadTopicSubject',
    Attachments: 'attachments',
    DivAttachedFiles: 'divAttachedFiles',
    LiParentBoards: 'liParentBoards',
    LiOrganizations: 'liThreadTopicOrganizations',
    DdlPrivacy: 'ddlThreadTopicPrivacy',
    DdlOrganizations: 'ddlThreadTopicOrganizations',

    JQTxtThreadTopicSubject: '#txtThreadTopicSubject',
    JQLiParentBoards: '#liParentBoards',
    JQLiOrganizations: '#liThreadTopicOrganizations',
    JQDdlPrivacy: '#ddlThreadTopicPrivacy',
    JQDdlOrganizations: '#ddlThreadTopicOrganizations'
}

var ForumThreadTopicFieldNames =
{
    Subject: 'Subject',
    Message: 'Message',
    Board: 'Board',
    Privacy: 'Private?'
}

/************************************************************************ 
                        Functions  
**************************************************************************/
function threadTopicsPageLoad() {

    showWaitDialog(InformationMessages.ForumThreadTopicRetrieval);

    setForumThreadTopicCountUsingService();
    getThreadTopicOrganizationsUsingService();
    getSecondIINoneThreadTopicsUsingService();
    getForumSubBoardsUsingService();
}

function setForumThreadTopicCountUsingService() {
    ForumUIService.GetForumThreadCount(_boardId, onSetForumThreadTopicCountUsingServiceComplete, onSecondIINoneForumError);
}

function onSetForumThreadTopicCountUsingServiceComplete(threadTopicCount) {

    _forumTopicCount = threadTopicCount;
}

function resetForumThreadTopicForm() {
    var attachments = $get(ForumThreadTopicElements.Attachments);
    var subject = $get(ForumThreadTopicElements.TxtThreadTopicSubject);
    var message = $get(ForumThreadTopicASPElements.TxtThreadTopicMessage);
    var privacy = $get(ForumThreadTopicElements.DdlPrivacy);
    var organization = $get(ForumThreadTopicElements.DdlOrganizations);

    attachments.value = '';
    subject.value = '';
    message.value = '';

    privacy.options[0].selected = true;
    organization.options[0].selected = true;

    var organizationsListItem = $get(ForumThreadPostElements.LiOrganizations);
    hideContent(organizationsListItem)
}

function updateThreadTopic(id) {
    _selectedThreadTopicId = id;

    getThreadTopicByIdUsingService();
}

function getThreadTopicByIdUsingService() {
    ForumUIService.GetForumThreadTopicById(_selectedThreadTopicId, onGetThreadTopicByIdUsingServiceComplete, onSecondIINoneForumError);
}

function onGetThreadTopicByIdUsingServiceComplete(threadTopic){
    populateThreadTopicFormByTopicId(threadTopic);
}

function populateThreadTopicFormByTopicId(threadTopic) {
    var subject = $get(ForumThreadTopicElements.TxtThreadTopicSubject);
    var message = $get(ForumThreadTopicASPElements.TxtThreadTopicMessage);
    var privacy = $get(ForumThreadTopicElements.DdlPrivacy);
    var organization = $get(ForumThreadTopicElements.DdlOrganizations);

    subject.value = threadTopic.Subject;
    message.value = threadTopic.Message;

    _currentAttachedFiles = threadTopic.Attachment;
    populateHiddenAttachedFiles();

    if (threadTopic.IsPrivate) {
        setSelectedDropDownListIndex(privacy, 2);

        toggleThreadTopicOrganizationsDropDownListDisplay();

        setSelectedDropDownListIndex(organization, threadTopic.OrganizationId);
    }
    else {
        setSelectedDropDownListIndex(privacy, 1);
    }

    subject.focus();
}

function getThreadTopicOrganizationsUsingService() {
    SecondIINoneUIService.GetOrganizations(onGetThreadTopicOrganizationsComplete, onSecondIINoneForumError);
}

function onGetThreadTopicOrganizationsComplete(organizations) {

    var element = $get(ForumThreadTopicElements.DdlOrganizations);

    populateSecondIINoneOrganizations(element, organizations);
}

function toggleThreadTopicOrganizationsDropDownListDisplay() {
    var organizationsListItem = $get(ForumThreadTopicElements.LiOrganizations);
    if ($(ForumThreadTopicElements.JQDdlPrivacy).val() == 2) {
        showContent(organizationsListItem)
    }
    else {
        hideContent(organizationsListItem)
    }
}

function validateForumThreadTopicForm() {

    var subject = $(ForumThreadTopicElements.JQTxtThreadTopicSubject);
    var message = $(ForumThreadTopicASPElements.JQTxtThreadTopicMessage);
    var privacy = $(ForumThreadTopicElements.JQDdlPrivacy);

    _errorManager.ClearErrors();

    var errorCount = 0;

    if (subject.val().trim() == "") {
        _errorManager.AddErrorMessage(subject, String.format(ErrorMessages.RequiredField, ForumThreadTopicFieldNames.Subject));
        errorCount++;
    }

    if (message.val().trim() == "") {
        _errorManager.AddErrorMessage(message, String.format(ErrorMessages.RequiredField, ForumThreadTopicFieldNames.Message));
        errorCount++;
    }

    if (privacy.val() == 0) {
        _errorManager.AddErrorMessage(privacy, String.format(ErrorMessages.RequiredField, ForumThreadTopicFieldNames.Privacy));
        errorCount++;
    }

    if (errorCount > 0) {
        _errorManager.ShowErrors();
        return false;
    }
    else {
        return true;
    }
}

function saveSecondIINoneForumThreadTopic() {

    var formValidated = validateForumThreadTopicForm();

    if (!formValidated)
        return false;

    showWaitDialog(InformationMessages.ForumThreadTopicSave);

    saveForumThreadTopic();
}

function saveForumThreadTopic() {
    var attachments = getElementText($get(ForumThreadTopicElements.Attachments));
    var subject = getElementText($get(ForumThreadTopicElements.TxtThreadTopicSubject));
    var message = getElementText($get(ForumThreadTopicASPElements.TxtThreadTopicMessage));
    var isPrivate = getSelectedValue($get(ForumThreadTopicElements.DdlPrivacy));
    var organizationId = getSelectedValue($get(ForumThreadTopicElements.DdlOrganizations));

    var forumThreadTopicData = new ForumThreadTopicData();

    isPrivate = isPrivate - 1;

    forumThreadTopicData.Subject = subject;
    forumThreadTopicData.Message = message;
    forumThreadTopicData.Attachment = attachments;
    forumThreadTopicData.BoardId = _boardId;
    forumThreadTopicData.IsPrivate = isPrivate;
    
    if (organizationId > 0 && isPrivate == 1)
        forumThreadTopicData.OrganizationId = organizationId;

    if (_selectedThreadTopicId > 0)
        forumThreadTopicData.Id = _selectedThreadTopicId;

    saveSecondIINoneThreadTopicUsingService(forumThreadTopicData);
}

function saveSecondIINoneThreadTopicUsingService(forumThreadTopicData) {
    ForumUIService.SaveForumThreadTopic(forumThreadTopicData, onSaveSecondIINoneThreadTopicComplete, onSecondIINoneForumError);
}

function onSaveSecondIINoneThreadTopicComplete() {
    if (_isUserAuthenticated)
        resetForumThreadTopicForm();
        
    _selectedThreadTopicId = 0;
        
    getSecondIINoneThreadTopicsUsingService();
}

function getSecondIINoneThreadTopicsUsingService() {
    ForumUIService.GetForumThreadTopicsByBoardId(_threadTopicBoardId, _forumTopicPageSize, _forumTopicPageNumber, onGetSecondIINoneThreadTopicsComplete, onSecondIINoneForumError);
}

function onGetSecondIINoneThreadTopicsComplete(threadTopics) {
    displayThreadTopics(threadTopics);
}

function displayThreadTopics(threadTopics) {

    _threadTopics = threadTopics;

    var divError = $get(ForumElements.DivSecondIINoneForumError);
    hideContent(divError);

    var threadTopicsDiv = $get(ForumElements.DivForum);
    showContent(threadTopicsDiv);

    var threadTopicsString = '';

    if (threadTopics.length > 0) {
        _isUserAuthenticated = threadTopics[0].IsUserAuthenticated;
        _isOfficer = threadTopics[0].IsOfficer;

        threadTopicsString = threadTopicsString + '<table cellpadding="0" cellspacing="0" align="center" class="forumContainerTable">';
        threadTopicsString = threadTopicsString + '<tr class="breadCrumb">';
        threadTopicsString = threadTopicsString + '<td>';
        threadTopicsString = threadTopicsString + getBreadCrumb(_threadTopicText);
        threadTopicsString = threadTopicsString + '</td>';
        threadTopicsString = threadTopicsString + '</tr>';
        threadTopicsString = threadTopicsString + '<tr class="blueBorder">';
        threadTopicsString = threadTopicsString + '<td class="forumTableTopicCount">';

        if (_forumTopicCount > _forumTopicPageSize)
        {
            threadTopicsString = threadTopicsString + getThreadTopicsPagingMessage();
        }
        else
        {
            threadTopicsString = threadTopicsString + String.format(InformationMessages.ThreadTopicsPagingSinglePagePostCountMessage, _forumTopicCount);
        }
        
        threadTopicsString = threadTopicsString + '</td>';
        threadTopicsString = threadTopicsString + '</tr>';
        threadTopicsString = threadTopicsString + '<tr class="blueBorder">';
        threadTopicsString = threadTopicsString + '<td class="greyBackground">';

        if (_forumTopicCount > _forumTopicPageSize) {
            var threadTopicPageCount = _forumTopicCount / _forumTopicPageSize;

            threadTopicsString = threadTopicsString + '&nbsp;<p class="gridPageNumber">';
            for (var j = 1; j < threadTopicPageCount + 1; j++) {
                if (_forumTopicPageNumber != j) {
                    threadTopicsString = threadTopicsString + '<a href="javascript:getRequestedThreadTopicPage('
                    threadTopicsString = threadTopicsString + j;
                    threadTopicsString = threadTopicsString + ')" >';
                    threadTopicsString = threadTopicsString + j;
                    threadTopicsString = threadTopicsString + '</a>&nbsp;';
                }
                else {
                    threadTopicsString = threadTopicsString + j;
                }
            }
            threadTopicsString = threadTopicsString + '</p>';
        }
        else {
            threadTopicsString = threadTopicsString + '<p>&nbsp;</p>';
        }

        threadTopicsString = threadTopicsString + '<table cellpadding="0" cellspacing="0" align="center" class="forumTable">';
        threadTopicsString = threadTopicsString + '<tr class="blueBorder">';
        threadTopicsString = threadTopicsString + '<th>';
        threadTopicsString = threadTopicsString + '&nbsp;Author&nbsp;';
        threadTopicsString = threadTopicsString + '</th>';
        threadTopicsString = threadTopicsString + '<th>';
        threadTopicsString = threadTopicsString + '&nbsp;Topic&nbsp;';
        threadTopicsString = threadTopicsString + '</th>';
        threadTopicsString = threadTopicsString + '<th>';
        threadTopicsString = threadTopicsString + '&nbsp;Posts&nbsp;';
        threadTopicsString = threadTopicsString + '</th>';
        threadTopicsString = threadTopicsString + '<th>';
        threadTopicsString = threadTopicsString + '&nbsp;Views&nbsp;';
        threadTopicsString = threadTopicsString + '</th>';
        threadTopicsString = threadTopicsString + '<th>';
        threadTopicsString = threadTopicsString + '&nbsp;Last Post&nbsp;';
        threadTopicsString = threadTopicsString + '</th>';
        threadTopicsString = threadTopicsString + '</tr>';

        for (var threadTopicPostCount = 0; threadTopicPostCount < threadTopics.length; threadTopicPostCount++) {

            var isCreator = (threadTopics[threadTopicPostCount].CreatedBy == threadTopics[threadTopicPostCount].CurrentUserId);

            threadTopicsString = threadTopicsString + '<tr>';
            threadTopicsString = threadTopicsString + '<td class="createdBy">';
            threadTopicsString = threadTopicsString + '<img class="picture" src="' + threadTopics[threadTopicPostCount].UserPicture + '" />';
            threadTopicsString = threadTopicsString + '<br /><span class="username">' + threadTopics[threadTopicPostCount].CreatedByUsername + '</span>';

            if (threadTopics[threadTopicPostCount].CreatedByOffice != _riderText) {
                threadTopicsString = threadTopicsString + '<br /><span class="office">' + threadTopics[threadTopicPostCount].CreatedByOffice + '</span>';
            }
            
            threadTopicsString = threadTopicsString + '<br /><span class="date">&nbsp;on&nbsp;' + threadTopics[threadTopicPostCount].CreatedDate + '</span>';
            threadTopicsString = threadTopicsString + '</td>';
            threadTopicsString = threadTopicsString + '<td class="content">';

            threadTopicsString = threadTopicsString + '<div class="forumTableMessage">';

            threadTopicsString = threadTopicsString + '<a href="#" class="topic" onclick="javascript:getThreadPostsByThreadTopicId(' + threadTopics[threadTopicPostCount].Id + ');return false;">';
            threadTopicsString = threadTopicsString + threadTopics[threadTopicPostCount].Subject;
            threadTopicsString = threadTopicsString + '</a>';
            threadTopicsString = threadTopicsString + '<br/>';
            threadTopicsString = threadTopicsString + '<br/>';
            threadTopicsString = threadTopicsString + threadTopics[threadTopicPostCount].Message.replace(/\n/g, '<br />');

            threadTopicsString = threadTopicsString + getAttachments(threadTopics[threadTopicPostCount].Id, threadTopics[threadTopicPostCount].Attachment.replace('/','//'), _isOfficer, isCreator);
            threadTopicsString = threadTopicsString + '</div>';

            threadTopicsString = threadTopicsString + '<div class="forumTableLinks">';

            threadTopicsString = threadTopicsString + '<a href="#" class="topic" onclick="javascript:getThreadPostsByThreadTopicId(' + threadTopics[threadTopicPostCount].Id + ');return false;">';
            threadTopicsString = threadTopicsString + 'View Post(s)';
            threadTopicsString = threadTopicsString + '</a>';

            if (_isOfficer || isCreator) {

                threadTopicsString = threadTopicsString + '&nbsp;|&nbsp;';
                threadTopicsString = threadTopicsString + '<a href="#" class="topic" onclick="javascript:updateThreadTopic(' + threadTopics[threadTopicPostCount].Id + ');return false;">';
                threadTopicsString = threadTopicsString + 'Modify';
                threadTopicsString = threadTopicsString + '</a>';
            
                threadTopicsString = threadTopicsString + '&nbsp;|&nbsp;';
                threadTopicsString = threadTopicsString + '<a href="#" class="topic" onclick="javascript:deleteThreadTopic(' + threadTopics[threadTopicPostCount].Id + ');return false;">';
                threadTopicsString = threadTopicsString + 'Delete';
                threadTopicsString = threadTopicsString + '</a>';
                
            }

            threadTopicsString = threadTopicsString + '</div>';
            
            threadTopicsString = threadTopicsString + '</td>';
            threadTopicsString = threadTopicsString + '<td class="count">';
            threadTopicsString = threadTopicsString + threadTopics[threadTopicPostCount].PostCount;
            threadTopicsString = threadTopicsString + '</td>';
            threadTopicsString = threadTopicsString + '<td class="count">';
            threadTopicsString = threadTopicsString + threadTopics[threadTopicPostCount].Views;
            threadTopicsString = threadTopicsString + '</td>';
            threadTopicsString = threadTopicsString + '<td class="createdBy">';

            if (threadTopics[threadTopicPostCount].LastPostCreatedByUsername != undefined) {
                threadTopicsString = threadTopicsString + '<img class="picture" src="' + threadTopics[threadTopicPostCount].LastPostUserPicture + '" />';
                threadTopicsString = threadTopicsString + '<br /><span class="username">' + threadTopics[threadTopicPostCount].LastPostCreatedByUsername + '</span>';

                if (threadTopics[threadTopicPostCount].LastPostCreatedByOffice != _riderText) {
                    threadTopicsString = threadTopicsString + '<br /><span class="office">' + threadTopics[threadTopicPostCount].LastPostCreatedByOffice + '</span>';
                }
                
                threadTopicsString = threadTopicsString + '<br /><span class="date">&nbsp;on&nbsp;' + threadTopics[threadTopicPostCount].LastPostCreatedDate + '</span>';
            }
            else {
                threadTopicsString = threadTopicsString + '<p>&nbsp;</p>';
            }
            
            threadTopicsString = threadTopicsString + '</td>';
            threadTopicsString = threadTopicsString + '</tr>';
        }

        threadTopicsString = threadTopicsString + '</table>';

        threadTopicsString = threadTopicsString + '<p>&nbsp;</p>';
    
        threadTopicsString = threadTopicsString + '</td>';
        threadTopicsString = threadTopicsString + '</tr>';
        threadTopicsString = threadTopicsString + '</table>';
    }
    else {
        threadTopicsString = '<div class="whiteMessage">There are no topics under this board.</div>';

        threadTopicsString = threadTopicsString + '<p>&nbsp;</p>';
        threadTopicsString = threadTopicsString + '<p>&nbsp;</p>';
        threadTopicsString = threadTopicsString + '<div class="breadCrumbEmptyRowCount">';
        threadTopicsString = threadTopicsString + getBreadCrumb(_threadPostText);
        threadTopicsString = threadTopicsString + '</div>';
    }

    threadTopicsDiv.innerHTML = threadTopicsString;

    hideWaitDialog();
}

function getAttachments(topicId, attachments, isOfficer, isCreator) {
    var attachmentString = '';

    if (attachments.length > 0) {
        var attachmentsSplit = attachments.split(",");

        attachmentString = '<br/>';
        attachmentString = attachmentString + '<br/>';
        attachmentString = attachmentString + '<br/>';
        attachmentString = attachmentString + 'ATTACHMENTS:<br/>';
            
        for (i = 0; i < attachmentsSplit.length; i++) {
            attachmentString = attachmentString + '<a target="_blank" href="' + _applicationWebPath + attachmentsSplit[i] + '">' + attachmentsSplit[i].substring(attachmentsSplit[i].lastIndexOf("/")+1, attachmentsSplit[i].length) + '</a>';
            if (isOfficer || isCreator) {
                attachmentString = attachmentString + '&nbsp;<a href="#" onclick="javascript:deleteAttachment(' + topicId + ',' + i + ')">[x]</a>';
            }
            attachmentString = attachmentString + '<br/>';
        }

        attachmentString = attachmentString + '<br/>';
        attachmentString = attachmentString + '<br/>';
    }

    return attachmentString;
}

function getForumSubBoardsUsingService() {
    ForumUIService.GetForumBoardsByBoardId(_boardId, onGetForumSubBoardsComplete, onSecondIINoneForumError);
}

function onGetForumSubBoardsComplete(forumBoards) {
    displayForumSubBoards(forumBoards);
}

function displayForumSubBoards(forumBoards) {

    _boards = forumBoards;

    var divError = $get(ForumElements.DivSecondIINoneForumError);
    hideContent(divError);

    var forumBoardsDiv = $get(ForumElements.DivForumSubBoards);
    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 style="border: 1px solid #56A5EC;">';
        forumBoardsString = forumBoardsString + '<th>';
        forumBoardsString = forumBoardsString + '&nbsp;Author&nbsp;';
        forumBoardsString = forumBoardsString + '</th>';
        forumBoardsString = forumBoardsString + '<th>';
        forumBoardsString = forumBoardsString + '&nbsp;Sub 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>';

        for (var forumBoardPostCount = 0; forumBoardPostCount < forumBoards.length; forumBoardPostCount++) {

            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:getThreadTopicsByForumSubBoardId(' + 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:getThreadTopicsByForumSubBoardId(' + forumBoards[forumBoardPostCount].Id + ');return false;">';
            forumBoardsString = forumBoardsString + 'View Topic(s)';
            forumBoardsString = forumBoardsString + '</a>';

            if (_isOfficer) {
                forumBoardsString = forumBoardsString + '&nbsp;|&nbsp;';
                forumBoardsString = forumBoardsString + '<a href="#" class="topic" onclick="javascript:updateForumBoard(' + forumBoards[forumBoardPostCount].Id + ');return false;">';
                forumBoardsString = forumBoardsString + 'Modify';
                forumBoardsString = forumBoardsString + '</a>';
                
                forumBoardsString = forumBoardsString + '&nbsp;|&nbsp;';
                forumBoardsString = forumBoardsString + '<a href="#" class="topic" onclick="javascript:deleteForumBoard(' + forumBoards[forumBoardPostCount].Id + ');return false;">';
                forumBoardsString = forumBoardsString + 'Delete';
                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>';
        forumBoardsString = forumBoardsString + '<p>&nbsp;</p>';
    }
    else {
        hideContent(forumBoardsDiv);
    }

    forumBoardsDiv.innerHTML = forumBoardsString;
}

function getRequestedThreadTopicPage(pageNumber) {
    _forumTopicPageNumber = pageNumber;
    getSecondIINoneThreadTopicsUsingService();
}

function getThreadTopicsPagingMessage() {
    var firstRow = 1
    var lastRow = 1
    var numberOfPostsToList = _forumTopicPageSize

    var numberOfPages = (_forumTopicCount / _forumTopicPageSize);
    var remainingPosts = (_forumTopicCount % _forumTopicPageSize);
    if (remainingPosts < 10)
        numberOfPages = Math.round(numberOfPages) + 1;
    else
        numberOfPages = Math.round(numberOfPages);

    var maxRowCount = (numberOfPages * _forumTopicPageSize);

    firstRow = _forumTopicCount - (_forumTopicPageSize * (_forumTopicPageNumber - 1));
    if (_forumTopicPageNumber == numberOfPages) {
        numberOfPostsToList = remainingPosts;
        lastRow = 1;
    }
    else {
        lastRow = firstRow - (_forumTopicPageSize) + 1;
    }

    if (firstRow < 0)
        firstRow = 1;

    return String.format(InformationMessages.ThreadTopicsPagingPostCountMessage, numberOfPostsToList, lastRow, firstRow);
}

function deleteAttachment(topicId, index) {
    _selectedThreadTopicId = topicId;
    _deletedFileIndex = index;
    showConfirm(InformationMessages.FileDeletionConfirm, OnConfirmDeleteForumAttachmentFile);
}

function OnConfirmDeleteForumAttachmentFile() {
    removeConfirm();
    showWaitDialog(InformationMessages.FileDeletion);
    ForumUIService.DeleteForumAttachmentFile(_selectedThreadTopicId, _deletedFileIndex, onGetSecondIINoneDeleteForumAttachmentFileComplete, onSecondIINoneForumError);
}

function onGetSecondIINoneDeleteForumAttachmentFileComplete(isFileDeleted) {
    threadTopicsPageLoad();
}

function populateHiddenAttachedFiles() {
    var hidFilesAttached = $get(ForumThreadTopicElements.Attachments);
    if (_currentAttachedFiles != null) {
        hidFilesAttached.value =  _currentAttachedFiles
    }
}
