window.addEvent('domready', function(){
	
	if($('Cart_OrderForm_delivery')) 
	{
		$('Cart_OrderForm_delivery').setStyles({'visibility': 'hidden','display':'none'});
		$('showDelivery').addEvent('change', function(){
			if($('Cart_OrderForm_delivery').getStyle('visibility') == 'hidden')
			{
				$('Cart_OrderForm_delivery').setStyles({'visibility': 'visible','display':'block'});
			}
			else
			{
				$('Cart_OrderForm_delivery').setStyles({'visibility': 'hidden','display':'none'});
			}
		});
	}
	$$('.product_qty').each(function(qtyItem){
		qtyItem.addEvent('change', function()
		{
			var order_id = $('orderID').get('value');
			var product_id = this.get('id').split('_')[1];
			var newQty = this.get('value').toInt();
			var req = new Request.JSON({
	            url: '/AJAX/index.php?view=cart&event=setProductQty&productID='+product_id+'&newQty='+newQty,
	    		onSuccess: function(JSONObject) {
	    			var price_field = 'qty_price_'+product_id; 
	    			var price = commaToDot($(price_field).get('html'));
	    			var newPrice = price * newQty;
	    			newPrice = dotToComma(formatPriceTag(newPrice));
	    			var rp_field = 'rp_tuote_'+product_id;
					$(rp_field).set('html', newPrice);
					updateTotalPrice();
	    		}
	    	}).send();
	    });
	});
});

function updateTotalPrice()
{
	var total = 0;
	$$('.totalPrice').each(function(item){
		var price = commaToDot(item.get('html'));
		total += price.toFloat();
	});
	total = dotToComma(formatPriceTag(total));
	$('checkoutTotal').set('value', total);
}

