/**
 * @author Gyu
 */

function boardWrite() {
	var writeForm = document.writeForm;
	var title = $.trim($('#title').val());
	var contents = writeForm.contents.value = xed.getCurrentContent(true);
	$.post(
		"/checkLogin.htmls",
		function(data) {
			if (data == "notLogin") {
				alertLogin();
				return;
			} else if (data == "restrict") {
				alert("公式掲示板への書き込みを制限されているIDです。\n詳細については公式サイト内お問い合わせフォームご利用の上\nお問い合わせください。");
				return;
			} else if (data == 'levelRestrict') {
				alert('ご投稿には利用者情報を登録する必要があります。');
				document.location.href = "http://www.arario.jp/home.htmls?pn=regit_i";
				return;
			} else {
				if (confirm("投稿しますか？")) {
					if (title.length < 1) {
						alert("タイトルを入力してください。");
						$("#title").focus();
					} else {
						writeForm.action = "/boardWrite.htmls";
						writeForm.submit();
					}
				}
			}
		});
};

function boardUpdate() {
	v_title = trim(document.writeForm.title.value);
	var contents = document.writeForm.contents.value = xed
			.getCurrentContent(true);
	if (v_title.length < 1) {
		alert('タイトルを入力してください。');
		document.writeForm.title.focus();
	} else {
		if (confirm("修正しますか？")) {
			document.writeForm.action = "/boardUpdate.htmls";
			document.writeForm.submit();
		}
	}
}

function boardDelete() {
	var boardName = $('#bn').val();
	var seq_no = $('#sno').val();

	if (confirm("削除しますか？")) {
		document.location.href = "/boardDelete.htmls?bn=" + boardName + "&sno="
				+ seq_no;
	}

}

function clearBoard() {
	if (confirm('取り消しますか？')) {
		var xquaredEditor = (document.all) ? document.frames[0].frames[0].document.body
				: document.getElementsByTagName('iframe')[0].contentDocument.body
						.getElementsByTagName('iframe')[0].contentDocument.body;
		xquaredEditor.innerHTML = "";
	}
}

function sswrite() {
	v_title = trim(document.writeForm.title.value);
	v_file = trim(document.writeForm.file.value);
	document.writeForm.contents.value = xed.getCurrentContent(true);
	$.post(
		"/checkLogin.htmls",
		function(data) {
			if (data == "notLogin") {
				alertLogin();
				return;
			} else if (data == "restrict") {
				alert("公式掲示板への書き込みを制限されているIDです。\n詳細については公式サイト内お問い合わせフォームご利用の上\nお問い合わせください。");
				return;
			} else if (data == 'levelRestrict') {
				alert('ご投稿には利用者情報を登録する必要があります。');
				document.location.href = "http://www.arario.jp/home.htmls?pn=regit_i";
				return;
			}else {
				if (v_title.length < 1) {
					alert('タイトルを入力してください。');
					document.writeForm.title.focus();
				} else if (v_file.length < 1) {
					alert('ファイルを選んでください。');
				} else {
					if (confirm("投稿しますか？")) {
						document.writeForm.target = "_self";
						document.writeForm.action = "/ssWrite.htmls";
						document.writeForm.submit();
					}
				}
			}
		});
}

function ssupdate() {
	v_title = trim(document.writeForm.title.value);
	v_sno = document.writeForm.sno.value;
	v_pno = document.writeForm.pno.value;
	document.writeForm.contents.value = xed.getCurrentContent(true);
	if (v_title.length < 1) {
		alert('タイトルを入力してください。');
		document.writeForm.title.focus();
	} else {
		if (confirm("修正しますか？")) {
			document.writeForm.target = "_self";
			document.writeForm.action = "/ssUpdate.htmls";
			document.writeForm.submit();
		}
	}
}

function imgPreview(obj) {
	var chimg = new Image();
	chimg.src = obj.value;

	var ele = (document.all) ? event.srcElement : obj.target;

	if (ele.value.toLowerCase().match(/(.jpg|.gif|.jpeg)/)) {
		// document.getElementById("file_name").value = obj.value;
		document.writeForm.target = "hiddenIFrame";
		document.writeForm.action = "/preUpload.htmls";
		document.writeForm.submit();
		// document.getElementById("imgview").src = chimg.src;
	} else {
		alert("jpgまたはgif形式のファイルのみアップできます。");
	}

	// updateByPost('preUpload.action', 'ifrFileUp', 'writeForm');
}

function gotoPage(pno) {
	document.bbsForm.pno.value = pno;
	document.bbsForm.action = "/boardList.htmls";
	document.bbsForm.submit();

}

