﻿// TODO:Save post of topic
// Global Var
var gpbb_id = 0, gallow = true, ggrp_id = 0;
$(document).ready(function() {
    setCookie('focusTab', 'forums');
    setCookie('focusSub', 'forums');
    header.update();

    $("#btnShare").click(function() {
        onPostTopic();
    });

    $('.forumTopic ul li').each(function() {
        $(this).click(function() {
            $('.forumTopic ul li').attr('class', '');
            $(this).attr('class', 'tselected');
        });
    });

    if ($('#txtTopicMessage').length > 0) { $('#txtTopicMessage').autoResize(); }

    $('#forum_dialog').append(getSendPrvMsgDialog());
    initSendPrvMsgDialog();
    initDeleteForum();
    initDeleteCommentPublicBoard();
    if ($('#forumResult .friendListPic a img').attr('title') != '') {
        initBaseThumnailToolTip("#forumResult .friendListPic a img");
    }

    $("#btnDelTopicYes").click(function() {
        onDeleteForumTopic(gpbb_id);
    });

    if (cateTypeCd != '') {
        if (forumId.trim() != "") {
            //if (cateTypeCd != "GEN")
            onShowTopic(cateTypeCd, cateFull, forumId, mid);
            //else
            //onScrollToTopic("#yomShared" + forumId);
        }
        else {
            if (cateTypeCd != "GEN") {
                $('.forumTopic ul li').attr('class', '');
                $("#Category" + cateTypeCd).attr('class', 'tselected');
                onShowByCategory(cateTypeCd, cateFull, '', 0, '0');
            }
        }

    }
    if (ceo_msg.toLowerCase() == "true") { viewCEOMessage(); }    

    // TODO: Send Private Comment
    $('#btnMsgSend').click(function() {
        onCloseSendPrvMsgDialog();
        $(this).oneTime(100, 'timer_send_prv', function() {
            var result = sendMessage(0, $('#ms_subject').val(), $('#ms_message').val(), '', $('#ms_to_mbr_id').attr('accesskey'));
            $('#msgResult').html(result);
            $('#msgResult').show();
        });
    });

    $("#showMoreTpc").click(function() {
        onShowMoreRecentTopicAndReplies("TPC");
    });

    $("#showMoreRep").click(function() {
        onShowMoreRecentTopicAndReplies("REP");
    });

    $("#showMoreHot").click(function() {
        onShowMoreRecentTopicAndReplies("HOT");
    });
    if ($('#imgPoll').length > 0) { getPollBox('#imgPoll', 2); }
});
function viewCEOMessage() {
    var ceoContent = "";
    if ($('#currentLang').val() == 'en-US')
    { ceoContent = $('#ceoen').html(); }
    else { ceoContent = $('#ceokh').html(); }
    
    $('#forumCategoryTitle h3').text('Welcome Message from AngkorOne CEO');
    if ($('.forumComposer').length > 0) {$('.forumComposer').hide(); }
    $('#viewAllTopicLink').html("<a onclick=\"onShowByCategory('GEN', 'General', '', 0, '0');\">View General Topics</a>");
    $('#viewAllTopicLink').show();
    $('#forumFilterView strong').hide();
    
    $('#forumResult').html(ceoContent);
}
function onViewCEOMessage(lang) {
    var ceoContent = "";
    ceoContent = $('#ceo' + lang).html();
    $('#forumResult').html(ceoContent);
}


function onSuccess() { window.location.reload(); }
function onMainSuccess() { window.location.reload(); }
function onPostTopic() {
    if ($("#yomShared0").length > 0)
        $("#yomShared0").remove();
    var title = StripHTMLAndTrim($("#txtTopicTitle").val());
    $("#share_title").html(title);
    var message = "";
    if ($("#txtTopicMessage").val().indexOf("<object") > -1)
        message = $("#txtTopicMessage").val();
    else
        message = StripHTMLAndTrim($("#txtTopicMessage").val());
    if (!$.browser.msie) {
        $('#share_tmp').html(message);
    }
    var str = "";
    var i = 0;
    var strRandomId = randomString();
    $('#share_tmp object').each(function() {
        var embed = $(this).find('embed').attr("src");
        str = "<a onclick=\"onShowEmbedMedia('" + strRandomId + "','" + embed + "', " + i + ");\">View Embedded Media</a>";
        $(str).insertAfter('#share_tmp object:eq(' + i + ')');
        i++;
    });
    $('#share_tmp object').remove();
    if (i > 0)
        $('#share_tmp').append('<span id=\"' + strRandomId + '\" style=\"display:none;\"></span>');

    $("#share_title").encHTML();
    title = $("#share_title").html();

    if (!$.browser.msie) {
        $('#share_tmp').encHTML();
        message = $('#share_tmp').html();
    }
    var category = $("#hdCat").val();
    if (category != "AOP" && category != "LIF") {
        if (title.trim() != "" && message.trim() != "") {
            $("#postLoading").show();
            $("#btnShare").attr("disabled", "disabled");
            $("#btnShare").attr("class", "buttonSGray leftButton");
            clearFormPostTopic();
            var url = server_root + "/Processor/GeneralProcessPart7.aspx";
            $.ajax({
                url: url,
                async: true,
                cache: false,
                type: "POST",
                dataType: "html",
                data: {
                    proc_id: 30,
                    shared_title: title,
                    shared_tx: message,
                    shared_cat: category
                },
                success: function(responseText) {
                    $("#postLoading").fadeOut(200, function() {
                        $("#btnShare").removeAttr("disabled");
                        $("#btnShare").attr("class", "buttonSBlue leftButton");
                        var pCount = $("#topicCount" + category).text();
                        pCount = parseInt(pCount, 10) + 1;
                        $("#topicCount" + category).text(pCount);
                        var grandTopic = $("#grandTopic").text();
                        if (grandTopic.indexOf(',') > -1)
                            grandTopic = grandTopic.replace(",", "");
                        grandTopic = formatNumber1000CommaStyle(parseInt(grandTopic, 10) + 1);
                        $("#grandTopic").text(grandTopic);

                        var old_text = $("#sharePrepend").html();
                        if ((responseText.indexOf('"after_post_msg"') > -1 && (old_text.indexOf('"after_post_msg"') < 0 || old_text.indexOf('"after_post_msg"') > 500)) || responseText.indexOf('"after_post_msg"') < 0) {
                            if (responseText.indexOf('"after_post_msg"') > -1) {
                                $("#post_msg").html(responseText);
                            } else {
                                $("#sharePrepend").prepend(responseText);
                                $("#post_msg").html('');
                            }
                        }

                        $("#txtTopicTitle").removeClass("error");
                        $("#txtTopicMessage").removeClass("error");
                        //$('#txtTopicMessage').attr('style', '');
                        if ($('#forumResult .friendListPic a img').attr('title') != '') {
                            initBaseThumnailToolTip("#forumResult .friendListPic a img");
                        }
                    });
                }
            });
        }
        else {
            if (title.trim() == "") {
                $("#txtTopicTitle").addClass("error");
                $("#txtTopicTitle").focus();
            }
            if (message.trim() == "")
                $("#txtTopicMessage").addClass("error");
        } 
    }
}

