 
// ** BEGIN GLOBAL ADJUSTMENTS ** //
// Order form columns and rows specifications
// **Column titles CANNOT CONTAIN PERIODS
var columnHeads = "Qty,Description,ExVAT,VAT,Total, , ".split(",");
var columnWidths = "3,40,7,7,7".split(",");
var numberOfRows = itemsInBasketSoFar;
// data entry row math



function makeTitleRow() {
	var titleRow = '<tr>';
	for (var i = 0; i < columnHeads.length; i++) {
titleRow += '<th>' + columnHeads[i] + '</th>';
	}
	titleRow += '</tr>';
	return titleRow;
}



function makeOneRow(rowNum) {
	var oneRow = '<tr>';
	for (var i = 0; i < ( columnHeads.length - 2) ; i++) {
oneRow += '<td><input class="textfield" type="text" size="' + 
	columnWidths[i] + '" name="' + columnHeads[i] + 
	'" /></td>';
	}
	oneRow += '<td><button class="standard" onClick="reviewAnItem(' + rowNum + ')">Details</button></td>';
	oneRow += '<td><button class="standard" onClick="removeAnItem(' + rowNum + ')">Remove</button></td>';
	oneRow += '</tr>';
	oneRow += '<tr><td>&nbsp;</td><td><input class="textfield" type="text" size="40" name="DescCont1" /></td><td colspan="' + (columnHeads.length - 2) + '">&nbsp;</td></tr>';
	oneRow += '<tr><td>&nbsp;</td><td><input class="textfield" type="text" size="40" name="DescCont2" /></td><td colspan="' + (columnHeads.length - 2) + '">&nbsp;</td></tr>';
	return oneRow
}



function initialiseBasket() { 
		var newHTML2 = "";
		var basketArrayRaw = getCookie("joanAllenBasketItems");
		if (basketArrayRaw == "") { //no items yet
			newHTML2 += '<p>You have not added anything to your shopping basket yet.</p>';
			newHTML2 += '<p class="buttons"><button class="standard" onClick="keepShopping()">Continue shopping ...</button></p>';
		}
		else { // there are items
			var str = new String(basketArrayRaw);
			var re = /EITM/;
			if ( str.indexOf("EITM") == -1) {
				arrayOfItems[0] = str;
				itemsInBasketSoFar = 1;
			}
			else {
				arrayOfItems = str.split(re);
				itemsInBasketSoFar = arrayOfItems.length;
			}
			newHTML2 += '<form onsubmit="return false"><table class="checkout">';
			newHTML2 += makeTitleRow();
			// order form entry rows
			numberOfRows = itemsInBasketSoFar; //updates
			for (var i = 0; i < numberOfRows; i++) {
				newHTML2 += makeOneRow(i);
			}

			newHTML2 += '<tr><td colspan="7">&nbsp;</td></tr>'; 
			newHTML2 += '<tr><td></td>'; 
			newHTML2 += '<td id="postageTotal">Delivery to a UK address<br />(see below for Channel Islands)</td><td><input type="text" class="textfield" size="7" name="postexvat" /></td><td><input type="text" class="textfield" size="7" name="postvat" /></td>';
			newHTML2 +=	'<td><input type="text" class="textfield" size="7" name="totalpost" /></td><td colspan="2">&nbsp;</td></tr>';

			newHTML2 += '<tr><td colspan="7">&nbsp;</td></tr>'; 
			newHTML2 += '<tr><td colspan="1"></td>'; 
			newHTML2 += '<td class="grandTotal" colspan="3">Total including UK delivery and VAT &pound;</td>';
			newHTML2 +=	'<td><input type="text" class="textfield" size="7" name="total" /></td><td colspan="2">&nbsp;</td></tr>';
			newHTML2 +=	'</table></form>';
		}

		window.document.getElementById("theform").innerHTML = newHTML2;
}




var highOrLow = "high";

