
var bottles = new Array();

bottles['PNBT072'] = new Array('60', '2007 Pinot Noir, Buena Tierra Vineyard');
bottles['PNRR072'] = new Array('42', '2007 Pinot Noir, Russian River Valley');
bottles['PNHC072'] = new Array("42", '2007 Pinot Noir, Humboldt County');
bottles['TROIKA']  = new Array("120","2006 Pinot Noir, Troika, Barrel Select Reserve");
bottles['PNMD062'] = new Array("45", "2006 Pinot Noir, Morning Dew Ranch");
bottles['PNWV062'] = new Array("60", "2006 Pinot Noir, Wiley Vineyard");
bottles['ZNVN072'] = new Array("34", '2007 Zinfandel, Guido Venturi Vineyard &nbsp; &nbsp; <font color="red">New!</font>');
bottles['ZNBR072'] = new Array('34', '2007 Zinfandel, Braccialini Vineyard');
bottles['ZNMA062'] = new Array("45", "2006 Zinfandel, Martinelli Road Vineyard Old Vine");
bottles['SYR072']  = new Array("36", "2007 Syrah, Russian River Valley");
bottles['FRC092']  = new Array("18", '2009 French Colombard, Russian River Valley &nbsp; <font color="red">New!</font>');

function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1) { endstr = document.cookie.length; }
  return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
  var arg = name + "="; var alen = arg.length;
  var clen = document.cookie.length; var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg) { return getCookieVal (j); }
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) { break; } 
  }
  return null;
}

function SetCookie (name, value) {
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path));
}

function DeleteCookie (name) {
  var exp = new Date();
  exp.setTime (exp.getTime() - 1000*(24 * 60 * 60 * 1000));  // This cookie is history
  var cval = GetCookie (name);
  SetCookie (name, cval, exp, '/')
}

var Chips = new Array();

// The "cart" cookie contains a string in the form sku1=m:sku2=n:.. etc.
// Typical example: PNRR072=2:TROIKA=1:ZNBR062=3
// This is broken into an array with the : delimiter to get sku & quantity pairs
// They come in no particular order and new skus can be added without code mods.
// Each chip is an element of an associative array with the sku as the key and the
// quantity as the value.
// Chips are typically called with GetChip(sku) and SetChip(sku,num) within documents.
// Those routines iterate through all the Chips to find the matching sku,
// and then extract or insert the appropriate number. If a sku/number to be added or
// modified isn't already listed in the cookie string, it is appended at the end.
// The whole cookie is re-written when setting a chip, using existing values in
// the stored chips.
// The cookie value is shared between all pages at the same directoty level, so
// it can be used to preserve wine selection information between pages and visits.

function GetChips() {
   var tmp = GetCookie('cart');
   var bottleCount = 0;
   if (tmp != null) {
      var chipList = tmp.split(":");
      for(i=0; i<chipList.length; i++) {
         skunum = chipList[i].split("=");
         sku = skunum[0]; num = skunum[1];
         if (num != '') { Chips[sku] = new Array(num); }
      }
      for (j in Chips) { if ((Chips[j] != '') && (j != 'state') && (j != 'ship_method')) { bottleCount += parseInt(Chips[j]) } }
   }
   return bottleCount;
}

function SetChip(sku,val) {
   GetChips();
   var found=false;
   var newString = "";
   for (j in Chips) { if (sku == j) { Chips[j] = val; found=true; }  }
   if (! found) { Chips[sku] = new Array(val); }
   for (j in Chips) { if ((j != '') && (Chips[j] != "0")) { newString += j + "=" + Chips[j] + ":"; } }
   var expdate = new Date();
   expdate.setTime (expdate.getTime() + 30*(24 * 60 * 60 * 1000)) // 30 days from now
   SetCookie ('cart',newString,expdate,'/');
}

function updateCart() {
   bottleCount = GetChips();
   var cartCode = '';
   if (bottleCount >0) {
      cartCode += '<p class="carttext" >' + bottleCount + ' bottle';
      if (bottleCount > 1) { cartCode += 's'; }
      cartCode += ' in your Shopping Cart<br /> <a href="order.html"><b>View Cart</b></a> &nbsp; <a href="javascript:clearCart()"><b>Clear Cart</b> </p>';
      document.getElementById('topcart').innerHTML = cartCode;
      document.getElementById('topcart').style.visibility='visible'; document.getElementById('topcart').style.display='block';
   } else {  document.getElementById('topcart').style.visibility='hidden'; document.getElementById('topcart').style.display='none';
   }
   for (k in Chips) {
     if ((Chips[k] != '') && (k != 'state') && (k != 'ship_method') && (k != 'SYR072') && (k != '')) {  document.forms.orderform[k].value=Chips[k]; }
   }
}

function checkData(sku,data) {
   if(data.length > 0) {
      if (isNaN(data)) {
         alert("Quantity field can only accept numbers.");
         document.forms.orderform[sku].value = "";
      } else  {
         SetChip(sku,data);
         updateCart();
         if (data == "0") { document.forms.orderform[sku].value=''; }
      }
   }
}

function clearCart() {
   for (j in Chips) { SetChip(j,"0"); }
   DeleteCookie('cart'); 
   updateCart();
   for (i in bottles) {
      if ((i != '') && (i != 'SYR072')) { document.forms.orderform[i].value=''; }
   }
}
