//AJAX cart functions begin
//N.B. TO WORK WITH PROTOTYPEJS library (http://www.prototypejs.org/download) 

function deleteCart(delete_url){
	$('basket_short').update('Loading...');
	new Ajax.Request(delete_url,	{
		onSuccess: function(transport) {
			var response=transport.responseText.evalJSON();
			com_virtuemart_basket_short(response.result);
		}
	});
}

function addToCart(product_id){
	var submitTo = 'index.php';
	$('basket_short').update('Loading...');
	product_form=$('addtocart' + product_id);
	new Ajax.Request('index.php?option=com_virtuemart&task=cartadd',	{
		method: 'get',
		parameters: '&product_id='+product_form.product_id.value + '&quantity='+product_form.quantity.value + '&page='+product_form.page.value + '&ch=json' + '&func='+product_form.func.value + '&Itemid='+product_form.Itemid.value + '&lang='+product_form.lang.value + '&category_id='+product_form.category_id.value,
		onSuccess: function(transport) {
			var response=transport.responseText.evalJSON();
			com_virtuemart_basket_short(response.result);
		}
	}); 
}

function com_virtuemart_basket_short(result){
	var htmlResult="";
	var labels = result.labels;
	if(result.product_list.length > 0){
		htmlResult="<table class=\"cart_short\">";
		htmlResult += "<tr><th class=\"name\">" + labels["name_header"] + "</th>";
		htmlResult += "<th class=\"quantity\">" + labels["quantity"] + "</th>";
		htmlResult += "<th class=\"subtotal\">" + labels["subtotal_header"] + "</th>";
		htmlResult += "<th class=\"delete\"></th></tr>";
		var totQuantity=0;
		for (var i = 0; i < result.product_list.length; i++){
			var productName = result.product_list[i].product_name;
			var productQty = result.product_list[i].product_quantity;
			totQuantity += parseInt(productQty);
			var productPrice = result.product_list[i].product_price;
			var productSubtotal = result.product_list[i].subtotal;
			var deleteForm = result.product_list[i].delete_form;
			var deleteButton = "<a href=\"" + result.product_list[i].delete_button["link_url"] + "\"";
				deleteButton +=  " onClick=\"deleteCart('" + result.product_list[i].delete_button["link_url"] + "&ch=json');return false;\"";
				deleteButton +=  " title=\"" + result.product_list[i].delete_button["link_title"] + "\">";
				deleteButton += "<img src=\"" + result.product_list[i].delete_button["image_url"];
				deleteButton += "\" alt=\"" + result.product_list[i].delete_button["image_alt"] + "\" /></a>";
			htmlResult += "<tr class=\"cart_product_row\" >";
			htmlResult += "<td class=\"name\">" + productName + "</td>";
			htmlResult += "<td class=\"quantity\">" + productQty + "</td>";
			htmlResult += "<td class=\"subtotal\">" + productSubtotal + "</td>";
			htmlResult += "<td class=\"delete\">" + deleteButton + "</td>";
			htmlResult += "</tr>";
		};
		htmlResult += "<tr><td colspan=\"4\">" + totQuantity + " " + labels["products"] +"</td><td></td></tr>";
		htmlResult += "<tr><td class=\"value\" colspan=\"3\">" + result.total + "</td><td></td></tr>";
		htmlResult+="</table>";
	}else{
		htmlResult=labels["empty_cart"];	
	}
	var obj = document.getElementById('basket_short');
	obj.innerHTML = htmlResult; 
}
//AJAX cart functions end