// ckout.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("CPcart")) {
 substr0 = getCookieData("CPcart")
 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 and**
// ** create the buyer details input form.**

var orddesc = 'Order Details: '		// order description

function writeCart() {
  var ordFrm = '<h2 align="center"></h2><p>'

  ordFrm += '<table><table border="0" width="100%">'
  ordFrm += '    <tr>'
    ordFrm += '      <td width="50%"><p align="left">'
  ordFrm += '<input type="submit" value=" Continue Shopping " onClick="javascript:history.go(-1)"></td>'
  ordFrm += '    </tr>'
  ordFrm += '  </table>'
  ordFrm += '<form name="shopCart" method="POST" action="https://secure.wp3.rbsworldpay.com/wcc/purchase" onSubmit="return validate_form()">'
  ordFrm += '<table border=1 align=center bgcolor="#CCCCCC" width="100%">'
  ordFrm += '<tr bgcolor="#CCCCCC">'
  ordFrm += '  <td align="center"><font face="Arial, Helvetica, sans-serif" size="2"><strong>Item</strong></font></td>'
  ordFrm += '  <td><font face="Arial, Helvetica, sans-serif" size="2"><strong>Description</strong></font></td>'
  ordFrm += '  <td align="center"><font face="Arial, Helvetica, sans-serif" size="2"><strong>Price</strong></font></td>'
  ordFrm += '  <td align="center"><font face="Arial, Helvetica, sans-serif" size="2"><strong>Qty</strong></font></td>'
  ordFrm += '  <td align="center"><font face="Arial, Helvetica, sans-serif" size="2"><strong>Cost</strong></font></td>'
  ordFrm += '  <td align="center"><font face="Arial, Helvetica, sans-serif" size="2"><strong> </strong></font></td>'
  ordFrm += '</tr>'

  for (i = 1; i <= counter; i++) {
    extCst[i]=eval(itemCost[i]*itemQt[i])
    ordFrm += '<tr>'
    ordFrm += '<td width="8%" align="center"> <font face="Arial, Helvetica, sans-serif" size="2">' + itemNum[i] + '</font></td>'
    ordFrm += '<td width="30%"><font face="Arial, Helvetica, sans-serif" size="2">' + itemDescr[i] + '</font></td>'
    ordFrm += '<td width="7%" align="center"><font face="Arial, Helvetica, sans-serif" size="2">£' + itemCost[i] + '</font></td>'
    ordFrm += '<td width="5%" align="center"><font face="Arial, Helvetica, sans-serif" size="2">' + itemQt[i] + '</font></td>'
    ordFrm += '<td width="7%" align="center"><font face="Arial, Helvetica, sans-serif" size="2">£' + fix(extCst[i]) + '</font></td>'
    ordFrm += '<td width="7%"align=center><a href="javascript:clearIt('+i+')">Remove</a></td></tr>'

    orddesc += '(' + itemNum[i] + '/' + itemQt[i] + '/' + itemCost[i] + ')'  
  }
 
 
//   ordFrm += '<input type="hidden" name="instId" value="57523">'
  ordFrm += '<input type="hidden" name="instId" value="240270">'
  ordFrm += '<input type="hidden" name="cartId" value="TankZone">'
  ordFrm += '<input type="hidden" name="currency" value="GBP">'
  ordFrm += '<input type="hidden" name="testMode" value="0">'

  ordFrm += '<input type="hidden" name="desc" value="' + orddesc + '">'

  ordFrm += '<tr><td align=right colspan=4>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 align=center><font color="#FFFF99"></font></td></tr>'
  ordFrm += '<tr><td align=right colspan=4>' + writeShipping() + '</td><td align=center><font color="#FFFF99"></font></td></tr>'
  ordFrm += '<tr>'
//  ordFrm += '   <td align=right colspan=2><a href="http://uk.finance.yahoo.com/m5?a=1&s=GBP&t=USD" target="_blank"><font face="Arial, Helvetica, sans-serif" size="2" color="#0000FF">Currency converter</font></a></TD>'
  ordFrm += '   <td align=right colspan=2><font face="Arial, Helvetica, sans-serif" size="2" color="#0000FF"></font></TD>'
  ordFrm += '   <td align=right colspan=2>Grand Total: </td><td align=left><input type="text" '
  ordFrm += 'name="cost" size=6 maxlength=6 value="0.00" '
  ordFrm += 'onFocus="document.shopCart.cost.blur()"></td><td>'
  ordFrm += '<font color="#FFFF99"></font></td></tr></table>'

  ordFrm += '<table width="100%">'
	
  ordFrm += '  <tr>'
  ordFrm += '    <td width="50%"><p align="left"><br>'
  ordFrm += '    <input type="reset" value="Empty Shopping Cart" onClick="killCart()"></td>'
  ordFrm += '  </tr>'
  ordFrm += '</table>'

 ordFrm += '<p align="left"><font face="Arial" size="3" color="#FF6600">Pay Online By Credit/Debit Card </font><br><br>'
 
  ordFrm += ' <p align="left"><font face="Arial, Helvetica, sans-serif" size="2" color="#000000">When '
  ordFrm += ' you have finished shopping, '
  ordFrm += ' click the "Proceed To WorldPay Secure Payment" button to pay online.'
  ordFrm += ' Oversea buyers, please remember to include shipping.'
  ordFrm += ' We accept all major credit and debit cards including Visa, Mastercard and Switch.'
  ordFrm += '</font></p>'  
 
  ordFrm += '  <table border="0" width="100%">'
  ordFrm += '    <tr>'
  ordFrm += '        <td width="50%"><form><input type="submit" value="Proceed To WorldPay Secure Payment"></form></td>'
  ordFrm += '    </tr>'
  ordFrm += '  </table>'  

  ordFrm += ' <p align="left"><font face="Arial, Helvetica, sans-serif" size="2" color="#000000">'
  ordFrm += ' Thank you for shopping with TankZone! '
  ordFrm += '</font></p></table>'  
 
  document.write(ordFrm);

  document.close();
}


