/*******************************************************************************************
 * addLoadEvent
 * Originally written by Simon Willison (http://simonwillison.net/2004/May/26/addLoadEvent/)
 * Incorporated with code by Dean Edwards (http://dean.edwards.name/weblog/2006/06/again)
 * Takes a function as an argument which should be executed once the DOM has loaded
 * Parameters: function func
 * Example:
 *		addLoadEvent(myFunction);
 *		addLoadEvent(function() {
 *			alert ('a');
 *		});
 *******************************************************************************************/

	//--------------------------------------------------
	// Loading setup

		var addLoadEventStack = new Array();
		var addLoadEventCount = 0;

		function addLoadEvent(func) {
			addLoadEventStack[addLoadEventCount++] = func;
		}

	//--------------------------------------------------
	// Execute the functions when the page has loaded

		var addLoadEventDone = false;

		function addLoadEventInit() {

			//--------------------------------------------------
			// Do not run the functions twice

				if (addLoadEventDone) {
					return;
				} else {
					addLoadEventDone = true;
				}

			//--------------------------------------------------
			// Remove the loading timer for Safari

				if (addLoadEventSafariTimer) {
					clearInterval(addLoadEventSafariTimer);
				}

			//--------------------------------------------------
			// Execute the functions - cannot use variable 'i'
			// as the executed functions scope could change it

				for (addLoadEventProgress = 0; addLoadEventProgress < addLoadEventCount; addLoadEventProgress++) {
					addLoadEventStack[addLoadEventProgress]();
				}

		}

	//--------------------------------------------------
	// Triggers for the different browsers

		//--------------------------------------------------
		// For DOM compatible browsers - Firefox and Opera

		 	if (document.addEventListener) {
		 		document.addEventListener('DOMContentLoaded', addLoadEventInit, false);
		 	}

		//--------------------------------------------------
		// For Safari

			if (/WebKit/i.test(navigator.userAgent)) { // sniff
				var addLoadEventSafariTimer = setInterval(function() {
					if (/loaded|complete/.test(document.readyState)) {
						addLoadEventInit(); // call the onload handler
					}
				}, 10);
			}

		//--------------------------------------------------
		// For Internet Explorer

			/*@cc_on @*/
			/*@if (@_win32)

				document.write('<script id="addLoadEventIeOnload" defer="defer" src=//:><\/script>');
				var script = document.getElementById("addLoadEventIeOnload");
				script.onreadystatechange = function() {
					if (this.readyState == "complete") {
						addLoadEventInit(); // call the onload handler
					}
				};

			@end @*/

		//--------------------------------------------------
		// Fall back for other browsers

			window.onload = addLoadEventInit;

/*******************************************************************************************
 * cssjs
 * Originally written by Christian Heilmann (http://icant.co.uk)
 * Eases the dynamic application of CSS classes via DOM
 * Example:	cssjs('add', document.getElementById('foo'), 'bar');
 *******************************************************************************************/

	function cssjs(action, object, className) {

		if (action == 'add') {

			if (!cssjs('check', object, className)) {
				object.className += (object.className == '' ? '' : ' ') + className;
			}

		} else if (action == 'remove') {

			var exp = new RegExp('(^' + className + '( |$)| ' + className + '\\b)');
			object.className = object.className.replace(exp, '');

		} else if (action == 'check') {

			var exp = new RegExp('\\b' + className + '\\b');
			return exp.test(object.className);

		}

		return true;

	}

/*******************************************************************************************
 * addCssRule
 * Originally written by Craig Francis (http://craigfrancis.co.uk)
 * Add a CSS rule to the document - includes support for xhtml+xml
 * Example:	addCssRule('#itemId { position: absolute; left: -5000px; }');
 *******************************************************************************************/

	function addCssRule(cssRule) {

		this.useXmlMethods = (document.contentType && document.contentType.indexOf('xml') > -1);
		if (this.useXmlMethods) {

			var styleElement = createElement('style');
			var styleContent = document.createTextNode(cssRule);

			styleElement.setAttribute('type', 'text/css');
			styleElement.appendChild(styleContent);

			var headRef = document.getElementsByTagName('head');
			if (headRef[0]) {
				headRef[0].appendChild(styleElement);
			}

		} else {

			document.write ('<style type="text\/css"> ' + cssRule + ' <\/style>');

		}

	}

