/*******************************************************************************
	Description		: GENERAL Script
	Last Modified	: 2005.01.25
*******************************************************************************/
/*******************************************************************************
	CHECKBOX script
*******************************************************************************/
function check_option() {
	// check option layer
	var obj = document.getElementById("id_checkbox");

	// set position
	obj.style.posLeft = event.x + document.body.scrollLeft - 8;
	obj.style.posTop = event.y + document.body.scrollTop - 7;
	obj.style.visibility = "visible";
}

function show_check_option() {
	document.getElementById("id_checkbox").style.visibility = "visible";
}

function hide_check_option() {
	document.getElementById("id_checkbox").style.visibility = "hidden";
}

function select_all() {
	for(var i = 1; i <= ARTICLE_ROWS; i++) {
		var obj = document.getElementById("check" + i);

		if(obj) {
			obj.value = document.getElementById("article" + i + "_idx").value;
			document.getElementById("check_img" + i).src = PATH_SKIN + "img/chkbox_on.gif";
		}
	}

	hide_check_option();
}

function cancel_all() {
	for(var i = 1; i <= ARTICLE_ROWS; i++) {
		var obj = document.getElementById("check" + i);

		if(document.getElementById("check" + i)) {
			obj.value = 0;
			document.getElementById("check_img" + i).src = PATH_SKIN + "img/chkbox_off.gif";
		}
	}

	hide_check_option();
}

function reverse_chk() {
	for(var i = 1; i <= ARTICLE_ROWS; i++) {
		if(document.getElementById("check" + i)) set_check(i);
	}

	hide_check_option();
}

function set_check(row_num) {
	var obj = document.getElementById("check" + row_num);
	var idx = document.getElementById("article" + row_num + "_idx").value;

	if(obj.value == idx) {
		obj.value = 0;
		document.getElementById("check_img" + row_num).src = PATH_SKIN + "img/chkbox_off.gif";
	} else {
		obj.value = idx;
		document.getElementById("check_img" + row_num).src = PATH_SKIN + "img/chkbox_on.gif";
	}
}

/*******************************************************************************
	PREVIEW script
*******************************************************************************/
function preview(content) {
	var obj = document.getElementById("id_preview");

	// check content
	if(content == "") {
		obj.innerHTML = "";
		return;
	}

	var text = "";
	text =  "<table width='100%' border='0' cellspacing='0' cellpadding='0'>"
	text += "    <tr>";
	text += "        <td>";
	text += "            <td bgcolor='#FFFFFF' style='word-break:break-all; padding:5px;'><xmp>" + content + "</xmp></td>";
	text += "        </td>";
	text += "    </tr>";
	text += "</table>";

	obj.innerHTML = text;
	move_preview();
	obj.style.visibility = "visible";
}

function move_preview() {
	var obj = document.getElementById("id_preview");

	if(obj.innerHTML != "") {
		obj.style.posLeft = event.x - 40 + document.body.scrollLeft;
		obj.style.posTop = event.y + 20 + document.body.scrollTop;
	}
}

function hide_preview() {
	document.getElementById("id_preview").style.visibility = "hidden";
}

