/*
 * Copyright (C), Perpetual rights reserved
 * BrayWorth Pty Ltd
 * PO Box 292, Tugun Q. Australia 4224
 * Tel/Fax: +61 7 55982108
 *
 * All Rights Reserved
 *
 * No part of this work may be reproduced, transmitted,
 * transcribed, stored in a retrieval system, or translated
 * into any other language or computer language in whole or
 * in part, in any form or by any means, whether it be
 * electronic, mechanical, magnetic, optical, manual or
 * otherwise, without prior written consent of BrayWorth Pty Ltd.
 *
 */
var isNS6 = document.getElementById && !document.all;
var isOpera = navigator.userAgent.indexOf('Opera') > -1;
var isIE = navigator.userAgent.indexOf('MSIE') > 1 && !isOpera;
var isIE7 = (isIE && document.all && !window.opera && window.XMLHttpRequest) ? true : false;
var isMoz = navigator.userAgent.indexOf('Mozilla/5.') == 0 && !isOpera;
var isFireFox = navigator.userAgent.indexOf('Firefox') > -1;
var isChrome = navigator.userAgent.indexOf('Chrome') > -1;
var isSafari = navigator.userAgent.indexOf('Safari') > -1;
var isIPhone = navigator.userAgent.toLowerCase().indexOf('iphone') > -1;
var isIPad = navigator.userAgent.toLowerCase().indexOf('ipad') > -1;
var isMobileDevice = ( isIPhone || isIPad );

var zIndex_OVERLAY = 90;
var zIndex_SIMPLEDIV = 150;
var zIndex_HOURGLASS = 999;

var K_ENTER = 13;		//   Enter Key

