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


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

/*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.setEndDate = function(end) {
this.endDate = end;
  //alert(this.now+'/'+this.endDate);

};

Countdown.prototype.update = function() {
   

  // var now = new Date();
	this.now++;
 //  var now = this.now;
   var difference = this.endDate - this.now;
   
   var days    = parseInt(difference / 86400) + '';
   var hours   = parseInt((difference % 86400) / 3600) + '';
   var minutes = parseInt((difference % 3600) / 60) + '';
   var seconds = parseInt((difference % 60) / 1) + '';
   var milliseconds = parseInt(difference % 1) + '';
   
   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';
		if(days<10) days="0".concat(days);
		if(hours<10) hours="0".concat(hours);
		if(minutes<10) minutes="0".concat(minutes);
		if(seconds<10) seconds="0".concat(seconds);
	
	
	if (this.format != '') {
      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;
      }
   }
};

