function addAddress(name, kana, zipcode, pref, city, town, tel, email) {
	if (window.opener && !window.opener.closed) {
		window.opener.addAddressInfo(name, kana, zipcode, pref, city, town, tel, email);
	} else {
		alert("親ウインドウがありません。");
	}
}

function addAddressInfo(name, kana, zipcode, pref, city, town, tel, email) {
	$("name").value = name;
	$("kana").value = kana;
	$("zipcode").value = zipcode;
	$("pref").value = pref;
	$("city").value = city;
	$("town").value = town;
	$("tel").value = tel;
	$("email0").value = email;
	$("email1").value = email;
}

function addBookmark(title, url) {
	if (window.sidebar) {
		window.sidebar.addPanel(title, url, "");
	} else if (document.all) {
		window.external.AddFavorite(url, title);
	} else if (window.opera && window.print) {
		return true;
	}
}

function closePriceList() {
	var pricelist = $("pricelist");
	pricelist.style.display = "none";
}

function decQuantity(index) {
	var q = $("quantity" + index);
	if (!isNaN(q.value)) {
		if (q.value > 0) {
			q.value--;
		}
	} else {
		q.value = 0;
	}
}

function doSearch() {
	var keyword = $("keyword");
	if (!keyword || !keyword.value) return;
	doRedirect("/search/index/W/" + encodeURI(keyword.value));
}

function getAddressJSONP() {
	var z = $("zipcode").value;
	if (z == "") {
		alert("郵便番号が入力されていません。");
	} else {
		var url = location.protocol + "//www.shift-net.co.jp/zipcode/search/ZC/" + z.replace("-", "");
		var sc = document.createElement("script");
		sc.setAttribute("type", "text/javascript");
		sc.setAttribute("src", url);
		document.getElementsByTagName("body").item(0).appendChild(sc);
	}
}

function hideStockDialog() {
	$("stock_dialog").style.display = "none";
}

function incQuantity(index) {
	var q = $("quantity" + index);
	if (!isNaN(q.value)) {
		q.value++;
	} else {
		q.value = 0;
	}
}

function numberFormat(num) {
  return num.toString().replace( /([0-9]+?)(?=(?:[0-9]{3})+$)/g , '$1,' );
}

function setAddressJSONP(data) {
	if (!data.pref) {
		alert("見つかりません。");
	} else {
		$("pref").selectedIndex = data.pref;
		$("city").value = data.city + data.town;
	}
}

function showAddressPopup() {
	var w;
	w = window.open(
		"https://" + location.hostname + "/address/popup",
//		"http://" + location.hostname + "/address/popup",
		"addr",
		"width=520,height=620"
	);
	if (w && !w.closed) {
		w.focus();
	}
}

function showGuidePopup(page, section) {
	var w;
	var url = "http://" + location.hostname + "/guide";
	if (page) {
		url += "/index/P/" + page;
		if (section) {
			url += "#" + section;
		}
	}
	w = window.open(url,
		"guide",
		"width=800,height=620");
	if (w && !w.closed) {
		w.focus();
	}
}

function showPriceList(title, contents) {
	var pricelist = $("pricelist");
	var caption = $("pricelist_caption");
	caption.innerHTML = title;
	var tbody = $("pricelist_tbody");
	dropChild(tbody);
	var prices = contents.split(",");
	var i;
	var tr = $E("tr");
	var th = $E("th");
	th.style.cssText = "background-color: #E8E8E8";
	var t = $T("ご注文数量");
	th.appendChild(t);
	tr.appendChild(th);
	var th = $E("th");
	th.style.cssText = "background-color: #E8E8E8";
	var t = $T("販売価格");
	th.appendChild(t);
	tr.appendChild(th);
	tbody.appendChild(tr);
	var j = 0;
	for (i = 0; i < 4; i++) {
		j = i * 2 + 1;
		if (prices[j] == 0) break;
		var tr = $E("tr");
		var th = $E("th");
		th.appendChild($T(prices[j] + "～"));
		tr.appendChild(th);
		var td = $E("td");
		t = $T(numberFormat(prices[j + 1]) + "円");
		td.appendChild(t);
		tr.appendChild(td);
		tbody.appendChild(tr);
	}
	tbody.appendChild(tr);
	pricelist.style.display = "block";
	pricelist.style.left = (scrollLeft() + parseInt((clientWidth() - pricelist.offsetWidth) * 0.5)) + "px";
	pricelist.style.top = (scrollTop() + parseInt((clientHeight() - pricelist.offsetHeight) * 0.5)) + "px";
}

function showQaPopup(id, number) {
	var w;
	var url = "http://" + location.hostname + "/qa";
	if (id) {
		url += "/index/ID/" + id;
	}
	if (number) {
		url += "#" + number;
	}
	w = window.open(url,
		"qa",
		"width=800,height=620");
	if (w && !w.closed) {
		w.focus();
	}
}

function showStockDialog(name, stock, stock_unlimit, arrival_note) {
	var d = $("stock_dialog");
	d.style.display = "block";
	d.style.left = (scrollLeft() + parseInt((clientWidth() - d.offsetWidth) * 0.5)) + "px";
	d.style.top = (scrollTop() + parseInt((clientHeight() - d.offsetHeight) * 0.5)) + "px";
	$("stock_name").innerHTML = name;
	var count = $("stock_count");
	if (stock_unlimit || stock > 9) {
		count.innerHTML = "10以上";
	} else if (stock < 1) {
		count.innerHTML = "別倉庫";
	} else {
		count.innerHTML = stock;
	}
	var note = $("stock_note");
	if (arrival_note == "") {
		note.innerHTML = "※在庫数を超えてのご注文は、事前にお問い合わせください。";
	} else {
		note.innerHTML = arrival_note;
	}
	note.innerHTML += "<br>※直前に、他のお客様よりご注文があった等により、ご注文のタイミングによっては、在庫不足となる場合がございます。何卒ご容赦くださいませ。";
}

