var myMonthYearCode = "0110";

var itemsInBasketSoFar = 0;


//alert("Sorry - Web site closed please phone the smokehouse on 01840 261785 if you would like to place an order");


function navigate(choice) {
	var url = choice.options[choice.selectedIndex].value;
	if (url) {
		top.location.href = url;
	}
}


function navigate2(pageAddress) {
	pageAddress = "a_" + pageAddress + ".html";
	top.location.href = pageAddress;
}


function navigate3(pageAddress) {
	top.location.href = pageAddress;
}


function resetSelectElements() {
	document.getElementById("manufacturer").options[0].selected = true;
}


	
	

	






function recordPage() {

		if (cookieEnabled == false) { // do nothing
		}
		else {
			savePageToCookie(thisPage);	
		}


}




function viewBasket() {
	top.location.href = "a_shopping_basket.html";
}











var postageTotalForNextPage = 0;



// tests for cookies enabled
var cookieEnabled = false;
if (typeof document.cookie == "string") {
	if (document.cookie.length == 0) {
		setCookie("joanallenctest", "hello");
		var joanallenctestRead = getCookie("joanallenctest");
		if (joanallenctestRead == "hello") {
			cookieEnabled = true;
		}
		else {
			cookieEnabled = false;
		}
	}
	else {
		cookieEnabled = true;
	}
}

function getCookieVal(offset) {
	var endstr = document.cookie.indexOf(";", offset);
	if (endstr == -1) {
		endstr = document.cookie.length;
	}
	return unescape(document.cookie.substring(offset, endstr));
}

function getCookie(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) {
			return getCookieVal(j);
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return "";
}