function populateCheckoutVariablesArray(myReferrer) {

		var subTotal = 0;
		var postTotal = 0;
		if (itemsInBasketSoFar == 0) { ; } // do nothing
		else {
			var unitPriceWithVAT = 0;
			var mainDesc = "";
			var dC1 = "";
			var dC2 = "";
			if ( itemsInBasketSoFar == 1 ) { 
				var itemNo = parseInt(arrayOfItems[0]);
				var str = new String(arDet[itemNo])
				var re = /_sep_/
				var arrayOfItemFields = str.split(re)
	
				// can't have a detector and accessories - only 1 item
				unitPriceWithVAT = parseFloat(arrayOfItemFields[0]);

				postTotal = parseFloat(arrayOfItemFields[1]);
				mainDesc = arrayOfItemFields[2];;
				dC1 = arrayOfItemFields[3];
				dC2 = arrayOfItemFields[4];
				document.forms[0].Qty.value = "1";
				document.forms[0].Description.value = mainDesc;
				document.forms[0].DescCont1.value = dC1;
				document.forms[0].DescCont2.value = dC2;
				document.forms[0].Total.value = knockOffSomeDecPlaces(unitPriceWithVAT,2);
				subTotal += unitPriceWithVAT;
				var withoutVAT = knockOffSomeDecPlaces(unitPriceWithVAT*0.85106,2);
				document.forms[0].ExVAT.value = withoutVAT;
				document.forms[0].VAT.value = knockOffSomeDecPlaces(unitPriceWithVAT - withoutVAT,2);
				document.forms[0].Qty.readOnly = true;
				document.forms[0].Description.readOnly = true;
				document.forms[0].DescCont1.readOnly = true;
				document.forms[0].DescCont2.readOnly = true;
				document.forms[0].Total.readOnly = true;
			}
			else {
				// check for explorer or quattro
				for (var i = 0; i < arrayOfItems.length; i++ ) { //an item at a time
					if (arrayOfItems[i] == "2" || arrayOfItems[i] == "268") {
						highOrLow = "low"
					}
				}

				for (var i = 0; i < arrayOfItems.length; i++ ) { //an item at a time
					var itemNo = parseInt(arrayOfItems[i]);
					var str = new String(arDet[itemNo])
					var re = /_sep_/
					var arrayOfItemFields = str.split(re)



					var costRaw = new String(arrayOfItemFields[0]);
					re = /or/;
					var newCostArray = new Array();
					var costHigh = 0;
					var costLow = 0;
					var costFinal = 0;
					if ( costRaw.indexOf("or") != -1) {
						newCostArray = costRaw.split(re);
						costHigh = newCostArray[0];
						costLow = newCostArray[1];
						if (highOrLow == "low") {
							costFinal = parseFloat(costLow)
						}
						else {
							costFinal = parseFloat(costHigh)
						}
					}
					else {
						costFinal = parseFloat(arrayOfItemFields[0])
					}


					unitPriceWithVAT = parseFloat(costFinal);
					postTotal += parseFloat(arrayOfItemFields[1]);
					mainDesc = arrayOfItemFields[2];;
					dC1 = arrayOfItemFields[3];
					dC2 = arrayOfItemFields[4];
					document.forms[0].Qty[i].value = "1";
					document.forms[0].Description[i].value = mainDesc;
					document.forms[0].DescCont1[i].value = dC1;
					document.forms[0].DescCont2[i].value = dC2;	
					document.forms[0].Total[i].value = knockOffSomeDecPlaces(unitPriceWithVAT,2);
					subTotal += unitPriceWithVAT;
					var withoutVAT = knockOffSomeDecPlaces(unitPriceWithVAT*0.85106,2);
					document.forms[0].ExVAT[i].value = withoutVAT;
					document.forms[0].VAT[i].value = knockOffSomeDecPlaces(unitPriceWithVAT - withoutVAT,2);
					document.forms[0].Qty[i].readOnly = true;
					document.forms[0].Description[i].readOnly = true;
					document.forms[0].DescCont1[i].readOnly = true;
					document.forms[0].DescCont2[i].readOnly = true;
					document.forms[0].Total[i].readOnly = true;
				}
			}
			if (postTotal > 7.50 ) {
				postTotal = 7.50;
			}
			if (subTotal > 9969) {
				postTotal = 0;
				document.getElementById("postageTotal").innerHTML = "<strong>FREE DELIVERY</strong> to a UK address<br />(see below for Channel Islands)";
			}
			subTotal += postTotal;

			var postWithoutVAT = knockOffSomeDecPlaces(postTotal*0.85106,2);
	
			document.forms[0].totalpost.value = knockOffSomeDecPlaces(postTotal,2);
			if (postTotal > 0) {
				document.forms[0].postexvat.value = postWithoutVAT;
				document.forms[0].postvat.value = knockOffSomeDecPlaces(postTotal - postWithoutVAT,2);
			}
			else {
				document.forms[0].postexvat.value = "0.00";
				document.forms[0].postvat.value = "0.00";
			}

			document.forms[0].postexvat.readOnly = true;
			document.forms[0].totalpost.readOnly = true;
			document.forms[0].postvat.readOnly = true;

			document.forms[0].total.value = knockOffSomeDecPlaces(subTotal,2);
			document.forms[0].total.readOnly = true;

			var newHTML = ""
			if ( itemsInBasketSoFar > 2 ) { 
				newHTML += '<p class="buttons"><button class="checkout" onClick="checkOut()">';
				newHTML += 'Click to Check Out</button>&nbsp;&nbsp;&nbsp;';
				newHTML += '<button class="standard" onClick="keepShopping()">';
				newHTML += 'Keep Shopping</button>&nbsp;&nbsp;&nbsp;<button class="standard" onClick="window.print()">Print</button>&nbsp;&nbsp;&nbsp;<button class="standard" onClick="location.href=\'a_terms.html\'">Terms</button></p>';
			}

			newHTML += '<p id="nonuk1"><strong>NON-UK AND CHANNEL ISLAND DELIVERIES:</strong> <a href="javascript:void showNonUK()">Please click here for details</a></p>';
			newHTML += '<p id="nonuk2"><strong>POSTAGE:</strong> We charge &pound;20 for deliveries to the Channel Islands (&pound;15 accessories only). For Southern Ireland we will add &pound;25 to the total above (&pound;15 accessories only). For deliveries outside the UK but within the EU we will add &pound;35 to the total above (&pound;15 accessories only). For deliveries outside the EU we will add &pound;60 to the total above.</p>';
			newHTML += '<p id="nonuk3"><strong>VAT: </strong>Value Added Tax will be deducted from the total above only in the case of :<br /> Orders delivered outside the countries of the EU (European Union), and also paid for from outside the EU <strong>or</strong> orders from non-UK EU businesses giving their VAT Registration numbers, delivered within EU but outside the UK. Our VAT Registration : GB 206 2592 81</p>';	
			newHTML += '<p id="nonuk4"><strong>CURRENCY: </strong>To convert from pounds sterling to your currency please use the converter at <a href="javascript:void showCurrency()">www.xe.com/ucc/</a></p>';	


			newHTML += '<p><strong>PRIVACY &amp; SECURITY:</strong> Our checkout system is completely secure and uses Secure Sockets Layer (SSLv3) to encrypt data travelling over the Internet. If you would like to read about how we protect your privacy rights please <a href="a_privacy.html">click here</a></p>';

			newHTML += '<ul><li><strong>Pay Online:</strong> Click on the Click to Check Out button to pay online by credit or debit card.</li><li><strong>Apply for credit: </strong> <a href="a_interest_free_credit.html">Click here to apply</a> for our interest free credit facility.</li><li><strong>Bank transfer:</strong> Please <a href="a_howtoorder.html">click here</a> for details</li><li><strong>Pay by Cheque:</strong> If you would like to pay by Sterling cheque please print out this page and post it to us along with your cheque and delivery address. </li><li><strong>Pay by Credit/debit card offline:</strong> If you would like to fax or phone through your credit card details please print out this page for reference.</li></ul>';
			document.getElementById("bottombit").innerHTML = newHTML;

			newHTML = '<p class="buttons"><button class="checkout" onClick="checkOut()">';
			newHTML += 'Click to Check Out</button>&nbsp;&nbsp;&nbsp;';
			newHTML += '<button class="standard" onClick="keepShopping()">';
			newHTML += 'Keep Shopping</button>&nbsp;&nbsp;&nbsp;<button class="standard" onClick="window.print()">Print</button>&nbsp;&nbsp;&nbsp;<button class="standard" onClick="location.href=\'a_terms.html\'">Terms</button></p>';
			
			if (myReferrer != "removeAnItem") { //causes crashing 5.5
				document.getElementById("topbit").innerHTML = newHTML;
			}
		}
		document.getElementById("baskettitle").innerHTML = "Shopping Basket";
		setCookie("joanAllenTotal", subTotal);
		if (myReferrer == "removeAnItem") { //only way to clear topbit div without crashing 5.5
			location.href="a_shopping_basket.html";
		}


} // END OF populateCheckoutVariablesArray





