function addItemtoCart(product_id, quantity)
{
	$url = '/shoppingcart/additem/';
	
	if (product_id && quantity)
	{
		$url += product_id + '/' + quantity;
	}
	else
	{
		$url += $('product_id').value + '/' + $('quantity').value;
	}
		
	new Ajax.Updater ('message', $url);
}

function updateShoppingCart()
{
	new Ajax.Updater ('shopping_cart', '/shoppingcart/update',
					  	{
							method: 'post',
							parameters: Form.serialize('shopping_cart_form'),
							evalScripts: true
						}
					);
}

var infoFields = {
			0: {
				"billing": "orderbillingname",
				"shipping": "ordershippingname",
				"required": 0
			},
			1: {
				"billing": "orderbillingaddress1",
				"shipping": "ordershippingaddress1",
				"required": 1
			},
			2: {
				"billing": "orderbillingaddress2",
				"shipping": "ordershippingaddress2",
				"required": 0
			},
			3: {
				"billing": "orderbillingcity",
				"shipping": "ordershippingcity",
				"required": 1
			},
			4: {
				"billing": "orderbillingstateid",
				"shipping": "ordershippingstateid",
				"required": 1
			},
			5: {
				"billing": "orderbillingzipcode",
				"shipping": "ordershippingzipcode",
				"required": 1
			}
		}
			
function sameasbilling(dontchangestatus)
{
	for (var field in infoFields)
	{
		var shippingfield = $(infoFields[field]['shipping']);
		var billingfield = $(infoFields[field]['billing']);

		if ($('same_as_billing').value == 0 || dontchangestatus == true)
		{
			if (billingfield.type && billingfield.type == "text")
			{
				$(infoFields[field]['shipping']).value = $(infoFields[field]['billing']).value;
				$(infoFields[field]['billing']).onchange = function () {isSameAsShipping(this);}
			}
			else if (billingfield.type && billingfield.type == "select-one")
			{
				if (billingfield.selectedIndex > 0)
				{
					shippingfield.selectedIndex = billingfield.selectedIndex;
					$(infoFields[field]['billing']).onchange = 'isSameAsShipping(this);';
				}
			}
			if (dontchangestatus == false)
				deactivate(shippingfield);
		}
		else {
			if (dontchangestatus == false)
				activate(shippingfield);
		}
	}
	
	if (dontchangestatus == false)
	{
		if ($('same_as_billing').value == 0)
			$('same_as_billing').value = 1;
		else if ($('same_as_billing').value == 1)
			$('same_as_billing').value = 0;
	}

	updateShipping();
}
function isSameAsShipping(field)
{
	if ($('same_as_billing').value==1)
		sameasbilling(true);
}
function disableAllFields()
{
	for (var field in infoFields)
	{
		deactivate($(infoFields[field]['shipping']));
		deactivate($(infoFields[field]['billing']));
	}
}

function enableAllFields()
{
	for (var field in infoFields)
	{
		if ($('same_as_billing').value==0)
			activate($(infoFields[field]['shipping']));
		activate($(infoFields[field]['billing']));
	}
}

function deactivate(field)
{
	field.style.background = '#EEE';
	field.readOnly = true;
}
function activate(field)
{
	field.style.background = '#FFF';
	field.readOnly = false;
}
function updateShipping()
{
	var data = '';
	for (var field in infoFields)
	{	
		if ($(infoFields[field]['shipping']))
		{
			if ($(infoFields[field]['shipping']).value != '')
			{
				data += '&' + $(infoFields[field]['shipping']).name.substring($(infoFields[field]['shipping']).name.lastIndexOf('[')+1,$(infoFields[field]['shipping']).name.length-1) + '=' + $(infoFields[field]['shipping']).value;
			}
			else
			{
				if ($(infoFields[field]['required']==1))
				{
					Element.update('shipping_method','Please fill in required shipping information to receive a shipping quote.');
					//$('checkout_button').disabled = true;
					//document.getElementByID('checkout_button').disabled = true;
					return;
				}
			}
		}
		else return;	
	}
	
	Element.update('shipping_method','');
	
	//updateCosts();
	new Ajax.Updater('shipping_method','/shoppingcart/get_shipping/',
					 {
						 onLoading: function(request)
						 {
							 Element.show('throbber');
							 disableAllFields();
						 },
						 onLoaded: function(request)
						 {
							 Element.hide('throbber');
							 enableAllFields();
						},
						method: 'post',
						evalScripts:true,
						parameters:data,
						requestHeaders:['X-Update', 'shipping_selection']
					});
	//$('checkout_button').disabled = false;
	//document.getElementByID('checkout_button').disabled = false;
}
function updateCosts(theSelect)
{
	if (theSelect && typeof(theSelect) == 'object')
	{
		var service = theSelect.value;
		if (service != '')
		{
			//$('shipping_cost').value = money_format($('shipping_'+service).value);
			Element.update('shipping_cost', money_format($('shipping_' + service).value));
			$('shipping_cost_value').value = $('shipping_'+service).value;
			calculateTotal();
		}
	}
	else {
		
		if (theSelect && typeof(theSelect) == 'number' || typeof(theSelect) == 'string')
		{
			Element.update('shipping_cost', '$' + theSelect);
			$('shipping_cost_value').value = theSelect;
		}
		else {
			Element.update('shipping_cost','$0.00');
			$('shipping_cost_value').value = '0.00';
		}
		
		calculateTotal();
	}
}

var total = 0;
function calculateTotal(reset)
{
	var shippingCost = $('shipping_cost_value').value;

	shippingCost = shippingCost * 1;

	if (shippingCost >= 0)
		total = total+shippingCost;
	
	$('total_cost').innerHTML = money_format(total);
	//Element.update('total_cost',(total + shippingCost));
}

function money_format(num) {
	num = num.toString().replace("/\$|\,/g",'');
	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}
/*
var onload = function() {
	if (document.getElementById('shipping_method'))
	{
		updateCosts(document.getElementById('shipping_method'));
	}
	else {
		if (document.getElementById('shipping_cost_value').value != '0.00')
		{
			updateCosts(document.getElementById('shipping_cost_value').value);
		}
		else {
			updateCosts();
		}
	} 
}
*/