<!--
/**
* Shopping cart functionality
*
* @author		Scott Carpenter <scott@tireguru.net>
* @copyright	Copyright © 2006 ABMS
* @version		1.0
* @package		Tire Link v2
*/

/**
* Update the shopping cart
* @param	string	act
*/
var last_act = '';
var is_empty = false;
function setItem(id,act)
{
	$('cartForm').itemID.value=id;
	$('cartForm').act.value  = act;
	if(act != 'remove')
	{
		$('cartForm').qty.value  = $('product'+id).qty.value;
		$('cartForm').cost.value = $('product'+id).cost.value;
	}
}
function updateCart(act)
{
	
	last_act = act;

	if (!act || act == 'get')
	{
		var params = 'act=get';
	}
	else if (act == 'checkout')
	{
		var params = 'act=get&checkout=' + false;
	}
	else if (act == 'update')
	{
		$('act').value = act;
		var params = Form.serialize('updateForm');
	}
	else
	{
		$('act').value = act;
		var params = Form.serialize('cartForm');
	}

	obj=window.parent.$('shopping_cart');
	var url = './cartUpdate.php';
	var ajax = new Ajax.Updater(
		obj,
		url,
		{
			method:'post',
			parameters:params,
			evalScripts:true,
			onComplete:function()
			{
				cartUpdated(obj);
			}
		}
	);
	
	return false;
}


function cartUpdated(obj)
{
	new Effect.Highlight(obj);
}

/**
* Make sure they want to remove items and then do so
*/
function removeItems(item)
{
	setItem(item,'remove');
	if (confirm('Are you sure you would like to remove the selected items from your shopping cart?'))
	{
		updateCart('delete');
	}
}

/**
* Check that they can update the cart and update it if applicable
*/
function checkUpdate()
{
	var has_zero = false;
	var objs = Form.getInputs('cartForm','text');
	for (var i=0; i<objs.length; i++)
	{
		val = parseInt($F(objs[i]));
		if (!isNaN(val) && val <= 0)
		{
			has_zero = true;
			break;
		}
	}
	
	if (!has_zero || confirm('One or more of your shopping cart items has a quantity that is less than one.\nThese items will be removed from your cart.\nAre you sure this is what you would like to do?'))
	{
		Form.checkAll('cartForm',false);
		updateCart('update');
	}
}

// -->
