// This file is (C) Copyright 2004 by ASI Image Studios, Inc.
// All rights reserved.
//
// this file depends on cookie.js being loaded first.
// Public interfaces:
// cartInitialize() initializes the shopping cart from a (possibly)
//   pre-existing cookie;
// cartAddProduct(id, qty) adds a product/count to the shopping cart;
// if shoppingCartMeta is provided in the form as shown below
//   then manufacturer names, product model names/numbers, and prices
//   are passed around and optionally displayed;
// if cartDisplay() is provided, the function is called with an array
//   of product ids/qtys, as well as the shoppingCartMeta array.

var shoppingCart;
var shoppingCartMeta;

// format for shoppingCartMeta
// shoppingCartMeta[id]['manufacturer']
// shoppingCartMeta[id]['model']
// shoppingCartMeta[id]['price']

function cartPopulate()
{
  var cart_contents;
  var products, spec, id, qty;
  var nsk;

  cart_contents = getCookie("cart");
  if(cart_contents)
    products = cart_contents.split(":");
  else
    products = new Array();
  for(i = 0; i < products.length; i++)
  {
    spec = products[i].split("#");
    id = spec[0];
    qty = new Number(spec[1]);
    if(qty)
    {
      if(shoppingCart[id])
        shoppingCart[id] += qty;
      else
        shoppingCart[id] = qty;
    }
  }

  // take cookie information from cartMeta (since the back button defeats
  // us otherwise), but allow cartMetaSource to override the cartmeta cookie.
  cart_contents = getCookie("cartmeta");
  if(cart_contents)
    products = cart_contents.split(":");
  else
    products = new Array();
  for(i = 0; i < products.length; i++)
  {
    spec = products[i].split("#");
    id = spec[0];
    shoppingCartMeta[id] = new Object;
    shoppingCartMeta[id]['manufacturer'] = spec[1];
    shoppingCartMeta[id]['model'] = spec[2];
    shoppingCartMeta[id]['price'] = parseInt(spec[3]);
  }
  for(id in shoppingCartMetaSource)
  {
    if(!shoppingCartMeta[id])
      shoppingCartMeta[id] = new Object;
    shoppingCartMeta[id]['manufacturer'] = 
      shoppingCartMetaSource[id]['manufacturer'];
    shoppingCartMeta[id]['model'] = shoppingCartMetaSource[id]['model'];
    shoppingCartMeta[id]['price'] = shoppingCartMetaSource[id]['price'];
  }

  nsk = new Array();
  for(id in shoppingCart)
  {
    if(shoppingCartMeta[id])
      nsk[id] = shoppingCart[id];
  }
  shoppingCart = nsk;
}

// internal function called by cartFormURI() and cartUpdate()
function cartToString()
{
  var id;
  var carray = new Array();
  var count = 0;

  for(id in shoppingCart)
  {
    if(shoppingCart[id] > 0)
      carray[count++] = id + "#" + shoppingCart[id];
  }
  return(carray.join(":"));
}

function cartMetaToString()
{
  var id;
  var carray = new Array();
  var count = 0;

  for(id in shoppingCartMeta)
  {
    if(shoppingCart[id] > 0)
      carray[count++] = id + "#" + shoppingCartMeta[id]['manufacturer']
                           + "#" + shoppingCartMeta[id]['model']
			   + "#" + shoppingCartMeta[id]['price'];
  }
  return(carray.join(":"));
}

function cartUpdate()
{
  setCookie("cart", cartToString(), 0, "/", cookieDomain, 0);
  setCookie("cartmeta", cartMetaToString(), 0, "/", cookieDomain, 0);
}

function cartInitialize()
{
  var current_cookie;
  var id, products_to_display;

  shoppingCart = new Array();
  shoppingCartMeta = new Array();

  current_cookie = getCookie("cart");
  if(!current_cookie)
  {
    setCookie("cart", "", 0, "/", cookieDomain, 0);
    setCookie("cartmeta", "", 0, "/", cookieDomain, 0);
  }
  cartPopulate();

  if(self.cartDisplay)
    cartDisplay(shoppingCart, shoppingCartMeta);

  if(self.cartMetaDisplay)
  {
    products_to_display = 0;
    for(id in shoppingCart)
    {
      if(id > 0 && shoppingCart[id] > 0)
        products_to_display++;
    }
    cartMetaDisplay(products_to_display > 0);
  }
}

function cartMetaProduct(id, manufacturer, model, price)
{
  if(!shoppingCartMeta[id])
  {
    shoppingCartMeta[id] = new Object;
    shoppingCartMeta[id]['manufacturer'] = manufacturer;
    shoppingCartMeta[id]['model'] = model;
    shoppingCartMeta[id]['price'] = price;
  }

  cartUpdate();

  if(self.cartDisplay)
    cartDisplay(shoppingCart, shoppingCartMeta);
  if(self.cartMetaDisplay)
    cartMetaDisplay(true);
}

function cartAddProduct(id, qty, manufacturer, model, price)
{
  if(!shoppingCart)
    return(false);

  if(!shoppingCart[id])
    shoppingCart[id] = new Number(qty);
  else
    shoppingCart[id] += new Number(qty);
  if(shoppingCart[id] < 0)
    shoppingCart[id] = 0;

  cartMetaProduct(id, manufacturer, model, price);
}

function cartSetProduct(id, qty, manufacturer, model, price)
{
  if(!shoppingCart)
    return(false);

  shoppingCart[id] = new Number(qty);
  if(shoppingCart[id] < 0)
    shoppingCart[id] = 0;

  cartMetaProduct(id, manufacturer, model, price);
}

function cartEmpty()
{
  if(!shoppingCart)
    return(false);

  shoppingCart = new Array();
  setCookie("cart", "", 0, "/", cookieDomain, 0);
  shoppingCartMeta = new Array();
  setCookie("cartmeta", "", 0, "/", cookieDomain, 0);
  if(self.cartDisplay)
    cartDisplay(shoppingCart, shoppingCartMeta);
  if(self.cartMetaDisplay)
    cartMetaDisplay(true);
}
