﻿var prefixId = "ctl00_ctl00_ctl00_ctl00_a_b_c_e_";
var rteFrame = null;

//used when trying to open edit form when there's already opened form
var tempId = -1;
var tempEditMode = "";

// ShortPost object
//#region
function ShortPost()
{
    this.PostID = -1;
    this.ThreadID = -1;
    this.PostContent = "";
    this.Attachment = "";
    this.IsAnswer = false;
    this.IsProposedAnswer = false;
    this.HasCode = false;
    this.ReplyTo = -1;
    this.DatePosted = "";
    this.DateModified = "";
    this.DateModifiedInfo = "";
    this.Status = false;
    this.PostInfo = "";

    this.UserID = -1;
    this.UserName = "";
    this.UserLink = "";
    this.UserRole = "";
    this.UserAvatar = "";
    this.UserSignature = "";

    this.ThreadType = "";
};
//#endregion

// rating
//#region
function RateThis(element)
{
    if (event.srcElement && HasLogin(event.srcElement))
    {
        var parentElement = element.parentElement;
        var className = "ratedstar"
        var rate = 0;

        for (var i = 0; i < parentElement.children.length; i++)
        {
            parentElement.children[i].className = className;

            if (parentElement.children[i] == element)
            {
                rate = i + 1;
                className = "unratedstar";
            }
        }

        var permalink = document.getElementById("permalink");
        var forum = document.getElementById("forum");

        var fpb = ISGetObject("WebFlyPostBackManager1");
        if (fpb != null)
            fpb.RateThis(forum.value, permalink.value, rate);
    }
}
//#endregion

// toggle edit
//#region
function OnEditClicked(id)
{
    if (event.srcElement && NotBusy(event.srcElement) && ConfirmCancel(event.srcElement))
    {
        CloseEditorFrame(); //close previous rte frame -> used when isdirty is false
        EditClicked(id);
    }
    else
    {
        tempId = id;
        tempEditMode = "Edit";
    }
}

function EditClicked(id)
{
    NewPost.ResetValue();
    NewPost.Id = id;
    NewPost.ThreadId = parseInt(document.getElementById("threadID").value);
    NewPost.EditMode = "Edit"; // animation mode: fade in/out

    DisplayEditDiv();
    ResizeEditor();

    var content = document.getElementById(GetId("content", id));
    SetEditorValue(content.innerHTML, false);

    FocusOnEditor();

    NewPost.HasExitEditMode = false;
}

function DisplayEditDiv()
{
    var id = NewPost.Id;
    var div = document.getElementById("edit" + id);

    div.appendChild(rteFrame);
    div.parentElement.style.display = "";

    var att = document.getElementById("attContent" + id);
    if (att != null)
        FillEditAttachment(att);

    ToggleEdit(true, id);
    RegisterIFrameBehavior();

    if (NeedsScrolling(div, editorFrameHeight))
        SmoothScrollToAnchor(rteFrame, (editorFrameHeight + 10 - document.documentElement.offsetHeight));
    else
    {
        if (!IS.ie)
            AnimateEditor(true);
    }
}

function ToggleEdit(isDisplay, id)
{
    if (!isDisplay)
    {
        var div = rteFrame.parentElement;
        id = div.id.substring(4);

        ToggleElement(document.getElementById(GetId("content", id)), true);
        ToggleElement(document.getElementById("edit" + id), false);
        ToggleElement(document.getElementById("action" + id), true);

        var sig = document.getElementById(GetId("sig", id));
        if (sig != null)
            ToggleElement(sig, true);

        var att = document.getElementById(GetId("att", id));
        var attContent = document.getElementById("attContent" + id);
        ToggleElement(att, GetAttachmentAttribute(attContent) != "");

        ToggleElement(document.getElementById(prefixId + "ReplyUserInfo"), true);
    }
    else
    {
        ToggleElement(document.getElementById(GetId("content", id)), false);
        ToggleElement(document.getElementById("edit" + id), true);
        ToggleElement(document.getElementById("action" + id), false);
        ToggleElement(document.getElementById(prefixId + "ReplyUserInfo"), false); //reply user info in rte frame - not needed in edit mode

        var sig = document.getElementById(GetId("sig", id));
        if (sig != null)
            ToggleElement(sig, false);

        ToggleElement(document.getElementById(GetId("att", id)), false);

        var attContent = document.getElementById("attContent" + id);
        if (GetAttachmentAttribute(attContent) != "")
        {
            var el = document.getElementById("replyAttachment");
            ToggleElement(el, true);
        }

        var rte = ISGetObject("te1");
        rte.SetHeaderHeightCache();
    }
}
//#endregion

