/*******************************************************************************
Taken from phpied.com
URL: http://www.phpied.com/javascript-include/

*******************************************************************************/


//==============================================================================
// Function: include
// A simple function to include a JavaScript file callable from JavaScript
//
// Note: Using this function it is possible to include the same external javascript multiple times.
//       For a solution, see the include_once function below.
//
// Parameters:
//	o	sJSFile	-> STRING: name of the external Javascript file, including the
//						path BUT excluding the extension as it is assumed to be .JS
// Returns:
//		None
function include(sJSFile)
{
	document.write("<script src=\""+sJSFile+".js\" type=\"text/javascript\"><\/script>");
}
//------------------------------------------------------------------------------

/*
//==============================================================================
// Function: include
//
// Parameters:
//
// Returns:
//
function include(sTemplate)
{
	return document.write("<script language=\"JavaScript\" type=\"text/javascript\" src=\"" + sTemplate + "\"><\/script>");
}
//------------------------------------------------------------------------------
*/

//==============================================================================
// Function: include_dom
// The DOM way of including an external javascript file.
//
// Note: This has the same problem as the include function above.
//       Also, it doesn't appear to work in Safari (http://www.robrohan.com/client/index.cfm?mode=entry&entry=FC0DA501-A92B-330D-FA47D1B7D144B356)
//
// Parameters:
//	o	sJSFile	-> STRING: name of the external Javascript file, including the
//						path BUT excluding the extension as it is assumed to be .JS
//
// Returns:
//	o	BOOLEAN	-> Always FALSE
function include_dom(script_filename)
{
	var html_doc = document.getElementsByTagName('head').item(0);
	var js = document.createElement('script');
	try
	{
		js.setAttribute('language', 'javascript');
		js.setAttribute('type', 'text/javascript');
		js.setAttribute('src', script_filename + ".js");
		html_doc.appendChild(js);
	} 
	catch(err) 
	{	
	}
	return false;
}
//------------------------------------------------------------------------------


//##############################################################################

// Include only once per page
var included_files = new Array();

//==============================================================================
// Function: include_once
// Include an external Javascript file but only if it hasn't already been included
//
// Parameters:
//	o	script_filename	- STRING: name of the external Javascript file,
//								including the path BUT excluding the
//								extension as it is assumed to be .JS
//
// Returns:
//		NONE
function include_once(script_filename)
{
	try
	{
		if (!in_array(script_filename, included_files))
		{
			included_files[included_files.length] = script_filename;
	//		include_dom(script_filename);
			include(script_filename);
		}
	} 
	catch(err) 
	{	
	}
}
//------------------------------------------------------------------------------


//==============================================================================
// Function: in_array
// Search an array for an item
//
// Parameters:
//	o	needle		-> The item to find in the array
//	o	haystack	-> The array to be searched
//
// Returns:
//	o	BOOLEAN		-> True if in the array, False if not
function in_array(needle, haystack)
{
	try
	{
		for (var i = 0; i < haystack.length; i++)
		{
			if (haystack[i] == needle)
			{
				return true;
			}
		}
	} 
	catch(err) 
	{	
	}
    return false;
}
//------------------------------------------------------------------------------

