/*
 * TTBOARD 2002 BOARD SKIN
 * Description		: GENERAL Script
 * Last Modified	: 2002/09/24
 */

/*************************************************************************
	CHECKBOX script
*************************************************************************/
function check_option() {
	// check option layer
	var obj = document.all["id_checkbox"];

	// set position
	obj.style.posLeft = event.x + document.body.scrollLeft;
	obj.style.posTop = event.y + document.body.scrollTop;
	obj.style.visibility = "visible";
}

function show_check_option() {
	document.all["id_checkbox"].style.visibility = "visible";
}

function hide_check_option() {
	document.all["id_checkbox"].style.visibility = "hidden";
}

function select_all() {
	for(var i = 1; i <= ARTICLE_ROWS; i++) {
		var obj = document.all["check" + i];

		if(obj) {
			obj.value = document.all["article" + i + "_idx"].value;
			document.all["check_img" + i].src = PATH_SKIN + "images/chkbox_on.gif";
		}
	}

	hide_check_option();
}

function cancel_all() {
	for(var i = 1; i <= ARTICLE_ROWS; i++) {
		var obj = document.all["check" + i];

		if(document.all["check" + i]) {
			obj.value = 0;
			document.all["check_img" + i].src = PATH_SKIN + "images/chkbox_off.gif";
		}
	}

	hide_check_option();
}

function reverse_chk() {
	for(var i = 1; i <= ARTICLE_ROWS; i++) {
		if(document.all["check" + i]) set_check(i);
	}

	hide_check_option();
}

function set_check(row_num) {
	var obj = document.all["check" + row_num];
	var idx = document.all["article" + row_num + "_idx"].value;

	if(obj.value == idx) {
		obj.value = 0;
		document.all["check_img" + row_num].src = PATH_SKIN + "images/chkbox_off.gif";
	} else {
		obj.value = idx;
		document.all["check_img" + row_num].src = PATH_SKIN + "images/chkbox_on.gif";
	}
}

function init_check(row_num, idx) {
	if(document.all["check" + row_num].value == idx)
		document.all["check_img" + row_num].src = PATH_SKIN + "images/chkbox_on.gif";
	else
		document.all["check_img" + row_num].src = PATH_SKIN + "images/chkbox_off.gif";
}

/*************************************************************************
	PREVIEW script
*************************************************************************/
function preview(content) {
	var obj = document.all["id_preview"];

	// check content
	if(content == "") {
		obj.innerHTML = "";
		return;
	}

	var text = "";
	text =  "<table width='100%' border='0' cellspacing='0' cellpadding='0' align='center'>"
	text += "    <tr>";
	text += "        <td>";
	text += "            <table width='100%' border='0' cellspacing='0' cellpadding='0' align='center'>";
	text += "                <tr>";
	text += "                    <td height='10' width='10'><img src='" + PATH_SKIN + "img/pre_tl.gif' width='10' height='10'></td>";
	text += "                    <td background='" + PATH_SKIN + "img/pre_tm.gif'><img src='" + PATH_SKIN + "img/pre_tm.gif' width='1' height='10'></td>";
	text += "                    <td height='10' width='1'><img src='" + PATH_SKIN + "img/pre_tr.gif' width='1' height='10'></td>";
	text += "                </tr>";
	text += "            </table>";
	text += "            <table width='100%' border='0' cellspacing='0' cellpadding='5' align='center'>";
	text += "                <tr>";
	text += "                    <td colspan=3 bgcolor='#ffffff' style='word-break:break-all;' style='border-bottom: 1px solid #993243;border-right: 1px solid #993243;border-left: 1px solid #993243 ' >" + content + "</td>";
	text += "                </tr>";
	text += "            </table>";
	text += "        </td>";
	text += "    </tr>";
	text += "</table>";

	obj.innerHTML = text;
	move_preview();
	obj.style.visibility = "visible";
}

function move_preview() {
	var obj = document.all["id_preview"];

	if(obj.innerHTML != "") {
		obj.style.posLeft = event.x - 40 + document.body.scrollLeft;
		obj.style.posTop = event.y + 10 + document.body.scrollTop;
	}
}

function hide_preview() {
	document.all["id_preview"].style.visibility = "hidden";
}

