/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var itemIdArray = new Array();
var endDate = new Array();
var endDateB = new Array();
var nowDateA=new Date();
var dnow    = new Date();
var dateTag=0;
 var tmpData=new Array();
function startTime()
{
    var i = 0;
   
//    alert(nowDateA);
     var dateTmp = new Date(nowDateA.substr(6,4),nowDateA.substr(3,2),nowDateA.substr(0,2),nowDateA.substr(11,2),nowDateA.substr(14,2),nowDateA.substr(17,2));
//     var dateTmp =new Date();
//    var mnth    = dateTmp.getMonth();
//    var yr  =dateTmp.getFullYear();
//    if(mnth==12)
//          {
//            yr=yr+1;
//            mnth = 1;
//          }
//          else
//              mnth=mnth+1;
//          alert(dateTag);
    if(dateTag==0)
    {
//        dnow = new Date(yr,mnth,dateTmp.getDate(),dateTmp.getHours(),dateTmp.getMinutes(),dateTmp.getSeconds());
        dnow = dateTmp;
        dateTag=1;
    }
//    else
//    var dnow = new Date(dateTmp);
//    alert(dnow);
    var newTime ='';
    var aBidMessage = '';
    for(i=0; i<endDate.length;i++)
    {
//        alert(endDate[i]);
        TargetDate = new Date(endDate[i].substr(6,4),endDate[i].substr(3,2),endDate[i].substr(0,2),endDate[i].substr(11,2),endDate[i].substr(14,2),endDate[i].substr(17,2));

        BackColor = "palegreen";
        ForeColor = "navy";
        CountActive = true;
        CountStepper = -1;
        LeadingZero = true;
        DisplayFormat = "%%D%%d %%H%%h %%M%%m %%S%%s";

        CountStepper = Math.ceil(CountStepper);
        if (CountStepper == 0)
            CountActive = false;
        var dthen = new Date(TargetDate);
        if(dnow>=dthen)
            {
                    call_ajax(itemIdArray[i],i);
//                    alert(tmpData[i]);
                    if(tmpData[i])
                    {
                        endDate[i]=tmpData[i].substr(0,19);
                        aBidMessage=tmpData[i].substr(19,tmpData[i].length);
                        document.getElementById('a_bid_'+i).innerHTML=aBidMessage;
                    }
                    if(aBidMessage=='auction closed')
                    newTime =aBidMessage;
                else
                    newTime ='calculating...';
//                alert(dnow);
//                alert(dthen);
//                location.reload();
            }
            else
            {
                if(CountStepper>0)
                    ddiff = new Date(dnow-dthen);
                else
                    ddiff = new Date(dthen-dnow);
                gsecs = Math.floor(ddiff.valueOf()/1000);
                newTime =CountBack(gsecs);
            }
        document.getElementById('end_time_'+i).innerHTML=newTime;
    }
//    alert(document.getElementById('end_time_b_0'));
    if(document.getElementById('end_time_b_0'))
    {
         for(i=0; i<endDateB.length;i++)
        {
            TargetDate = new Date(endDateB[i].substr(6,4),endDateB[i].substr(3,2),endDateB[i].substr(0,2),endDateB[i].substr(11,2),endDateB[i].substr(14,2),endDateB[i].substr(17,2));
            BackColor = "palegreen";
            ForeColor = "navy";
            CountActive = true;
            CountStepper = -1;
            LeadingZero = true;
            DisplayFormat = "%%D%%d %%H%%h %%M%%m %%S%%s";

            CountStepper = Math.ceil(CountStepper);
            if (CountStepper == 0)
                CountActive = false;
            var dthen = new Date(TargetDate);
            if(dnow>=dthen)
            {
                   call_ajax(itemIdArray[i],i);
                   if(tmpData[i])
                       {
                        endDateB[i]=tmpData[i].substr(0,19);
                        aBidMessage=tmpData[i].substr(19,tmpData[i].length);
                        document.getElementById('a_bid_'+i).innerHTML=aBidMessage;
                    }
                    if(aBidMessage=='auction closed')
                    newTime =aBidMessage;
                else
                    newTime ='calculating...';
//                alert(dnow);
//                alert(dthen);
//                location.reload();
            }
            else
            {
//                location.reload();
                if(CountStepper>0)
                    ddiff = new Date(dnow-dthen);
                else
                    ddiff = new Date(dthen-dnow);
                gsecs = Math.floor(ddiff.valueOf()/1000);
                newTime =CountBack(gsecs);
            }
            document.getElementById('end_time_b_'+i).innerHTML=newTime;
        }
    }
    dnow.setSeconds(dnow.getSeconds()+1);
//    alert(gsecs);
    t=setTimeout('startTime()',1000);
}

function calcage(secs, num1, num2)
{
    s = ((Math.floor(secs/num1))%num2).toString();
    if (LeadingZero && s.length < 2)
        s = "0" + s;
    return "<b>" + s + "</b>";
}

function CountBack(secs)
{
    DisplayStr = DisplayFormat.replace(/%%D%%/g, calcage(secs,86400,100000));
    DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,24));
    DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs,60,60));
    DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60));
    return DisplayStr;
}

function updateClock ( )
{
//     var dateTmp =new Date();
     var dateTmp =dnow;
    var mnth    = dateTmp.getMonth();
    var yr  =dateTmp.getFullYear();
    if(mnth==12)
          {
            yr=yr+1;
            mnth = 1;
          }
          else
              mnth=mnth+1;
    var currentTime = new Date(yr,mnth,dateTmp.getDate(),dateTmp.getHours(),dateTmp.getMinutes(),dateTmp.getSeconds())

  var currentHours = currentTime.getHours ( );
  var currentMinutes = currentTime.getMinutes ( );
  var currentSeconds = currentTime.getSeconds ( );
//currentTime.getDate()
  // Pad the minutes and seconds with leading zeros, if required
  currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
  currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;

  // Choose either "AM" or "PM" as appropriate
  var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";

  // Convert the hours component to 12-hour format if needed
  currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;

  // Convert an hours component of "0" to "12"
  currentHours = ( currentHours == 0 ) ? 12 : currentHours;

  // Compose the string for display
  var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;

  // Update the time display
  document.getElementById("clock").firstChild.nodeValue = currentTimeString;
}


