var SHOPPINGCART_COOKIENAME = "shoppingcart"
var SHOPPINGCART_SEP_ATTR = "]|[";
var SHOPPINGCART_SUPPORTED_ATTRIBUTES = ["count"];
var SHOPPINGCART_DEFAULT_ATTRIBUTEVALUES = [0];

function ShoppingCart()
{
  // alias member functions
  this.load = ShoppingCart_load;
  this.loadShoppingCart = ShoppingCart_loadShoppingCart;
  this.save = ShoppingCart_save;
  this.saveShoppingCart = ShoppingCart_saveShoppingCart;

  // set default values for all supported attributes
  for (var i=0; i<SHOPPINGCART_SUPPORTED_ATTRIBUTES.length; i++)
  {
    var attrName = SHOPPINGCART_SUPPORTED_ATTRIBUTES[i];
    var attrValue = SHOPPINGCART_DEFAULT_ATTRIBUTEVALUES[i];
    this[attrName] = attrValue;
  }

  // call loadShoppingCart member function
  this.load();
}

/**
 *  Loads the shoppingcart info. Delegates to loadShoppingCart
 */
function ShoppingCart_load()
{
  this.loadShoppingCart(SHOPPINGCART_COOKIENAME);
}

/**
 *  Loads the shoppingcart info from a cookie
 *
 *  @param  cookieName  The name of the cookie to load from.
 */
function ShoppingCart_loadShoppingCart(cookieName)
{
  var cookieValue = null;

  // Code 'borrowed' from Quirksmode.org, thanks Peter-Paul Koch!
  // http://www.quirksmode.org/js/cookies.html
  var nameEQ = cookieName + "=";
  var cookies = document.cookie.split(';');
  for(var i=0; i<cookies.length; i++) 
  {
    var c = cookies[i];

    while (c.charAt(0) == ' ')
      c = c.substring(1, c.length);

    if (c.indexOf(nameEQ) == 0) 
    {
      cookieValue = unescape(c.substring(nameEQ.length, c.length));
      break;
    }
  }

  if (cookieValue != null)
  {
    var attrStrings = cookieValue.split(SHOPPINGCART_SEP_ATTR);
    for (var i=0; i<attrStrings.length; i++)
    {
      var attrString = attrStrings[i];
      var eqIdx = attrString.indexOf("=");
      if (eqIdx >= 0)
      {
        var attrName = attrString.substring(0, eqIdx);
        var attrValue = attrString.substring(eqIdx + 1);
        if (attrName != "")
        {
          var defaultAttrValue = null;
          for (var attrIdx=0; attrIdx<SHOPPINGCART_SUPPORTED_ATTRIBUTES.length; attrIdx++)
          {
            if (SHOPPINGCART_SUPPORTED_ATTRIBUTES[attrIdx] == attrName)
              defaultAttrValue = SHOPPINGCART_DEFAULT_ATTRIBUTEVALUES[attrIdx];
          }
          if (defaultAttrValue != null)
          {
            if (typeof defaultAttrValue == "number")
            {
              // add "" to force to string for indexOf
              defVal = defaultAttrValue + "";
              // javascript 'casting'... make sure that we get an int or float instead of a string
              if (defVal.indexOf(".") >= 0)
                attrValue = parseFloat(attrValue);
              else
                attrValue = parseInt(attrValue);
            }
          }
          this[attrName] = attrValue;
        }
      }
    }
  }
}  

/**
 *  Saves the shoppingcart info. Delegates to saveShoppingCart
 */
function ShoppingCart_save()
{
  this.saveShoppingCart(SHOPPINGCART_COOKIENAME);
}

/**
 *  Saves the shoppingcart info to a cookie.
 *
 *  @param  cookieName  The name of the cookie to save to.
 */
function ShoppingCart_saveShoppingCart(cookieName)
{
  var payload = "";
  for (var i=0; i<SHOPPINGCART_SUPPORTED_ATTRIBUTES.length; i++)
  {
    var attrName = SHOPPINGCART_SUPPORTED_ATTRIBUTES[i];
    if (payload != "")
      payload = payload + SHOPPINGCART_SEP_ATTR;
    payload = payload + attrName + "=" + this[attrName];
  }
  document.cookie = cookieName + "=" + escape(payload) + "; path=/";
}

