/* Copyright 2006 Livesite Networks, LLC. All rights reserved. */
/* Written by: Ryan Gies */

/*------------------------------------------------------------------------------
 * include - Include another javascript file on this server.
 * @param name of the package to import
 * @param path (optional) path to script directory
 *
 * Use the javascript variable JAVASCRIPT_ROOT to load scripts from another 
 * location than current document's location.
 *
 * This function allows dependencies to reside inside the .js scripts
 * themselves. The name is translated as:
 *  a) dots (.) become forward slashes (/);
 *  b) '.js' is appended.
 *----------------------------------------------------------------------------*/

function include(name) {
  var url;
  if (defined('JAVASCRIPT_ROOT')) {
    url = JAVASCRIPT_ROOT;
  } else {
    url = new String(window.location);
    url = url.replace(/\/[^\/]*$/,'');
  }
  url += '/' + name.replace(/\./g,'/') + ".js";
  document.write('<script type="text/javascript" src="'+url+'"></script>');
}

/*------------------------------------------------------------------------------
 * defined - Return true if the name is a javascript variable
 * @param name of the variable (remember to quote it)
 *
 * Determine if an object is defined in the current scope.  This method safely 
 * navigates the name so you won't get javascript errors if the parents are
 * missing.
 *----------------------------------------------------------------------------*/

function defined(name) {
  var objects = name.split(".");
  var address_path = new String();
  for( var i = 0; i < objects.length; i++ ) {
    if( address_path.length > 0 ) {
        address_path += ".";
    }
    address_path += objects[i];
    var address_result = eval("typeof(" + address_path + ")");
    if( address_result == 'undefined') {
      return false;
    }
  }
  return true;
}

