function init() {
	writeMenuItems();
	setFooter();
}
window.onresize = function() {setFooter();}


//---- this variable defines the build file for beoswer type for the left menu! ----
var buildDir = "js/";
//---------------------------------

var ie4 = false, ie5_mac = false, ie55 = false;
var nn4 = false, dom = false;
var agent = navigator.userAgent.toLowerCase();

if (document.all)
{
	var version = parseFloat(agent.substr(agent.indexOf("msie")+5, 3));
	if (agent.indexOf("mac") != -1 && version >= 5)
		ie5_mac = true;
	else
	{
		if (version >= 5) // it was 'if (version >= 5.5)' but the ie4 build breaks for 5 so...
			ie55 = true;
		else if (version >= 4)
			ie4 = true;
	}
}
//else if (document.layers)
else if (navigator.appName == "Netscape" && parseInt(navigator.appVersion) >= 4 &&
	parseInt(navigator.appVersion) < 5)
	nn4 = true;
else if (document.getElementById)
	dom = true;

var blank = new Image();
blank.src = buildDir + "blank.gif";

if (ie4)
	document.write('<script type="text/javascript" src="' + buildDir + 'ie4-build.js"></script>');
else if (ie5_mac)
	document.write('<script type="text/javascript" src="' + buildDir + 'ie5m-build.js"></script>');
else if (ie55)
	document.write('<script type="text/javascript" src="' + buildDir + 'ie55-build.js"></script>');
else if (nn4)
	document.write('<script type="text/javascript" src="' + buildDir + 'ns4-build.js"></script>');
else if (dom)
	document.write('<script type="text/javascript" src="' + buildDir + 'dom-build.js"></script>');
else{
	alert("These pages require javascript which your browser does not support.");
	//location.href = buildDir + "upgrade.html";
	document.write('<script type="text/javascript" src="' + buildDir + 'no-build.js"></script>');
}

/* END OF MENU CODE */

// Anti spam bot for email
var lhs = "sales";
var rhs = "floors2urdoor.co.uk";
function email_me(aClass) {
	document.write("<a class=\""+aClass+"\" href=\"mailto");
	document.write(":" + lhs + "@");
	document.write(rhs + "\">" + lhs + "@" + rhs + "<\/a>");
}


function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	}	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}	else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

function setFooter() {
	if (document.getElementById) {
		var windowHeight = getWindowHeight();
		if (windowHeight > 0) {

			//var treeElement = document.getElementById('tree');
			var contentElement = document.getElementById('content');
			var footerElement  = document.getElementById('footer');

			//var topFillerHeight = bannerElement.offsetHeight + topNavElement.offsetHeight + rangeElement.offsetHeight;
			//var treeHeight = treeElement.offsetHeight;
			var contentHeight = contentElement.offsetHeight; // a variable height

			// use above if you want to calculate stuff dynamically
			var topFillerHeight = 139;
			var treeHeight = 463;
			var treeFooterTop = 		topFillerHeight + treeHeight;
			var contentFooterTop = 	topFillerHeight + contentHeight;
			var footerHeight = 122; // does not register if its invisible

			var minFooterTop = windowHeight - footerHeight ;
			var footerTop;
			if (treeFooterTop < contentFooterTop) {
				footerTop = contentFooterTop;
			}else{
				footerTop = treeFooterTop;
			}

			if (footerTop < minFooterTop) {
				footerTop = minFooterTop;
			}
			footerElement.style.top = footerTop + 'px';
		}
	}
	footerElement.style.display = 'block';

	/*
	var frameWidth;
	// finally, a little snip to re-position top nav
	if (self.innerWidth) {
		//alert('aa');
		frameWidth = self.innerWidth;
		//frameHeight = self.innerHeight;
	}	else if (document.documentElement && document.documentElement.clientWidth) {
		frameWidth = document.documentElement.clientWidth;
		//frameHeight = document.documentElement.clientHeight;
	}	else if (document.body) {
		frameWidth = document.body.clientWidth;
		//frameHeight = document.body.clientHeight;
	}

	var myMenuWidth=450;
	var styleObject = getStyleObject('myMenu');

	if(styleObject) {
		if (frameWidth>1024) frameWidth=1024;
		moveObject('myMenu', frameWidth-myMenuWidth-30, 0)
  }
	*/
	return;

}

function doForm(myForm) {
	// just submit the bloody form
	document.forms[myForm].submit();
}

