/* 의견글 보여주기 */
var showComment = function(po_idx) {
	$('#cmtContainer_' + po_idx).load(root_path + '/ajax/showComment.php', {table: table, po_idx: po_idx});
};
/* 의견글 쓰기 */
// 비회원용 미리보기
var changeCmtIcon = function(po_idx, select) {
	document.getElementById('cmtAddPreview_'+po_idx).src = root_path+'/images/icon/'+select.value;
};
// 쓰기
var addComment = function(po_idx) {
	var content = document.getElementById('cmtAddContent_'+po_idx);
	if(!content.value) { alert('의견글 내용을 입력해주세요.'); content.focus(); return; }

	// 비회원용 처리를 포함
	var nick = document.getElementById('cmtAddNick_'+po_idx), pwd = document.getElementById('cmtAddPwd_'+po_idx), icon = document.getElementById('cmtAddIcon_'+po_idx);
	if(!nick) {
		nick = {value: null};
		pwd = {value: null};
		icon = {value: null};
	} else {
		if(!nick.value) { alert('닉네임을 입력해주세요.'); nick.focus(); return; }
		if(!pwd.value) { alert('비밀번호를 입력해주세요.\n수정 및 삭제시 사용됩니다.'); pwd.focus(); return; }
	}

	$.post(root_path + '/ajax/addComment.php', {table: table, po_idx: po_idx, content: content.value, nick: nick.value, pwd: pwd.value, icon: icon.value}, addCommentExec, 'json');
};
var addCommentExec = function(res) {
	if(res.error) alert(res.error);
	else {
		var po_idx = res.po_idx;
		document.getElementById('cmtAddContent_'+po_idx).value = '';
		if(document.getElementById('cmtAddNick_'+po_idx)) document.getElementById('cmtAddNick_'+po_idx).value = '';
		if(document.getElementById('cmtAddPwd_'+po_idx)) document.getElementById('cmtAddPwd_'+po_idx).value = '';

		showComment(po_idx);
		if(res.message) MoePopup.show(res.message);
	}
};

// 수정
// 1. 통신 후 회원의 댓글이면 권한이 있는 지 확인, 비회원의 댓글이면 비번 입력 창을 띄움
// 2. 편집 모드로 변경
// 3. 최종 확인이 되면 댓글 전체를 리로드
var editComment = function(cm_idx) {
	var parameters = {table: table, cm_idx: cm_idx};

	// 비회원 - 비밀번호 입력 창이 있을 경우
	var pwd = document.getElementById('cmtEditPwd_'+cm_idx);
	if(pwd) {
		if(!pwd.value) { alert('비밀번호를 입력해주세요'); pwd.focus(); return; }
		else parameters.pwd = pwd.value;
	}

	// 이미 수정중인 경우
	var content = document.getElementById('cmtEditInput_'+cm_idx);
	if(content) {
		if(!content.value) { alert('의견글 내용을 입력해주세요'); content.focus(); return; }
		else parameters.content = content.value;
	}

	$.post(root_path + '/ajax/editComment.php', parameters, editCommentCheck, 'json');
};
var editCommentCheck = function(res) {
	if(res.error) { alert(res.error); return; }

	if(res.mode == 'success') showComment(res.po_idx);
	else if(res.mode == 'edit') {
		res.content = res.content.replace(/&/g, '&amp;');
		document.getElementById('cmtMessage_'+res.cm_idx).innerHTML = '';
		document.getElementById('cmtContent_'+res.cm_idx).innerHTML = '<textarea id="cmtEditInput_'+res.cm_idx+'" cols="30" rows="4">'+res.content+'</textarea><br><input type="button" value="수정" onclick="editComment('+res.cm_idx+')"> <input type="button" value="취소" onclick="showComment('+res.po_idx+')">';
	}
	else if(res.mode == 'guest') {
		document.getElementById('cmtMessage_'+res.cm_idx).innerHTML = '의견글을 수정하시려면 비밀번호를 입력해주세요 :: <input type="password" id="cmtEditPwd_'+res.cm_idx+'"> <input type="button" value="수정" onclick="editComment('+res.cm_idx+')"> <input type="button" value="취소" onclick="showComment('+res.po_idx+')">';
	}
	if(res.message) MoePopup.show(res.message);
};

// 제거
var removeComment = function(cm_idx) {
	var pwd = document.getElementById('cmtRemovePwd_'+cm_idx);
	var parameters = {table: table, cm_idx: cm_idx};
	if(pwd) parameters.pwd = pwd.value;
	$.post(root_path + '/ajax/removeComment.php', parameters, removeCommentCheck, 'json');
};
var removeCommentCheck = function(res) {
	if(res.error) { alert(res.error); return; }
	// 삭제가 완료되었으면 바로 리스트를 보여준다
	if(res.result == 'success') showComment(res.po_idx);
	else if(res.result == 'guest') {
		document.getElementById('cmtMessage_'+res.cm_idx).innerHTML = '의견글을 제거하시려면 비밀번호를 입력해주세요 :: <input type="password" id="cmtRemovePwd_'+res.cm_idx+'"> <input type="button" value="제거" onclick="removeComment('+res.cm_idx+')"> <input type="button" value="취소" onclick="showComment('+res.po_idx+')">';
	}
	if(res.message) MoePopup.show(res.message);
};