﻿var showComment = function(table, ar_idx) {    
    $('#CommentList').load('/_comm/_getCmtList.aspx', { table: table, ar_idx: ar_idx });
};

var writeComment = function(table, ar_idx, cm_idx) {

    if (!ar_idx) { showBoardMsgbox('System Error'); return; }
    if (!cm_idx) cm_idx = '';
    var nick = ($('#CmtName' + cm_idx)) ? $('#CmtName' + cm_idx).val() : null;
    var pwd = ($('#CmtPwd' + cm_idx)) ? $('#CmtPwd' + cm_idx).val() : null;
    var blog = ($('#CmtBlog' + cm_idx)) ? $('#CmtBlog' + cm_idx).val() : null;
    var content = $('#CmtContent' + cm_idx).val();

    if (!content) { showBoardMsgbox('Please enter the description.'); return; }

    $.getJSON('/_comm/_setCmtWrite.aspx',
        { table: table, ar_idx: ar_idx, cm_idx: cm_idx, nick: nick, pwd: pwd, blog: blog, content: content },
        function(json) {
            
            if (json.errno != 0) {
                showBoardMsgbox(json.error);
                return;
            }

            var cm_idx = (json.cm_idx > 0) ? json.cm_idx : '';

            if ($('#CmtName' + cm_idx)) $('#CmtName' + cm_idx).val('');
            if ($('#CmtPwd' + cm_idx)) $('#CmtPwd' + cm_idx).val('');
            if ($('#CmtBlog' + cm_idx)) $('#CmtBlog' + cm_idx).val('');

            $('#CmtContent' + cm_idx).val('');
            showComment(json.table, json.ar_idx);
        });

};

var replyComment = function(cm_idx) {
    var reply = $('#ReplyComment' + cm_idx);
    if (reply)    
        if (reply.css('display') == 'block') {reply.css('display', 'none');} else {reply.css('display', 'block');}
}

var deleteComment = function(table, cm_idx, mb_idx) {
    var success, pwd;
    if (mb_idx) {
        success = confirm("Are you sure you want to delete this Comment?");
    } else {
        showBoardPwdInput(table, cm_idx); return;
    }
    if (success) {

        $.getJSON('/_comm/_setCmtDelete.aspx', { table: table, cm_idx: cm_idx }, function(res) {
            if (res.errno > 0) showBoardMsgbox(res.error);
            else showComment(res.table, res.ar_idx);
        });
    }
};

var changeFaqTab = function(parent, object) {
    $('#' + parent + ' li').removeClass('select');
    $('#' + object).addClass('select');
    //   로딩
    Updating();    
    $('#stCBbsFaqContainer').load('/_comm/_getfaqBoard.aspx', { type: boardType(object), page: 1 }, ContainerLoadExec);
};

var boardType = function(object) {
    var board = '1';
    switch (object) {
        case 'bbsFaqMenu01': break;
        case 'bbsFaqMenu02': board = '2'; break;
        case 'bbsFaqMenu03': board = '3'; break;
        case 'bbsFaqMenu04': board = '4'; break;
    }
    return board;
}

var Updating = function() {
    $('#stCBbsFaqContainer').html(loader);
}
var ContainerLoadExec = function(msg) {

}

var PageMove = function(boardId, page) {
    Updating();
    $('#stCBbsFaqContainer').load('/_comm/_getfaqBoard.aspx', { board: boardId, page: page });
}

/* 글쓰기 시 체크 */
var confirmBoardWrite = function(subjectid, contentid) {

    if ($('#' + subjectid).val() == '') {
        showBoardMsgbox('Please enter the title.');
        return false;
    }
    if ($('#' + contentid).val() == '') {
        showBoardMsgbox('Please enter the description.');
        return false;
    }

    return true;
};

/* Board용 모달 폼 */
var layer_boardmsgbox;
var showBoardMsgbox = function(msg) {
    if (!msg) return;

    msg = msg.replace(/\n/g, '<br/>');
    var width = 300, height = 200, client = getClientInfo();
    var left = (client.cwidth - width) / 2 + client.left;
    var top = (client.cheight - height) / 2 + client.top;

    if (!layer_boardmsgbox) {
        layer_boardmsgbox = document.createElement('div');
        layer_boardmsgbox.className = 'board_msg';
        layer_boardmsgbox.style.position = 'absolute';
        layer_boardmsgbox.style.zIndex = 100;
        layer_boardmsgbox.innerHTML = '<div class="close"><img src="' + root_path + 'Images/bbs/board_msg_btn_close.gif" alt="Close" onclick="hideBoardMsgbox()"/></div><div id="boardMsgBoxMsg"></div><div class="cmd"><img src="' + root_path + 'Images/bbs/board_msg_btn_ok.gif" alt="OK" onclick="hideBoardMsgbox()"/></div>';

    }

    layer_boardmsgbox.style.left = (left == 0) ? left : left + 'px'
    layer_boardmsgbox.style.top = (top == 0) ? top : top + 'px'

    if(st_layerbg == null) 
        showLayerBg();

    st_layerbg.onclick = hideBoardMsgbox;
    document.body.appendChild(layer_boardmsgbox);
    $('#boardMsgBoxMsg').html(msg);
};
var hideBoardMsgbox = function() {
    hideLayerBg();
    document.body.removeChild(layer_boardmsgbox);
};