function clearFormPostTopic() {
    $("#txtTopicTitle").val('');
    $("#txtTopicMessage").val('');
}
function onShowPublicReplies(cmn_type_id, own, aop_id, cate_type_cd) {
    showContent(cmn_type_id, aop_id, cate_type_cd);
    //onGetComment(cmn_type_id);
    if (own != "0")
        onRecordVisitor(cmn_type_id, own);
}

function showContent(id, aop_id, cate_type_cd) {
    if ($('#showMsg' + id).is(":visible")) {
        $('#showMsg' + id).slideUp('slow');
        onGetComment(id);
    }
    else {
        if (aop_id == "0" && cate_type_cd != 'AOP' && cate_type_cd != 'LIF')
            $('#showMsg' + id).slideDown('slow', function() { onGetComment(id); });
        else {
            if (cate_type_cd == 'AOP') {
                getFixPollBoxForumArea('#showMsg' + id, aop_id, id, true);
            }
            if (cate_type_cd == 'LIF') {
                getNewsForumBox('#showMsg' + id, aop_id, id, true);
            }
        }
    }
}

function onSetThumbUpAndDown(t_id, t_cd, t_cmn_cd) {
    var url = server_root + "/Processor/GeneralProcessPart4.aspx";
    $.ajax({ url: url, async: true, chache: false, data: { proc_id: 30, t_id: t_id, t_cd: t_cd, t_cmn_cd: t_cmn_cd }, success: function(responseText) {
        if (responseText != '') {
            if (t_cd == "UP") {
                if (responseText.trim().indexOf('-') > 0) {
                    var rId = responseText.trim().substring('0', responseText.indexOf('-') - 1);
                    var subDw = parseInt($('#thumbdown' + rId).text()) - 1;
                    $('#thumbdown' + rId).text(subDw);

                    var addUp = parseInt($('#thumbup' + rId).text()) + 1; $('#thumbup' + rId).text(addUp);
                    if ($('#thumbup' + rId).val() == '0') {
                        $('#viewFriendAgreeClick' + rId).html('');
                        $('#viewFriendAgree' + rId).html('');
                    }
                    else {
                        $('#viewFriendAgreeClick' + rId).html("<img src=\"" + server_root + "/styles/images/thumb_more.gif\" onclick=\"onViewFriendAgree(" + rId + ",'PCM')\" title=\"View friends who liked\" />");
                    }
                }
                else {
                    var addUp = parseInt($('#thumbup' + responseText.trim()).text()) + 1; $('#thumbup' + responseText.trim()).text(addUp);
                    if ($('#thumbup' + responseText.trim()).text() == '0') {
                        $('#viewFriendAgreeClick' + responseText.trim()).html('');
                        $('#viewFriendAgree' + responseText.trim()).html('');
                    }
                    else {
                        $('#viewFriendAgreeClick' + responseText.trim()).html("<img src=\"" + server_root + "/styles/images/thumb_more.gif\" onclick=\"onViewFriendAgree(" + responseText.trim() + ",'PCM')\" title=\"View friends who liked\" />");
                    }
                }
            }
            else {
                if (responseText.trim().indexOf('-') > 0) {
                    var rId = responseText.trim().substring('0', responseText.indexOf('-') - 1);
                    var subUp = parseInt($('#thumbup' + rId).text()) - 1;
                    $('#thumbup' + rId).text(subUp);

                    var addDw = parseInt($('#thumbdown' + rId).text()) + 1;
                    $('#thumbdown' + rId).text(addDw);
                    if ($('#thumbup' + rId).text() == '0') {
                        $('#viewFriendAgreeClick' + rId).html('');
                        $('#viewFriendAgree' + rId).html('');
                    }
                }
                else {
                    var addDw = parseInt($('#thumbdown' + responseText.trim()).text()) + 1;
                    $('#thumbdown' + responseText.trim()).text(addDw);
                }
            }
        }
    }
    });
}

