// $Header: /HBI Websites/_client_common/js/include.js 3     12/18/02 12:28 Matthew.van.eerde $


/* include.js
 * 8/26/2002 by Matthew van Eerde
 *
 * This allows for dynamic inclusion of function definition scripts
 *
 * Put a function definition script (say, is_valid_date.js) in
 * a predefined folder (/_client_common/js/include/)
 *
 * If include.js is included, form validation function definition
 * scripts can ask for is_valid_date.js to be included by calling:
 *
 * // This line must exist outside of the function
 * // so the browser can download the is_valid_date.js file
 * // before validate_form calls is_valid_date
 * include("is_valid_date");
 *
 * function validate_form(this)
 * {	return is_valid_date(form.datefield.value)
 * }
 *
 *
 * Each function definition should begin with a line of the form
 * include_me("is_valid_date");
 *
 */

// private variable - shouldn't be queried directly
// does need to be global though
var included_keys = new Array;

// legacy variable until this is SOP
var inclusion_enabled = true;

// debug variable
var debug = false;

// just in case someone decides to call include("include")
include_me("include");

function is_included(key)
{	var is_key_included = false;

	if (debug)
	{	alert("is_included(" + key + ")");
	}

	for (var i = 0; i < included_keys.length; i++)
	{	if (included_keys[i] == key)
		{	is_key_included = true;
			break;
		}
	}

	return is_key_included;
}

function include_me(key)
{	var i = 0;

	if (debug)
	{	alert("include_me(" + key + ")");
	}

	if (is_included(key))
	{	alert("Error - attempt to include_me(" + key + ") which is already included!");
	} else
	{	i = included_keys.length;
		included_keys[i] = key;
	}

	return i;
}

function include(key)
{	if (debug)
	{	alert("include(" + key + ")");
	}

	if (is_included(key))
	{	// no need to do anything - file included.
	} else
	{	document.writeln("<script type=\"text/javascript\" src=\"/_client_common/js/" + key + ".js\">");
		document.writeln("</script>");
	}
}