/*******************************************************************************
	USER_MENU script
*******************************************************************************/
function mdownMenu(evt)
{
   var obj = document.getElementById("id_user_menu");
   obj.style.left = evt.pageX-10;
   obj.style.top  = evt.pageY-10;		
}
function user_menu(id, name, email, url) {
	// user menu layer
	var obj = document.getElementById("id_user_menu");

	if(navigator.appName == "Netscape"){
	   window.document.captureEvents(Event.MOUSEDOWN);
	   window.document.onmousedown  =mdownMenu;

	  } else {
	   obj.style.posLeft = event.x+ document.body.scrollLeft;
	   obj.style.posTop  = event.y+ document.body.scrollTop;
	  }


	var text = "";
	text += "";
	text += "<table width='100' border='0' cellspacing='0' cellpadding='0' align='center'>";
	text += "	<tr>";
	text += "		<td class='barcolor' height='12' style='padding-left:5px'><img src='" + PATH_SKIN + "img/user_menu_title.gif' width='59' height='5'></td>";
	text += "	</tr>";
	text += "	<tr>";
	text += "		<td bgcolor='#FFFFFF'>";
	text += "			<table width='100%' border='0' cellspacing='0' cellpadding='0' class='bordertb'>";

	// id or name
	if(id != "") {
		text += "				<tr height='18' onMouseOver=\"this.style.backgroundColor='#F5F5F5'\" onMouseOut=\"this.style.backgroundColor=''\">";
		text += "					<td width='5'></td>";
		text += "					<td align='center' width='20'><a href=\"javascript:search_by_id('" + id + "');\"><img src='" + PATH_SKIN + "img/usermenu_search.gif' border=0 width='11' height='11'></a></td>";
		text += "					<td><a href=\"javascript:search_by_id('" + id + "');\" class='menu' title='¾ÆÀÌµð·Î °Ë»ö'>Search Id</a></td>";
		text += "					<td width='5'></td>";
		text += "				</tr>";
		text += "				<tr height='18' onMouseOver=\"this.style.backgroundColor='#F5F5F5'\" onMouseOut=\"this.style.backgroundColor=''\">";
		text += "					<td width='5'></td>";
		text += "					<td align='center' width='20'><a href=\"javascript:search_by_name('" + name + "');\"><img src='" + PATH_SKIN + "img/usermenu_search.gif' border=0 width='11' height='11'></a></td>";
		text += "					<td><a href=\"javascript:search_by_name('" + name + "');\" class='menu' title='ÀÌ¸§À¸·Î °Ë»ö'>Search Name</a></td>";
		text += "					<td width='5'></td>";
		text += "				</tr>";
	} else {
		text += "				<tr height='18' onMouseOver=\"this.style.backgroundColor='#F5F5F5'\" onMouseOut=\"this.style.backgroundColor=''\">";
		text += "					<td width='5'></td>";
		text += "					<td align='center' width='20'><a href=\"javascript:search_by_name('" + name + "');\"><img src='" + PATH_SKIN + "img/usermenu_search.gif' border=0 width='11' height='11'></a></td>";
		text += "					<td><a href=\"javascript:search_by_name('" + name + "');\" class='menu' title='ÀÌ¸§À¸·Î °Ë»ö'>Search Name</a></td>";
		text += "					<td width='5'></td>";
		text += "				</tr>";
	}
	text += "			</table>";
	text += "		</td>";
	text += "	</tr>";
	text += "	<tr>";
	text += "		<td class='barcolor' height='12' style='padding-right:5px; align='right' onClick='javascript:hide_user_menu();'><img src='" + PATH_SKIN + "img/user_menu_close.gif' width='29' height='5' border='0'></td>";
	text += "	</tr>";
	text += "</table>";

	obj.innerHTML = text;
	obj.style.visibility = "visible";
}

function show_user_menu() {
	document.getElementById("id_user_menu").style.visibility = "visible";
}

function hide_user_menu() {
	document.getElementById("id_user_menu").style.visibility = "hidden";
}

function search_by_id(id) {
	location.href = "./bbs.php?idx=" + QUERY + "&s_mode=def&s_option=1&s_key=" + id;
	hide_user_menu();
}

function search_by_name(name) {
	location.href = "./bbs.php?idx=" + QUERY + "&s_mode=def&s_option=2&s_key=" + name;
	hide_user_menu();
}

function open_url(url) {
	if(url == "") return;
	window.open(url, "", "");
}

function user_info(id) {
	profile(id, 1, "");
}

/*******************************************************************************
	SEARCH script
*******************************************************************************/
function set_search(field) {
	var obj_field = eval("search_form.s_"+field); 

	if(obj_field.value == 1) {
		document.getElementById("s_"+field+"_chk").src = PATH_SKIN + "img/search_" + field + ".gif";
		obj_field.value = 0;
	} else {
		document.getElementById("s_"+field+"_chk").src = PATH_SKIN + "img/search_" + field + "-1.gif";
		obj_field.value = 1;
	}
}