// toggle reply
//#region
function OnReplyClicked(id, isQuote)
{
    if (event.srcElement && HasLogin(event.srcElement) && NotBusy(event.srcElement) && ConfirmCancel(event.srcElement))
    {
        CloseEditorFrame(); //close previous rte frame -> used when isdirty is false
        ReplyClicked(id, isQuote);
    }
    else
    {
        tempId = id;

        if (isQuote)  // animation mode: slide down/up
            tempEditMode = "Quote";
        else
            tempEditMode = "Reply";
    }
}

function ReplyClicked(id, isQuote)
{
    NewPost.ResetValue();
    NewPost.Id = id;
    NewPost.ThreadId = parseInt(document.getElementById("threadID").value);

    if (isQuote)  // animation mode: slide down/up
        NewPost.EditMode = "Quote";
    else
        NewPost.EditMode = "Reply";

    DisplayReplyDiv();
    ResizeEditor();

    if (isQuote)
    {
        var content = document.getElementById(GetId("content", id));
        SetEditorValue(content.innerHTML, true);
    }

    FocusOnEditor();

    NewPost.HasExitEditMode = false;
}

function DisplayReplyDiv()
{
    var id = NewPost.Id;
    var div = document.getElementById("reply" + id);

    div.appendChild(rteFrame);
    div.parentElement.style.display = "";

    GetReplyUserInfo();
    ToggleReply(true, id);
    RegisterIFrameBehavior();

    if (NeedsScrolling(div, editorFrameHeight))
        SmoothScrollToAnchor(rteFrame, (editorFrameHeight + 10 - document.documentElement.offsetHeight));
    else
    {
        if (!IS.moz)
            AnimateEditor(true);
    }
}

function ToggleReply(isDisplay, id)
{
    if (!isDisplay)
    {
        var div = rteFrame.parentElement;
        id = div.id.substring(5);

        ToggleElement(document.getElementById("replyFrame" + id), false);
        ToggleElement(document.getElementById("action" + id), true);

        var att = document.getElementById(GetId("att", id));
        var attContent = document.getElementById("attContent" + id);
        if (GetAttachmentAttribute(attContent) != "")
            ToggleElement(att, true);
    }
    else
    {
        ToggleElement(document.getElementById(GetId("att", id)), false);
        ToggleElement(document.getElementById("replyFrame" + id), true);
        ToggleElement(document.getElementById("action" + id), false);

        var rte = ISGetObject("te1");
        rte.SetHeaderHeightCache();
    }
}

function GetReplyUserInfo()
{
    var el = document.getElementById("replyInfo");

    var post = el;
    if (NewPost.Id != 0)
        post = document.getElementById("post" + NewPost.Id);

    el.innerHTML = "In response to " + post.attributes["userName"].value + " (Draft)";
}
//#endregion

// animation helper
//#region
function NeedsScrolling(div, height)
{
    var divTop = ISPosLib.getTop(div) - document.documentElement.scrollTop;
    var bodyHeight = document.body.offsetHeight;

    if ((bodyHeight - divTop) < height)
        return true;

    return false;
}

function ResetLayout()
{
    if (NewPost.EditMode == "Reply" || NewPost.EditMode == "Quote")
    {
        var parent = rteFrame.parentElement;

        rteFrame.style.position = "static";
        rteFrame.style.top = "0px";
        parent.style.position = "static";
        parent.style.overflow = "";
        parent.style.height = "";
        rteFrame.style.height = "";
        rteFrame.style.opacity = "";
    }
    else
    {
        rteFrame.style.filter = "";
        rteFrame.style.khtmlopacity = "";
        rteFrame.style.mozopacity = "";
        rteFrame.style.opacity = "";
    }
}

function OnCompleted(flag) // flag: true(slidedown/fadein); false (slideup/fadeout)
{
    ResetLayout();

    if (!flag)
    {
        if (NewPost.EditMode == "Reply" || NewPost.EditMode == "Quote")
            ToggleReply(false);
        else
        {
            ToggleEdit(false);

            if (NewPost.SubmitMode == "Submit")
            {
                NewPost.SubmitMode = "";
                AnimateColorAfterSubmit(document.getElementById("post" + NewPost.Id));
            }
        }
    }
}

