﻿// JScript 파일
//var bid; //게시판 id : List.js
//var bno; //게시물 번호 : List.js

///////////////////// Comment.js /////////////////////
//function modifyComment(cno) { alert("댓글 수정 : " + cno); }
function showOtherRefer(obj,cno){
// alert("참고자료 더보기 : " + cno);
    if(cno<1) return;

    new Ajax("/ajax/xb/CommentReferList.aspx?cno="+cno,
        {update:'comRef'+cno,onSuccess:function(ajax){
            if(ajax.getResponseText().substring(0,3)=="ER|"){ try{var pose=ajax.getResponseText().indexOf("$"); alert(aErrors[ajax.getResponseText().substring(3,pose)]);}catch(e){}  return;}
            obj.style.display='none';
        }, onError:function(ajax){alert(aErrors['NOAVAIL']);} });
}

///////공감///////
// o : 공감 버튼 object, agree : A=>agree D=>disagree
function agreeComment(cno, agree, o)
{
    var url = "/expert/ajax/ExpertAjax.aspx";
    var query = "?" + "act=aco&bid="+bid+"&bno="+bno+"&cno="+cno+"&agr="+agree+(fid?"&fid="+fid:"");
    new Ajax(url+query, { onSuccess:function(ajax) { onAgreeComment(ajax.getResponseText(), agree, o);}, onError:function(ajax) { onAjaxErr(ajax);} });
}
// o : 공감 버튼 object
function onAgreeComment(retStr, agree, o)
{
    var str = (agree=="A") ? "공감" : "비공감";
    switch (trim(retStr)) {
        case "-1" : alert("이전에 완료하셨습니다."); return;
        case "0"  : alert(str + "하기 에러입니다. 잠시 후 다시 시도하십시오."); return;
    }
    o.innerHTML = trim(retStr);
    alert(str + "이 완료되었습니다.");
    return;
}
///////공감///////
///////modifyComment///////
var comment=null;
function modifyComment(cno, obj)
{
    //window.location.href="#divCommentInBox";
    $('txtComment').focus();
    expertAjax("gco", "&cno="+cno, onModifyComment);
}
function onModifyComment(retstr)
{
    if (retstr.length < 3) return;
    comment = eval('('+retstr+')');
    $('btnWriteComment').innerHTML = '댓글수정';
    $('txtComment').value = comment[0]["Comment"];
    var cno = comment[0]["CNo"];
    var fileName = comment[0]["FileName"];
    if (fileName.length > 0) { $('divOldAttach').innerHTML = getAttachHtml(cno,comment); $HIDEB('divNewAttach');}
    else {$('divOldAttach').innerHTML = ''; $SHOWB('divNewAttach');}
    $('spnCNo').innerHTML = cno;
    expertAjax("gcr", "&cno="+cno, onGetCommentRefer);
}
function onGetCommentRefer(retstr)
{
    if (retstr.length < 3) return;
    var refer = eval('('+retstr+')');
    for (var i=0; i<refer.length; i++) { addReferHtml(refer[i]["Flag"], refer[i]["BID"], refer[i]["BName"], refer[i]["BNo"], refer[i]["Subject"]); }
}
function getAttachHtml(cno,comment)
{
    //alert($('bid').value);
    var html = '';
    html += "<span onclick='revertAttachStatus(" + cno + ", this);return false;'>";
    var fileName = comment[0]["FileName"];
    html += "<img src='" + getFilePath(fileName) + "' style='height:30px'/>";
    html += fileName;
    html += "(" + comment[0]["FileSize"] + " Bytes)";
    html += "</span>";
    html += " [<span id='btn" + cno + "'>삭제</span>]";
    return html;
}
function revertAttachStatus(cno, obj)
{
    var sta = ($('btn'+cno).innerHTML == "삭제") ? "D" : "A";
    obj.style.textDecoration = ((sta == "D") ? "line-through" : "none");
    $('btn'+cno).innerHTML = ((sta == "D") ? "복구" : "삭제");
    if (sta == "A") $HIDEB('divNewAttach');
    if (sta == "D") $SHOWB('divNewAttach');
}
///////modifyComment///////
///////writeComment///////
function getRefer()
{
    var refer = "";
    var ps = $('divRefer').getElementsByTagName("P");
    for (var i=(ps.length-1); i>=0; i--) { if (ps[i].style.display != 'none') refer += ps[i].id.substring(2) + "|"; }
    return refer;
}
function getCommentAjaxArg()
{
    if (!isRequired('txtComment'))  return "";
    var arg = "";
    arg += "co="  + encodeURIComponent(getTextVal('Comment'));
    arg += "&af=" + encodeURIComponent(GetAttachKeys('divNewAttach'));
    arg += "&re=" + encodeURIComponent(getRefer());
    arg += (comment) ? "&cno=" + comment[0]["CNo"] : "";
    if (comment && $('btn'+comment[0]["CNo"]) && $('btn'+comment[0]["CNo"]).innerHTML == "삭제") {
        arg += (comment) ? "&fna=" + encodeURIComponent(comment[0]["FileName"]) : "";
        arg += (comment) ? "&fsi=" + encodeURIComponent(comment[0]["FileSize"]) : "";
        arg += (comment) ? "&mim=" + encodeURIComponent(comment[0]["MimeType"]) : "";
        arg += (comment) ? "&wid=" + encodeURIComponent(comment[0]["ImageWidth"]) : "";
        arg += (comment) ? "&hei=" + encodeURIComponent(comment[0]["ImageHeight"]) : "";
    }
    return arg;
}
function writeComment(bno)
{
    var arg = getCommentAjaxArg();
    if (arg.length < 1) return;
    if (gds == "y") { bid = $('bid').value; }
    if (bid == "circuit" && $('txtComment').value == '"아하!이 회로도"라고 생각되신다면 지금 바로 댓글의 첨부파일을 이용하여 올려주세요.') return;
    var act = (comment) ? "M" : "W";
    if (act == "W") expertAjax('wco', '', onWriteComment, 'post', arg);
    if (act == "M") expertAjax('mco', '', onWriteComment, 'post', arg);
    comment = null;
}
function onWriteComment(retStr)
{
    switch (trim(retStr)) {
        case "-1000": alert("권한이 없습니다.(-1000)"); return;
        case "-1"   : alert("댓글등록 에러입니다. 잠시 후 다시 시도하십시오.(-1)"); return;
        case "0"    : alert("댓글등록 에러입니다. 잠시 후 다시 시도하십시오.(0)"); return;
    }
    showComment(bno); //if ((bid=="circuit") && parent.$('if_'+bno)) { setTimeout("resizeFrameHeight('if_'+bno)", 2000);}
    alert("정상 처리 되었습니다.");
    return;
}
///////writeComment///////
function cancelComment() {showComment(bno);}
///////deleteComment///////
function deleteComment(cno) {if (confirm("댓글을 삭제하시겠습니까?")) expertAjax("dco", "&cno="+cno, onDeleteComment);}
function onDeleteComment(retStr)
{
    switch (trim(retStr)) {
        case "-1000"  : alert("권한이 없습니다.(-1000)"); return;
        case "0"  : alert("댓글삭제 에러입니다. 잠시 후 다시 시도하십시오.(0)"); return;
    }
    showComment(bno);
    alert("댓글이 삭제되었습니다.");
    return;
}
///////deleteComment///////
/////// 참고자료 ///////
// 인자:대상글제목,영역(B,F,S),대상글BNo
function showScrap(destsubject,flag,destbno){
    if(destsubject==undefined||destsubject.length<1||flag==undefined||flag.length<1||destbno==undefined||parseInt(destbno,10)<1) return;

    var pos=getCurrentXY();
    addLayer('divScrapForm','Bookmark','none',true,pos[0]-200,pos[1],true);
    new Ajax("/ajax/xb/ScrapForm.aspx?dsub="+encodeURIComponent(destsubject)+"&f="+flag+"&bno="+destbno,
        {update:'divScrapForm',onSuccess:function(ajax){
            if(ajax.getResponseText().substring(0,3)=="ER|"){ try{var pose=ajax.getResponseText().indexOf("$"); alert(aErrors[ajax.getResponseText().substring(3,pose)]);}catch(e){}  return;}
            $SHOWB('divScrapForm');
        }, onError:function(ajax){alert(aErrors['NOAVAIL']);} });
}

