var selectedObj;

function addBanner(number, alt, ref, size) {
	if (window.opener && !window.opener.closed) {
		window.opener.addBannerInfo(number, alt, ref, size);
	} else {
		alert("親ウインドウがありません。");
	}
}

function addBannerInfo(number, alt, ref, size) {
	var tbody = $("b" + size);
	if (!tbody) return;
	var tr = $E("tr");
	tr.className = "bg_highlight";
	var td;
	// 画像
	td = $E("td");
	td.className = "wp200 tc";
	var img = $E("img");
	var filename = number + "_" + size + ".jpg";
	var time = new Date().getTime();
	var url = "http://" + location.hostname + "/image/get/D/banner/F/" + filename + "/R/180/T/" + time;
	img.src = url;
	img.alt = alt;
	td.appendChild(img);
	tr.appendChild(td);
	// バナー名・参照先
	td = $E("td");
	td.className = "wp400";
	td.appendChild($T(alt));
	td.appendChild($E("br"));
	td.appendChild($T(ref));
	var input = $E("input");
	input.type = "hidden";
	input.name = "h" + size + "[]";
	input.value = number;
	td.appendChild(input);
	tr.appendChild(td);
	// 操作
	td = $E("td");
	td.className = "wd50 tc";
	input = $E("input");
	input.type = "button";
	input.value = "削除";
	input.onclick = function() {
		delBannerInfo(this, size);
	}
	td.appendChild(input);
	tr.appendChild(td);
	// 操作
	td = $E("td");
	td.className = "wp100 tc";
	input = $E("input");
	input.type = "button";
	input.value = "↑";
	input.onclick = function() {
		suBannerInfo(this, size);
	}
	td.appendChild(input);
	input = $E("input");
	input.type = "button";
	input.value = "↓";
	input.onclick = function() {
		sdBannerInfo(this, size);
	}
	td.appendChild(input);
	tr.appendChild(td);
	//
	tbody.appendChild(tr);
}

function addIcon(subdir, filename) {
	if (window.opener && !window.opener.closed) {
		window.opener.addIconInfo(subdir, filename);
	} else {
		alert("親ウインドウがありません。");
	}
}

function addIconInfo(subdir, filename) {
	var filepath = (subdir ? subdir + "/" : "") + filename;
	var tr = $("icons");
	var td = tr.firstChild;
	while (td) {
		if (td.tagName == "TD" && td.title == filepath) {
			return;
		}
		td = td.nextSibling;
	}
	td = $E("td");
	td.className = "wp70 bd0 tc";
	td.title = filepath;
	var input;
	input = $E("input");
	input.type = "hidden";
	input.name = "icons[]";
	input.value = filepath;
	td.appendChild(input);
	var div = $E("div");
	div.className = "suite_icon_image";
	var img = $E("img");
	var time = new Date().getTime();
	var url = "http://" + location.hostname + "/image/get/D/icon";
	if (subdir) {
		url += "/S/" + subdir;
	}
	url += "/F/" + filename + "/R/40/T/" + time;
	img.src = url;
	div.appendChild(img);
	td.appendChild(div);
	input = $E("input");
	input.type = "button";
	input.value = "削除";
	input.className = "mt5";
	input.onclick = function() {
		delIconInfo(this);
	}
	td.appendChild(input);
	tr.appendChild(td);
}

function addProduct(number, name, price) {
	if (window.opener && !window.opener.closed) {
		window.opener.addProductInfo(number, name, price);
	} else {
		alert("親ウインドウがありません。");
	}
}

