﻿function Timer() {
    this.timerArray = new Array();

    this.registerTimer = function(timerName) {
        if (!this.timerArray.contains(timerName)) {
            this.timerArray[this.timerArray.length] = timerName;
        }
    }

    this.timerHandle = 0;

    this.callBack = function() { };

    this.remove = function() {
        window.clearTimeout(this.timerHandle);
    }

    this.tick = function() {
        timerend = false;

        for (i = 0; i < this.timerArray.length; i++) {
            timername = this.timerArray[i];
            sec = Number(document.getElementById(timername + "sec").value);
            min = Number(document.getElementById(timername + "min").value);
            hour = Number(document.getElementById(timername + "hour").value);

            sec--;
            if (sec == -01) {
                sec = 59;
                min = min - 1;
            } else {
                min = min;
            }

            if (min == -01) {
                min = 59;
                hour = hour - 1;
            } else {
                hour = hour;
            }

            if (sec <= 9) { sec = "0" + sec; }

            time = (hour <= 9 ? "0" + hour : hour) + ":" + (min <= 9 ? "0" + min : min) + ":" + sec + "";

            if (document.getElementById) { document.getElementById(timername).innerHTML = time; }

            if (hour == '0' && min == '0' && sec == '00') {
                sec = "00";

                timerend = true;
            }

            document.getElementById(timername + "sec").value = sec;
            document.getElementById(timername + "min").value = min;
            document.getElementById(timername + "hour").value = hour;
        }
        if (timerend == false) {
            this.timerHandle = window.setTimeout("Timer.tick()", 1000);
        } else {
            this.callBack();
        }
    }
}

var Timer = new Timer();