function AnimateEditor(flag) // flag: true(slidedown/fadein); false (slideup/fadeout)
{
    HideCallOut();

    if (NewPost.EditMode == "Reply" || NewPost.EditMode == "Quote")
    {
        var div = rteFrame.parentElement;

        div.style.overflow = "hidden";
        div.style.position = "relative";
        rteFrame.style.position = "relative";

        if (flag)
        {
            div.style.height = "0px";
            rteFrame.style.top = (editorFrameHeight * -1) + "px";

            Animation.Do(rteFrame, "SlideDown", null, function() { OnCompleted(flag); }, { "Style": IS.ie ? "Easing" : "Accelerated", "Step": IS.ie ? 5 : 1 });
        }
        else
            Animation.Do(rteFrame, "SlideUp", null, function() { OnCompleted(flag); }, { "Style": IS.ie ? "Easing" : "Accelerated", "Step": IS.ie ? 5 : 1 });
    }
    else
    {
        if (flag)
            Animation.Do(rteFrame, "FadeIn", (IS.ie ? 7 : 3), function() { OnCompleted(flag); });
        else
            Animation.Do(rteFrame, "FadeOut", (IS.ie ? 50 : 5), function() { OnCompleted(flag); });
    }
}

function AnimateColorAfterSubmit(el)
{
    Animation.Do(el, "AnimateColor", null, function() { el.style.backgroundColor = "transparent"; }, { "FromColor": "#fdfad5", "ToColor": "#ffffff", "MaxStep": 100, "Speed": "VerySlow" });
}
//#endregion

// flypostback related
//#region
function IsEditorEmpty()
{
    var rte = ISGetObject("te1");
    
    if (rte.IsEditorEmpty())
    { 
        var btn = ISGetObject("wbSubmit");
        CreateForumCallOut("EditorEmpty", btn.FrameObj);
        
        return true;
    }
    
    return false;
}

function Submit()
{
    if (!IsEditorEmpty())
    {
        var rte = ISGetObject("te1");
        
        var textarea = document.getElementById("code_t");       
        if (NewPost.IsInsertCodeDirty && textarea.value != "")
        {
            if (!rte.TaskPaneShown)
                rte.ShowTaskPane("Insert Code");
            
            var btn = ISGetObject("wbSubmit");
            CreateForumCallOut("InsertCode", btn.FrameObj);
        }
        else
        {
            ToggleRTEFrameLayer(true);
            
            var uploader = rte.WebFileUploader;
            if (uploader != null && GetNonUploadedFilesCount() > 0)
            {
                NewPost.IsUploadingFilesOnSubmit = true;
                uploader.UploadFiles();
            }
            else
                SubmitAction();
        }
    }
}

function SubmitAction()
{
    var status = document.getElementById("statusbox");
    status.style.display = "none";

    var fpb = ISGetObject("WebFlyPostBackManager1");
    var rte = ISGetObject("te1");
    var attachments = NewPost.GetAttachmentStringForSubmit(false);

    rte.RemoveSpellChecker();
    rte.SetStatus("Saving...");
    NewPost.IsBusy = true;
    NewPost.IsInsertCodeDirty = false;

    HideCallOut();
    NewPost.SubmitMode = "Submit";

    switch (NewPost.EditMode)
    {
        case "Edit":
            var isThread = (NewPost.Id == 0);
            
            fpb.Edit((isThread) ? NewPost.ThreadId : NewPost.Id, rte.GetValue(), attachments,
                NewPost.HasCode, (NewPost.Id == 0));
            break;

        case "Reply":
        case "Quote":
            var replyTo = (NewPost.Id == 0) ? "" : NewPost.Id.toString();

            fpb.Reply(document.getElementById("forum").value, document.getElementById("permalink").value,
                replyTo, rte.GetValue(), attachments,
                NewPost.HasCode);
            break;
    }
}

function DoDiscardCode(isDiscard)
{
    if (isDiscard)
        SubmitAction();
        
    HideAllForumCallOut();
}

