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 closeOpinionBox() {
	$("opinion_thanks").style.display="none";
	$("opinion_edit").style.display="none";
	$("opinion_dialog").style.display="none";
	$("opinion").className = "s04";
	
	var obj = $("opinion_txtarea");
	
	obj.removeChild(obj.lastChild);
	obj.innerHTML = '';

}

function closePriceList() {
	var pricelist = $("pricelist");
	pricelist.style.display = "none";
}

function createHttpRequest(){
	if(window.ActiveXObject){
		try {
			return new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				return new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				return null;
			}
		}
	} else if(window.XMLHttpRequest){
		return new XMLHttpRequest();
	} else {
		return null;
	}
}

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 = document.getElementById("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);
	if (isNaN(qty.value) || qty.value < 1) {
		qty.style.cssText = "";
	} else {
		qty.style.cssText = "background-color: #FFFACD";
	}
}

function getReferUrl() {
	return document.URL;
}

function hideStockPop() {
	var pop = $("stock_pop");
	pop.style.display = "none";
}

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 setAddressJSONP(data) {
	if (!data.pref) {
		alert("見つかりません。");
	} else {
		$("pref").selectedIndex = data.pref;
		$("city").value = data.city + data.town;
	}
}

function numberFormat(num) {
  return num.toString().replace( /([0-9]+?)(?=(?:[0-9]{3})+$)/g , '$1,' );
}

function openOpinionDialog() {
//	$("opinion_comment").value = "";
	$("opinion_edit").style.display = "block";
	$("opinion_thanks").style.display = "none";
	$("opinion_dialog").style.display = "block";
	$("opinion").className = "opinion_on";
	
	
	var textarea = $E("textarea");
	textarea.className = "opinion_mess";
	textarea.name = "opinion_mess";
	textarea.id = "opinion_comment";
	$("opinion_txtarea").appendChild(textarea);
	
}

function sendOpinion() {
	var data = $("opinion_comment").value;
	var url = location.protocol + "//" + location.hostname + "/opinion"
	if (data == "") {
		alert("ご意見を入力してください");
		return;
	}
	var httpoj = createHttpRequest();
	httpoj.open('POST', url, true);
	httpoj.onreadystatechange = function() {
		if (httpoj.readyState==4) {
			on_loaded(httpoj)
		}
	}
	httpoj.setRequestHeader('Content-Type' ,'application/x-www-form-urlencoded; charset=UTF-8')
	httpoj.send("opinion_comment=" + encodeURI(data).replace(/&/g , "%26"));
}

function on_loaded(oj) {
	res  = oj.responseText;
	if (res == 1) {
		$("opinion_edit").style.display="none";
		$("opinion_thanks").style.display="block";
	}
}

function showIconPopup(number) {
	var w;
	var link = "";
	if (number > 0) {
		link = "#icon_" + number;
	}
	w = window.open("http://" + location.hostname + "/icon" + link,
			"icon",
			"width=368,height=443, menubar=0, toolbar=0, scrollbars=0");
	if (w && !w.closed) {
		w.focus();
	}
}

function showGuideDialog(site, id) {
	var w;
	var url = "";
	if (site == 2) {
		url = "/partner";
	}
	w = window.open("http://" + location.hostname + url + "/guide2/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 = 0; i < 4; i++) {
		j = i * 2 + 1;
		if (prices[j] == 0) break;
		var tr = $E("tr");
		var th = $E("th");
		th.className = "my_th";
		th.appendChild($T(prices[j] + "～"));
		tr.appendChild(th);
		var td = $E("td");
		td.className = "my_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 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>※直前に、他のお客様よりご注文があった等により、ご注文のタイミングによっては、在庫不足となる場合がございます。何卒ご容赦くださいませ。"
}

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 swapImage(obj, filename) {
	obj.src = "http://" + location.hostname + "/image/" + filename;
}