var gcomment_click = 0; var gcheck_id = 0;
function onGetComment(cmn_type_id, idToScroll) {
    var brdShowHide = $('#boardShowHide' + cmn_type_id).html();
    if ($(".comment_editor_del").length > 0) {
        if ($('#commentAdd' + cmn_type_id).length > 0)
            $(".comment_editor_del").remove();
    }
    if (brdShowHide == 0) {
        //if (gcomment_click == 0 || gcheck_id != cmn_type_id) {
        var getComment = getCommentPublicBoardList(cmn_type_id, 'PBB', idToScroll);
        $('#boardComment' + cmn_type_id).html(getComment);
        gcomment_click = 1;
        gcheck_id = cmn_type_id;
        //}
        if ($('#viewFriendAgree' + cmn_type_id).is(':visible')) {
            $('#viewFriendAgree' + cmn_type_id).hide();
            $('#boardComment' + cmn_type_id).show();
            $('#boardShowHide' + cmn_type_id).html(cmn_type_id);
            $('#isShow' + cmn_type_id).val('0');
        }
        else {
            $('#boardComment' + cmn_type_id).show();
            $('#boardShowHide' + cmn_type_id).html(cmn_type_id);
            $('#isShow' + cmn_type_id).val('0');
        }
    }
    else {
        if (!$('#viewFriendAgree' + cmn_type_id).is(':visible')) {
            $('#boardComment' + cmn_type_id).hide();
            $('#boardShowHide' + cmn_type_id).html('0');
            $('#isShow' + cmn_type_id).val(cmn_type_id);
        }
        else {
            $('#viewFriendAgree' + cmn_type_id).hide();
            $('#boardComment' + cmn_type_id).show();
            $('#boardShowHide' + cmn_type_id).html(cmn_type_id);
            $('#isShow' + cmn_type_id).val('0');
        }
    }

    if ($("#commentAdd" + idToScroll).length > 0) {
        onScrollToTopic("#commentAdd" + idToScroll);
    }

    if ($("#boardComment" + cmn_type_id + ' .friendListPic a img').attr('title') != '') {
        initThumnailToolTip("#boardComment" + cmn_type_id);
        if ($("#boardComment" + cmn_type_id + ' .blogTextarea').length > 0) { setTextBoxAutoResize("#boardComment" + cmn_type_id); }
    }
}

function onPrivateComment(to_id, to_name, on_topic) {
    $('#ms_subject').val('');
    $('#ms_message').val('');
    $('#ms_to_mbr_id').attr('accesskey', '');
    $('#ms_to_mbr_id').val('');
    $('.sendMsg_dialogue').dialog('open');
    $('#ms_subject').val('Reply to your topic posted on ' + on_topic + ' forum');
    $('#ms_to_mbr_id').val(to_name);
    $('#ms_to_mbr_id').attr('accesskey', to_id);
    $('#ms_message').focus();
}
var is_aop = "";
function onShowByCategory(category, desc, idToScroll, cmn_type_id, own) {
    $("#postLoading").show();
    $("#forumResult").html('');
    $("#hdCat").val(category);
    $("#forumCategoryTitle").html("<h3>" + desc + "</h3>");
    $('#viewAllTopicLink').hide();
    $('#forumFilterView strong').show();
    if (is_mas == "False") {
        if (category == "AOP") {
            $('.forumComposer').hide();
            is_aop = category;
        }        
        else
            $('.forumComposer').show();
    }
    else
        $('.forumComposer').show();

    if (category == "LIF") {
        $('#forumForm').hide();
        var height = $('#forumForm').height();
        $('#forumSummary').css("height", height + "px");
        $('#forumSummary').show();
    }
    else {
        $('#forumSummary').hide();
        $('#forumForm').show();
    }
    var url = server_root + "/Processor/GeneralProcessPart7.aspx";
    $.ajax({
        url: url,
        async: true,
        cache: false,
        type: "POST",
        dataType: "html",
        data: {
            proc_id: 31,
            shared_cat: category
        },
        success: function(responseText) {
            $("#postLoading").fadeOut("slow", function() {
                $(this).hide();
                renderCategoryClass(category);
                $("#forumResult").html(responseText);
                if ($('#forumResult .friendListPic a img').attr('title') != '') {
                    initBaseThumnailToolTip("#forumResult .friendListPic a img");
                }

                if (idToScroll != "") {
                    if (own != "") {
                        showContent(cmn_type_id, 0);
                        onGetComment(cmn_type_id, idToScroll);
                        if (own != "0") {
                            onRecordVisitor(cmn_type_id, own);
                        }
                    }
                    else
                        onScrollToTopic("#yomShared" + idToScroll);
                }
            });
        }
    });
}
function renderCategoryClass(category) {
    var classID = 'class' + category;
    $('.forumComposer').attr('id', classID);
}
function onShowMoreBoard() {
    var next = parseInt($('#showMoreBoard').val());
    var category = $("#hdCat").val();
    var url = server_root + "/Processor/GeneralProcessPart7.aspx";
    $('#moreBoardLoading').show();
    $.ajax({ url: url, async: true, chache: false, type: "POST", dataType: "html", data: { proc_id: 32, next: next, shared_cat: category },
        success: function(responseText) {
            if ($.browser.msie) {
                $('#moreBoardLoading').fadeOut(150, function() {
                    $('#showMoreText').remove();
                    $(responseText).insertBefore("#before");
                });
            }
            else {
                $('#moreBoardLoading').fadeOut(200, function() {
                    $('#showMoreText').remove();
                    $(responseText).insertBefore("#before");
                });
            }
        }
    });
}
function onViewFriendAgree(t_id, t_cmn_cd) {
    if ($('#isShow' + t_id).length > 0) {
        if ($('#isShow' + t_id).val() != "0") {
            if (!$('#boardComment' + t_id).is(':visible')) {
                $('#viewFriendAgree' + t_id).hide();
                $('#boardShowHide' + t_id).html('0');
                $('#isShow' + t_id).val('0');
            }
        }
        else {
            if ($('#boardComment' + t_id).is(':visible')) {
                $('#boardComment' + t_id).hide();
                $('#viewFriendAgree' + t_id).show();
                $('#boardShowHide' + t_id).html('0');
                $('#isShow' + t_id).val(t_id);
            }
            else {
                $('#viewFriendAgree' + t_id).show();
                $('#isShow' + t_id).val(t_id);
                $('#boardShowHide' + t_id).html(t_id);
            }
        }
    }
    else {
        if ($('#boardComment' + t_id).is(':visible')) {
            $('#boardComment' + t_id).hide();
            $('#boardShowHide' + t_id).html('0');
        }
        var url = server_root + "/Processor/GeneralProcessPart4.aspx";
        $.ajax({ url: url, async: true, chache: false, data: { proc_id: 31, t_id: t_id, t_cmn_cd: t_cmn_cd }, success: function(responseText) {
            $('#viewFriendAgree' + t_id).html(responseText); $('#viewFriendAgree' + t_id).show();
            if ($('#viewFriendAgree' + t_id + ' .friendImg a img').attr('title') != '') { initThumnailToolTip('#viewFriendAgree' + t_id); }
        }
        });
    }
}

