// ＵＴＦ－８（ＢＯＭなし）ＬＦ
// Copyright(c) 2009-2010 E TRUST, INC.
var _tm;
var _path = (location.pathname.replace("tegaki-yuzen.info/", "").indexOf("/",1) > -1) ? "../" : "";
var _busy_ = false;
var _stat_ = false;
document.onkeydown = KeyEvent;
function KeyEvent(e){
	if(event.keyCode == 13 && 
		window.event.srcElement.type != 'submit' &&
		window.event.srcElement.type != 'textarea'){
		return false;
	}}
function doSubmit() {
 return (location.protocol != "file:")
}
function debug() {
	var elm = $('debug');
	if (!elm) return;
	for (var n=0; n<arguments.length; n++) {
		if (!$(arguments[n])) elm.innerHTML += arguments[n] + " is not found.";
		else elm.innerHTML += arguments[n] + "=" + $F(arguments[n]) + "<br />";
	}}
function dump(name, value) {
	var elm = $('debug');
	if (!elm) return;
	elm.innerHTML += name + "=" + value + "<br />";
}
function getPostParams() {
	var arr = new Array();
	for (var n=0; n<arguments.length; n++) {
		if (!$(arguments[n])) continue;
		arr[n] =	arguments[n] + "=" + $F(arguments[n]);
	}
	return arr.join('&');
}
Event.observe(window, 'load', function() {
	var prms = new Array();
	prms[0] = "host=" + location.host;
	prms[1] = "path=" + location.pathname;
	prms[2] = "useragent=" + encodeURIComponent(navigator.userAgent);
	prms[3] = "referrer=" + encodeURIComponent(document.referrer);
	prms[4] = "width=" + screen.width;
	prms[5] = "height=" + screen.height;
	prms[6] = "color=" + screen.colorDepth;
	new Ajax.Request(_path + "Lib/TrackPage.class", {
		"method": "post", "parameters": prms.join("&"), onSuccess: function(req) {
			_tm = req.responseText; }});
});
window.onunload = function() {
	new Ajax.Request(_path + "Lib/TrackPage.class", {
		"method": "post", "asynchronous": false, "parameters": "tm=" + _tm });
}
function coreLogin() {
	if (location.protocol == "file:") return;
	location.href = _path + "shop/login.php";
}
function coreSearchWord(id) {
	if (location.protocol == "file:" || $F(id) == "") return;
	location.href = _path + "shop/index.html?q=" + encodeURIComponent($F(id));
}
function coreSelect(id, swp) {
	if (location.protocol == "file:") return;
	if (_busy_ || $F(id) == "") return;
	_busy_ = true;
	new Ajax.Request(_path + "Lib/ShopRequest.class", {
		"method": "post", "parameters": "group=1&v=" + $F(id),
		onSuccess: function(req) {
			if (req.responseText.indexOf("FAILURE:") == 0) $(swp).innerHTML = "";
			else $(swp).innerHTML = req.responseText;
		},
		onComplete: function(req) {
			//dump("res", req.responseText.escapeHTML());
			_busy_ = false;
		}});
}
function coreSearchItem() {
	if (location.protocol == "file:" || _busy_) return;
	var dt = new Date();
	$('search').value = dt.getTime();
	_busy_ = true;
	_stat_ = false;
	new Ajax.Request(_path + "Lib/ShopRequest.class", {
		"method": "post", "parameters": $('frm_search').serialize(),
		onSuccess: function(req) {
			if (req.responseText == "SUCCEED:") _stat_ = true;
		},
		onComplete: function(req) {
			//dump("res", req.responseText.escapeHTML());
			if (_stat_) location.href = _path + "shop/index.html?sh=" + $('search').value;
			_busy_ = false;
		}});
}
function coreSort(prm) {
	if (location.protocol == "file:" || _busy_) return;
	_busy_ = true;
	_stat_ = false;
	new Ajax.Request(_path + "Lib/ShopRequest.class", {
		"method": "post", "parameters": "sort=1&v=" + prm,
		onSuccess: function(req) {
			if (req.responseText == "SUCCEED:") _stat_ = true;
		},
		onComplete: function(req) {
			//dump("res", req.responseText.escapeHTML());
			_busy_ = false;
			if (_stat_) coreLoadShop();
		}});
}
function corePage($page) {
	if (location.protocol == "file:") return;
	var url = _path + "shop/index.html?p=" + $page;
	var arr, n;
	if (location.search != "") {
		arr = location.search.replace("?", "").split('&');
		for (n = 0; n < arr.length; n++) {
			if (arr[n].indexOf("p=") > -1) continue;
			url += "&" + arr[n];
		}
	}
	location.href = url;
}
function coreLoadShop() {
	if (_busy_) return;
	if (location.protocol == "file:") {
		$('dv_view').style.display = "block";
		return;
	}
	new Ajax.Request(_path + "Lib/ShopRequest.class", {
		"method": "post", "parameters": "load=1&" + location.search.replace("?", ""),
		onSuccess: function(req) {
			$('dv_view').innerHTML = req.responseText;
			$('dv_view').style.display = "block";
		},
		onComplete: function(req) {
			//dump("res", req.responseText.escapeHTML());
			_budy_ = false;
		}});
}
function coreLoadMember(_point_) {
	if (!$('login')) return;
	if (location.protocol == "file:") {
		$('login').style.display = "block";
		return;
	}
	new Ajax.Request(_path + "shop/login.php", {
	"method": "post", "parameters": "member=1&point=" + _point_ + "&path=" + location.pathname,
	onSuccess: function(req) {
		if (req.responseText.indexOf("FAILURE:") != 0) $('login').innerHTML = req.responseText;
	},
	onComplete: function(req) {
		//dump("res", req.responseText.escapeHTML());
		$('login').style.display = "block";
	}});
}
function coreLoadCart() {
	if (!$('shopping_cart')) return;
	if (location.protocol == "file:") {
		$('shopping_cart').style.display = "block";
		return;
	}
	new Ajax.Updater("dv_cart", _path + "Lib/ShopCart.class", {
		"method": "post", "parameters": "incart=1",
		onComplete: function(req) {
			//dump("res", req.responseText.escapeHTML());
			$('shopping_cart').style.display = "block";
		}});
}
function coreLoadStock(id) {
	if (location.protocol == "file:") {
		$('dv_item_' + id).style.display = "block";
		return;
	}
	sub = (arguments.length > 1) ? "1" : "0";
	new Ajax.Request(_path + "Lib/ShopCart.class", {
	"method": "post", "parameters": "stock=1&item=" + id + "&sub=" + sub,
	onSuccess: function(req) {
		if (req.responseText.indexOf("FAILURE:") != 0) $('dv_item_' + id).innerHTML = req.responseText;
		$('dv_item_' + id).style.display = "block";
	},
	onComplete: function(req) {
		//dump("res", req.responseText.escapeHTML());
	}});
}
function coreAddCart(item) {
	if (location.protocol == "file:" || _busy_) return;
	_busy_ = true;
	_stat_ = false;
	new Ajax.Request(_path + "Lib/ShopCart.class", {
		"method": "post", "parameters": $('frm_item_' + item).serialize(),
		onSuccess: function(req) {
			if (req.responseText == "SUCCEED:") _stat_ = true;
		},
		onComplete: function(req) {
			//dump("res", req.responseText.escapeHTML());
			_busy_ = false;
			if (_stat_) { alert("カートに追加しました。"); coreLoadCart(); }
		}});
}
function corePicup(item) {
	if (location.protocol == "file:" || _busy_) return;
	_busy_ = true;
	_stat_ = false;
	new Ajax.Request(_path + "Lib/ShopCart.class", {
		"method": "post", "parameters": "pickup=1&item=" + item,
		onSuccess: function(req) {
			if (req.responseText == "SUCCEED:") _stat_ = true;
		},
		onComplete: function(req) {
			//dump("res", req.responseText.escapeHTML());
			_busy_ = false;
			if (_stat_) { alert("カートに追加しました。"); coreLoadCart(); }
		}});
}
function coreContact(item) {
	if (location.protocol == "file:") return;
	_busy_ = true;
	_stat_ = false;
	new Ajax.Request(_path + "shop/contact.php", {
		"method": "post", "parameters": $('frm_item_' + item).serialize(),
		onSuccess: function(req) {
			if (req.responseText.indexOf("FAILURE:") != 0) _stat_ = req.responseText;
		},
		onComplete: function(req) {
			//dump("res", req.responseText.escapeHTML());
			_busy_ = false;
			if (_stat_) location.href = req.responseText;
		}});
}
function coreMoveCart() {
	if (location.protocol == "file:") return;
	location.href = _path + "shop/cart.html";
}
function coreMoveForm(url, id) {
	if (location.protocol == "file:") return;
	$('hd').value = id;
	$('formArea').action = url + "shop/form.php";
	$('formArea').method = "post";
	$('formArea').submit();
}
function coreLoadCartPage() {
	if (location.protocol == "file:" || _busy_) return;
	_busy_ = true;
	new Ajax.Request(_path + "Lib/ShopCart.class", {
		"method": "post", "parameters": "cartpage=1",
		onSuccess: function(req) {
			$('dv_cartitem').innerHTML = req.responseText;
			$('dv_cartitem').style.display = "block";
		},
		onComplete: function(req) {
			//dump("res", req.responseText.escapeHTML());
			_busy_ = false;
		}});
}
function coreReset(id) {
	if (location.protocol == "file:" || _busy_) return;
	_busy_ = true;
	new Ajax.Request(_path + "Lib/ShopCart.class", {
		"method": "post", "parameters": "reset=1&id=" + id + "&v=" + $F('tx_' + id),
		onSuccess: function(req) {
			$('dv_cartitem').innerHTML = req.responseText;
		},
		onComplete: function(req) {
			//dump("res", req.responseText.escapeHTML());
			_busy_ = false;
		}});
}
function coreRemove(id) {
	if (location.protocol == "file:" || _busy_) return;
	_busy_ = true;
	new Ajax.Request(_path + "Lib/ShopCart.class", {
		"method": "post", "parameters": "remove=1&id=" + id,
		onSuccess: function(req) {
			$('dv_cartitem').innerHTML = req.responseText;
		},
		onComplete: function(req) {
			//dump("res", req.responseText.escapeHTML());
			_busy_ = false;
		}});
}
function coreLoadDeliv() {
	if (location.protocol == "file:" || _busy_) return;
	_busy_ = true;
	new Ajax.Request(_path + "shop/step2.php", {
		"method": "post", "parameters": "load=1",
		onSuccess: function(req) {
			$('dv_deliv').innerHTML = req.responseText;
		},
		onComplete: function(req) {
			//dump("res", req.responseText.escapeHTML());
			_busy_ = false;
		}});
}
function coreAddDeliv() {
	if (location.protocol == "file:" || _busy_) return;
	_busy_ = true;
	new Ajax.Request(_path + "shop/step2.php", {
		"method": "post", "parameters": $('frm_deliv').serialize(),
		onSuccess: function(req) {
			if (req.responseText == "SUCCEED:") _stat_ = true;
		},
		onComplete: function(req) {
			//dump("res", req.responseText.escapeHTML());
			_busy_ = false;
			if (_stat_) coreLoadDeliv(); else alert(req.responseText);
		}});
}
function coreRemoveDeliv(id) {
	if (location.protocol == "file:" || _busy_) return;
	_busy_ = true;
	new Ajax.Request(_path + "shop/step2.php", {
		"method": "post", "parameters": "remove=1&id=" + id,
		onSuccess: function(req) {
			if (req.responseText == "SUCCEED:") _stat_ = true;
		},
		onComplete: function(req) {
			//dump("res", req.responseText.escapeHTML());
			_busy_ = false;
			if (_stat_) coreLoadDeliv();
		}});
}