// Add methods on load
Event.observe(window, 'load', function() {
  $$('#items_in_cart .amt_qty').each(function(field, i) {
	  field.observe('change', updateOrderLine);
  });
  $$('#items_in_cart .remove_from_order').each(function(field, i) {
	  field.observe('click', removeFromOrder);
  });
  $$('#also_available_items .amt_qty').each(function(field, i) {
	  field.observe('change', createOrderLine);
  });
})

// re-calculate order total items
function calcOrderTotal() {
  shop_item_count   = 0;
  shop_items_total  = 0.00;
  $$('#items_in_cart .amt_qty').each(function(field, i) {
	  if (parseInt(field.value) > 0) {
      ol_id             = field.id.gsub('amt_qty_', '');
      shop_item_count   += parseInt(field.value);
      shop_items_total  += parseFloat(field.value) * parseFloat($("amt_price_"+ol_id).value);
    }
  });
  shop_item_term        = shop_item_count == 1 ? "ITEM" : "ITEMS"
  
  $('basket_count_input').value = shop_item_count+' '+shop_item_term;
  $('items_total').update(shop_items_total.toFixed(2));
  shop_shipping_total           = parseFloat($('shipping_total').innerHTML);
  $('your_total').update((shop_items_total + shop_shipping_total).toFixed(2));
  
  // update tax
}

function updateOrderLine(e) {
  ol_id       = this.id.gsub('amt_qty_', '');
  amt_qty     = 0;
  if (parseInt(this.value) > 0) {
    amt_qty     = parseInt(this.value);
  } else {
    // if the value entered is 0, or not a number, re-populate the field with 0
    this.value  = 0;
  }
  update_url  = '/shop/update_order_line/'+ol_id+'?amt_qty='+amt_qty;
  // send update order_line
  new Ajax.Request(update_url, {
    onSuccess: function(t) {
      response  = t.responseText;
      
      // re-calculate order total items
      calcOrderTotal();
    }
  });
}

function createOrderLine(e) {
  p_id    = this.id.gsub('amt_qty_', '');
  amt_qty = 0;
  if (parseInt(this.value) > 0) {
    amt_qty     = parseInt(this.value);
  } else {
    // if the value entered is 0, or not a number, re-populate the field with 0
    this.value  = 0;
  }
  if (amt_qty > 0) {
    // only add order_line if amt_qty > 0
    create_url  = '/shop/create_order_line/'+p_id+'?amt_qty='+amt_qty;
    // send create order_line
    new Ajax.Updater('items_in_cart', create_url, {
      insertion: 'bottom',
      onComplete: function(t) {
        // remove <p> for also available item
        $('available_line_'+p_id).remove();
        // re-calculate order total items
        calcOrderTotal();
        
        // add functions again
        $$('#items_in_cart .amt_qty').each(function(field, i) {
      	  field.observe('change', updateOrderLine);
        });
        $$('#items_in_cart .remove_from_order').each(function(field, i) {
      	  field.observe('click', removeFromOrder);
        });
      }
    });
  }

}

function removeFromOrder(e) {
  ol_id       = this.id.gsub('remove_from_order_', '');
  this.src    = '/images/css_images/remove_this_item_down.png'
  delete_url  = '/shop/remove_from_order/'+ol_id;
  // send update order_line
  new Ajax.Request(delete_url, {
    onSuccess: function(t) {
      response  = t.responseText;
      // remove <p> for also order_line
      $('order_line_'+ol_id).remove();
      // re-calculate order total items
      calcOrderTotal();
    }
  });
}