(function() {	// Strings
	String.prototype.trim = function() {return this.replace(/^\s+|\s+$/g,"");}
	String.prototype.ltrim = function() {return this.replace(/^\s+/,"");}
	String.prototype.rtrim = function() {return this.replace(/\s+$/,"");}
	String.prototype.pad = function(len, padChar){
		if ( padChar == undefined ) {padChar = " ";}
		if ( isNaN(len) ) { len = this.length; }
		var res = this;
		while ( res.length < len ) {res = res.concat(padChar);}
		return ( res );
	};

	String.prototype.padLeft = function(len, padChar){
			if ( padChar == undefined ) {padChar = " ";}
			if ( isNaN(len) ) { len = this.length; }
			var res = this;
			if (res.length > len) {
				var iStart = ( res.length - len );
				res = res.substring( iStart );
			} else {
				while ( res.length < len ) {res = padChar.concat(res);};
			}
			return ( res );
	};

	String.prototype.toCapitalCase = function() {
		var re = /\s/;
		var words = this.split(re);
		re = /(\S)(\S+)/;
		var reI = /^[a-zA-Z]'[a-zA-Z]+$/;
		for (var i = words.length - 1; i >= 0; i--) {
			if ( words[i] != "&" ) {
				if ( words[i].length > 3 && reI.test(words[i])) {
					//~ alert( 'It\'s Irish' );
					parts = words[i].split(/'/);
					words[i] = parts[0].toUpperCase() + "'" + parts[1].substring(0,1).toUpperCase() + parts[1].substring(1).toLowerCase();
				} else if ( re.test(words[i])) {
					words[i] = RegExp.$1.toUpperCase() + RegExp.$2.toLowerCase();
				}
			}
		}
		return words.join(' ');
	}
})();

(function() {	// Misc Functions

	window.setCookie = function( c_name, value, exdays) {
		var exdate = new Date();
		exdate.setDate( exdate.getDate() + exdays );
		var c_value = escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
		document.cookie = c_name + "=" + c_value;
	}

	window.getCookie = function(c_name) {

		var x,y,ARRcookies=document.cookie.split(";");
		for (var i=0;i<ARRcookies.length;i++) {

			x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
			y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
			x=x.replace(/^\s+|\s+$/g,"");
			if (x==c_name)
				return unescape(y);
		}
	}

	window.BlurOverLay = function(jIn) {

		var zIndex = zIndex_OVERLAY;
		if ( jIn ) {
			if (jIn.zIndex) zIndex = jIn.zIndex;
		}

		// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
		this.Overlay = document.createElement("div");
			this.Overlay.setAttribute('id','overlay');
			this.Overlay.onclick = function () {
				this.style.display = 'none'; return false;
				if ( this.parentNode ) this.parentNode.removeChild( this );
			}
			//~ this.Overlay.style.display = 'none';
			this.Overlay.style.position = 'absolute';
			this.Overlay.style.top = '0';
			this.Overlay.style.left = '0';
			this.Overlay.style.zIndex = zIndex;
			this.Overlay.style.width = '100%';
			this.Overlay.style.backgroundColor = 'transparent';

		this.Show = function() {

			var arrayPageSize = getPageSize();

			// set height of Overlay to take up whole page and show
			this.Overlay.style.height = (arrayPageSize[1] + 'px');
			this.Overlay.style.display = 'block';

			var o = document.getElementsByTagName("body").item(0);
				o.insertBefore(this.Overlay, o.firstChild);
		}

		this.Hide = function() { this._Hide(); }
		this._Hide = function() {
			if ( this.Overlay.parentNode ) this.Overlay.parentNode.removeChild( this.Overlay );
			this.Overlay.style.display = 'none';
		}
	}

	window.getOffSets = function( source ) {
		/*
		 * Returns the Elements Position Relative to the Page
		 */
		this.top = 0;
		this.left = 0;
		this.height = source.offsetHeight;
		this.width = source.offsetWidth;
		this.parent = DocumentBody();
		var src = source;
		while(src) {
			if ( /relative/.test( src.style.position ) || src.tagName.toLowerCase() == 'body' ) {
				this.parent = src;
				return;
			}
			this.top += parseInt(src.offsetTop,10);
			this.left += parseInt(src.offsetLeft,10);
			src = src.offsetParent || null;
		}
	}

	window.PageDimensions = function () {

		/*
		 * Dimensions of the Window
		 */

		var arrayPageSize = getPageSize();

		this.width = parseInt(arrayPageSize[0],10);
		this.height = parseInt(arrayPageSize[1],10);
		this.WindowWidth = parseInt(arrayPageSize[2],10);
		this.WindowHeight = parseInt(arrayPageSize[3],10);

		this.body = document.body;
		this.scrollTop = parseInt(this.body.scrollTop,10);
		this.scrollLeft = parseInt(this.body.scrollLeft,10);

		this.topCenter = parseInt(this.WindowHeight/2,10)+this.scrollTop;
		this.leftCenter = parseInt(this.WindowWidth/2,10)+this.scrollLeft;
	}

	var getPageSize = function() {

		/*
		 * getPageSize()
		 *
		 * Returns array with page width, height and window width, height
		 * Core code from - quirksmode.org
		 * Edit for Firefox by pHaez
		 */

		var xScroll, yScroll;

		if (window.innerHeight && window.scrollMaxY) {
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}

		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}

		pageHeight = (yScroll < windowHeight ? windowHeight : yScroll );		// for small pages with total height less then height of the viewport
		pageWidth = (xScroll < windowWidth ? windowWidth : xScroll );			// for small pages with total width less then width of the viewport

		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
		return arrayPageSize;
	}

})();

function parseVal(val) {
   while (val.charAt(0) == '0')
      val = val.substring(1, val.length);

   return (val);
}

