// jshop.js JavaScript Development by JavaScriptDesign.com

// ****Begin shopping cart functions.****

// ** global variables for the shopping cart.**

var itemNum = new Array();
var itemDescr = new Array();
var itemCost = 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('descrInd' + i + ' = item' + i + '.indexOf("~")');
  eval('itemDes' + i + ' = item' + i + '.substring((catInd' + i + ' + 1),descrInd' + i + ')');
  eval('itemPr' + i + ' = item' + i + '.substring((descrInd' + i + ' + 2),offset' + i + ')'); 
  eval('orderDetail(i,itemCat' + i + ',itemDes' + i + ',itemPr' + i + ')');
  j++
 }
 writeCart();
 }
}

// **Builds an array of the items to load the cart.**

function orderDetail(seq,num,descr,cost) {
 itemNum[seq] = num
 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">EZ Cash Shopping Cart</h2><p>'
  ordFrm += '<form name="shopCart"><table border=1 align=center>'
  ordFrm += '<tr><td>Item</td><td>Item Description</td><td align="center">Item<br>Price</td><td align="center">Click to<br>Remove</td></tr>'
  for (i = 1; i <= counter; i++) {
   ordFrm += '<tr><td>' + itemNum[i] + '</td>'
   ordFrm += '<td>' + itemDescr[i] + '</td>'
   ordFrm += '<td align="center">' + itemCost[i] + '</td>'
   ordFrm += '<td align=center><a href="javascript:clearIt('+i+')">Remove</a></td></tr>'
  }
  ordFrm += '<tr><td align=right colspan=2>SUB TOTAL: </td><td>'
  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><input type="button" name="return" '
  ordFrm += 'value="*Return To Catalogs*" onClick="history.go(-1)">'
  ordFrm += ' &nbsp; &nbsp; <a href="checkout.htm"><font size="+1">'
  ordFrm += 'Checkout</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") {
    dataUpdate += itemNum[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(itemCost[' + i + ']);');
  document.shopCart.subtotal.value= fix(sub_total);
 }
}

function fix(num) {
 string = "" + num;
 if (string.indexOf('.') == -1)
  return string + '.00';
 seperation = string.length - string.indexOf('.');
 if (seperation > 3)
  return string.substring(0,string.length-seperation+3);
 else if (seperation == 2)
  return string + '0';
 return string;
}

// **Function to clear the shopping cart.**

function killCart() {
 killCookie("Scart")
 history.go(-1)
}
