function Countdown(name, updateFrequency)
{
  this.name = name;
  this.updateFrequency = updateFrequency;
  this.images = null;
  this.endDate = new Date();
  this.format = (document.getElementById && document.getElementById(this.name)) ? document.getElementById(this.name).innerHTML : '';
}

Countdown.prototype.setEndDate = function(year, month, day, hour, minute, second, milliseconds)
{
  this.endDate = new Date(year, month - 1, day, ((hour) ? hour : 0), ((minute) ? minute : 0), ((second) ? second : 0), ((milliseconds) ? milliseconds : 0));
};

Countdown.prototype.start = function()
{
  this.update();
  setInterval(this.name + '.update()', (this.updateFrequency ? this.updateFrequency : 1000) );
};

Countdown.prototype.update = function()
{
  var now = new Date();
  var difference = this.endDate - now;

  var days    = parseInt(difference / 86400000) + "";
  var hours   = parseInt((difference % 86400000) / 3600000) + "";
  var minutes = parseInt((difference % 3600000) / 60000) + "";
  var seconds = parseInt((difference % 60000) / 1000) + "";
  var milliseconds = parseInt(difference % 1000) + '';
   
  if (isNaN(days) || days.charAt(0) == '-') days = '0';
  if (isNaN(hours) || hours.charAt(0) == '-') hours = '0';
  if (isNaN(minutes) || minutes.charAt(0) == '-') minutes = '0';
  if (isNaN(seconds) || seconds.charAt(0) == '-') seconds = '0';
  if (isNaN(milliseconds) || milliseconds.charAt(0) == '-') milliseconds = '0';

  // Make milliseconds to a single digit
  milliseconds = milliseconds.charAt(0);

  if (document.getElementById && document.getElementById(this.name))
  {
    var html = this.format;
    html = html.replace(/~d~/, days);
    html = html.replace(/~h~/, hours);
    html = html.replace(/~m~/, minutes);
    html = html.replace(/~s~/, seconds);
    html = html.replace(/~ms~/, milliseconds);
    document.getElementById(this.name).innerHTML = html;
  }
}