function jstimer(strdate,htmlid) {
//defaults
year="2068"; month="1"; day="1"; hour="00"; min="00"; sec="00"; tz_hours = -8; //PST
date_and_time = strdate.split(' ');
if (typeof(date_and_time[0]) != 'undefined' && date_and_time[0] != '') {
date_only = date_and_time[0].split('-');
year = date_only[0] || year;
month = date_only[1] || month ;
day = new String(date_only[2] || day) ;
}
if (typeof(date_and_time[1]) != 'undefined' && date_and_time[1] != '') {
time_only = date_and_time[1].split(':');
hour = time_only[0] || hour;
min = time_only[1] || min;
sec = time_only[2] || sec;
}
if (typeof(date_and_time[2]) != 'undefined' && date_and_time[2] != '') {
tz_hours = date_and_time[2];
}
this.to_date = new Date();
this.to_date.setFullYear(year,month-1,day);
this.to_date.setHours(hour);
this.to_date.setMinutes(min);
this.to_date.setSeconds(sec);
var tz_offset_client = -(this.to_date.getTimezoneOffset()*60*1000); //getTimezoneOffset: minutes between the time on the current machine and UTC
var tz_offset = (tz_hours*60*60*1000);
var msecs = this.to_date.getTime();
this.to_date.setTime(msecs - tz_offset + tz_offset_client);
var myDays=["Sun","Mon","Tue","Wed","Thu","Fri","Sat","Sun"];
var myMonths=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
dow = myDays[this.to_date.getDay()];
moy = myMonths[this.to_date.getMonth()];
this.outformat='%days% days, %hours% hours, %mins% minutes ('+dow + ' ' + moy + day+')';
this.htmlid=htmlid;
this.start = start;
}
function start() {
display_countdown(this.outformat,set_countdown(this.to_date),this.htmlid);
}
function set_countdown(to_date) {
var from_date = new Date();
// return initial difference in seconds at start
return Math.floor((to_date - from_date)/1000);
}
function display_countdown(outformat,countdn,spanid) {
if (countdn <= 1) {
document.getElementById(spanid).innerHTML = ":Countdown Completed";
}
else {
var secs = countdn % 60;
if (secs < 10) secs = '0'+secs;
var countdn1 = (countdn - secs) / 60;
var mins = countdn1 % 60;
if (mins < 10) mins = '0'+mins;countdn1 = (countdn1 - mins) / 60;
var hours = (countdn1 % 24);
var days = (countdn1 - hours) / 24;
var outhtml = new String(outformat);
outhtml = outhtml.replace(/%days%/,days);
outhtml = outhtml.replace(/%hours%/,hours);
outhtml = outhtml.replace(/%mins%/,mins);
outhtml = outhtml.replace(/%secs%/,secs);
if ( days < 2 ) {
outhtml = outhtml.replace(/days/,'day');
}
document.getElementById(spanid).innerHTML = outhtml;
setTimeout('display_countdown(\''+ outformat + '\','+(countdn-1)+',\''+spanid+'\');',999);
}
}
if (isJsEnabled()) {
addLoadEvent(countdown_auto_attach);
}
//need span with id=uniqueid, class="countdown", innerhtml="somedate somehour (8-5-2006 12)"
function countdown_auto_attach() {
var spans = document.getElementsByTagName('span');
for (var i = 0; span = spans[i]; i++) {
if (span && hasClass(span, 'countdowntimer')) {
var timer = new jstimer(span.innerHTML,span.getAttribute('id'));
timer.start();
}
}
}