(function() {	// Date Functions


	var CheckDateFormat = function( obj ) {

		/* Date/Time Validation */

		if ( ( ! obj ) || ( ! obj.tagName )) {

			// obj is probably an Event ..
			if ( this.tagName ) {
				if ( /input/i.test(this.tagName)) obj = this;

				// otherwise fall through and error ..
			}
		}

		var strVal = obj.value.trim();
		if (strVal == '')
			return true;

		var d = new Date();

		var dayOff = 0;
		var day = d.getDate();
		var month = d.getMonth()+1;
		var year = d.getFullYear();

		/* work some additions */
		if ( strVal.search(/tod|tom/i) > -1 ) {

			if ( strVal.search(/tom/i) > -1 )
				d = addDaysToDate( d, 1 );
			day = d.getDate();
			month = d.getMonth()+1;
			year = d.getFullYear();
		} else if ( strVal.search(/sun|mon|tue|wed|thu|fri|sat/i) > -1 ) {

			if ( strVal.search(/sun/i) > -1 ) var target = 0;
			else if ( strVal.search(/mon/i) > -1 ) var target = 1;
			else if ( strVal.search(/tue/i) > -1 ) var target = 2;
			else if ( strVal.search(/wed/i) > -1 ) var target = 3;
			else if ( strVal.search(/thu/i) > -1 ) var target = 4;
			else if ( strVal.search(/fri/i) > -1 ) var target = 5;
			else if ( strVal.search(/sat/i) > -1 ) var target = 6;

			day = d.getDay();
			dayOff = target - day + ( day > target ? 7 : 0  );
			d = addDaysToDate( d, dayOff );
			day = d.getDate();
			month = d.getMonth()+1;
			year = d.getFullYear();
		} else if ( strVal.search(/d|w|m/i) > -1 ) {

			if ( strVal.search(/d/i) > -1 ) {

				var iPos = strVal.search(/d/i);
				var dayOff = parseInt( strVal.substring(0,iPos).trim(),10);
				d = addDaysToDate( d, dayOff );
			} else if ( strVal.search(/w/i) > -1 ) {

				var iPos = strVal.search(/w/i);
				var dayOff = parseInt( strVal.substring(0,iPos).trim(),10)*7;
				d = addDaysToDate( d, dayOff );
			} else if ( strVal.search(/m/i) > -1 ) {

				var iPos = strVal.search(/m/i);
				var monthOff = parseInt( strVal.substring(0,iPos).trim(),10);
				d = addMonthsToDate( d, monthOff );
			}
			day = d.getDate();
			month = d.getMonth()+1;
			year = d.getFullYear();
		} else if ( strVal.indexOf('+') > -1 ) {

			// must be at beginning or end
			if ( strVal.substr(0,1) == "+" ) {

				var dayOff = parseInt( strVal.substring(1),10);
				d = addDaysToDate( d, dayOff );
				day = d.getDate();
				month = d.getMonth()+1;
				year = d.getFullYear();
			} else if ( strVal.substr(--strVal.length,1) == "+" ) {

				var dayOff = parseInt( strVal.substring(0,--strVal.length),10);
				d = addDaysToDate( d, dayOff );
				day = d.getDate();
				month = d.getMonth()+1;
				year = d.getFullYear();
			}
		} else if ( strVal.indexOf('/') > 0 ) {

			var a = strVal.split('/');
			day = parseInt( parseVal(a[0]),10);
			month = parseInt( parseVal(a[1]),10);
			if (a.length > 2)
				year = parseInt(a[2],10);
		} else if ( strVal.indexOf('.') > 0 ) {

			var a = strVal.split('.');
			day = parseInt( parseVal(a[0]),10);
			month = parseInt( parseVal(a[1]),10);
			if (a.length > 2)
				year = parseInt(a[2],10);
		} else {

			day = parseInt( parseVal(strVal),10);
			if (day < d.getDate())
				month ++;
		}


		if ( isNaN( day ))
			day = d.getDate();

		if ( day < 1 || day > 31 ) {

			alert( 'Invalid Date : ' + day );
			return false;
		}

		if ( month > 12 ) {
			month -= 12;
			year ++;
		}

		// set month first - otherwise some day rejected
		if ( month > 0 || month < 13 )
			d.setMonth( month-1 );


		d.setDate( day );

		if ( year > 0 && year.toString().length == 4 )	// only process a full year format
			d.setYear(year);

		var a = {
			day		: d.getDate(),
			month	: (parseInt(d.getMonth(),10)+1),
			year	: d.getFullYear()
		}

		a.day = a.day.toString().padLeft( 2, '0' );
		a.month = a.month.toString().padLeft( 2, '0' );

		obj.value = a.day + '/' + a.month + '/' + a.year;
		return true;
	}

	var CheckTimeFormat = function( obj ) {

		if ( ( ! obj ) || ( ! obj.tagName )) {

			// obj is probably an Event ..
			if ( this.tagName ) {
				if ( /input/i.test(this.tagName)) obj = this;

				// otherwise fall through and error ..
			}
		}

		var strVal = obj.value.trim();
		if (strVal == '')
			return true;

		var eTag = strVal.substring( strVal.length -1, strVal.length );
		var sSuffix = 'am';
		var iHours = 0, iMinutes = 0;

		try {

			if ( eTag == 'p' || eTag == 'P' ) {

				sSuffix = 'pm';
				strVal = strVal.substring(0, strVal.length -1 ).trim();
			} else if ( eTag == 'a' || eTag == 'A' ) {

				strVal = strVal.substring(0, strVal.length -1 ).trim();
			} else if ( eTag == 'm' || eTag == 'M' ) {

				eTag = strVal.substring( strVal.length -2, strVal.length );
				if ( eTag == 'pm' || eTag == 'pM' || eTag == 'Pm' || eTag == 'PM' ) {

					sSuffix = 'pm';
					strVal = strVal.substring(0, strVal.length -2 ).trim();
				} else if ( eTag == 'am' || eTag == 'aM' || eTag == 'Am' || eTag == 'AM' ) {

					strVal = strVal.substring(0, strVal.length -2 ).trim();
				}
			}

			if ( strVal.indexOf(':') > 0 ) {

				// hours and minutes
				var a = strVal.split(':');
				iHours = parseInt(a[0],10);
				iMinutes = parseInt(a[1],10);
			} else if ( strVal.indexOf('.') > 0 ) {

				// hours and minutes
				var a = strVal.split('.');
				iHours = parseInt(a[0],10);
				iMinutes = parseInt(a[1],10);
			} else {

				iHours = parseInt(strVal,10);
			}

			if ( iHours >= 12 ) {

				sSuffix = 'pm';
				if ( iHours > 12 )
					iHours -= 12;
			}

			var sHours = String(iHours)
			var sMinutes = String(iMinutes)
			//~ obj.value = sHours.padLeft(2,'0') + ':' + sMinutes.pad(2,'0') + ' ' + sSuffix;
			obj.value = sHours + ':' + sMinutes.pad(2,'0') + ' ' + sSuffix;

			return true;
		} catch(e) {

			alert( 'unable to parse time format : ' + e.description  );
			return false;
		}
	}
	window.CheckDateFormat = CheckDateFormat;
	window.CheckTimeFormat = CheckTimeFormat;
})();


