// ****Begin shopping cart functions.****

// ** global variables for the shopping cart.**

var itemNum = new Array();
var itemDescr = new Array();
var itemCost = new Array();
var itemQt=new Array();
var extCst=new Array();


function itemsOrdered() {
 if (getCookieData("Scart")) {
 substr0 = getCookieData("Scart")
 cLen = substr0.length
 offset0 = substr0.indexOf("@")
 counter = substr0.substring(0,offset0)
 j = 0
 for (i=1; i<=counter; i++) {
  offsetq = eval('offset' + j + '');
  substrq = eval('substr' + j + '');
  eval('ind' + i + ' = offsetq + 1');
  eval('substr' + i + ' = substrq.substring(ind' + i + ',cLen)');
  eval('offset' + i + ' = substr' + i + '.indexOf("^")');
  eval('item' + i + ' = substr' + i + '.substring(0,offset' + i + ')');
  eval('catInd' + i + ' = item' + i + '.indexOf("`")');
  eval('itemCat' + i + ' = item' + i + '.substring(0,catInd' + i + ')');
  eval('catqt' + i + ' = item' + i + '.indexOf("*")');
  eval('qtcat' + i + ' = item' + i + '.substring((catInd'+i+'+1),catqt' + i + ')');
  eval('descrInd' + i + ' = item' + i + '.indexOf("~")');
  eval('itemDes' + i + ' = item' + i + '.substring((catqt' + i + ' + 1),descrInd' + i + ')');
  eval('itemPr' + i + ' = item' + i + '.substring((descrInd' + i + ' + 2),offset' + i + ')'); 
  eval('orderDetail(i,itemCat' + i + ',qtcat' + i + ',itemDes' + i + ',itemPr' + i + ')');
  j++
 }
 writeCart();
 }
}



function orderDetail(seq,num,qt,descr,cost) {
 itemNum[seq] = num
 itemQt[seq] = qt
 itemDescr[seq] = descr
 itemCost[seq] = cost
}




function writeCart() {
  var ordFrm = '<h2 align="center">&nbsp;</h2><p>'
  ordFrm += '<form name="shopCart"><table width=550 cellpadding=3 cellspacing=1 border="0" bgcolor=#000000>'
  ordFrm += '<tr><td colspan=6 bgcolor=#008080><center><img src="pics/cards2.gif" width=108 height=80 align=right><font class=4pt color=#DDEEEE><b>Welcome to Your "Heaven Scent"</b><br><font color=#EFEFCF class=10pt><b>Online Shopping Cart</b><br><font class=3pt color=#DDEEEE><b>The following items are currently in your cart.</b></td></tr>'
  ordFrm += '<tr><td bgcolor=#004040 align=center><font class=2pt color=#FFFFFF><b>SKU#</b></td><td bgcolor=#004040 align=center><font class=2pt color=#FFFFFF><b>Item Description</b></td><td bgcolor=#004040 align=center><font class=2pt color=#FFFFFF><b>Item Price</b></td><td bgcolor=#004040 align=center><font class=2pt color=#FFFFFF><b>Order Qty</b></td><td bgcolor=#004040 align=center><font class=2pt color=#FFFFFF><b>Extended Cost</b></td><td bgcolor=#004040 align=center><font class=2pt color=#FFFFFF><b>Click to Remove</b></td></tr>'
  for (i = 1; i <= counter; i++) {
   extCst[i]=eval(itemCost[i]*itemQt[i])
   ordFrm += '<tr><td align=center bgcolor=#DDEEEE><font class=2pt color=#000000><b>' + itemNum[i] + '</td>'
   ordFrm += '<td bgcolor=#DDEEEE><font class=2pt color=#000000><b>' + itemDescr[i] + '</td>'
   ordFrm += '<td bgcolor=#DDEEEE align=center><font class=2pt color=#000000><b>$' + itemCost[i] + '</td>'
   ordFrm += '<td bgcolor=#DDEEEE align=center><font class=2pt color=#000000><b>' + itemQt[i] + '</td>'
   ordFrm += '<td bgcolor=#DDEEEE align=center><font class=3pt color=#C00000><b>$' + fix(extCst[i]) + '</td>'
   ordFrm += '<td bgcolor=#DDEEEE align=center><font class=2pt color=#000000><b><a class=black href="javascript:clearIt('+i+')">Remove</a></td></tr>'
  }
  ordFrm += '<tr><td bgcolor=#004040 align=right colspan=4><font class=4pt color=#FFFFFF><b>SUB TOTAL:</b> </td><td align=center bgcolor=#DDEEEE>'
  ordFrm += '<input type="text" name="subtotal" size=6 maxlength=6 value="0.00" '
  ordFrm += 'onFocus="document.shopCart.subtotal.blur()"></td><td align=center bgcolor=#FF6300><font color=#FFFFFF class=3pt><b><a class=black href="checkout1.htm"><img src=cart/orange_arrow.gif width=7 height=9 border=0> Go To<br>Checkout</a></b></font></td></tr></table>'
  ordFrm += '<center><p><input type="button" name="return" '
  ordFrm += 'value="*Return To Catalogs*" onClick="history.go(-1)">'
  ordFrm += ' &nbsp; &nbsp; <font color=#FFFFFF class=6pt><b><a class=orange href="checkout1.htm">'
  ordFrm += 'Checkout</a></b></font>'
  ordFrm += ' &nbsp; &nbsp; <input type="reset" '
  ordFrm += 'value="Empty Shopping Cart" onClick=killCart()>'
  ordFrm += '</p></center></form><p>'
  document.write(ordFrm);
  document.close();
}



function clearIt(num) {
 itemNum[num] = "item"
 rewriteCookie(num)
}



function rewriteCookie(num) {
 dataUpdate = ""
 for (i=1; i<=counter; i++) {
   if (itemNum[i] != "item") {
    dataUpdate += itemNum[i] + '`' + itemQt[i] + '*' + itemDescr[i] + '~$' + itemCost[i] + '^'
   }
 }
 counter = counter - 2
 cookData = dataUpdate
 setCookieData("Scart", cookData, expdate.toGMTString())
 history.go(0)
}

// ****End shopping cart detail section.****

// ****Start of code section to display subtotal.****

function update() {
 if (getCookieData("Scart")) {
  var sub_total = 0;
  for (i=1; i<itemNum.length; i++)
   eval('sub_total += parseFloat(extCst[' + i + ']);');
  document.shopCart.subtotal.value= fix(sub_total);
 }
}

function fix(num) {
 var decplaces = 2
  var str = "" + Math.round (eval(num) * Math.pow(10,decplaces))
  while (str.length <= decplaces) {
   str = "0" + str
  }
  var decpoint = str.length - decplaces
  return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
 return str;
}

// **Function to clear the shopping cart.**

function killCart() {
 killCookie("Scart")
 history.go(-1)
}