/*******************************************************************************************
 * isEmailAddr
 * Written by Christian Heilmann (http://icant.co.uk)
 * Check that the email address matches the correct basic format
 * Parameters: string str
 * Example:	isEmailAddr('1@1.com');
 *******************************************************************************************/

	function isEmailAddr(str) {
		var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/;
		return re.test(str);
	}

/*******************************************************************************************
 * isValidDate
 * Written by Chris Hogben (http://www.codetoad.com)
 * Check that the date is valid
 * Example:	isValidDate(30, 1, 2005);
 *******************************************************************************************/

	function isValidDate(day, month, year) {
		month--; // JavaScript takes January as "0"
		var dteDate=new Date(year, month, day);
		return ((day==dteDate.getDate()) && (month==dteDate.getMonth()) && (year==dteDate.getFullYear()));
	}

/*******************************************************************************************
 * printMe
 * Try to run the "print" function, otherwise tell the user how to print correctly.
 * Example:	printMe();
 *******************************************************************************************/

	function printMe() {
		try {
			print();
		} catch(exception){
			alert("To print this page, click file and\n select 'Print' or 'Print Preview' ");
		}
	}

/*******************************************************************************************
 * findPosX and findPosY
 * Written by Peter-Paul Koch (http://www.quirksmode.org/) and Alex Tingle (http://blog.firetree.net/)
 * Find where an element is on the page.
 * Example:	findPosX(this);
 *******************************************************************************************/

	function findPosX(obj) {
		var curleft = 0;
		if (obj.offsetParent) {
			while(1) {
				curleft += obj.offsetLeft;
				if(!obj.offsetParent) {
					break;
				}
				obj = obj.offsetParent;
			}
		} else if (obj.x) {
			curleft += obj.x;
		}
		return curleft;
	}

	function findPosY(obj) {
		var curtop = 0;
		if (obj.offsetParent) {
			while(1) {
				curtop += obj.offsetTop;
				if (!obj.offsetParent) {
					break;
				}
				obj = obj.offsetParent;
			}
		} else if (obj.y) {
			curtop += obj.y;
		}
		return curtop;
	}

/*******************************************************************************************
 * createElement
 * Written by Simon Willison (http://simon.incutio.com/)
 * Create an xhtml element with a nameSpace if possible
 * Example:	createElement('a');
 *******************************************************************************************/

	function createElement(element) {
		if (typeof document.createElementNS != 'undefined') {
			return document.createElementNS('http://www.w3.org/1999/xhtml', element);
		}
		if (typeof document.createElement != 'undefined') {
			return document.createElement(element);
		}
		return false;
	}

/*******************************************************************************************
 * getParent
 * Written by Richard Cornford (http://www.litotes.demon.co.uk/)
 * Move up the DOM to the specified tag
 * Example:	getParent(this, 'form');
 *******************************************************************************************/

	function getParent(el, pTagName) {
		if (el != null) {
			if ((el.nodeType == 1) && (el.tagName.toUpperCase() == pTagName.toUpperCase())) {
				return el;
			} else if (el.parentNode) {
				return getParent(el.parentNode, pTagName);
			}
		}
		return null;
	}

/*******************************************************************************************
 * loadUrl
 * Common AJAX function, original unknown
 * Expanded by Jamie White to include onAjaxLoad
 *******************************************************************************************/

	function loadUrl(dest, loadDiv, onAjaxLoad){

		var ajaxRequest;
		try{
			ajaxRequest = new XMLHttpRequest();// Opera 8.0+, Firefox, Safari
		}catch(e){// Internet Explorer Browsers
			try{
				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
				try{
					ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
				}catch(e){
					return false;
				}
			}
		}

		// Create a function that will receive data sent from the server
		ajaxRequest.onreadystatechange = function(){
			if(ajaxRequest.readyState == 4){
				document.getElementById(loadDiv).innerHTML = ajaxRequest.responseText;
//				onAjaxLoad();
			}
		}

		ajaxRequest.open("GET", dest, true);
		ajaxRequest.send(null);
	}