function calculateDetail(mode, packsize, price) {
	var anError = false;
	if (mode==1) {
		if (isNaN(parseFloat(document.myForm.rWidth.value))) {
		 	anError = true;
		}else if(isNaN(parseFloat(document.myForm.rLength.value))) {
			anError = true;
		}

		if (anError) {
			alert ("To calulate the total cost, please enter the room width and length");
			return;
		}
		var area = document.myForm.rWidth.value * document.myForm.rLength.value;
		document.myForm.area.value = roundNumber(area, 3);
		// set packs needed
		//if (area/packsize - Math.floor(area/packsize) != 0) {
		if (area/packsize % Math.floor(area/packsize) == 0) {
			var packs = Math.floor(area/packsize);
		}else{
			var packs = Math.floor(area/packsize+1);
		}
		document.myForm.quantity.value=packs;
	}else if (mode==2) {
		if (isNaN(parseFloat(document.myForm.area.value))) {
			alert ("To calulate the total cost, please enter the area of the room");
			return;
		}
		var area = roundNumber(document.myForm.area.value, 3);
		document.myForm.area.value = area;
		if (area/packsize - Math.floor(area/packsize) != 0) {
			var packs = Math.floor(area/packsize+1);
		}else{
			var packs = Math.floor(area/packsize);
		}

		document.myForm.quantity.value=packs;
	}else if (mode==3) {
		// clear other inputs, incase they dont agree
		document.myForm.rWidth.value = '';
		document.myForm.rLength.value = '';
		document.myForm.area.value = '';

		if (isNaN(parseFloat(document.myForm.quantity.value))) {
			alert ("To calulate the total cost, please enter the number of packs needed");
			return;
		}
		var packs = Math.floor(document.myForm.quantity.value);
	}


	// set sub-total
	var subtotal= price*packs;
	var target = document.getElementById("subtotal");
	target.innerHTML= "&pound;"+roundNumber(subtotal, 2);

}

// numberField: Field where the number appears.rlength: The number of decimal places to round to
function roundNumber(numberField, rlength) {
	var newnumber = Math.round(numberField*Math.pow(10,rlength))/Math.pow(10,rlength);
	return newnumber;
}

function buy() {
	if (document.myForm.quantity.value>0)
		document.myForm.submit();
	else
		alert ("Please use the buttons above to calculate the packs needed first");
	return;
}

function editBasket(myForm, anAction, id) {
	if (anAction == 'update') {
		if (!integerCheck(document.forms[myForm].quantity.value)) {
			alert ("Please enter a whole number");
			return;
		}
	}
	document.forms[myForm].action.value=anAction;
	document.forms[myForm].id.value=id;
	document.forms[myForm].submit();
}

function addAccessory(myForm) {
	if (!integerCheck(document.forms[myForm].quantity.value)) {
		alert ("Please enter a whole number");
		return;
	}
	document.forms[myForm].submit();
}


function integerCheck(myNumber) {
	if (isNaN(parseFloat(myNumber))) {
		return false;
	}else if (myNumber - Math.floor(myNumber) != 0) {
		return false;
	}
	return true;
}

function confirmPurchase() {
	if (!document.forms["Test"].agreement.checked) {
		alert ("Please check the terms and conditions box, in order to proceed.");
		return;
	}
	document.forms["Test"].submit();
}

function getMainObj(name){
  if (document.getElementById) {
  	this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
  } else if (document.all) {
		this.obj = document.all[name];
		this.style = document.all[name].style;
  } else if (document.layers) {
   	this.obj = document.layers[name];
   	this.style = document.layers[name];
  }
}


// ************************
// layer utility routines *
// ************************

function getStyleObject(objectId) {
    // cross-browser function to get an object's style object given its id
    if(document.getElementById && document.getElementById(objectId)) {
	// W3C DOM
	return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
	// MSIE 4 DOM
	return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
	// NN 4 DOM.. note: this won't find nested layers
	return document.layers[objectId];
    } else {
	return false;
    }
} // getStyleObject

function changeObjectVisibility(objectId, newVisibility) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	styleObject.visibility = newVisibility;
	return true;
    } else {
	// we couldn't find the object, so we can't change its visibility
	return false;
    }
} // changeObjectVisibility

function moveObject(objectId, newXCoordinate, newYCoordinate) {
 	// get a reference to the cross-browser style object and make sure the object exists
  var styleObject = getStyleObject(objectId);
  if(styleObject) {
		styleObject.left = newXCoordinate;
		styleObject.top = newYCoordinate;
		return true;
  } else {
		// we couldn't find the object, so we can't very well move it
		return false;
  }
} // moveObject