// Load the config that is set using php
require('/vibe/jscript/config.php');
require('/vibe/jscript/validation.js');
//require('/vibe/jscript/alert.js');

function create_XMLHttpRequest()
{
	if (window.XMLHttpRequest)
	{
		return new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		return false;
	}
}


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 = curtop = 0;

	if (obj.offsetParent)
	{
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent)
		{
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}



/**
	Search through the document and find any class that match

	@param string *searchClass
	@param string node
	@param string tag

	return array
*/
function getElementsByClass(searchClass,node,tag)
{
	var classElements = new Array();

	if( node == null )
	{
		node = document;
	}

	if( tag == null )
	{
		tag = '*';
	}

	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");

	for (i = 0, j = 0; i < elsLen; i++)
	{
		if ( pattern.test(els[i].className) )
		{
			classElements[j] = els[i];
			j++;
		}
	}

	return classElements;
}


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 destroyElement(element)
{
	if( element )
	{
		if( element.parentNode )
		{
			element.parentNode.removeChild(element);
		}
	}
}

function devAlert(message)
{
	if( DEV_SERVER )
	{
		alert(message, 'DEV ALERT');
	}
}



/*
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 init_functions array with:

init_functions[init_functions.length] = "function_call('one', 2);";
*/
init_functions = new Array();
function initialise_functions()
{
	var index;

	for( index in init_functions )
	{
		try
		{
			eval(init_functions[index]);
		}
		catch( ex )
		{
			devAlert("init_functions Failed: " + init_functions[index] + "\nError Message:" + ex.message + "\non line " + ex.lineNumber + " in file " + ex.fileName);
		}
	}
}

window.onload = initialise_functions;