function set_search_num() {
	var total_num = SEARCH_TOTAL_COUNT;
	var search_num = search_form.s_num.value;

	if(search_num != '') {
		if(search_num > total_num) {
			search_num = total_num;
			search_form.s_num.value = total_num;
		}

		// show search field
		for(var i = 1; i <= total_num; i++) {
			if(i <= search_num)
				document.getElementById("id_search" + i).style.display = "block";
			else
				document.getElementById("id_search" + i).style.display = "none";
		}
	}
}

function show_adv_search() {
	document.getElementById("id_default_search").style.display = "none";
	document.getElementById("id_adv_search").style.display = "block";
}

function show_default_search() {
	document.getElementById("id_default_search").style.display = "block";
	document.getElementById("id_adv_search").style.display = "none";
}

function search(mode) {
	if(mode == "default") {
		if(search_form.s_key.value == "")  {
			alert("°Ë»ö¾î¸¦ ÀÔ·ÂÇØ ÁÖ½Ê½Ã¿À.");
			search_form.s_key.focus();
			return;
		}

		search_form.action = "./bbs.php?idx=" + QUERY+"&s_mode=def";;
	} else if(mode == "advanced") {
		// check search key
		for(var i = 1; i <= search_form.s_num.value; i++) {
			var obj = document.getElementById("s_key" + i);

			if(obj.value == "") {
				alert("°Ë»ö¾î¸¦ ÀÔ·ÂÇØ ÁÖ½Ê½Ã¿À.");
				obj.focus();
				return;
			}
		}

		search_form.action = "./bbs.php?idx=" + QUERY_DB + "&s_mode=adv";
	}

	search_form.submit();
}

/*******************************************************************************
	COMMENT script
*******************************************************************************/
function add_comment(idx) {
	
	if(document.getElementById("cmt"+idx+"_name").value == "") {
		alert("ÀÌ¸§À» ½á ÁÖ¼¼¿ä");
		document.getElementById("cmt"+idx+"_name").focus();
		return;
	}

	if(MEMBER_IDX == 0) {
		// ·Î±×ÀÎÇÑ °æ¿ì´Â ºñ¹Ð¹øÈ£¸¦ ¹ÞÁö¾ÊÀ½.
		if(document.getElementById("cmt"+idx+"_pwd").value == "") {
			alert("ºñ¹Ð¹øÈ£À» ½á ÁÖ¼¼¿ä");
			document.getElementById("cmt"+idx+"_pwd").focus();
			return;
		}
	}

	if(document.getElementById("cmt"+idx+"_content").value == "") {
		alert("³»¿ëÀ» ½á ÁÖ¼¼¿ä");
		document.getElementById("cmt"+idx+"_content").focus();
		return;
	}

	// set value
	document.frmCom.comment_name.value = document.getElementById("cmt"+idx+"_name").value;
	if(MEMBER_IDX == 0) {
		frmCom.comment_pwd.value = document.getElementById("cmt"+idx+"_pwd").value;
	}
	document.frmCom.comment_content.value = document.getElementById("cmt"+idx+"_content").value;

	document.frmCom.action = "./bbs.php?idx=" + QUERY + "&uid=" + idx;
	document.frmCom.act_fn.value = "read_skin";
	document.frmCom.act_cat.value = 1;
	document.frmCom.mode.value = "add_comm";
	document.frmCom.submit();
}

function show_comment_pwd(article_idx, comment_idx) {
	if(MEMBER_IDX == 0) {
		// before login
		var obj = document.getElementById("id_comment_pwd");

		if(article_idx != 0 && comment_idx != 0) {
			document.frmCom.del_pwd.value = "";
			document.frmCom.article_idx.value = article_idx;
			document.frmCom.comment_idx.value = comment_idx;

			obj.style.posLeft = event.x - 170 + document.body.scrollLeft;
			obj.style.posTop = event.y + document.body.scrollTop;
		}

		obj.style.visibility = "visible";
		document.frmCom.del_pwd.focus();
	} else {
		// after login
		document.frmCom.del_pwd.value = "";
		document.frmCom.article_idx.value = article_idx;
		document.frmCom.comment_idx.value = comment_idx;

		del_comment();
		event.returnValue = false;
	}
}

