  // cart operations 
  var cartReq = false;

  function getJQty(qpref, iid) {
    var jqty = jQuery('#' + qpref + iid);
    if (!jqty.length || !jqty.get(0)) {
      return false;
    }
    return jqty.get(0);
  }

  function getQty(qpref, iid) {
    var jqty = getJQty(qpref, iid);
    if (jqty === false) {
      return false;
    }
    if (jqty.value.toString() == "") {
      alert("Please enter the desired quantity");
      jqty.focus();
      return false;
    }
    return jqty.value.toString();
  }

  function cartCall(call, qpref, items, ajaxid, pagename, noconfirm)
  {
    var params = {
      'call' : call
    };
    if (call == 'CUQ') {
      for (var i = 1; i<=items.length; i++) {
        params['iid' + i] = items[i-1];
        params['qty' + i] = getQty(qpref, items[i-1]);
        if (params['qty' + i] === false) {
          return;
        }
        if (!noconfirm && parseInt(params['qty' + i] - 0) <= 0) {
          if (!confirm("Are you sure you want to remove this product from the cart?")) {
            return;
          }
        }
      }
    } else {
      if (items.length) {
        params['iid'] = items[0];
        if (qpref) {
          params['qty'] = getQty(qpref, items[0]);
          if (params['qty'] === false) {
            return;
          }
          if (parseInt(params['qty'] - 0) <= 0) {
            if (!noconfirm && !confirm("Are you sure you want to remove this product from the cart?")) {
              return;
            }
          }
        }
      }
    }
    if (cartReq) {
      cartReq.abort();
    }
    jQuery(".cart_item_box_but").hide();
    jQuery("#cartwidgetloader, #checkoutloader").show();
    cartReq = ajaxLoad(ajaxid, '/' + pagename + '.html', params)();
  }

  function updateCartItem(ajaxid, qpref, iid, qty, pagename)
  {
    if (parseInt(qty - 0) <= 0) {
      if (!confirm("Are you sure you want to remove this product from the cart?")) {
        return;
      }
    }
    var jqty = getJQty(qpref, iid);
    if (jqty === false) {
      return;
    }
    jqty.value = Math.max(0, parseInt(qty - 0));
    cartCall("CUQ", qpref, [iid], ajaxid, pagename, true);
  }

  function adjustCartItem(ajaxid, qpref, iid, dqty, pagename)
  {
    var jqty = getJQty(qpref, iid);
    if (jqty === false) {
      return;
    }
    updateCartItem(ajaxid, qpref, iid, parseInt(jqty.value - 0) + parseInt(dqty - 0), pagename);
  }

  function updateCart(ajaxid, qpref, pagename)
  {
    var items = [];
    jQuery("input[iid][id^=" + qpref + "]").each(function(items) {
      return function() {
        items.push(this.getAttribute('iid'));
      }
    }(items));
    cartCall("CUQ", qpref, items, ajaxid, pagename);
  }


  function updateCartItemQty(ajaxid, qpref, iid, pagename)
  {
    cartCall("CUQ", qpref, [iid], ajaxid, pagename);
  }


  function removeCartItem(ajaxid, qpref, iid, pagename)
  {
    if (!confirm("Are you sure you want to remove this product from the cart?")) {
      return;
    }
    cartCall("CRI", qpref, [iid], ajaxid, pagename);
  }

  function emptyCart(ajaxid, pagename)
  {
    if (!confirm("Are you sure you want to remove all products from the cart?")) {
      return;
    }
    cartCall("ECI", '', [], ajaxid, pagename);
  }

  function getFreeSample(ajaxid, iid, pagename)
  {
    cartCall("GFS", '', [iid], ajaxid, pagename);
  }