function onRecordVisitor(type_id, own) {
    var url = server_root + "/Processor/GeneralProcessPart7.aspx";
    $.ajax({
        url: url,
        async: true,
        cache: false,
        type: 'POST',
        data: {
            proc_id: 34,
            type_id: type_id,
            t_cd: 'PBB',
            own: own
        },
        success: function(responseText) {
            if (responseText.indexOf("Yes") > -1) {
                if ($('#visitValueForumView' + type_id).text() != "0") {
                    var visitValue = $('#visitValueForumView' + type_id).text();
                    var intValueIncrease = parseInt(visitValue, 10) + 1;

                    $('#visitValueForumView' + type_id).text(intValueIncrease);
                }
                else {
                    $('#visitValueForumView' + type_id).text('1');
                    $('#linkViewForumVisitor' + type_id).show();
                    $('#visitForumView' + type_id).attr("onclick", "onGetViewForumVisitor(" + type_id + ")");
                }
            }
        }
    });
}

function onGetViewForumVisitor(type_id) {
    if ($("#viewForumVisitors" + type_id).is(":visible")) {
        $("#viewForumVisitors" + type_id).hide();
    }
    else {
        if ($("#viewFriendAgree" + type_id).is(":visible")) {
            $("#viewFriendAgree" + type_id).hide();
        }

        $("#viewForumVisitors" + type_id).html(createDivLoading("Loading..."));
        $("#viewForumVisitors" + type_id).show();
        var url = server_root + "/Processor/GeneralProcessPart6.aspx";
        $.ajax({
            url: url,
            async: true,
            cache: false,
            type: 'POST',
            data: {
                proc_id: 23, //Save Group News
                blg_id: type_id,
                t_cd: 'PBB'
            },
            success: function(responseText) {
                $("#createDivLoading").fadeOut(200, function() {
                    $("#viewForumVisitors" + type_id).html(responseText);
                    if ($("#viewForumVisitors" + type_id + ' .friendListPic a img').attr('title') != '') {
                        initThumnailToolTip("#viewForumVisitors" + type_id);
                    }
                    $("#createDivLoading").remove();
                });
            }
        });
    }
}

function createDivLoading(text_loading) { var str = "<div id=\"createDivLoading\" style=\"text-align:center;\"><img src=\"" + server_root + "/styles/images/ajax-loader.gif\" /><h3>" + text_loading + "</h3></div>"; return str; }

function getSendPrvMsgDialog() {
    var dialog_result = "<!--End Choose Friend-->" +
                        "<div class=\"sendMsg_dialogue\" title=\"Send Message\" style=\"display:none;\">" +
                        "<div class=\"beforeSendMsg centerAlign\">" +
                        "    <form id=\"composeMsg\">" +
                        "        <table class=\"composerField form\">  " +
                        "            <tbody>" +
                        "            <tr><td class=\"rightAlign\"><label>To:</label></td>" +
                        "            <td class=\"field\"><input id=\"ms_to_mbr_id\" name=\"ms_to_mbr_id\" type=\"text\" class=\"inputtext\" readonly=\"readonly\" /></td>" +
                        "            <tr><td class=\"rightAlign\"><label>Subject:</label></td>" +
                        "            <td class=\"field\"><div><input id=\"ms_subject\" name=\"ms_subject\" type=\"text\" class=\"inputtext\" maxlength=\"100\" /></div></td></tr>" +
                        "            <tr><td class=\"rightAlign\"><label>Message:</label></td>" +
                        "            <td class=\"field\"><div><textarea id=\"ms_message\" name=\"ms_message\" rows=\"5\" cols=\"40\" onkeypress=\"return textareaMsgMaxLength(event,999,this);\"></textarea></div></td></tr>" +
                        "            <tr><td></td>" +
                        "            <td class=\"msgSubmit\"><input type=\"button\" onclick=\"\" id=\"btnMsgSend\" name=\"btnMsgSend\" value=\"Send\" class=\"buttonSBlue\" />&nbsp;<input type=\"button\" onclick=\"onCloseSendPrvMsgDialog()\" id=\"btnMsgCancel\" name=\"btnMsgCancel\" value=\"Cancel\" class=\"buttonSBlue\" /></td></tr>" +
                        "            </tbody>      " +
                        "        </table>" +
                        "    </form>   " +
                        "</div>" +
                        "<div id=\"msgResult\" class=\"successMsg\" style=\"display:none;\"></div> " +
                        "</div>";
    return dialog_result;
}

