// GETOBJECT 
function getObject(strId)
{
	return document.getElementById(strId);
}


// CREATE ELEMENT
function createElement(strTag)
{
	return document.createElement(strTag);
}

// TRY 
var Try = 
	{ 
		these: function() 
		{
  		var returnValue;

      for (var i = 0; i < arguments.length; i++) 
			{
      	var lambda = arguments[i];
        try 
				{
          returnValue = lambda();
          break;
        } 
				catch (e) 
				{}
      }

    	return returnValue;
  	}
  }

//
// HTTPREQUEST 
//

// Invio della richiesta
function s58HttpSendRequest(htCommand, htQuery) {
	if(!s58HttpRequest.instance.processing) {
		getObject('ht_error').style.display = 'none';
		getObject('ht_loading').style.display = 'block';
		s58HttpRequest.processing = true;
		hturl = 'index.php?htcmd='+htCommand;
		hturl = hturl + '&'+htQuery;
		hturl = hturl + '&randid='+Math.random();
		s58HttpRequest.instance.open('get', hturl);
		s58HttpRequest.instance.onreadystatechange = s58HttpRequest.getResponse; 
		s58HttpRequest.instance.send(null);
	}
};

// Ricezione risposta
function s58HttpGetResponse() {
	if(s58HttpRequest.instance.readyState == s58HttpRequest.RS_FINISHED) {
		s58HttpRequest.evalResponse(s58HttpRequest.instance);
		getObject('ht_loading').style.display = 'none';
		s58HttpRequest.processing = false;
	}
};

// Processore delle risposta
function s58HttpEvalResponse(htResult){
	if (htResult.responseXML){
		var rxml = htResult.responseXML;
		var itemElements = rxml.getElementsByTagName("item");
		for (var i=0; i<itemElements.length; i++){
			if (itemElements[i].getAttribute("type")=="html"){
				if (objHTML = getObject(itemElements[i].getAttribute("id")) ){
					//objHTML.innerHTML = unescape(itemElements[i].childNodes[0].nodeValue);
					objHTML.innerHTML = itemElements[i].childNodes[0].nodeValue;
				}
			}
			else if (itemElements[i].getAttribute("type")=="script"){
				//eval(unescape(itemElements[i].childNodes[0].nodeValue));
				eval(itemElements[i].childNodes[0].nodeValue);
			}
		}
	}
	else {
		//alert('Text: '+htResp.responseText);
		getObject('ht_error').innerHTML = htResult.responseText;
		getObject('ht_error').style.display = 'block';
	}
}

var s58HttpRequest = 
{
	// Creazione oggetto HTTP
	getTransport: function() {
		return Try.these (
			function() {return new ActiveXObject('Msxml2.XMLHTTP')},
			function() {return new ActiveXObject('Microsoft.XMLHTTP')},
			function() {return new XMLHttpRequest()}
		) || false;
	},

	// Inizializzazione oggetti necessari
	init: function(){
		if (getObject('ht_error')==null){
			var htErrorDiv = document.createElement('div');
			htErrorDiv.id='ht_error';
			htErrorDiv.style.display='none';
			document.body.appendChild(htErrorDiv);
		}
		if (getObject('ht_loading')==null){
			var htLoadingDiv = document.createElement('div');
			htLoadingDiv.id='ht_loading';
			htLoadingDiv.style.display='none';
			htLoadingDiv.innerHTML='Loading...';
			document.body.appendChild(htLoadingDiv);
		}
	},

	// Handle all'invio della richiesta
	sendRequest: function(htCommand, htQuery){
		s58HttpSendRequest(htCommand, htQuery);
	},

	// Handle alla funzione di risposta
	getResponse: function(){
		s58HttpGetResponse();
	},

	// Handle al processore delle risposta
	evalResponse: function(htResult){
		s58HttpEvalResponse(htResult);
	},

	// Variabili di appoggio
	activeRequestCount: 0,

	// Costanti XMLHTTP
	RS_UNINITIALIZED: 0,
	RS_LOADING: 1,
	RS_LOADED: 2,
	RS_INTERACTIVE: 3,
	RS_FINISHED: 4

}

// NAVIGAZIONE
var s58SessID;
var s58MenuID;

function fAddField(objForm,fName,fValue){
	var oNewNode = document.createElement('input');
	oNewNode.setAttribute('type', 'hidden');
	oNewNode.setAttribute('name', fName);
	oNewNode.setAttribute('value', fValue);
	objForm.appendChild(oNewNode);
}

function fSubmitForm(frmName){
	objForm = document.getElementById(frmName);
	fAddField(objForm, 'sid', s58SessID);
	fAddField(objForm, 'menuid', s58MenuID);
	objForm.submit();
}

function fGoToMenu(idMenu){
	if (idMenu!='' && idMenu!=0) { s58MenuID = idMenu}
	fSubmitForm('frmS58');
}


// INIZIALIZZAZIONE
function s58init() {
	if (_sid!='') s58SessID=_sid;

	var s58Form = document.createElement('form');
	s58Form.id='frmS58';
	s58Form.method='POST';
	s58Form.action='index.php';
	document.body.appendChild(s58Form);

	s58HttpRequest.instance = s58HttpRequest.getTransport();
	s58HttpRequest.init();
}

window.onload = s58init;