// 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 bgcolor="FFFFFF" border="0" width="100%">'
//  ordFrm += '    <tr>'
//  ordFrm += '      <td width="100%" colspan="2"><p align=center><img src="../jcart/ChineseGallery_logo_135_t.gif" '
//  ordFrm += '      alt="Toyloft Ready To Run R/C Tanks" WIDTH="300" HEIGHT="109"></td>'
//  ordFrm += '    </tr>'
//  ordFrm += '    <tr>'
//  ordFrm += '      <td width="50%"><p align="left"><font face="Arial" size="4" '
//  ordFrm += '        color="#FF6600">Shopping Cart/Checkout</font></td>'
//  ordFrm += '      <td width="50%"><p align="right">'
//  ordFrm += '      <form><input type="submit" value=" Continue Shopping " onClick="history.go(-1)"></form></td>'
//  ordFrm += '    </tr>'
  ordFrm += '  </table>'
  ordFrm += '<form name="shopCart" method="POST" action="https://select.worldpay.com/wcc/purchase" onSubmit="return validate_form()">'
  ordFrm += '<table border=1 align=center bgcolor="#FFFFCC" width="100%">'
  ordFrm += '<tr bgcolor="#FFFFCC">'
  ordFrm += '  <td align="center"><strong>Item</strong></td>'
  ordFrm += '  <td><strong>Item Description</strong></td>'
  ordFrm += '  <td align="center"><strong>Item Price</strong></td>'
  ordFrm += '  <td align="center"><strong>Order Qty</strong></td>'
  ordFrm += '  <td align="center"><strong>Total Cost</strong></td>'
  ordFrm += '  <td align="center"><strong>Click to<br>Remove</strong></td>'
  ordFrm += '</tr>'

  for (i = 1; i <= counter; i++) {
    extCst[i]=eval(itemCost[i]*itemQt[i])
    ordFrm += '<tr>'
    ordFrm += '<td width="8%" align="center">' + itemNum[i] + '</td>'
    ordFrm += '<td width="30%">' + itemDescr[i] + '</td>'
    ordFrm += '<td width="7%" align="center">£' + itemCost[i] + '</td>'
    ordFrm += '<td width="5%" align="center">' + itemQt[i] + '</td>'
    ordFrm += '<td width="7%" align="center">£' + fix(extCst[i]) + '</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="254158">'
  ordFrm += '<input type="hidden" name="cartId" value="Chinese Gallery">'
  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="#000033"></font></td></tr></table>'

  ordFrm += '<table width="100%">'
	
  ordFrm += '  <tr>'
  ordFrm += '    <td width="100%"><p align="right"><br> <align="right">'
  ordFrm += '    <input type="reset" value="Empty Shopping Cart" onClick="killCart()"></td>'
  ordFrm += '  </tr>'
  ordFrm += '    <tr>'
//  ordFrm += '      <td width="50%"><p align="left"><font face="Arial" size="4" '
//  ordFrm += '        color="#FF6600"><strong>Checkout</strong></font></td>'
//  ordFrm += '      <td width="100%"><p align="right">'
//  ordFrm += '      <form><input type="submit" value="  Continue Shopping  " onClick="history.go(-1)"></form></td>'
  ordFrm += '    </tr>'
  ordFrm += '</table>'

  ordFrm += ' <p align="left"><font face="Arial, Helvetica, sans-serif" size="2" color="#000033"> When '
  ordFrm += ' you have finished shopping, click the "Proceed To WorldPay Secure Payment" button to pay online. There will be options to pay by USD, Euros or GBP'
 
  ordFrm += '  <table border="0" width="100%">'
  ordFrm += '    <tr>'
  ordFrm += '      <td width="29%"></td>'
  ordFrm += '      <td width="71%"></td>'
  ordFrm += '    </tr>'
  ordFrm += '    <tr>'
//  ordFrm += '      <td width="29%"><img src="paymentcards.gif" alt="Click to pay via Secure WorldPay Payment Gateway"'
//  ordFrm += '      WIDTH="276" HEIGHT="39"></td>'
  ordFrm += '        <td width="71%"><form><input type="submit" value="Proceed To WorldPay Secure Payment"></form></td>'
  ordFrm += '    </tr>'
  ordFrm += '  </table>'  
//  ordFrm += ' <p>&nbsp;</p> </form>'

  ordFrm += '<br><p align="left"><font face="Arial" size="3" color="#FF6600">Other Payment Methods </font><br><br>'

  ordFrm += ' <p align="left"><font face="Arial, Helvetica, sans-serif" size="2" color="#000033">If'
  ordFrm += ' you would like us to process your credit card on your behalf without you having to go through the online purchase yourself'
  
  ordFrm += ', you can print out this completed page;'
  ordFrm += ' add the following card details; then fax or post'
  ordFrm += ' it to us.<br><br><br>'
  ordFrm += '   Card Issuer: ____________________ (i.e. Visa, Mastercard or Switch)<br><br>'
  ordFrm += '   Card No:     ____________________ <br><br>'
  ordFrm += '   Expiry Date: ____________________ <br><br>'
   ordFrm += '   Security code: ____________________ <br><br>'
  ordFrm += '   Issue No:    ____________________ (If Switch) <br><br>'
  ordFrm += '   Name:        ____________________ (As it appears on the card) <br><br><br>'
  ordFrm += '   Signature:   ____________________ '
  ordFrm += ' </font></p>'

ordFrm += '<br><p align="left"><font face="Arial" size="3" color="#FF6600">Delivery Details </font><br><br>'
ordFrm += '   Name:        ____________________  <br><br><br>'
ordFrm += '  Delivery address:    ____________________  <br><br><br>'
ordFrm += ' Telehpone number:     ____________________  <br><br><br>'
ordFrm += ' Email address:        ____________________  <br><br><br>'


  ordFrm += ' <br><p align="left"><font face="Arial, Helvetica, sans-serif" size="2" color="#000033">You '
  ordFrm += ' may also pay by cheque, postal order or international money order in UK'
  ordFrm += ' Pound Sterling (for customers outside UK) drawn on a UK bank. Print out this completed page,'     
  ordFrm += ' then post it to us along with your cheque/postal order/international money. We would despatch the goods after payment has been cleared'      
  ordFrm += ' for the total amount. <br><br>'        
  ordFrm += ' Click the "Empty Shopping Cart" button after you have finished printing.'
  ordFrm += ' Thank you for shopping with ChineseGallery.</font></p>'

  ordFrm += ' <font size="4"><font face="Arial, Helvetica, sans-serif"><font size="2" color="#000033">ChineseGallery<br>'
          ordFrm += ' PO Box 258<br>'
          ordFrm += ' Reigate, Surrey RH2 8YN<br>'
          ordFrm += ' United Kingdom<br>'
          ordFrm += ' Tel/Fax: 01737 270622 (from UK)   </font></font></font><br><br>'

// ordFrm += ' <div align="center"><font size="1" color="#000033">Copyright @ ChineseGallery, 2002.' 
//   ordFrm += ' All Rights Reserved.</font></div>'


  ordFrm += ' <p>&nbsp;</p> </form>'  
          
//  ordFrm += ' <font face="arial,helvetica" size="-2"><a href="terms.html">'
//  ordFrm += ' <p align="center">Terms &amp; Conditions</a>&nbsp;&nbsp;Copyright©1998-2001,'
//  ordFrm += ' prettyfans.co.uk. All rights reserved</font></p>'

  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 = 3;
 options = new Array(leng);
 options[0] = "UK Delivery";
 options[1] = "Europe Delivery";
 options[2] = "Rest Of The World";

 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 - 2 // variable to increment charges
multiplier = 1 // additional charge per item after first.
 if (document.shopCart.ShipVia.options[0].selected) {
   shipCost = 3 + .95 }  // 3.10 for UK delivery
 if (document.shopCart.ShipVia.options[1].selected) { 
   shipCost = 6 + .95 }  // 15.25 for Europe delivery
 if (document.shopCart.ShipVia.options[2].selected) { 
   shipCost = 13 + .95 }  // 18.60 for ROTW delivery
 }

// **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)
}

