/*
'####################################################################################
'# © Copyright 2008 Netfactors Internet (www.netfactors.co.uk). All rights reserved. #
'#----------------------------------------------------------------------------------#
'# This library of routines has been developed by Netfactors Internet and is         #
'# partially customised for use on the bfl magazine website (www.bflmagazine.co.uk) #
'# and licensed for free use by Stable Publishing for a time indefinite period.     #
'# Copyright of the library and any routines therein remains the property           #
'# Netfactors Internet. 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 popupMenuList = new Array();
var popupMenuStatus = new Array();
var popupMenuLocks = new Array();
var popupMenuTimers = new Array();
var popupMenuButtonStyles = new Array();
var agentCompatible = false;
if (document.getElementById && document.createTextNode) {
	agentCompatible = true;
}

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();
}
