﻿/************************************************************************  
                                VARS
**************************************************************************/
var _currentPostId = 0;
var _currentPostMessage = '';
var _reText = 'Re...';

/************************************************************************ 
                        Page Element Enums
**************************************************************************/
var ForumThreadPostReplyElements =
{

    DivSecondIINoneThreadPostReplyError: 'divSecondIINoneThreadPostReplyError',
    DlgThreadPostReplyBehavior: 'dlgThreadPostReplyBehavior',
    LiThreadPostReplyOrganizations: 'liThreadPostReplyOrganizations',
    DdlThreadPostReplyPrivacy: 'ddlThreadPostReplyPrivacy',
    DdlThreadPostReplyOrganizations: 'ddlThreadPostReplyOrganizations',

    JQDivSecondIINoneThreadPostReplyError: '#divSecondIINoneThreadPostReplyError',
    JQLiThreadPostReplyOrganizations: '#liThreadPostReplyOrganizations',
    JQDdlThreadPostReplyPrivacy: '#ddlThreadPostReplyPrivacy',
    JQDdlThreadPostReplyOrganizations: '#ddlThreadPostReplyOrganizations'

}

var ForumThreadPostReplyFieldNames =
{
    Message: 'Message',
    Privacy: 'Private?'
}

/************************************************************************ 
                        Functions  
**************************************************************************/

function toggleThreadPostReplyOrganizationsDropDownListDisplay() {
    var organizationsListItem = $get(ForumThreadPostReplyElements.LiThreadPostReplyOrganizations);
    if ($(ForumThreadPostReplyElements.JQDdlThreadPostReplyPrivacy).val() == 2) {
        showContent(organizationsListItem)
    }
    else {
        hideContent(organizationsListItem)
    }
}

function resetForumThreadPostReplyForm() {

    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 saveSecondIINoneForumThreadPostReply() {

    if (!validateForumThreadPostReplyForm()) {
        return false;
    }
    else {
        showWaitDialog(InformationMessages.ForumThreadPostSave);
        
        var message = getElementText($get(ForumThreadPostASPElements.TxtThreadPostReplyMessage));
        var isPrivate = getSelectedValue($get(ForumThreadPostReplyElements.DdlThreadPostReplyPrivacy));
        var organizationId = getSelectedValue($get(ForumThreadPostReplyElements.DdlThreadPostReplyOrganizations));

        var forumThreadPostData = new ForumThreadPostData();

        isPrivate = isPrivate - 1;

        forumThreadPostData.ThreadTopicId = _threadTopicId;
        forumThreadPostData.Message = message;
        forumThreadPostData.IsPrivate = isPrivate;
        forumThreadPostData.ThreadPostId = _currentPostId;
        
        if (organizationId > 0 && isPrivate == 1)
            forumThreadPostData.OrganizationId = organizationId;

        saveSecondIINoneForumThreadPostUsingService(forumThreadPostData);
    }

    closeThreadPostReplyDialog();
}

function validateForumThreadPostReplyForm() {

    var _threadPostReplyErrorManager = new ErrorManagerJQ($(ForumThreadPostReplyElements.JQDivSecondIINoneThreadPostReplyError));
    var message = $(ForumThreadPostASPElements.JQTxtThreadPostReplyMessage);
    var privacy = $(ForumThreadPostReplyElements.JQDdlThreadPostReplyPrivacy);

    _threadPostReplyErrorManager.ClearErrors();

    var errorCount = 0;

    if (message.val().trim() == "") {
        _threadPostReplyErrorManager.AddErrorMessage(message, String.format(ErrorMessages.RequiredField, ForumThreadPostReplyFieldNames.Message));
        errorCount++;
    }

    if (privacy.val() == 0) {
        _threadPostReplyErrorManager.AddErrorMessage(privacy, String.format(ErrorMessages.RequiredField, ForumThreadPostReplyFieldNames.Privacy));
        errorCount++;
    }

    if (errorCount > 0) {
        _threadPostReplyErrorManager.ShowErrors();
        return false;
    }
    else {
        return true;
    }
}

function getForumThreadPost(forumThreadPostId) {
    getForumThreadPostByIdUsingService(forumThreadPostId)
}

function getForumThreadPostByIdUsingService(forumThreadPostId) {
    ForumUIService.GetForumThreadPostById(forumThreadPostId, onGetForumThreadPostByIdUsingServiceComplete, onSecondIINoneForumError);
}

function onGetForumThreadPostByIdUsingServiceComplete(threadPost) {

    _currentPostMessage = threadPost.Message;
    _currentPostId = threadPost.Id;

    setElementText($get(ForumThreadPostASPElements.TxtThreadPostReplyMessage), _reText + _currentPostMessage);
    
    openThreadPostReplyDialog();
}

function closeThreadPostReplyDialog() {
    $find(ForumThreadPostReplyElements.DlgThreadPostReplyBehavior).hide();
    _isThreadPostReplyDialogVisible = false;
}

function openThreadPostReplyDialog() {
    showModalPopup($find(ForumThreadPostReplyElements.DlgThreadPostReplyBehavior));
    _isThreadPostReplyDialogVisible = true;
}

function onSecondIINoneThreadPostReplyError(result) {
    var problems = new Array();
    problems[problems.length] = result.get_message();
    setErrorInformation($get(ForumThreadPostReplyElements.DivSecondIINoneThreadPostReplyError), problems);

    hideWaitDialog();
}

