﻿/************************************************************************  
                                VARS
**************************************************************************/
var _isThreadPostReplyDialogVisible = false;
var _currentPostMessage = '';
var _selectedThreadPostId = 0;

/************************************************************************ 
                        Page Element Enums
**************************************************************************/
var ForumThreadPostElements =
{
    LiOrganizations: 'liThreadPostOrganizations',
    DdlPrivacy: 'ddlThreadPostPrivacy',
    DdlOrganizations: 'ddlThreadPostOrganizations',

    JQLiOrganizations: '#liThreadPostOrganizations',
    JQDdlPrivacy: '#ddlThreadPostPrivacy',
    JQDdlOrganizations: '#ddlThreadPostOrganizations'

}

var ForumThreadPostFieldNames =
{
    Message: 'Message',
    Privacy: 'Private?'
}

/************************************************************************ 
                        Functions  
**************************************************************************/

function threadPostsPageLoad() {

    showWaitDialog(InformationMessages.ForumThreadPostRetrieval);

    setForumThreadPostCountUsingService();
    getThreadPostOrganizationsUsingService();
}

function setForumThreadPostCountUsingService() {
    ForumUIService.GetForumThreadPostCount(_threadTopicId, onSetForumThreadPostCountUsingServiceComplete, onSecondIINoneForumError);
}

function onSetForumThreadPostCountUsingServiceComplete(threadPostCount) {

    _forumPostCount = threadPostCount;
}

function getThreadPostOrganizationsUsingService() {
    SecondIINoneUIService.GetOrganizations(onGetThreadPostOrganizationsComplete, onSecondIINoneForumError);
}

function onGetThreadPostOrganizationsComplete(organizations) {

    var element = $get(ForumThreadPostElements.DdlOrganizations);
    var elementThreadPostReply = $get(ForumThreadPostReplyElements.DdlThreadPostReplyOrganizations);

    populateSecondIINoneOrganizations(element, organizations);
    populateSecondIINoneOrganizations(elementThreadPostReply, organizations);

    getSecondIINoneForumThreadPostsUsingService();
}

function saveSecondIINoneForumThreadPost() {

    var formValidated = validateForumThreadPostForm();

    if (!formValidated)
        return false;

    showWaitDialog(InformationMessages.ForumThreadPostSave);

    saveForumThreadPost();
}

function toggleThreadPostOrganizationsDropDownListDisplay() {
    var organizationsListItem = $get(ForumThreadPostElements.LiOrganizations);
    if ($(ForumThreadPostElements.JQDdlPrivacy).val() == 2) {
        showContent(organizationsListItem)
    }
    else {
        hideContent(organizationsListItem)
    }
}

function updateThreadPost(id) {
    _selectedThreadPostId = id;

    getThreadPostByIdUsingService();
}

function getThreadPostByIdUsingService() {
    ForumUIService.GetForumThreadPostById(_selectedThreadPostId, onGetThreadPostByIdUsingServiceComplete, onSecondIINoneForumError);
}

function onGetThreadPostByIdUsingServiceComplete(threadPost){
    populateThreadPostFormByPostId(threadPost);
}

function populateThreadPostFormByPostId(threadPost) {
    var message = $get(ForumThreadPostASPElements.TxtMessage);
    var privacy = $get(ForumThreadPostElements.DdlPrivacy);
    var organization = $get(ForumThreadPostElements.DdlOrganizations);

    message.value = threadPost.Message;

    if (threadPost.IsPrivate) {
        setSelectedDropDownListIndex(privacy, 2);

        toggleThreadPostOrganizationsDropDownListDisplay();

        setSelectedDropDownListIndex(organization, threadPost.OrganizationId);
    }
    else {
        setSelectedDropDownListIndex(privacy, 1);
    }

    message.focus();
}

function resetForumThreadPostForm() {

    var message = $get(ForumThreadPostASPElements.TxtMessage);
    var privacy = $get(ForumThreadPostElements.DdlPrivacy);
    var organization = $get(ForumThreadPostElements.DdlOrganizations);

    message.value = '';

    privacy.options[0].selected = true;
    organization.options[0].selected = true;

    var organizationsListItem = $get(ForumThreadPostElements.LiOrganizations);
    hideContent(organizationsListItem)
}

function validateForumThreadPostForm() {

    var message = $(ForumThreadPostASPElements.JQTxtMessage);
    var privacy = $(ForumThreadPostElements.JQDdlPrivacy);
  
    _errorManager.ClearErrors();

    var errorCount = 0;

    if (message.val().trim() == "") {
        _errorManager.AddErrorMessage(message, String.format(ErrorMessages.RequiredField, ForumThreadPostFieldNames.Message));
        errorCount++;
    }
    
    if (privacy.val() == 0) {
        _errorManager.AddErrorMessage(privacy, String.format(ErrorMessages.RequiredField, ForumThreadPostFieldNames.Privacy));
        errorCount++;
    }
    
    if (errorCount > 0) {
        _errorManager.ShowErrors();
        return false;
    }
    else {
        return true;
    }
}

