var selectedObj;

function addBanner(number, name, alt, url, visible) {
	if (window.opener && !window.opener.closed) {
		window.opener.addBannerInfo(number, name, alt, url, visible);
	} else {
		alert("親ウインドウがありません。");
	}
}

function addBannerInfo(number, name, alt, url, visible) {
	var tbody = $("element");
	if (!tbody) return;
	var tr = $E("tr");
	if (visible) {
		tr.className = "bg_highlight";
	}
	var td;
	// 画像
	td = $E("td");
	td.className = "wp_200 ta_c";
	var img = $E("img");
	var filename = number + ".jpg";
	var time = new Date().getTime();
	var src = "http://" + location.hostname + "/image/get/D/banner/F/" + filename + "/R/180/T/" + time;
	img.src = src;
	img.alt = alt;
	td.appendChild(img);
	tr.appendChild(td);
	// バナー名・URL
	td = $E("td");
	td.className = "wp_350";
	td.appendChild($T(name));
	td.appendChild($E("br"));
	td.appendChild($T(url));
	tr.appendChild(td);
	// 操作
	td = $E("td");
	td.className = "ta_c";
	var input = $E("input");
	input.type = "button";
	input.value = "削除";
	input.onclick = function() {
		deleteRow(this);
	}
	td.appendChild(input);
	tr.appendChild(td);
	td = $E("td");
	td.className = "ta_c";
	input = $E("input");
	input.type = "button";
	input.value = "↓";
	input.onclick = function() {
		shiftDownRow(this);
	}
	td.appendChild(input);
	input = $E("input");
	input.type = "button";
	input.value = "↑";
	input.onclick = function() {
		shiftUpRow(this, 'header');
	}
	td.appendChild(input);
	input = $E("input");
	input.type = "hidden";
	input.name = "numbers[]";
	input.value = number;
	td.appendChild(input);
	tr.appendChild(td);
	//
	tbody.appendChild(tr);
}

function addBlock(position, number, name, visible) {
	if (window.opener && !window.opener.closed) {
		window.opener.addBlockInfo(position, number, name, visible);
	} else {
		alert("親ウインドウがありません。");
	}
}

function addBlockInfo(position, number, name, visible) {
	var tbody = $("layout" + position);
	if (!tbody) return;
	var tr = $E("tr");
	if (visible) {
		tr.className = "bg_highlight";
	}
	var td;
	// バナー名・URL
	td = $E("td");
	td.width = 180;
	td.appendChild($T(name));
	tr.appendChild(td);
	// 操作
	td = $E("td");
	td.width = 110;
	var input = $E("input");
	input.type = "button";
	input.value = "↓";
	input.onclick = function() {
		shiftDownRow(this);
	}
	td.appendChild(input);
	input = $E("input");
	input.type = "button";
	input.value = "↑";
	input.onclick = function() {
		shiftUpRow(this);
	}
	td.appendChild(input);
	input = $E("input");
	input.type = "button";
	input.value = "X";
	input.onclick = function() {
		deleteRow(this);
	}
	td.appendChild(input);
	input = $E("input");
	input.type = "hidden";
	input.name = "params[" + position + "][]";
	input.value = "0," + number;
	td.appendChild(input);
	tr.appendChild(td);
	//
	tbody.appendChild(tr);
}

function addIcon(number, name) {
	if (window.opener && !window.opener.closed) {
		window.opener.addIconInfo(number, name);
	} else {
		alert("親ウインドウがありません。");
	}
}

