function pause(millisecondi)
{
    var now = new Date();
    var exitTime = now.getTime() + millisecondi;

    while(true)
    {
        now = new Date();
        if(now.getTime() > exitTime) return;
    }
}
var timerID = 0;
var tStart  = null;
var tStart1  = null;
var stop = 0;
function stringtime(secs, num1, num2)
{
  s = ((Math.floor(secs/num1))%num2).toString();
  if (s.length < 2) s = "0" + s;
  return (s);
}

function UpdateTimer() {
   if (stop == 1){return false;}
   time_ = tStart.getTime();
   time_ = time_ + 1000;

   time_1 = tStart1.getTime();
   time_1 = time_1 - 1000;

   tStart.setTime(time_);
   tStart1.setTime(time_1);

   h1 = tStart.getHours();
   h1 = stringtime(h1,1,24);
   m1 = tStart.getMinutes();
   m1 = stringtime(m1,1,60);
   s1 = tStart.getSeconds();
   s1 = stringtime(s1,1,60);
   h2 = tStart1.getHours();
   h2 = stringtime(h2,1,24);
   m2 = tStart1.getMinutes();
   m2 = stringtime(m2,1,60);
   s2 = tStart1.getSeconds();
   s2 = stringtime(s2,1,60);

   if (document.getElementById('time_now') == null){
   return false;
   }
   document.getElementById("time_now").innerHTML = h1 + ":" + m1 + ":" + s1;
   document.getElementById("q_time").value = h2 + ":" + m2 + ":" + s2;
   document.getElementById("time_left").innerHTML = h2 + ":" + m2 + ":" + s2;   

   document.getElementById("t_h").value=h2;
   document.getElementById("t_m").value=m2;
   document.getElementById("t_s").value=s2;
   document.getElementById("t_h1").value=h1;
   document.getElementById("t_m1").value=m1;
   document.getElementById("t_s1").value=s1;

   if (h2=="00" && m2=="00" && s2=="00"){
   Stop();
   } 
   timerID = setTimeout("UpdateTimer()", 1000);
}


function Start(h, m, s, h1, m1, s1) {

   h = stringtime(h,1,24);
   m = stringtime(m,1,60);
   s = stringtime(s,1,60);

   h1 = stringtime(h1,1,24);
   m1 = stringtime(m1,1,60);
   s1 = stringtime(s1,1,60);
   tStart = new Date(1970, 2, 24, h1, m1, s1);
//   tStart1 = new Date();
//   time = tStart.getTime();
//   if (h!=0){
//   time = h * m * 60 * 1000;
//   }
//   else{
//   time = m * 60 * 1000;
//   }
   tStart1 = new Date(1970, 2, 24, h, m, s);
//   tStart1.setTime(time);
   i = 0;
   j = 0;
   document.getElementById("time_now").innerHTML = h1 + ":" + m1 + ":" + s1;
   document.getElementById("time_left").innerHTML = h + ":" + m + ":" + s;
   if (document.getElementById("time_left").innerHTML!="00:00:00" && document.getElementById("time_left").innerHTML!=""){
	   UpdateTimer();
   }
}

function Stop() {
  if(timerID) {
      clearTimeout(timerID);
      timerID  = 0;
   }
   stop =1;
   document.getElementById("time_now").innerHTML = "out of time";
   document.getElementById("time_left").innerHTML = "00:00:00";
   document.getElementById("q_time").value = "off";
}
