/*
if (!document.all) {
	(function() {
		var events = ["mousedown", "mouseover", "mouseout", "mousemove",
					  "mousedrag", "click", "dblclick", "keydown"];
		for (var i = 0; i < events.length; i++){
			window.addEventListener(events[i], function(e) {
			window.event = e;
		}, true);
	}
	}());
}
*/

function changeImage(filename, width, height) {
	var img = $("main_pic");
	var src = "http://" + location.hostname + "/updir/suite/" + filename;
	if (img.src != src) {
		img.src = src;
		img.style.width = width + "px";
		img.style.height = height + "px";
	}
}

function closePriceList() {
	var pricelist = $("pricelist");
	pricelist.style.display = "none";
}

function decQty(id) {
	var qty = $("qty_" + id);
	if (!isNaN(qty.value)) {
		if (qty.value > 0) {
			qty.value--;
		}
	} else {
		qty.value = 0;
	}
	drawBackground(id);
}

function doSearch(site) {
	var keyword = $("keyword");
	if (!keyword) return;
	var url = "";
	if (site == 2) {
		url = "/partner";
	}
	location.href = "http://" + location.hostname + url + "/search/listname/KW/" + encodeURI(keyword.value);
}

function doSearchByMap(pref) {
	$("pref").options[pref].selected = true;
	document.forms[0].submit();
}

function drawBackground(id) {
	var qty = $("qty_" + id);
	var row = $("row_" + id);
	if (isNaN(qty.value) || qty.value < 1) {
		row.style.cssText = "background-color: #FFFFFF";
	} else {
		row.style.cssText = "background-color: #FFFACD";
	}
}

function incQty(id) {
	var qty = $("qty_" + id);
	if (!isNaN(qty.value)) {
		qty.value++;
	} else {
		qty.value = 0;
	}
	drawBackground(id);
}

function getAddressJSONP() {
	var z1 = $("zipcode1").value;
	var z2 = $("zipcode2").value;
	if (z1 == "" || z2 == "") {
		alert("郵便番号が入力されていません。");
	} else {
		//var url = location.protocol + "//" + location.hostname + "/zipcode/search/ZC/" + z1 + z2;
		var url = location.protocol + "//www.shift-net.co.jp/zipcode/search/ZC/" + z1 + z2;
		var sc = document.createElement("script");
		sc.setAttribute("type", "text/javascript");
		sc.setAttribute("src", url);
		document.getElementsByTagName("body").item(0).appendChild(sc);
	}
}

function hideStockPop() {
	var pop = $("stock_pop");
	pop.style.display = "none";
}

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 showGuideDialog(site, id) {
	var w;
	var url = "";
	if (site == 2) {
		url = "/partner";
	}
	w = window.open("http://" + location.hostname + url + "/guide/index/id/" + id,
			"guide",
			"width=800,height=720");
	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.className = "my_th";
	th.style.cssText = "background-color: #E8E8E8";
	var t = $T("ご注文数量");
	th.appendChild(t);
	tr.appendChild(th);
	var th = $E("th");
	th.className = "my_th";
	th.style.cssText = "background-color: #E8E8E8";
	var t = $T("販売価格");
	th.appendChild(t);
	tr.appendChild(th);
	tbody.appendChild(tr);
	var j = 0;
	for (i = 1; i < prices.length; i++) {
		if (prices[i] != prices[j]) {
			var tr = $E("tr");
			var th = $E("th");
			th.className = "my_th";
			var t = $T((5 * j + 1) + "～" + (5 * i) + "個");
			th.appendChild(t);
			tr.appendChild(th);
			var td = $E("td");
			td.className = "my_td";
			t = $T(numberFormat(prices[j]) + "円");
			td.appendChild(t);
			tr.appendChild(td);
			tbody.appendChild(tr);
			j = i;
		}
	}
	var tr = $E("tr");
	var th = $E("th");
	th.className = "my_th";
	var t = $T((5 * j + 1) + "個～");
	th.appendChild(t);
	tr.appendChild(th);
	var td = $E("td");
	td.className = "my_td";
	t = $T(numberFormat(prices[j]) + "円");
	td.appendChild(t);
	tr.appendChild(td);
	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";
/*
	pricelist.style.left = scrollLeft() + event.clientX + "px";
	pricelist.style.top = scrollTop() + event.clientY + "px";
*/
}

function showQaPopup(id, number) {
	var w;
	var url = "";
	w = window.open("http://" + location.hostname + "/qa/index/id/" + id + "#" + number,
			"qa",
			"width=800,height=720");
	if (w && !w.closed) {
		w.focus();
	}
}

function showStockPop(name, stock, stock_unlimit, due_date) {
	var pop = $("stock_pop");
	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";
	var stock_name = $("stock_name");
	stock_name.innerHTML = name;
	var stock_count = $("stock_count");
	if (stock_unlimit || stock > 9) {
		stock_count.innerHTML = "10以上";
	} else if (stock < 1) {
		stock_count.innerHTML = "別倉庫";
	} else {
		stock_count.innerHTML = stock;
	}
	var comment = $("stock_comment");
	comment.innerHTML = "※在庫数を超えてのご注文は、「<b>" + due_date + "</b>」です。<br>※直前に、他のお客様よりご注文があった等により、ご注文のタイミングによっては、在庫不足となる場合がございます。何卒ご容赦くださいませ。"
}
