window.onload = init;

var objXHR;

function init()
{
	var objInterval, objUpdate;

        writeControls(); // in textResizer.js
        if (document.getElementByIfd) {
                menu = new Menu();
                menu.addSection('navDIVcontentIn');
                menu.addSection('navDIVWelcome');
                menu.addSection('navDIVcontentOut');
                menu.addSection('preferences');
                if (menu.halt=='n') {
                        menu.calibrate();
                        menu.addLinks();
                        }
                }

        window.onresize = resize;





	if (!document.getElementById ||
		!document.createTextNode ||
		(typeof XMLHttpRequest == 'undefined' && !ActiveXObject))
	{
		return;
	}

	objUpdate = document.getElementById('updatetimer');
	if (objUpdate)
	{
		objUpdate.parentNode.removeChild(objUpdate);
	}
	objInterval = setTimeout(updateTimer, 1000);
}

function updateTimer()
{
	// Create an instance of the XMLHttpRequest object
	objXHR = window.XMLHttpRequest ?
	  new XMLHttpRequest() : 
	  new ActiveXObject("MSXML2.XMLHTTP.3.0");

	if (objXHR)
	{
		// Setup the request object
		objXHR.onreadystatechange = processResult;
		anno=getQSParam('anno');
		mese=getQSParam('mese');
		giorno=getQSParam('giorno');
		diff=getQSParam('diff');
		objXHR.open('GET', 'tempo.php?anno='+anno+"&mese="+mese+"&giorno="+giorno+"&diff="+diff, true);
		objXHR.send(null);
	}
}

function processResult()
{
	var objResult, objCurrent, objReplacement, objInterval;

	if (objXHR.readyState == 4)
	{
		if (objXHR.status == 200)
		{
			// Get the response
			objResult = objXHR.responseXML.documentElement;
			valueTimer = objResult.getElementsByTagName('tempo')[0].getAttribute('valore');
			objCurrent = document.getElementById("timer");
			objCurrent.textContent = valueTimer;
			objCurrent.innerText = valueTimer;
			anno=getQSParam('anno');
	                mese=getQSParam('mese');
        	        giorno=getQSParam('giorno');
			diff=getQSParam('diff');
			if (diff)
			document.title="Mancano "+valueTimer;
			else
			document.title="Mancano "+valueTimer+" al "+giorno+"-"+mese+"-"+anno;
			objInterval = setTimeout(updateTimer, 1000);
		}
	}
}

function getQSParam(ParamName) {
  QS=window.location.toString(); 
  var indSta=QS.indexOf(ParamName); 
  if (indSta==-1 || ParamName=="") return null; 
  var indEnd=QS.indexOf('&',indSta); 
  if (indEnd==-1) indEnd=QS.length; 
  var valore = unescape(QS.substring(indSta+ParamName.length+1,indEnd)); 
  return valore; 
  }
