// RICORDARSI DI INIZIALIZZARE CON apa_init();

// variabili globali usati da APA framework
var apa = null;

// inizializzazion framework APA
function apa_init(waitmsg)
{
	apa = Object();

	// crea il div di overlay ombra
	apa.mainframe = document.createElement('div');
	apa.mainframe.setAttribute('id', 'apa_mainframe');
	apa.mainframe.style.position = 'absolute';
	// dimensioni = quelle dello schermo, impostate al momento
	apa.mainframe.style.backgroundColor = '#000000';
	//apa.mainframe.style.display = 'none';
	apa.mainframe.style.top = '0px';
	apa.mainframe.style.left = '0px';
	apa.mainframe.style.visibility = 'hidden';
	apa.mainframe.style.opacity = 0.3;
	apa.mainframe.style.filter = "alpha(opacity=30)";
	document.body.appendChild(apa.mainframe);

	// clessidra - wait
	apa.hg = document.createElement('div');
	apa.hg.setAttribute('id', 'apa_hourglass');
	apa.hg.style.position = 'absolute';
	apa.hg.style.top = '50%';
	apa.hg.style.left = '50%';
	apa.hg.style.width = '200px';
	apa.hg.style.height = '40px';
	apa.hg.style.marginTop = '-20px';
	apa.hg.style.marginLeft = '-100px';
	apa.hg.style.backgroundColor = '#D0D0FF';
	apa.hg.style.border = '1px black solid';
	apa.hg.style.visibility = 'hidden';
	apa.hg.style.fontFamily = 'Tahoma';
	apa.hg.style.fontSize = '11px';
	apa.hg.style.fontWeight = 'bold';
	apa.hg.innerHTML = '<table><tr><td valign="middle" style="padding: 4px;"><img src="img/wait.gif"></td><td valign="middle" style="padding: 4px;">&nbsp;$ATTENDERE$&nbsp;</td></tr></table>'.replace('$ATTENDERE$', waitmsg);
	document.body.appendChild(apa.hg);
	
	apa.working = false;
	apa.what = false;
	apa.who = null;
	apa.xhr = null;
	
	// funzione asincrona per xhr
	apa.OnReadyStateChange = function()
	{
		switch (apa.xhr.readyState) {
		
		// not init
		case 0:
			apa.set_working(false);
			break;
		
		// open/sent/received
		case 1:
		case 2:
		case 3:
			apa.set_working(true);
			break;
			
		// loaded
		case 4:
			apa.set_working(false);
			if (apa.xhr.status != 200) alert("Errore di comunicazione con il server web (codice: " + apa.xhr.status + ")");
			else if (apa.what) {
				var rv = apa.xhr.responseText.split("\r\n");
				var retcode = rv[0];
				if ((retcode != "OK") && (retcode != "ERR")) alert("Errore nella risposta del server web (risposta non interpretabile).");
				else apa.what(apa.who, retcode, rv.slice(1));
			}
			break;
			
		 }
	}
	
	// invio richieste APA
	apa.send = function(url, avpairs)
	{
		var data = "";
		
		// (ri)crea l'oggetto xmlhttp
		try { apa.xhr = new ActiveXObject('Msxml2.XMLHTTP'); }
		catch (e1) {
			try { apa.xhr = new ActiveXObject("Microsoft.XMLHTTP"); }
			catch (e2) {
				try { apa.xhr = new XMLHttpRequest(); }
				catch (e3) { apa.xhr = null; }
			}
		}
		if (apa.xhr) apa.xhr.onreadystatechange = apa.OnReadyStateChange;
		
		apa.xhr.open('POST', url, true);
		apa.xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
		if (avpairs) {
			for (var attribute in avpairs) {
				var value;
				if (typeof avpairs[attribute] == "string") value = avpairs[attribute];
				else if (typeof avpairs[attribute] == "number") value = avpairs[attribute].toString();
				else continue;
				data += attribute + "=" + encodeURIComponent(value) + "&";
			}
			if (data.length > 0) data = data.slice(0, -1);
		}
		apa.xhr.send(data);
	}

	// imposta lo stato "attendere"
	apa.set_working = function(state)
	{
		apa.working = state;
		this.set_popup_background(state);
		if (state) {
		
			apa.hg.style.visibility = "visible";
			document.body.style.cursor = "wait";
			
		} else {
		
			apa.hg.style.visibility = "hidden";
			document.body.style.cursor = "default";
			
		}
	}
	
	
	// imposta il background oscurato
	apa.set_popup_background = function(on)
	{
		if (on) {
		
			var wh, ww;
			
			// stabilisce la larghezza della finestra per i vari browser
			if (typeof(window.innerWidth) == 'number') {
				ww = window.innerWidth;
				wh = window.innerHeight;
			} else if (document.documentElement && document.documentElement.clientWidth) {
				ww = document.documentElement.clientWidth;
				wh = document.documentElement.clientHeight;
			} else if (document.body && document.body.clientWidth) {
				ww = document.body.clientWidth;
				wh = document.body.clientHeight;
			}
		
			apa.mainframe.style.width = ww + 'px';
			apa.mainframe.style.height = wh + 'px';
			apa.mainframe.style.visibility = "visible";
			
		} else {
		
			apa.mainframe.style.visibility = "hidden";
			
		}
		
	}

}

// --------------------- altri oggetti di utilita' ----------------


// oggetto Pos -- stabilisce la posizione relativa per vari oggetti di interfaccia
// tra cui dropdown treeview
function Pos(obj)
{
	var curleft = 0, curtop = 0;
	if (obj.offsetParent) {
		while (1) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
			if (!obj.offsetParent) break;
			obj = obj.offsetParent;
		}
	} else {
		if (obj.x) curleft += obj.x;
		if (obj.y) curtop += obj.y;
	}
	this.x = curleft;
	this.y = curtop;
}

