function updateCopiesInOrder(orderId, fieldId, updatePriceFields)
{
	startUnstoppableProcess();
	
	numCopies = $("#"+fieldId).val();
	
	if(numCopies < 1 || !isNumeric(numCopies)) {
		numCopies = 1;
	}
	
	$.get("/async_js/set_copies/&order_id="+orderId+"&copies="+numCopies, function(data) { updateCopiesInOrderCallback(orderId, numCopies, data); });
}

function updateCopiesInOrderCallback(orderId, numCopies, data)
{
	finishUnstoppableProcess();
	
	if(data != "0" && isNumeric(data))
	{
		orderPrice	= number_format(data, 2, ',', ' ');
		perPrice	= number_format(data/numCopies, 2, ',', ' ');
		
		$("#order_price_"+orderId).html(orderPrice);
		$("#order_per_price_"+orderId).html(perPrice);
	}
	else
	{
		$("#order_price_"+orderId).html("-");
		$("#order_per_price_"+orderId).html("-");
	}
	
	updateCartSummary();
}

/* Made by Mathias Bynens <http://mathiasbynens.be/> */
function number_format(a, b, c, d) {
 a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
 e = a + '';
 f = e.split('.');
 if (!f[0]) {
  f[0] = '0';
 }
 if (!f[1]) {
  f[1] = '';
 }
 if (f[1].length < b) {
  g = f[1];
  for (i=f[1].length + 1; i <= b; i++) {
   g += '0';
  }
  f[1] = g;
 }
 if(d != '' && f[0].length > 3) {
  h = f[0];
  f[0] = '';
  for(j = 3; j < h.length; j+=3) {
   i = h.slice(h.length - j, h.length - j + 3);
   f[0] = d + i +  f[0] + '';
  }
  j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
  f[0] = j + f[0];
 }
 c = (b <= 0) ? '' : c;
 return f[0] + c + f[1];
}

function updateCartSummary()
{
	if ( $("#cartSumContainer").length > 0 ) {
		$.get("/async_js/get_cart_sum", function(data) { updateCartSummaryCallback(data); });
	}
}

function updateCartSummaryCallback(data)
{
	$("#cartSumContainer").html(data);
}

function isNumeric(value) {
	  if (value == null || !value.toString().match(/^[-]?\d*\.?\d*$/)) return false;
	  return true;
}

var numUnstoppableProcesses=0;

function startUnstoppableProcess()
{
	window.onbeforeunload=confirmExit
	numUnstoppableProcesses++;
}

function finishUnstoppableProcess()
{
	if(numUnstoppableProcesses != 0) {
		numUnstoppableProcesses--;
	}
	
	if(numUnstoppableProcesses == 0) {
		window.onbeforeunload="";
	}
}

function confirmExit()
{
	return "Din beställning håller på att sparas till vår server. Är du säker på att du vill avbryta?";
}

function updateCustomDataInOrder(orderId, productId, customType, customData)
{
	if(orderId != "") // It's valid to leave orderId empty if there is no order in the cart yet
	{
		startUnstoppableProcess();
		$.get("/async_js/set_custom_data/&order_id="+orderId+"&type="+customType+"&data="+customData, function(data) { updateCustomDataInOrderCallback(); });
	}
	else if(productId != "")
	{
		startUnstoppableProcess();
		$.get("/async_js/set_custom_data/&product_id="+productId+"&type="+customType+"&data="+customData, function(data) { updateCustomDataInOrderCallback(); });
	}
	
	//alert($("#"+customType).val());
}

function updateCustomDataInOrderCallback()
{
	finishUnstoppableProcess();
}

function updateCustomDataInOrderForRadios(orderId, productId, customType)
{
	customData = $("input[name='"+customType+"']:checked").val();
	updateCustomDataInOrder(orderId, productId, customType, customData);
	//alert(customData);
}

function csEnableAndDisableShippingWithPayment(payment)
{
	$('input[name=shipping]').attr("disabled", false);
	$('input[name=shipping]').attr("checked", false);
	
	if(payment == "cash") // Only allow cash for shipping "hämtas"
	{
		$('input[value!=hamtas][name=shipping]').attr("disabled", true);
		//$('input[value=hamtas][name=shipping]').attr("checked", true);
	}
	else if(payment == "cash_on_delivery")
	{
		$('input[value=hamtas][name=shipping]').attr("disabled", true);
		//$('input[value=postpaket][name=shipping]').attr("checked", true);
	}
}