/*************************************************************************
	USER_MENU script
*************************************************************************/
function user_menu(id, name, email, url) {
	// user menu layer
	var obj = document.all["id_user_menu"];

	if(obj.style.visibility == "hidden") {
		// set position
		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' style='border: 1px solid #666666'>";
		text += "	<tr>";
		text += "		<td bgcolor='#FFFFFF'>";
		text += "			<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
		text += "				<tr>";
		text += "					<td width='5'><img src='" + PATH_SKIN + "images/pmenu/box_tl.gif' width=5 height=11></td>";
		text += "					<td background='" + PATH_SKIN + "images/pmenu/box_tr2.gif'><img src='" + PATH_SKIN + "images/pmenu/box_tl1.gif' width=10 height=11></td>";
		text += "					<td align='right' width='5'><img src='" + PATH_SKIN + "images/pmenu/box_tr.gif' width=5 height=11></td>";
		text += "				</tr>";
		text += "				<tr>";
		text += "					<td background='" + PATH_SKIN + "images/pmenu/box_ml.gif'></td>";
		text += "				    <td>";
		text += "						<table width='100%' border='0' cellspacing='0' cellpadding='0' class='text8pt' style='border: 1px solid #666666'>";

		// id or name
		text += "							<tr height='20' onMouseOver=\"this.style.backgroundColor='#EFEFEF'\" onMouseOut=\"this.style.backgroundColor=''\">";
		text += "								<td width='5'></td>";
		if(id != "") {
			text += "				        		<td width='20'><a href=\"javascript:search_by_name('" + id + "');\"><img src='" + PATH_SKIN + "images/pmenu/usermenu_search.gif' border=0 width='13' height='11'></a></td>";
			text += "				        		<td><a href=\"javascript:search_by_name('" + id + "');\"><font class='text8pt' color=black>Search</font></a></td>";
		} else {
			text += "				                <td width='20'><a href=\"javascript:search_by_name('" + name + "');\"><img src='" + PATH_SKIN + "images/pmenu/usermenu_search.gif' border=0 width='13' height='11'></a></td>";
			text += "				                <td><a href=\"javascript:search_by_name('" + name + "');\"><font class='text8pt' color=black>Search</font></a></td>";
		}
		text += "				                <td width='5'></td>";
		text += "							</tr>";

		// email
		if(email != "") {
			text += "							<tr>";
			text += "                           	<td colspan='4' background='" + PATH_SKIN + "images/pmenu/line.gif'></td>";
			text += "							</tr>";
			text += "							<tr height='20' onMouseOver=\"this.style.backgroundColor='#EFEFEF'\" onMouseOut=\"this.style.backgroundColor=''\">";
			text += "                                <td></td>";
			text += "                                <td><a href=\"javascript:ttmailer('" + email + "');hide_user_menu();\"><img src='" + PATH_SKIN + "images/pmenu/usermenu_mail.gif' border=0 width='13' height='11'></a></td>";
			text += "                                <td><a href=\"javascript:ttmailer('" + email + "');hide_user_menu();\"><font class='text8pt' color=black>Mail</font></a></td>";
			text += "                                <td></td>";
			text += "							</tr>";
		}

		// url
		if(url != "" && url != "http://" && url != "HTTP://") {
			text += "							<tr>";
			text += "                           	<td colspan='4' background='" + PATH_SKIN + "images/pmenu/line.gif'></td>";
			text += "							</tr>";
			text += "							<tr height='20' onMouseOver=\"this.style.backgroundColor='#EFEFEF'\" onMouseOut=\"this.style.backgroundColor=''\">";
			text += "								<td></td>";
			text += "								<td><a href=\"javascript:open_url('" + url + "');hide_user_menu();\"><img src='" + PATH_SKIN + "images/pmenu/usermenu_home.gif' border=0 width='13' height='11'></a></td>";
			text += "								<td><a href=\"javascript:open_url('" + url + "');hide_user_menu();\"><font class='text8pt' color=black>Home</font></a></td>";
			text += "								<td></td>";
			text += "							</tr>";
		}

		// memo and personal info
		if(VER_USE_MEMBER && id != "")  {
			text += "							<tr>";
			text += "                           	<td colspan='4' background='" + PATH_SKIN + "images/pmenu/line.gif'></td>";
			text += "                           </tr>";
			text += "							<tr height='20' onMouseOver=\"this.style.backgroundColor='#EFEFEF'\" onMouseOut=\"this.style.backgroundColor=''\">";
			text += "								<td></td>";
			text += "								<td align=center><a href=javascript:send_memo('" + id + "');hide_user_menu();><img src='" + PATH_SKIN + "images/pmenu/usermenu_memo.gif' border=0></a></td>";
			text += "								<td><a href=\"javascript:send_memo('" + id + "');hide_user_menu();\"><font class='text8pt' color=black>Memo</td>";
			text += "								<td></td>";
			text += "							</tr>";
			text += "							<tr height='20' onMouseOver=\"this.style.backgroundColor='#EFEFEF'\" onMouseOut=\"this.style.backgroundColor=''\">";
			text += "								<td></td>";
			text += "								<td align=center><a href=javascript:user_info('" + id + "');hide_user_menu();><img src='" + PATH_SKIN + "images/pmenu/usermenu_info.gif' border=0></a></td>";
			text += "								<td><a href=\"javascript:user_info('" + id + "');hide_user_menu();\"><font class='text8pt' color=black>Info</td>";
			text += "								<td></td>";
			text += "							</tr>";
		}

		text += "                        </table>";
		text += "                    </td>";
		text += "                    <td align='right' background='" + PATH_SKIN + "images/pmenu/box_mr.gif'></td>";
		text += "                </tr>";
		text += "				<tr>";
		text += "					<td width='5'><img src='" + PATH_SKIN + "images/pmenu/box_bl.gif' width=5 height=5></td>";
		text += "                    <td background='" + PATH_SKIN + "images/pmenu/box_bm.gif'></td>";
		text += "                    <td align='right' width='5'><img src='" + PATH_SKIN + "images/pmenu/box_br.gif' width=5 height=5></td>";
		text += "                </tr>";
		text += "			</table>";
		text += "        </td>";
		text += "    </tr>";
		text += "</table>";

		obj.innerHTML = text;
		obj.style.visibility = "visible";
	} else {
		hide_user_menu();
	}
}

