function cartAddElement(cart_arr, count, price, type)
{
	// new count, price per item, product type (affects postage), original count.
	cart_arr[cart_arr.length] = [count, price, type, count];
}

function cartSetCount(cart_arr, idx, count, count_id, price_id)
{
	var count_field = elementGet(count_id);

	/// Make sure we can find the count and price fields.
	if (count_field == null)
	{
		alert(count_id + ' NOT FOUND.');
		return;
	}

	// Update the cart count value.
	val = new Number(count);
	cart_arr[idx][0] = val;

	// Update count & price fields.
	if (count != '')
		elementSetValue(count_field, val.toString());

	val *= cart_arr[idx][1];
	setDivValue(price_id, '$' + val.toFixed(2));
}

function cartSubTotal(cart_arr)
{
	var i;
	var amount = 0.00;

	for (i = 0; (i < cart_arr.length); i++)
		amount += (cart_arr[i][0] * cart_arr[i][1]);
	return amount;
}

function cartPostage(cart_arr, local)
{
	var i, pattern_count = 0, kit_count = 0;
	var postage = 0.00, fab_count = 0.00;

	// cart_arr[0][0] = count
	// cart_arr[0][1] = price
	// cart_arr[0][2] = type

	for (i = 0; (i < cart_arr.length); i++)
	{
		var str = cart_arr[i][2];

		if (str == 'Pattern')
		{
			pattern_count +=cart_arr[i][0];
		}
		// else if (str == 'Kit')
		// {
			// kit_count +=cart_arr[i][0];
		// }
		// else if (str.indexOf('Readymade') != -1)
		// {
			// price included for local only 
			// if (!local)
				// return -1;	
			// continue;
		// }
		// else if (str == 'Bronwyn Hayes Kit')
		// {
			// price included for local only
			// if (!local)
				// return -1;
			// continue;
		// }
		// else if ((str.indexOf('cm') != -1)
		// || (str.indexOf('Block of the Month') != -1)
		// || (str == 'Fat Eigth')
		// || (str == 'Fat Quarter')
		// || (str == 'per Meter')
		// || (str == 'Bundle'))
		// {
			// fab_count +=(cart_arr[i][0] * cart_arr[i][1]);
		// }
		else
		{
			return -1;
		}
	}

	if (local)
	{
		postage += (kit_count * 6.00);
		if ((fab_count > 0.00) && (fab_count < 10.00))
			fab_count = 10.00;
		postage += (fab_count * 0.1);
	}
	else
	{
		postage += (kit_count * 14.00);
		while (fab_count > 0.00)
		{
			postage += 9.00;
			fab_count -= 25.00;
		}
	}
	while(pattern_count > 5)
	{
		postage += (local) ? 3.00 : 6.00;
		pattern_count -= 5;
	}
	if(pattern_count > 2)
		postage += (local) ? 3.00 : 6.00;
	else if(pattern_count > 0)
		postage += (local) ? 1.00 : 4.00;

	return postage;
}

// Clears the cart value on cb_checked == true. resets to original val if not. Updates count_id and price_id fields.
function cartClearEntry(cart_arr, idx, is_deleted, count_id, price_id)
{
	val = is_deleted ? 0 : cart_arr[idx][3];
	cartSetCount(cart_arr, idx, val.toString(), count_id, price_id);
}

function cartSetTotal(cart_arr, totalName, postageName)
{
	var total = cartSubTotal(cart_arr);
	var postage = 0;

	if (postageName)
	{
		var ps = elementGet(postageName + 'Destination');
		var islocal = 1;

		if (ps == null)
		{
			alert(postageName + 'Destination field not found.');
			return;
		}
		if (ps.value != 'Within Australia')
			islocal = 0;

		if ((postage = cartPostage(cart_arr, islocal)) == -1)
		{
			setDivValue(postageName, '<font color =\"red\">TBA</font>');
		}
		else
		{
			setDivValue(postageName, '$' + postage.toFixed(2));
			total += postage;
		}
	}

	if (postage == -1)
		setDivValue(totalName, '$' + total.toFixed(2) + '+postage');
	else
		setDivValue(totalName, '$' + total.toFixed(2));
}