function reviewAnItem(rowNumber) {
	var itemNumber2 = arrayOfItems[rowNumber]
	var str = new String(arDet[parseInt(itemNumber2)])
	var re = /_sep_/
	var arrayOfItemFields = str.split(re)
	var theUrl = new String(arrayOfItemFields[5]);
	re = /bh\#/gi
	theUrl = theUrl.replace(re,"a_bounty_hunter_metal_detectors_uk.html#")
	re = /qd\#/gi
	theUrl = theUrl.replace(re,"a_moreinfo_quickdraw_uk.html#")
	re = /ma\#/gi
	theUrl = theUrl.replace(re,"a_moreinfo_advantage_uk.html#")
	re = /qmp\#/gi
	theUrl = theUrl.replace(re,"a_moreinfo_quattro_uk.html#")
	re = /dfx\#/gi
	theUrl = theUrl.replace(re,"a_moreinfo_dfx_uk.html#")
	re = /f1236\#/gi
	theUrl = theUrl.replace(re,"a_moreinfo_1236_uk.html#")
	re = /fid\#/gi
	theUrl = theUrl.replace(re,"a_moreinfo_idexcel_uk.html#")
	re = /v20\#/gi
	theUrl = theUrl.replace(re,"a_moreinfo_vk20_uk.html#")
	re = /v30\#/gi
	theUrl = theUrl.replace(re,"a_moreinfo_vk30_uk.html#")
	re = /v40\#/gi
	theUrl = theUrl.replace(re,"a_moreinfo_vk40_uk.html#")
	re = /mex\#/gi
	theUrl = theUrl.replace(re,"a_moreinfo_explorer_uk.html#")
	re = /met\#/gi
	theUrl = theUrl.replace(re,"a_moreinfo_etrac_uk.html#")
	re = /ft\#/gi
	theUrl = theUrl.replace(re,"a_moreinfo_fasttracker_uk.html#")
	re = /tj\#/gi
	theUrl = theUrl.replace(re,"a_moreinfo_junior_uk.html#")
	re = /xt7\#/gi
	theUrl = theUrl.replace(re,"a_moreinfo_xterra70_uk.html#")
	re = /xt5\#/gi
	theUrl = theUrl.replace(re,"a_moreinfo_xterra50_uk.html#")
	re = /xt3\#/gi
	theUrl = theUrl.replace(re,"a_moreinfo_xterra30_uk.html#")
	re = /tf\#/gi
	theUrl = theUrl.replace(re,"a_moreinfo_tracker4_uk.html#")
	re = /ls\#/gi
	theUrl = theUrl.replace(re,"a_moreinfo_quicksilver_uk.html#")
	re = /p2\#/gi
	theUrl = theUrl.replace(re,"a_moreinfo_prism2_uk.html#")
	re = /p4\#/gi
	theUrl = theUrl.replace(re,"a_moreinfo_prism4_uk.html#")
	re = /p5\#/gi
	theUrl = theUrl.replace(re,"a_moreinfo_prism5_uk.html#")
	re = /sgt\#/gi
	theUrl = theUrl.replace(re,"a_moreinfo_sovereign_uk.html#")
	re = /r1\#/gi
	theUrl = theUrl.replace(re,"a_moreinfo_r1_uk.html#")
	re = /wh\#/gi
	theUrl = theUrl.replace(re,"a_whites_metal_detectors_uk.html#")
	re = /dp\#/gi
	theUrl = theUrl.replace(re,"a_detectorpro_metal_detectors_uk.html#")
	re = /fs\#/gi
	theUrl = theUrl.replace(re,"a_fisher_metal_detectors_uk.html#")
	re = /ga\#/gi
	theUrl = theUrl.replace(re,"a_garrett_metal_detector_suppliers_uk.html#")
	re = /la\#/gi
	theUrl = theUrl.replace(re,"a_laser_metal_detector_manufacturers_uk.html#")
	re = /mi\#/gi
	theUrl = theUrl.replace(re,"a_minelab_metal_detectors_uk.html#")
	re = /te\#/gi
	theUrl = theUrl.replace(re,"a_tesoro_metal_detectors_uk.html#")
	re = /tr\#/gi
	theUrl = theUrl.replace(re,"a_troy_metal_detector_uk.html#")
	re = /vk\#/gi
	theUrl = theUrl.replace(re,"a_viking_metal_detectors_uk.html#")
	re = /ac\#/gi
	theUrl = theUrl.replace(re,"a_metal_detector_accessories.html#")
	re = /db\#/gi
	theUrl = theUrl.replace(re,"a_metal_detectors_books_uk.html#")
	re = /ud\#/gi
	theUrl = theUrl.replace(re,"a_used_metal_detectors.html#")
	re = /sr\#/gi
	theUrl = theUrl.replace(re,"a_minelab_accessories_sunray.html#")
	re = /hhs\#/gi
	theUrl = theUrl.replace(re,"a_hand_held_security_detectors_uk.html#")
	re = /exc\#/gi
	theUrl = theUrl.replace(re,"a_acc_minelab_explorer.html#")
	re = /qxc\#/gi
	theUrl = theUrl.replace(re,"a_acc_minelab_quattro.html#")
	re = /de\#/gi
	theUrl = theUrl.replace(re,"a_detech_metal_detectors_uk.html#")
	re = /tk\#/gi
	theUrl = theUrl.replace(re,"a_teknetics_detectors_uk.html#")
	re = /cs\#/gi
	theUrl = theUrl.replace(re,"a_moreinfo_coinshooter_uk.html#")

	location.href = theUrl;
	
}