/* 비회원 비밀번호 입력 프로세스. */
var showBoardPwdInput = function(table, cm_idx) {
    var width = 300, height = 200, client = getClientInfo();
    var left = (client.cwidth - width) / 2 + client.left;
    var top = (client.cheight - height) / 2 + client.top;

    layer_boardmsgbox = null;
    layer_boardmsgbox = document.createElement('div');
    layer_boardmsgbox.className = 'board_msg';
    layer_boardmsgbox.style.position = 'absolute';
    layer_boardmsgbox.style.zIndex = 100;
    layer_boardmsgbox.innerHTML = '<div class="close"><img src="/Images/bbs/board_msg_btn_close.gif" alt="Close" onclick="hideBoardPwdInput()"/></div><div id="boardMsgBoxMsg"><div class="contents">Please enter your password.</div><div><input type="hidden" id="CmtTableName" value="' + table + '"/><input type="hidden" id="CmtCmIdx" value="' + cm_idx + '"/><input type="password" id="CmtDelPwd" size="15"/></div><div id="boardMsgboxCommand"></div></div><div class="cmd"><img src="/Images/bbs/board_msg_btn_ok.gif" alt="OK" onclick="submitBoardPwdInput()"/></div>';

    layer_boardmsgbox.style.left = (left == 0) ? left : left + 'px';
    layer_boardmsgbox.style.top = (top == 0) ? top : top + 'px';

    showLayerBg();
    st_layerbg.onclick = hideBoardPwdInput;
    document.body.appendChild(layer_boardmsgbox);
};
var hideBoardPwdInput = function() {
    hideLayerBg();
    document.body.removeChild(layer_boardmsgbox);
    layer_boardmsgbox = null;
};
var submitBoardPwdInput = function() {
    var pwd = $('#CmtDelPwd');
    if (pwd.val() == '') { $('#boardMsgboxCommand').html('Password is Required.'); return; }
    $.post('/_comm/_setCmtDelete.aspx', { table: $('#CmtTableName').val(), cm_idx: $('#CmtCmIdx').val(), pwd: pwd.val() },
    function(res) {
        if (res.errno > 0) {
            $('#boardMsgboxCommand').html(res.error);
        }
        else {
            hideBoardPwdInput(); showComment(res.table, res.ar_idx);
        }
    }, 'json'
    );
    $('#CmtDelPwd').val('');
};


/* 실 요청하기  */
/* rachael */
var getSealRequestPage = function(paging) {
}
var showRequestSealViewCmtList = function(ar_idx) {
    
    $('#vsCmtInfo_' + ar_idx).hide();
    $('#vsCmtList_' + ar_idx).load('/_comm/_sealRequestViewCmtCmt.aspx', { ar_idx: ar_idx, isRequest: true });
};
var writeRequestSealViewCmtCmt = function(ar_idx) {
    var comment = document.getElementById('vsCmtCmtInput_' + ar_idx);
    if (!comment.value) { alert('Please enter your reply.'); comment.focus(); return; }

    $.post('/_comm/_sealRequestViewCmtCmtCommand.aspx', { ar_idx: ar_idx, cm_content: comment.value }, function(res) { if (res.error) alert(res.error); else $('#vsCmtList_' + res.ar_idx).load('/_comm/_sealRequestViewCmtCmt.aspx', { ar_idx: res.ar_idx }); }, 'json');
};
// 댓글을 추천한다.addVoteSealViewComment
var addVoteRequestSealViewComment = function(page, ar_idx) {    
    $.post('/_comm/_sealRequestViewCommentWrite.aspx', { page: page, ar_idx: ar_idx },
    function(res) { if (res.error) alert(res.error); else { alert('Vote Completed.'); document.getElementById('svCmtVote_' + ar_idx).innerHTML = res.ar_vote; } }, 'json');
};
// 요청 글을 작성한다.
var writeSealViewComment = function(seal_idx) {
    var content = document.getElementById('sealViewCmtBox'), icon = document.getElementById('sealViewCmtIcon');
    if (!content) return;
    if (!content.value) { alert('Please enter your reply.'); content.focus(); return; }
    if (!icon) icon = { value: '' };

    $.post('/_comm/_sealRequestViewCommentWrite.aspx', { ar_content: content.value },
    function(res) {
        if (res.error) alert(res.error);
        else window.location.href = '/HTML/board/Request/?page=1'; 
    }, 'json');
};

var deleteSealViewCmtCmt = function(ar_idx, cm_idx) {
    $.post('/_comm/_sealRequestViewCmtCmtCommand.aspx', { ar_idx: ar_idx, cm_idx: cm_idx }, function(res) { if (res.error) alert(res.error); else $('#vsCmtList_' + res.ar_idx).load('/_comm/_sealRequestViewCmtCmt.aspx', { ar_idx: res.ar_idx }); }, 'json');
};
