﻿// JScript File
var timerID 	  	 = null;
var timerRunning  	 = false;
var agora 		  	 = new Date();
var hours 		  	 = agora.getHours();
var minutes 	  	 = agora.getMinutes();
var seconds 	  	 = agora.getSeconds(); 
var nTotSecClient 	 = agora.getHours() * 3600 + agora.getMinutes() * 60 + agora.getSeconds();
var nTotSecServer;
var nDifClientServer;

function TimeToSegundos(nHora,nMinuto,nSegundo){
return(nHora * 3600 + nMinuto * 60 + nSegundo)
}
function HoraSegundos(nSegundos){
return(Math.floor(nSegundos / 3600))
}
function MinutoSegundos(nSegundos){
return( Math.floor((nSegundos - HoraSegundos(nSegundos) * 3600) / 60))
}
function SegundoSegundos(nSegundos){
return((nSegundos % 3600) % 60)
}
function smiddleclock(){
	if(timerRunning)
			clearTimeout(timerID);
	timerRunning = false;
}
function startclock(Hora,Minuto,Segundo) {
    nTotSecServer 	 = Hora * 3600 + Minuto * 60 + Segundo;
    nDifClientServer = nTotSecClient-nTotSecServer;
	// Make sure the clock is smiddleped
	smiddleclock();
	showtime(":");
}
function showtime(separador) {
	var now 	  = new Date();
	var hours 	  = now.getHours();
	var minutes   = now.getMinutes();
	var seconds   = now.getSeconds()
	var timeValue = ((hours < 10) ? "0" : "") + hours
	timeValue += ((minutes < 10) ? ":0" : ":") + minutes
	timeValue += ((seconds < 10) ? ":0" : ":") + seconds
	
	var nTotSegundosClient=TimeToSegundos(hours,minutes,seconds);
	hours=HoraSegundos(nTotSegundosClient-nDifClientServer);
	minutes=MinutoSegundos(nTotSegundosClient-nDifClientServer);
	seconds=SegundoSegundos(nTotSegundosClient-nDifClientServer);
	timeValue = ((hours < 10) ? "0" : "") + hours
	timeValue += ((minutes < 10) ? ":0" : separador) + minutes
	
	document.getElementById("boxRelogio").value=timeValue
	if (separador==" ") {
		separador=":"
	} else {
		separador=" "
	}
	timerID = setTimeout("showtime('"+separador+"')",1000);
	timerRunning = true;
}
