var SecondsPerDay = 86400;
var SecondsPerHour = 3600;
var Timer = new Array();
var timerCNT;
var secondsLeft;
var serverTime;// = document.getElementById("servertime").value;
//var Timer_nm = document.getElementById("timerfield").value;
var onComplete;
function setupTimer(Today, End_dt, Timer_nm, Format, onTimerCompleted) {
    // Ending time of the contest EST
    var TargetDate, CurDate;
    
    TargetDate = End_dt; //new Date(2008, 10, 20);//("29 September, 2008 7:55");

    if(timerCNT == null)
        timerCNT = 0;
    else
        timerCNT++;
        
    // CurDate should come from the server, as the time on the computer may be incorrect
    CurDate = new Date(Today);
    secondsLeft = parseInt((TargetDate.getTime() - CurDate.getTime()) / 1000);
    
    //TimerTick();
    //setTimeout(TimerTick, 1000);
    //,
    Timer[timerCNT] = new PBTimer(null, serverTime, TargetDate ,secondsLeft, Timer_nm, Format);
    //var i
    //Timer.StartTimer();
    onComplete = onTimerCompleted;
    if(timerCNT == 0)
        setInterval(TimerTick, 1000);
    //setTimeout(function(){TimerTick(obj)}, 1000);
    
}

function stopTimer(myinterval) {
           clearInterval(myinterval);
}

// New Class decalaration
function PBTimer(element, ServerTime, Target_dt, SecondsLeft, Timer_nm, Format) { 
 this.ServerTime = ServerTime; 
 this.Target_dt = Target_dt; 
 this.SecondsLeft = SecondsLeft; 
 this.Timer_nm = Timer_nm; 
 this.element = element;
 this.format = Format;
 this.StartTimer(this); //StartTimer(this.ServerTime, this.Target_dt, this.SecondsLeft, this.Timer_nm);
} 

PBTimer.prototype.ServerTime = new Date();
PBTimer.prototype.Target_dt = new Date();
PBTimer.prototype.SecondsLeft = 0;
PBTimer.prototype.Timer_nm = "";
PBTimer.prototype.element;
PBTimer.prototype.format = "long";

//PBTimer.prototype = { 
// ServerTime: new Date(), 
// Target_dt: new Date(), 
// SecondsLeft: 0, 
// Timer_nm: "", 
// myinterval: Object,
// element: null
//}

 PBTimer.prototype.StartTimer = function(obj, format) { 
//   this.ServerTime = ServerTime; 
//   this.Target_dt = Target_dt;
//   this.SecondsLeft = SecondsLeft;
//   this.Timer_nm = Timer_nm;
   //this.AddIt();
   //obj.myinterval = setInterval(this.TimerTick, obj, 1000);
   //alert(this.myinterval
 } 
 
 function TimerTick() {
    var i;
    
    for (i=0; i <= timerCNT; i++) {
        UpdateTimer(i);
    }
    
 }
 
 
function UpdateTimer(cnt) {
    var days, hours, minutes, seconds,TotalSeconds;
    var obj = Timer[cnt];
    obj.SecondsLeft--;
    TotalSeconds = obj.SecondsLeft;
    if (TotalSeconds > 0){            
        days = parseInt(TotalSeconds / SecondsPerDay);
        TotalSeconds = TotalSeconds - (days * SecondsPerDay);
        hours = parseInt(TotalSeconds / SecondsPerHour);
        TotalSeconds = TotalSeconds - (hours * SecondsPerHour);
        minutes = parseInt(TotalSeconds / 60);
        TotalSeconds = TotalSeconds - (minutes * 60);
        seconds = TotalSeconds;
        
        if(obj.format == "long"){
            var sDays = days == 1 ? "day" : "days";
            var sHours = hours == 1 ? "hour" : "hours";
            var sMinutes = minutes == 1 ? "minute" : "minutes";
            var sSeconds = seconds == 1 ? "second" : "seconds"; 
        
            document.getElementById(obj.Timer_nm).innerHTML = "Time Left: " +  days + " " + sDays + ", " + hours + " " + sHours + ", " + minutes + " " + sMinutes + ", " + seconds + " " + sSeconds;
        }else if(obj.format == "short"){
            if(seconds < 10){ seconds = "0" + seconds;}
            if(minutes < 10){ minutes = "0" + minutes;}
            if(hours < 10){ hours = "0" + hours;}
        
            document.getElementById(obj.Timer_nm).innerHTML = "Time Left: " + days + "d " + hours + "h " + minutes + "m " + seconds + "s";           
        }
    }
    else{
       document.getElementById(obj.Timer_nm).innerHTML = "Contest Over!!!"; 
        if( typeof (onComplete) == 'function')
            onComplete();
       }
}