function initSendPrvMsgDialog() {
    $(function() {
        $('.sendMsg_dialogue').dialog({
            autoOpen: false,
            width: 500,
            height: 'auto',
            closeOnEscape: false,
            closeText: 'close',
            position: 'middle',
            draggable: true,
            modal: true
        });
    });
}

function onCloseSendPrvMsgDialog() {
    $('.sendMsg_dialogue').dialog('close');
}

function showDelete(pbb_id) {
    $('#aDelete' + pbb_id).show();
}

function hideDelete(pbb_id) {
    $('#aDelete' + pbb_id).hide();
}

function initDeleteForum() {
    var deleteDialog = "                                        <!--Cancel Dialog--> " +
                        "                                        <div id=\"deleteForum_dialogue\" class=\"deleteComment_dialogue\" title=\"Delete Confirmation\" style=\"height:auto !important;display:none;\"> " +
                        "                                            <div class=\"centerAlign\" style=\"padding-top:5px;\">" +
                        "                                              <label>Do you wish to delete this topic?</label>" +
                        "                                              <div class=\"centerAlign topPadding\"><input type=\"button\" id=\"btnDelTopicYes\" name=\"btnDelTopicYes\" value=\"Yes\" class=\"buttonSBlue\" /> <input type=\"button\" id=\"btnDelTopicNo\" name=\"btnDelTopicNo\" value=\"No\" class=\"buttonSBlue\" onclick=\"closeDeleteForumDialogue();\" /></div>" +
                        "                                              <div id=\"delMsgLoading\" class=\"centerAlign\" style=\"display:none;\"><img src=\"styles/images/ajax-loader.gif\" /></div> " +
                        "                                              <div id=\"delMsg\" class=\"successMsg\" style=\"display:none;\"></div> " +
                        "                                            </div> " +
                        "                                        </div>";

    $('#forum_dialog').append(deleteDialog);

    $(function() {
        $('#deleteForum_dialogue').dialog({
            autoOpen: false,
            width: 400,
            height: 'auto',
            closeOnEscape: false,
            closeText: 'close',
            position: 'middle',
            draggable: true,
            modal: true
        });
    });
}

function closeDeleteForumDialogue() {
    $('#deleteForum_dialogue').dialog('close');
}

function openDeleteForumDialogue(pbb_id) {
    gpbb_id = pbb_id;
    $('#deleteForum_dialogue').dialog('open');
}

function onDeleteForumTopic(pbb_id) {
    var category = $("#hdCat").val();
    var url = server_root + "/Processor/GeneralProcessPart7.aspx";
    $.ajax({
        url: url,
        async: true,
        cache: false,
        type: 'POST',
        data: {
            proc_id: 36, //Delete Forum Topic
            type_id: pbb_id,
            shared_cat: category
        },
        success: function(responseText) {
            if (parseInt(responseText.trim()) > 0) {
                closeDeleteForumDialogue();
                $("#yomShared" + pbb_id).remove();
                var topicCount = parseInt($("#topicCount" + category).text());
                if (topicCount > 0)
                    $("#topicCount" + category).text(topicCount - 1);

                var grandTopic = $("#grandTopic").text();
                if (parseInt(grandTopic) > 0) {
                    if (grandTopic.indexOf(',') > -1)
                        grandTopic = grandTopic.replace(",", "");
                    grandTopic = formatNumber1000CommaStyle(parseInt(grandTopic) - 1);
                    $("#grandTopic").text(grandTopic);
                }
            }
            else {
                onShowDialogMsg('delMsg', "Sorry you cannot remove because it has replies in this topic.", 35, true);
                $(this).oneTime(3500, "del_timer", function() {
                    closeDeleteForumDialogue();
                });
            }
        }
    });
}

function onSetSubThumbUpAndDown(t_id, t_cd, t_cmn_cd) {
    var url = server_root + "/Processor/GeneralProcessPart4.aspx";
    $.ajax({ url: url, async: true, chache: false, data: { proc_id: 30, t_id: t_id, t_cd: t_cd, t_cmn_cd: t_cmn_cd }, success: function(responseText) {
        if (responseText != '') {
            if (t_cd == "UP") {
                if (responseText.trim().indexOf('-') > 0) {
                    var rId = responseText.trim().substring('0', responseText.indexOf('-') - 1);
                    var subDw = parseInt($('#thumbsdown' + rId).text()) - 1;
                    $('#thumbsdown' + rId).text(subDw);

                    var addUp = parseInt($('#thumbsup' + rId).text()) + 1; $('#thumbsup' + rId).text(addUp);
                    if ($('#thumbsup' + rId).val() == '0') {
                        $('#viewSFriendAgreeClick' + rId).html('');
                        $('#viewSFriendAgree' + rId).html('');
                    }
                    else {
                        $('#viewSFriendAgreeClick' + rId).html("<img src=\"" + server_root + "/styles/images/thumb_more.gif\" onclick=\"onViewSFriendAgree(" + rId + ",'SPBB')\" title=\"View friends who liked\" />");
                    }
                }
                else {
                    var addUp = parseInt($('#thumbsup' + responseText.trim()).text()) + 1; $('#thumbsup' + responseText.trim()).text(addUp);
                    if ($('#thumbsup' + responseText.trim()).text() == '0') {
                        $('#viewSFriendAgreeClick' + responseText.trim()).html('');
                        $('#viewSFriendAgree' + responseText.trim()).html('');
                    }
                    else {
                        $('#viewSFriendAgreeClick' + responseText.trim()).html("<img src=\"" + server_root + "/styles/images/thumb_more.gif\" onclick=\"onViewSFriendAgree(" + responseText.trim() + ",'SPBB')\" title=\"View friends who liked\" />");
                    }
                }
            }
            else {
                if (responseText.trim().indexOf('-') > 0) {
                    var rId = responseText.trim().substring('0', responseText.indexOf('-') - 1);
                    var subUp = parseInt($('#thumbsup' + rId).text()) - 1;
                    $('#thumbsup' + rId).text(subUp);

                    var addDw = parseInt($('#thumbsdown' + rId).text()) + 1;
                    $('#thumbsdown' + rId).text(addDw);
                    if ($('#thumbsup' + rId).text() == '0') {
                        $('#viewSFriendAgreeClick' + rId).html('');
                        $('#viewSFriendAgree' + rId).html('');
                    }
                }
                else {
                    var addDw = parseInt($('#thumbsdown' + responseText.trim()).text()) + 1;
                    $('#thumbsdown' + responseText.trim()).text(addDw);
                }
            }
        }
    }
    });
}

