// ****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 = '<table align=center width=550 cellpadding=3 cellspacing=1 border="0" bgcolor=#000000><tr><td colspan=4 bgcolor=#008080><center><img src="pics/cards2.gif" width=108 height=80 align=right><font class=3pt color=#FFFFFF><b>Welcome to your "Heaven Scent"</b><br><font color=#EFEFCF class=10pt><b>Online Checkout Center</b><br><font class=3pt color=#FFFFFF><b>The following items are currently in your shopping cart.</b></td></tr>'
// **comment out or delete the next line when formmail.cgi is installed 
//    ordFrm += '<form name="shopCart" method="GET" action="http://www.infohiway.com/javascript/shopcart/tyfyo.htm" onSubmit="return validate_form()"><table border=1 align=center>'
// **uncomment the next 5 lines when formmail.cgi is installed
  ordFrm += '<form name="shopCart" method="POST" action="http://www.hsbath.com/cart/formmail.php" onSubmit="return validate_form()"><table width=550 border=0 cellpadding=3 cellspacing=1 align=center>'
  ordFrm += '<input type="hidden" name="recipient" value="sandramcmillen@hotmail.com">'
  ordFrm += '<input type="hidden" name="subject" value="Online Catalog Order">'
  ordFrm += '<input type="hidden" name="redirect" value="http://www.hsbath.com/tyfyo.htm">'
  ordFrm += '<input type="hidden" name="print_config" value="orderDetail">'
  ordFrm += '<input type="hidden" name="orderDetail" value="newOrder">'
  ordFrm += '<tr><td align=center bgcolor=#004040><font class=2pt color=#FFFFFF><b>SKU#</b></td><td align=center bgcolor=#004040><font class=2pt color=#FFFFFF><b>Item Description</b></td><td align=center bgcolor=#004040><font class=2pt color=#FFFFFF><b>Item<br>Price</b></td><td align=center bgcolor=#004040><font class=2pt color=#FFFFFF><b>Order<br>Qty</b></td><td align=center bgcolor=#004040><font class=2pt color=#FFFFFF><b>Extended<br>Cost</b></td><td align=center bgcolor=#004040><font class=2pt color=#FFFFFF><b>Click to<br>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 align=center bgcolor=#DDEEEE><font class=2pt color=#000000><b>$' + itemCost[i] + '</td>'
   ordFrm += '<td align=center bgcolor=#DDEEEE><font class=2pt color=#000000><b>' + itemQt[i] + '</td>'
   ordFrm += '<td align=center bgcolor=#DDEEEE><font class=3pt color=#C00000><b>$' + fix(extCst[i]) + '</td>'
   ordFrm += '<td align=center bgcolor=#DDEEEE><font class=2pt color=#FFFFFF><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: </td><td bgcolor=#DDEEEE>'
  ordFrm += '<input type="text" name="subtotal" size=6 maxlength=6 value="0.00" '
  ordFrm += 'onFocus="document.shopCart.subtotal.blur()"></td><td bgcolor=#004040 align=center><font class=3pt color=#DDEEEE><b>&nbsp;</font></td></tr>'
  ordFrm += '<tr><td bgcolor=#008080 align=right colspan=4><font class=3pt color=#FFFFFF><b>Do You Live In Michigan?: &nbsp; <input type="radio" '
  ordFrm += 'name="stateResident" value="No" CHECKED onClick=update()>No<input type="radio" '
  ordFrm += 'name="stateResident" value="Yes" onClick=update()>Yes = 6.0% Sales Tax</td><td bgcolor=#DDEEEE> '
  ordFrm += '<input type="text" name="stateTaxes" size=6 maxlength=6 value="0.00" '
  ordFrm += 'onFocus="document.shopCart.stateTaxes.blur()"</td><td bgcolor=#004040 align=center><font class=3pt color=#DDEEEE><b>&nbsp;</font></td></tr>'
  ordFrm += '<tr><td bgcolor=#008080 align=right colspan=4>' + writeShipping() + '</td><td bgcolor=#004040 align=center><font class=3pt color=#DDEEEE><b>&nbsp;</font></td></tr>'
  ordFrm += '<tr><td bgcolor=#004040 align=right colspan=4><font color=#FFFFFF class=4pt><b>GRAND TOTAL: </td><td bgcolor=#DDEEEE align=left><input type="text" '
  ordFrm += 'name="grandtotal" size=6 maxlength=6 value="0.00" '
  ordFrm += 'onFocus="document.shopCart.grandtotal.blur()"></td><td bgcolor=#004040>'
  ordFrm += '<font class=3pt color=#DDEEEE><b>&nbsp;</font></td></tr></table><br>'

  ordFrm += '<table align=center width=550 border=0 cellpadding=5 cellspacing=0><tr><td>'
  ordFrm += '<p><font class=2pt color=#FFFFFF>*To avoid shipping delays - if you are ordering from us by credit card for the first time, '
  ordFrm += 'you can expedite your order by calling your credit card issuer and adding your "Ship To" address (if different from your regular billing address) to their records.</td></tr></table><br>'
  
  ordFrm += '<table align=center width=550 bgcolor=#004040 border=0 cellpadding=3 cellspacing=0><tr><td>'
  ordFrm += '<p><font class=3pt color=#FFFFFF><b>*We Value and Respect Your Privacy!</b> The personal information we receive from our customers is kept private -- we absolutely <u><b>WILL NOT</b></U> sell or share this information with anyone!<br><br>'
  ordFrm += '<center><font color=#DDEEEE class=4pt><b>Email: &nbsp; &nbsp;<input type="text" size=53 name="email"></td></tr>'
  ordFrm += '<tr><td align=center><font color=#DDEEEE class=4pt><b>Area Code and Phone Number: <input type="text" size="27" name="phone"></td></tr>'
  ordFrm += '<tr><td align=center><font color=#DDEEEE class=4pt><b>Payment: &nbsp; &nbsp; Credit Card<input type="radio" name="pay" value="Credit" CHECKED> '
  ordFrm += ' &nbsp; &nbsp; Check<input type="radio" name="pay" value="check">'
  ordFrm += ' &nbsp; &nbsp; Money Order<input type="radio" name="pay" value="moneyOrd"></td></tr>'
  ordFrm += '<tr><td align=center><font color=#DDEEEE class=4pt><b>Credit Card Number: <input type="text" '
  ordFrm += 'size="32" name="credit_card_number"> &nbsp;Exp: <input type="text" size="5" maxlength="5" name="expiration_date"></td></tr>'
  ordFrm += '<tr><td align=center><br>'
  ordFrm += '<font color=#DDEEEE class=4pt><b>First Name: <input type="text" size="50" name="FirstName"></td></tr>'
  ordFrm += '<tr><td align=center><font color=#DDEEEE class=4pt><b>Last Name: <input type="text" size="50" name="LastName"></td></tr>'
  ordFrm += '<tr><td align=center><font color=#DDEEEE class=4pt><b>Street Address: <input type="text" size="47" name="Street1"></td></tr>'
  ordFrm += '<tr><td align=center><font color=#DDEEEE class=4pt><b>Street Address: <input type="text" size="47" name="Street2"></td></tr>'
  ordFrm += '<tr><td align=center><font color=#DDEEEE class=4pt><b>City: <input type="text" size="55" name="City"></td></tr>'
  ordFrm += '<tr><td align=center><font color=#DDEEEE class=4pt><b>State: <input type="text" size="54" name="State"></td></tr>'
  ordFrm += '<tr><td align=center><font color=#DDEEEE class=4pt><b>Country: <input type="text" size="52" name="Country"></td></tr>'
  ordFrm += '<tr><td align=center><font color=#DDEEEE class=4pt><b>Postal Code: <input type="text" size="49" name="zip"></td></tr>'
  ordFrm += '<tr><td><center><br><font color=#DDEEEE class=4pt><b>Special Instructions, Inquiries or Questions</b><br><font class=2pt color=#FFFFFF><b>(If you are shipping to an address other than you normal billing address, list it here.)</b><br>'
  ordFrm += '<textarea wrap=virtual rows=6 cols=58 name="instructions"></textarea>'
  ordFrm += '<br></center><font class=2pt color=#FFFFFF><b>*To avoid shipping delays</b> - if you are ordering from us by credit card for the first time, '
  ordFrm += 'you can expedite your order by calling your credit card issuer and adding your "Ship To" address (if different from your regular billing address) to their records.'
  ordFrm += '</td></tr></table><br><center><font class=3pt color=#FFFFFF><b>Your order will take 45-60 seconds to process.<br><font class=5pt color=#FF6300>Please hit the SEND button only once.</b><br><br><input type="submit" '
  ordFrm += 'name="submit" value="*Send Your Order*"> &nbsp; &nbsp; '
  ordFrm += '<input type="reset" value="Empty Shopping Cart" '
  ordFrm += 'onClick=killCart()></center><br><br></form><p>'
  document.write(ordFrm);
  document.close();
}


