
   /**
    *  In order to use this script, there must be a reference to it above any other scripts calling it.  Currently,
    *  there are references placed in the top level tiles.  Next create a file named loggingConfig.js in the
    *  javascript directory under web.  In this file, put your statements for creating loggers for various pages.
    *  Below is an example of a few lines that could be put into the file:
    *
    *       createLogger(LogLevels.debug, "CDM_Web");
    *       createLogger(LogLevels.info, "CDM_Web/login.do");
    *
    *  The script has its own logger and log level.  To change the log level of the script do the following:
    *
    *       setScriptLogLevel(LogLevels.debug);
    *
    *  This would turn logging on at a debug level for all pages that didn't have a specified url.  The default setting
    *  for the root logger is LogLevels.error.
    *
    *  The above statments should never be in a function.  You can put as many statments as necessary.
    *  To actually log a message, use a method reflecting the log level you wish to use.  For example, to log
    *  using a log level of debug, you could do the following.  The first statement will log to the window using
    *  default colors.  The second will set foreground and background colors of the message.
    *
    *       function logTest()
    *       {
    *           logDebug("test login");
    *           logDebugWithColors("test login", "Green", "lightBlue");  //Note: do not use lightGray as IE fails with it.
    *       }
    *
    *
    *
    *  I have tested this with IE6 and Fire Fox.
    **/

   var messageDocument;
   var messageBody
   var messageFrame;
   var logWindow;
   var loggers = new Array();
   var LogLevels = new LogLevelObject();
   var scriptLogger = new JsLogger(LogLevels.error, "");

   /**
   * Does all of the setup for the logger.
   **/
   function setUpLogWindow()
   {
    if(!window.top.logWindow)
    {
      logWindow = window.open("", "logWindow", "width=470,height=400");

      if(logWindow.frames.length == 0)
      {
        writeFrameSet(logWindow.document);
      }
      else
      {
        messageFrame = logWindow.frames[0];
        messageDocument = messageFrame.document;
        messageBody = messageDocument.body;
      }
    }
   }

   /**
   * Logs the message if the page logger has been set to debug.
   **/
   function logDebug(message)
   {
        logMessage(getPageLogger(), LogLevels.debug, message);
   }

   /**
   * Logs the message if the page logger has been set to debug.  Uses the colors to hilite the message.
   **/
   function logDebugWithColors(message, foreground, background)
   {
       logMessageWithColors(getPageLogger(), LogLevels.debug, message, foreground, background);
   }

   /**
   * Logs the message if the page logger has been set to info or higher.
   **/
   function logInfo(message)
   {
       logMessage(getPageLogger(), LogLevels.info, message);
   }

   /**
   * Logs the message if the page logger has been set to info or higher.  Uses the colors to hilite the message.
   **/
   function logInfoWithColors(message, foreground, background)
   {
       logMessageWithColors(getPageLogger(), LogLevels.info, message, foreground, background);
   }

   /**
   * Logs the message if the page logger has been set to warn or higher.
   **/
   function logWarn(message)
   {
        logMessage(getPageLogger(), LogLevels.warn, message);
   }

   /**
   * Logs the message if the page logger has been set to warn or higher.  Uses the colors to hilite the message.
   **/
   function logWarnWithColors(message, foreground, background)
   {
       logMessageWithColors(getPageLogger(), LogLevels.warn, message, foreground, background);
   }

   /**
   * Logs the message if the page logger has been set to error or higher.
   **/
   function logError(message)
   {
       logMessage(getPageLogger(), LogLevels.error, message);
   }

   /**
   * Logs the message if the page logger has been set to error or higher.  Uses the colors to hilite the message.
   **/
   function logErrorWithColors(message, foreground, background)
   {
       logMessageWithColors(getPageLogger(), LogLevels.error, message, foreground, background);
   }

   /**
   * Logs the message if the page logger has been set to fatal or higher.
   **/
   function logFatal(message)
   {
      logMessage(getPageLogger(), LogLevels.fatal, message);
   }

   /**
   * Logs the message if the page logger has been set to fatal or higher.  Uses the colors to hilite the message.
   **/
   function logFatalWithColors(message, foreground, background)
   {
       logMessageWithColors(getPageLogger(), LogLevels.fatal, message, foreground, background);
   }


   /**
   * Writes a message and applies the given style.
   **/
   function logMessage(pageLogger, logLevel, message)
   {
     if(messageDocument && pageLogger != null && pageLogger.logLevel >= logLevel)
     {
     	writeMessage(message, "black", "white");
     }
   }

   /**
   * Writes a message and applies the given style.
   **/
   function logMessageWithColors(pageLogger, logLevel, message, foreground, background)
   {
     if(messageDocument && pageLogger != null && pageLogger.logLevel >= logLevel)
     {
     	writeMessage(message, foreground, background);
     }
   }
   
   /**
    * Writes the message to the window.
    **/
    function writeMessage(message, foreground, background){
    	var messageNode = messageDocument.createTextNode(message);

        var divElement = messageDocument.createElement("DIV");
        divElement.appendChild(messageNode);
        divElement.style.background = background;
        divElement.style.color = foreground;
        messageBody.appendChild(divElement);
        messageFrame.window.scrollTo(0, messageBody.scrollHeight);
    }

   /**
   * Gets the first matching logger for this page.
   **/
   function getPageLogger()
   {
      var baseUrl = window.location.protocol + "//" + window.location.host;
      var hrefText = window.location.href;
      var logger = null;

      logMessage(scriptLogger, LogLevels.debug, "hrefText: " + hrefText);

      for(var i = 0; i < loggers.length; i++)
      {
         var loggerResource = loggers[i].resource;

         if(!(loggerResource.charAt(0) == '/'))
         {
            loggerResource = "/" + loggerResource;
         }

         loggerResource = baseUrl + loggerResource;

         logMessage(scriptLogger, LogLevels.debug, "loggerResource: " + loggerResource);

         if(hrefText.indexOf(loggerResource) == 0)
         {
            if(logger == null || loggers[i].resource.length > logger.resource.length)
            {
               logger = loggers[i];
            }
         }
      }

      return logger;
   }

   /**
   * Creates the document for the message frame of the log window.  This method is called from the onload event of the
   * frameset of the log window.
   **/
   function initMessageFrameElements(frame)
   {
      messageFrame = frame;
      messageDocument = frame.document;
      messageBody = messageDocument.body;
   }

   /**
   * Creates the base document for the log window.
   **/
   function writeFrameSet(document)
   {
    document.open();

    document.writeln("<html>");
    document.writeln("<head>");
    document.writeln("<title>");
    document.writeln("JavaScript Log Window");
    document.writeln("</title>");
    document.writeln("</head>");
    document.writeln("<FRAMESET rows='90%, 10%' border='0' onload='opener.initMessageFrameElements(messageFrame);opener.writeButtonFrame(buttonFrame.document)'>");
    document.writeln("  <FRAME src='' name='messageFrame' scrolling='auto' frameborder='0' noresize>");
    document.writeln("  <FRAME src='' name='buttonFrame' frameborder='0' scrolling='no' noresize>");
    document.writeln("  <NOFRAMES>");
    document.writeln("    This document contains frames and your browser does not support them.");
    document.writeln("  </NOFRAMES>");
    document.writeln("</FRAMESET>");

    document.close();
   }

   /**
   * This method will create the document for the button bar at the bottom of the log window.
   * Current, it is called from the onload event of the frameset of the log window.
   **/
   function writeButtonFrame(document)
   {

    document.open();

    document.writeln("<html>");
    document.writeln("<head>");
    document.writeln("<title>");
    document.writeln("JavaScript Log Window Button Bar");
    document.writeln("</title>");
    document.writeln("<script language='JavaScript'>");
    document.writeln("function clearMessages()");
    document.writeln("{");
    document.writeln("   var document = parent.window.messageFrame.document;");
    document.writeln("   var documentBody = parent.window.messageFrame.document.body;");
    document.writeln("   clearChildren(documentBody);");
    document.writeln("}");
    document.writeln(" ");
    document.writeln("function clearChildren(parentNode)");
    document.writeln("{");
    document.writeln("   var length = parentNode.childNodes.length;");
    document.writeln("   for(var i = 0; i < length;i++)");
    document.writeln("   {");
    document.writeln("        var node = parentNode.childNodes[0];");
    document.writeln("        if(node.hasChildNodes)");
    document.writeln("        {");
    document.writeln("          clearChildren(node);");
    document.writeln("        }");
    document.writeln("        parentNode.removeChild(node);");
    document.writeln("   }");
    document.writeln("}");
    document.writeln("</script>");
    document.writeln("</head>");
    document.writeln("<body>");
    document.writeln("<form name='logOutputForm'>");
    document.writeln("<center><input type='button' value='Clear' onclick='clearMessages();'></center>");
    document.writeln("</form>");
    document.writeln("</body>");
    document.writeln("</html>");

    document.close();
   }

   /**
   * Create a logger with default foreground and background styles.
   *  logLevel - a value from the LogLevels object
   *  resource - a string representing the resource to log
   **/
   function createLogger(logLevel, resource)
   {
      loggers[loggers.length] = new JsLogger(logLevel, resource);
      setUpLogWindow();
   }


   /**
   * Constructor for the JsLogger object.
   *  logLevel - a value from the LogLevels object
   *  resource - a string representing the resource to log
   *  foreground - text color to use when logging a message
   *  background - the background color to use when logging a message
   **/
   function JsLogger(logLevel, resource)
   {
      this.logLevel = logLevel;
      this.resource = resource;
   }

   /**
   * Creates the log level object.
   **/
   function LogLevelObject()
   {
      this.debug = 4;
      this.info = 3;
      this.warn = 2;
      this.error = 1;
      this.fatal = 0;
   }

   /**
    * Returns the root logger
    **/
   function setScriptLogLevel(logLevel)
   {
     scriptLogger.logLevel = logLevel;
     setUpLogWindow();
   }



