/*#################################################################################*\
'# © Copyright 2007 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 Academy Class Training                       #
'# (www.academyclasstraining.co.uk) website and licensed for free use by Academy    #
'# Class Limited for an 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 blockCount = 0;
var popupMenuList = new Array('subtopmenu1','subtopmenu2');
var popupMenuStatus = new Array(false,false);
var popupMenuLocks = new Array(new Array(false,false),new Array(false,false));
var popupMenuTimers = new Array(new Array(false,false,false),new Array(false,false,false));
var popupMenuDefaultOpenTime = 150;
var itemAvailableList = new Array();
var itemSelectedList = new Array(Array(0,Array()));
var currentList;
var agentCompatible = false;
if (document.getElementById && document.createTextNode) {
	agentCompatible = true;
}

function hideInfoBlock(blockNumber) {
	if (document.getElementById('blk' + blockNumber)) {
		document.getElementById('blk' + blockNumber).style.display = 'none';
		document.getElementById('blkbtn' + blockNumber).innerHTML = '<a href="#" onclick="showInfoBlock(' + blockNumber + '); return false" class="infobox">&nbsp;SHOW &raquo;<\/a>';
		blockCount --;
		showBookingLink();
	}
}

function showInfoBlock(blockNumber) {
	if (document.getElementById('blk' + blockNumber)) {
		document.getElementById('blk' + blockNumber).style.display = 'block';
		document.getElementById('blkbtn' + blockNumber).innerHTML = '<a href="#" onclick="hideInfoBlock(' + blockNumber + '); return false" class="infobox">&nbsp;&laquo; HIDE<\/a>';
		blockCount ++;
		showBookingLink();
	}
}

function hideAllInfoBlocks() {
	i = 0;
	while (document.getElementById('blk' + i)) {
		hideInfoBlock(i);
		i++;
	}
}

function showBookingLink() {
	if (blockCount < 0) {
		blockCount = 0;
	}
	if (document.getElementById('bookinglink')) {
		if (blockCount > 0) {
			document.getElementById('bookinglink').innerHTML = '&nbsp;<a href="#" title="Booking a Course" onClick="hideAllInfoBlocks(); return false" class="grey">GO&nbsp;&raquo;</a>';
		} else {
			document.getElementById('bookinglink').innerHTML = '';
		}
	}
}

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 + '");',popupMenuDefaultOpenTime);
				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.style.backgroundColor = '#BCBDC0';
						menuButton.style.color = '#80992A';
					}
				}
				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').style.background = 'none';
				document.getElementById(popupMenuList[i] + 'button').style.color = '#636467';
			}
		}
	}
}

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.style.background = 'none';
							menuButton.style.color = '#636467';
						}
					}
					break;
				}
			}
		}
	}
}

function resetItemListAll() {
	for (var i=itemSelectedList.length - 1; i>=0; i--) {
		resetItemList(i);
	}
}

function resetItemList(listNumber) {
	currentList = listNumber;
	var itemAvailable = document.getElementById('itemavailable');
	var itemSelected = document.getElementById('itemselected');
	if (itemAvailable) {
		while (itemAvailable.options.length > 0) {
			itemAvailable.remove(0);
		}
		for (var i=0; i<itemAvailableList.length; i++) {
			if (findItem(itemAvailableList[i][0]) == -1) {
				var itemOption = document.createElement('option');
				itemOption.value = itemAvailableList[i][0];
				itemOption.text = itemAvailableList[i][1];
				itemAvailable.options.add(itemOption);
			}
		}
	}
	while (itemSelected.options.length > 0) {
		itemSelected.remove(0);
	}
	for (var i=0; i<itemSelectedList[currentList][1].length; i++) {
		var itemOption = document.createElement('option');
		itemOption.value = itemSelectedList[currentList][1][i][0];
		itemOption.text = itemSelectedList[currentList][1][i][1];
		itemSelected.options.add(itemOption);
	}
	generateOrder();
}

function generateOrder() {
	var itemSelected = document.getElementById('itemselected');
	var itemGenerated = document.getElementById('itemordergenerated' + itemSelectedList[currentList][0]);
	itemGenerated.value = '';
	for (var i=0; i<itemSelected.length; i++) {
		itemGenerated.value += itemSelected.options[i].value;
		if (i != itemSelected.length-1) {
			itemGenerated.value += ',';
		}
	}
}

function moveUp() {
	var itemSelected = document.getElementById('itemselected');
	if (!itemSelected.options[0].selected) {
		for (var i=0; i<itemSelected.length; i++) {
			if ((itemSelected.options[i].selected) && (i != 0)) {
				var currentValue = itemSelectedList[currentList][1][i][0];
				var currentText = itemSelectedList[currentList][1][i][1];
				itemSelectedList[currentList][1][i][0] = itemSelectedList[currentList][1][i-1][0];
				itemSelectedList[currentList][1][i][1] = itemSelectedList[currentList][1][i-1][1];
				itemSelectedList[currentList][1][i-1][0] = currentValue;
				itemSelectedList[currentList][1][i-1][1] = currentText;
				itemSelected.options[i].value = itemSelected.options[i-1].value;
				itemSelected.options[i].text = itemSelected.options[i-1].text;
				itemSelected.options[i].selected = itemSelected.options[i-1].selected;
				itemSelected.options[i-1].value = currentValue;
				itemSelected.options[i-1].text = currentText;
				itemSelected.options[i-1].selected = true;
			}
		}
		generateOrder();
	}
}

function moveDown() {
	var itemSelected = document.getElementById('itemselected');
	if (!itemSelected.options[itemSelected.length-1].selected) {
		for (var i=itemSelected.length-1; i>=0; i--) {
			if ((itemSelected.options[i].selected) && (i != itemSelected.length-1)) {
				var currentValue = itemSelectedList[currentList][1][i][0];
				var currentText = itemSelectedList[currentList][1][i][1];
				itemSelectedList[currentList][1][i][0] = itemSelectedList[currentList][1][i+1][0];
				itemSelectedList[currentList][1][i][1] = itemSelectedList[currentList][1][i+1][1];
				itemSelectedList[currentList][1][i+1][0] = currentValue;
				itemSelectedList[currentList][1][i+1][1] = currentText;
				itemSelected.options[i].value = itemSelected.options[i+1].value;
				itemSelected.options[i].text = itemSelected.options[i+1].text;
				itemSelected.options[i].selected = itemSelected.options[i+1].selected;
				itemSelected.options[i+1].value = currentValue;
				itemSelected.options[i+1].text = currentText;
				itemSelected.options[i+1].selected = true;
			}
		}
		generateOrder();
	}
}

function findItem(itemToFind) {
	var itemValue = -1;
	for (var i=0; i<itemSelectedList[currentList][1].length; i++) {
		if (itemSelectedList[currentList][1][i][0] == itemToFind) {
			itemValue = i;
			break;
		}
	}
	return itemValue;
}

function itemAvailableSelected(itemToFind) {
	var itemAvailable = document.getElementById('itemavailable');
	itemSelected = false;
	for (var i=0; i<itemAvailable.options.length; i++) {
		if ((itemAvailable.options[i].value == itemToFind) && (itemAvailable.options[i].selected)) {
			itemSelected = true;
			break;
		}
	}
	return itemSelected
}

function moveRight() {
	for (var i=0; i<itemAvailableList.length; i++) {
		if ((findItem(itemAvailableList[i][0]) == -1) && (itemAvailableSelected(itemAvailableList[i][0]))) {
			itemSelectedList[currentList][1].push(itemAvailableList[i]);
		}
	}
	resetItemList(currentList);
}

function moveLeft() {
	var itemSelected = document.getElementById('itemselected');
	var tempArray = new Array();
	for (var i=0; i<itemSelected.options.length; i++) {
		if (!itemSelected.options[i].selected) {
			tempArray.push(Array(itemSelected.options[i].value, itemSelected.options[i].text))
		}
	}
	itemSelectedList[currentList][1] = tempArray;
	resetItemList(currentList);
}

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 doTinyMCE(elementName) {
	tinyMCE.init({
		mode : 'exact',
		elements : elementName,
		theme : 'advanced',
		plugins : 'safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,inlinepopups',
		theme_advanced_buttons1 : 'undo,redo,|,cut,copy,paste,pastetext,pasteword,|,search,replace,|,cleanup,removeformat,|,code,preview,|,help',
		theme_advanced_buttons2 : 'formatselect,|,bold,italic,underline,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,outdent,indent,blockquote,|,sub,sup,|,link,unlink,anchor,|,forecolor,backcolor',
		theme_advanced_buttons3 : 'tablecontrols,|,image,hr,charmap,emotions',
		theme_advanced_buttons4 : '',
		theme_advanced_toolbar_location : 'top',
		theme_advanced_toolbar_align : 'left',
		theme_advanced_statusbar_location : 'bottom',
		theme_advanced_resizing : false,
		content_css : '/tiny_mce/css/content.css',
		template_external_list_url : '/tiny_mce/lists/template_list.js',
		external_link_list_url : '/tiny_mce/lists/link_list.js',
		external_image_list_url : '/tiny_mce/lists/image_list.js',
		media_external_list_url : '/tiny_mce/lists/media_list.js',
		template_replace_values : { 
			username : '',
			staffid : ''
		}
	});
}

function doTinyMCE(elementName) {
	tinyMCE.init({
		mode : 'exact',
		elements : elementName,
		theme : 'advanced',
		plugins : 'safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,inlinepopups',
		theme_advanced_buttons1 : 'undo,redo,|,cut,copy,paste,pastetext,pasteword,|,search,replace,|,cleanup,removeformat,|,code,preview,|,help',
		theme_advanced_buttons2 : 'formatselect,|,bold,italic,underline,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,outdent,indent,blockquote',
		theme_advanced_buttons3 : 'sub,sup,|,link,unlink,anchor,|,forecolor,backcolor',
		theme_advanced_buttons4 : 'tablecontrols,|,image,hr,charmap,emotions',
		theme_advanced_toolbar_location : 'top',
		theme_advanced_toolbar_align : 'left',
		theme_advanced_statusbar_location : 'bottom',
		theme_advanced_resizing : false,
		content_css : '/stylesheet.css',
		template_external_list_url : '/tiny_mce/lists/template_list.js',
		external_link_list_url : '/tiny_mce/lists/link_list.js',
		external_image_list_url : '/tiny_mce/lists/image_list.js',
		media_external_list_url : '/tiny_mce/lists/media_list.js',
		template_replace_values : { 
			username : '',
			staffid : ''
		}
	});
}