var items = new Array("CHIS", "CHIS10MG", "CHIS10MX", "CHIS10WG", "CHIS12WX", "CHIS18G", "CHIS18X", "CHIS8NG", "CHIS8NX", "CHISG", "GEO16TS", "GEO18WS", "GEO20SF", "GEO24WS", "GEO32LUMP", "GEO8SS", "HLDRSL", "MAG5X", "PRY30", "SAFEV", "TAPEADD", "TAPEPG", "TAPEPGG", "TAPEPGO", "TAPEPGP", "TAPEPGY", "TRO20WH", "TRO28SF", "TYV35X25", "TYV46X25", "TYV57X25", "TYV69X24", "TYV810X25", "TYVASS", "TYVBAG");
var names = new Array("Splitting chisel", "19&times;250mm splitting chisel + guard", "19&times;250mm splitting chisel", "25&times;250mm splitting chisel + guard", "25&times;250mm splitting chisel", "25&times;450mm splitting chisel + guard", "25&times;450mm splitting chisel", "12&times;200mm splitting chisel + guard", "12&times;200mm splitting chisel", "Splitting chisel with hand guard", "16oz tubular shaft hammer", "18oz wooden shaft hammer", "20oz solid forged hammer", "24oz wooden shaft hammer", "Lump hammer", "Small chipping hammer", "Hammer holder", "10&times; Magnifying hand lens", "Pry bar", "High-visibility jackets", "Tape on item", "Fluorescent tape", "Green tape (roll)", "Orange tape (roll)", "Pink tape (roll)", "Yellow tape (roll)", "Budget archaeologist&#x2019;s trowel", "Solid forged archaeologist#x2019;s trowel", "3×5in TYVEK™ specimen bag (×24)", "4×6in TYVEK™ specimen bag (×24)", "5×7in TYVEK™ specimen bag (×24)", "6x9in TYVEK specimen bag (x24)", "8×10in TYVEK™ specimen bag (×24)", "Tyvek specimen bag selection", "Tyvek specimen bags");
var prices = new Array("5.99", "5.99", "4.49", "7.99", "6.49", "11.49", "9.99", "4.49", "2.99", "0.00", "14.99", "15.99", "23.95", "16.99", "6.99", "9.99", "4.49", "2.99", "7.29", "4.99", "1.79", "3.29", "3.29", "3.29", "3.35", "3.29", "6.49", "14.69", "4.59", "5.49", "6.79", "8.79", "11.49", "9.49", "5.49");
var weights = new Array("350", "494", "371", "620", "585", "950", "920", "337", "190", "450", "821", "595", "989", "978", "1117", "429", "100", "30", "483", "160", "5", "50", "50", "50", "50", "30", "220", "600", "36", "48", "48", "72", "96", "80", "48");
var blurb = new Array("Postage to European addresses", "Postage beyond the EU", "The ultimate splitting tool", "The ultimate splitting tool", "The ultimate splitting tool", "The ultimate splitting tool", "The ultimate splitting tool", "The ultimate splitting tool", "The ultimate splitting tool", "The ultimate splitting tool", "The ultimate splitting tool", "For a safer split", "with a lightweight tubular shaft", "with an elegant wooden shaft", "One piece, robust construction", "with an elegant wooden shaft", "for a firm blow", "sprung shaft, lightly built", "keeping your hammer to hand", "for a closer look", "perfect for splitting shales", "vital safety equipment", "Applied by our master craftsmen", "A splash of colour", "For that fluorescent glow", "For that fluorescent glow", "For that fluorescent glow", "For that fluorescent glow", "Soil-be-gone", "Clear away that overlay", "Keeping your specimens safe", "Keeping your specimens safe", "Keeping your specimens safe", "Keeping your specimens safe", "Keeping your specimens safe", "For specimens of all shapes and sizes", "safe storage for specimens. Pack of 24.");
var qt = new Array("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "");
var sessQt = new Array("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""); // this variable takes note of what the server thinks is in the basket.
var root = "/";
var emptyMessage = '<i>Your basket is currently empty.</i>';
var shipTo = "UK";

var airmail = Array(
Array(10,1.24,1.64),
Array(20,1.24,1.64),
Array(40,1.24,1.64),
Array(60,1.24,1.64),
Array(80,1.24,1.64),
Array(100,1.24,1.64),
Array(120,1.36,1.87),
Array(140,1.50,2.10),
Array(160,1.63,2.33),
Array(180,1.77,2.56),
Array(200,1.90,2.80),
Array(220,2.03,3.01),
Array(240,2.15,3.22),
Array(260,2.28,3.43),
Array(280,2.39,3.65),
Array(300,2.51,3.87),
Array(320,2.62,4.09),
Array(340,2.73,4.31),
Array(360,2.84,4.53),
Array(380,2.95,4.75),
Array(400,3.06,4.97),
Array(420,3.17,5.19),
Array(440,3.28,5.41),
Array(460,3.39,5.63),
Array(480,3.50,5.85),
Array(500,3.61,6.07),
Array(520,3.71,6.27),
Array(540,3.81,6.47),
Array(560,3.91,6.67),
Array(580,4.01,6.87),
Array(600,4.11,7.07),
Array(620,4.21,7.27),
Array(640,4.31,7.47),
Array(660,4.41,7.67),
Array(680,4.51,7.87),
Array(700,4.61,8.07),
Array(720,4.71,8.27),
Array(740,4.81,8.47),
Array(760,4.91,8.67),
Array(780,5.01,8.87),
Array(800,5.11,9.07),
Array(820,5.21,9.27),
Array(840,5.31,9.47),
Array(860,5.41,9.67),
Array(880,5.51,9.87),
Array(900,5.61,10.07),
Array(920,5.71,10.27),
Array(940,5.81,10.47),
Array(960,5.91,10.67),
Array(980,6.01,10.87),
Array(1000,6.11,11.07),
Array(1020,6.21,11.27),
Array(1040,6.31,11.47),
Array(1060,6.41,11.67),
Array(1080,6.51,11.87),
Array(1100,6.61,12.07),
Array(1120,6.71,12.27),
Array(1140,6.81,12.47),
Array(1160,6.91,12.67),
Array(1180,7.01,12.87),
Array(1200,7.11,13.07),
Array(1220,7.21,13.27),
Array(1240,7.31,13.47),
Array(1260,7.41,13.67),
Array(1280,7.51,13.87),
Array(1300,7.61,14.07),
Array(1320,7.71,14.27),
Array(1340,7.81,14.47),
Array(1360,7.91,14.67),
Array(1380,8.01,14.87),
Array(1400,8.11,15.07),
Array(1420,8.21,15.27),
Array(1440,8.31,15.47),
Array(1460,8.41,15.67),
Array(1480,8.51,15.87),
Array(1500,8.61,16.07),
Array(1520,8.71,16.27),
Array(1540,8.81,16.47),
Array(1560,8.91,16.67),
Array(1580,9.01,16.87),
Array(1600,9.11,17.07),
Array(1620,9.21,17.27),
Array(1640,9.31,17.47),
Array(1660,9.41,17.67),
Array(1680,9.51,17.87),
Array(1700,9.61,18.07),
Array(1720,9.71,18.27),
Array(1740,9.81,18.47),
Array(1760,9.91,18.67),
Array(1780,10.01,18.87),
Array(1800,10.11,19.07),
Array(1820,10.21,19.27),
Array(1840,10.31,19.47),
Array(1860,10.41,19.67),
Array(1880,10.51,19.87),
Array(1900,10.61,20.07),
Array(1920,10.71,20.27),
Array(1940,10.81,20.47),
Array(1960,10.91,20.67),
Array(1980,11.01,20.87),
Array(2000,11.11,21.07)
);
  
//Define indexOf for IE
if(!Array.indexOf){
	Array.prototype.indexOf = function(obj){
		for(var i=0; i<this.length; i++){
				if(this[i]==obj){
						return i;
				}
		}
		return -1;
	}
}

function addIfEnter(e, id) {
	if (e.keyCode == 13) AddToBasket(id);
}

function AddToBasket(item) {
	var InNow = qt[items.indexOf(item)];
	var ToAdd = document.getElementById("Quantity_" + item).value;
	InNow = 1 * InNow;
	ToAdd = 1 * ToAdd;
	if (!isNaN(ToAdd)) InNow += ToAdd;
	if (InNow<0) InNow = 0;
	qt[items.indexOf(item)] = Math.round(InNow);
	if (document.getElementById("removePic_" + item)) {
		if (InNow == 0) {
			document.getElementById("removePic_" + item).src = RemoveGrey.src;
		} else {
			document.getElementById("removePic_" + item).src = RemoveBlack.src;
		}
	}
	UpdateBasket();
}

function quickAdd(item) {
	var InNow = qt[items.indexOf(item)];
	InNow = 1 * InNow;
	InNow += 1;
	qt[items.indexOf(item)] = Math.round(InNow);
	if (document.getElementById("removePic_" + item)) document.getElementById("removePic_" + item).src = RemoveBlack.src;
	UpdateBasket();
}

function Remove(item) {
	qt[items.indexOf(item)] = 0;
	if (document.getElementById("removePic_" + item)) document.getElementById("removePic_" + item).src = RemoveGrey.src;
	UpdateBasket();
}

function BuyNow(){
	UpdateBasket();
  //window.location.href = "/3ologic.php?page=checkout";
  window.location.href = "/checkout-page.html";
}

function changeShipping(zone) {
  document.getElementById("shipUK").src=root + "images/Buttons/UKwhite.png";
  document.getElementById("shipEU").src=root + "images/Buttons/EUwhite.png";
  document.getElementById("shipRoW").src=root + "images/Buttons/RoWwhite.png";
  document.getElementById("ship" + zone).src = root + "images/Buttons/" + zone + "green.png";
  shipTo = zone;
  ajax("setshipping&to=" + zone);
  UpdateBasket();
}

function UpdateBasket() {
	var ListOfContents = "<table>";
	var orderWeight = 0;
	var orderValue = 0;
  var euPostage = 0;
  var rowPostage = 0;
	var cost = Array();

	//Update session data and product boxes
	for (i = 0; i < items.length; i++) {
		//Session data
		quant = qt[i];
		id = items[i];
		cost[i] = quant * prices[i];
		orderValue += cost[i];
    orderWeight += (quant * weights[i]);
		if (quant > 0) {
			ListOfContents+= "<tr><td class=basket-item>"
				+ quant + " &times; <a href='/product-" + id + ".html' title='More about this item'>" + names[i] + "</a>"
				+ "</font></td><td class=basket-cost>£" + num2cash(quant*prices[i]) +"</td><td onclick='Remove(\"" + id + "\")' class=basket-remove title='Remove this item from your basket by clicking here'>[X]</td></tr>";
		}
		if (quant != sessQt[i]) {
			//Add item to belt
      
			ajax("additemtobelt&id=" + id + "&quantity=" + quant);
			sessQt[i] = quant;
		}
		//Product boxes
		if (document.getElementById("currentBox_" + items[i])) document.getElementById("currentBox_" + items[i]).innerHTML=(quant>0)?quant:"0";
	}

	//Postage and packing
  var packageWeight = (orderWeight * 1.02) + 20; // Estimated weight after packaging

  if (packageWeight < 2000) {
    for (i = 0; i < airmail.length; i++) {
      if (packageWeight > airmail[i][0]) {
        euPostage = airmail[i][1];
        rowPostage = airmail[i][2];
      }
    }
  } else {
    euPostage = "999";
    rowPostage = "999";
  }
  if (orderValue < 15) {
    euPostage += 2.99;
    rowPostage += 2.99;
  }
  ajax("shipCost&UK=" + ((orderValue<15)?2.99:0) + "&EU=" + euPostage + "&RoW=" + rowPostage);
  if (shipTo != "UK") {
    if (shipTo == "EU") {
        var oShipping = euPostage;
      } else if (shipTo == "RoW") {
        var oShipping = rowPostage;
      }
    if (oShipping < 999) {
      var PnP = "<span class=PnP> + £" + Math.round(oShipping*100)/100 + " P&P";
    } else {
      var PnP = Math.round(packageWeight/10 - 200)*10 + "g too heavy for Airmail. <a href='overweight-page.html' title='Suggestions for making your parcel lighter'>Options</a>";
    }
  } else {
    var PnP = "<span class=PnP title='Postage and packaging to UK addresses is free for orders over £15'>" + 
          ((orderValue<15)?" + £2.99 UK P&P<br><em>Free postage for orders over £15</em>":"Free UK P&P");
  }
  PnP += "</span><br><br style='line-height:0.5'>"
	  
  
	//Display contents to user
	if (orderValue == 0) {
	 	document.getElementById("BasketContents").innerHTML = emptyMessage;
	 	if (document.getElementById("payExpl")) document.getElementById("payExpl").style.display = 'none';
	} else {
	 	if (document.getElementById("payExpl")) document.getElementById("payExpl").style.display = 'block';
		document.getElementById("BasketContents").innerHTML = ListOfContents
			+ "<tr><td colspan=3 style='padding:8 0 0 0;' align=center><span class=basket-total title='The total amount payable. For a breakdown, go to the checkout.'>Total: £"
			+ num2cash(orderValue)
			+ "</span><br" + ">"
			+ PnP
			+ "</td></tr></table>"
			+ "<p class=buynow onclick='BuyNow();''><img src='http://img.geological-supplies.com/ProceedToPayment.PNG' alt='Proceed to Payment' title='Review your order at the checkout stage by clicking here' onmouseover='this.src=\"http://img.geological-supplies.com/ProceedToPaymentHover.PNG\";' onMouseOut='this.src=\"http://img.geological-supplies.com/ProceedToPayment.PNG\";' height=37 width=109></p>";
	}
}

function setSaving(saving) {
	ajax("setsaving&saving="+saving);
}