var order_cost = 0;
var order_items = 0;

function AddOrderItem(obj, url, code, cost, condition, listing_id, order_id) {
  r_url = url + 'order/code:' + code + '/cost:' + cost + '/condition:' + condition + '/listing_id:' + listing_id + '/order_id:' + order_id;

  new Ajax.Request(r_url, {
    method: 'get',
    onSuccess: function(transport) {
      s = transport.responseText;
      
      if (s == 'success') {
        obj.disabled = true;
        
        order_cost += parseFloat(cost);
        order_items += 1;

        $('order-summary').update(order_items + ' items | $' + formatNumber(order_cost, 2));
        
        $('item-added-gray').show();
        $('item-added').show();
      }
      
      if (s == 'exists') {
        alert('Error: ' + code + ' already exists in your order.');
      }
    }
  });
}

function updateOrderTotal(total, postal) {
  return formatNumber(parseFloat(total)+parseFloat(postal), 2);
}

function formatNumber(number, precision) 
{ 
  var decimal = 1;
  
  for(i=1; i <= precision; i++) {
    decimal = decimal * 10;
  }
  
  return (Math.round(number * decimal) / decimal).toFixed(precision) 
}