function saveForumThreadPost() {
    var message = getElementText($get(ForumThreadPostASPElements.TxtMessage));
    var isPrivate = getSelectedValue($get(ForumThreadPostElements.DdlPrivacy));
    var organizationId = getSelectedValue($get(ForumThreadPostElements.DdlOrganizations));

    var forumThreadPostData = new ForumThreadPostData();
    
    isPrivate = isPrivate-1;

    forumThreadPostData.ThreadTopicId = _threadTopicId;
    forumThreadPostData.Message = message;
    forumThreadPostData.IsPrivate = isPrivate;

    if (organizationId > 0 && isPrivate == 1)
        forumThreadPostData.OrganizationId = organizationId;
        
    if (_selectedThreadPostId > 0)
        forumThreadPostData.Id = _selectedThreadPostId;

    saveSecondIINoneForumThreadPostUsingService(forumThreadPostData);
}

function saveSecondIINoneForumThreadPostUsingService(forumThreadPostData) {
    ForumUIService.SaveForumThreadPost(forumThreadPostData, onSaveSecondIINoneThreadPostsComplete, onSecondIINoneForumError);
}

function onSaveSecondIINoneThreadPostsComplete() {
    if (_isUserAuthenticated)
        resetForumThreadPostForm();
        
    _selectedThreadPostId = 0;
        
    getSecondIINoneForumThreadPostsUsingService();
}

function getSecondIINoneForumThreadPostsUsingService() {
    
    ForumUIService.GetForumThreadPostsByThreadTopicId(_threadTopicId, _forumPostPageSize, _forumPostPageNumber, onGetSecondIINoneThreadPostsComplete, onSecondIINoneForumError);
}

function onGetSecondIINoneThreadPostsComplete(threadPosts) {
    _threadPosts = threadPosts;

    displayThreadPosts(threadPosts);
}