// ** Function to delete a line item upon user request. **
function clearIt(num) {
 itemNum[num] = "item"
 itemDescr[num] = "cancelled"
 itemCost[num] = 0
 rewriteCookie(num)
 update()
}

// **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("CPcart", 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 = 26;
 options = new Array(leng);
 
 options[0] = "FREE Mainland UK Delivery";
 options[1] = "UK Next Working Day Special Delivery - £10";
 options[2] = "--------------------------------------";
 options[3] = "Europe Up to 500g - £4.29";
 options[4] = "Europe Up to 1kg  - £6.69";
 options[5] = "Europe Up to 2kg  - £10.69";
 options[6] = "Europe  2 to 10kg - £29.20";
 options[7] = "Europe 10 to 25kg - £42.48";
 options[8] = "--------------------------------------";
 options[9] =  "Rest of the World Up to 500g - £7.23";
 options[10] = "Rest of the World Up to 1kg  - £10.46";
 options[11] = "Rest of the World Up to 2kg  - 19.59";
 options[12] = "Rest of the World 2 to 10kg  - 54.48";
 options[13] = "Rest of the World 10 to 25kg - £82.95";
 options[14] = "--------------------------------------";
 options[15] = "Special Charge - £10.00";
 options[16] = "Special Charge - £20.00";
 options[17] = "Special Charge - £30.00";
 options[18] = "Special Charge - £40.00";
 options[19] = "Special Charge - £50.00";
 options[20] = "Special Charge - £60.00";
 options[21] = "Special Charge - £70.00";
 options[22] = "Special Charge - £80.00";
 options[23] = "Special Charge - £90.00";
 options[24] = "Special Charge - £100.00";
 options[25] = "Special Charge - £200.00";

 html_code2 = "";