function removeAnItem(rowNumber) {
	itemsInBasketSoFar -= 1;
	var str = "";
	if ( itemsInBasketSoFar == 0 ) {
		document.getElementById("bottombit").innerHTML = "";
		str = "";
	}
	else { 
		str = ""; 
		for (var i = 0; i < arrayOfItems.length; i++) {
			if ( i != rowNumber ) { // i.e. skip the one we want to remove
				str += arrayOfItems[i];
				if ( rowNumber == (arrayOfItems.length - 1) && i == (arrayOfItems.length - 2) ) { ; } // if last row is being removed and this is row before last
				else if ( i != (arrayOfItems.length - 1) && arrayOfItems.length != 2 ) { str += "EITM"; } // don't append eitm to last item	
			}
		}
	}
	setCookie("joanAllenBasketItems", str);
	initialiseBasket();
	populateCheckoutVariablesArray('removeAnItem');
//	location.href="a_shopping_basket.html";
}




function swopPageNoForUrl(lastPage) {
	switch(parseInt(lastPage)) {
		case 1: // now 93 - leave here for now
			return "index.html";
		case 2:
			return "a_minelab_metal_detectors_uk.html";
		case 3:
			return "a_bounty_hunter_metal_detectors_uk.html";
		case 4:
			return "a_c_scope_metal_detector_suppliers_uk.html";
		case 5:
			return "a_detectorpro_metal_detectors_uk.html";
		case 6:
			return "a_tesoro_metal_detectors_uk.html";
		case 7:
			return "a_troy_metal_detector_uk.html";
		case 8:
			return "a_viking_metal_detectors_uk.html";
		case 9:
			return "a_whites_metal_detectors_uk.html";
		case 10:
			return "a_fisher_metal_detectors_uk.html";
		case 11:
			return "a_garrett_metal_detector_suppliers_uk.html";
		case 12:
			return "a_laser_metal_detector_manufacturers_uk.html";
		case 13:
			return "index.html";
		case 14:
			return "a_metal_detector_prices_byprice.html";
		case 15:
			return "a_metal_detector_prices_start200.html";
		case 16:
			return "a_metal_detector_prices_start400.html";
		case 17:
			return "a_metal_detector_prices_start600.html";
		case 18:
			return "a_metal_detector_prices_start800.html";
		case 19:
			return "a_metal_detector_accessories.html";
		case 20:
			return "a_metal_detectors_books_uk.html";
		case 21:
			return "a_used_metal_detectors.html";
		case 22:
			return "a_interest_free_credit.html";
		case 23:
			return "a_metal_detector_wizard.html";
		case 24:
			return "a_hand_held_security_detectors_uk.html";
		case 25:
			return "a_acc_minelab_explorer.html";
		case 26:
			return "a_acc_minelab_quattro.html";

		// 27-51 used for moreinfo_pages

		case 27:
			return "a_moreinfo_lonestar_uk.html";
		case 28:
			return "a_moreinfo_vk30_uk.html";
		case 29:
			return "a_moreinfo_1236_uk.html";
		case 30:
			return "a_moreinfo_x3_uk.html";
		case 31:
			return "a_moreinfo_quattro_uk.html";
		case 32:
			return "a_moreinfo_vk20_uk.html";
		case 33:
			return "a_moreinfo_prism2_uk.html";
		case 34:
			return "a_moreinfo_sharpshooter_uk.html";
		case 35:
			return "a_moreinfo_vk40_uk.html";
		case 36:
			return "a_moreinfo_prism4_uk.html";
		case 37:
			return "a_moreinfo_r1_uk.html";
		case 38:
			return "a_moreinfo_sovereign_uk.html";
		case 39:
			return "a_moreinfo_cz3d_uk.html";
		case 40:
			return "a_moreinfo_cz70pro_uk.html";
		case 41:
			return "a_moreinfo_fasttracker_uk.html";
		case 42:
			return "a_moreinfo_explorer_uk.html";
		case 43:
			return "a_moreinfo_advantage_uk.html";
		case 44:
			return "a_moreinfo_dfx_uk.html";
		case 45:
			return "a_moreinfo_excalibur_uk.html";
		case 46:
			return "a_moreinfo_quickdraw_uk.html";
		case 47:
			return "a_moreinfo_tracker4_uk.html";
		case 48:
			return "a_moreinfo_1270_uk.html";
		case 49:
			return "a_moreinfo_idexcel_uk.html";
		case 50:
			return "a_moreinfo_prism3_uk.html";
		case 51:
			return "a_moreinfo_prism5_uk.html";


		// 90+ are used for landing page tracking - they will default to index.html
		default:
			return "index.html";
	}
}




