/*
 * libreria js solo lato client; è presente in ogni pagina
 */

var objBody_ = null;
var objCtlSearch_ = null; // destinatario della ricerca corrente

// inizializzazione pagina

addEvent(window, "load", initPage);

// funzioni di utilità solo lato client

function addEvent(objTgt, strEvt, objFun) // aggancia un nuovo gestore di eventi per l'oggetto
	{
	if(objFun == null) return;
	if(typeof(objFun) == "string" || objFun instanceof String) objFun = new Function(objFun);
	if(objTgt.addEventListener)
		objTgt.addEventListener(strEvt, objFun, false);
	else if(objTgt.attachEvent)
		objTgt.attachEvent("on" + strEvt, objFun);
	}

function clientHeight() // altezza della client-area
	{
	var iHeight = window.innerHeight;
	if(iHeight == null) iHeight = objBody_.clientHeight;
	return iHeight;
	}

function comboAddOption(objCbo, strValue, strText, bSelected) // aggiunge una option ad una select
	{
	var objEl;
	objEl = newEl("option", "value", strValue);
	if(bSelected != undefined && bSelected) objEl.selected = true;
	objEl.appendChild(newTx(strText));
	objCbo.appendChild(objEl);
	}

function comboValue(objCbo, strValue) // restituisce o imposta il valore di una combobox
	{
	var iIdx;
	
	if(strValue == undefined) // get
		{
		iIdx = objCbo.selectedIndex;
		if(iIdx == -1) return "";
		else return objCbo.options[iIdx].value;
		}
	else // set
		{
		bOk = false;
		
		for(iIdx = 0; iIdx < objCbo.options.length; iIdx++)
			if(objCbo.options[iIdx].value == strValue)
				{
				objCbo.selectedIndex = iIdx;
				return;
				}
		
		objCbo.selectedIndex = -1;
		}
	}

function dropDOMChildren(objDOM) // elimina tutti i children di un nodo DOM
	{
	if(objDOM == null) return;
	while(objDOM.hasChildNodes())
		objDOM.removeChild(objDOM.firstChild);
	}

function dropEvent(objTgt, strEvt, objFun) // elimina un gestore di eventi per l'oggetto
	{
	if(objFun == null) return;
	if(typeof(objFun) == "string" || objFun instanceof String) objFun = new Function(objFun);
	if(objTgt.removeEventListener)
		objTgt.removeEventListener(strEvt, objFun, false);
	else if(objTgt.detachEvent)
		objTgt.detachEvent("on" + strEvt, objFun);
	}

function eventKey(objEvt) // restituisce il keycode associato all'evento
	{
	if(objEvt.which == undefined || objEvt.which == 0)
		return objEvt.keyCode;
	else
		return objEvt.which;
	}

function getCookie(strName, strKey) // ricava il valore di un cookie
	{
	var strVal;
	
	function sepValue(strCoo, strName, strSep)
		{
		var strVal;
		var iIdx;
		var iIdx2;
		
		strName = escape(strName) + "=";
		
		iIdx = 0;
		
		while(true)
			{
			iIdx = strCoo.indexOf(strName, iIdx);
			if(iIdx == -1)
				return "";
			if(iIdx == 0 || strCoo.charAt(iIdx - 1) == strSep)
				break;
			iIdx += strName.length;
			}
		
		iIdx2 = strCoo.indexOf(strSep, iIdx);
		if(iIdx2 == -1)
			iIdx2 = strCoo.length;
	
		return strCoo.substr(iIdx + strName.length, iIdx2 - iIdx - strName.length);
		}
	
	strVal = sepValue(document.cookie, strName, "; ");
	if(strKey == undefined || strKey == "")
		return unescape(strVal);
	
	return unescape(sepValue(strVal, strKey, "&"));
	}

