// Load the config that is set using php
require('/jscript/config.php');
require('/jscript/validation.js');



function create_XMLHttpRequest()
{
	if (window.XMLHttpRequest)
	{
		return new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		return false;
	}
}

/**
 * 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 (usually a <span> element),
 * a count of how many characters can still be entered will be automatically displayed
 * inside that element.
 *
 * @param field_id				-- the id of the form field to limit the max characters on
 * @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
 * @param exclude_line_breaks 	-- if true, excludes line breaks from the count of total characters (defaults to false if ommitted)
 */
function checkMaxChars(field_id, charsleft_text_id, maxChars, exclude_line_breaks)
{
	var formField = document.getElementById(field_id);
	var length_of_line_breaks = 0;

	// Attempt to access the <span> element that contains the count of the number of characters left to enter.
	if( charsleft_text_id.length > 0 )
	{
		var charsLeftField = document.getElementById(charsleft_text_id);
	}


	var linebreak_count = 0;

	var check_text = formField.value.replace(/\r/g, "");

	charsLeftField.innerHTML = maxChars - check_text.length;
}

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];
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
	    {
	    c_start=c_start + c_name.length+1;
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    }
	  }
	return "";
}

/**
	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);
	}
}

/**
 * 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:
 *
 * onLoad.push("function_call('one', 2);");
 *
 * The onLoad array should be initialised with these lines in your HTML <head>:
 * <script type="text/javascript"> //<![CDATA[
 * 	var onLoad = new Array();
 * //]]> </script>
 */
function initialise_functions()
{
	var index;

	if( "undefined" != typeof onLoad )
	{
		for( index in onLoad )
		{
			try
			{
				eval(onLoad[index]);
			}
			catch( ex )
			{
				devAlert("init_functions Failed: " + onLoad[index] + "\nError Message:" + ex.message + "\non line " + ex.lineNumber + " in file " + ex.fileName);
			}
		}
	}
}

// Set the above function to be run when the page finishes loading
window.onload = initialise_functions;


function hover(path, height)
{
	if( !height )
	{
		height = "300px";
	}

	window.scroll(0,0);
	document.documentElement.style.overflow = 'hidden';
    var blocking_el = document.createElement('div');
    blocking_el.setAttribute('id', "hbf_hover_blockingEl");
    blocking_el.style.backgroundColor = '#FFFFFF';
    blocking_el.style.opacity = 0.80;
    blocking_el.style.filter = 'alpha(opacity=80)';
    blocking_el.style.position = 'absolute';
    blocking_el.style.width = '100%';
    blocking_el.style.height = '100%';
    blocking_el.style.top = '0px';
    blocking_el.style.left = '0px';
    document.body.appendChild(blocking_el);


    var iframe_el = document.createElement('iframe');
    iframe_el.setAttribute('id', 'hbf_hover');
    iframe_el.setAttribute('name', 'hbf_hover');
    iframe_el.setAttribute('scrolling', false);
    iframe_el.style.width = '540px';
    iframe_el.style.height = height;
    iframe_el.style.border = '0px';
    iframe_el.style.background = 'white';
    iframe_el.style.backgroundColor = '#f4f3f3';
    iframe_el.frameBorder = '0';
    iframe_el.style.styleFloat = 'left';
    iframe_el.style.top = '30%';
    iframe_el.style.left = '30%';
    iframe_el.style.position = 'absolute';
    document.body.appendChild(iframe_el);

    var iframe_src = path;

    iframe_el.src = iframe_src;
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}


function hoverClose()
{
	var blocking_el = parent.document.getElementById("hbf_hover_blockingEl");
	if( blocking_el )
	{
		blocking_el.parentNode.removeChild(blocking_el);
	}

	parent.document.documentElement.style.overflow = 'auto';

	var initiative_el = parent.document.getElementById("hbf_hover");
	if( initiative_el )
	{
		initiative_el.parentNode.removeChild(initiative_el);
	}

	document.documentElement.style.overflow = '';
}