// html_code2 += 'Postage & Packing: &nbsp; <select size=1 '
 
 html_code2 += '&nbsp; <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>';
 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 - 30 // variable to increment charges
multiplier = 1 // additional charge per item after first.
 if (document.shopCart.ShipVia.options[0].selected) { shipCost = 0.00 } // UK FREE
 if (document.shopCart.ShipVia.options[1].selected) { shipCost = 10.00 } // Next Day
 if (document.shopCart.ShipVia.options[2].selected) { shipCost = 0.00 } //  
 if (document.shopCart.ShipVia.options[3].selected) { shipCost = 4.29 } // Europe to 500g
 if (document.shopCart.ShipVia.options[4].selected) { shipCost = 6.69 } // Europe to 1kg
 if (document.shopCart.ShipVia.options[5].selected) { shipCost = 10.69 } // Europe to 2kg
 if (document.shopCart.ShipVia.options[6].selected) { shipCost = 29.20 } // Europe 2-10kg
 if (document.shopCart.ShipVia.options[7].selected) { shipCost = 42.48 } // Europe 10-25kg
 if (document.shopCart.ShipVia.options[8].selected) { shipCost = 0.00 } //  
 if (document.shopCart.ShipVia.options[9].selected) { shipCost = 7.23 } // ROTW to 500g
 if (document.shopCart.ShipVia.options[10].selected) { shipCost = 10.46 } // ROTW to 1kg
 if (document.shopCart.ShipVia.options[11].selected) { shipCost = 19.59 } // ROTW to 2kg
 if (document.shopCart.ShipVia.options[12].selected) { shipCost = 54.48 } // ROTW 2-10kg
 if (document.shopCart.ShipVia.options[13].selected) { shipCost = 82.95 } // ROTW 10-25kg
 if (document.shopCart.ShipVia.options[14].selected) { shipCost = 0.00 } // 
 
 if (document.shopCart.ShipVia.options[15].selected) { shipCost = 10.00 } //  
 if (document.shopCart.ShipVia.options[16].selected) { shipCost = 20.00 } //  
 if (document.shopCart.ShipVia.options[17].selected) { shipCost = 30.00 } //  
 if (document.shopCart.ShipVia.options[18].selected) { shipCost = 40.00 } //  
 if (document.shopCart.ShipVia.options[19].selected) { shipCost = 50.00 } //  
 if (document.shopCart.ShipVia.options[20].selected) { shipCost = 60.00 } //  
 if (document.shopCart.ShipVia.options[21].selected) { shipCost = 70.00 } //  
 if (document.shopCart.ShipVia.options[22].selected) { shipCost = 80.00 } //  
 if (document.shopCart.ShipVia.options[23].selected) { shipCost = 90.00 } //  
 if (document.shopCart.ShipVia.options[24].selected) { shipCost = 100.00 } //  
 if (document.shopCart.ShipVia.options[25].selected) { shipCost = 200.00 } //  

 }

// **Function to calculate the order totals. **
// **This is triggered whenever the order form **
// **info is changed by the user.**

function update() {
if (getCookieData("CPcart")) {
 var sub_total = 0;

 for (i=1; i<itemNum.length; i++) {
  eval('sub_total += parseFloat(extCst[' + i + ']);');
 }
 document.shopCart.desc.value = orddesc;

 document.shopCart.subtotal.value= fix(sub_total);
 shippingCost()
 taxes = fix(sub_total) * .04 // 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
 grand_total = sub_total + with_Shipping

// noTaxTotal = sub_total + with_Shipping
// if (document.shopCart.stateResident[0].checked) {
//   document.shopCart.cost.value = fix(noTaxTotal)
// } else {
//  document.shopCart.cost.value = fix(grand_total)
// }
 document.shopCart.cost.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("CPcart");
// }
// return validity;
}

// **Function to clear the shopping cart.**

function killCart() {
 killCookie("CPcart")
 history.go(-1)
}

