$(document).ready(
	function ()
	{
		$("a.but_update").bind('click',
			function(e)
			{
				update_cart(e.target.id.split('_')[1]);
				return false;
			}
		)
	}
);

costshipping = 0;

function update_cart(id)
{
	line = getdataforid(id);

	// line["subfield"].text("$"+(parseFloat(line["subtotal"]).toFixed(2)));
	
	// add to the cart
	// empty the cart items
	$("#autofields").empty();
	var total = 0;
	var findex = 1;
	var totalquantity = 0;
	$("select.quanitity_select").each(
		function(i)
		{
			var line = getdataforid(this.id.split('_')[1]);
			var index = i+1;
			
			if(line["quantity"] > 0)
			{
				// add the subtotal
				line["subfield"].text("$"+(parseFloat(line["subtotal"]).toFixed(2)));
			
				// add the object fields to the buy form
				$("#autofields").append('<input type="hidden" name="item_name_'+findex+'" value="'+line["name"]+'"/>');
				$("#autofields").append('<input type="hidden" name="amount_'+findex+'" value="'+line["unitprice"]+'"/>');
				$("#autofields").append('<input type="hidden" name="quantity_'+findex+'" value="'+line["quantity"]+'"/>');
				findex++;
				totalquantity+=Number(line["quantity"]);
				
				// add the total cost
				total += line["subtotal"];
			} else
			{
				line["subfield"].html("&nbsp;");
			}
		}
	)
			
	// add the price to the form
	$("input#paypal_total").attr("value",total);
	$("input#paypal_shipping").attr("value",costshipping);
	$("span#cost_total").text("$"+(parseFloat(total).toFixed(2)));
}

function getdataforid(id)
{
	var rtn = {};
	rtn["subtotal"] = 0;

	if(!id) return rtn;
	
	var name = $("#name_"+id).text();
	var sub = $("#subtotal_"+id+" span");
	var dd = $("#quantity_"+id);
	var quantity = parseInt($("select#quantity_"+id+" option:selected").val());
//	var quantity = parseInt($("input#quantity_"+id).val());
	var unitprice = parseFloat($("#price_"+id+" span").text().substring(1));
	unitprice = (Math.round(unitprice * 100)) / 100;
	var subtotal = 0;
	
	if(!dd || !sub || isNaN(unitprice) || isNaN(quantity)) return rtn;
	
	rtn["subtotal"] = quantity * unitprice;
	rtn["unitprice"] = unitprice;
	rtn["name"] = name;
	rtn["quantity"] = quantity;
	rtn["subfield"] = sub;
	
	return rtn;
}