
function init_page() {
	<!-- If there is no crumbtrail already present put one in, either set in the page,-->
	<!--or just a link to home-->
	<!--if you want to set the crumbtrail for a particular htm page just include a lil script-->
	<!-- near the top that sets var defaultCrumbs to something useful-->

	var useTpHeader = arguments[0];

	var crumbtrail = document.getElementById('content').innerHTML;
	if (!crumbtrail) {
		document.getElementById('content').innerHTML=defaultCrumbs || "<a href='/index.html'>Home</a>";
	}
	
	<!-- Select the appropriate button -->
	var buttons = new Array("home","about","catalogue","contact","help","basket","details","builders","lowEnergy","news","links");
	for (var loop =0; loop < buttons.length; loop++) {
		if (buttons[loop] == selectedButton) {
			document.getElementById(buttons[loop] + "Link").style.backgroundPosition="0 -44px";
			document.getElementById(buttons[loop] + "Link").style.color= "#000000";
		}		
	}
	
	calculateResultsPerPage();
	setDynamicHeader(useTpHeader);

	
}

function setDynamicHeader() {
	<!-- Pick a random background images, if we're not looking at a builder -->
	var buildersCode = cookie("builderscode");
	if (arguments[0] || (!buildersCode || (buildersCode == 0))) {
		var choice = Math.round(Math.random() * 10);
		document.getElementById('topImages').style.background="url(/images_auto/top/" + choice + ".jpg) top left repeat-x";
	}
}


function popup(page,w,h) {
	
		if (page.indexOf("/cgi-bin") != 0) {
			page = "/popups/" + page + ".html";
		}
		var params = "toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no,width=" + w + ",height=" + h;
		OpenWin = window.open(page, "thing", params);
}

function reset_builders_code() {
	cookie_domain = document.domain.substr(document.domain.indexOf("."),document.domain.length);
	document.cookie="builderscode=0; path=/; domain=" + cookie_domain + ";";
}

function cookie(name) {
	
	// return the values of cookies that may be set in the current document
	var allCookies = document.cookie || "";
	
	var index = allCookies.indexOf(name + "=");
	var value;
	
	if (index > -1) {
		var start = index + name.length + 1;
		var end = allCookies.indexOf(";", start);
		if (end == -1) end = allCookies.length;
		value = allCookies.substring(start,end);
		value = unescape(value);
		
	} else {
		value="";	
	}
	
	return value;
}

function calculateResultsPerPage() {
	var resultsPerPage = 36;
	
	if (screen.width < 1600) {
		resultsPerPage = 21;
	}
	
	if (screen.width < 1280) {
		resultsPerPage = 15
	}

	if (screen.width < 1024) {
		resultsPerPage = 9
	}
	document.getElementById('catalogueLink').href = document.getElementById('catalogueLink').href + "?perpage=" + resultsPerPage;
	

	if (document.getElementById("searchPerPage")) {
		document.getElementById("searchPerPage").value = resultsPerPage;
	}
	
	// the global variable declared at the top of each page
	document.searchResultsPerPage = resultsPerPage;
	
	if (cookie('builderscode') > 0) {
		if (document.getElementById('mainLogoLink')) {	// this might not be defined if we're not on a dynamic page
			document.getElementById('mainLogoLink').href = "javascript:showCatalogue()";
		}
	}
}
	
function showCatalogue() {
	window.location="/cgi-bin/catalogue.pl?perpage=" + document.searchResultsPerPage;
}