function clearIt(num) {
 itemNum[num] = "item"
 itemDescr[num] = "cancelled"
 itemCost[num] = 0
 rewriteCookie(num)
 update()
}



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 totals****
// ****and shipping/handling select box.****

function writeShipping() {
 // you can change the values in the shipping pop-up menu
 // by changing the values in the options array below
 leng = 13;
 options = new Array(leng);
 options[0] = "Please Select Appropriate Shipping Rate: &nbsp; ";
 options[1] = "(based on amount of your purchase subtotal)";
 options[2] = "*********************************************";
 options[3] = "$0.01 to $15.00 ......................... $5.95 ";
 options[4] = "$15.01 to $25.00 ....................... $7.95 ";
 options[5] = "$25.01 to $35.00 ....................... $8.95 ";
 options[6] = "$35.01 to $45.00 ....................... $10.95 ";
 options[7] = "$45.01 to $55.00 ....................... $12.95 ";
 options[8] = "$55.01 to $65.00 ....................... $13.95 ";
 options[9] = "$65.01 to $75.00........................ $14.95 ";
 options[10] = "$75.01 to $85.00........................ $16.95 ";
 options[11] = "$85.01 to $95.00........................ $17.95 ";
 options[12] = "Over $95.01................................ $18.95 ";
 html_code2 = "";
 
 html_code2 += '<img src="cart/arrow_down1.gif"> &nbsp; <font color=#FFFFFF class=4pt><b>SHIPPING and HANDLING: &nbsp;<img src="cart/arrow_down1.gif">&nbsp; <br><select size=1 '
  + 'name="ShipVia" onChange="update()">';
 for (o=0;o<leng;o++)
  html_code2 += '<option value="'+options[o]+'"'
   + ((o==0) ? ' selected>':'>') + options[o];
 html_code2 += '</select></td><td bgcolor=#DDEEEE>';
 html_code2 += '<input type="text" name="ShipCharges" size=6 '
 html_code2 += 'maxlength=6 value="0.00" '
 html_code2 += 'onFocus="document.shopCart.ShipCharges.blur()">';
 return html_code2;
}