function hide_comment_pwd() {
	document.getElementById("id_comment_pwd").style.visibility = "hidden";
}

function del_comment() {
	var pwd = frmCom.del_pwd.value;

	hide_comment_pwd();
	frmCom.action = "./bbs.php?idx=" + QUERY + "&pwd=" + pwd;
	frmCom.act_cat.value = 1;
	frmCom.act_fn.value = "read_skin";
	frmCom.mode.value = "del";
	frmCom.submit();
}

// comment resize
function resize_comment(idx, height) {
	obj = document.getElementById("cmt" + idx + "_content");
	if(obj) {
		var obj_height = parseInt(obj.rows);
		if(obj_height + height >= 3)
			obj.rows = obj_height + height;
	}
}

function cmt_mdel(idx) {
	var obj = document.frmNm.elements["cmt" + idx + "_idx[]"];
	if(!obj) return;
	
	// article index
	document.frmCom.article_idx.value = idx;

	// comment index
	var cmt_idx = document.frmCom.comment_idx;
	cmt_idx.value = "";

	if(obj.length) {
		// more than two objects
		for(var i = 0; i < obj.length; i++) {
			if(obj[i].checked == true) {
				if(cmt_idx.value != "") cmt_idx.value += ",";
				cmt_idx.value += obj[i].value;
			}
		}
	} else {
		// only one object
		cmt_idx.value = obj.value;
	}

	document.frmCom.action = "./bbs.php?idx=" + QUERY;
	document.frmCom.act_cat.value = 1;
	document.frmCom.act_fn.value = "read_skin";
	document.frmCom.mode.value = "del";
	document.frmCom.submit();
}
/*******************************************************************************
	WRITE script
*******************************************************************************/
String.prototype.trim = function()
{
	return this.replace(/^ *| *$/g, "");
}

function verify_data() {

	// check name
	if(document.frmWrite.name.value == "" || document.frmWrite.name.value.trim().length ==  0) {
		alert("ÀÌ¸§À» ÀÔ·ÂÇØÁÖ¼¼¿ä.");
		document.frmWrite.name.focus();
		return;
	}
	// check email
	if(document.frmWrite.email.value != "") {
		if(!check_email(document.frmWrite.email.value)) {
			alert("ÀÌ¸ÞÀÏ Çü½ÄÀÌ ¿Ã¹Ù¸£Áö ¾Ê½À´Ï´Ù.");
			document.frmWrite.email.focus();
			return;
		}
	}
	// check password
	if(WRITE_MODE != "modify" && MEMBER_IDX == 0) {
		if(document.frmWrite.pwd && document.frmWrite.pwd.value == "") {
			alert("ÆÐ½º¿öµå¸¦ ÀÔ·ÂÇØÁÖ¼¼¿ä.");
			document.frmWrite.pwd.focus();
			return;
		}
	}
	// check title
	if(document.frmWrite.title.value == "" || document.frmWrite.title.value.trim().length ==  0) {
		alert("Á¦¸ñÀ» ÀÔ·ÂÇØÁÖ¼¼¿ä.");
		document.frmWrite.title.focus();
		return;
	}
	// check content
	if(document.frmWrite.content.value == "" || document.frmWrite.content.value.trim().length ==  0) {
		alert("³»¿ëÀ» ÀÔ·ÂÇØÁÖ¼¼¿ä.");
		document.frmWrite.content.focus();
		return;
	}
	// set content info
//	if(frmWrite.content_width)
//		frmWrite.content_width.value = parseInt(document.getElementById('content').style.width);
//	if(frmWrite.content_height)
//		frmWrite.content_height.value = parseInt(document.getElementById('content').style.height);
	document.frmWrite.action = "./bbs.php?idx="+QUERY+"&mode="+WRITE_MODE+"&uid="+uid;
	document.frmWrite.act_cat.value="1";
	document.frmWrite.act_fn.value="write_skin";
	document.frmWrite.scodekey.value= CODE;
	document.frmWrite.submit();
}