function addProductInfo(number, name, price) {
	var tbody = $("suite");
	var tr = $E("tr");
	var td;
	var a;
	var input;
	var index = getProductInfoIndex();
	var rate;
	// №
	td = $E("td");
	td.className = "wp50 tc";
	input = $E("input");
	input.type = "hidden";
	input.name = "suite[" + index + "][0]";
	input.value = number;
	td.appendChild(input);
	td.appendChild($T(number));
	tr.appendChild(td);
	// 商品名
	td = $E("td");
	td.className = "wp250";
	a = $E("a");
	a.href = "http://" + location.hostname + "/control/product/editor/pN/" + number;
	a.target = "_blank";
	a.appendChild($T(name));
	td.appendChild(a);
	tr.appendChild(td);
	// 区切
	td = $E("td");
	td.className = "wp50 tc";
	input = $E("input");
	input.type = "hidden";
	input.name = "suite[" + index + "][1]";
	input.value = 1;
	td.appendChild(input);
	input = $E("input");
	input.type = "checkbox";
	input.name = "suite[" + index + "][12]";
	input.value = 1;
	td.appendChild(input);
	tr.appendChild(td);
	// 通常価格
	td = $E("td");
	td.className = "wp75 tr";
	td.appendChild($T(price));
	tr.appendChild(td);
	// 販売価格
	td = $E("td");
	td.className = "wp75 tr";
	input = $E("input");
	input.type = "text";
	input.name = "suite[" + index + "][2]";
	input.value = price;
	input.maxlength = 9;
	input.className = "wd65 tr";
	td.appendChild(input);
	for (var i = 3; i <= 10; i++) {
		input = $E("input");
		input.type = "hidden";
		input.id = "p_" + index + "_" + Math.floor((i - 3) / 2) + ((i % 2) > 0 ? "_a" : "_b");
		input.name = "suite[" + index + "][" + i + "]";
		input.value = 0;
		td.appendChild(input);
	}
	input = $E("input");
	input.type = "hidden";
	input.id = "p_" + index + "_4_b";
	input.name = "suite[" + index + "][11]";
	input.value = 0;
	td.appendChild(input);
	tr.appendChild(td);
	// 率
	td = $E("td");
	td.className = "wp50 tc";
	rate = price > 0 ? "100" : "-";
	td.appendChild($T(rate));
	tr.appendChild(td);
	// 操作
	td = $E("td");
	td.className = "wp200 tl";
	input = $E("input");
	input.type = "button";
	input.value = "LOT";
	input.onclick = function() {
		showPricePop(index);
	}
	td.appendChild(input);
	input = $E("input");
	input.type = "button";
	input.value = "削除";
	input.onclick = function() {
		delProductInfo(this);
	}
	td.appendChild(input);
	tr.appendChild(td);
	//
	tr.title = index;
	tbody.appendChild(tr);
}

function addQa(number, name) {
	if (window.opener && !window.opener.closed) {
		window.opener.addQaInfo(number, name);
	} else {
		alert("親ウインドウがありません。");
	}
}

function addQaInfo(number, name) {
	var tbody = $("qa");
	var tr = $E("tr");
	var td;
	var input;
	var index = getQaInfoIndex();
	// №
	td = $E("td");
	td.className = "wp50 tc";
	input = $E("input");
	input.type = "hidden";
	input.name = "qas[]";
	input.value = number;
	td.appendChild(input);
	td.appendChild($T(number));
	tr.appendChild(td);
	// 質問内容
	td = $E("td");
	td.className = "wp600";
	td.appendChild($T(name));
	tr.appendChild(td);
	// 操作
	td = $E("td");
	td.className = "wp100 tc";
	td.appendChild(input);
	input = $E("input");
	input.type = "button";
	input.value = "削除";
	input.onclick = function() {
		delQaInfo(this);
	}
	td.appendChild(input);
	tr.appendChild(td);
	//
	tr.title = index;
	tbody.appendChild(tr);
}

function addStorage(id, subdir, filename) {
	if (window.opener && !window.opener.closed) {
		window.opener.addStorageInfo(id, subdir, filename);
	} else {
		alert("親ウインドウがありません。");
	}
}

function addStorageInfo(id, subdir, filename) {
	if ($(id).style.display == "none") {
		togglePreview(id);
	}
	var tag = "<img src=\"http://" + location.hostname + "/updir/storage/";
	if (subdir) tag += subdir + "/";
	tag += filename + "\" border=0>";
	insertTag(id, tag);
}

function addSuite(number) {
	if (window.opener && !window.opener.closed) {
		window.opener.doSubmit("add", number);
	} else {
		alert("親ウインドウがありません。");
	}
}