// **Begin code section for order totals calculations**

var shipCost = 0

// **Change the values below to your shipping charges.**
// **These definitions correspond to the shipping select**
// **options defined in the writeShipping() function.**

function shippingCost() {
shipCost = 0
itemUp = counter - 1 // variable to increment charges
multiplier = 1 // additional charge per item after first.
 if (document.shopCart.ShipVia.options[0].selected) {
   shipCost = 0 }  // 
if (document.shopCart.ShipVia.options[1].selected) {
   shipCost = 0 }  // 
if (document.shopCart.ShipVia.options[2].selected) {
   shipCost = 0 }  // 
 if (document.shopCart.ShipVia.options[3].selected) {
   shipCost = 5.95 }  // 
 if (document.shopCart.ShipVia.options[4].selected) {
   shipCost = 7.95 }  // 
 if (document.shopCart.ShipVia.options[5].selected) {
   shipCost = 8.95 }  // 
 if (document.shopCart.ShipVia.options[6].selected) {
   shipCost = 10.95 }  // 
 if (document.shopCart.ShipVia.options[7].selected) {
   shipCost = 12.95 }  // 
 if (document.shopCart.ShipVia.options[8].selected) {
   shipCost = 13.95 }  // 
 if (document.shopCart.ShipVia.options[9].selected) {
   shipCost = 14.95 }  // 
 if (document.shopCart.ShipVia.options[10].selected) {
   shipCost = 16.95 }  // 
 if (document.shopCart.ShipVia.options[11].selected) {
   shipCost = 17.95 }  // 
 if (document.shopCart.ShipVia.options[12].selected) {
   shipCost = 18.95 }  // 
}

// **Function to calculate the order totals. **
// **This is triggered whenever the order form **
// **info is changed by the user.**

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);
 shippingCost()
 taxes = fix(sub_total) * .06 // adjust for your tax rate.
 with_Shipping = shipCost

// if you want to establish a minimum shipping charge, adjust the statements
// below for the mimimum charge per category and uncomment the statements