function gotoVPage(pno) {
	var v_bn = trim(document.bbsForm.bn.value);
	var v_stp = trim(document.bbsForm.stp.value);
	var v_skey = trim(document.bbsForm.skey.value);
	var v_sno = trim(document.bbsForm.sno.value);
	var v_data = 'bn=' + v_bn + '_inner&pno=' + pno + '&sno=' + v_sno;
	v_data = v_data + "&stp=" + v_stp + "&skey=" + v_skey;
	$('#inner_list').load("/boardList.htmls", v_data);
}

function gotoCPage(pno, type) {
	var v_bn = trim(document.cmtForm.bn.value);
	var v_sno = trim(document.bbsForm.sno.value);
	var v_ot = trim(document.cmtForm.ot.value);
	var v_data = 'bn=' + v_bn + '&pno=' + pno + '&sno=' + v_sno + '&ot=' + v_ot;
	document.cmtForm.pno.value = pno;

	$.ajax( {
		type : "post",
		url : "/commentList.htmls",
		data : v_data,
		success : function(msg) {
			document.getElementById("cmt_list").innerHTML = trim(msg);
			if (type != "init") {
				document.location.href = "#cmt_list";
			}
		}
	})
}

function gotoSearch() {
	var v_skey = trim(document.bbsForm.skey.value);
	if (v_skey.length < 2) {
		alert("キーワードは２文字を入力してください。");
	} else {
		gotoPage(1);
	}
}

function gotoView(sno) {
	document.bbsForm.sno.value = sno;
	document.bbsForm.action = "/boardView.htmls";
	document.bbsForm.submit();
}

function gotoList() {
	var v_pno = document.bbsForm.pno.value;
	var v_bn = trim(document.bbsForm.bn.value);
	document.location.href = "/boardList.htmls?bn=" + v_bn + "&pno=" + v_pno;
}

function gotoCmtForm() {
	if (trim(document.cmtForm.contents.value) == "200文字以内でコメントを記入してください。") {
		document.cmtForm.contents.value = "";
	}
	document.cmtForm.contents.focus();
}

function addComment() {
	var v_cnts = trim(document.cmtForm.contents.value);
	var v_ta_cnt = txtAreaCheck(v_cnts);

	if (v_cnts.length < 1) {
		alert("内容を入力してください。");
		document.cmtForm.contents.focus();
	} else if (v_cnts == "200文字以内でコメントを記入してください。") {
		alert("内容を入力してください。");
		document.cmtForm.contents.value = "";
		document.cmtForm.contents.focus();
	} else if (v_cnts.length > 200) {
		alert("200文字以内で入力してください。");
		document.cmtForm.contents.value = v_cnts.substr(0, 300);
		document.cmtForm.contents.focus();
	} else if (v_ta_cnt >= 8) {
		alert("7行以内で入力してください。");
		document.cmtForm.contents.focus();
	} else {
		$.post(
			"/checkLogin.htmls",
			function(data) {
				if (data == "notLogin") {
					alertLogin();
					return;
				} else if (data == "restrict") {
					alert("公式掲示板への書き込みを制限されているIDです。\n詳細については公式サイト内お問い合わせフォームご利用の上\nお問い合わせください。");
					return;
				} else if (data == 'levelRestrict') {
					alert('ご投稿には利用者情報を登録する必要があります。');
					document.location.href = "http://www.arario.jp/home.htmls?pn=regit_i";
					return;
				} else {
					if (confirm("登録しますか？")) {
						var v_bn = trim(document.cmtForm.bn.value);
						var v_sno = trim(document.bbsForm.sno.value);
						var v_data = 'bn=' + v_bn + '&pno=1&sno='
								+ v_sno + '&contents='
								+ encodeURIComponent(v_cnts);
						$
								.ajax( {
									type : "post",
									url : "/addComment.htmls",
									data : v_data,
									success : function(msg) {
										document.cmtForm.contents.value = "";
										changeOrdering("desc",
												"init");
									}
								});
					}
				}
			});
	}
}

function delComment(v_bno) {
	if (confirm("削除しますか？")) {
		var v_bn = trim(document.cmtForm.bn.value);
		var v_sno = document.cmtForm.sno.value;
		var v_data = 'bn=' + v_bn + '&sno=' + v_sno + '&bno=' + v_bno;

		$.ajax( {
			type : "post",
			url : "/delComment.htmls",
			data : v_data,
			success : function(msg) {
				document.cmtForm.contents.value = "";
				changeOrdering("desc");
			}
		});
	}
}

function changeOrdering(ot, type) {
	if (type == "init")
		document.cmtForm.pno.value = 1;
	var pno = document.cmtForm.pno.value;
	document.cmtForm.ot.value = ot;
	gotoCPage(pno);
}

function goUpdate() {
	document.bbsForm.action = "goUpdate.htmls";
	document.bbsForm.submit();
}