function addTable(id, body) {
	if (window.opener && !window.opener.closed) {
		window.opener.addTableInfo(id, body);
	} else {
		alert("親ウインドウがありません。");
	}
}

function addTableInfo(id, body) {
	if ($(id).style.display == "none") {
		togglePreview(id);
	}
	insertTag(id, body);
}

function addTag(name) {
	var text = $("tag_text");
	if (text.value != "") {
		text.value += ",";
	}
	text.value += name;
}

function calcTax(id) {
	var obj = $(id);
	if (!obj || isNaN(obj.value)) return;
	obj.value = parseInt(obj.value * 1.05);
}

function cancelEdit() {
	$("view_mode").value = "add";
	$("view_data").value = "";
	$("name").value = "";
	$("regist").value = "追加";
	$("cancel").style.visibility = "hidden";
	clearHighlight();
}

function changeBgColor(id, obj) {
	var color = obj.options[obj.selectedIndex].value;
	$(id).style.backgroundColor = "#" + color;
}

function changeColor(id, color) {
	if (window.opener && !window.opener.closed) {
		window.opener.changeColorInfo(id, color);
	} else {
		alert("親ウインドウがありません。");
	}
}

function changeColorInfo(id, color) {
	var obj = $(id);
	obj.title = "#" + color;
	obj.style.backgroundColor = obj.title;
}

function changeStorageImage(subdir, filename, width, height) {
	var img = $("image_view");
	var src = "http://" + location.hostname + "/updir/storage/";
	if (subdir != "") {
		src += subdir + "/";
	}
	src += filename;
	if (img.src != src) {
		img.src = src;
		img.style.width = width + "px";
		img.style.height = height + "px";
	}
}

function clearHighlight() {
	if (!selectedObj) return;
	var child = selectedObj.parentNode.parentNode.parentNode.firstChild;
	while (child) {
		if (child.tagName == "TR") {
			child.style.cssText = "";
		}
		child = child.nextSibling;
	}
}

function clearTextarea(id) {
	var textarea = $(id);
	if (confirm("HTMLをクリアしますか?")) {
		if (textarea.style.display == "none") {
			togglePreview(id);
		}
		textarea.value = "";
	}
}

function delBannerInfo(obj, size) {
	var tbody = $("b" + size);
	if (!tbody) return;
	tbody.removeChild(obj.parentNode.parentNode);
}

function delIconInfo(obj) {
	var tr = $("icons");
	tr.removeChild(obj.parentNode);
}

function delProductInfo(obj) {
	var tbody = $("suite");
	tbody.removeChild(obj.parentNode.parentNode);
}

function delQaInfo(obj) {
	var tbody = $("qa");
	tbody.removeChild(obj.parentNode.parentNode);
}

function doAction(action, target) {
	document.forms[0].action = action;
	document.forms[0].target = target ? target : "";
}

function doPopup(url) {
	window.open(
		"http://" + location.hostname + url
	);
}

function doRedirect(url) {
	location.href = "http://" + location.hostname + url;
}

function doSubjectEdit(index) {
	var link = $("subject_link_" + index);
	var edit = $("subject_edit_" + index);
	if (link.style.display == "" || link.style.display == "block") {
		link.style.display = "none";
		edit.style.display = "block";
	} else {
		edit.style.display = "none";
		link.style.display = "block";
	}
}

