/****************************************************************************/
/* stopwatch.js                   (C) Copyright 1999 by Calocybe Creations  */
/*                                                                          */
/* Simple stopwatch object                                                  */
/*                                                                          */
/* Compatibility: Unknown, assume JavaScript1.1                             */
/*                                                                          */
/* This version: 1999-08-15 / 1736                                          */
/*                                                                          */
/* You may use, copy or redistribute this code for your own purposes        */
/* as long as this copyright notice remains intact. You may also modify the */
/* code, provided that you insert unambiguous notices stating that you      */
/* changed the code.                                                        */
/*                                                                          */
/* Authors contact: calocybe@geocities.com                                  */
/*                                                                          */
/****************************************************************************/

/* constants  (or what would be constants in C, respectively) */
var SW_STATUS_PAUSING = 0;
var SW_STATUS_RUNNING = 1;



function CStopWatch() {
    this.Start = CStopWatch_Run;
    this.Stop = CStopWatch_Stop;
    this.Reset = CStopWatch_Reset;
    this.GetElapsedTime = CStopWatch_GetElapsedTime;
    this.GetElapsedSeconds = CStopWatch_GetElapsedSeconds;

    this.Reset();
}



function CStopWatch_Reset() {
    this.status = SW_STATUS_PAUSING;
    this.elapsed = 0;
    this.starttime = 0;
}



function CStopWatch_Run() {
    var now;

    if (this.status == SW_STATUS_PAUSING) {
        now = new Date();
        this.starttime = now.getTime()
        this.status = SW_STATUS_RUNNING;
    }
}



function CStopWatch_Stop() {
    var now;

    if (this.status == SW_STATUS_RUNNING) {
        now = new Date();
        this.elapsed += now.getTime() - this.starttime;
        this.starttime = 0;
        this.status = SW_STATUS_PAUSING;
    }
}


function CStopWatch_GetElapsedTime() {
    return (new Date(this.GetElapsedSeconds()));
}


function CStopWatch_GetElapsedSeconds() {
    var now;

    if (this.status == SW_STATUS_RUNNING) {
        now = new Date();
        return this.elapsed + now.getTime() - this.starttime;
    } else if (this.status == SW_STATUS_PAUSING)  return this.elapsed;

    return null;
}