function regScrap(flag,bno){
    if(bno==undefined||bno<1)return;
    var scomment=$('txtScrapComment');
    
    if(scomment==undefined||scomment.value.length<1){
        alert('책갈피 코멘트를 입력해주세요');return;
    }
    
    var data="f="+flag+"&bno="+bno+"&sc="+encodeURIComponent(scomment.value);
    
    new Ajax("/ajax/xb/XBoardAjax.aspx?cmd=regscrap",{method:'post',content:data,onSuccess:function(ajax){
            if(ajax.getResponseText().substring(0,3)=="ER|"){ try{var pose=ajax.getResponseText().indexOf("$"); alert(aErrors[ajax.getResponseText().substring(3,pose)]);}catch(e){} return;}
            if(ajax.getResponseText()=="OK") { alert(aErrors['SCRAPOOK']); hideLayer('divScrapForm'); return; }
            alert(aErrors['NOAVAIL']);
        }, onError:function(ajax){alert(aErrors['NOAVAIL']);} });
}

function showMyScrap(){
    var pos=getCurrentXY();
    addLayer('divMyScrapList','Attchpopup','none',true,pos[0],pos[1],true);
    new Ajax("/ajax/xb/MyScrapList.aspx",
        {update:'divMyScrapList',onSuccess:function(ajax){
            if(ajax.getResponseText().substring(0,3)=="ER|"){ try{var pose=ajax.getResponseText().indexOf("$"); alert(aErrors[ajax.getResponseText().substring(3,pose)]);}catch(e){}  return;}
            $SHOWB('divMyScrapList');
        }, onError:function(ajax){alert(aErrors['NOAVAIL']);} });
}

