/*
Codice javascript per visualizzare ora del server (richiamata con xmlHttpRequest) e del client

Va aggiunto:
1)
<script type="text/javascript" src="./js/oggetto_xmlhttp.js"></script> (nell'head)
2)
window.onload=function(){
	onload_orario();
}
3)
nel body un contenitore (span) con id "servertime" e un altro contenitore con id "hosttime"
*/

var url_orario = "ora_server.php"; // The server-side script
var http_orario = getHTTPObject();

function handleHttpResponse_orario() {
	if (http_orario.readyState == 4) {
		if(http_orario.status == 200 || http_orario.status == 304 || http_orario.status == 307){
			//document.getElementById('orario_div').innerHTML=http_orario.responseText;
			currenttime=http_orario.responseText;
			serverdate=new Date(currenttime);
			occupato=false;
		}
	}
}

var occupato = false;
function orario() {
  if (!occupato && http_orario) {
	http_orario.open("GET", url_orario, true);
    http_orario.onreadystatechange = handleHttpResponse_orario;
    occupato = true;
    http_orario.send(null);
  }
}

// Current Server Time script (SSI or PHP)- By JavaScriptKit.com (http://www.javascriptkit.com)
// For this and over 400+ free scripts, visit JavaScript Kit- http://www.javascriptkit.com/
// This notice must stay intact for use.

//Depending on whether your page supports SSI (.shtml) or PHP (.php), UNCOMMENT the line below your page supports and COMMENT the one it does not:
//Default is that SSI method is uncommented, and PHP is commented:

//var currenttime = '<!--#config timefmt="%B %d, %Y %H:%M:%S"--><!--#echo var="DATE_LOCAL" -->' //SSI method of getting server date
//var currenttime = '<?php print date("F d, Y H:i:s", time())?>' //PHP method of getting server date

///////////Stop editting here/////////////////////////////////

var montharray=new Array("gennaio","febbraio","marzo","aprile","maggio","giugno","luglio","agosto","settembre","ottobre","novembre","dicembre")
//var serverdate=new Date(currenttime)

function padlength(what){
	var output=(what.toString().length==1)? "0"+what : what
	return output
}

function displaytime(){
	serverdate.setSeconds(serverdate.getSeconds()+1)
	var datestring=padlength(serverdate.getDate())+" "+montharray[serverdate.getMonth()]+" "+serverdate.getFullYear()
	var timestring=padlength(serverdate.getHours())+":"+padlength(serverdate.getMinutes())+":"+padlength(serverdate.getSeconds())
	document.getElementById("servertime").innerHTML=datestring+" "+timestring
	
	var hostdate=new Date();
	var datestringhost=padlength(hostdate.getDate())+" "+montharray[hostdate.getMonth()]+" "+hostdate.getFullYear()
	var timestringhost=padlength(hostdate.getHours())+":"+padlength(hostdate.getMinutes())+":"+padlength(hostdate.getSeconds())
	document.getElementById("hosttime").innerHTML=datestringhost+" "+timestringhost
}

function onload_orario(){
	orario();
	setTimeout("orario()",1000);
	setTimeout("orario()",4000);
	setTimeout("orario()",9000);
	setInterval("orario()",60000); //aggiorna l'ora a quella del server una volta ogni minuto attraverso una richiesta XMLHTTPREQUEST
	setInterval("displaytime()", 1000);
}