function show_user_menu() {
	document.all["id_user_menu"].style.visibility = "visible";
}

function hide_user_menu() {
	document.all["id_user_menu"].style.visibility = "hidden";
}

function search_by_name(name) {
	location.href = LINK_LIST + "&s_mode=def&s_name=1&s_key=" + name;
	hide_user_menu();
}

function send_mail(email) {
	if(email) {
		location.href = "mailto:" + email;
	}
}

function ttmailer(email) {
	if(email) {
		window.open("ttmailer.cgi?act=write&to="+email, "", "width=320, height=350, resizable=1, scrollbars=1");
	}
}

function open_url(url) {
	window.open(url, "", "");
}

/*************************************************************************
	SEARCH script
*************************************************************************/
function set_search_name() {
	var obj = document.search_form;

	if(obj.s_name.value == 1) {
		document.all["search_name_check"].src = PATH_SKIN + "images/chkbox_off.gif";
		obj.s_name.value = 0;
	} else {
		document.all["search_name_check"].src = PATH_SKIN + "images/chkbox_on.gif";
		obj.s_name.value = 1;
	}
}

function set_search_title() {
	var obj = document.search_form;

	if(obj.s_title.value == 1) {
		document.all["search_title_check"].src = PATH_SKIN + "images/chkbox_off.gif";
		obj.s_title.value = 0;
	} else {
		document.all["search_title_check"].src = PATH_SKIN + "images/chkbox_on.gif";
		obj.s_title.value = 1;
	}
}

function set_search_content() {
	var obj = document.search_form;

	if(obj.s_content.value == 1) {
		document.all["search_content_check"].src = PATH_SKIN + "images/chkbox_off.gif";
		obj.s_content.value = 0;
	} else {
		document.all["search_content_check"].src = PATH_SKIN + "images/chkbox_on.gif";
		obj.s_content.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.all["id_search" + i].style.display = "block";
			else
				document.all["id_search" + i].style.display = "none";
		}
	}
}

function show_adv_search() {
	document.all["id_default_search"].style.display = "none";
	document.all["id_adv_search"].style.display = "block";
}

function show_default_search() {
	document.all["id_default_search"].style.display = "block";
	document.all["id_adv_search"].style.display = "none";
}

