/*
'####################################################################################
'# © Copyright 2008 Netfactors Limited (www.netfactors.co.uk). All rights reserved. #
'#----------------------------------------------------------------------------------#
'# This library of routines has been developed by Netfactors Limited and is         #
'# partially customised for use on the CADology website (www.CADology.com) and      #
'# licensed for free use by CADology Limited for a time indefinite period.          #
'# Copyright of the library and any routines therein remains the property           #
'# Netfactors Limited. In the event of any amendments by third parties, this        #
'# copyright notice must be kept intact, with comments added to indicate such       #
'# amendments.                                                                      #
'####################################################################################
*/

addEvent(window, 'load', textAreasInitialise);
var preloadImageList = new Array('/images/pagebg.gif','/images/headerbg.png','/images/infosectiontopleftbg.gif','/images/infosectiontopbg.gif','/images/infosectionleftbg.gif','/images/infosectionbg.gif','/images/infosectionbottomleftbg.gif','/images/infosectionbottombg.gif','/images/tab0bg.gif','/images/tab1bg.gif','/images/tababove1bg.gif','/images/tabbelow1bg.gif','/images/tababovefirst0.gif','/images/tababovefirst1.gif','/images/tabbelowlast0.gif','/images/tabbelowlast1.gif');
var preloadImageObjects = new Array();
var popupMenuList = new Array();
var popupMenuStatus = new Array();
var popupMenuLocks = new Array();
var popupMenuTimers = new Array();
var popupMenuButtonStyles = new Array();
var productTabs = new Array();
var productTabFreeze = false;
var currentSectionID = '';
var currentTabID = '';
var currentAboveTabID = '';
var currentBelowTabID = '';
var agentCompatible = false;
if (document.getElementById && document.createTextNode) {
	agentCompatible = true;
}
for (var i=0; i<preloadImageList.length; i++) {
	preloadImageObjects[i] = new Image();
	preloadImageObjects[i].src = preloadImageList[i]
}

function initialisePopupMenus() {
	for (var i=0; i<popupMenuList.length; i++) {
		popupMenuStatus[i] = false;
		popupMenuLocks[i] = new Array(false,false);
		popupMenuTimers[i] = new Array(false,false,false);
		popupMenuButtonStyles[i] = document.getElementById(popupMenuList[i] + 'button').className;
	}
}

function setPopupMenuLock(menuName,lockNumber,lockState) {
	if (lockState) {
		for (var i=0; i<popupMenuList.length; i++) {
			if (popupMenuList[i] == menuName) {
				popupMenuLocks[i][lockNumber] = true;
				if (popupMenuTimers[i][lockNumber] != false) {
					clearTimeout(popupMenuTimers[i][lockNumber]);
					popupMenuTimers[i][lockNumber] = false;
				}
				break;
			}
		}
	} else {
		for (var i=0; i<popupMenuList.length; i++) {
			if (popupMenuList[i] == menuName) {
				popupMenuLocks[i][lockNumber] = false;
				popupMenuTimers[i][lockNumber] = setTimeout('hidePopupMenu("' + menuName + '");',500);
				break;
			}
		}
	}
}

function showPopupMenu(menuName) {
	hidePopupMenus();
	var menu = document.getElementById(menuName);
	var menuButton = document.getElementById(menuName + 'button');
	if (agentCompatible && menu) {
		for (var i=0; i<popupMenuList.length; i++) {
			if (popupMenuList[i] == menuName) {
				if (popupMenuStatus[i] == false) {
					menu.style.display = 'block';
					popupMenuStatus[i] = true;
					if (menuButton) {
						menuButton.className = 'menuactive';
					}
				}
				break;
			}
		}
	}
}

function hidePopupMenus() {
	if (agentCompatible) {
		for (var i=0; i<popupMenuList.length; i++) {
			if (document.getElementById(popupMenuList[i])) {
				document.getElementById(popupMenuList[i]).style.display = 'none';
			}
			popupMenuStatus[i] = false;
			clearTimeout(popupMenuTimers[i][2]);
			if (document.getElementById(popupMenuList[i] + 'button')) {
				document.getElementById(popupMenuList[i] + 'button').className = popupMenuButtonStyles[i];
			}
		}
	}
}

