<!--
/***************************
Timer Object
***************************/
// This collection will save the timer objects for the setTimeout calls
var oColTimers = new Object;

function CTimer() {
/* --CTimer object-- */
     /* --CTimer object properties-- */
    this.rTimer = null;
    this.isTicking = false;
    this.lcSecPerDay = 24 * 60 * 60;
    this.lcSecLeft = 0;
    this.lcDays = 0;
    this.lDays = 0;
    this.lcHours = 0;
    this.lHours = 0;
    this.lcMinutes = 0;
    this.lMinutes = 0;
    this.lSeconds = 0;
    this.sTime = '';
    this.sID = (new Date()).getTime() + '' + Math.floor(Math.random() * 100);
    oColTimers[this.sID] = this;
    
    /* --CTimer object methods-- */
    this.setSecondsLeft = CTimer_setSecondsLeft;
    this.startTicking = CTimer_startTicking;
    this.stopTicking = CTimer_stopTicking;
    this.getTime = CTimer_getTime;
    this.getIsTicking = CTimer_getIsTicking;

    /* --CTimer object callback methods-- */
    this.callback = null;

    /* --CTimer can allow multiple constructors 
      and therefore checks the recieved arguments-- */
    switch (CTimer.arguments.length)
    {
        case 0:
            // Do Nothing;
            break;
        case 1: 
            if ('number' == typeof(CTimer.arguments[0])) // Only one string parameter
            {
                // Only seconds left recieved
                this.setSecondsLeft(CTimer.arguments[0]);
            } else if ('function' == typeof(CTimer.arguments[0])) // Only one function parameter
            {
                // Only callback recieved
                this.callback = CTimer.arguments[0];
            }
            break;
        case 2:
            if ('number' == typeof(CTimer.arguments[0]) && 'function' == typeof(CTimer.arguments[1])) // Two string parameters
            {
                // seconds left and callback recieved
                this.setSecondsLeft(CTimer.arguments[0]);
                this.callback = CTimer.arguments[1];   
                this.startTicking();
            } else if (('function' == typeof(CTimer.arguments[0]) && 'number' == typeof(CTimer.arguments[1]))) // One string & one function parameter
            {
                // callback and seconds left recieved
                this.callback = CTimer.arguments[0];
                this.setSecondsLeft(CTimer.arguments[1]);
                this.startTicking();
            }
            break;
        default:
            alert("Can't find a matching constructor for the suplied parameters!!!");
            break;
    }
}

/* --Set the seconds left.-- */
function CTimer_setSecondsLeft(sec)
{
    var bShouldRestart = this.isTicking;
    
    if ('number' == typeof(sec))
    {
        this.stopTicking();
        
        if (1 < sec)
            this.lcSecLeft = sec;
        else
            this.lcSecLeft = 1;
            
        this.lcDays = this.lcSecLeft / this.lcSecPerDay;
        this.lDays = Math.floor(this.lcDays);
        this.lcHours = (this.lcDays - this.lDays) * 24;
        this.lHours = Math.floor(this.lcHours);
        this.lcMinutes = (this.lcHours - this.lHours) * 60;
        this.lMinutes = Math.floor(this.lcMinutes);
        this.lSeconds = Math.floor((this.lcMinutes - this.lMinutes) * 60);
        
        if (bShouldRestart)
            this.startTicking();
    }
}

/* --Start the timer ticks.-- */
function CTimer_startTicking()
{
    this.isTicking = true;
    this.sTime = '';
	   
    if (0 == this.lSeconds)
    {
        if (0 == this.lMinutes)
        {
            if (0 == this.lHours)
            {
                if (0 < this.lDays)
                {
                    this.lDays --;
                }
                this.lHours = 23;
            } else {
                this.lHours --;
            }
            this.lMinutes = 59;
        } else {
            this.lMinutes --;
        }
        this.lSeconds = 59;
    } else {
        this.lSeconds --;
    }
    
    if (1 < this.lDays)
        this.sTime = this.lDays + ' Days ';
    else if (0 < this.lDays)
        this.sTime = this.lDays + ' Day ';
        
    if (10 > this.lHours)
        this.sTime = this.sTime + '0';
        
    this.sTime = this.sTime + this.lHours + ':';
    
    if (10 > this.lMinutes)
        this.sTime = this.sTime + '0';
    
    this.sTime = this.sTime + this.lMinutes + ':';
    
    if (10 > this.lSeconds)
        this.sTime = this.sTime + '0';
    
    this.sTime = this.sTime + this.lSeconds;
           
    // The apply method on a given function allows us to call a function and specify 
    // what the keyword "this" will refer to within the context of that function. 
    // The first argument should be an object to which the keyword "this" will refer 
    // to within the context of that function. The second argument to the apply method 
    // is an array. The elements of this array will be passed as the arguments to the 
    // function being called. The array parameter can be either an array literal or 
    // the deprecated arguments property of a function.
    // So what I have to do is to only call the apply on the callback function
    // with the xmlhttp object and the parameters array that had been passed to the 
    // current method. This way, the keyword "this" inside the callback function will 
    // refer to the xmlhttp object and the result can be retrieved by using "this.responseText" 
    // and the parameters array will be passed as the arguments to the function being called.
    
    // Check if we have a callback.
    if (this.callback)
    {
        try
        {
            // Apply parameters and call the error callback function
            this.callback.apply(this, this.startTicking.arguments);
        } catch (e)
        {
            // Do Nothing;
        }   
    }
    
    if (0 == this.lDays && 0 == this.lHours && 0 == this.lMinutes && 0 == this.lSeconds)
        this.stopTicking();
    else
        this.rTimer = window.setTimeout('oColTimers[' + this.sID + '].startTicking()', 1000);
}

/* --Stop the timer ticks.-- */
function CTimer_stopTicking()
{
    if (this.isTicking && this.rTimer)
        clearTimeout(this.rTimer);
}

/* --Get the timer ticks.-- */
function CTimer_getTime()
{
    return this.sTime;
}

/* --Get the is ticking flag.-- */
function CTimer_getIsTicking()
{
    return this.isTicking;
}
// -->
