//<![CDATA[

var getCartPath			= '/shop/index/cart-form/';
var updateQuantityPath	= '/shop/index/update-quantity/';

function CartForm() {
	this.initialize.apply(this, arguments);
}

CartForm.prototype = {
	areaId : 'cartForm',
	spinnerId : 'cartSpinner',
	requestUri : getCartPath,
	initialize : function(requestUri, areaId, spinnerId) {
		if (areaId) {
			this.areaId = areaId;
		}
		if (spinnerId) {
			this.spinnerId = spinnerId;
		}
		if (requestUri) {
			this.requestUri = requestUri;
		}
	},
	request: function(code)
	{
		var parameters = '';
		
		this.loading();
		
		var ajax = new Ajax.Request(
			this.requestUri + 'code/' + code,
			{
				method		: 'get',
				onSuccess	: this.onSuccess.bind(this),
				onFailure	: this.error.bind(this),
				onException	: this.error.bind(this),
				parameters	: parameters
			}
		);
	},
	loading : function() {
		Element.hide(this.areaId);
		this.resizeLayer(this.spinnerId);
	},
	resizeLayer: function(id, flag)
	{
		var size = new Array();
		var size = Element.getDimensions(this.areaId);
		
		var elm = $(id);
		elm.style.width  = size['width'] + 'px';
		elm.style.height = size['height'] + 'px';
		elm.style.display = 'block';
	},
	onSuccess : function(res) {
		var resText = res.responseText;
		var elm = $(this.areaId);
		elm.innerHTML = this.convertResponseText(res);
		Element.hide($(this.spinnerId));
		Element.show(elm);
		
		this.postDispatch();
	},
	error : function() {
		var elm = $(this.areaId);
		elm.innerHTML = 'システムエラーが発生いたしました。';
		Element.hide($(this.spinnerId));
		Element.show(elm);
	},
		
	convertResponseText: function(res)
	{
		var text = res.responseText;
		// for safari
		if (navigator.appVersion.indexOf('KHTML') > -1) {
			var esc = escape(text);
			if (esc.indexOf('%u') < 0 && esc.indexOf('%') > -1) {
				text = decodeURIComponent(esc);
			}
		}
		return text;
	},
	
	postDispatch: function()
	{}

};

function ajaxUpdateQuantity(code, change) {
	var inst = new CartForm(updateQuantityPath + 'change/' + change + '/', 'ajaxFrame');
	inst.request(code);
}


/* the prevention of double submit
------------------------------------------------------------------------------*/
function FormUtil() {
	this.initialize.apply(this, arguments);
}

FormUtil.prototype = {
	initialize: function()
	{
	},
		
	submit: function(formId, btnId)
	{
		var elm = document.getElementById(btnId);
		// disable button and remove onclick event.
		elm.disabled = true;
		elm.onclick = null;
		document.getElementById(formId).submit();
	}
}


//]]>


