  /*
   * This is the item in the cart that stores the accessories.
   */
  var cartName = "access";
  
  /*
   * This is the item type for the cart entry that stories the accessories.
   */
  var cartType = "order";
  
  /*
   * This is the separator of the name and type of the item
   * that is put in the cart.
   */
  var elementSeparator = ":";
  
  /*
   * This is the separator character for items in the accessory
   * string.  Note: This must not conflict with the cart.
   */
  var itemSeparator = "|";
  
  /*
   * genElement(name, type) - This method generates and
   * element, to be stored in the accessory cookie.
   */
  function genElement(name, type)
  {
    return name + elementSeparator + type;
  }
  
  /*
   * addAccessory(name, type) - This method will add the 
   * specified item to the accessory cookie.
   */
  function addAccessory(name, type)
  {
    // Get the existing accessory cookie string.
    var access = getItemValue(cartName);
    
    // Build the element to be added.
    var element = genElement(name, type);
    
    // Separator of the elements.
    var sep = "";
    
    // If the accessory string is not empty then we need
    // to add the separator.
    if (access != "")
    {
      sep = itemSeparator;
    }
    
    // If the element is not already in the cookie string then
    // add it.
    if (access.indexOf(element) < 0)
    {
      // Add the new item to the list.
      access += sep + element;
      
      // Write out to the cart.
      addItemValue(cartName, cartType, access);
    }
  }
  
  /*
   * removeAccessory(name, type) - This method will remove
   * the specified item from the accessory cookie.
   */
  function removeAccessory(name, type)
  {
    var leftpos;
    var rightpos;
    
    // Get the existing accessory cookie string.
    var access = getItemValue(cartName);
    
    // Build the element to be added.
    var element = genElement(name, type);
    
    // Check it the item exists in the accessory string.
    if ((leftpos = access.indexOf(element)) >= 0)
    {
      // Figure out the right pos of the element.
      rightpos = leftpos + element.length;
      
      // If the item is not at the beginning of the
      // string then we need to remove the separator too.
      if (leftpos > 0)
        leftpos -= itemSeparator.length;
        
      // Rebuild the string and remove the item.
      access = access.substring(0, leftpos) +
               access.substring(rightpos, access.length);

      // If the first item was removed then the separator
      // may need to be removed.
      if (access.indexOf(itemSeparator) == 0)
        access = access.substring(1, access.length);
                     
      // update the cart.
      if (access == "")
      {
        removeItem(cartName);
      }
      else
      {
        addItemValue(cartName, cartType, access);
      }
    }
  }  
    
  /*
   * removeAwningAccessories() - This method removes only  the
   * awning accessories from the cart.  What currently determines
   * a non-awning accessory is the type starts with "itemno". So
   * we will remove any items that don't fit this.
   */
  function removeAwningAccessories()
  {
    // Get the existing accessory cookie string.
    var access = getItemValue(cartName);
    
    if (access == "")
      return;
      
    // Split the string into array elements.
    var accessories = access.split(itemSeparator);    
    
    // Loop through and see if we find items that
    // fit the criteria.
    var i;     
    for (i=0; i<accessories.length; i++)
    {
      // Get the element from the array.
      var element = accessories[i].split(elementSeparator);
      
      // See if we meet the criteria, if the type
      // does not begin with "itemno" then remove it.
      if (element[1].indexOf("itemno") != 0)
      {
        removeAccessory(element[0], element[1]);
      }
    }
  }
