function Item(type, sku) {

  this.type = type;
  this.sku = sku;
  this.group = '';
  this.family = '';
  this.subcategory = '';
  this.name = '';
  this.manufacturer = '';
  this.price = 0;
  this.quantity = 0;

  this.getType = function() {
    return this.type;
  }

  this.getSku = function() {
    return this.sku;
  }

  this.getGroup = function() {
    return this.group;
  }

  this.setGroup = function(group) {
    this.group = group;
  }

  this.getFamily = function() {
    return this.family;
  }

  this.setFamily = function(family) {
    this.family = family;
  }

  this.getSubcategory = function() {
    return this.subcategory;
  }

  this.setSubcategory = function(subcategory) {
    this.subcategory = subcategory;
  }

  this.getName = function() {
    return this.name;
  }

  this.setName = function(name) {
    this.name = name;
  }

  this.getManufacturer = function() {
    return this.manufacturer;
  }

  this.setManufacturer = function(manufacturer) {
    this.manufacturer = manufacturer;
  }

  this.getPrice = function() {
    return this.price;
  }

  this.setPrice = function(price) {
    this.price = price;
  }

  this.getQuantity = function() {
    return this.quantity;
  }

  this.setQuantity = function(quantity) {
    this.quantity = quantity;
  }

  this.inc = function() {
    this.quantity++;
  }

  this.dec = function() {
    this.quantity--;
  }

}

