/*
js_console

Date:       2006-03-21
Author:     Wai Yip Tung
URL:        http://tungwaiyip.info/software/js_console.html

This script insert a javascript console into your web page. It allow you
to test out javascript interactively in the context of the web page.
Simply add this tag to your web page:

    <script src='js_console.js'></script>

*/
/*
Copyright (c) 2006, Wai Yip Tung
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.
* Neither the name Wai Yip Tung nor the names of its contributors may be
  used to endorse or promote products derived from this software without
  specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


var MAX_OUTPUT = 15
var output = Array();

function print(s) {
    // add s to the output array
    output.push(s);
    if (output.length > MAX_OUTPUT) {
        output.shift();
    }
    var o = output.join('\n');

    // set text in TEXTAREA
    var console_output = document.getElementById('console_output');
    console_output.value = o;
    // don't work in Opera?
    console_output.scrollTop = 9999;
}

function run() {
    var console_input = document.getElementById('console_input');
    var command = console_input.value;
    print('>> ' + command);
    var r = null;
    try {
        r = eval(command);
    }
    catch (e) {
        alert(e.message, e.description);
    }
    if (r != null) {
        print(r.toString());
    }
    console_input.focus();
    console_input.select();
    return false;
}

// insert HTML form
var FORM = "\
<form onsubmit='return run()'>\
  <textarea id='console_output' cols='80' rows='15' >\
  </textarea>\
  <p>Javascript <input id='console_input' type='text' size='80' />\
  <input type='submit' value='Eval' xonclick='javascript:run();'/>\
  </p>\
</form>\
";
document.write(FORM);