function search(mode) {
	if(mode == "default") {
		if(search_form.s_name.value == 0 && search_form.s_title.value == 0 && search_form.s_content.value == 0) {
			alert("°Ë»öÇÒ ÇÊµå¸¦ ¼±ÅÃÇØ ÁÖ½Ê½Ã¿À.");
			return;
		} else if(search_form.s_key.value == "")  {
			alert("°Ë»ö¾î¸¦ ÀÔ·ÂÇØ ÁÖ½Ê½Ã¿À.");
			search_form.s_key.focus();
			return;
		}

		search_form.action = LINK_LIST + "&s_mode=def";
	} else if(mode == "advanced") {
		// check search key
		for(var i = 1; i <= search_form.s_num.value; i++) {
			var obj = document.all["s_key" + i];

			if(obj.value == "") {
				alert("°Ë»ö¾î¸¦ ÀÔ·ÂÇØ ÁÖ½Ê½Ã¿À.");
				obj.focus();
				return;
			}
		}

		search_form.action = LINK_LIST + "&s_mode=adv";
	}

	search_form.submit();
}

/*************************************************************************
	COMMENT script
*************************************************************************/
function add_comment(idx) {
	if(document.all["cmt"+idx+"_name"].value == "") {
		alert("ÀÌ¸§À» ½á ÁÖ¼¼¿ä");
		document.all["cmt"+idx+"_name"].focus();
		return;
	} else if(document.all["cmt"+idx+"_pwd"].value == "") {
		alert("ºñ¹Ð¹øÈ£À» ½á ÁÖ¼¼¿ä");
		document.all["cmt"+idx+"_pwd"].focus();
		return;
	} else if(document.all["cmt"+idx+"_content"].value == "") {
		alert("³»¿ëÀ» ½á ÁÖ¼¼¿ä");
		document.all["cmt"+idx+"_content"].focus();
		return;
	}

	// set value
	comment_form.comment_name.value = document.all["cmt"+idx+"_name"].value;
	comment_form.comment_pwd.value = document.all["cmt"+idx+"_pwd"].value;
	comment_form.comment_content.value = document.all["cmt"+idx+"_content"].value;

	comment_form.action = LINK_ADD_COMMENT + "&idx=" + idx;
	comment_form.submit();
}