//referBno : 참고 글 번호 , subject : 참고글 제목
function addReferHtml(flag, bid, bName, referBno, subject)
{
    var b = false;
    hideLayer('divMyScrapList');
    var ps = $('divRefer').getElementsByTagName("P");
    for (var i=(ps.length-1); i>=0; i--) {
        if (ps[i].style.display != "none" && ps[i].id.substring(2).split(':')[0] == referBno) { b=true; break; }
    }
    if (b) {alert('추가된 참고자료입니다.'); return; }
    
    var str = "";
    str += "<p id='rp" + referBno + ":" + flag +"'>";
    str += "<img src='http://image.surae.com/img/Common/Icon/writing.gif' alt='책갈피'/> <a href='/API/Redirect/XBoard.aspx?f="+flag+"&bno=" + referBno + "' target='_blank'>" + subject + "</a>";
    str += "<a class='CommentIcondel' href='#' title='삭제' onclick='this.parentNode.style.display=\"none\";return false;'></a></p>";
    $('divRefer').innerHTML += str;
}
/////// 참고자료 ///////
/////// init ///////
//bid:board id list.js에서 받음, no:게시물 번호
function showComment(no)
{
    var arg = "bid="+bid+"&bno="+no+(fid?"&fid="+fid:"");
    if (bid=="shopconsult" && $('hidShopID')) arg += "&sho=" + $('hidShopID').value; 
    if (gds=="y") arg += "&gds=y";
    //alert(arg);
    new Ajax("/expert/ajax/Comment.aspx?" + arg, {
                onSuccess:function(ajax) {onInitComment(ajax.getResponseText()); /* if (bid == "circuit") {  setTimeout("resizeFrameHeight('if_'+no)",2000); } */ },
                onError:function(ajax) { /*onAjaxErr(ajax);*/$('divComment').innerHTML = ajax.getResponseText(); } });
}
function onInitComment(retstr)
{
    if (retstr.length < 250) retstr = "<div class='Viewcommenttitle' style='height:1px'></div>";
    $('divComment').innerHTML = retstr;
    InitFileUpload("divNewAttach", 1);
    
    if (bid == "qna") onCommentQna();
    if (bid == "discuss") onCommentDiscuss();
    //if (bid == "circuit") onCommentCircuit();
    if ($('hidBoardType') && $('hidBoardType').value=="C") onCommentDiscuss();
    if (bid == "shopnote") $HIDEB('divCommentInBox');
}
/////// init ///////
///////////////////// Comment.js /////////////////////