function WebFlyPostBackManager1_OnReply(returnValue)
{
    ToggleReply(false);

    var post = returnValue;
    if (post != null)
    {
        AddNewPost(post);

        var el = document.getElementById("post" + post.PostID);
        if (el != null)
        {
            NewPost.Id = post.PostID; //set the newpost id to the new id (reply post id)

            var height = el.offsetHeight;
            if (NeedsScrolling(el, height))
            {
                var onCompleted = function()
                {
                    var el = document.getElementById("post" + NewPost.Id);
                    if (el != null)
                        AnimateColorAfterSubmit(el);
                };

                SmoothScrollToAnchor(el, 0, onCompleted); //TO DO: figure out the second parameter value so the post will be at the bottom of the page
                //SmoothScrollToAnchor(el, (height - (document.documentElement.offsetHeight - document.documentElement.scrollTop)));
            }
            else
                AnimateColorAfterSubmit(el);
        }
    }

    NewPost.HasExitEditMode = true;
    NewPost.IsDirty = false;

    ResetBusyStatus();

    var rte = ISGetObject("te1");
    rte.SetStatus(rte.TextSettings.ReadyText);
}

function WebFlyPostBackManager1_OnEdit(returnValue)
{
    var post = returnValue;
    if (post != null)
    {
        var el = document.getElementById(GetId("content", post.PostID));
        if (el != null)
        {
            el.innerHTML = post.PostContent;

            //if the edited post has been marked as answer, modify the content of the answer post as well
            if (post.IsAnswer)
            {
                var ans = document.getElementById("ansContent" + post.PostID);
                ans.innerHTML = post.PostContent;
            }
        }

        SetAttachmentAttribute(document.getElementById("attContent" + post.PostID), post.Attachment);
        WriteAttachments(post);
        AnimateEditor(false);

        NewPost.HasExitEditMode = true;
        NewPost.IsDirty = false;

        ResetBusyStatus();
    }
    else
        ExitEditMode(); //if the return value is null, cancel the changes and reset the ui layout    

    var rte = ISGetObject("te1");
    rte.SetStatus(rte.TextSettings.ReadyText);
}

function WebFlyPostBackManager1_OnMarkAsAnswer(returnValue)
{
    var post = returnValue;
    if (post != null)
    {
        AddNewAnswerPost(post);
        AdjustAnswerIndicator(true, post.PostID);
        RemoveMarkAsAnswerAction(post.PostID);

        var div = document.getElementById("answerPost" + post.PostID);
        if (div != null)
            SmoothScrollToAnchor(div, 10);

        AnimateColorAfterSubmit(document.getElementById("post" + post.PostID));
    }
}

function WebFlyPostBackManager1_OnProposeAsAnswer(returnValue)
{
    if (returnValue)
    {
        AdjustAnswerIndicator(false);
        RemoveProposeAsAnswerAction(NewPost.Id);

        var div = document.getElementById("post" + NewPost.Id);
        if (div != null)
            AnimateColorAfterSubmit(div);
    }
}

function WebFlyPostBackManager1_OnAlertMe(returnValue)
{
    if (returnValue)
    {
        var el = document.getElementById(prefixId + "ThreadAlert");
        el.innerHTML = "You are subscribed to this post.";
        el.style.cursor = "default";
    }
}

function WebFlyPostBackManager1_OnMarkFavorite(returnValue)
{
    if (returnValue)
    {
        var el = document.getElementById(prefixId + "ThreadFavorite");
        el.innerHTML = "";
        el.style.display = "none";
    }
}

function WebFlyPostBackManager1_OnError(controlId, methodName, errorObject)
{
    var status = document.getElementById("statusbox");

    var message = ValidationMessage("error", "An error has occured when trying to post your reply. Please contact our support at technical@intersoftpt.com for further assistance.")
    status.innerHTML = message;
    status.style.display = "";

    ToggleRTEFrameLayer(false);

    var rte = ISGetObject("te1");
    rte.SetStatus(rte.TextSettings.ReadyText);
}
//#endregion

// answer post
//#region

// general
//#region
function DoAnswerAction(value, markAsAnswer)
{
    if (value == "Yes")
    {
        var fpb = ISGetObject("WebFlyPostBackManager1");

        if (markAsAnswer)
            fpb.MarkAsAnswer(NewPost.Id);
        else
            fpb.ProposeAsAnswer(NewPost.Id);
    }
            
    HideAllForumCallOut();
}

function AdjustAnswerIndicator(isMark, id)
{
    if (isMark)
    {
        RemoveProposeAsAnswerIndicator(id);
        AddMarkAsAnswerIndicator();
    }
    else
        AddProposeAsAnswerIndicator();
}

