// JavaScript Document
function setAucTime(which, totalTimers)
{
  //Specify the arrays as display format.
  var maxTime = new Array(60, 60, 24, 30, 12);
  var timeDesc = new Array("s", "m", "o", "z", "M");

  var delay = 1;
  if (totalTimers > 50 && totalTimers < 250)
     delay = 10;
  else if (totalTimers >= 250) 
     delay = 25;
     
  //Extract the innerHTML of the Element, and split it into array of
  //reversed order i.e. seconds are at 0.
  var curTime = document.getElementById('auctime' + which).innerHTML;

  //Extract data between <font style=''>.....</font> tags
  var stIndex = curTime.indexOf(">") + 1;
  var endIndex = curTime.indexOf("<", stIndex);
  curTime = curTime.substring(stIndex, endIndex);

  curTime = curTime.replace(/[^0-9 ]+/g,"");
  if (curTime.charAt(0) == ' ')
     curTime = curTime.substr(1);

  var timeArray = curTime.split(" ");
  timeArray.reverse();

  timeArray[0] -= delay;

  //Length <= 2 considered as the space within <font color=....> tag
  //leads to this.
  //If seconds value of any auction reaches 0, then reload the page from server.
  var FLAG = 0;  if (timeArray.length <= 2 && ((timeArray.length == 1 && timeArray[0] <= 0) || (timeArray[0] == "" && timeArray[1] <= 0)))
  {
      FLAG = 1;
      document.getElementById('auctime' + which).innerHTML = "<font style='font: 13px arial, sans-serif; color:black; background-color: white'><b>CLOSED</b></font>";

      return;
  }

  //Decrement the time correctly to a new value.
  var timeStr = "";
  for (var i=1; i<timeArray.length; i++)
  {
      if (timeArray[i-1] < 0)
      {
         timeArray[i-1] = maxTime[i-1] + timeArray[i-1];
         timeArray[i] -= 1;
      }

      if (i == 1)
          timeStr = timeArray[i-1] + timeDesc[i-1];
      else
          timeStr = timeArray[i-1] + timeDesc[i-1] + " " + timeStr;
  }

  //Append the Highest time unit to output string if and only if its
  //greater than 0.
  //This way we eliminate 0 value displays.
  if (timeArray[timeArray.length-1] > 0)
  {
     if (timeArray.length == 1)
        timeStr = timeArray[timeArray.length-1] + timeDesc[timeArray.length-1];
     else
        timeStr = timeArray[timeArray.length-1] + timeDesc[timeArray.length-1] + " " + timeStr;
  }

  //Blink background of auctions less than 60 min, and
  //Display a changed background for auctions < 60s
  if (timeArray.length == 1 && FLAG == 0)
     timeStr = "<font>" + timeStr + "</font>";
  else if (timeArray.length == 2 && timeArray[1] < 5 && FLAG == 0)
     timeStr = "<font>" + timeStr + "</font>";
  else if (timeArray.length == 2 && FLAG == 0)
     timeStr = "<font>" + timeStr + "</font>";
  else
     timeStr = "<font>" + timeStr + "</font>";

  //Write the new decremented timer value back
  var func = "setAucTime(" + which + ", " + totalTimers + ")";
//  alert(document.getElementById('auctime' + which).innerHTML);
  document.getElementById('auctime' + which).innerHTML = timeStr;
//  alert(document.getElementById('auctime' + which).innerHTML);

setTimeout(func,(delay * 1000));       //Recursive function call at 1, 30 or 60 second intervals
}