﻿/************************************** Splendid *************************************
 * Created By:		Steve Doggett
 * Creation Date:	21st August 2007
 * Edited ----------------------------------------------------------------------------
 *      By:               On:
 * Description -----------------------------------------------------------------------
 *      A Debug object to aid with deugging Javascript projects
 *
 *      Objects:
 *         Debug(traceflags):
 *              Write(comment)
 *              WriteLine(comment)
 *              Assert(condition, comment)
 *              Trace(traceflag, comment)
 *              StartTrace(traceflag, comment)
 *              StartTimer(traceflag)
 *              WriteLapTime(traceflag, comment)
 *              EndTimer(traceflag)
 *          NoDebug(traceflags):
 *              Write(comment)
 *              WriteLine(comment)
 *              Assert(condition, comment)
 *              Trace(traceflag, comment)
 *              StartTrace(traceflag, comment)
 *              StartTimer(traceflag)
 *              WriteLapTime(traceflag, comment)
 *              EndTimer(traceflag)
 *
 **************************************************************************************/

/*************************************** Globals **************************************/
/****
 * Debug trace flags
 ****/
var TRACE_GENERAL       = 0;
var TRACE_DETAILED      = 10;

/************************************** Functions *************************************/

/****
 * Debug object
 * Creates a reporting window to write into.
 ****/ 
function Debug(traceflags)
{
       this.windowDebug = window.open("", "debugWindow", 'top=0, left=0, width=1000, height=800, scrollbars=yes, resizable=yes');       
       this.TraceFlags = traceflags;  
       this.StartTime = null;        
        
       // Write Method
       this.Write = function(comment)
       {
            try
            {
                this.windowDebug.document.write(comment);
                this.windowDebug.scrollTo(0,100000);
                this.windowDebug.focus();
            }
            catch(err){}
       }
       
       // WriteLine method
       this.WriteLine = function(comment)
       {
            this.Write(comment+"<br>");
            this.windowDebug.focus();
       }       
       
       // Assert method
       this.Assert = function(condition, comment)
       {
            if (!condition)
            {
                this.Write("ASSERT "+comment);
                alert("ASSERT : "+comment);                    
            }
       }
       
       // Trace method
       this.Trace = function(traceflag, comment)
       {
            if (this.TraceFlags == traceflag)
                this.WriteLine(comment);
       }

       // Trace method
       this.StartTrace = function(traceflag, comment)
       {
            if (this.TraceFlags == traceflag)
            {
                this.WriteLine('');
                this.WriteLine('STARTING NEW TRACE...');
                this.WriteLine(comment);
            }
       }
       
       // Start a code execution timer method
       this.StartTimer = function(traceflag)
       {
            if( this.TraceFlags == traceflag )
            {
                this.StartTime = new Date();
                this.WriteLine('Starting the code execution timer');
            }
       }
       
       // Write the lap time for the code execution timer method
       this.WriteLapTime = function(traceflag, comment)
       {
            if( this.TraceFlags == traceflag )
            {
                var endTime = new Date();
                this.WriteLine(endTime.getTime()-this.StartTime.getTime()+'ms: '+comment);
            }
       }
       
       // End the code execution timer method
       this.EndTimer = function(traceflag)
       {
            if( this.TraceFlags == traceflag )
            {
                var end = new Date().getTime();
                this.WriteLine('Execution Time: '+(end-this.StartTime.getTime())+'ms');
                this.StartTime = null;
            }
       }
}

/****
 * no debug functionality
 ****/
function NoDebug(traceflags)
{
       // Write Method
       this.Write = function(comment) {}
       
       // WriteLine method
       this.WriteLine = function(comment) {}       
       
       // Assert method
       this.Assert = function(condition, comment) {}
       
       // Trace method
       this.Trace = function(traceflag, comment) {}

       // Start a new Trace method
       this.StartTrace = function(traceflag, comment) {}

       // Start a code execution timer method
       this.StartTimer = function(traceflag) {}

       // Write the lap time for the code execution timer method
       this.WriteLapTime = function(traceflag, comment) {}

       // End the code execution timer method
       this.EndTimer = function(traceflagt) {}
}

/****
 * Adds a basic error handler for uncaught errors
 ****/
window.onerror = function (err, file, line) 
{
    alert('ERROR: The following error occured:\n\n' + err + '\n' +'In file: ' + file + '\n' +'At line: ' + line);
    return true;
}