function displayThreadPosts(threadPosts) {

    var divError = $get(ForumElements.DivSecondIINoneForumError);
    hideContent(divError);

    var threadPostsDiv = $get(ForumElements.DivForum);
    showContent(threadPostsDiv);

    var threadPostsString = ''

    if (threadPosts.length > 0) {
        _isUserAuthenticated = threadPosts[0].IsUserAuthenticated;
        _isOfficer = threadPosts[0].IsOfficer;

        threadPostsString = threadPostsString + '<table cellpadding="0" cellspacing="0" align="center" class="forumContainerTable">';
        threadPostsString = threadPostsString + '<tr>';
        threadPostsString = threadPostsString + '<td class="breadCrumb">';
        threadPostsString = threadPostsString + getBreadCrumb(_threadPostText);
        threadPostsString = threadPostsString + '</td>';
        threadPostsString = threadPostsString + '</tr>';
        threadPostsString = threadPostsString + '<tr class="blueBorder">';
        threadPostsString = threadPostsString + '<td>';

        if (_forumPostCount > _forumPostPageSize) {
            threadPostsString = threadPostsString + getThreadPostsPagingMessage();
        }
        else {
            threadPostsString = threadPostsString + String.format(InformationMessages.ThreadPostsPagingSinglePagePostCountMessage, _forumPostCount);
        }
        
        threadPostsString = threadPostsString + '</td>';
        threadPostsString = threadPostsString + '</tr>';
        threadPostsString = threadPostsString + '<tr class="blueBorder">';
        threadPostsString = threadPostsString + '<td class="greyBackground">';

        if (_forumPostCount > _forumPostPageSize) {
            var threadPostPageCount = _forumPostCount / _forumPostPageSize;

            threadPostsString = threadPostsString + '&nbsp;<p class="gridPageNumber">';
            for (var j = 1; j < threadPostPageCount + 1; j++) {
                if (_forumPostPageNumber != j) {
                    threadPostsString = threadPostsString + '<a href="javascript:getRequestedThreadPostPage('
                    threadPostsString = threadPostsString + j;
                    threadPostsString = threadPostsString + ')" >';
                    threadPostsString = threadPostsString + j;
                    threadPostsString = threadPostsString + '</a>&nbsp;';
                }
                else {
                    threadPostsString = threadPostsString + j;
                }
            }
            threadPostsString = threadPostsString + '</p>';
        }
        else {
            threadPostsString = threadPostsString + '<p>&nbsp;</p>';
        }

        threadPostsString = threadPostsString + '<table cellpadding="0" cellspacing="0" align="center" class="forumTable">';
        threadPostsString = threadPostsString + '<tr class="blueBorder">';
        threadPostsString = threadPostsString + '<th>';
        threadPostsString = threadPostsString + '&nbsp;Author&nbsp;';
        threadPostsString = threadPostsString + '</th>';
        threadPostsString = threadPostsString + '<th>';
        threadPostsString = threadPostsString + '&nbsp;Topic: - ' + _threadTopicName + '&nbsp;';
        threadPostsString = threadPostsString + '</th>';
        threadPostsString = threadPostsString + '</tr>';

        for (var threadPostCount = 0; threadPostCount < threadPosts.length; threadPostCount++) {

            threadPostsString = threadPostsString + '<tr>';
            threadPostsString = threadPostsString + '<td class="createdBy">';
            threadPostsString = threadPostsString + '<img class="picture" src="' + threadPosts[threadPostCount].UserPicture + '" />';
            threadPostsString = threadPostsString + '<br /><span class="username">' + threadPosts[threadPostCount].CreatedByUsername + '</span>';
            threadPostsString = threadPostsString + '<br /><span class="office">' + threadPosts[threadPostCount].CreatedByOffice + '</span>';
            threadPostsString = threadPostsString + '<br /><span class="date">&nbsp;on&nbsp;' + threadPosts[threadPostCount].CreatedDate + '</span>';
            threadPostsString = threadPostsString + '</td>';
            threadPostsString = threadPostsString + '<td>';
            
            threadPostsString = threadPostsString + '<div class="forumTableMessage">';
            
            threadPostsString = threadPostsString + threadPosts[threadPostCount].Message.replace(/\n/g, '<br />');

            threadPostsString = threadPostsString + '</div>';

            threadPostsString = threadPostsString + '<div class="forumTableLinks">';

            threadPostsString = threadPostsString + '<a href="#" class="topic" onclick="javascript:getForumThreadPost(' + threadPosts[threadPostCount].Id + ');return false;">';
            threadPostsString = threadPostsString + 'Reply';
            threadPostsString = threadPostsString + '</a>';

            if (_isOfficer || (threadPosts[threadPostCount].CreatedBy == threadPosts[threadPostCount].CurrentUserId)) {
                threadPostsString = threadPostsString + '&nbsp;|&nbsp;';
                threadPostsString = threadPostsString + '<a href="#" class="topic" onclick="javascript:updateThreadPost(' + threadPosts[threadPostCount].Id + ');return false;">';
                threadPostsString = threadPostsString + 'Modify';
                threadPostsString = threadPostsString + '</a>';
                
                threadPostsString = threadPostsString + '&nbsp;|&nbsp;';
                threadPostsString = threadPostsString + '<a href="#" class="topic" onclick="javascript:deleteThreadPost(' + threadPosts[threadPostCount].Id + ');return false;">';
                threadPostsString = threadPostsString + 'Delete';
                threadPostsString = threadPostsString + '</a>';
            }

            threadPostsString = threadPostsString + '</div>';
            
            
            threadPostsString = threadPostsString + '</td>';
            threadPostsString = threadPostsString + '</tr>';
        }

        threadPostsString = threadPostsString + '</td>';
        threadPostsString = threadPostsString + '</tr>';
        threadPostsString = threadPostsString + '</table>';

        threadPostsString = threadPostsString + '<p>&nbsp;</p>';

        threadPostsString = threadPostsString + '</td>';
        threadPostsString = threadPostsString + '</tr>';
        threadPostsString = threadPostsString + '</table>';
    }
    else {
        threadPostsString = '<div class="whiteMessage">There are no posts under this topic.</div>';
        
        threadPostsString = threadPostsString + '<p>&nbsp;</p>';
        threadPostsString = threadPostsString + '<p>&nbsp;</p>';
        threadPostsString = threadPostsString + '<table cellpadding="0" cellspacing="0" align="center" class="forumTable">';
        threadPostsString = threadPostsString + '<tr class="blueBorder">';
        threadPostsString = threadPostsString + '<td>';
        threadPostsString = threadPostsString + getBreadCrumb(_threadPostText);
        threadPostsString = threadPostsString + '</tr>';
        threadPostsString = threadPostsString + '</table>';

    }

    threadPostsDiv.innerHTML = threadPostsString;

    hideWaitDialog();
}

function getRequestedThreadPostPage(pageNumber) {
    _forumPostPageNumber = pageNumber;
    getSecondIINoneForumThreadPostsUsingService();
}

function getThreadPostsPagingMessage() {
    var firstRow = 1
    var lastRow = 1
    var numberOfPostsToList = _forumPostPageSize

    var numberOfPages = (_forumPostCount / _forumPostPageSize);
    var remainingPosts = (_forumPostCount % _forumPostPageSize);
    if (remainingPosts < 10)
        numberOfPages = Math.round(numberOfPages) + 1;
    else
        numberOfPages = Math.round(numberOfPages);

    var maxRowCount = (numberOfPages * _forumPostPageSize);

    firstRow = _forumPostCount - (_forumPostPageSize * (_forumPostPageNumber - 1));
    if (_forumPostPageNumber == numberOfPages) {
        numberOfPostsToList = remainingPosts;
        lastRow = 1;
    }
    else {
        lastRow = firstRow - (_forumPostPageSize) + 1;
    }

    if (firstRow < 0)
        firstRow = 1;

    return String.format(InformationMessages.ThreadPostsPagingPostCountMessage, numberOfPostsToList, lastRow, firstRow);
}