function initFocus() // cerca nel DOM un controllo con tabindex = 1 e, se lo trova, imposta il fuoco
	{
	var objEls;
	var iIdx;
	var objEl = null;
	
	objEls = document.getElementsByTagName("input"); // prima cerca tra gli INPUT
	for(iIdx = 0; iIdx < objEls.length; iIdx++)
		if((objEl = objEls.item(iIdx)).tabIndex == 1) break;
	
	if(objEl == null || objEl.tabIndex != 1)
		{
		objEls = document.getElementsByTagName("select"); // poi cerca tra i SELECT
		for(iIdx = 0; iIdx < objEls.length; iIdx++)
			if((objEl = objEls.item(iIdx)).tabIndex == 1) break;
		}
	
	if(objEl == null || objEl.tabIndex != 1)
		{
		objEls = document.getElementsByTagName("textarea"); // quindi cerca tra i textarea
		for(iIdx = 0; iIdx < objEls.length; iIdx++)
			if((objEl = objEls.item(iIdx)).tabIndex == 1) break;
		}
	
	if(objEl == null || objEl.tabIndex != 1)
		{
		objEls = document.getElementsByTagName("button"); // ancora cerca tra i BUTTON
		for(iIdx = 0; iIdx < objEls.length; iIdx++)
			if((objEl = objEls.item(iIdx)).tabIndex == 1) break;
		}
	
	if(objEl == null || objEl.tabIndex != 1)
		{
		objEls = document.getElementsByTagName("a"); // infine cerca tra gli A
		for(iIdx = 0; iIdx < objEls.length; iIdx++)
			if((objEl = objEls.item(iIdx)).tabIndex == 1) break;
		}
	
	if(objEl != null &&  objEl.tabIndex == 1)
		try
			{
			objEl.focus();
			}
		catch(objErr_)
			{
			}
	}

function initPage() // inizializzatore comune a tutte le pagine
	{
	objBody_ = document.getElementsByTagName("body").item(0);
	if(typeof(startPage) == "function")
		startPage();
	initFocus();
	}

function newEl(strTag) // crea un nuovo elemento DOM con i parametri passati
	{
	var iIdx;
	var iIdx2;
	var strP;
	var objP;
	var objEl;
	
	if(strTag.charAt(0) == "<") // specificato tramite codice HTML
		{
		objP = document.createElement("div"); // contenitore temporaneo
		objP.innerHTML = strTag; // crea l'elemento mediante html
		objEl = objP.firstChild.cloneNode(true); // clona ricorsivamente l'elemento
		objP = null;
		}
	else // specificato tramite tagName
		objEl = document.createElement(strTag);
	
	for(iIdx = 1; iIdx < arguments.length; iIdx += 2)
		{
		strP = String(arguments[iIdx]).split(".");
		objP = objEl;
		for(iIdx2 = 0; iIdx2 < strP.length - 1; iIdx2++)
			objP = objEl[strP[iIdx2]];
		objP[strP[iIdx2]] = arguments[iIdx + 1];
		}
	return objEl;
	}

function newTx(strTxt) // crea un nodo testo
	{
	return document.createTextNode(strTxt);
	}

function popUp(strUrl, iWidth, iHeight, strCmd, strQry, strWinName, objCtlSrc) // popup generico
	{
	if(objCtlSrc != null)
		objCtlSearch_ = objCtlSrc;
	
	if(strQry != undefined && strQry != "")
		strCmd += "&" + strQry;
	
	strWinName = dVal(strWinName, "");
	
	return window.open(strBaseUrl_ + strUrl + "?cmd=" + strCmd, strWinName, windowOpenParams(iWidth, iHeight));
	}

function showHelp(strIdHelp) // mostra la finestra con la guida
	{
	window.open(strBaseUrl_ + "/help.asp" + (strIdHelp == undefined ? "" : "?idh=" + escape(strIdHelp)), "", windowOpenParams(640, 550));
	}

function validateForm(objForm) // validazione di un form
	{
	var objVal;
	
	if(!(objVal = objForm.validate()).ok)
		{
		alert(objVal.err);
		objVal.obj.focus();
		return false;
		}
	
	return true;
	}

function windowClose() // chiude la finestra
	{
	window.close();
	}

function windowOpenParams(iW, iH) // restituisce la stringa per centrare le finestre pop-up
	{
	var iX = (screen.width - iW - 8) / 2;
	var iY = (screen.height - iH - 31) / 2;
	return "" +
		"width=" + iW + ",height=" + iH + ",left=" + iX + ",screenX=" + iX + ",top=" + iY + ",screenY=" + iY + "," +
		"directories=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=no";
	}