function hidePopupMenu(menuName) {
	var menu = document.getElementById(menuName);
	var menuButton = document.getElementById(menuName + 'button');
	if (agentCompatible && menu) {
		for (var i=0; i<popupMenuList.length; i++) {
			if (popupMenuList[i] == menuName) {
				if ((popupMenuLocks[i][0] == false) && (popupMenuLocks[i][1] == false)) {
					if (popupMenuStatus[i] == true) {
						menu.style.display = 'none';
						popupMenuStatus[i] = false;
						clearTimeout(popupMenuTimers[i][2]);
						if (menuButton) {
							menuButton.className = popupMenuButtonStyles[i];
						}
					}
					break;
				}
			}
		}
	}
}

function addEvent(eventListener, eventType, eventFunction, useCapture) {
	if (eventListener.addEventListener) {
		eventListener.addEventListener(eventType, eventFunction, useCapture);
		return true;
	} else if (eventListener.attachEvent) {
		return eventListener.attachEvent("on"+eventType, eventFunction);
	}
}

function textAreasInitialise() {
	var objectSelection = document.getElementsByTagName("textarea");
	var objectIndex;
	var currentObject;
	for (var i=0; i<objectSelection.length; i++) {
		currentObject = objectSelection[i];
		if (currentObject.getAttribute('maxlength')) {
			currentObject.onkeyup = forceMaxLength;
			currentObject.onpaste = forceMaxLength;
		}
	}
}

function forceMaxLength() {
	var maxLength = parseInt(this.getAttribute('maxlength'));
	if (this.value.length > maxLength) {
		this.value = this.value.substring(0,maxLength);
	}
}

function doPopupWin() {
	window.open('','popupwin','width=700,height=600,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=no');
}

function printPage() {
	if (allowPrint) {
		window.print();
	}
}

function closeWindow() {
	window.close();
}

function initialiseTabs(tabArray) {
	var tabName = window.location.hash.substring(1);
	var tabNumber = 0;
	if (tabArray.length > 0) {
		for (var i=0; i<tabArray.length; i++) {
			if ((document.getElementById('tab-' + tabArray[i])) && (document.getElementById('section-' + tabArray[i]))) {
				productTabs[productTabs.length] = tabArray[i];
				if ((tabName != '') && (tabName == tabArray[i])) {
					tabNumber = i;
				}
			}
		}
		if (productTabs.length > 0) {
			showTab(productTabs[tabNumber]);
		}
	}
}

function showTab(tabToShow) {
	if ((productTabs.length > 0) && (!productTabFreeze)) {
		productTabFreeze = true;
		for (var i=0; i<productTabs.length; i++) {
			if (productTabs[i] == tabToShow) {
				break;
			}
		}
		if (productTabs[i] == tabToShow) {
			if (currentTabID != 'tab-' + productTabs[i]) {
				if (currentSectionID != '') {
					document.getElementById(currentSectionID).style.display = 'none';
				}
				if (currentTabID != '') {
					document.getElementById(currentTabID).className = 'tab0';
				}
				if (currentAboveTabID == 'tababovefirst') {
					document.getElementById(currentAboveTabID).className = 'tababovefirst0';
				} else if (currentAboveTabID != '') {
					document.getElementById(currentAboveTabID).className = 'tab0';
				}
				if (currentBelowTabID == 'tabbelowlast') {
					document.getElementById(currentBelowTabID).className = 'tabbelowlast0';
				} else if (currentAboveTabID != '') {
					document.getElementById(currentBelowTabID).className = 'tab0';
				}
				currentSectionID = 'section-' + productTabs[i];
				document.getElementById(currentSectionID).style.display = 'block';
				currentTabID = 'tab-' + productTabs[i];
				document.getElementById(currentTabID).className = 'tab1';
				if (i == 0) {
					currentAboveTabID = 'tababovefirst';
					document.getElementById(currentAboveTabID).className = 'tababovefirst1';
				} else {
					currentAboveTabID = 'tab-' + productTabs[i-1];
					document.getElementById(currentAboveTabID).className = 'tababove1';
				}
				if (i == productTabs.length-1) {
					currentBelowTabID = 'tabbelowlast';
					document.getElementById(currentBelowTabID).className = 'tabbelowlast1';
				} else {
					currentBelowTabID = 'tab-' + productTabs[i+1];
					document.getElementById(currentBelowTabID).className = 'tabbelow1';
				}
			}
		}
		productTabFreeze = false;
	}
}

function showImage(imageID) {
    if (document.getElementById(imageID)) {
        document.getElementById(imageID).style.display = 'block';
    }
}

function hideImage(imageID) {
    if (document.getElementById(imageID)) {
        document.getElementById(imageID).style.display = 'none';
    }
}