function ToggleThreadAnswers(isDisplay)
{
    var el = document.getElementById("threadAnswers");
    var separator = document.getElementById("answerSeparator");

    if (isDisplay)
    {
        ToggleElement(el, true);
        separator.style.display = "";
    }
    else
    {
        ToggleElement(el, false);
        separator.style.display = "none";
    }
}
//#endregion

// mark as answer
//#region
function OnMarkClicked(id)
{
    NewPost.ResetValue();
    NewPost.Id = id;

    CreateForumCallOut("MarkAsAnswer", document.getElementById("mark" + id));
}

function AddMarkAsAnswerIndicator()
{
    var id = NewPost.Id;

    if (id != -1)
    {
        var div = document.createElement("DIV");
        div.innerHTML = "<img class=\"postmark\" src=\"/WebResources/Images/Community/status_answer.png\"/>";

        var el = document.getElementById("poststatus" + id);
        el.appendChild(div.children[0]);
    }
}

function RemoveMarkAsAnswerAction(id)
{
    var el = document.getElementById("actionMark" + id);
    if (el != null)
        el.parentElement.removeChild(el);
}
//#endregion

// propose as answer
//#region
function OnProposeClicked(id)
{
    NewPost.ResetValue();
    NewPost.Id = id;

    CreateForumCallOut("ProposeAsAnswer", document.getElementById("propose" + id));
}

function AddProposeAsAnswerIndicator()
{
    var id = NewPost.Id;

    if (id != -1)
    {
        var div = document.createElement("DIV");
        div.innerHTML = "<img class=\"postmark\" src=\"/WebResources/Images/Community/status_proposedanswer.png\"/>";

        var el = document.getElementById("poststatus" + id);
        el.appendChild(div.children[0]);
    }
}

function RemoveProposeAsAnswerAction(id)
{
    var el = document.getElementById("actionPropose" + id);
    if (el != null)
        el.parentElement.removeChild(el);
}

function RemoveProposeAsAnswerIndicator(id)
{
    var el = document.getElementById("proposeStatus" + id);
    if (el != null)
        el.parentElement.removeChild(el);
}
//#endregion

// new answer
//#region
function AddNewAnswerPost(post)
{
    ToggleThreadAnswers(true);

    var div = document.createElement("DIV");
    div.innerHTML = CreateAnswerPost(post);

    var postFrame = document.getElementById("answerFrame");
    postFrame.appendChild(div.children[0]);

    WriteAttachments(post);
}

function CreateAnswerPost(post)
{
    var builder = new StringBuilder();

    builder.Append("<div id=\"answerPost" + post.PostID + "\" class=\"post\">");

    // post - status
    builder.Append("<div class=\"poststatus\">");

    switch (post.UserRole)
    {
        case "Administrator":
        case "Moderator":
        case "Support":
            builder.Append("<img class=\"postmark\" src=\"/WebResources/Images/Community/status_staffresponse.png\"/>");
            break;
    }

    builder.Append("</div>");

    // post - user info
    builder.Append("<div>");

    builder.Append("<img class=\"postuserpicture\" src=\"" + post.UserAvatar + "\"/>");

    builder.Append("<div class=\"postuser\">");
    builder.Append("<div>");
    builder.Append("<span class=\"postusername\">" + post.UserLink + "</span><span class=\"postuserrole\">" + post.UserRole + "</span>");
    builder.Append("</div>");
    builder.Append("<div class=\"postinfo\">");
    builder.Append(post.PostInfo);
    builder.Append("</div>");
    builder.Append("</div>");

    builder.Append("<div class=\"clear\"></div>");

    builder.Append("</div>");

    // post - content
    builder.Append("<div id=\"ansContent" + post.PostID + "\" class=\"postcontent\">");
    builder.Append(post.PostContent);
    builder.Append("</div>");

    // post - signature
    if (post.UserSignature != "")
    {
        builder.Append("<div class=\"signature\">");
        builder.Append("<div class=\"signatureseparator\">&nbsp;</div>");
        builder.Append("<div class=\"signaturecontent\">" + post.UserSignature + "</div>");
        builder.Append("</div>");
    }

    // post - attachment box
    builder.Append("<div id=\"answerAtt" + post.PostID + "\" class=\"answerattachment\" ");

    if (post.Attachment == "")
        builder.Append("style=\"display: none;\"");

    builder.Append(">");

    builder.Append("<div class=\"attachmentcontent\">");

    builder.Append("<div>");
    builder.Append("<img src=\"/WebResources/Images/Community/attachment.png\" align=\"top\" />");
    builder.Append("<span id=\"answerAttSummary" + post.PostID + "\" count=\"\" style=\"font-weight: bold;\"></span>");
    builder.Append("</div>");

    builder.Append("<div id=\"answerAttContent" + post.PostID + "\" attachments=\"" + post.Attachment
        + "\" class=\"attachmentitemcontainer\">");
    builder.Append("</div>");

    builder.Append("</div>");
    builder.Append("</div>");

    builder.Append("</div>");

    return builder.ToString();
}
//#endregion

