function getChildElementsByTagName( element, tagName ) {
	var children = new Array();

	for(var i=0;i<element.childNodes.length;i++) {
		if( element.childNodes[i].nodeType == 1 && element.childNodes[i].nodeName.toLowerCase() == tagName ) {
			children.push( element.childNodes[i] );
		}
	}
	
	return children;
}

Event.observe( window, "load", init );

var initFunctions = new Array();
var focusField = null;

function init() {
	// This solves the cellspacing problem in IE5 Mac
	var tables = document.getElementsByTagName("table");
	if( navigator.appName.indexOf( "mac" ) ) for(i=0;i<tables.length;i++) tables[i].cellSpacing = "0";
	
	// This solves the lack of abbr tag support in IE
	if( document.all ) {
		var oldBodyText, reg, newBodyText, i, j, liElements, node;
		var abbrevs = document.getElementsByTagName("abbr");

		if( abbrevs.length > 0 ) {
			oldBodyText = document.body.innerHTML;
		    reg = /<ABBR([^>]*)>([^<]*)<\/ABBR>/g;
		    newBodyText = oldBodyText.replace(reg, '<abbr $1><span class=\"abbr\" $1>$2</span></abbr>');
		    //document.body.innerHTML = newBodyText;
		}
		
		// This does the list navigation filters because IE does not support the strictly CSS mehtod
		var menus = document.getElementsByClassName( "dynMenu" );

		for(i = 0;i <menus.length;i++) {
			liElements = getChildElementsByTagName( menus[i], "li" );
			for(j = 0;j < liElements.length;j++) {
				node = liElements[j];
				node.onmouseover = function() { Element.addClassName(this, "hover"); }
				node.onmouseout = function() { Element.removeClassName(this, "hover"); }
			}
		}
	}
	
	var popupLinks = document.getElementsByClassName( "popup-link" );
	for(i=0;i<popupLinks.length;i++) {
		Event.observe( popupLinks[i], "click", openPopup );
	}
	if($("filter-manf-link")) Event.observe( "filter-manf-link", "click", killEvent );
	if($("filter-range-link")) Event.observe( "filter-range-link", "click", killEvent );
	if($("filter-feature-link")) Event.observe( "filter-feature-link", "click", killEvent );
	
	if($("navbox-topcategories-link")) Event.observe( "navbox-topcategories-link", "click", showHideCats );
	
	for(i=0;i<initFunctions.length;i++) initFunctions[i]();

	if( $("page-wrapper") && !Element.hasClassName( "page-wrapper", "nosidenavs" ) ) {
		$("main-wrapper").style.height = Element.getHeight("page-wrapper").toString() + "px";
	}
	
	if( focusField != null ) Field.focus(focusField);
}

function doAddClass( e ) {
	var myElement = Event.findElement( e, 'li' );
	Element.addClassName( myElement, "hover" );
}

function doRemoveClass( e ) {
	var myElement = Event.findElement( e, 'li' );
	Element.removeClassName( myElement, "hover" );
}

function killEvent(e){
	Event.stop(e);
}

function showHideCats(e) {
	var categoryList = $("navbox-topcategories-list");
	var categoryBox = $("navbox-topcategories");
	
	if( Element.hasClassName( categoryBox, "navlink" ) ) {
		Element.show( categoryList );
		Element.removeClassName( categoryBox, "navlink" );
		Element.addClassName( categoryBox, "navbox" );
	} else {
		Element.hide( categoryList );
		Element.removeClassName( categoryBox, "navbox" );
		Element.addClassName( categoryBox, "navlink" );
	}
	
	Event.stop(e);
}

// Formats a float as a string (e.g. ?1,250.00 or -?0.31)
function formatCurrency(price) {
	var result = "";

	// Write price to temp variable
	var tmpPrice = price;
	// Get absolute value of price to format
	price = Math.abs( price );
	
	// Get the decimals from the price and add a '0' in front if needed
	var decimals = ( Math.round( ( price - Math.floor( price ) ) * 100 ) ).toString();
	if (decimals >= 100) {
		decimals = "00";
		price = price + 1;
	}
	if( decimals.length == 1 ) decimals = "0" + decimals;

	// Get the thousands from the price
	var thousands = Math.floor( price / 1000 );
	
	// If we have any add them to the formatted string with a coma
	if( thousands > 0 ) {
		price -= thousands * 1000;
		result = thousands.toString() + ",";
		units = Math.floor( price );
		if( units < 10 ) result = result + "00" + units.toString();
		else if( units < 100 ) result = result + "0" + units.toString();
		else result = result + units.toString();
	} else result = Math.floor( price ).toString();
	
	// Add the rest of the price to the string
	result = result + "." + decimals;
	
	// Place a pound sign in front
	result = "&pound;" + result;
	// Add a minus sign if price is negative
	if( tmpPrice < 0 ) result = "-" + result;
	
	return result;
}

function copyDetails(form) {
	form.dFullName.value = form.firstName.value + ' ' + form.lastName.value; 
	form.dAddressLine1.value = form.bAddressLine1.value;
	form.dAddressLine2.value = form.bAddressLine2.value;
	form.dCity.value = form.bCity.value;
	form.dCounty.value = form.bCounty.value;
	form.dPostcode.value = form.bPostcode.value;
}

var popupParams = new Array();
popupParams["verisign-popup"] = "location=yes,status=yes,resizable=yes,scrollbars=yes,width=560,height=500";
popupParams["verisign-popup-2"] = "location=yes,status=yes,resizable=yes,scrollbars=yes,width=560,height=500";

function openPopup(e) {
	var link = Event.findElement( e, "a" );
	window.open( link.getAttribute( "href" ), "popup", popupParams[link.id] );
	Event.stop(e);
}