function show_comment_pwd(article_idx, comment_idx) {
	var obj = document.all["id_comment_pwd"];

	if(article_idx != 0 && comment_idx != 0) {
		comment_form.del_pwd.value = "";
		comment_form.article_idx.value = article_idx;
		comment_form.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";
	comment_form.del_pwd.focus();
}

function hide_comment_pwd() {
	document.all["id_comment_pwd"].style.visibility = "hidden";
}

function del_comment() {
	var pwd = comment_form.del_pwd.value;

	hide_comment_pwd();
	comment_form.action = LINK_DEL_COMMENT + "&pwd=" + pwd;
	comment_form.submit();
}

/*************************************************************************
	WRITE script
*************************************************************************/
function verify_data() {
	if(write_form.name.value == "") {
		alert("ÀÌ¸§À» ÀÔ·ÂÇØÁÖ¼¼¿ä.");
		write_form.name.focus();
		return;
	} else if(write_form.email.value != "") {
		if(!check_email(write_form.email.value)) {
			alert("ÀÌ¸ÞÀÏ Çü½ÄÀÌ ¿Ã¹Ù¸£Áö ¾Ê½À´Ï´Ù.");
			write_form.email.focus();
			return;
		}
	} else if(write_form.title.value == "") {
		alert("Á¦¸ñÀ» ÀÔ·ÂÇØÁÖ¼¼¿ä.");
		write_form.title.focus();
		return;
	} else if(write_form.content.value == "") {
		alert("³»¿ëÀ» ÀÔ·ÂÇØÁÖ¼¼¿ä.");
		write_form.content.focus();
		return;
	} else if(write_form.cate_idx) {
		// use categoy
		if(CATEGORY_WRITE_METHOD == 0) {
			// essential field
			if(write_form.cate_idx.value == 0) {
				alert("Ä«Å×°í¸®¸¦ ¼±ÅÃÇØÁÖ¼¼¿ä.");
				return;
			}
		} else if(CATEGORY_WRITE_METHOD == 1) {
			// alert message
			if(write_form.cate_idx.value == 0) {
				var bYes = confirm("Ä«Å×°í¸®¸¦ ¼±ÅÃÇÏÁö ¾ÊÀ¸¼Ì½À´Ï´Ù.\nÀúÀåÇÏ½Ã°Ú½À´Ï±î?");
				if(!bYes) return;
			}
		} else if(CATEGORY_WRITE_METHOD == 2) {
			// no action
		}
	}

	if(WRITE_MODE != "modify") {
		if(write_form.pwd && write_form.pwd.value == "") {
			alert("ÆÐ½º¿öµå¸¦ ÀÔ·ÂÇØÁÖ¼¼¿ä.");
			write_form.pwd.focus();
			return;
		}
	}

	// set content info
	if(write_form.content_width)
		write_form.content_width.value = parseInt(document.all['content'].style.width);
	if(write_form.content_height)
		write_form.content_height.value = parseInt(document.all['content'].style.height);

	write_form.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.all['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.all['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.all[obj]) {
		var obj_width = parseInt(document.all[obj].style.width);
		if(obj_width + width >= min_width)
			document.all[obj].style.width = obj_width + width;
	}
}

function resize_content_height(height) {
	if(document.all["content"]) {
		var obj_height = parseInt(document.all['content'].style.height);
		if(obj_height + height >= CONTENT_HEIGHT)
			document.all['content'].style.height = obj_height + height;
	}
}

// file & link
function show_file() {
	var obj = write_form.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.all["id_file" + i];
		var obj_info = document.all["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 = write_form.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.all["id_link" + i].style.display = 'block';
		else
			document.all["id_link" + i].style.display = 'none';
	}
}

function add_var(id, idx) {
	if(id == 'link') {
		write_form.all["content"].value += "{LINK:" + idx + "}";
	} else if(id == 'file') {
		write_form.all["content"].value += "{FILE:" + idx + "}";;
	}
}

/*************************************************************************
	ACTION script
*************************************************************************/
function check_enter(id) {
	if(event.keyCode == 13) {
		if(id == "search") search('default');
		else if(id == "jump_page") jump_to();
		else if(id == "search_num") set_search_num();
		else if(id == "del_comment") del_comment();

		event.returnValue = false;
	}
}

function read_article(idx) {
	if(LIST_TITLE_ON_CLICK == 0) {
		location.href = LINK_READ + "&page=" + PAGE_CUR_PAGE + "&idx=" + idx;
	} else if(LIST_TITLE_ON_CLICK == 1) {
		window.open(LINK_POPUP + "&page=" + PAGE_CUR_PAGE + "&idx=" + idx, "", "width="+LIST_POPUP_WIDTH+", height="+LIST_POPUP_HEIGHT+", scrollbars=1, resizable=1");
	} else if(LIST_TITLE_ON_CLICK == 2) {
		window.open(LINK_READ + "&page=" + PAGE_CUR_PAGE + "&idx=" + idx, "", "width="+LIST_POPUP_WIDTH+", height="+LIST_POPUP_HEIGHT+", scrollbars=1, resizable=1");
	}
}

function multi_read() {
	var	flag = 0;

	// check index
	for(var i = 1; i <= ARTICLE_ROWS; i++) {
		var obj = document.all["check" + i];
		if(obj && obj.value != 0) {
			// check secret article
			if(document.all["article" + i + "_secret"].value == 1) {
				alert("ºñ¹Ð °Ô½Ã¹°Àº ´ÙÁßÀÐ±â¸¦ ÇÏ½Ç ¼ö ¾ø½À´Ï´Ù.");
				return;
			}

			flag = 1;
		}
	}

	if(!flag) {
		alert("ÀÐÀ» °Ô½Ã¹°À» ¼±ÅÃÇØÁÖ½Ê½Ã¿À.");
		return;
	}

	list_form.action = LINK_MULTI_READ;
	list_form.submit();
}

function multi_delete() {
	var	flag = 0;

	// check index
	for(var i = 1; i <= ARTICLE_ROWS; i++) {
		var obj = document.all["check" + i];
		if(obj && obj.value != 0) flag = 1;
	}

	if(!flag) {
		alert("»èÁ¦ÇÒ °Ô½Ã¹°À» ¼±ÅÃÇØÁÖ½Ê½Ã¿À.");
		return;
	}

	list_form.action = LINK_MULTI_DEL + "&page=" + PAGE_CUR_PAGE;
	list_form.submit();
}

function reply_article(idx) {
	location.href = LINK_REPLY + "&idx=" + idx;
}

function modify_article(idx) {
	location.href = LINK_MODIFY + "&idx=" + idx;
}

function delete_article(idx) {
	if(confirm("°Ô½Ã¹°À» »èÁ¦ÇÏ½Ã°Ú½À´Ï±î?")) {
		location.href = LINK_DELETE + "&idx=" + idx;
	}
}

function recommend(idx) {
	location.href = LINK_RECOM + "&idx=" + idx;
}

function jump_to() {
	var obj = document.all["jump_page"];
	var page = obj.value;
	var last_page = document.all["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 = LINK_LIST + "&page="+page;
		}
	}
}