function ShoppingCart(shoppingCartStoreId, shoppingCartType) {

  var days = 6 * 7;
  var eventAdd = 'a';
  var eventPurchase = 'p';
  var eventRemove = 'r';
  var eventView = 'v';

  this.storeId = shoppingCartStoreId;
  this.type = shoppingCartType;
  this.itemList = new Array();
  this.webtrends = true;

  this.init = function() {
    var shoppingCartCookie = readCookie('shoppingCart' + '_' + this.storeId + '_' + this.type);

    if (shoppingCartCookie != null && shoppingCartCookie != '') {
      var items = shoppingCartCookie.split('|');

      for (var i = 0; i < items.length; i++) {
        var itemData = items[i].split('@');

        if (itemData.length == 9) {
          var item = new Item(itemData[0], itemData[1]);
          item.setGroup(itemData[2]);
          item.setFamily(itemData[3]);
          item.setSubcategory(itemData[4]);
          item.setName(itemData[5]);
          item.setManufacturer(itemData[6]);
          item.setPrice(itemData[7]);
          item.setQuantity(itemData[8]);
          this.itemList.push(item);
        }
      }
    }
  }

  this.purchase = function() {
    this.sendWebtrends(eventPurchase);
    this.itemList = new Array();
    this.setCookie();
  }

  this.getSize = function() {
    return this.itemList.length;
  }

  this.setCookie = function() {
    var shoppingCartCookie = '';

    for (var i = 0; i < this.itemList.length; i++) {
      var item = this.itemList[i];
      shoppingCartCookie += item.getType() + '@';
      shoppingCartCookie += item.getSku() + '@';
      shoppingCartCookie += item.getGroup() + '@';
      shoppingCartCookie += item.getFamily() + '@';
      shoppingCartCookie += item.getSubcategory() + '@';
      shoppingCartCookie += item.getName() + '@';
      shoppingCartCookie += item.getManufacturer() + '@';
      shoppingCartCookie += item.getPrice() + '@';
      shoppingCartCookie += item.getQuantity() + '@';
      shoppingCartCookie = shoppingCartCookie.substring(0, shoppingCartCookie.length - 1);
      shoppingCartCookie += '|';
    }
    shoppingCartCookie = shoppingCartCookie.substring(0, shoppingCartCookie.length - 1);
    shoppingCartCookie = shoppingCartCookie.replace(/,/g, '');
    createCookie('shoppingCart' + '_' + this.storeId + '_' + this.type, shoppingCartCookie, days);
  }

  this.getItemIndex = function(sku) {
    var result = -1;

    for (var i = 0; i < this.itemList.length; i++) {

      if (this.itemList[i].getSku() == sku) {
        result = i;
      }
    }
    return result;
  }

  this.addItem = function(type, sku, group, family, subcategory, name, manufacturer, price) {
    var itemIndex = this.getItemIndex(sku);

    if (itemIndex == -1) {
      this.itemList.push(new Item(type, sku));
      itemIndex = this.itemList.length - 1;
    }
    var item = this.itemList[itemIndex];
    item.setGroup(group.toLowerCase());
    item.setFamily(family.toLowerCase());
    item.setSubcategory(subcategory.toLowerCase());
    item.setName(name.toLowerCase());
    item.setManufacturer(manufacturer.toLowerCase());
    item.setPrice(price);
    item.inc();
    this.setCookie();
    this.sendWebtrends(eventAdd);
  }

  this.removeItem = function(sku) {
    var itemIndex = this.getItemIndex(sku);

    if (itemIndex > -1) {
      var item = this.itemList[itemIndex];
      var newList = new Array();

      for (var i = 0; i < this.itemList.length; i++) {

        if (i != itemIndex) {
          newList.push(this.itemList[i]);
        }
      }
      this.itemList = newList;
      this.setCookie();
      this.sendWebtrends(eventRemove);
    }
  }

  this.getItemQuantity = function(sku) {
    var result = 0;
    var itemIndex = this.getItemIndex(sku);

    if (itemIndex > -1) {
      var item = this.itemList[itemIndex];
      result = item.getQuantity();
    }
    return result;
  }

  this.setItemQuantity = function(sku, quantity) {
    var itemIndex = this.getItemIndex(sku);

    if (itemIndex > -1) {
      var item = this.itemList[itemIndex];

      if (item.getQuantity() != quantity) {
        var event = quantity > item.getQuantity() ? eventAdd : eventRemove;
        item.setQuantity(quantity);
        this.setCookie();
        this.sendWebtrends(event);
      }
    }
  }

  this.getItemPrice = function(sku) {
    var result = 0;
    var itemIndex = this.getItemIndex(sku);

    if (itemIndex > -1) {
      var item = this.itemList[itemIndex];
      result = item.getPrice() * item.getQuantity();
    }
    return result;
  }

  this.getSum = function() {
    var result = 0;

    for (var i = 0; i < this.itemList.length; i++) {
      var item = this.itemList[i];
      result += item.getPrice() * item.getQuantity();
    }
    return result;
  }

  this.getItemListShort = function() {
    var result = '';

    for (var i = 0; i < this.itemList.length; i++) {
      var item = this.itemList[i];
      result += item.getSku() + '@';
      result += item.getQuantity() + '@';
      result = result.substring(0, result.length - 1);
      result += '|';
    }
    result = result.substring(0, result.length - 1);
    return result;
  }

  this.getWebtrends = function() {
    return this.webtrends;
  }

  this.setWebtrends = function(webtrends) {
    this.webtrends = webtrends;
  }

  this.sendWebtrends = function(event) {
    var pn_gr = '';
    var pn_fa = '';
    var pn_sc = '';
    var pn_sku = '';
    var pn_ma = '';
    var pn = '';
    var tx_u = '';
    var tx_s = '';
    var tx_e = event;

    if (this.webtrends) {

      for (var i = 0; i < this.itemList.length; i++) {
        var item = this.itemList[i];
        pn_gr += item.getGroup() + ';';
        pn_fa += item.getFamily() + ';';
        pn_sc += item.getSubcategory() + ';';
        pn_sku += item.getSku() + ';';
        pn_ma += item.getManufacturer() + ';';
        pn += getValidProductName(item.getName())+ ';';
        tx_u += item.getQuantity() + ';';
        tx_s += item.getQuantity() * item.getPrice() + ';';
      }

      if (this.itemList.length > 0) {
        pn_gr = pn_gr.substring(0, pn_gr.length - 1);
        pn_fa = pn_fa.substring(0, pn_fa.length - 1)
        pn_sc = pn_sc.substring(0, pn_sc.length - 1)
        pn_sku = pn_sku.substring(0, pn_sku.length - 1)
        pn_ma = pn_ma.substring(0, pn_ma.length - 1)
        pn = pn.substring(0, pn.length - 1)
        tx_u = tx_u.substring(0, tx_u.length - 1)
        tx_s = tx_s.substring(0, tx_s.length - 1)
      }
      dcsMultiTrack('WT.pn_gr', pn_gr, 'WT.pn_fa', pn_fa, 'WT.pn_sc', pn_sc, 'WT.pn_sku', pn_sku, 'WT.pn', pn, 'WT.pn_ma', pn_ma, 'WT.tx_u', tx_u, 'WT.tx_s', tx_s, 'WT.tx_e', tx_e);
    }
  }

  this.init();
}

var cart = null;

function getShoppingCart(storeId, type) {

  if (cart == null) {
    cart = new ShoppingCart(storeId, type);
  }
  return cart;
}

function getValidProductName(pn) {

  if (pn != '') {  
    pn = pn.replace(/&/g, "+");
    pn = pn.replace(/=/g, ":");
    if(pn.length>64) {
      pn = pn.substr(0, 64);
    }
  }
  return pn;
  
}
