/*
root javascript class containing commonly used generic functions

V1.14 - 05/03/10 TS
- added method AddElementTo()

V1.13 - 23/06/08 TS
- added method SetDialogWidth()

V1.12 - 21/02/08 TS
- added method GetCurrentURLPath()

V1.11 - 31/01/08 TS
- added method IsValidEmailAddress()

V1.1 - 15/10/07 PH
- RemoveChildNodes() now supports an optional third parameter as the object position multiplier (defaults to 2?)

V1.0 - 09/08/07 TS
- new library
*/

function Root() {
	// states of elements to be shown/hidden
	this.elementStates = new Array();
	// preloaded images
	this.preloadedImages = new Array();
	
	// define class functions
	this.RemoveChildNodes = RemoveChildNodes;
	this.AddSelectOption = AddSelectOption;
	this.AddElementTo = AddElementTo;
	this.SetShow = SetShow;
	this.SetHide = SetHide;
	this.UpdateDisplay = UpdateDisplay;
	this.BackgroundHover = BackgroundHover;
	this.ImageSwap = ImageSwap;
	this.PreloadImages = PreloadImages;
	this.AddTableRow = AddTableRow;
	this.SetSelectByValue = SetSelectByValue;
	this.SetDialogHeight = SetDialogHeight;
	this.SetDialogWidth = SetDialogWidth;
	this.IsValidEmailAddress = IsValidEmailAddress;
	this.GetCurrentURLPath = GetCurrentURLPath;
}

// remove all current child nodes
function RemoveChildNodes(parentObject, remainElements, objectMultiplier) {
	if (!remainElements) remainElements = 0;	
	if (!objectMultiplier) objectMultiplier = 2;				// Default was two objects originally
	
	while (parentObject.childNodes[remainElements*objectMultiplier]) {
		parentObject.removeChild(parentObject.lastChild);
	}
}

// add an option to a drop down
function AddSelectOption(selectObject, optionValue, optionText, matchValue, legacyMode) {
	// if using legacy mode, add the option in the below way
	if (legacyMode) {
		i = selectObject.options.length;
		selectObject.options.length++;
		selectObject.options[i].value = optionValue;
		selectObject.options[i].text = optionText;
		// if the option value matches the match value, select it
		if (matchValue) {
			if (optionValue == matchValue) {
				selectObject.options[i].selected = true;	
			}
		}
		return selectObject.options[i];
	// otherwise use the appendChild method
	} else {
		// create an option object
		var optionObject = document.createElement('option');
		optionObject.value = optionValue;
		optionObject.innerHTML = optionText;
		// if the option value matches the match value, select it
		if (matchValue) {
			if (optionValue == matchValue) {
				optionObject.selected = true;
			}
		}
		// add it to the select object
		return selectObject.appendChild(optionObject);
	}
}

// adds an element of the specified tag name and returns a reference
function AddElementTo(parentNode, tagName, inputType) {
	// create a new node
	var childNode = document.createElement(tagName);
	// for input elements, the type MUST be specified before it's added to the document. if tag is 'input, the type can be set here
	if (tagName == 'input' && typeof(inputType) != 'undefined') {
		childNode.type = inputType;
	}
	// add the node to the document and return a reference
	parentNode.appendChild(childNode);
	return childNode;
}

// adds a an array of data as a row to a table
function AddTableRow(parentObject, cellContents) {
	// create a row object
	var rowObject = document.createElement('tr');
	// add each of the subsequent parameters as table cells to the row
	for (i in cellContents) {
		cellObject = document.createElement('td');
		cellObject.innerHTML = cellContents[i];
		rowObject.appendChild(cellObject);
	}
	// append the object to it's parent object
	parentObject.appendChild(rowObject);
	// return the reference to the row just created
	return rowObject;
}

// sets an element(s) referenced by id to be shown
function SetShow() {
	for (i = 0; i < arguments.length; i++) {
		elementId = arguments[i];
		this.elementStates[elementId] = '';
	}
}

// sets an element(s) referenced by id to be hidden
function SetHide() {
	for (i = 0; i < arguments.length; i++) {
		elementId = arguments[i];
		this.elementStates[elementId] = 'none';
	}
}

// updates the display with the actions set
function UpdateDisplay() {
	for (elementId in this.elementStates) {
		document.getElementById(elementId).style.display = this.elementStates[elementId];
	}
	// reset the array with the list of changes
	this.elementStates = new Array();
}

// changes the background colour on mouseover and restores on mouseout
function BackgroundHover(hoverObject, hoverColour) {
	if (typeof(hoverObject.mouseoutSet) == 'undefined') {
		hoverObject.mouseoutSet = true;
		hoverObject.onmouseout = function() { hoverObject.style.backgroundColor = ''; };
	}	
	hoverObject.style.backgroundColor = hoverColour;
}

// swaps an image for another and sets the mouseover image to the origional
function ImageSwap(imageObject, mouseOutImage) {
	if (typeof(imageObject.mouseoutSet) == 'undefined') {
		imageObject.mouseoutSet = true;
		var currentImage = imageObject.src;
		imageObject.onmouseout = function() { imageObject.src = currentImage; };
	}
	imageObject.src = mouseOutImage;
}

// preloads images into memory
function PreloadImages() {
	var tempImage;
	// loop through all image filenames supplied
	for (i = 0; i < arguments.length; i++) {
		tempImage = new Image();
		tempImage.src = arguments[i];
		this.preloadedImages.push(tempImage);
	}
}

// trys to set a select option's selected option by value
function SetSelectByValue(selectObject, newValue) {
	for (i = 0; i < selectObject.options.length; i++) {
		if (selectObject.options[i].value == newValue) {
			selectObject.options[i].selected = true;
		} else {
			selectObject.options[i].selected = false;
		}
	}
}

// changes dialog height
function SetDialogHeight(dialogHeight) {
	window.dialogHeight = dialogHeight+"px";	
}

// changes dialog height
function SetDialogWidth(dialogWidth) {
	window.dialogWidth = dialogWidth+"px";	
}

// checks that the supplied text is a valid email address
function IsValidEmailAddress(emailAddress) {
	if (emailAddress.indexOf('@') > -1 && emailAddress.indexOf('.')) {
		return true;
	} else {
		return false;
	}
}

// get the URL path of the current document
function GetCurrentURLPath() {
	var currentUrl = document.URL;
	var urlSplit = currentUrl.split('/');
	urlSplit[urlSplit.length-1] = '';
	return urlSplit.join('/');
}
