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



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 require(filename)
{
	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 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 devAlert(message)
{
	if( is_dev_profile() )
	{
		alert(message);
	}
}


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)
{
    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 put the
function calls into the onLoad array with:

You can either pass a string to be evaled
onLoad.push("function_call('one', 2)");

or

Pass in a function to be run
onLoad.push(alert('test'));

*/

if( !onLoad ) var onLoad = new Array();

function onLoad_function()
{
	var index;

	for( index in onLoad )
	{
		try
		{
			if( typeof(onLoad[index]) == 'function' )
			{
				// The object is a function so run it.
				onLoad[index]();
			}
			else
			{
				// The object is a string eval it.
				eval(onLoad[index]);
			}
		}
		catch( ex )
		{
			devAlert("init_functions Failed: " + onLoad[index] + "\nError Message:" + ex.message + "\non line " + ex.lineNumber + " in file " + ex.fileName);
		}
	}
}

// Load the current contents of window.onload into the onLoad array;
if( window.onload != 'undefined')
{
	onLoad.push(window.onload);
}

// Wait a split second before loading anything.
window.onload = onLoad_function;
