new Event.observe(window,"load",initCart);

function initCart() {
	$$("input.fcm_shop_add_num").each(function(o) {
		
	});
	$$("input.fcm_shop_cart_quantity").each(function(o) {
		new Event.observe(o,"change",updateCartRow);
	});
	$$("button.fcm_shop_cart_delete").each(function(o) {
		new Event.observe(o,"click",deleteCartRow);
	});
	$$("button.shop_order_button").each(function(o) {
		new Event.observe(o,"click",sendOrder);
	});
}
function addToCart(what) {
	var id = what.adjacent("input.fcm_shop_add_id")[0];
	var quantity = what.adjacent("input.fcm_shop_add_quantity")[0];
	if(quantity.value<1) return false;
	new Ajax.Request("/?ma=add_cart_row",{
		method: "post",
		postBody: "pid="+id.value+"&quantity="+quantity.value,
		onComplete: function(response) {
			if(response.responseText.length>0) alert(response.responseText);
			else {
				reloadCart();
				quantity.value="1";
				document.body.scrollTo();
			}
		}
	});
}
function reloadCart() {
	if($$(".fcm_shop_cart_container")) {
		$$(".fcm_shop_cart_container").each(function(o) {
			if(o.id) {
				new Ajax.Updater(o.id,"?l=cart&container="+o.id,{
					method: "get",
					onComplete: function(response) {
						initCart();
						initShop();
					}
				});
			}
		});
	}
}
function updateCartRow(e) {
	what = e.findElement();
	if(!what) return false;
	var id = what.adjacent("input.fcm_shop_cart_id")[0];
	var quantity = what.value;
	what.value = what.value.replace(".",",");
	new Ajax.Request("/?ma=edit_cart_row",{
		method: "post",
		postBody: "id="+id.value+"&quantity="+quantity,
		onComplete: function(response) {
			if(response.responseText.length>0) alert(response.responseText);
			else {
				reloadCart();
			}
		}
	});
}
function deleteCartRow(e) {
	var what = e.findElement();
	if(!what) return false;
	var id = what.adjacent("input.fcm_shop_cart_id")[0];
	if(!id) return false;
	if(confirm("Är du säker på att du vill ta bort raden?")) {
		new Ajax.Request("/?ma=delete_cart_row&id="+id.value,{
			method: "post",
			onComplete: function(response) {
				if(response.responseText.length>0) alert(response.responseText);
				else reloadCart();
			}
		});
	}
}
function changeDeliveryMethod(what) {
	new Ajax.Request("/?ma=change_cart_delivery_method",{
		method: "post",
		postBody: "deliverymethodid="+what.value,
		onComplete: function(response) {
			if(response.responseText.length>0) alert(response.responseText);
			else reloadCart();
		}
	});
}
function changePaymentMethod(what) {
	new Ajax.Request("/?ma=change_cart_payment_method",{
		method: "post",
		postBody: "paymentmethodid="+what.value,
		onComplete: function(response) {
			if(response.responseText.length>0) alert(response.responseText);
			else reloadCart();
		}
	});
}
function sendOrder(e) {
	new Ajax.Request("?ma=send_shop_order",{
		method: "post",
		onComplete: function(response) {
			if(response.responseText.length>0) alert(response.responseText);
			else {
				alert("Beställningen skickad!");
				reloadCart();
			}
		}
	});
}
function clearCart() {
	new Ajax.Request("?ma=clear_cart",{
		method: "post",
		onComplete: function(response) {
			if(response.responseText.length>0) alert(response.responseText);
			else reloadCart();
		}
	});
}
