var sgChart = {
  debug: false,

  addItem: function(url, itemObj) {
    sgChart._remoteCall(url, 'PUT', itemObj);
  },

  updateQty: function(url, idItem, qty) {
    sgChart._remoteCall(url, '', {'idItem': idItem, 'qty': qty});
  },

  delItem: function(url, idItem) {
    sgChart._remoteCall(url, 'DELETE', {'idItem': idItem});
  },

  empty: function(url) {
    sgChart._remoteCall(url, 'DELETE', {'action': 'empty'});
  },

  _remoteCall: function(url, method, data) {
    frmRef = sgChart._getForm();
    frmRef.action = url;
    frmRef.meth.value = method;
    frmRef.data.value = JSON.stringify(data);
    if (! sgChart.debug) {
      frmRef.submit();
    }
  },

  _getForm: function() {
    if (document.getElementById('sgChartForm')) {
      return document.getElementById('sgChartForm');
    } else {
      // build the form and return it
      var frmStr = '<form name="sgChartForm" id="sgChartForm" action="" method="POST">';
      frmStr += '<input type="hidden" name="meth" value="" />';
      frmStr += '<input type="hidden" name="data" value="" />';
      frmStr += '</form>';
      jQuery("body").append(frmStr);
      return document.getElementById('sgChartForm');
    }
  }
};

function addToChart(iditem) {
  var id = iditem;
  var code = jQuery('#code_'+iditem).val();
  var descr = jQuery('#descr_'+iditem).val();
  var qty = jQuery('#qty_'+iditem).val();
  var price = jQuery('#price_'+iditem).val();
  var color = jQuery('#color_'+iditem).val();
  var color2 = jQuery('#color2_'+iditem).val();
  var size = jQuery('#size_'+iditem).val();
  var f1 = jQuery('#f1_'+iditem).val();
  var f2 = jQuery('#f2_'+iditem).val();
  var r1 = jQuery('#r1_'+iditem).val();
  var r2 = jQuery('#r2_'+iditem).val();
  if (f1) {
    descr += ' Fronte R1: '+f1;
  }
  if (f2) {
    descr += ' Fronte R2: '+f2;
  }
  if (r1) {
    descr += ' Retro R1: '+r1;
  }
  if (r2) {
    descr += ' Retro R2: '+r2;
  }
  if (color) {
    descr += ' - '+color;
  }
  if (color2) {
    descr += ' - '+color2;
  }
  if (size) {
    descr += ' - '+size;
  }
  sgChart.addItem('chart.html?lang=it', {'idItem': id,
                               'code':   code,
                               'descr':  descr,
                               'qty':    qty,
			       'price':  price});
  return false;
}