function keepShopping() {
	var arrayOfPagesVisited = new Array();
	var pageVisitedArray = getCookie("joanAllenPageHistory");
	if (pageVisitedArray == "") { //just in case 
		location.href = "index.html";
	}
	else {
		var str = new String(pageVisitedArray);
		var re = /_/;
		if ( str.indexOf("_") == -1) {
			location.href = swopPageNoForUrl(str);
		}
		else {
			arrayOfPagesVisited = str.split(re);
			var lastPageVisited = arrayOfPagesVisited[arrayOfPagesVisited.length - 1]
			if (parseInt(lastPageVisited) > 0 && parseInt(lastPageVisited) < 200) { // pages that sell must be less than 200
				location.href = swopPageNoForUrl(lastPageVisited);	
			}
			else { // non selling page
				location.href = "index.html";
			}
		}
	}
}

function showNonUK() {
	document.getElementById("nonuk1").style.display = "none";
 	document.getElementById("nonuk2").style.display = "block";
 	document.getElementById("nonuk2").style.backgroundColor = "yellow";
 	document.getElementById("nonuk3").style.display = "block";
 	document.getElementById("nonuk3").style.backgroundColor = "yellow";
 	document.getElementById("nonuk4").style.display = "block";
 	document.getElementById("nonuk4").style.backgroundColor = "yellow";
}

function checkOut() {
	location.href="a_aftercheckout.html";

}