function doSubmit(mode, data) {
	var viewMode = $("view_mode");
	var viewData = $("view_data");
	var viewPage = $("view_page");
	var viewCurr = $("view_curr");
	if (mode == "search" && viewPage) {
		viewPage.value = 1;
	} else if (mode == "prev_page" && viewPage) {
		viewPage.value--;
	} else if (mode == "next_page" && viewPage) {
		viewPage.value++;
	} else if (mode == "jump_page" && viewPage) {
		viewPage.value = data;
	} else if (mode == "shiftdown" || mode == "shiftup") {
	} else if (mode == "add") {
	} else if (mode == "update") {
	} else if (mode == "delete" || mode == "delete_image") {
		if (!confirm("削除しますか?")) return;
	} else if (mode == "clone") {
		if (!confirm("複製を作成しますか?")) return;
	} else if (mode == "upload_image") {
	} else if (mode == "chdir" && viewCurr) {
		viewCurr.value = data;
	} else if (mode == "status") {
		if (!confirm("変更しますか?")) return;
	} else if (mode == "complete") {
		if (!confirm("完了しますか?")) return;
	} else if (mode == "sendback") {
		if (!confirm("返品処理しますか?")) return;
	} else if (mode == "favorite") {
		if (!confirm("お気に入りに追加しますか?")) return;
	} else if (mode == "openfile") {
	} else if (mode == "delivery") {
	} else if (mode == "alter_email") {
		if (!confirm("メールアドレスを書き換えますか?")) return;
	} else if (mode == "recalc") {
		if (!confirm("合計金額を再計算しますか?")) return;
	} else if (mode == "area") {
	} else {
		return;
	}
	viewMode.value = mode;
	viewData.value = data;
	document.forms[0].action = location.href;
	document.forms[0].submit();
}

function genTable(id) {
	if ($(id).style.display == "none") {
		togglePreview(id);
	};
	var cols = $("cols").selectedIndex + 1;
	var rows = $("rows").selectedIndex + 1;
	var colwidth = $("colwidth").value;
	if ($("dirv").checked) {
		var dir = "v";
	} else {
		var dir = "h";
	}
	var size = $("size").selectedIndex + 1;
	var bgcolor = $("bgcolor").title;
	var bdcolor = $("bdcolor").title;
	var padding = $("padding").selectedIndex
	if (colwidth.match(/[0-9]+/g) != colwidth || colwidth <= 0) {
		alert("列幅は0より大きい数字を入力してください。");
		return;
	}
	var r = parseInt("0x" + bgcolor.substr(1, 2));
	var g = parseInt("0x" + bgcolor.substr(3, 2));
	var b = parseInt("0x" + bgcolor.substr(5, 2));
	var brightness = 3 * r + 6 * g + b;
	var header;
	var value;
	var html = "<table border=1 bordercolor=\"" + bdcolor + "\" style=\"border-collapse: collapse\">\t";
	for (var y = 1; y <= rows; y++) {
		html += "<tr>\t";
		for (var x = 1; x <= cols; x++) {
			header = 0;
			value = y + ":" + x;
			if ((dir == "v" && y == 1) || (dir == "h" && x == 1)) {
				html += "<th bgcolor=\"" + bgcolor + "\"";
				header = 1;
			} else {
				html += "<td";
			}
			if (y == 1) {
				html += " width=" + colwidth;
			}
			if (padding > 0) {
				html += " style=\"padding: " + padding + "px\"";
			} 
			html += "><font";
			if (header > 0 && brightness > 1280) {
				html += " color=\"#333333\"";
			}
			html += " size=" + size+ ">" + value + "</font>";
			if (header > 0) {
				html += "</th>\t";
			} else {
				html += "</td>\t";
			}
		}
		html += "</tr>\t";
	}
	html += "</table>\t";
	insertTag(id, html);
}

function getProductInfoIndex() {
	var parent = $("suite");
	var child = parent.firstChild;
	var index = -1;
	while (child) {
		if (child.tagName == "TR" && child.title != "" && child.title > index) {
			index = parseInt(child.title);
		}
		child = child.nextSibling;
	}
	index++;
	return index;
}

function getQaInfoIndex() {
	var parent = $("qa");
	var child = parent.firstChild;
	var index = -1;
	while (child) {
		if (child.tagName == "TR" && child.title != "" && child.title > index) {
			index = parseInt(child.title);
		}
		child = child.nextSibling;
	}
	index++;
	return index;
}

function hidePricePop(save) {
	var pop = $("pop");
	if (save == 1) {
		var index = $("p_curr").value;
		for (var i = 0; i < 4; i++) {
			$("p_" + index + "_" + i + "_a").value =  $("r_" + i + "_a").value;
			$("p_" + index + "_" + i + "_b").value =  $("r_" + i + "_b").value;
		}
		$("p_" + index + "_4_b").value =  $("r_4_b").value;
	}
	pop.style.display = "none";
}

