/*Name:Utility Function*/
/*Default:true*/
/*Order:1*/


/* 	
	----------------------------------------------------------------------
	LOAD FUNCTION
	For scripts that need to be initalised, we can use have multipules
	run on page load. Yaaaa booo.
	---------------------------------------------------------------------- 
*/
window.onload = function () {
	for (var i in arguments.callee.actions) {
		arguments.callee.actions[i]();
	}
};
window.onload.actions = [];

/* 	
	----------------------------------------------------------------------
	HAS CLASS
	Ver simple function that looks at an element checks if it has 
	specified class name.
	
	In the future I will improve this to deal with a single class. I 
	might even make it a method of the DOM objects...
	---------------------------------------------------------------------- 
*/
function hasClass(element, test) {
	var classes = element.className.split(' ');
	for (var i = 0; i < classes.length; i++) {
		if (classes[i] == test) {
			return true;
		}
	}
	return false;
}

/* 	
	----------------------------------------------------------------------
	ERROR DISPLAY
	A few functions to display errors inline
	---------------------------------------------------------------------- 
*/
var elError = null;
var elErrorParent;
var errorTimeout = null;
function errorDisplay(message, element) {
	// Check to see if we are displaying an error already
	if (elError != null) {
		errorHide(elError);
		errorDisplay(message, element);
	}
	else {
		// Make sure the parent element is relative so it contains this div
		element.parentNode.style.position = 'relative';
		// Create the div to insert
		elError = document.createElement('div');
		elError.innerHTML = message;
		elError.className = 'error';
		// Tricky positioning (need to make this work in IE)
		elError.style.left = element.offsetLeft + element.offsetWidth + 'px';
		elError.style.top = element.offsetTop + 'px';
		// we may need to add some onresize events to make this stay positioned
		elErrorParent = element;
		window.onresize = errorMove;
		// Now we need to position the sucker
		element.parentNode.insertBefore(elError, element);
		// Set a timeout to hide it
		errorTimeout = window.setTimeout('errorHide(elError)', 3500);
	}
}
function errorHide(error) {
	window.clearTimeout(errorTimeout);
	error.parentNode.removeChild(error);
	elError = null;
	errorTimeout = null;
	window.onresize = null;
}
function errorMove() {
	elError.style.left = elErrorParent.offsetWidth + elErrorParent.offsetLeft + 5 + 'px';
}

/* 	
	----------------------------------------------------------------------
	ADD/REMOVE CLASS
	This adds or removes a class depending on if is all ready attached to
	an element.
	---------------------------------------------------------------------- 
*/
function addClass(element, className) {
	var classes = element.className.split('');
	// Check to see if it exists
	// If it does skip out
	for (var i = 0; i < classes.length; i++) {
		if (classes[i] == className) {
			return;
		}
	}
	// Otherwise add it
	element.className = element.className + ' ' + className;
}
function removeClass(element, className) {
	var newClass = ''
	var classes = element.className.split(' ');
	for (var i = 0; i < classes.length; i++) {
		if (classes[i] != className) {
			newClass += classes[i] + ' ';
		}
	}
	// set the class
	element.className = rightTrim(newClass);
}

/* 	
	----------------------------------------------------------------------
	STRING FUNCTIONS
	Two functions to strip white space from the left or right of a string
	---------------------------------------------------------------------- 
*/
function leftTrim(sString) {
	while (sString.substring(0,1) == ' ') {
		sString = sString.substring(1, sString.length);
	}
	return sString;
}
function rightTrim(sString) {
	while (sString.substring(sString.length-1, sString.length) == ' ') {
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

/* 	
	----------------------------------------------------------------------
	TRIM ARRAY
	Cleans out empty entries in an array 
	---------------------------------------------------------------------- 
*/
function trimArray(a){
	var tmp=new Array();
	for(j=0;j<a.length;j++)
		if(a[j]!='')
			tmp[tmp.length]=a[j];
	a.length=tmp.length;
	for(j=0;j<tmp.length;j++)
		a[j]=tmp[j];
	return a;
}
/* Remove hased array entry */
function removeHashedArrayEntry(array, element) {
	var newArray = new Array();
	for (var i in array) {
		if (array[i] != element) {
			newArray[i] = array[i];
		}
	}
	return newArray;
}

/* 	
	----------------------------------------------------------------------
	CURRENCY FORMATTER
	Pretty simple. This one just makes our currencies look nice.
	---------------------------------------------------------------------- 
*/
function currencyFormatter(amount) {
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

