// scart.js JavaScript Development by JavaScriptDesign.com
// email solutions@javascriptdesign.com  Copyright 2000
// All Rights Reserved. Copyright Notice Must Remain in
// Source files and Catalog Pages.

// ****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 to parse the cookie and extract the ordered **
// **items from the string. This in turn triggers the **
// **writeCart() function to display the shopping cart. **

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();
 }
}

// **Builds an array of the items to load the cart.**

function orderDetail(seq,num,qt,descr,cost) {
 itemNum[seq] = num
 itemQt[seq] = qt
 itemDescr[seq] = descr
 itemCost[seq] = cost
}

// ** Function to write the shopping cart details **
// ** into the table on the shopping cart page.


function writeCart() {
  var ordFrm = '<h2 align="center">Shopping Cart</h2><p>'
  ordFrm += '<form name="shopCart"><table border=1 align=center width=100% cellspacing=2>'
  ordFrm += '<tr><td bgcolor=FFFFCC align=center><b>Product Description</b></td>'
  ordFrm += '<td bgcolor=FFFFCC align=center><b>Unit Price</b></td>'
  ordFrm += '<td bgcolor=FFFFCC align=center><b>Order<br>Qty</b></td>'
  ordFrm += '<td bgcolor=FFFFCC align=center><b>Extended<br>Cost</b></td>'
  ordFrm += '<td bgcolor=FFFFCC align=center>Click to<br>Remove</td></tr>'
  for (i = 1; i <= counter; i++) {
   extCst[i]=eval(itemCost[i]*itemQt[i])
   ordFrm += '<tr><td>' + itemDescr[i] + '</td>'
   ordFrm += '<td align="center">' + itemCost[i] + '</td>'
   ordFrm += '<td align="center">' + itemQt[i] + '</td>'
   ordFrm += '<td align="center">' + fix(extCst[i]) + '</td>'
   ordFrm += '<td align=center><a href="javascript:clearIt('+i+')">Remove</a></td></tr>'
  }
  ordFrm += '<tr><td align=right colspan=3>SUB TOTAL: </td><td align=center>'
  ordFrm += '<input type="text" name="subtotal" size=6 maxlength=6 value="0.00" '
  ordFrm += 'onFocus="document.shopCart.subtotal.blur()"></td><td>&nbsp; </td></tr></table>'
  ordFrm += '<center><p><a href="#" onClick="history.go(-1)">Shop for More</a>'
  ordFrm += ' &nbsp; &nbsp; <a href="checkout.htm"><font size="+1">'
  ordFrm += 'Checkout</font></a>'
  ordFrm += ' &nbsp; &nbsp; <a href="#" onClick=killCart()>Empty Shopping Cart</a>'
  ordFrm += '</p></center></form><p>'
  document.write(ordFrm);
  document.close();
}

// ** Function to delete a line item upon user request. **

function clearIt(num) {
 itemNum[num] = "item"
 rewriteCookie(num)
}

// **Function to rewrite the cookie when the user **
// **deletes a line item from the shopping cart. **

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)
}
