function LoadJslbAjax(){
    if(typeof(sendRequest)=='undefined' && typeof(createHttpRequest)=='undefined'){
        document.write('<scr'+'ipt type="text/javascript" src="/js/front19/common/jslb_ajax.js"></scr'+'ipt>');
    }
}
LoadJslbAjax();

function comment_del( form )
{
	msg = "정말로 삭제 하시겠습니까?";

	if( !confirm( msg ) )
	{
		return false;
	}

    form.mode.value = 'del';

    try {
        if(AuthSSL_encode(form, ["passwd", "comment_name", "comment_passwd"]) == true)
            return false;
    } catch (e) {}
        
    return true;
}

function board_read_mileage_pop( user_id, tmp )
{
	
	if( !user_id )
	{
		alert( '적립금바로주기는 회원에게만 가능합니다.' );

		return false;
	}

	href = '/admin/php/c/member_admin_d_f.php';
	href += '?user_id=' + user_id + '&mode=3&comment_tmp=' + tmp;

	option = 'toolbar=no location=no scrollbars=yes resizable=yes width=680 height=780';

	window.open( href, '', option  );

}

var isSubmit = false;   // 댓글 작성 submit 여부
var cmtSort = 'DESC';

function comment_submit_check( form, used_name, cmt_sort )
{
    form.target="_self";
    var comment = get_string_trim(form.comment.value);
    form.comment.value = comment;

    if(used_name == 'block')
    {
        form.comment_name.value = get_string_trim(form.comment_name.value);
        form.comment_passwd.value = get_string_trim(form.comment_passwd.value);

        if(form.comment_name.value == '') {
            alert('이름을 입력하세요');
            form.comment_name.focus();
            return false;
        }

        if(form.comment_passwd.value == '') {
            alert('비밀번호를 입력하세요');
            form.comment_passwd.focus();
            return false;
        }
    }

    if(comment == '')
    {
        alert('댓글 내용을 입력하세요');
        form.comment.focus();
        return false;
    }

    if(isSubmit === false) {
        isSubmit = true;

        if (document.getElementById('use_pager') && document.getElementById('use_pager').value == 'T') {
            form.target = 'iComment';
            form.submit();
            setTimeout("document.location.href = document.getElementById('refurl').value;", 1000);
        } else {
            if (cmt_sort != '') {
                cmtSort = cmt_sort;
            }

            var sIsSecret = 'F';

            if (form.is_secret) {
                for (var i=0; i<form.is_secret.length; i++) {
                    if (form.is_secret[i].checked === true) {
                        sIsSecret = form.is_secret[i].value;
                    }
                }
            }

            // SSL
            try {
                var ssl_callback_fn = function(output){
                    parma_str = "&mode=comment_add&board_no="+form.board_no.value+"&comment="+form.comment.value+"&no="+form.no.value+"&is_secret="+sIsSecret+"&"+AuthSSL.key+"="+output;
                    sendRequest(json_comment_insert, parma_str,'POST', '/front/php/b/board_write_a_new.php', true, true);
                };
                if(AuthSSL_encode(form, ["comment_name", "comment_passwd"], ssl_callback_fn) == true)
                    return false;
            } catch (e) {}

            // http
            parma_str = "&mode=comment_add&board_no="+form.board_no.value+"&comment_name="+form.comment_name.value+"&comment_passwd="+form.comment_passwd.value+"&comment="+form.comment.value+"&no="+form.no.value+"&is_secret="+sIsSecret;
            sendRequest(json_comment_insert, parma_str,'POST', '/front/php/b/board_write_a_new.php', true, true);
        }
    } else {
        alert('처리중입니다.');
    }
    
    return false;
}

var commentControlTarget = '';
function getCommentPasswordForm(iCommentIdx)
{
    if (commentControlTarget != '') {
        alert('처리중입니다.');
    } else {
        commentControlTarget = iCommentIdx;
        sParams = "&url=commentPasswordForm&comment_no="+iCommentIdx;
        sendRequest(setCommentPasswordForm, sParams,'POST', '/Front/Board/', true, true);
    }
}

function setCommentPasswordForm(obj)
{
    document.getElementById('comment_control_area_'+commentControlTarget).style.display = '';
    document.getElementById('comment_control_area_'+commentControlTarget).innerHTML = obj.responseText;
    commentControlTarget = '';
}