// resize
function resize_content_width(width) {
	var obj, total_count;

	// url
	resize_object_width("url", width, CONTENT_WIDTH);

	// title
	resize_object_width("title", width, CONTENT_WIDTH);

	// content
	resize_object_width("content", width, CONTENT_WIDTH);

	// file
	obj = document.getElementById('file_num');
	if(obj) {
		total_count = obj.options[obj.length-1].value;
		for(var i = 1; i <= total_count; i++) {
			resize_object_width("file"+i, width, FILE_WIDTH);
		}
	}

	// link
	obj = document.getElementById('link_num');
	if(obj) {
		total_count = obj.options[obj.length-1].value;
		for(var i = 1; i <= total_count; i++) {
			resize_object_width("link"+i, width, FILE_WIDTH);
		}
	}
}

function resize_object_width(obj, width, min_width) {
	if(document.getElementById(obj)) {
		var obj_width = parseInt(document.getElementById(obj).style.width);
		if(obj_width + width >= min_width)
			document.getElementById(obj).style.width = obj_width + width;
	}
}

function resize_content_height(height) {
	if(document.getElementById("content")) {
		var obj_height = parseInt(document.getElementById('content').style.height);
		if(obj_height + height >= CONTENT_HEIGHT)
			document.getElementById('content').style.height = obj_height + height;
	}
}

// file & link
function show_file() {
	var obj = document.frmWrite.file_num;
	var file_num = obj.options[obj.selectedIndex].value;
	var total_file = obj.options[obj.length-1].value;

	for(var i = 1; i <= total_file; i++) {
		var obj_file = document.getElementById("id_file" + i);
		var obj_info = document.getElementById("id_file" + i + "_info");

		if(i <= file_num) {
			obj_file.style.display = 'block';
			if(obj_info) obj_info.style.display = 'block';
		} else {
			obj_file.style.display = 'none';
			if(obj_info) obj_info.style.display = 'none';
		}
	}
}

function show_link() {
	var obj = frmWrite.link_num;
	var link_num = obj.options[obj.selectedIndex].value;
	var total_link = obj.options[obj.length-1].value;

	for(var i = 1; i <= total_link; i++) {
		if(i <= link_num)
			document.getElementById("id_link" + i).style.display = 'block';
		else
			document.getElementById("id_link" + i).style.display = 'none';
	}
}

function add_var(id, idx) {
	if(id == 'link') {
		document.getElementById("content").value += "{LINK:" + idx + "}";
	} else if(id == 'file') {
		document.getElementById("content").value += "{FILE:" + idx + "}";
	}
}

/*******************************************************************************
	ACTION script
*******************************************************************************/
// rollover bgcolor.
function rollover(obj, color) {
	if(!obj) return false;
	obj.style.backgroundColor = color;
}

function check_enter(id) {
	if(event.keyCode == 13) {
		if(id == "search") search('default');
		else if(id == "jump_page") jump_to();
		else if(id == "del_comment") del_comment();

		event.returnValue = false;
	}
}

function read_article(uid) {
	if(listTitleOnClick == 1) {
		location.href = "./bbs.php?fn=read_skin&uid="+ uid+"&idx="+QUERY;
	} else if(listTitleOnClick == 2) {
		window.open("./bbs.php?fn=popup_skin&idx=" + QUERY + "&uid=" + uid,
			"", "width="+LIST_POPUP_WIDTH+", height="+LIST_POPUP_HEIGHT+", scrollbars=1, resizable=1");
	} else if(listTitleOnClick == 3) {
		window.open("./bbs.php?fn=popup_skin&idx=" + QUERY + "&uid=" + uid,
			"", "width="+LIST_POPUP_WIDTH+", height="+LIST_POPUP_HEIGHT+", scrollbars=1, resizable=1");
	}
}

