// 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();
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('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 = '<br>'
  ordFrm += '<form name="shopCart"><table bgcolor="#72502D" border=0 cellspacing=1 cellpadding=1 align=center>'
  ordFrm += '<tr bgcolor="#EBE0D6"><td align="center"><font color="#000000" face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Código</b></td>'
  ordFrm += '<td align="center"><font color="#000000" face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Valor<br>Unitário<b></td>'
  ordFrm += '<td align="center"><font color="#000000" face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Quant.</b></td>'
  ordFrm += '<td align="center"><font color="#000000" face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Valor Total</b></td>'
  ordFrm += '<td align="center"><font color="#000000" face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Click para<br>Remover</b></td></tr>'

  for (i = 1; i <= counter; i++) {
   extCst[i]=eval(itemCost[i]*itemQt[i])
   ordFrm += '<tr bgcolor="#ffffff"><td align="center">' + itemNum[i] + '</td>'
   ordFrm += '<td align="center">' + itemCost[i] + '</td>'
   ordFrm += '<td align="center">' + itemQt[i] + '</td>'
   ordFrm += '<td align="center">' + extCst[i] + '</td>'
   ordFrm += '<td align="center"><a href="javascript:clearIt('+i+')">Remover</a></td></tr>'
  }
  ordFrm += '<tr bgcolor="#ffffff"><td align=right colspan=4><font color="#000000" face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Valor Total:</b></td><td align="center">'
  ordFrm += '<input type="text" name="subtotal" size=6 maxlength=6 value="0.00" '
  ordFrm += 'onFocus="document.shopCart.subtotal.blur()"></td></tr></table>'
 // ordFrm += '<center><p><input type="button" name="return" '
 // ordFrm += 'value="*Retornar para o catálogo*" onClick="history.go(-1)">'
 // ordFrm += ' &nbsp; &nbsp; <a href="checkout.htm"><font size="+1">'
 // ordFrm += '<img src="final.gif" border="0"></font></a>'
 // ordFrm += ' &nbsp; &nbsp; <input type="reset" '
 // ordFrm += 'value="Limpar a tabela de compras" 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] + '`' + 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) {
 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)
}
