Function.prototype.Timer = function (interval, calls, onend) {
  var count = 0;
  var payloadFunction = this;
  var startTime = new Date();
  var callbackFunction = function () {
    return payloadFunction(startTime, count);
  };
  var endFunction = function () {
    if (onend) {
      onend(startTime, count, calls);
    }
  };
  var timerFunction =  function () {
    count++;
    if (count < calls && callbackFunction() != false) {
      window.setTimeout(timerFunction, interval);
    } else {
      endFunction();
    }
  };
  timerFunction();
};

function leadingzero (number) {
    return (number < 10) ? '0' + number : number;
}
function countdown (start, seconds, target) {
  if (!border) {
    var border = seconds;
    if (start >= seconds) {
      var border_style= 'color: #B00300;';
    }
    seconds = start;
  }
  var element = document.getElementById(target);
  var calculateAndShow = function () {
    if (seconds >= 0) {
      var h = Math.floor(seconds / 3600);
      var m = Math.floor((seconds % 3600) / 60);
      var s = seconds % 60;
      element.innerHTML= '<span style="'+border_style+'">' + 
        leadingzero(h) + ':' +
        leadingzero(m) + ':' +
        leadingzero(s) + '</span>';
      seconds++;
      if (seconds == border) {
	border_style = 'color: #B00300;';
      }
    } else {
      return false;
    }
  };
  var completed = function () {
    element.innerHTML = "Zeit abgelaufen";
    alert(target + ' abgelaufen!');
  };
  calculateAndShow.Timer(1000, Infinity, completed);
}


