function adjustQty(product_id)
{

	var qty = $(product_id+'-qty');
	fixInputBox(qty,0);


	new Request({
		method: 'post',
		url: 'shoppingCart.php',
		onComplete : function()
		{
			calcLineTotal(product_id);
			displayTotal(this.response.text);
		}
	}).send("func=adjustQty&product_id="+product_id+"&qty="+qty.value+"&selectable_options_value="+escape($(product_id+'-selectable_options_value').innerHTML));
}
function removeProduct(product_id)
{
	new Request({
		method: 'post',
		url: 'shoppingCart.php',
		onComplete : function()
		{
			$(product_id+'-product_row').dispose();
			displayTotal(this.response.text);
		}
	}).send("func=removeProduct&product_id="+product_id+"&selectable_options_value="+escape($(product_id+'-selectable_options_value').innerHTML));
}
function changeShippingOption()
{
	new Request({
		method: 'post',
		url: 'shoppingCart.php',
		onComplete : function()
		{
			//displayTotal(this.response.text);
			//var json_array = JSON.decode(this.response.text);
			//$('shipping_total').innerHTML = json_array.shipping_total;
		}
	}).send("func=changeShippingOption&shipping_options_id="+$('shipping_options_id').value);
}


function calcLineTotal(product_id)
{
	var cost = $(product_id+'-cost');
	var qty = $(product_id+'-qty');

	$(product_id+'-product_total').innerHTML =((cost.innerHTML *1) * (qty.value *1)).toFixed(2);
}

function displayTotal(json_string)
{
	var json_array = JSON.decode(json_string);

	$('sub_total').innerHTML = json_array.sub_total;
	$('tax').innerHTML = json_array.tax_amount;
	
	$('grand_total').innerHTML = json_array.grand_total; 
	



	if(json_array.promo_discount_amount > 0)
	{
		$('promo_code_check').addClass('positive');
		$('promo_code_amount').innerHTML = json_array.promo_discount_amount.toFixed(2);
	}
	else
	{
		$('promo_code_check').removeClass('positive');
		$('promo_code_amount').innerHTML = '0.00';
	}
	
	try
	{
		$('shipping_total').innerHTML = json_array.shipping_total;
	}
	catch(err)
	{
		
	}

}
function validatePromoCode(element)
{
	new Request({
		method: 'post',
		url: 'shoppingCart.php',
		onComplete : function()
		{
			displayTotal(this.response.text);
		}
	}).send("func=validatePromoCode&promo_code="+element.value);
}







function fixInputBox(i,fixedPlace)
{
	i.value = Math.abs(i.value*1);
	i.value = (i.value*1).toFixed(fixedPlace);
	if(i.value == "0")
	{
		i.value =1;
	}
	if(i.value == "NaN")
	{
		i.value = "1";
	}
}