function onViewSFriendAgree(t_id, t_cmn_cd) {
    if ($('#isShow' + t_id).length > 0) {
        if ($('#isShow' + t_id).val() != "0") {
            if (!$('#boardSComment' + t_id).is(':visible')) {
                $('#viewSFriendAgree' + t_id).hide();
                $('#boardSShowHide' + t_id).html('0');
                $('#isShow' + t_id).val('0');
            }
        }
        else {
            if ($('#boardSComment' + t_id).is(':visible')) {
                $('#boardSComment' + t_id).hide();
                $('#viewSFriendAgree' + t_id).show();
                $('#boardSShowHide' + t_id).html('0');
                $('#isShow' + t_id).val(t_id);
            }
            else {
                $('#viewSFriendAgree' + t_id).show();
                $('#isShow' + t_id).val(t_id);
                $('#boardSShowHide' + t_id).html(t_id);
            }
        }
    }
    else {
        if ($('#boardSComment' + t_id).is(':visible')) {
            $('#boardSComment' + t_id).hide();
            $('#boardSShowHide' + t_id).html('0');
        }
        var url = server_root + "/Processor/GeneralProcessPart4.aspx";
        $.ajax({ url: url, async: true, chache: false, data: { proc_id: 31, t_id: t_id, t_cmn_cd: t_cmn_cd }, success: function(responseText) {
            $('#viewSFriendAgree' + t_id).html(responseText); $('#viewSFriendAgree' + t_id).show();
        }
        });
    }
}

function onShowEmbedMedia(id, url, num) {
    var v = $("#hdCheckShow").val();
    var r = $("#hdCheckShow").attr("rel");
    if (v == id && r == num) {
        if ($("#" + id).is(":visible")) {
            $("#" + id).slideUp(300);
            $("#hdCheckShow").val('');
            $("#hdCheckShow").attr("rel", "");
        }
        else {
            var str = '<object width="380" height="265">';
            str += '<param name="movie" value="' + url + '"></param>';
            str += '<param name="allowFullScreen" value="true"></param>';
            str += '<param name="allowscriptaccess" value="always"></param>';
            str += '<param name="wmode" value="transparent" />';
            str += '<embed src="' + url + '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="380" height="265" wmode="transparent" style="width:380px; height:265px;"></embed>';
            str += '</object>';
            $("#" + id).html(str).slideDown(300);
            $("#hdCheckShow").val(id);
            $("#hdCheckShow").attr("rel", num);
        }
    }
    else {
        var str = '<object width="380" height="265">';
        str += '<param name="movie" value="' + url + '"></param>';
        str += '<param name="allowFullScreen" value="true"></param>';
        str += '<param name="allowscriptaccess" value="always"></param>';
        str += '<param name="wmode" value="transparent" />';
        str += '<embed src="' + url + '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="380" height="265" wmode="transparent" style="width:380px; height:265px;"></embed>';
        str += '</object>';
        $("#" + id).html(str).slideDown(300);
        $("#hdCheckShow").val(id);
        $("#hdCheckShow").attr("rel", num);
    }
}

function randomString() {
    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
    var stringLength = 8;
    var randomstring = '';
    for (var i = 0; i < stringLength; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        randomstring += chars.substring(rnum, rnum + 1);
    }
    return randomstring;
}

