  /**
   * Cart
   */
  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 getJVariation(qpref, iid) {
    var jvariation = jQuery('#' + qpref.replace('qty_sample', 'qty').replace('qty', 'vid') + iid);
    if (!jvariation.length || !jvariation.get(0)) {
      return false;
    }
    return jvariation.get(0);
  }

  function getVariation(qpref, iid) {
    var jvariation = getJVariation(qpref, iid);
    if (jvariation === false) {
      return false;
    }
    return jvariation.value.toString();
  }

  function addCartPattern(ajaxid, pagename) {
    var params = {
      'call' : 'APC'
    }
    var pattern_parameters = jQuery("#pattern").serializeArray();
    for (var i=0; i<pattern_parameters.length; i++) {
      params[pattern_parameters[i].name] = pattern_parameters[i].value;
    }
    if (cartReq) {
      cartReq.abort();
    }
    jQuery(".cart_item_box_but").hide();
    showCart();
    jQuery("#cartwidgetloader, #checkoutloader").show();
    cartReq = ajaxLoad(ajaxid, '/' + pagename + '.html', params)();
  }

  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].toString().match(/(\d+)_(\d+)/) ? RegExp.$1 : items[i-1];
        params['qty' + i] = getQty(qpref, items[i-1]);
        if (params['qty' + i] === false) {
          return;
        }
        params['vid' + i] = getVariation(qpref, items[i-1]);
        if (!noconfirm && parseInt(params['qty' + i] - 0) <= 0) {
          jConfirm("Are you sure you want to remove this product from the cart?", "Remove Cart Item", function(r) {
            if (r) {
              confirmedCartCall(ajaxid, pagename, params);
            }
          });
          return;
        }
      }
    } else {
      if (items.length) {
        params['iid'] = items[0].toString().match(/(\d+)_(\d+)/) ? RegExp.$1 : items[0];
        if (qpref) {
          params['qty'] = getQty(qpref, items[0]);
          if (params['qty'] === false) {
            return;
          }
          params['vid'] = getVariation(qpref, items[0]);
          if (call != 'GFS' && parseInt(params['qty'] - 0) <= 0) {
            jConfirm("Are you sure you want to remove this product from the cart?", "Remove Cart Item", function(r) {
              if (r) {
                confirmedCartCall(ajaxid, pagename, params);
              }
            });
            return;
          }
        }
      }
    }
    confirmedCartCall(ajaxid, pagename, params);
  }

  function confirmedCartCall(ajaxid, pagename, params)
  {
    if (cartReq) {
      cartReq.abort();
    }
    jQuery(".cart_item_box_but").hide();
    showCart();
    jQuery("#cartwidgetloader, #checkoutloader").show();
    cartReq = ajaxLoad(ajaxid, '/' + pagename + '.html', params)();
  }

  function updateCartItem(ajaxid, qpref, iid, qty, pagename)
  {
    if (parseInt(qty - 0) <= 0) {
      jConfirm("Are you sure you want to remove this product from the cart?", "Remove Cart Item", function(r) {
        if (r) {
          confirmedUpdateCartItem(ajaxid, qpref, iid, qty, pagename);
        }
      });
      return;
    }
    confirmedUpdateCartItem(ajaxid, qpref, iid, qty, pagename);
  }

  function confirmedUpdateCartItem(ajaxid, qpref, iid, qty, pagename)
  {
    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, delay)
  {
    if (delay) {
      if (updateCart.t) {
        clearTimeout(updateCart.t);
      }
      updateCart.t = setTimeout("updateCart('" + ajaxid + "', '" + qpref + "', '" + pagename + "')", delay);
      return;
    }
    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, delay)
  {
    if (delay) {
      if (updateCart.t) {
        clearTimeout(updateCart.t);
      }
      updateCart.t = setTimeout("updateCartItemQty('" + ajaxid + "', '" + qpref + "', '" + iid + "', '" + pagename + "')", delay);
      return;
    }
    cartCall("CUQ", qpref, [iid], ajaxid, pagename);
  }

  function removeCartItem(ajaxid, qpref, iid, pagename)
  {
    jConfirm("Are you sure you want to remove this product from the cart?", "Remove Cart Item", function(r) {
      if (r) {
        cartCall("CRI", qpref, [iid], ajaxid, pagename, true);
      }
    });
  }

  function removeCartPattern(ajaxid, qpref, iid, pagename)
  {
    jConfirm("Are you sure you want to remove this product from the cart?", "Remove Cart Item", function(r) {
      if (r) {
        cartCall("CRP", qpref, [iid], ajaxid, pagename, true);
      }
    });
  }

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

  function getSample(ajaxid, qpref, iid, pagename)
  {
    cartCall("GFS", qpref, [iid], ajaxid, pagename, true);
  }

  function showModel() {
    jQuery("#cart_link").removeClass('trd_title_on');
    jQuery("#model_link").addClass('trd_title_on');
    jQuery('#cart').fadeOut();
    jQuery(jQuery("#triedon .fabrics_box_left3d").length ? '#model, #like' : '#model').fadeIn();
    jQuery.cookie('tlrn', 'model', {path: '/'});
  }

  function showCart() {
    jQuery("#model_link").removeClass('trd_title_on');
    jQuery("#cart_link").addClass('trd_title_on');
    jQuery('#model, #like').fadeOut();
    jQuery('#cart').fadeIn();
    jQuery.cookie('tlrn', 'cart', {path: '/'});
  }