(function() {	// Window Functions

	window.navto = function( sUrl ){
		window.location=sUrl;
	}

	window.StopPropagation = function ( evt ) {

		evt = (evt) ? evt : ((window.event) ? window.event : '');

		if (evt) {
			if ( evt.preventDefault ) evt.preventDefault();
			if (isIE) {
				evt.cancelBubble = true;
				evt.returnValue = false;
			}
			if( evt.stopPropagation ) evt.stopPropagation();
		}
	}

	window.ApplyHTMLAttributes = function( el, j ) {

		if ( j ) {

			if ( j.id ) el.id = j.id;

			if ( j.title ) el.title = j.title;
			if ( j.className ) el.className = j.className;
			if ( j.alt ) el.alt = j.alt;
			if ( j.src ) el.src = j.src;
			if ( j.innerHTML ) el.innerHTML = j.innerHTML;
			if ( j.href ) el.href = j.href;

			if ( j.textAlign ) el.style.textAlign = j.textAlign;
			if ( j.verticalAlign ) el.style.verticalAlign = j.verticalAlign;
			if ( j.color ) el.style.color = j.color;
			if ( j.backgroundColor ) el.style.backgroundColor = j.backgroundColor;
			if ( j.fontSize ) el.style.fontSize = j.fontSize;
			if ( j.fontStyle ) el.style.fontStyle = j.fontStyle;
			if ( j.fontWeight ) el.style.fontWeight = j.fontWeight;
			if ( j.overflow ) el.style.overflow = j.overflow;
			if ( j.overflowX ) el.style.overflowX = j.overflowX;
			if ( j.overflowY ) el.style.overflowY = j.overflowY;

			if ( j.display ) el.style.display = j.display;
			if ( j.position ) el.style.position = j.position;
			if ( j.zIndex ) el.style.zIndex = j.zIndex;
			if ( /string/i.test( typeof( j.top ))) el.style.top = j.top;
			if ( /string/i.test( typeof( j.left ))) el.style.left = j.left;
			if ( /string/i.test( typeof( j.bottom ))) el.style.bottom = j.bottom;
			if ( /string/i.test( typeof( j.right ))) el.style.right = j.right;
			if ( /string/i.test( typeof( j.width ))) el.style.width = j.width;
			if ( /string/i.test( typeof( j.height ))) el.style.height = j.height;

			if ( j.border ) el.style.border = j.border;
			if ( j.borderTop ) el.style.borderTop = j.borderTop;
			if ( j.margin ) el.style.margin = j.margin;
			if ( j.padding ) el.style.padding = j.padding;
			if ( /number|string/i.test( typeof( j.cellPadding )) ) el.cellPadding = j.cellPadding;
			if ( /number|string/i.test( typeof( j.cellSpacing )) ) el.cellSpacing = j.cellSpacing;
			if ( /number|string/i.test( typeof( j.colSpan )) ) el.colSpan = j.colSpan;
			if ( /number|string/i.test( typeof( j.rowSpan )) ) el.rowSpan = j.rowSpan;

			if ( j.cursor ) el.style.cursor = j.cursor;
			if ( /function/i.test( typeof( j.onclick )) ) el.onclick = j.onclick;
			if ( /function/i.test( typeof( j.onblur )) ) el.onblur = j.onblur;
			if ( /function/i.test( typeof( j.onmouseover )) ) el.onmouseover = j.onmouseover;
			if ( /function/i.test( typeof( j.onmouseout )) ) el.onmouseout = j.onmouseout;
			if ( /function/i.test( typeof( j.oncontextmenu )) ) el.oncontextmenu = j.oncontextmenu;


			if ( j.cssFloat ) {
				if (isIE)
					el.style.styleFloat = j.cssFloat;
				else
					el.style.cssFloat = j.cssFloat;
			}

			// Input Elements
			if ( /string/i.test( typeof( j.type ))) el.type = j.type;
			if ( j.size ) el.size = j.size;
			if ( j.name ) el.name = j.name;
			if ( /number|string/i.test( typeof( j.maxlength )) ) el.maxLength = j.maxlength;
			if ( /number|string/i.test( typeof( j.maxLength )) ) el.maxLength = j.maxLength;
			if ( ! /undefined/i.test(j.value) ) el.value = j.value;

			if ( /select/i.test( el.tagName ) ) {

				if (j.options) {

					for ( var i = 0; i < j.options.length; i ++ ) {
						var o = htmlElement({tagName:'option'});
						if ( /string/i.test( typeof(j.options[i].value))) {
							o.value = j.options[i].value;
							o.text = j.options[i].text;
						} else {
							o.value = j.options[i];
							o.text = j.options[i];
						}
						(isIE ? el.add(o) : el.add(o,null));
						if ( j.value && j.value == o.value ) {
							o.defaultSelected = true;
							o.selected = true;
						}
					}
				}
			} else if ( /textarea/i.test( el.tagName ) ) {

				if ( /number|string/i.test( typeof( j.rows )) ) el.rows = j.rows;
				if ( /number|string/i.test( typeof( j.cols )) ) el.cols = j.cols;
			}
		}
	}

	window.addCell = function( row, value, j ) {
		var c = row.insertCell(-1);
		if (typeof(value) == 'object')
			c.appendChild(value);
		else
			c.innerHTML = value;

		ApplyHTMLAttributes( c, j );
		return (c);
	}

	window.htmlElement = function( j ) {

		if (j) {

			var el = document.createElement(( j.tagName ? j.tagName : 'span' ));
			ApplyHTMLAttributes( el, j );
			return (el);
		} else {

			return (document.createElement('span'));
		}
	}

	window.pQ = function( s ) {
		var el = document.getElementById(s);
		return ( el == null ? false : el );
	}

	window.msg = function(v) {
		var d = htmlElement({tagName:'div',innerHTML:v});
			DocumentBody().appendChild(d);
	}

	window.DocumentBody = function() {
		return ( (document.compatMode && document.compatMode!="BackCompat" ) ? document.documentElement : document.body );
	}

	window.hideURLbar = function() {
		if ( isIPhone ) window.scrollTo(0, 0.9);
	}

	window.simpleDiv = function( jIn ) {

		var el = {

			htmlElement : htmlElement({tagName:'div'}),
			parent : DocumentBody(),
			padding : '3px',
			backgroundColor : 'white',
			className : 'bordered',
			zIndex : zIndex_SIMPLEDIV,

			appendChild	: function( el ) {
				this.htmlElement.appendChild(el);
			},

			Show : function( top, left ) {

				if ( this.Overlay ) this.Overlay.Show();

				var p = new PageDimensions();

				ApplyHTMLAttributes(this.htmlElement,{
					zIndex : this.zIndex,
					backgroundColor : this.backgroundColor,
					className : this.className,
					padding : this.padding,
					position : 'absolute',
					display : 'block'
				});
				this.htmlElement.style.visibility = 'hidden';
				this.parent.appendChild(this.htmlElement);

				/* at this point the element has dimensions */

				if ( ! ( typeof(top)=='number' && typeof(left)=='number' )) {

					if ( ! top) top = ( p.topCenter - (this.htmlElement.offsetHeight/2));
					if ( ! left) left = ( p.leftCenter - (this.htmlElement.offsetWidth/2));
				}

				if ( top < 0 ) top = 0;
				if ( ( left + this.htmlElement.offsetWidth ) > p.WindowWidth )
					left = p.WindowWidth - this.htmlElement.offsetWidth;

				/* apply position based on dimensions */
				ApplyHTMLAttributes(this.htmlElement,{ top: top + 'px', left: left + 'px' });

				if ( /html/i.test(this.parent.tagName) ) {

					// otherwise we are not attached to the window
					var ScrolledOff = p.WindowHeight - ( top + this.htmlElement.offsetHeight );
					if ( ScrolledOff < 0  )	{ // it's off the page
						if( top < p.scrollTop ) {
							window.scroll( 0, top );
						} else if ( this.htmlElement.offsetHeight >= p.WindowHeight ) {
							window.scroll( 0, top );
						} else if( ( - ScrolledOff ) > p.scrollTop ) {
							window.scroll( 0, ( - ScrolledOff ) );
						}
					} else if( top < p.scrollTop ) {
						window.scroll( 0, top );
					}
				}
				this.htmlElement.style.visibility = 'visible';
			},

			Hide : function() {
				if ( this.htmlElement.parentNode ) this.htmlElement.parentNode.removeChild( this.htmlElement );
				if ( this.Overlay ) this.Overlay.Hide();
			}
		}

		if ( jIn ) {
			if ( jIn.modal ) {

				el.Overlay = new BlurOverLay();
				//~ el.Overlay.Overlay.setAttribute('id','overlay_light');
				el.Overlay.Overlay.div = el;
				el.Overlay.Overlay.onclick = '';
			} else if ( jIn.Overlay ) {

				el.Overlay = jIn.Overlay;
			}
			if ( jIn.parent ) el.parent = jIn.parent;
			if ( /number|string/i.test( typeof( jIn.padding ))) el.padding = jIn.padding;
			if ( jIn.backgroundColor ) el.backgroundColor = jIn.backgroundColor;
			if ( jIn.className ) el.className = jIn.className;
			if ( jIn.zIndex ) el.zIndex = jIn.zIndex;
		}

		return ( el );
	}

	window.simpleDIV = window.simpleDiv;

	window.newsflash = function( msg, timeout ) {
		var dimensions = new PageDimensions();
		var iRandom = Math.floor(Math.random() * 99999);

		var d = htmlElement({tagName:"div",
				position  : 'absolute',
				zIndex    : zIndex_HOURGLASS,
				innerHTML : msg,
				className : 'newsflash',
				padding   : '10px',
				textAlign : 'center',
				id        : 'nf' + iRandom })

		d.style.visibility = 'hidden';

		DocumentBody().appendChild(d);

		d.style.top = ( (dimensions.WindowHeight - 16) / 2 ) + 'px';
		d.style.left = ( (dimensions.WindowWidth - parseInt( d.offsetWidth )) / 2 ) + 'px';
		d.style.visibility = 'visible';

		window.setTimeout( "var e = document.getElementById('" + d.id + "');e.parentNode.removeChild(e);", (timeout?timeout:1000) );
	}

	window.hourglass = {

		on  : function() {

			if ( hourglass.img )
				return;

			var dimensions = new PageDimensions();
			var img = htmlElement({tagName:'img',
				src:'images/loading.gif',
				position:'absolute',
				top: ( (dimensions.WindowHeight - 16) / 2 ) + 'px',
				left: ( (dimensions.WindowWidth - 16) / 2 ) + 'px',
				zIndex: zIndex_HOURGLASS });

			DocumentBody().appendChild(img);
			hourglass.img = img;
		},

		off : function() {

			if ( hourglass.img )
				hourglass.img.parentNode.removeChild(hourglass.img);	// vaporised

			hourglass.img = false;
		}
	};

	window.FormatCurrency = function(num,decimalNum) {
		var v = FormatNumber( num, decimalNum, false, false, true );
		return ("$"+v);
	};

	window.FormatNumber = function(num,decimalNum,bolLeadingZero,bolParens,bolCommas) {
		/*
		 * IN:
		 * 	NUM - the number to format
		 *	decimalNum - the number of decimal places to format the number to
		 *	bolLeadingZero - true / false - display a leading zero for
		 *									numbers between -1 and 1
		 *	bolParens - true / false - use parenthesis around negative numbers
		 *	bolCommas - put commas as number separators.
		 *
		 * RETVAL:
		 *	The formatted number!
		 */

		if (isNaN(parseInt(num,10))) return "NaN";

		var tmpNum = num;
		var iSign = num < 0 ? -1 : 1;		// Get sign of number

		if ( ! ( decimalNum < 0 )) {
			// Adjust number so only the specified number
			// of numbers after the decimal point are shown.
			tmpNum *= Math.pow(10,decimalNum);
			tmpNum = Math.round(Math.abs(tmpNum))
			tmpNum /= Math.pow(10,decimalNum);
			tmpNum *= iSign;						// Readjust for sign

			var sNumStr = new String(tmpNum);			// Create a string object to do our formatting on
			if(sNumStr.indexOf('.') < 0) { sNumStr += '.'; }
			while (sNumStr.length < ( sNumStr.indexOf('.') + decimalNum + 1))
				sNumStr += '0';

			tmpNum = sNumStr;
		}

		var tmpNumStr = new String(tmpNum);			// Create a string object to do our formatting on

		// See if we need to strip out the leading zero or not.
		if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
			if (num > 0)
				tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
			else
				tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);

		// See if we need to put in the commas
		if (bolCommas && (num >= 1000 || num <= -1000)) {
			var iStart = tmpNumStr.indexOf(".");
			if (iStart < 0)
				iStart = tmpNumStr.length;

			iStart -= 3;
			while (iStart >= 1) {
				tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
				iStart -= 3;
			}
		}

		// See if we need to use parenthesis
		if (bolParens && num < 0)
			tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";

		return ( tmpNumStr.toString());		// Return our formatted string!
	};
}) ()