//#endregion

// new post
//#region
function AddNewPost(post)
{
    ToggleThreadPosts(true);

    var div = document.createElement("DIV");
    div.innerHTML = CreateNewPost(post);

    var postFrame = document.getElementById("postFrame");
    postFrame.appendChild(div.children[0]);

    WriteAttachments(post);
}

function CreateNewPost(post)
{
    var builder = new StringBuilder();

    builder.Append("<div class=\"post\" id=\"post" + post.PostID + "\" userName=\"" + post.UserName + "\" dateModified=\"" + post.DateModifiedInfo + "\" style=\"background-color: #fdfad5;\">");

    // post - status
    builder.Append("<div id=\"poststatus" + post.PostID + "\" class=\"poststatus\">");
    switch (post.UserRole)
    {
        case "Administrator":
        case "Moderator":
        case "Support":
            builder.Append("<img class=\"postmark\" src=\"/WebResources/Images/Community/status_staffresponse.png\"/>");
            break;
    }

    if (post.IsAnswer)
    {
        builder.Append("<img class=\"postmark\" src=\"/WebResources/Images/Community/status_answer.png\"/>");
    }
    else if (post.IsProposedAnswer)
    {
        builder.Append("<img id=\"proposeStatus" + post.PostID + "\" class=\"postmark\" src=\"/WebResources/Images/Community/status_proposedanswer.png\"/>");
    }

    builder.Append("</div>");

    // post - user info
    builder.Append("<div>");

    builder.Append("<img class=\"postuserpicture\" src=\"" + post.UserAvatar + "\"/>");

    builder.Append("<div class=\"postuser\">");
    builder.Append("<div>");
    builder.Append("<span class=\"postusername\">" + post.UserLink + "</span><span class=\"postuserrole\">" + post.UserRole + "</span>");
    builder.Append("</div>");
    builder.Append("<div class=\"postinfo\">");
    builder.Append(post.PostInfo);
    builder.Append("</div>");
    builder.Append("</div>");

    builder.Append("<div class=\"clear\"></div>");

    builder.Append("</div>");

    // post - content
    builder.Append("<div id=\"content" + post.PostID + "\" class=\"postcontent\" style=\"margin-bottom: 10px;\">");
    builder.Append(post.PostContent);
    builder.Append("</div>");

    // post - signature
    if (post.UserSignature != "" && post.UserSignature != null)
    {
        builder.Append("<div id=\"sig" + post.PostID + "\" class=\"signature\">");
        builder.Append("<div class=\"signatureseparator\">&nbsp;</div>");
        builder.Append("<div class=\"signaturecontent\">" + post.UserSignature + "</div>");
        builder.Append("</div>");
    }

    // post - attachment box
    builder.Append("<div id=\"att" + post.PostID + "\" class=\"attachment\" ");

    if (post.Attachment == "")
        builder.Append("style=\"display: none;\"");

    builder.Append(">");

    builder.Append("<div class=\"attachmentcontent\">");

    builder.Append("<div>");
    builder.Append("<img src=\"/WebResources/Images/Community/attachment.png\" align=\"top\" />");
    builder.Append("<span id=\"attSummary" + post.PostID + "\" count=\"\" style=\"font-weight: bold;\"></span>");
    builder.Append("</div>");

    builder.Append("<div id=\"attContent" + post.PostID + "\" attachments=\"" + post.Attachment
        + "\" class=\"attachmentitemcontainer\">");
    builder.Append("</div>");

    builder.Append("</div>");

    builder.Append("</div>");

    // post - edit frame
    builder.Append("<div id=\"edit" + post.PostID + "\" style=\"display: none\"></div>");

    // post - action
    builder.Append("<div id=\"action" + post.PostID + "\" class=\"postcontentaction\">");
    builder.Append("<div class=\"postactionleft\"></div>");
    builder.Append("<div class=\"postactionright\">");

    builder.Append("<div class=\"horizontalline\"></div>");

    builder.Append("<div class=\"postaction\">");
    builder.Append("<a class=\"contentlink\" href=\"\" onclick=\"OnEditClicked(" + post.PostID + "); return false;\">");
    builder.Append("<img src=\"/WebResources/Images/Community/icon_edit.png\" />");
    builder.Append("Edit</a>");
    builder.Append("</div>");

    builder.Append("<div class=\"postaction\">");
    builder.Append("<a class=\"contentlink\" href=\"\" onclick=\"OnReplyClicked(" + post.PostID + ", false); return false;\">");
    builder.Append("<img src=\"/WebResources/Images/Community/icon_reply.png\" />");
    builder.Append("Reply</a>");
    builder.Append("</div>");

    builder.Append("<div class=\"postaction\">");
    builder.Append("<a class=\"contentlink\" href=\"\" onclick=\"OnReplyClicked(" + post.PostID + ", true); return false;\">");
    builder.Append("<img src=\"/WebResources/Images/Community/icon_quote.png\" />");
    builder.Append("Quote</a>");
    builder.Append("</div>");

    if (post.ThreadType == "Question")
    {
        if (!post.IsProposedAnswer && !post.IsAnswer)
        {
            builder.Append("<div id=\"actionPropose" + post.PostID + "\" class=\"postaction\">");
            builder.Append("<span id=\"propose" + post.PostID + "\"><a class=\"contentlink\" href=\"\" onclick=\"OnProposeClicked(" + post.PostID + "); return false;\">");
            builder.Append("<img src=\"/WebResources/Images/Community/icon_proposeasanswer.png\" />");
            builder.Append("Propose as answer</a></span>");
            builder.Append("</div>");
        }

        var isCreator = document.getElementById("isCreator").value.bool();
        if (isCreator || post.UserRole == "Administrator" || post.UserRole == "Support" || post.UserRole == "Moderator")
        {
            if (!post.IsAnswer)
            {
                builder.Append("<div id=\"actionMark" + post.PostID + "\" class=\"postaction\">");
                builder.Append("<span id=\"mark" + post.PostID + "\"><a class=\"contentlink\" href=\"\" onclick=\"OnMarkClicked(" + post.PostID + "); return false;\">");
                builder.Append("<img src=\"/WebResources/Images/Community/icon_markasanswer.png\" />");
                builder.Append("Mark as answer</a></span>");
                builder.Append("</div>");
            }
        }
    }

    builder.Append("</div>");

    builder.Append("</div>");

    // post - reply frame
    builder.Append("<div id=\"replyFrame" + post.PostID + "\" style=\"display: none\">");
    builder.Append("<div class=\"horizontalline\" style=\"margin-bottom: 10px;\"></div>");
    builder.Append("<div id=\"reply" + post.PostID + "\"></div>");
    builder.Append("</div>");

    builder.Append("</div>");

    return builder.ToString();
}
//#endregion

