function CountDown(t,m,e,d,f,p) {
  document.write('<div id="'+t+'" style="width: 100%; font-weight: bold; text-align: center">&nbsp;</div>')
  if ((!document.getElementById)||(!document.getElementById(t))) {return}
  this.Running=false
  this.TotalEvents=0
  this.MaxEvents=0
  if (m!=null) {this.MaxEvents=m}
  this.TextLocation=document.getElementById(t)
  this.EventName=new Array()
  this.Position=new Array()
  this.TimeToGo=new Array()
  this.YearsToGo=new Array()
  this.WeeksToGo=new Array()
  this.DaysToGo=new Array()
  this.HoursToGo=new Array()
  this.MinutesToGo=new Array()
  this.SecondsToGo=new Array()
  this.DisplayAs=new Array()
  if (e!=null) {this.AddNewEvent(e,d,f,p)}
}
 
CountDown.prototype.AddNewEvent=function(e,d,f,p) {
  this.EventName[this.TotalEvents]=e
  var dt=d.split(' ')
  if (dt.length==1) {this.TimeToGo[this.TotalEvents]=dt[0]}
  else {this.TimeToGo[this.TotalEvents]=this.SetDateTime(dt[0],dt[1])}
  this.YearsToGo[this.TotalEvents]=0
  this.WeeksToGo[this.TotalEvents]=0
  this.DaysToGo[this.TotalEvents]=0
  this.HoursToGo[this.TotalEvents]=0
  this.MinutesToGo[this.TotalEvents]=0
  this.SecondsToGo[this.TotalEvents]=0
  if (f!=null) {this.DisplayAs[this.TotalEvents]=f}
  else {this.DisplayAs[this.TotalEvents]='ywdhms'}
  if (p!=null) {this.Position[this.TotalEvents]=p}
  else {this.Position[this.TotalEvents]=0}
  this.TotalEvents++
  this.SortEvents()
  this.Running=true
}
 
CountDown.prototype.SortEvents=function() {
  if (this.TotalEvents<1) {return}
  var n,t,p
  for (i=0;i<this.TotalEvents-1;i++) {
    for (x=i+1;x<this.TotalEvents;x++) {
      if ((this.Position[x]-this.Position[i])<0) {
        n=this.EventName[x]
        t=this.TimeToGo[x]
        p=this.Position[x]
        this.EventName[x]=this.EventName[i]
        this.TimeToGo[x]=this.TimeToGo[i]
        this.Position[x]=this.Position[i]
        this.EventName[i]=n
        this.TimeToGo[i]=t
        this.Position[i]=p
      }
    }
  }
}
 
CountDown.prototype.SetDateTime=function(d,t) {
  var dt=d.split("/")
  var tm=t.split(":")
  var td=new Date()
  var rt=new Date()
  rt.setFullYear(dt[2],dt[1]-1,dt[0])
  rt.setHours(tm[0],tm[1],tm[2],0)
  return Math.round((rt.getTime()-td.getTime())/1000)
}
 
CountDown.prototype.FormatPeriod=function(v,n,i) {
  if ((v==0)&&(this.DisplayAs[i].search(n.toUpperCase().substr(0,1))<0)) {return ''}
  var z=''
  if ((this.DisplayAs[i].search(/00/)>=0)&&(v<10)) {z='0'}
  if (v==1) {return ', '+z+v+' '+n}
  else {return ', '+z+v+' '+n+'s'}
}
 
CountDown.prototype.CalcPeriod=function() {
  var t=0
  var s=''
  for (i=0;i<this.TotalEvents;i++) {
    t=Math.abs(this.TimeToGo[i])
    s=this.DisplayAs[i]
    if (s.toLowerCase().search(/y/)>=0) {this.YearsToGo[i]=Math.floor(t/31536000)}
    else {this.YearsToGo[i]=0}
    t=t-(this.YearsToGo[i]*31536000)
    if (s.toLowerCase().search(/w/)>=0) {this.WeeksToGo[i]=Math.floor(t/604800)}
    else {this.WeeksToGo[i]=0}
    t=t-(this.WeeksToGo[i]*604800)
    if (s.toLowerCase().search(/d/)>=0) {this.DaysToGo[i]=Math.floor(t/86400)}
    else {this.DaysToGo[i]=0}
    t=t-(this.DaysToGo[i]*86400)
    if (s.toLowerCase().search(/h/)>=0) {this.HoursToGo[i]=Math.floor(t/3600)}
    else {this.HoursToGo[i]=0}
    t=t-(this.HoursToGo[i]*3600)
    if (s.toLowerCase().search(/m/)>=0) {this.MinutesToGo[i]=Math.floor(t/60)}
    else {this.MinutesToGo[i]=0}
    t=t-(this.MinutesToGo[i]*60)
    if (s.toLowerCase().search(/s/)>=0) {this.SecondsToGo[i]=t}
    else {this.SecondsToGo[i]=0}
  }
}
 
CountDown.prototype.SubtractSecond=function(s) {
  var flg=0
  for (i=0;i<this.TotalEvents;i++) {
    this.TimeToGo[i]=this.TimeToGo[i]-s
    if (this.TimeToGo[i]<0) {flg++}
  }
  if (flg==this.TotalEvents) {this.Running=false}
}
 
CountDown.prototype.ShowResults=function() {
  var thisobj=this
  if (this.Running) {
    setTimeout(function(){thisobj.ShowResults()}, 1000)
    this.CalcPeriod()
    var cd=''
    var x=0
    var ev=''
    var mx=0
    for (i=0;i<this.TotalEvents;i++) {
      if (this.TimeToGo[i]>=0) {
        cd=this.FormatPeriod(this.YearsToGo[i],'year',i)+this.FormatPeriod(this.WeeksToGo[i],'week',i)+this.FormatPeriod(this.DaysToGo[i],'day',i)+this.FormatPeriod(this.HoursToGo[i],'hour',i)+this.FormatPeriod(this.MinutesToGo[i],'minute',i)+this.FormatPeriod(this.SecondsToGo[i],'second',i)
        if (cd=='') {cd='  Now!'}
        else {cd=cd+'.'}
        cd=cd.substr(2)
        x=cd.lastIndexOf(",")
        if (x>0) {cd=cd.substr(0,x)+' and'+cd.substr(x+1)}
        if (ev!='') {ev+='<br>'}
        ev+=this.EventName[i]+' '+cd
        mx++
      }
      if (mx==this.MaxEvents) {break}
    }
    this.SubtractSecond(1)
  }
  else {
    ev='No Countdown Event'
  }
  this.TextLocation.innerHTML=this.formatresults(ev)
}
 
CountDown.prototype.Padding=function(v) {
  if (v<10) {return '0'+v}
  return v
}
 
CountDown.prototype.DisplayTime=function(functionref) {
  this.formatresults=functionref
  this.ShowResults()
}
 
function ReturnArray(a,s) {
  return a.split(s)
}
 
function FormatResults() {
  var d=arguments[0]
  return d
}