function formatNumber1000CommaStyle(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

function onShowMoreRecentTopicAndReplies(type_cd) {
    if (type_cd == "TPC")
        $("#topicMoreLoading").show();
    else if (type_cd == "REP")
        $("#replyMoreLoading").show();
    else
        $("#hottestMoreLoading").show();

    var url = server_root + "/Processor/GeneralProcessPart8.aspx";
    $.ajax({
        url: url,
        dataType: "html",
        async: true,
        cache: false,
        type: 'POST',
        data: {
            proc_id: 22,
            type_cd: type_cd,
            blnIsHotDiff: false
        },
        success: function(responseText) {
            if (type_cd == "TPC") {
                $("#moreRcTopic").remove();
                $("#topicMoreLoading").fadeOut(200, function() {
                    $("#rcMoreTopics").append(responseText);
                });
            }
            else if (type_cd == "REP") {
                $("#moreRcReply").remove();
                $("#replyMoreLoading").fadeOut(200, function() {
                    $("#rcMoreReplies").append(responseText);
                });
            }
            else {
                $("#moreRcHottest").remove();
                $("#hottestMoreLoading").fadeOut(200, function() {
                    $("#rcMoreHottest").append(responseText);
                });
            }
        }
    });
}

function onScrollToTopic(idToScroll) {
    var scrollTopVal = $(idToScroll).offset().top;
    $('html,body').animate({ scrollTop: scrollTopVal }, 1000);
}

function onShowTopic(cat, cat_name, idToScroll, own) {    
    $("#postLoading").show();
    $("#forumResult").html('');
    $("#hdCat").val(cat);
    $("#forumCategoryTitle").html("<h3>" + cat_name + "</h3>");

    if (is_mas == "False") {
        if (cat == "AOP") {
            $('.forumComposer').hide();
            is_aop = cat;
        }
        else
            $('.forumComposer').show();
    }
    else
        $('.forumComposer').show();

    if (cat == "LIF") {        
        $('#forumForm').hide();
        var height = $('#forumForm').height();
        $('#forumSummary').css("height", height + "px");
        $('#forumSummary').show();
    }
    else {
        $('#forumSummary').hide();
        $('#forumForm').show();
    }
    
    var url = server_root + "/Processor/GeneralProcessPart8.aspx";
    $.ajax({
        url: url,
        async: true,
        cache: false,
        type: "POST",
        dataType: "html",
        data: {
            proc_id: 23,
            shared_cat: cat,
            top_id: idToScroll
        },
        success: function(responseText) {
            renderCategoryClass(cat);
            $('.forumTopic ul li').attr('class', '');
            $("#Category" + cat).attr('class', 'tselected');

            $("#postLoading").fadeOut(200, function() {
                $("#forumResult").html(responseText);
                $('#viewAllTopicLink').show();
                if ($('#forumResult .friendListPic a img').attr('title') != '') {
                    initBaseThumnailToolTip("#forumResult .friendListPic a img");
                }

                onScrollToTopic("#yomShared" + idToScroll);

                var strLink = "<a onclick=\"onShowByCategory('" + cat + "', '" + cat_name + "', '', 0, '0');\">View all topics</a>";
                $('#viewAllTopicLink').html(strLink).show();

                if (own != "0")
                    onRecordVisitor(idToScroll, own);
            });

            $(this).oneTime(250, function() {
                if ($("#boardComment" + idToScroll + ' .blogTextarea').length > 0) { setTextBoxAutoResize("#boardComment" + idToScroll); }
            });

        }
    });
}

function onShowReplies(cat, cat_name, cmn_type_id, own, idToScroll) {
    $("#postLoading").show();
    $("#forumResult").html('');
    $("#hdCat").val(cat);
    $("#forumCategoryTitle").html("<h3>" + cat_name + "</h3>");
    var url = server_root + "/Processor/GeneralProcessPart8.aspx";
    $.ajax({
        url: url,
        async: true,
        cache: false,
        type: "POST",
        dataType: "html",
        data: {
            proc_id: 23,
            shared_cat: cat,
            top_id: cmn_type_id
        },
        success: function(responseText) {
            renderCategoryClass(cat);
            $('.forumTopic ul li').attr('class', '');
            $("#Category" + cat).attr('class', 'tselected');
            if ($.browser.msie) {
                $("#postLoading").fadeOut(50, function() {
                    $("#forumResult").html(responseText);
                    $('#viewAllTopicLink').show();
                    if ($('#forumResult .friendListPic a img').attr('title') != '') {
                        initBaseThumnailToolTip("#forumResult .friendListPic a img");
                    }
                    onScrollToTopic("#commentAdd" + idToScroll);

                    var strLink = "<a onclick=\"onShowByCategory('" + cat + "', '" + cat_name + "', '', 0, '0');\">View all topics</a>";
                    $('#viewAllTopicLink').html(strLink).show();
                    if (own != "0")
                        onRecordVisitor(cmn_type_id, own);
                });
            }
            else {
                $("#postLoading").fadeOut(200, function() {
                    $("#forumResult").html(responseText);
                    $('#viewAllTopicLink').show();
                    if ($('#forumResult .friendListPic a img').attr('title') != '') {
                        initBaseThumnailToolTip("#forumResult .friendListPic a img");
                    }
                    onScrollToTopic("#commentAdd" + idToScroll);

                    var strLink = "<a onclick=\"onShowByCategory('" + cat + "', '" + cat_name + "', '', 0, '0');\">View all topics</a>";
                    $('#viewAllTopicLink').html(strLink).show();
                    if (own != "0")
                        onRecordVisitor(cmn_type_id, own);

                });
            }
            $(this).oneTime(250, function() {
                if ($("#boardComment" + cmn_type_id + ' .blogTextarea').length > 0) { setTextBoxAutoResize("#boardComment" + cmn_type_id); }
            });
        }
    });
}

function onShowDialogMsg(msgId, msgText, numIcrease, isShow) {
    $('.ui-widget-shadow').css('height', $('.ui-widget-shadow').height() + parseInt(numIcrease));
    $('#' + msgId).html(msgText);
    if (isShow)
        $('#' + msgId).show();
}

function onResultForumFilterClick(pol_id) {
    getPollResultForum('#pollResultBoxForum', pol_id, true);
}
function getPollResultForum(container_id, pol_id, visibility) {
    var voter_location = $('input[name=p_fresult_type' + pol_id + ']:checked').val();
    voter_location = voter_location == null ? -1 : voter_location;
    var voter_gender = $('input[name=p_fresult_gender' + pol_id + ']:checked').val();
    voter_gender = voter_gender == null ? 'MF' : voter_gender;
    var url = server_root + "/Processor/GeneralProcessPart11.aspx";
    $.ajax({
        url: url,
        async: true,
        chache: false,
        type: "POST",
        dataType: "html",
        data: {
            proc_id: 27,
            pol_id: pol_id,
            lang_type: glang_type,
            poll_type: 'ALL',
            voter_location: voter_location,
            voter_gender: voter_gender,
            acc_type: 'forum'
        },
        success: function(respText) {
            $(container_id + pol_id).html(respText);
            if (visibility == true)
            { $('#pollResultForum' + pol_id).show(); }
            initBaseThumnailToolTip('.pollChoice img');
        }
    });
}

function getFixPollBoxForumArea(container_id, pol_id, id, bln) {
    var visibility = $(container_id + ' .pollAnswer:visible').length > 0 ? true : false;
    var url = server_root + "/Processor/GeneralProcessPart11.aspx";
    $.ajax({
        url: url,
        async: true,
        chache: false,
        type: "POST",
        dataType: "html",
        data: {
            proc_id: 25,
            pol_id: pol_id,
            lang_type: glang_type,
            poll_type: 'ALL',
            acc_type: 'forum'
        },
        success: function(respText) {
            if (respText.indexOf('success') > -1) {
                $(container_id).html(respText);
                $(this).oneTime(350, "timer_delay_for_result", function() {
                    getPollFixResultForumArea('#pollResultBoxForum' + pol_id, pol_id, visibility);
                });
                $('#showMsg' + id).slideDown('slow', function() { onGetComment(id); });
            } else {
                $(container_id).html(respText);
                initBaseThumnailToolTip('.pollAnswer img');
                if (visibility == true)
                { $('#poll' + pol_id).show(); }
                var winnerBlink = 0;
                $(this).oneTime(500, function() {
                    if (winnerBlink == 0) { $('#voteImg').attr('src', 'styles/images/vote_animate.gif'); }
                    $(this).everyTime(100, 'newVote', function() {
                        winnerBlink++;
                        if ($(container_id + ' .orangeBorder').length > 0) {
                            $(container_id + ' .blockAdContent').removeClass('orangeBorder');
                        }
                        else { $(container_id + ' .blockAdContent').addClass('orangeBorder'); }
                        if (winnerBlink > 40) {
                            $(this).stopTime("newVote");
                            $(container_id + ' .blockAdContent').removeClass('orangeBorder');
                            $('#voteImg').attr('src', 'styles/images/vote.gif');
                        }
                    });
                });
                if (bln)
                    $('#showMsg' + id).slideDown('slow', function() { onGetComment(id); });
            }
        }
    });
}

function onPollResultLangChangeForumArea(pol_id, lang_type, id) {
    glang_type = lang_type;
    var imgPollBody = $('#showMsg' + id).html();
    getPollFixResultForumArea('#pollResultBoxForum' + pol_id, pol_id, true);
}

function onPollVoteLangChangeForumArea(pol_id, lang_type, id) {
    glang_type = lang_type;
    var imgPollBody = $('#showMsg' + id).html();
    getFixPollBoxForumArea('#showMsg' + id, pol_id, id, false);
}
function getPollFixResultForumArea(container_id, pol_id, visibility) {
    var voter_location = $('input[name=p_result_type' + pol_id + ']:checked').val();
    voter_location = voter_location == null ? -1 : voter_location;
    var voter_gender = $('input[name=p_result_gender' + pol_id + ']:checked').val();
    voter_gender = voter_gender == null ? 'MF' : voter_gender;

    var url = server_root + "/Processor/GeneralProcessPart11.aspx";
    $.ajax({
        url: url,
        async: true,
        chache: false,
        type: "POST",
        dataType: "html",
        data: {
            proc_id: 27,
            pol_id: pol_id,
            lang_type: glang_type,
            poll_type: 'ALL',
            voter_location: voter_location,
            voter_gender: voter_gender,
            acc_type: 'forum'
        },
        success: function(respText) {
            $(container_id).html(respText);
            if (visibility == true)
            { $('#pollForumResult' + pol_id).show(); }
            initBaseThumnailToolTip('.pollChoice img');
        }
    });
}
function showPollCContentForumArea(id) {
    if ($('#pollForumArea' + id + ':visible').length == 0) {
        $('#pollForumArea' + id).slideDown('slow'); initBaseThumnailToolTip('.viewVoteName .friendListPic img');
    } else {
        $('#pollForumArea' + id).slideUp('slow');
    }
}
function submitVoteForumArea(pol_id, id) {
    var pod_id = $('input[name=pollChoice]:checked').val();
    var is_cam = $('input[name=p_result_loc' + pol_id + ']:checked').val();
    is_cam = is_cam != null ? is_cam : 1;
    is_cam = is_cam == 1 ? true : false;

    var is_reveal = $('input[name=p_reveal' + pol_id + ']:checked').val();
    is_reveal = is_reveal == null ? 'off' : 'on';
    is_reveal = is_reveal == 'on' ? true : false;

    var is_male = $('input[name=p_result_gender' + pol_id + ']:checked').val();
    is_male = is_male == null ? 'M' : is_male;
    is_male = is_male == 'M' ? true : false;

    //alert(pod_id + ' |Cam: ' + is_cam + ' |Reveal: ' + is_reveal + ' |Male: ' + is_male);

    if (pod_id != null) {
        var url = server_root + "/Processor/GeneralProcessPart11.aspx";
        $.ajax({
            url: url,
            async: true,
            chache: false,
            type: "POST",
            dataType: "html",
            data: {
                proc_id: 26,
                pod_id: pod_id,
                is_cam: is_cam,
                is_reveal: is_reveal,
                is_male: is_male
            },
            success: function(respText) {
                var imgPollBody = $('#showMsg' + id).html();
                getPollFixResultForumArea('#pollResultBoxForum' + pol_id, pol_id, true);
            }
        });
    }
}