// cancel
//#region
function Cancel()
{
    var status = document.getElementById("statusbox");
    status.style.display = "none";

    var wb = ISGetObject("wbCancel");
    if (NewPost.IsDirty)
        CreateForumCallOut("Cancel", wb.FrameObj);
    else
        ExitEditMode();

}

function ExitEditMode(usingAnimation)
{
    if (typeof (usingAnimation) == "undefined")
        usingAnimation = true;

    HideCallOut();

    if (!NewPost.HasExitEditMode && NewPost.IsDirty)
    {
        var att = NewPost.GetAttachmentStringForSubmit(true);

        if (att != "")
        {
            var fpb = ISGetObject("WebFlyPostBackManager1");
            fpb.DeleteAddedAttachments(att);
        }
    }

    switch (NewPost.EditMode)
    {
        case "Edit":
            if (usingAnimation)
                AnimateEditor(false);
            else
                ToggleEdit(false);

            break;

        case "Reply":
        case "Quote":
            if (IS.moz || !usingAnimation)
                ToggleReply(false);
            else
                AnimateEditor(false);

            break;
    }

    NewPost.HasExitEditMode = true;
    NewPost.IsDirty = false;

    ResetBusyStatus();
}

function ConfirmCancel(el)
{
    if (!NewPost.HasExitEditMode)
    {
        if (NewPost.IsDirty)
        {
            CreateForumCallOut("CancelEdit", el);
            return false;
        }

        return true;
    }

    return true;
}

