var Contador;
if (typeof(Contador)=="undefined")
   Contador=function(Interval)
   {
      this.Register();
      if (typeof(Interval)!="undefined")
         this.Interval=Interval;
      else
         this.Interval=Contador.DefaultUpdateInterval;
      this.TimerHandle=0;
      var d=new Date();
      d=new Date(d.getFullYear(),0,1);
      this.BaseSeconds=Date.parse(d)/1000;
      this.BaseTicks=null;
   }
Contador.BolsasPerSegundo=91;
Contador.DefaultUpdateInterval=100;

Contador.Agregar=function(Interval)
{
   var bc=new Contador(Interval);
   document.write(bc.Html());
   bc.Start();
   return bc;
}

Contador.prototype.Register=function()
{
   if (typeof(Contador.Registry)=="undefined")
      Contador.Registry=new Array();
   this.Index=Contador.Registry.length;
   Contador.Registry[this.Index]=this;
}

Contador.prototype.GetSpanId=function()
{
   return "Contador_"+this.Index;
}

if (typeof(document.getElementById)=="function")
   Contador.prototype.GetSpan=function()
   {
      return document.getElementById(this.GetSpanId());
   }
else
   Contador.prototype.GetSpan=function()
   {
      return document.all[this.GetSpanId()];
   }

Contador.prototype.Html=function()
{
   return "<span id=\""+this.GetSpanId()+"\">"+this.ContadorBolsaStr()+"</span>";
}

Contador.prototype.Start=function(Interval)
{
   this.Stop();
   if (typeof(Interval)!="undefined")
      this.Interval=Interval;
   this.GetBaseTicks();
   this.TimerHandle=window.setInterval("Contador.Registry["+this.Index+"].Update()",this.Interval,"javascript");
}

Contador.prototype.Stop=function()
{
   if (this.TimerHandle!=0)
   {
      window.clearInterval(this.TimerHandle);
      this.TimerHandle=0;
   }
   this.BaseTicks=null;
}

Contador.prototype.GetBaseTicks=function()
{
   if (this.BaseTicks==null)
      return this.SetBaseTicks();
   var t=this.BaseTicks+this.Count*this.Interval;
   if (t<Date.parse(new Date()))
       return this.SetBaseTicks();
   return t;
}

Contador.prototype.SetBaseTicks=function()
{
   this.BaseTicks=Date.parse(new Date());
   this.Count=0;
   return this.BaseTicks;
}

Contador.prototype.Update=function()
{
   this.Count++;
   this.GetSpan().innerHTML=this.ContadorBolsaStr();
}

Contador.prototype.ContadorBolsaStr=function()
{
   var segs=this.GetBaseTicks()/1000-this.BaseSeconds;
   var bolsas=Math.round(segs*Contador.BolsasPerSegundo);
   bolsas=bolsas.toString();
   var s="";
   for (var i=0;i<bolsas.length;i++)
   {
      if (i>0 && i%3==0)
         s=","+s;
      s=bolsas.charAt(bolsas.length-i-1)+s;
   }
   return s;
}