var secretCommentReadTarget = '';
var secretCommentModifyTarget = '';
function chkCommentPassword(oForm, iCommentIdx, isModifyMode)
{
    if (secretCommentReadTarget != '') {
        alert('처리중입니다.');
    } else {
        var iCommentNo = oForm.comment_no.value;
        var sPassword = oForm.comment_passwd.value;
        secretCommentReadTarget = iCommentIdx;
        if (isModifyMode == 'T') {
            secretCommentModifyTarget = iCommentIdx;
        } else {
            isModifyMode = 'F';
        }

        try {
            // SSL
            var ssl_callback_fn = function(output){
                var sParams = "&url=commentPasswordCheck&isModifyMode="+isModifyMode+"&"+AuthSSL.key+"="+output;
                sendRequest(setSecretCommentContents, sParams, 'POST', '/Front/Board/', true, true);
            };
            if (AuthSSL_encode(oForm, ["comment_no", "comment_passwd"], ssl_callback_fn) == true) {
                return false;
            }
        } catch (e) {}

        // HTTP
        var sParams = "&url=commentPasswordCheck&comment_no="+iCommentNo+"&"+"comment_passwd="+sPassword+"&isModifyMode="+isModifyMode;
        sendRequest(setSecretCommentContents, sParams,'POST', '/Front/Board/', true, true);
    }

    return false;
}

function setSecretCommentContents(obj)
{
    var sChkCode = obj.responseText;
    sChkCode = sChkCode.replace(/[\r\n\s]/, '');
    var aErrorCode = {
            'ERROR_8001' : '해당 댓글의 읽기 권한이 없습니다.',
            'ERROR_8002' : '잘못된 비밀번호입니다.',
            'ERROR_9001' : '잘못된 댓글정보입니다. 관리자에게 문의하여 주세요.'
    };
    
    if (sChkCode == '') {
        alert('알 수 없는 오류입니다. 관리자에게 문의하여 주세요.');
        secretCommentReadTarget = '';
        return false;
    }

    if (aErrorCode[sChkCode]) {
        alert(aErrorCode[sChkCode]);
        secretCommentReadTarget = '';
        return false;
    } else {
        if (document.getElementById('secret_comment_'+secretCommentReadTarget)) {
            document.getElementById('secret_comment_'+secretCommentReadTarget).innerHTML = obj.responseText;
        }
        if (secretCommentModifyTarget == secretCommentReadTarget) {
            getCommentModifyForm(secretCommentModifyTarget);
        } else {
            document.getElementById('comment_control_area_'+secretCommentReadTarget).style.display = 'none';
        }

        secretCommentReadTarget = '';
        secretCommentModifyTarget = '';
        return true;
    }
}

function getCommentModifyForm(iCommentIdx)
{
    if (commentControlTarget != '') {
        alert('처리중입니다.');
    } else {
        commentControlTarget = iCommentIdx;
        sParams = "&url=commentModifyForm&comment_no="+iCommentIdx;
        sendRequest(setCommentModifyForm, sParams,'POST', '/Front/Board/', true, true);
    }
}

function setCommentModifyForm(obj)
{
    var sChkCode = obj.responseText;
    sChkCode = sChkCode.replace(/[\r\n\s]/, '');
    var aErrorCode = {
            'ERROR_8001' : '해당 댓글의 수정 권한이 없습니다.',
            'ERROR_9001' : '잘못된 댓글정보입니다. 관리자에게 문의하여 주세요.'
    };
    
    if (sChkCode == '') {
        alert('알 수 없는 오류입니다. 관리자에게 문의하여 주세요.');
        commentControlTarget = '';
        return false;
    }
    
    if (aErrorCode[sChkCode]) {
        alert(aErrorCode[sChkCode]);
        commentControlTarget = '';
        return false;
    } else {
        document.getElementById('comment_control_area_'+commentControlTarget).style.display = '';
        document.getElementById('comment_control_area_'+commentControlTarget).innerHTML = obj.responseText;
        commentControlTarget = '';
        return true;
    }
}

function setCommentModifyDao(oForm)
{
    if (isSubmit != false) {
        alert('처리중입니다.');
    } else {
        isSubmit = true;

        var iCommentNo = oForm.comment_no.value;
        var sPassword = oForm.comment_passwd.value;
        var sName = oForm.comment_name.value;
        var sContents = oForm.comment.value;

        var sIsSecret = 'F';

        if (oForm.is_secret) {
            for (var i=0; i<oForm.is_secret.length; i++) {
                if (oForm.is_secret[i].checked === true) {
                    sIsSecret = oForm.is_secret[i].value;
                }
            }
        }

        try {
            // SSL
            var ssl_callback_fn = function(output){
                var sParams = "&url=Commentmodify&is_secret="+sIsSecret+"&"+AuthSSL.key+"="+output;
                sendRequest(getCommentModifyResult, sParams, 'POST', '/Front/Board/', true, true);
            };
            if (AuthSSL_encode(oForm, ["comment_no", "comment_passwd", "comment_name", "comment"], ssl_callback_fn) == true) {
                return false;
            }
        } catch (e) {}

        // HTTP
        var sParams = "&url=Commentmodify&comment_no="+iCommentNo+"&comment_passwd="+sPassword+"&comment_name="+sName+"&comment="+sContents+"&is_secret="+sIsSecret;
        sendRequest(getCommentModifyResult, sParams,'POST', '/Front/Board/', true, true);
    }

    return false;
}