function addIconInfo(number, name) {
	var tr = $("icons");
	var td = tr.firstChild;
	while (td) {
		if (td.tagName == "TD" && td.title == number) {
			return;
		}
		td = td.nextSibling;
	}
	td = $E("td");
	td.className = "bd_none ta_c";
	td.title = number;
	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/F/" + number + ".jpg/R/40/T/" + time;
	img.src = url;
	div.appendChild(img);
	td.appendChild(div);
	var input;
	input = $E("input");
	input.type = "hidden";
	input.name = "icons[]";
	input.value = number;
	td.appendChild(input);
	input = $E("input");
	input.type = "button";
	input.value = "削除";
	input.onclick = function() {
		deleteCol(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 = "ta_c";
	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");
	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 = "ta_c";
	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 = "ta_r";
	td.appendChild($T(price));
	tr.appendChild(td);
	// 販売価格
	td = $E("td");
	td.className = "ta_r";
	input = $E("input");
	input.type = "text";
	input.name = "suite[" + index + "][2]";
	input.value = price;
	input.maxlength = 9;
	input.className = "wd_65 ta_r";
	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 = "ta_c";
	rate = price > 0 ? "100" : "-";
	td.appendChild($T(rate));
	tr.appendChild(td);
	// 操作
	td = $E("td");
	td.className = "ta_l";
	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() {
		deleteRow(this);
	}
	td.appendChild(input);
	tr.appendChild(td);
	//
	tr.title = index;
	tbody.appendChild(tr);
}

function addQa(number, category, question) {
	if (window.opener && !window.opener.closed) {
		window.opener.addQaInfo(number, category, question);
	} else {
		alert("親ウインドウがありません。");
	}
}

function addQaInfo(number, category, question) {
	var tbody = $("qa");
	var tr = $E("tr");
	var td;
	var input;
	var index = getQaInfoIndex();
	// №
	td = $E("td");
	td.className = "ta_c";
	input = $E("input");
	input.type = "hidden";
	input.name = "qa[" + index + "][0]";
	input.value = number;
	td.appendChild(input);
	td.appendChild($T(number));
	tr.appendChild(td);
	// 質問内容
	td = $E("td");
	td.appendChild($T(question));
	input = $E("input");
	input.type = "hidden";
	input.name = "qa[" + index + "][1]";
	input.value = category;
	td.appendChild(input);
	input = $E("input");
	input.type = "hidden";
	input.name = "qa[" + index + "][2]";
	input.value = question;
	td.appendChild(input);
	tr.appendChild(td);
	// 操作
	td = $E("td");
	td.className = "ta_c";
	input = $E("input");
	input.type = "button";
	input.value = "削除";
	input.onclick = function() {
		deleteRow(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) {
	var url = "http://" + location.hostname + "/updir/storage/" + (subdir ? subdir + "/" : "") + filename;
	RE_insertImage(id, url);
}

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) {
	RE_inserthtml(id, body);
}

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 = "";
	if ($("name")) {
		$("name").value = "";
	}
	$("regist").value = "追加";
	$("cancel").style.visibility = "hidden";
	clearHighlight();
}

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 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 changeUpdirImage(id, dir, subdir, filename, size) {
	var img = $(id);
	var src = "http://" + location.hostname + "/image/get/D/" + dir;
	if (subdir != "") {
		src += "/S/" + subdir;
	}
	src += "/F/" + filename;
	if (size) {
		src += "/R/" + size;
	}
	if (img.src != src) {
		img.src = src;
	}
}

function deleteCol(obj) {
	//var tr = $("icons");
	//tr.removeChild(obj.parentNode);
	obj.parentNode.parentNode.removeChild(obj.parentNode);
}

function deleteRow(obj) {
	obj.parentNode.parentNode.parentNode.removeChild(obj.parentNode.parentNode);
}

function doAction(action, target) {
	document.forms[0].action = action;
	document.forms[0].target = target ? target : "";
}

function doPopup(url, secure) {
	window.open(
		(secure ? "https" : "http") + "://" + location.hostname + url
	);
}

function doRedirect(url, secure) {
	location.href = (secure ? "https" : "http") + "://" + location.hostname + url;
}

function doRedirectEx(url) {
	location.href = location.protocol + "//" + location.hostname + url;
}

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 if (mode == "grouping") {
		if (!confirm("商品を分類しますか?")) return;
	} else if (mode == "ungrouping") {
		if (!confirm("商品を解除しますか?")) return;
	} else if (mode == "reset") {
		if (!confirm("商品をリセットしますか?")) return;
	} else if (mode == "change_site") {
	} else {
		return;
	}
	viewMode.value = mode;
	viewData.value = data;
	document.forms[0].action = location.href;
	document.forms[0].submit();
}

function genTable(id) {
	var cols = $("cols").selectedIndex + 1;
	var rows = $("rows").selectedIndex + 1;
	var colwidth = $("colwidth").value;
	var dir = $("dir").selectedIndex;
	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\">";
	for (var y = 1; y <= rows; y++) {
		html += "<tr>";
		for (var x = 1; x <= cols; x++) {
			header = 0;
			value = y + ":" + x;
			if ((dir == 0 && y == 1) || (dir == 1 && 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>";
			} else {
				html += "</td>";
			}
		}
		html += "</tr>";
	}
	html += "</table>";
	RE_inserthtml(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;
		}
	}
	pop.style.display = "none";
}

function hideProgressBar() {
	var bar = $("progress_bar");
	bar.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 progressBar(percent) {
	var p = $("progress_percent");
	p.style.width = percent + "px";
}

function progressChangeMsg(msg) {
	$("progress_msg").innerHTML = msg;
}

function progressEnd(result_msg) {
	var b = $("progress_back");
	var w = $("progress_bar");
	b.style.cursor = "auto";
	w.style.display = "none";
	var msg = result_msg + "<br><br><input type='button' value='OK' onclick='progressHide()'>";
	progressChangeMsg(msg);
}

function progressHide(action) {
	var b = $("progress_back");
	b.style.display = "none";
}

function progressPostErr(id, msg) {
	$(id).innerHTML = msg;
}


function progressStart(){
	var b = $("progress_back");
	var d = $("progress_dialog");
	var p = $("progress_percent");
	var w = $("progress_bar");
	b.style.display = "block";
	b.style.cursor = "progress";
	d.style.left = scrollLeft() + parseInt((clientWidth() - d.offsetWidth) * 0.5) + "px";
	d.style.top = scrollTop() + parseInt((clientHeight() - d.offsetHeight) * 0.5 - 130) + "px";
	w.style.display = "block";
	p.style.width = "0px";
	progressChangeMsg("処理中です...");
}

function rakutenSuiteAdd(id, url) {
	$(id).src = location.protocol + "//" + location.hostname + url;
}

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 setSelectAll() {
	setSelectMultiple("category_save");
	setSelectMultiple("feature_save");
}

function setSelectMultiple(id, saved) {
	var select = $(id);
	if (!select) return;
	var option = select.firstChild;
	var value = "";
	while (option) {
		if (option.tagName == "OPTION") {
			option.selected = true;
			if (value) value += ",";
			value += option.value;
		}
		option = option.nextSibling;
	}
	if ($(saved)) {
		$(saved).value = value;
	}
}

function shiftDownRow(obj) {
	var curr = obj.parentNode.parentNode;
	if (!curr) return;
	var next = getNextNode(curr, "TR");
	if (!next) return;
	curr.parentNode.insertBefore(next, curr);
}

function shiftUpRow(obj, header_id) {
	var curr = obj.parentNode.parentNode;
	if (!curr) return;
	var prev = getPreviousNode(curr, "TR");
	if (!prev || prev.id == header_id) return;
	curr.parentNode.insertBefore(curr, prev);
}

function showBannerPopup() {
	var w;
	w = window.open(
		"http://" + location.hostname + "/control/popup/banner",
		"banner",
		"width=520,height=690"
	);
	if (w && !w.closed) {
		w.focus();
	}
}

function showBlockPopup(position) {
	var w;
	w = window.open(
		"http://" + location.hostname + "/control/popup/block/P/" + position,
		"block",
		"width=520,height=620"
	);
	if (w && !w.closed) {
		w.focus();
	}
}

function showColorPopup(id) {
	var w;
	w = window.open(
		"http://" + location.hostname + "/control/popup/color/id/" + id,
		"color",
		"width=520,height=740"
	);
	if (w && !w.closed) {
		w.focus();
	}
}

function showIconPopup() {
	var w;
	w = window.open(
		"http://" + location.hostname + "/control/popup/icon",
		"icon",
		"width=520,height=690"
	);
	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;
	}
	$("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 showProductPopup() {
	var w;
	w = window.open(
		"http://" + location.hostname + "/control/popup/product",
		"product",
		"width=520,height=750"
	);
	if (w && !w.closed) {
		w.focus();
	}
}

function showProgressBar() {
	var bar = $("progress_bar");
	bar.style.display = "block";
	bar.style.left = (scrollLeft() + parseInt((clientWidth() - bar.offsetWidth) * 0.5)) + "px";
	bar.style.top = (scrollTop() + parseInt((clientHeight() - bar.offsetHeight) * 0.5)) + "px";
}

function showQaPopup() {
	var w;
	w = window.open(
		"http://" + location.hostname + "/control/popup/qa",
		"qa",
		"width=520,height=700"
	);
	if (w && !w.closed) {
		w.focus();
	}
}

function showStoragePopup(id) {
	var w;
	w = window.open(
		"http://" + location.hostname + "/control/popup/storage/id/" + id,
		"storage",
		"width=520,height=710"
	);
	if (w && !w.closed) {
		w.focus();
	}
}

function showSuitePopup(number) {
	var w;
	w = window.open(
		"http://" + location.hostname + "/control/popup/suite/N/" + number,
		"suite",
		"width=520,height=730"
	);
	if (w && !w.closed) {
		w.focus();
	}
}

function showTablePopup(id) {
	var w;
	w = window.open(
		"http://" + location.hostname + "/control/popup/table/id/" + id,
		"table",
		"width=520,height=620"
	);
	if (w && !w.closed) {
		w.focus();
	}
}

function startEdit(obj, curr, name) {
	selectedObj = obj;
	$("view_mode").value = "update";
	$("view_data").value = curr;
	if ($("name")) {
		$("name").value = name;
	}
	$("regist").value = "変更";
	$("cancel").style.visibility = "visible";
	clearHighlight();
	selectedObj.parentNode.parentNode.style.cssText = "background-color: #FFFACD";
}

function toggleCheckbox(name, value) {
	var i = 0;
	while (document.forms[0].elements[name + "[" + i + "]"]) {
		document.forms[0].elements[name + "[" + i + "]"].checked = value;
		i++;
	}
}

function updateProgressBar(percent) {
	var value = $("progress_value");
	if (percent > 100) percent = 100;
	value.width = percent + "%";
}