function multi_read() {
	var idx = "";

	// make index.
	for(var i = 1; i <= ARTICLE_ROWS; i++) {
		var obj = document.getElementById("check" + i);
		if(obj && obj.value != 0) {
			// check secret article
			if(document.getElementById("article" + i + "_secret").value == 1) {

				alert("ºñ¹Ð °Ô½Ã¹°Àº ´ÙÁßÀÐ±â¸¦ ÇÏ½Ç ¼ö ¾ø½À´Ï´Ù.");
				return;
			}

			if(idx != "") idx += ",";
			idx += obj.value;
		}
	}

	if(idx == "") {
		alert("ÀÐÀ» °Ô½Ã¹°À» ¼±ÅÃÇØÁÖ½Ê½Ã¿À.");
		return;
	}

	location.href = "./bbs.php?fn=read_skin&uid=" + idx +"&idx="+QUERY;
}

function article_manage() {
	var uid = "";

	// make index.
	for(var i = 1; i <= ARTICLE_ROWS; i++) {
		var obj = document.getElementById("check" + i);
		if(obj && obj.value != 0) {
			if(uid != "") uid += ",";
			uid += obj.value;
		}
	}
	if(uid == "") {
		alert("°Ô½Ã¹°À» ¼±ÅÃÇØÁÖ¼¼¿ä.");
		return;
	}

	var url = "/dtboard/admin/main/bbs_pop/article_manage.php?bid="+DB+"&uid="+uid;
	window.open(url, "", "width=450, height=160, resizable=1");
}

function reply_article(uid) {
	document.hform.action = "./bbs.php?fn=write_skin&idx=" + QUERY +"&uid="+uid;
	document.hform.mode.value="reply";
	document.hform.act_fn.value="write_skin";
	document.hform.submit();
}

function modify_article(uid) {
	document.hform.action = "./bbs.php?fn=write_skin&idx=" + QUERY +"&uid="+uid;
	document.hform.mode.value="modify";
	document.hform.submit();
}

function delete_article(uid) {
	if(confirm("°Ô½Ã¹°À» »èÁ¦ÇÏ½Ã°Ú½À´Ï±î?")) {
		document.hform.action = "./bbs.php?idx=" + QUERY +"&uid="+uid;
		document.hform.act_cat.value="1";
		document.hform.act_fn.value="read_skin";
		document.hform.mode.value="article_del";
		document.hform.submit();
	}
}

function recommend(uid) {
	document.hform.action = "./bbs.php?idx=" + QUERY +"&uid="+uid;
	document.hform.act_cat.value="1";
	document.hform.act_fn.value="read_skin";
	document.hform.mode.value="recommend";
	document.hform.submit();
}

function jump_to() {
	var obj = document.getElementById("jump_page");
	var page = obj.value;
	var last_page = document.getElementById("last_page").value;

	if(!check_number(page)) {
		alert("page¿¡´Â ¼ýÀÚ¸¸ ¾²½Ç ¼ö ÀÖ½À´Ï´Ù.");
		obj.value = "";
		obj.focus();
		return;
	}

	if(page != "") {
		if(parseInt(page) < 1) {
			alert("ÆäÀÌÁö´Â 1º¸´Ù Ä¿¾ßÇÕ´Ï´Ù.");
			obj.value = "";
			obj.focus();
			return;
		} else if(parseInt(page) > parseInt(last_page)) {
			alert(page + " ÆäÀÌÁö´Â Á¸ÀçÇÏÁö ¾Ê´Â ÆäÀÌÁöÀÔ´Ï´Ù.");
			obj.value = "";
			obj.focus();
			return;
		} else {
			location.href = "./bbs.php?page="+page+"&idx="+idx;
		}
	}
}