function getCommentModifyResult(obj)
{
    var sChkCode = obj.responseText;
    sChkCode = sChkCode.replace(/[\r\n\s]/, '');
    var aErrorCode = {
            'ERROR_0000' : '댓글이 수정되었습니다.',
            'ERROR_8001' : '해당 댓글의 수정 권한이 없습니다.',
            'ERROR_9001' : '잘못된 댓글정보입니다. 관리자에게 문의하여 주세요.',
            'ERROR_9003' : '지정된 댓글이 존재하지 않습니다.',
            'ERROR_9999' : '알 수 없는 오류입니다. 관리자에게 문의하여 주세요.'
    };

    if (aErrorCode[sChkCode]) {
        alert(aErrorCode[sChkCode]);
        isSubmit = false;
        if (sChkCode == 'ERROR_0000') {
            // 수정 성공시 reload
            location.reload();
        }
        return false;
    } else {
        alert(aErrorCode['ERROR_9999']);
        isSubmit = false;
        return false;
    }
    return false;
}

function get_string_trim( string )
{
    return string.replace(/(^\s*)|(\s*$)/g, "");
}

function get_string_length( string )
{
    var count = 0;

    var tmp_str = new String(string);
    var temp = tmp_str.length;

    var onechar;

    for ( var k=0; k<temp; k++ )
    {
        onechar = tmp_str.charAt(k);

        if (escape(onechar).length > 4)
        {
            count += 2;
        }
        else
        {
            count += 1;
        }
    }

    return count;
}

function str_limit_check( obj )
{
	comment = obj.value;
	max_len = 600;

	content_length = get_string_length( comment );

	if( typeof( document.all['cbyte_span'] ) == "object" )
	{
		document.all['cbyte_span'].innerHTML = content_length;
	}

/*	if( content_length > max_len )
	{
		msg = '메시지는 ' + max_len + ' Byte 이하로 입력해주세요.';
		alert( msg );
		obj.value = comment.substr(0, max_len);

		if( typeof( document.all['cbyte_span'] ) == "object" )
		{
			document.all['cbyte_span'].innerHTML = max_len;
		}
	}*/
}

function board_copy( form )
{
    msg = "글 복사시 첨부파일 및 이미지는 원본글과 같이 사용하게 되므로 글삭제시 주의하시기 바랍니다. \r\r복사하시겠습니까?";

    if( !confirm( msg ) )
    {
         return false;
    }
    else
    {
        form.mode.value = 'copy_board';
        return true;
    }
}


function json_comment_insert(oj) {
    try {
        eval("var res = " + oj.responseText);
        
	    comment_tpl = document.getElementById('div_comment_tpl').value;
		comment_tpl = comment_tpl.replace(/\[ajax_comment_no\]/gi,res.Static[0].comment_no);
	    comment_tpl = comment_tpl.replace(/\[ajax_comment_cnt\]/gi,res.Static[1].comment_cnt);
	    comment_tpl = comment_tpl.replace(/\[ajax_comment_name\]/gi,res.Static[2].comment_name);
	    comment_tpl = comment_tpl.replace(/\[ajax_comment\]/gi,document.getElementById('comment').value.replace(/\r\n/ig, '<br/>'));
	    comment_tpl = comment_tpl.replace(/\[ajax_comment_member_id\]/gi,res.Static[3].comment_member_id);
		comment_tpl = comment_tpl.replace(/\[ajax_comment_write_date\]/gi,res.Static[4].comment_write_date);
		comment_tpl = comment_tpl.replace(/\[ajax_comment_group_icon\]/gi,res.Static[5].comment_group_icon);
	    
	    document.getElementById('comment').value="";
	    
	    ret_comment_tpl = comment_tpl;
	    if (cmtSort == 'DESC') {
	        ret_comment_tpl = ret_comment_tpl + document.getElementById('div_comment_list').innerHTML;
	    } else {
	        ret_comment_tpl = (document.getElementById('div_comment_list').innerHTML)+ret_comment_tpl;
	    }
	    document.getElementById('div_comment_list').innerHTML=ret_comment_tpl;
	    
	    if(document.getElementById('blog_ck').value=="T") {
	        self.resizeTo(document.body.scrollWidth, document.body.scrollHeight);
	    }
	    
	    alert('등록되었습니다.');
    } catch(e) {
        alert ('댓글 갱신에 실패하였습니다.');        
    }
    
    isSubmit = false;
}

function view_crm(isAdmin, member_id)
{
	if (isAdmin != 'T') return false;

	if (member_id != '') {
		window.open('/Admin/Member/Crm/?sMemberID=' + member_id, member_id, 'width=900,height=700,titlebar=no,scrollbars=no,resizable=yes,menubar=no,toolbar=no');
	}
}

function recommend(no)
{
    var aPoint = document.getElementsByName('point');
    var point = 0;
    
    for (i = 0; i < aPoint.length; i++) {
        if (aPoint[i].checked) {
            point = aPoint[i].value;
        }
    }
    
    if (point < 1) {
        alert('점수를 선택하세요.');
        return false;
    }
    
    location.href = './?url=recommend&point=' + point + '&no=' + no;
}