function insertTag(ID, tag) {
	tag = tag.replace(/\t/g, "\n");
	var textarea = $(ID);
	textarea.focus();
	if (document.selection) {
		var range = document.selection.createRange();
		range.text = tag;
	} else if (textarea.selectionStart != undefined) {
		var start = textarea.selectionStart;
		var end = textarea.selectionEnd;
		textarea.value = textarea.value.substring(0, start) + tag + textarea.value.substring(end);
		textarea.setSelectionRange(start + tag.length, start + tag.length);
	} else {
		textarea.value += tag;
	}
}

function sdBannerInfo(obj, size) {
	var tbody = $("b" + size);
	if (!tbody) return;
	var f = 0;
	var srcTr = obj.parentNode.parentNode;
	var dstTr;
	var child = tbody.firstChild;
	while (child) {
		if (child.tagName == "TR") {
			if (child == srcTr) {
				f = 1;
			} else if (f) {
				dstTr = child;
				break;
			}
		}
		child = child.nextSibling;
	}
	if (dstTr) {
		var tmpTr = dstTr.cloneNode(true);
		tbody.replaceChild(srcTr.cloneNode(true), dstTr);
		tbody.replaceChild(tmpTr, srcTr);
	}
}

function suBannerInfo(obj, size) {
	var tbody = $("b" + size);
	if (!tbody) return;
	var f = 0;
	var srcTr = obj.parentNode.parentNode;
	var dstTr;
	var child = tbody.firstChild;
	while (child) {
		if (child.tagName == "TR") {
			if (child == srcTr) {
				break;
			} else if (f) {
				dstTr = child;
			}
			f++;
		}
		child = child.nextSibling;
	}
	if (dstTr) {
		var tmpTr = dstTr.cloneNode(true);
		tbody.replaceChild(srcTr.cloneNode(true), dstTr);
		tbody.replaceChild(tmpTr, srcTr);
	}
}

function selectCategory(dir) {
	if (dir == 0) {
		var src = $("category_tree");
		var dst = $("category_save");
	} else {
		var src = $("category_save");
		var dst = $("category_tree");
	}
	var option = src.firstChild;
	while (option) {
		var nextOption = option.nextSibling;
		if (option.tagName == "OPTION" && option.selected) {
			dst.appendChild(option.cloneNode(true));
			src.removeChild(option);
		}
		option = nextOption;
	}
}

function setChargeAll() {
	var price = $("price").value;
	for (var i = 0; i < 47; i++) {
		var charge = $("charge" + i);
		charge.value = price;
	}
}

function setSelectAll() {
	var reg = $("category_save");
	var option = reg.firstChild;
	while (option) {
		if (option.tagName == "OPTION") {
			option.selected = true;
		}
		option = option.nextSibling;
	}
}

function showBannerDialog(size) {
	var w;
	w = window.open(
		"http://" + location.hostname + "/control/popup/banner/size/" + size,
		"banner",
		"width=520,height=690"
	);
	if (w && !w.closed) {
		w.focus();
	}
}

function showColorDialog(id) {
	var w;
	w = window.open(
		"http://" + location.hostname + "/control/popup/color/id/" + id,
		"color",
		"width=520,height=720"
	);
	if (w && !w.closed) {
		w.focus();
	}
}

function showIconDialog() {
	var w;
	w = window.open(
		"http://" + location.hostname + "/control/popup/icon",
		"icon",
		"width=520,height=620"
	);
	if (w && !w.closed) {
		w.focus();
	}
}

function showPreviewDialog(number) {
	var w;
	w = window.open(
		"http://" + location.hostname + "/control/suite/preview/sN/" + number + "/m/popup",
		"preview",
		"width=795,height=820"
	);
	if (w && !w.closed) {
		w.focus();
	}
}

