/*
Author: Robert Hashemian
http://www.hashemian.com/

You can use this code in any manner so long as the author's
name, Web address and this disclaimer is kept intact.
********************************************************
Usage Sample:

<script language="JavaScript">
TargetDate = "12/31/2020 5:00 AM";
BackColor = "palegreen";
ForeColor = "navy";
CountActive = true;
CountStepper = -1;
LeadingZero = true;
DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
FinishMessage = "It is finally here!";
</script>
<script language="JavaScript" src="http://scripts.hashemian.com/js/countdown.js"></script>
*/
var BackColor, ForeColor, TargetDate, DisplayFormat, CountActive, FinishMessage, CountStepper, LeadingZero, oID,SetTimeOutPeriod



function calcage(secs, num1, num2) 
{
    s = ((Math.floor(secs/num1))%num2).toString();
    //alert('secs=' + secs + '*num1=' + num1 + '*num2=' + num2 + '**='+ s);
    if (LeadingZero && s.length < 2)
        s = "0" + s;
    //return "<b>" + s + "</b>";
    return s
}

function CountBack(oid,secs)
{
    
    //alert(oid)
    if (secs < 0)
    {
        document.getElementById(oid).innerHTML = FinishMessage();
        return;
    }
    
    oDate = calcage(secs,86400,86400);
    
    // if oDate is 0 then 0 days
    if (oDate == 00){
        // displaying no day format
        DisplayStr = DisplayFormat.replace(/%%D%%/g, '');
        // display seconds
        DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60) + ' secs');
    }else{
        // multiple days left
        //DisplayStr = DisplayFormat.replace(/%%D%%/g, calcage(secs,86400,100000) + ' days');
        DisplayStr = DisplayFormat.replace(/%%D%%/g, calcage(secs,86400,86400) + ' days');
        DisplayStr = DisplayStr.replace(/%%S%%/g, '');
    }
    DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,24) + ' hrs');
    DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs,60,60) + ' mins');
    

    document.getElementById(oid).innerHTML = DisplayStr;
    if (CountActive)
        setTimeout("CountBack('" + oid + "'," + (secs+CountStepper) + ")", SetTimeOutPeriod);
}

function putspan(oid, backcolor, forecolor)
{
    document.write("<span id='" + oid + "' style='background-color:" + backcolor + 
                "; color:" + forecolor + "'></span>");
}

function getAuctionTimer(oTargetDate,sID,serverCurrentTime,isPutSpan)
{
      //BackColor = "white";
    //ForeColor= "black";
      //alert(sID)
      TargetDate = oTargetDate; //"12/31/2020 5:00 AM";
      DisplayFormat = "%%D%% %%H%% %%M%% %%S%%";
      CountActive = true;
      FinishMessage = function() { return "Game Finished"; };
      CountStepper = -1;
      LeadingZero = true;
      oID = sID
    
    CountStepper = Math.ceil(CountStepper);
    if (CountStepper == 0)
      CountActive = false;
    SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 990;

    if (isPutSpan == 1) {
    putspan(oID, BackColor, ForeColor); 
    }
    
    
    var dthen = new Date(TargetDate);
    var dnow = new Date(serverCurrentTime);
    if(CountStepper>0)
    {
      ddiff = new Date(dnow-dthen);      
    }
    else
    {
        ddiff = new Date(dthen-dnow);
    }
    gsecs = Math.floor(ddiff.valueOf()/1000);
    CountBack(oID,gsecs);
}

function getAuctionOpenTimer(oTargetDate,sID,serverCurrentTime)
{
    getAuctionTimer(oTargetDate,sID,serverCurrentTime);
    FinishMessage = function() 
    {
        window.location.reload();
        return "Game Live"; 
    };
}