function DiscardChangesAction()
{
    CloseEditorFrame();

    switch (tempEditMode)
    {
        case "Reply":
        case "Quote":
            ReplyClicked(tempId, tempEditMode == "Quote");
            break;

        case "Edit":
            EditClicked(tempId);
            break;
    }

    tempId = -1;
    tempEditMode = "";
}

function CloseEditorFrame()
{
    switch (NewPost.EditMode)
    {
        case "Reply":
        case "Quote":
            ToggleReply(false);
            break;

        case "Edit":
            ToggleEdit(false);
            break;
    }
}

function DoCancelEditAction(value)
{
    HideCallOut();

    switch (value)
    {
        case "Yes":
            DiscardChangesAction();
            break;

        case "No":
            SmoothScrollToAnchor(rteFrame, (editorFrameHeight + 50 - document.documentElement.offsetHeight));
            break;
    }
}

function DoCancelAction(value)
{
    HideCallOut();

    if (value == "Yes")
        ExitEditMode();
}
//#endregion

// get id
//#region

function GetId(type, id)
{
    var tempId = type + id.toString();
    if (id == 0)
        tempId = prefixId + tempId;

    return tempId;
}
//#endregion

// helper
//#region
function Load()
{
    rteFrame = document.getElementById("rteFrame");
    rteHeight = parseInt(document.getElementById("editorFrameHeight").value);
    SetEditorFrameHeight();

    window.setTimeout(function() { ToggleContainers(); }, 100);

    // set the style for prev and next images
    if (IS.ie && IS.GetIEVersion() == "7")
    {
        var img = document.getElementById(prefixId + "previmage");
        if (img != null)
        {
            img.style.position = "static";
            img.align = "absMiddle";
        }

        img = document.getElementById(prefixId + "nextimage");
        if (img != null)
        {
            img.style.position = "static";
            img.align = "absMiddle";
        }
    }
}

function ToggleContainers()
{
    var hasAnswers = document.getElementById("hasAnswers");
    var hasPosts = document.getElementById("hasPosts");

    if (hasAnswers != null)
    {
        //show answers container when there is at least one answer    
        ToggleThreadAnswers(hasAnswers.value.bool());
    }

    if (hasPosts != null)
    {
        //show posts container when there is at least one post
        ToggleThreadPosts(hasPosts.value.bool());
    }

    var attContent = document.getElementById("attContent" + 0);
    if (GetAttachmentAttribute(attContent) == "")
        ToggleElement(document.getElementById(GetId("att", 0)), false);
}

function ToggleThreadPosts(isDisplay)
{
    var el = document.getElementById("threadPosts");
    var separator = document.getElementById("postSeparator");

    if (isDisplay)
    {
        ToggleElement(el, true);
        separator.style.display = "";
    }
    else
    {
        ToggleElement(el, false);
        separator.style.display = "none";
    }
}

function CreateNewThread(isQuestion)
{
    if (event.srcElement && HasLogin(event.srcElement))
        return true;

    return false;
}

function AlertMe(el)
{
    if (HasLogin(el) && document.getElementById("threadID") != null)
    {
        var fpb = ISGetObject("WebFlyPostBackManager1");

        if (fpb == null)
            window.setTimeout(function() { AlertMe(); }, 100);
        else
            fpb.AlertMe(parseInt(document.getElementById("threadID").value));

        ToggleElement(document.getElementById("alertIndicator"), true);
    }
}

function MarkFavorite(el)
{
    if (HasLogin(el) && document.getElementById("threadID") != null)
    {
        var fpb = ISGetObject("WebFlyPostBackManager1");

        if (fpb == null)
            window.setTimeout(function() { MarkFavorite(); }, 100);
        else
            fpb.MarkFavorite(parseInt(document.getElementById("threadID").value));

        ToggleElement(document.getElementById("favIndicator"), true);
    }
}

function ScrollToPost(id)
{
    var el = document.getElementById("post" + id);
    SmoothScrollToAnchor(el, -10);
}

function NotBusy(el)
{
    if (NewPost.IsBusy)
    {
        CreateForumCallOut("BusyMode", el);
        return false;
    }

    return true;
}

function ResetBusyStatus()
{
    NewPost.IsBusy = false;
    HideAllForumCallOut(false);
}
//#endregion
