
function xml_get_field( xmldoc, field_name )
{
	var xml_array = xmldoc.getElementsByTagName(field_name);
	if( xml_array.length > 0 )
	{
		if( xml_array[0].firstChild != null )
		{

			// If the client is using mozilla, we fetch the xml element differently as Mozilla has a bug not allowing more than 4096 chars in each element.
			if( true == isMoz() )
			{
				return xml_array[0].textContent;
			}
			else
			{
				return xml_array[0].firstChild.nodeValue;
			}
		}
		else
		{
			return "";
		}
	}
	else
	{
		return "";
	}
}

/**
	Returns true or false if browser is mozilla

	return bool
*/
function isMoz()
{
	// Set the default value to false.
	var moz = false;

	if( !document.layers )
	{
		// Is the browser Konquerer.
		konq = ( navigator.userAgent.indexOf( 'Konqueror' ) != -1 );

		// Is the browser Safari?
		saf = ( navigator.userAgent.indexOf( 'Safari' ) != -1 );

		// Is the browser Mozilla?
		moz = ( navigator.userAgent.indexOf( 'Gecko' ) != -1 && !saf && !konq);
	}

	return moz;
}



/**
	Finds the absolute X/Y coordinates of an item

	@param element *obj

	return array (left, top)

*/
function findPos( obj )
{
	var curleft = 0;
	var curtop = 0;
	var pos = new Object();
	pos.left = 0;
	pos.top = 0;

	if( obj.offsetParent )
	{
		pos.left = obj.offsetLeft;
		pos.top = obj.offsetTop;
		while( obj = obj.offsetParent )
		{
			pos.left += obj.offsetLeft;
			pos.top += obj.offsetTop;
		}
	}
	return pos;
}

function last_path_element( path )
{

	var elements = path.split("/");
	return elements[elements.length-1];
}

function require( filename )
{
	var require_uri = parseUri(filename);
	var current_js = document.getElementsByTagName('script');
	var check_host = true;
	// The required file is a relative link.
	if( require_uri.host == '' || require_uri.host == filename )
	{
		check_host = false;
	}

	// Go through all the js and make sure the file has not already been included.
	for( var i=0; current_js.length > i; i++ )
	{

		if( current_js[i].src != "" )
		{
			var uri = parseUri(current_js[i].src);

			if( uri.path == require_uri.path )
			{
				// The paths match
				if( check_host )
				{
					var current_page = parseUri(location.href);

					// We where passed an absolute link make sure the hosts match aswell.
					if( require_uri.host == current_page.host )
					{
						return;
					}
				}
				else
				{
					return;
				}
			}
		}
	}

	var fileref=document.createElement('script')
	fileref.setAttribute("type","text/javascript")
	fileref.setAttribute("src", filename)

	if( typeof fileref!="undefined" )
	{
		document.getElementsByTagName("head")[0].appendChild(fileref)
	}
}