function showPricePop(index) {
	var pop = $("pop");
	for (var i = 0; i < 4; i++) {
		$("r_" + i + "_a").value = $("p_" + index + "_" + i + "_a").value;
		$("r_" + i + "_b").value = $("p_" + index + "_" + i + "_b").value;
	}
	$("r_4_b").value = $("p_" + index + "_4_b").value;
	$("p_curr").value = index;
	pop.style.display = "block";
	pop.style.left = (scrollLeft() + parseInt((clientWidth() - pop.offsetWidth) * 0.5)) + "px";
	pop.style.top = (scrollTop() + parseInt((clientHeight() - pop.offsetHeight) * 0.5)) + "px";
}

function showProductDialog() {
	var w;
	w = window.open(
		"http://" + location.hostname + "/control/popup/product",
		"product",
		"width=520,height=660"
	);
	if (w && !w.closed) {
		w.focus();
	}
}

function showQaDialog() {
	var w;
	w = window.open(
		"http://" + location.hostname + "/control/popup/qa",
		"qa",
		"width=520,height=660"
	);
	if (w && !w.closed) {
		w.focus();
	}
}

function showStorageDialog(id) {
	var w;
	w = window.open(
		"http://" + location.hostname + "/control/popup/storage/id/" + id,
		"storage",
		"width=520,height=630"
	);
	if (w && !w.closed) {
		w.focus();
	}
}

function showSuiteDialog(sN) {
	var w;
	w = window.open(
		"http://" + location.hostname + "/control/popup/suite/sN/" + sN,
		"suite",
		"width=520,height=690"
	);
	if (w && !w.closed) {
		w.focus();
	}
}

function showTableDialog(id) {
	var w;
	w = window.open(
		"http://" + location.hostname + "/control/popup/table/id/" + id,
		"table",
		"width=520,height=580"
	);
	if (w && !w.closed) {
		w.focus();
	}
}

function startEdit(obj, id, data) {
	selectedObj = obj;
	$("view_mode").value = "update";
	$("view_data").value = id;
	$("name").value = data;
	$("regist").value = "変更";
	$("cancel").style.visibility = "visible";
	clearHighlight();
	selectedObj.parentNode.parentNode.style.cssText = "background-color: #FFFACD";
}

function togglePreview(id) {
	var textarea = $(id);
	if (!textarea) return;
	var div = $("preview_" + id);
	if (!div) return;
	if (textarea.style.display == "" || textarea.style.display == "block") {
		textarea.style.display = "none";
		div.style.display = "block";
		div.innerHTML = textarea.value;
	} else {
		div.style.display = "none";
		textarea.style.display = "block";
	}
}

function toggleTags() {
	var button = $("tag_button");
	var list = $("tag_list");
	if (button.style.display == "" || button.style.display == "block") {
		button.style.display = "none";
		list.style.display = "block";
	} else {
		list.style.display = "none";
		button.style.display = "block";
	}
}

function verifyTags(id) {
	var subject = $(id).value;
	var matches = subject.match(/<[^>]+>/g);
	if (matches == null) {
		alert("エラーはありません。");
		return;
	}
	var tags = new Array();
	var tag;
	var e;
	var i;
	for (i = 0; i < matches.length; i++) {
		tag = matches[i].toLowerCase();
		tag = tag.substr(1, tag.length - 2);
		if (tag.substr(0, 1) == "/") {
			e = tag.split("/");
			if (!e[1]) {
				alert("不適切なタグがあります。 ==> " + matches[i]);
				return;
			} else if (tags.length == 0) {
				alert("</" + e[1] + ">の開始タグがありません。");
				return;
			} else if (e[1] != tags[0]) {
				alert("開始タグ<" + tags[0] + ">と終了タグ</" + e[1] + ">が一致しません。");
				return;
			}
			tags.shift();
		} else {
			e = tag.split(" ");
			if (!e[0] || e[0] == "!--" || e[0] == "br" || e[0] == "hr" || e[0] == "img" || e[0] == "input") continue;
			tags.unshift(e[0]);
		}
	}
	if (tags.length == 0) {
		alert("エラーはありません。");
	} else {
		var m = "";
		for (i = tags.length - 1; i >= 0; i--) {
			m += "<" + tags[i] + ">の終了タグがありません。\n";
		}
		alert(m);
	}
}