/*
 if ((with_Shipping < 8) && (document.shopCart.ShipVia.options[0].selected)) {
   with_Shipping = 8 // adjust to minimum shipping charge for shipping option 0
 }
 if ((with_Shipping < 7) && (document.shopCart.ShipVia.options[1].selected)) {
   with_Shipping = 6 // adjust to minimum shipping charge for shipping option 1
 }
 if ((with_Shipping < 6) && (document.shopCart.ShipVia.options[2].selected)) {
   with_Shipping = 6 // adjust to minimum shipping charge for shipping option 2
 }
 if ((with_Shipping < 5) && (document.shopCart.ShipVia.options[3].selected)) {
   with_Shipping = 5 // adjust to minimum shipping charge for shipping option 3
 }
*/

// if you want to establish a maximum shipping charge, adjust the statements
// below for the top charge per category and uncomment the statements

/*
 if ((with_Shipping > 15) && (document.shopCart.ShipVia.options[0].selected)) {
   with_Shipping = 15 // adjust to maximum shipping charge for shipping option 0
 }
 if ((with_Shipping > 14) && (document.shopCart.ShipVia.options[1].selected)) {
   with_Shipping = 14 // adjust to maximum shipping charge for shipping option 1
 }
 if ((with_Shipping > 13) && (document.shopCart.ShipVia.options[2].selected)) {
   with_Shipping = 13 // adjust to maximum shipping charge for shipping option 2
 }
 if ((with_Shipping > 12) && (document.shopCart.ShipVia.options[3].selected)) {
   with_Shipping = 12 // adjust to maximum shipping charge for shipping option 3
 }
*/


 if (document.shopCart.stateResident[0].checked) {
   document.shopCart.stateTaxes.value = "0.00"
 } else {
  document.shopCart.stateTaxes.value = fix(taxes)
 }

 document.shopCart.ShipCharges.value = fix(with_Shipping)
 grand_total = sub_total + taxes + with_Shipping
 noTaxTotal = sub_total + with_Shipping
 if (document.shopCart.stateResident[0].checked) {
   document.shopCart.grandtotal.value = fix(noTaxTotal)
 } else {
  document.shopCart.grandtotal.value = fix(grand_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 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 validate_form() {
 detailUpdate = "\n"
 for (i=1; i<=counter; i++) {
   fixExtended = fix(extCst[i])
   detailUpdate += itemNum[i] + ' ' + itemDescr[i] + ' QTY ' + itemQt[i] + ' at $' + itemCost[i] + ' ea. = $' + fixExtended + '\n'
   fixExtended = ""
   }
 document.shopCart.orderDetail.value = detailUpdate
 validity = true;
 if (document.shopCart.FirstName.value == "") {
  alert('You must enter your first name!');
  validity = false;
 }
 if (document.shopCart.LastName.value == "") {
  alert('You must enter your last name!');
  validity = false;
 }
 if (document.shopCart.email.value == "") {
  alert('You must enter your email address!');
  validity = false;
 }
 if (document.shopCart.phone.value == "") {
  if (confirm("You did not enter a phone number! Click 'Cancel' if you do not wish to provide a phone number. Click 'OK' if you want to provide a phone number."))
   valid = false;
  else
   document.shopCart.phone.value = 'no phone number provided';
 }
 if ((document.shopCart.credit_card_number.value == "") && (document.shopCart.pay[0].checked)) {
  alert('You must enter the credit card number!');
  validity = false;
 } else if ((document.shopCart.expiration_date.value == "") && (document.shopCart.pay[0].checked)) {
  alert('You must enter the expiration date!');
  validity = false;
 }
 if ((document.shopCart.Street1.value == "") && (document.shopCart.Street2.value == "")) {
  alert('You must enter a street address!');
  validity = false;
 }
 if (document.shopCart.City.value == "") {
  alert('You must enter a city!');
  validity = false;
 }
 if (document.shopCart.State.value == "") {
  alert('You must enter a state or province!');
  validity = false;
 }
 if (document.shopCart.Country.value == "") {
  if (confirm("You did not enter a country! Click 'Cancel' if you live in the United States. Click 'OK' if you need to fill in a country."))
   valid = false;
  else
   document.shopCart.Country.value = 'United States';
 }
 if (document.shopCart.zip.value == "") {
  alert('You must enter zip code or postal code!');
  validity = false;
 }
 if (validity) {
  alert("Thank you for your order!");
  killCookie("Scart");
 }
 return validity;
}

// **Function to clear the shopping cart.**

function killCart() {
 killCookie("Scart")
 history.go(-1)
}