function parseUri( str )
{
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while( i-- ) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function( $0, $1, $2 )
	{
		if( $1 )
		{
			uri[o.q.name][$1] = $2;
		}
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};

function requireCSS( filename )
{
	var fileref=document.createElement("link")
	fileref.setAttribute("rel", "stylesheet")
	fileref.setAttribute("type", "text/css")
	fileref.setAttribute("href", filename)
	if( typeof fileref!="undefined" )
	{
		document.getElementsByTagName("head")[0].appendChild(fileref)
	}
}

function destroyElement( element )
{
	if( element )
	{
		if( element.parentNode )
		{
			element.parentNode.removeChild(element);
		}
	}
}


/**
 * Restricts a form field with an id of field_id to be maxChars in length
 *
 * It should be called onkeyup and onchange from the form field declaration.
 *
 * If the form element with id of charsleft_text_id exists, a count of how many
 * characters can still be entered will be automatically updated with that number
 *
 * @param formField			-- the object that is being tested
 * @param charsleft_text_id	-- the id of the html element of where to display the number of characters remaining
 * @param maxChars			-- the maximum allowed characters
 */
function checkMaxChars( formField, charsleft_text_id, maxChars )
{
	if( charsleft_text_id.length > 0 )
	{
		var charsLeftField = document.getElementById(charsleft_text_id);
	}

	if( formField.value.length > maxChars )
	{
		formField.value = formField.value.substring(0,maxChars);

		if( charsLeftField != undefined )
		{
			charsLeftField.innerHTML = '0';
		}
	}
	else
	{
		charsLeftField.innerHTML = (maxChars - formField.value.length);
	}
}

/*
*
*	@usage
*	document.getElementsByClass('style');
*	document.getElementsByClass('style','p'); // limit to p tags
*/

function getElementsByClass( searchClass, tag )
{
	var returnArray = [];
	tag = tag || '*';
	var els = document.getElementsByTagName(tag);
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');

	for( var i = 0; i < els.length; i++ )
	{
		if( pattern.test(els[i].className) )
		{
			returnArray.push(els[i]);
		}
	}

	return returnArray;
}

/**
 * A function that will search an element to see if it contains a set class
 */
function containsClass( element, className )
{
	if( undefined != element.className )
	{
		var classArray = element.className.split(" ");
		for( keyVar in classArray )
		{
			if( classArray[keyVar].toLowerCase() == className.toLowerCase() )
			{
				return true;
			}
		}
	}

	return false;
}

/**
 * Used to get page information
 *
 * example: getPageSize().height
 */
function getPageSize()
{
	if( window.innerHeight && window.scrollMaxY )
	{
		// Firefox
		var yWithScroll = window.innerHeight + window.scrollMaxY;
		var xWithScroll = window.innerWidth + window.scrollMaxX - 20;
	}
	else if( document.body.scrollHeight > document.body.offsetHeight )
	{
		// all but Explorer Mac
		var yWithScroll = document.body.scrollHeight;
		var xWithScroll = document.body.scrollWidth;
	}
	else
	{
		// works in Explorer 6 Strict, Mozilla (not FF) and Safari
		var yWithScroll = document.body.offsetHeight;
		var xWithScroll = document.body.offsetWidth;
	}

	return new Object({"width": xWithScroll,"height": yWithScroll});
}

/**
 * Used to get the size of the current viewport
 *
 * example: getViewportSize().height
 */
function getViewportSize()
{
	var viewportwidth;
	var viewportheight;

	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight

	if( typeof window.innerWidth != 'undefined' )
	{
		viewportwidth = window.innerWidth;
		viewportheight = window.innerHeight;
	}

	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

	else if( 	typeof document.documentElement != 'undefined'
				&& typeof document.documentElement.clientWidth != 'undefined'
				&& document.documentElement.clientWidth != 0 )
	{
		viewportwidth = document.documentElement.clientWidth;
		viewportheight = document.documentElement.clientHeight;
	}

	// older versions of IE

	else
	{
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
		viewportheight = document.getElementsByTagName('body')[0].clientHeight;
	}

	return new Object({"width": viewportwidth,"height": viewportheight});
}


/**
 * Get the scrolled position of the window
 *
 * example: getScrollPosition().x
 */
function getScrollPosition()
{
	var scrollY = 0;
	var scrollX = 0;

	if( window.scrollY )
	{
		scrollY = window.scrollY;
	}
	else
	{
		scrollY = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}

	if( window.scrollX )
	{
		scrollX = window.scrollX;
	}
	else
	{
		scrollX = (document.body.parentElement) ? document.body.parentElement.scrollLeft : 0;
	}

	return new Object({"x": scrollX, "y": scrollY});
}


/*
Nice helper javascript to enable you to make some javascript commands any where
in your script and call them once the page has loaded. To use, simply call
onLoad.push() to add a function as an event:

Pass in a function to be run
    onLoad.push(my_onload_function);

Note that onLoad.push() is called with the function itself, not the result of
calling the function!

You also pass a string to be evaluated if your function requires parameters:
    onLoad.push("function_call('one', 2)");

but consider using an anonymous function instead, as this will give you more
immediate feedback on syntax errors:
    onLoad.push(function() { function_call('one', 2); });
*/

function OnloadRunner()
{
	this.push = function( item )
	{
		var fun = this.wrapFunction(item);

		if( typeof window.addEventListener != 'undefined' )
		{
			// standards compliant browsers
			window.addEventListener('load', fun, false);
		}
		else if( typeof document.addEventListener != 'undefined' )
		{
			// opera 7
			document.addEventListener('load', fun, false);
		}
		else if( typeof window.attachEvent != 'undefined' )
		{
			// win/ie
			window.attachEvent('onload', fun);
		}
		else
		{
			//.. mac/ie5 and anything else that gets this far
			if( typeof window.onload == 'function' )
			{
				var existing = window.onload;

				window.onload = function()
				{
					existing();

					fun();
				}
			}
			else
			{
				window.onload = fun;
			}
		}
	}

	/*
	 * Wrap a function (or string to be evaluated) in a try/catch.
	 */
	this.wrapFunction = function( func_or_string )
	{
		return function()
		{
			try
			{
				if( typeof func_or_string == 'function' )
				{
					func_or_string();
				}
				else
				{
					eval(func_or_string);
				}
			}
			catch( ex )
			{
			}
		}
	}
}

var onLoad = new OnloadRunner();

// Load the config that is set using php
require('/framework/javascript/validation.js');

