
// 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 goback() {
    history.go(-1);
}





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 class="amount"><a href="a_shopping_basket.html"><img src="images\/basketlogoflash.gif" alt="Click to view your shopping basket" \/></a>&nbsp;: &pound;' + basketTotal + '</p><p><button onclick="viewBasket()">View Basket</button></p>';
	}
	else {
		basketHTML += '<p><img src="images\/basketlogo.gif" alt="*" \/>&nbsp;: &pound;0.00</p><p><button onclick="location.href=\'a_howtoorder.html\'">How to order</button></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;
	}
	setCookie("joanAllenPageHistory", pageVisitedArray);
}


function setServerUrls() {
		var locPagesVisited = getCookie("joanAllenPageHistory");
		var locBasketArrayRaw = getCookie("joanAllenBasketItems");
		var locBasketTotal = getCookie("joanAllenTotal");
		locBasketTotal = knockOffSomeDecPlaces(locBasketTotal, 2);
		if (locBasketTotal == "") {
			locBasketTotal = 0;
		}
		urlNil = "https://www.joanallen.co.uk/secure/a_secure_checkout.html?tPn=" + locBasketTotal + "&server=nil&orderNo=DR" + myMonthYearCode + "N" + myOrderNo + "&basketArray=" + locBasketArrayRaw + "&pagesVisited=" + locPagesVisited;
		urlMay = "";
}



function setServerUrlsBrochure() {
		var locPagesVisited = getCookie("joanAllenPageHistory");
		urlNil = "https://www.joanallen.co.uk/secure/a_secure_brochurerequest.html?server=nil" + "&pagesVisited=" + locPagesVisited;
		urlMay = "";

}



function setServerUrlsCredit() {
		var locPagesVisited = getCookie("joanAllenPageHistory");
		urlNil = "https://www.joanallen.co.uk/secure/a_secure_creditapplic.html?server=nil&orderNo=CA" + myMonthYearCode + "N" + myOrderNo + "&pagesVisited=" + locPagesVisited;
		urlMay = "";

}





var idExp6 = "no";
var agt2 = new String(navigator.userAgent.toLowerCase());
if (agt2.indexOf("msie 6") != -1) {
	idExp6 = "yes";
}



var winHeight = 400;
function getInsideWindowHeight() {
	isIE6CSS = (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? true : false;
	if (window.innerHeight) {
		return window.innerHeight;
	}
	else if (isIE6CSS) {
		// measure the html element's clientHeight
		return document.documentElement.clientHeight;
	}
	else if (document.body && document.body.clientHeight) {
		return document.body.clientHeight;
	}
	return 1; // 0 could lead to dividing 0 by 2.
}




var winWidth = 740;
function getInsideWindowWidth() {
	isIE6CSS = (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? true : false;
	if (window.innerWidth) {
		return window.innerWidth;
	}
	else if (isIE6CSS) {
		// measure the html element's clientWidtht
		return document.documentElement.clientWidth;
	}
	else if (document.body && document.body.clientWidth) {
		return document.body.clientWidth;
	}
	return 1; // 0 could lead to dividing 0 by 2.
}






function resetStyleSheet() {
		winWidth = getInsideWindowWidth();
		if (winWidth > 1000) { // keep it simple
			winWidth = 1000;
		}
		var extraWidAv = winWidth - 740;
		if (extraWidAv > 2) { // don't bother for less
			if (document.styleSheets[0].rules) { // IE
				document.styleSheets[0].rules[13].style.width = (550 + extraWidAv) + "px";
				document.styleSheets[0].rules[20].style.width = (260 + extraWidAv) + "px";
				if (extraWidAv > 190) {
					document.styleSheets[0].rules[4].style.marginLeft = "40px"; 
					document.styleSheets[0].rules[26].style.marginLeft = "10px"; 
				}
				document.styleSheets[0].rules[5].style.left = (620 + extraWidAv) + "px";
				document.styleSheets[0].rules[10].style.left = (593 + extraWidAv) + "px";
				document.styleSheets[0].rules[11].style.left = 255 + extraWidAv/2 + "px";

				var agt = navigator.userAgent.toLowerCase(); 
				if ((agt.indexOf("mac_")!=-1) || (agt.indexOf("macintosh")!=-1)) {
					if (agt.indexOf("msie")!=-1) {
						document.styleSheets[0].rules[24].style.left = "-292px"; 
					}
				} 

			}
			else { // MOZILLA
				extraWidAv -= 20; // doesn't allow for scrollbars	

				document.styleSheets[0].cssRules[13].style.width = (550 + extraWidAv) + "px";
				document.styleSheets[0].cssRules[20].style.width = (260 + extraWidAv) + "px";
				if (extraWidAv > 190) {
					document.styleSheets[0].cssRules[4].style.marginLeft = "40px"; 
					document.styleSheets[0].cssRules[26].style.marginLeft = "10px"; 
				}
				document.styleSheets[0].cssRules[5].style.left = (620 + extraWidAv) + "px";
				document.styleSheets[0].cssRules[10].style.left = (593 + extraWidAv) + "px";
				document.styleSheets[0].cssRules[11].style.left = 255 + extraWidAv/2 + "px";
			}
			// nb: opera has issues here - fix sometime (works OK as is though)
		}
}




		function resetStyleSheetBasket() {
			winWidth = getInsideWindowWidth();
			if (winWidth > 1024) { // keep it simple
			//	winWidth = 1024;
			}
			var extraWidAv = winWidth - 740;
			if (extraWidAv > 2) { // don't bother for less
				if (document.styleSheets[0].rules) { // IE
				    	var agt = navigator.userAgent.toLowerCase(); 
					if ((agt.indexOf("mac_")!=-1) || (agt.indexOf("macintosh")!=-1)) {
						if (agt.indexOf("msie")!=-1) {
							document.styleSheets[0].rules[1].style.fontSize = "large";
						}
					} 

					document.styleSheets[0].rules[5].style.width = (700 + extraWidAv) + "px";
					document.styleSheets[0].rules[4].style.left = (593 + extraWidAv) + "px";
				}
				else { // MOZILLA
					extraWidAv -= 20; // doesn't allow for scrollbars	

					document.styleSheets[0].cssRules[5].style.width = (700 + extraWidAv) + "px";
					document.styleSheets[0].cssRules[4].style.left = (593 + extraWidAv) + "px";
				}
			}
		}








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



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

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





function showTestReport(modelName) {
	var pageName = "a_testreports/" + modelName + ".html?src=internal";
	var newWinName = modelName;
	var newWinHeight = parseInt(winWidth*.8*.6);
	var newWinFeatures = "width=1000,height=" + newWinHeight + ",left=0,toolbar,status,resizable=no,location=yes,scrollbars=yes";
	window.open(pageName, newWinName, newWinFeatures );
}




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





function addToBasket(locPrice, locItemNo) {
	if (cookieEnabled == false) {
		cookiesNotOnMessage();
	}
	else {
		addItemToBasketArray(locItemNo);		
		fillBasketDiv(parseFloat(locPrice)); // adds to Total etc
		viewBasket();							
	}
}




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



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