function setCookie(name, value) {
	var exp = new Date(); // to fix bug with Safari
	exp.setTime(exp.getTime() + 995000000);
	var expires = true;
	var path = false;
	var domain = false;
	var secure = false;

	document.cookie = name + "=" + escape (value) +
		((expires) ? "; expires=" + exp.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}

function deleteCookie(name,path,domain) {
	if (getCookie(name)) {
		document.cookie = name + "=" +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			"; expires=Thu, 01-Jan-70 00:00:01 GMT";

	}
}

function cookiesNotOnMessage() {
	alert("Your web browser is not accepting cookies which are necessary for our e-commerce system.  Please either enable cookies or order via our Sales Hotline");

}


function knockOffSomeDecPlaces(expr, decplaces) {	
	var str = "" + Math.round (eval(expr) * 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);
}	



function fillBasketDiv(extraToAdd) { // extraToAdd will be 0 or a number
	var basketTotal = getCookie("joanAllenTotal");
	if (basketTotal == "") {
		basketTotal = 0;
	}
	else {
		basketTotal = parseFloat(basketTotal);
	}
	basketTotal += parseFloat(extraToAdd);
	basketTotal = knockOffSomeDecPlaces(basketTotal, 2);
	setCookie("joanAllenTotal", basketTotal);
	var basketHTML = "";
	if ( basketTotal > 0 || thisPage == 22 ) {
		basketHTML += '<p><button onclick="viewBasket()">View Basket</button></p><p class="amount"><a href="a_shopping_basket.html"><img src="a_images\/basketlogoflash.gif" alt="Click to view your shopping basket" \/></a>&nbsp;: &pound;' + basketTotal + '</p>';
	}
	else {
		basketHTML += '<p><button onclick="location.href=\'a_howtoorder.html\'">How to order</button></p><p><img src="a_images\/basketlogo.gif" alt="*" \/>&nbsp;: &pound;0.00</p>';

	}
	document.getElementById("basket").innerHTML = basketHTML;
}


function addItemToBasketArray(itemNumber) {
	var basketArray = getCookie("joanAllenBasketItems");
	var eitmStr = "EITM";
	if (basketArray == "") { //no items yet
		eitmStr = ""; // don't want on first item
	}
	basketArrayStr = new String(basketArray);
	basketArrayStr += eitmStr;
	basketArrayStr += itemNumber;
	setCookie("joanAllenBasketItems", basketArrayStr);
}



function savePageToCookie(thePage) {
	var pageVisitedArray = new String(getCookie("joanAllenPageHistory"));
	if (pageVisitedArray == "") {
		pageVisitedArray += thePage;
	}
	else {
		if (pageVisitedArray.length > 1500) { // avoid too large cookies
			pageVisitedArray = pageVisitedArray.substr(0, 500);
		}
		var thePageShort = new String(thePage);	
		thePageShort = thePageShort.substr(0, 2);// to stop phrase and source repeating
		pageVisitedArray += "_" + thePageShort;
	}

	var lookForAnotherPrice = new String(pageVisitedArray);
	if (lookForAnotherPrice.indexOf("ad6") != -1) { // should see plus 25%)
		divertToAnotherPage = "b"
	}
	else if (lookForAnotherPrice.indexOf("ad7") != -1) { // should see 0%)
		divertToAnotherPage = "g"
	}
	setCookie("joanAllenPageHistory", pageVisitedArray);
}
 

function swopPageNoForUrl(lastPage) {
	switch(parseInt(lastPage)) {
		// no longer used - see ja if you want to resurrect

	}
}


function setServerUrls() {
	var locPagesVisited = getCookie("joanAllenPageHistory");
	var locBasketArrayRaw = getCookie("joanAllenBasketItems");
	var locBasketTotal = getCookie("joanAllenTotal");
	locBasketTotal = knockOffSomeDecPlaces(locBasketTotal, 2);

	if (locBasketTotal == "") {
		locBasketTotal = 0;
	}

	urlNil = "a_prelloydsbank.htm?tPn=" + locBasketTotal + "&server=nil&orderNo=DR" + myMonthYearCode + "N" + myOrderNo + "&basketArray=" + locBasketArrayRaw + "&postageTot=" + postageTotalForNextPage + "&pagesVisited=" + locPagesVisited;

	urlMay = "";

}



function addToBasket(locPrice, locItemNo) {
	if (cookieEnabled == false) {
		cookiesNotOnMessage();
	}
	else {
		addItemToBasketArray(locItemNo);		
		fillBasketDiv(parseFloat(locPrice)); // adds to Total etc
		alert("This item has been added to your basket. Please click on View Basket when you are ready to check out.")							
	}
}
 






function goback() {
    history.go(-1);
}




function getSearchAsArray() {
	var theResults = new Array()
	var input = unescape(location.search.substr(1))
	if (input) {
		var srchArray = input.split("&")
		var tempArray = new Array()
		for (var i = 0; i < srchArray.length; i++) {
			tempArray = srchArray[i].split("=")
			var varForKeyForArray = "'" + tempArray[0] + "'";
			theResults[varForKeyForArray] = tempArray[1]
		}
	}
	return theResults
}



/////////////////////// SHOPPING BASKET PAGE //////////////////





// ** BEGIN GLOBAL ADJUSTMENTS ** //
// Order form columns and rows specifications
// **Column titles CANNOT CONTAIN PERIODS
var columnHeads = "Qty,Description,Total, , ".split(",");
var columnWidths = "3,40,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="5">&nbsp;</td></tr>'; 
			newHTML2 += '<tr><td></td>'; 
			newHTML2 += '<td id="postageTotal">Delivery to a UK address *</td>';
			newHTML2 +=	'<td><input type="text" class="textfield" size="7" name="totalpost" /></td><td colspan="2">&nbsp;</td></tr>';

			newHTML2 += '<tr><td colspan="5">&nbsp;</td></tr>'; 
			newHTML2 += '<tr><td colspan="1"></td>'; 
			newHTML2 += '<td class="grandTotal" colspan="1">Total including VAT where applicable&nbsp;&nbsp; &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 = 8.5;
		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.86956,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.86956,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 > 8.50 ) {
				postTotal = 8.50;
			}
			if (subTotal > 39.99) {
				postTotal = 0;
				document.getElementById("postageTotal").innerHTML = "<strong>FREE DELIVERY</strong> to a Mainland UK address *";
			}

			postageTotalForNextPage = postTotal;
			subTotal += postTotal;

			// var postWithoutVAT = knockOffSomeDecPlaces(postTotal*0.86956,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_conditions.html\'">Terms</button></p>';
			}

			newHTML += '<p id="nonuk1"><strong>* UK DELIVERIES ONLY:<br />Delivery charges are priced at &pound;8.50 per delivery<br />unless you order &pound;40 or more when delivery is FREE.<br /><br />Northern Scotland, Northern Ireland, Scottish Isles, Scilly Isles, Isles of Man, Channel Islands: Delivery charges are priced at &pound;19.95 per delivery unless you order &pound;40 or more when delivery drops to &pound;9.95.</strong><br /><br />VAT does not apply to most of our products.  Where it does it is included in the price.</p>';



			newHTML += '<p><strong>INCLUDING MESSAGES:</strong> If you click on the Click to Check Out button you will see that our checkout includes a section for adding a personal message to go with the delivery.</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>';

			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_conditions.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";
		}

	// setServerUrls(); // update postage
} // 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 = /ss\#/gi
	theUrl = theUrl.replace(re,"a_smoked_salmon.html#")

	re = /gv\#/gi
	theUrl = theUrl.replace(re,"a_giftvouchers.html#")

	re = /os\#/gi
	theUrl = theUrl.replace(re,"a_oak_roasted_salmon.html#")

	re = /ot\#/gi
	theUrl = theUrl.replace(re,"a_oak_roasted_trout.html#")

	re = /om\#/gi
	theUrl = theUrl.replace(re,"a_oak_roasted_mackerel.html#")

	re = /oe\#/gi
	theUrl = theUrl.replace(re,"a_oak_roasted_eel.html#")

	re = /oc\#/gi
	theUrl = theUrl.replace(re,"a_oak_roasted_chickenbreast.html#")

	re = /od\#/gi
	theUrl = theUrl.replace(re,"a_oak_roasted_duckbreast.html#")

	re = /och\#/gi
	theUrl = theUrl.replace(re,"a_oak_smoked_cheddar.html#")

	re = /otu\#/gi
	theUrl = theUrl.replace(re,"a_oak_roasted_turkeybreast.html#")

	re = /oh\#/gi
	theUrl = theUrl.replace(re,"a_oak_roasted_ham.html#")

	re = /cb\#/gi
	theUrl = theUrl.replace(re,"a_st_marwenne_cheese.html#")


	re = /wc\#/gi
	theUrl = theUrl.replace(re,"a_wine_champagne.html#")

	re = /hm\#/gi
	theUrl = theUrl.replace(re,"a_hampers_mixed.html#")

	re = /hf\#/gi
	theUrl = theUrl.replace(re,"a_hampers_fish.html#")

	re = /hl\#/gi
	theUrl = theUrl.replace(re,"a_hampers_luxury.html#")

	re = /hk\#/gi
	theUrl = theUrl.replace(re,"a_hampers_king.html#")

	re = /sc\#/gi
	theUrl = theUrl.replace(re,"a_salmon_and_champagne.html#")

	re = /dh\#/gi
	theUrl = theUrl.replace(re,"a_denhay.html#")

	re = /cr\#/gi
	theUrl = theUrl.replace(re,"a_cranfields.html#")

	re = /df\#/gi
	theUrl = theUrl.replace(re,"a_dfc.html#")

	re = /hb\#/gi
	theUrl = theUrl.replace(re,"a_hampers_boe.html#")


	re = /ff\#/gi
	theUrl = theUrl.replace(re,"a_hampers_fizzyfish.html#")

	re = /sk\#/gi
	theUrl = theUrl.replace(re,"a_oak_smoked_kippers.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 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);
		location.href = "index.html"; // we want to go to home page for this site
		}
		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);	
				location.href = "index.html"; // we want to go to home page for this site

			}
			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() {

	var myUrl = "a_aftercheckout.html?postTot=" + postageTotalForNextPage; 
	location.href=myUrl; 

}

////////////////// END SHOPPING BASKET PAGE ////////////////////////

