// Thanks to the many contributors of the various scripts that
// make up this web site.  They made it possible to construct the
// web site at no cost and still obtain a professional appearance.

// Obtained from Cut-N-Paste JavaScript from ISN Toolbox
// Infohighway, Inc. www.infohighway.com
//    -Dynamic Pop Up Windows Copyright 1999
//    -Click Buttons
//    -JavaScript Shopping Cart Copyright 1999, 2000
//       JavaScriptDesign.com

// Obtained from Earthweb www.earthweb.com
//    -JavaScript Shopping Cart using frames Copyright Gordon Smith
//       with thanks to Joe Burns, Ph.D.

// Cosmetic changes and the various combinations of these
// scripts Copyright 2000 Benjamin Scott Abruzzo.  All comments and
// copyrights should be included if the javascript is borrowed.  No
// portion should be directly sold

// 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 itemWeight = 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)
	 //alert("substr0:" + substr0 + " cLen: " + cLen + " \ncounter: " + counter);

 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('weightIndex' + 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),weightIndex' + i + ')'); 

  eval('itemWgt' + i + ' = item' + i + '.substring((weightIndex' + i + ' + 1),offset' + i + ')');
  eval('orderDetail(i,itemCat' + i + ',qtcat' + i + ',itemDes' + i + ',itemPr' + i + ',itemWgt' + i + ')');
  j++
 }
 writeCart();
 }
}

// **Builds an array of the items to load the cart.**

function orderDetail(seq,num,qt,descr,cost,weight_) {
 itemNum[seq] = num
 itemQt[seq] = qt
 itemDescr[seq] = descr
 itemCost[seq] = cost
 itemWeight[seq] = weight_
}

// ** Function to write the shopping cart details **
// ** into the table on the shopping cart page.


function writeCart() {
  var ordFrm = '<form name="shopCart"><table border=1 align=center>'
  ordFrm += '<tr><td>Item</td><td>Item Description</td><td align="center">Order<br>Weight</td><td align="center">Item<br>Price</td><td align="center">Order<br>Qty</td><td align="center">Extended<br>Cost</td><td align="center">Click to<br>Remove</td></tr>'

  var totalWeight = 0;
  var subTotal = 0;
  for (i = 1; i <= counter; i++) {
   extCst[i]=eval(itemCost[i]*itemQt[i])
   ordFrm += '<tr><td>' + itemNum[i] + '</td>'
   ordFrm += '<td>' + itemDescr[i] + '</td>'
   ordFrm += '<td align="center">' + itemWeight[i] * itemQt[i] + ' oz. </td>'
   totalWeight += (itemWeight[i] * itemQt[i]);
   subTotal += (itemCost[i] * itemQt[i]);
   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>'
  }
  //Shipping Weight
  ordFrm += '<tr><td align=right colspan=2>Total Shipping Weight: </td><td>'
  ordFrm += '<input type="text" name="totalWgt" size=6 maxlength=6 value="' + totalWeight + ' oz." disabled></td>'

  //Total Price
  ordFrm += '<td align=right colspan=2>SUB TOTAL: </td><td>'
  ordFrm += '<input type="text" name="subtotal" size=6 maxlength=6 value="$' + fix(subTotal) + '" '
  ordFrm += 'onFocus="document.shopCart.subtotal.blur()"></td><td>&nbsp; </td></tr></table>'
  ordFrm += '<center><p><input type="button" name="return" '
  ordFrm += 'value="*Return To Catalogs*" onClick="history.go(-1)">'
  ordFrm += ' &nbsp; &nbsp; <a href="pause.asp"><font size="+1">'
  ordFrm += 'Check Out</font></a>'
  ordFrm += ' &nbsp; &nbsp; <input type="reset" '
  ordFrm += 'value="Empty Shopping Cart" onClick=killCart()>'
  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") {
	   //I think these markers are so you can find the values in the cookie
    dataUpdate += itemNum[i] + '`' + itemQt[i] + '*' + itemDescr[i] + '~$' + itemCost[i] + '%' + itemWeight[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)
}
