////////////////////////////////////////////////////////////////////////
//
// Filename: debug.js
// Purpose : Debug tools
// Method  : xhtml
//
// Author  : J.van.der.Steen@gobase.org
// Date    : 2010-03-07
//
////////////////////////////////////////////////////////////////////////

/**
 * Emit supplied text in debug panel
 */
function debug(text)
{
    Element.update('debug', text);
}

/**
 * Append supplied text to debug panel
 */
function debugAdd(text)
{
    Element.insert( 'debug', { bottom: text } );
}

/**
 * Return a human readable form of the content of supplied variable
 */
function print_r(obj, html, limit, depth)
{
    html  = html  ? html  : false;
    limit = limit ? limit : 99;
    depth = depth ? depth :  0;

    var returnString = '';

    if (html) {
        returnString += '<OL>';
    }

    for (var prop in obj)
    {
        // Property domConfig isn't accessible
        if (prop != 'domConfig')
        {
            returnString += ''
                         + (html ? '<LI>'      : '')
                         + (html ? '<STRONG>'  : '')
                         + prop
                         + (html ? '</STRONG>' : '')
                         + ' '
                         + (html ? '<SMALL>'   : '')
                         + '(' + (typeof obj[prop]) + ')'
                         + (html ? '</SMALL>'  : '')
                         ;

            if (typeof obj[prop] == 'number' || typeof obj[prop] == 'boolean') {
                returnString += ': '
                             + (html ? '<EM>'  : '')
                             + obj[prop]
                             + (html ? '</EM>' : '')
                             ;
            }
            if (typeof obj[prop] == 'string' && obj[prop]) {
                returnString += ': '
                             + (html ? '<DIV style="background:#C9C9C9;border:1px solid black; overflow:auto;">' : '')
                             + (html ? '<CODE>'  : '')
                             + "'"
                             + obj[prop].replace(/</g, '&amp;lt;').replace(/>/g, '&amp;gt;')
                             + "'"
                             + (html ? '</CODE>' : '')
                             + (html ? '</DIV>'  : '')
                             ;
            }

            if ((typeof obj[prop] == 'object') && (depth < limit)) {
                if (html === false) {
                    returnString += ' { ';
                }
                returnString += print_r(obj[prop], html, limit, (depth + 1));
                if (html === false) {
                    returnString += ' } ';
                }
            }

            if (html) {
                returnString += '</LI>';
            } else {
                returnString += ";";
            }
        }
    }
    if (html) {
        returnString += '</OL>';
    }

    if (false && depth === 0)
    {
        var winpop;
        winpop = window.open("", "","width=800,height=600,scrollbars,resizable");
        winpop.document.write('<PRE>' + returnString + '</PRE>');
        winpop.document.close();
    }       

    return returnString;
}

