/*

debug.js -- Simple Yet Powerful Free JavaScript Debugging Tools
Copyright (C) 2005 Cary Wyman (cary@omniumsoftware.com

This library is free software; you can redistribute it and/or modify it under 
the terms of Version 2.1 of the GNU Lesser General Public License as published 
by the Free Software Foundation.

This library is distributed in the hope that it will be useful, but 
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License 
for more details.

You can view a copy of the GNU Lesser General Public License at 
http://www.gnu.org/licenses/lgpl.html; or you can write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
for a copy.

////////////////////////////////////////////////////////////////////////////////

USAGE

debug.js file provides you with two different methods for debugging JavaScript code.  
To use either method, add this file to your web site files and include the 
following line in the <head> section of your page.
  <script src="../debug.js" type="text/javascript"></script>

If necessary, adjust the path to reflect the relative locations of your files 
(eg, src="../debug.js" assumes that debug.js is in the parent of the directory 
containing the file to which you added this line).

Method I

... allows you to write a stream of information to a textarea.

  dbVars(arg1, ... argn)
will write each of its arguments to a textarea, separated by commas.  You may 
specify any number of arguments (zero or more). If any argument is a string 
ending in "=", the separating comma will be omitted and you will see something 
of the form name=value.  Each call to dbVars starts a new line.  When dbVars 
is called from within a function, the name of the calling function will be 
displayed at the beginning of the line.

To use Method I, include these lines somewhere in your <body>.  They will not 
be visible until you start calling dbVars().
  <form action="" id="debugform" style="display:none">
    <h3>Debugging Log</h3>
    <p><textarea cols="120" rows="25" name="debuglog" id="debuglog"></textarea></p>
  </form>

To prevent the textarea growing so large that it slows your browser to a crawl 
(or worse), its size has an upper bound (see dbMaxLength below).  When this limit 
is exceeded, the oldest data is removed (see dbReduceLengthBy below).  Once you 
reach the limit, you will always have at least 7K of data, however.  You can change 
these parameters if you wish.

Method II

... allows you to write anything you like into special paragraphs that appear on your 
web page.  This can be useful if you want to monitor the values of some internal 
parameters (eg, the length of some element or the largest value seen or a mode, etc).

  dbWrite(s, t)
replaces the contents of a debugging paragraph with the 
string s.  s should be either plain text or XHTML text suitable for the interior of 
a paragraph.  The integer t specifies which paragraph should be used (see below).

To use Method II, include one or more lines such as the following somewhere in 
your <body>.  Each paragraph is independent.  The numbers correspond to the t
parameter mentioned above.

  <p id="debug1"></p>
  <p id="debug2"></p>
  <p id="debug3"></p>
*/

var dbMaxLength = 8192;       // upper bound on size of text area (efficiency consideration)
var dbReduceLengthBy = 1024;  // amount to discard when textarea too long
var dbLogVisible = false;

//////////////////////////////// Method I Interface ////////////////////////////////

function dbVars()             // write all arguments to debugging log
{
  var e = dbOpenLog();
  if (e == null) return;        // nothing to write to
  var s = dbFunctionName(dbVars.caller) + ":  ";
  var args = dbVars.arguments;
    // Add all arguments to the current line.  Separate by commas except when an arg ends with "=".
  for (var i = 0; i < args.length; i++)
    s += ((i > 0 && s.charAt(s.length - 1) != "=") ? ", " : "") + args[i];
  dbAppendLine(e,   s);
}

//////////////////////////////// Method II Interface ////////////////////////////////

function dbWrite(s, t)    // write debugging info to specified paragraph
{
  var e = document.getElementById("debug"+t);
  if (e != null) e.innerHTML = s;
}

//////////////////////////////// Utility Functions (Internal) ////////////////////////////////

function dbOpenLog()    // make sure debugging area is visible
{
  if (! dbLogVisible)
  {
    document.getElementById("debugform").style.display="block";
    dbLogVisible = true;
  }
  return document.getElementById("debuglog");
}

function dbFunctionName(f)    // extract function name from caller string
{
  if (f == null) return "document";       // if not called from function, we see null
  var s = f.toString();
  //if (! confirm(s)) stopSnowing();
  if (s.length < 1) return "document";
  var kn = s.indexOf("(");                            // find the (
  while (kn > 0 && s.charAt[kn-1] == " ") kn -= 1;    // back up over blanks
  var k0 = s.lastIndexOf(" ", kn);                    // find blank preceding name
  return s.substring(k0+1, kn);                       // return name
}

function dbAppendLine(e, s)     // add to debugging text area
{                               // don't let it grow beyond dbMaxLength
  e.value += s + "\n";
  if (e.value.length > dbMaxLength)
    e.value = e.value.substring(dbReduceLengthBy, e.value.length);
//dbWrite(e.value.length, 1);
  e.scrollTop = e.scrollHeight;
}

