// -------------------------------------
// Nome: 		functions.js
// Descrizione:	Funzioni JSCript di utilità ed uso generale
// Versione JS:	1.2
// Copyright:	Futuria Informatica Srl
// Ultima Mod:	28.09.2011
// -------------------------------------

// FUNZIONI DI DETECT BROWSER TYPE
// -------------------------------------------------------------
function Init()
{
	isDOM=document.getElementById?true:false
	isOpera=isOpera5=window.opera && isDOM
	isOpera6=isOpera && window.print
	isOpera7=isOpera && document.readyState
	isMSIE=isIE=document.all && document.all.item && !isOpera
	isStrict=document.compatMode=='CSS1Compat'
	isNN=isNC=navigator.appName=="Netscape"
	isNN4=isNC4=isNN && !isDOM
	isMozilla=isNN6=isNN && isDOM
}



// FUNZIONI ESTENSIONE FUNZIONALITA' CLASSE String
// -------------------------------------------------------------
String.prototype.isInt = function()
{
	return (this.match(/^-?[\d]+$/)!=null);
} 


Init();


// FUNZIONI DI SUPPORTO PER GESTIONE PAGE
// -------------------------------------------------------------
function GetWndBody(p_Wnd)
{
	if(!p_Wnd) p_Wnd=window;
	if(isStrict) return p_Wnd.document.documentElement;
	else return p_Wnd.document.body;
}

function GetWndScrollX(p_Wnd)
{
	if(!p_Wnd) p_Wnd=window;
	if(isMSIE || isOpera7) return GetWndBody(p_Wnd).scrollLeft;
	if(isNN || isOpera) return p_Wnd.pageXOffset;
}

function GetWndScrollY(p_Wnd)
{
	if(!p_Wnd) p_Wnd=window;
	if(isMSIE || isOpera7) return GetWndBody(p_Wnd).scrollTop;
	if(isNN || isOpera) return p_Wnd.pageYOffset;
}

function GetWndLeft(p_Wnd)
{
	if(!p_Wnd) p_Wnd=window;
	if(isMSIE || isOpera7) return p_Wnd.screenLeft;
	if(isNN || isOpera) return p_Wnd.screenX;
}

function GetWndTop(p_Wnd)
{
	if(!p_Wnd) p_Wnd=window;
	if(isMSIE || isOpera7) return p_Wnd.screenTop;
	if(isNN || isOpera) return p_Wnd.screenY;
}

function GetWndWidth(p_Wnd)
{
	if(!p_Wnd) p_Wnd=window;
	if(isMSIE) return GetWndBody(p_Wnd).clientWidth;
	if(isNN || isOpera) return p_Wnd.innerWidth;
}

function GetWndHeight(p_Wnd)
{
	if(!p_Wnd) p_Wnd=window;
	if(isMSIE) return GetWndBody(p_Wnd).clientHeight;
	if(isNN || isOpera) return p_Wnd.innerHeight;
}

function GetDocumentWidth(p_Wnd)
{
	if(!p_Wnd) p_Wnd=window;
	if(isMSIE || isOpera7) return GetWndBody(p_Wnd).scrollWidth;
	if(isNN) return p_Wnd.document.width;
	if(isOpera) return p_Wnd.document.body.style.pixelWidth;
}

function GetDocumentHeight(p_Wnd)
{
	if(!p_Wnd) p_Wnd=window;
	if(isMSIE || isOpera7) return GetWndBody(p_Wnd).scrollHeight;
	if(isNN) return p_Wnd.document.height;
	if(isOpera) return p_Wnd.document.body.style.pixelHeight;
}


function GetObjXMLHttp()
{
   try { return new XMLHttpRequest(); } catch(e) {}
   try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
	
   return null;	
}


// FUNZIONI DI SUPPORTO PER GESTIONE PAGE ITEMS
// -------------------------------------------------------------

// Restituisce il riferimento ad un Item dato il suo ID
function GetItemFromId(p_sItemId)
{
	//if (document.all && document.all[p_sItemId]) return document.all[p_sItemId];
	if (document.getElementById(p_sItemId)) return document.getElementById(p_sItemId);
	else if (document.layers && document.layers[p_sItemId]) return (document.layers[p_sItemId]);

	return null;
}


// Restituisce il valore di un Item, dato il suo riferimento o il suo ID
function GetItemValue(p_oItem)
{
	if (typeof(p_oItem) != 'object') p_oItem = GetItemFromId(p_oItem);
	
	sItemType = GetItemType(p_oItem);
	if (sItemType=='text' || sItemType=='textarea' || sItemType=='hidden')
		return p_oItem.value;
	if (sItemType=='select-one')
	{
		if (p_oItem.selectedIndex<0) return '';
		SelOption = p_oItem.options[p_oItem.selectedIndex];
		if (SelOption.value != '') return SelOption.value;
		else return SelOption.text;
	}
	if (sItemType=='select-multiple')
	{
	}
	
	// Restituisce l'inner-text (utile nel caso degli item <span>)
	return p_oItem.innerText;
}


// Imposta il valore di un Item, dato il suo riferimento o il suo ID
function SetItemValue(p_oItem, p_sValue)
{
	if (typeof(p_oItem) != 'object') p_oItem = GetItemFromId(p_oItem);
	
	sItemType = GetItemType(p_oItem);

	if ((sItemType=='text') || (sItemType=='textarea') || (sItemType=='hidden'))
	{
		p_oItem.value = p_sValue;
		return true;
	}
	return false;
}


// Restitruisce il tipo di un Item, dato il suo riferimento o il suo ID
function GetItemType(p_oItem)
{
	if (typeof(p_oItem) != 'object') p_oItem = GetItemFromId(p_oItem);
		
	if (p_oItem.type != null) return p_oItem.type;
	if (p_oItem.tagName != null) return p_oItem.tagName.toLowerCase();

	return 'unknown';
}

// Imposta lo stato di visibilità dell'Item, dato il suo riferimento o il suo ID
//optionally if sname is a radio button, then an optional third parameter restricts to a specific radio button
//if sname is a radio and the third parameter is ommitted then the function acts upon ALL;
function SetItemVisible(p_oItem, p_bshow)
{
	if (typeof(p_oItem) != 'object') p_oItem = GetItemFromId(p_oItem);
	if (p_oItem == null) return;
	var sDisplay = "";
	if(p_bshow != true) sDisplay = "none";

	if(p_oItem.style==null)
	{
		//the object isn't null by the name is.  Must be an array;
		//checkbox or radio
		if(p_oItem.length > 1)
		{
			var sValue = "";
			if(arguments.length > 2) sValue = arguments[2];
			//if Radio then we will check an optional third parameter to restrict to a value or all;
			for(var nIndex = 0; nIndex < oItem.length; nIndex++)
			{
				if(p_oItem[nIndex].style != null)
				{
					if(sValue.length > 0)
					{
						if(p_oItem[nIndex].value == sValue)
							p_oItem[nIndex].style.display = sDisplay;
						if(p_oItem[nIndex].value == null)
						{
							if(p_oItem[nIndex].innerText == sValue )
								p_oItem[nIndex].style.display = sDisplay;
						}
					}
					else
					{
						p_oItem[nIndex].style.display = sDisplay;
					}
				}
			}
		}
	}
	else
	{
		p_oItem.style.display = sDisplay;
	}
}


// Ritorna lo stato di visibilità di un Item, dato il suo riferimento o il suo ID
function GetItemVisible(p_oItem)
{
	if (typeof(p_oItem) != 'object') p_oItem = GetItemFromId(p_oItem);
	if (p_oItem == null) return false;
	if (p_oItem.style==null) return false;
	
	return ((p_oItem.style.display != 'none') && (p_oItem.style.display != ''));
}


// Alterno lo stato di visibilità di un Item, dato il suo riferimento o il suo ID
function DynToggleItemVisible(p_oItem)
{
	if (typeof(p_oItem) != 'object') p_oItem = GetItemFromId(p_oItem);
	SetItemVisible(p_oItem, !GetItemVisible(p_oItem));
}


// Imposta il valore di uno style CSS
function SetItemStyle(p_oItem, p_sStyle, p_sValue)
{
	if (typeof(p_oItem) != 'object') p_oItem = GetItemFromId(p_oItem);
	if (p_oItem == null) return false;
	eval("p_oItem.style."+p_sStyle+"='"+p_sValue+"'");
}


function GetItemLeft(p_oItem)
{
	if (typeof(p_oItem) != 'object') p_oItem = GetItemFromId(p_oItem);
	if (p_oItem == null) return null;

	if(isMSIE || isMozilla || isOpera7) return p_oItem.offsetLeft
	if(isOpera) return p_oItem.css.pixelLeft;
	if(isNN4) return p_oItem.x;
}

function GetItemTop(p_oItem)
{
	if (typeof(p_oItem) != 'object') p_oItem = GetItemFromId(p_oItem);
	if (p_oItem == null) return null;

	if(isMSIE || isMozilla || isOpera7) return p_oItem.offsetTop
	if(isOpera) return p_oItem.css.pixelHeight;
	if(isNN4) return p_oItem.y;
}

// Return absolute X Position
function GetItemPosX(p_oItem)
{
 	if (typeof(p_oItem) != 'object') p_oItem = GetItemFromId(p_oItem);
	if (p_oItem == null) return null;
	var obj = p_oItem;

	var curleft = 0;
    if (obj.offsetParent) {
        while (1) {
            curleft+=obj.offsetLeft;
            if (!obj.offsetParent) {
                break;
            }
            obj=obj.offsetParent;
        }
    } else if (obj.x) {
        curleft+=obj.x;
    }
    return curleft;
}

// Return absolute Y Position
function GetItemPosY(p_oItem)
{
	if (typeof(p_oItem) != 'object') p_oItem = GetItemFromId(p_oItem);
	if (p_oItem == null) return null;
 	var obj = p_oItem;

   var curtop = 0;
    if (obj.offsetParent) {
        while (1) {
            curtop+=obj.offsetTop;
            if (!obj.offsetParent) {
                break;
            }
            obj=obj.offsetParent;
        }
    } else if (obj.y) {
        curtop+=obj.y;
    }
    return curtop;
}


function GetItemHeight(p_oItem)
{
	if (typeof(p_oItem) != 'object') p_oItem = GetItemFromId(p_oItem);
	if (p_oItem == null) return null;

	if(isMSIE || isMozilla || isOpera7) return p_oItem.offsetHeight
	if(isOpera) return p_oItem.css.pixelHeight
	if(isNN4) return p_oItem.document.height
}


function GetItemWidth(p_oItem)
{
	if (typeof(p_oItem) != 'object') p_oItem = GetItemFromId(p_oItem);
	if (p_oItem == null) return null;
	
	if(isMSIE || isMozilla || isOpera7) return p_oItem.offsetWidth;
	if(isOpera) return p_oItem.style.pixelWidth;
	if(isNN4) return p_oItem.document.width;
}

// Utilizzabile su DIV
function GetItemScrollTop(p_oItem)
{
	if (typeof(p_oItem) != 'object') p_oItem = GetItemFromId(p_oItem);
	if (p_oItem == null) return null;
	
	if(isMSIE || isMozilla || isOpera7) return p_oItem.scrollTop;
	if(isOpera) return p_oItem.scrollTop;
	if(isNN4) return p_oItem.scrollTop;
}

// Utilizzabile su DIV
function GetItemScrollLeft(p_oItem)
{
	if (typeof(p_oItem) != 'object') p_oItem = GetItemFromId(p_oItem);
	if (p_oItem == null) return null;
	
	if(isMSIE || isMozilla || isOpera7) return p_oItem.scrollLeft;
	if(isOpera) return p_oItem.scrollLeft;
	if(isNN4) return p_oItem.scrollLeft;
}

function SetItemWidth(p_oItem, p_sValue)
{
	if (typeof(p_oItem) != 'object') p_oItem = GetItemFromId(p_oItem);
	if (p_oItem == null) return null;
	
	if (isNN) p_oItem.style.width = p_sValue;
	else if(isMSIE || isMozilla || isOpera7) p_oItem.style.width = p_sValue;
	else if(isOpera) p_oItem.style.pixelWidth = p_sValue;
	else if(isNN4) p_oItem.document.width = p_sValue;
}

function SetItemHeight(p_oItem, p_sValue)
{
	if (typeof(p_oItem) != 'object') p_oItem = GetItemFromId(p_oItem);
	if (p_oItem == null) return null;
	if (p_sValue == null) return false;
	var sValuePx = p_sValue + (p_sValue.toString().isInt()?'px':'');
	
	if (isNN) p_oItem.style.height = sValuePx;
	else if(isMSIE || isMozilla || isOpera7) p_oItem.style.height = sValuePx;
	else if(isOpera) p_oItem.style.pixelHeight = p_sValue;
	else if(isNN4) p_oItem.document.height = sValuePx;
}

function GetItemVisibility(p_oItem)
{
	if (typeof(p_oItem) != 'object') p_oItem = GetItemFromId(p_oItem);
	if (p_oItem == null) return null;
	
	if(isMSIE || isMozilla || isOpera7) return p_oItem.style.visibility;
	if(isOpera) return p_oItem.style.visibility;
	if(isNN4) return p_oItem.document.visibility;
}

function SetItemVisibility(p_oItem, p_sValue)
{
	if (typeof(p_oItem) != 'object') p_oItem = GetItemFromId(p_oItem);
	if (p_oItem == null) return;
	
	if(isMSIE || isMozilla || isOpera7) p_oItem.style.visibility = p_sValue;
	else if(isOpera) p_oItem.style.visibility = p_sValue;
	else if(isNN4) p_oItem.document.visibility = p_sValue;
}

function SetItemLeft(p_oItem, p_nLeft)
{
	if (typeof(p_oItem) != 'object') p_oItem = GetItemFromId(p_oItem);
	if (p_oItem == null) return null;

	if(isOpera) p_oItem.style.pixelLeft = p_nLeft;
	else if(isNN4) p_oItem.x = p_nLeft;
	else p_oItem.style.left = p_nLeft+"px";
}

function SetItemTop(p_oItem, p_nTop)
{
	if (typeof(p_oItem) != 'object') p_oItem = GetItemFromId(p_oItem);
	if (p_oItem == null) return null;

	if(isOpera) p_oItem.style.pixelTop = p_nTop;
	else if(isNN4) p_oItem.y = p_nTop;
	else p_oItem.style.top = p_nTop+"px";
}

function SetItemZIndex(p_oItem, p_nZIndex)
{
	if (typeof(p_oItem) != 'object') p_oItem = GetItemFromId(p_oItem);
	if (p_oItem == null) return null;

	if(isOpera) p_oItem.style.zIndex = p_nZIndex;
	else if(isNN4) p_oItem.zIndex = p_nZIndex;
	else p_oItem.style.zIndex = p_nZIndex;
}

function GetItemOuterHTML(p_oItem)
{
	if (typeof(p_oItem) != 'object') p_oItem = GetItemFromId(p_oItem);
	if (p_oItem == null) return null;
	if (isMSIE) return p_oItem.outerHTML;
	
	temp=p_oItem.cloneNode(true);
	document.getElementById('TempDiv').appendChild(temp);
	outer=document.getElementById('TempDiv').innerHTML;
	document.getElementById('TempDiv').innerHTML="";
	return outer;
}

function SetItemOuterHTML(p_oItem, p_sHtml)
{
	if (typeof(p_oItem) != 'object') p_oItem = GetItemFromId(p_oItem);
	if (p_oItem == null) return;
	if (isMSIE || isOpera) { p_oItem.outerHTML = p_sHtml; return; }
	
	var r = p_oItem.ownerDocument.createRange();
	r.setStartBefore(p_oItem);
	var df = r.createContextualFragment(p_sHtml);
	p_oItem.parentNode.replaceChild(df, p_oItem);
}

function ItemCopyValueToClipboard(p_oItem)
{
	var sValue = GetItemValue(p_oItem);
	
	//
	if (isMSIE)
	{
		window.clipboardData.setData("Text", sValue);
	}
}

function ItemAddClass(p_oItem, p_sClassName, p_bAdd)
{
	if (p_bAdd==null) p_bAdd=true;
	if (typeof(p_oItem) != 'object') p_oItem = GetItemFromId(p_oItem);

	var sClassName = p_oItem.className;
	var asClass = sClassName.split(' ');
	sClassName = '';
	for(var n=0; n<asClass.length; n++)
	{
		if (asClass[n] != p_sClassName) sClassName += (sClassName!=''?' ':'') + asClass[n];
	}
	if (p_bAdd)
	{
		sClassName += (sClassName!=''?' ':'') + p_sClassName;
	}
	p_oItem.className = sClassName;
}

function ItemRemoveClass(p_oItem, p_sClassName)
{
	return ItemAddClass(p_oItem, p_sClassName, false);
}



// FUNZIONI DI SUPPORTO PER GESTIONE PAGE ITEMS - LIST (select)
// -------------------------------------------------------------

// Rimuove la lista delle Options del List Item
function ListResetContent(p_oItem)
{
	if(p_oItem == null) return false;
	
	p_oItem.options.length = 0;
}

// Aggiunge una nuova Options al List Item
function ListAddOption(p_oItem, p_sText, p_sValue, p_bSelected, p_nPos)
{
	if(p_oItem == null) return false;
	if(p_oItem.nodeName != "SELECT") return false;

	var index = 0;
	//var newitem = document.createElement("option");
	var newitem = new Option(p_sText, p_sValue, false, p_bSelected);
	if(newitem == null) return -1;

	if(p_nPos != null)
		index = p_oItem.options.add(newitem, p_nPos);
	else
		index = p_oItem.options.add(newitem, p_oItem.options.length);
	return index;
}


// FUNZIONI DI SUPPORTO PER GESTIONE TEXTAREA
// -------------------------------------------------------------
function TAreaToggleHeight(p_oItem)
{
	if(p_oItem == null) return false;
	if (p_oItem.oheight==undefined || p_oItem.oheight==0)
	{
		var nHeight = GetItemHeight(p_oItem);
		p_oItem.oheight = nHeight;
		SetItemHeight(p_oItem, Math.round(nHeight*2.5));
	}
	else
	{
		SetItemHeight(p_oItem, p_oItem.oheight);
		p_oItem.oheight = 0;
	}
	return true;	
}




// FUNZIONI DI GESTIONE DEI COOKIES
// -------------------------------------------------------------

// Funzione di settaggio di un cookie per la pagina corrente
// Sintassi: SetCookie(name, value, ExpireTimeMin, path, domain, secure)
function SetCookie (p_sName, p_sValue)
{ 
	var today = new Date();
	var expire = new Date();	
	var argv = SetCookie.arguments; 
	var argc = SetCookie.arguments.length; 
	if(argc > 2) expire.setTime(today.getTime() + argv[2]*60*1000);
	else expire = null;
	var path = (argc > 3) ? argv[3] : '/'; 
	var domain = (argc > 4) ? argv[4] : null; 
	var secure = (argc > 5) ? argv[5] : false; 

	// kill cookie if exists
	var dKillDate = new Date("January 1, 1970"); // in the past
	var sKillDate = dKillDate.toGMTString();
	document.cookie = p_sName + '=dummy;expires=' + sKillDate;
	
	// rewrite cookie
	document.cookie = p_sName + '=' + escape(p_sValue) + 
		((expire == null) ? "" : ("; expires=" + expire.toGMTString())) + 
		((path == null) ? "" : ("; path=" + path)) + 
		((domain == null) ? "" : ("; domain=" + domain)) + 
		((secure == true) ? "; secure" : "");
}


// Funzione di lettura di un cookie per la pagina corrente
// Se il cookie non esiste viene restituito null; se il cookie non è valorizzato viene restituito ''
function GetCookie( check_name )
{
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );

		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}


// FUNZIONI DI SUPPORTO PER GESTIONE STRINGHE
// -------------------------------------------------------------

// Esegue il Trim di spazi all'inizio e alla fine di una stringa
function StrTrim(p_sStr)
{
	return p_sStr.replace(/^\s*/, '').replace(/\s*$/, ''); 
}


// Esegue il Replace
function StrReplace(p_sStr, p_sSearch, p_sReplace, p_bCaseSensitive)
{
	p_sSearch = p_sSearch.replace(/\$/, '\\$');
	p_sSearch = p_sSearch.replace(/\./, '\\.');
	
	var oRE = new RegExp(p_sSearch, 'g'+(p_bCaseSensitive?'':'i'));
	return p_sStr.replace(oRE, p_sReplace); 
}


// Effettua il Match globale sulla stringa [p_sStr] con l'expr reg [p_sRE]
function StrMatchAll(p_sRE, p_sStr, p_bCaseSensitive)
{
	var oRE = new RegExp(p_sRE, 'g'+(p_bCaseSensitive?'':'i'));
	var aResult = new Array();
	while(oRE.lastIndex < p_sStr.length)
	{
		aMatch = oRE.exec(p_sStr);
		if (aMatch==undefined) break;
		aResult = aResult.concat(aMatch);
	}
	return aResult; 
}


// Sostituisce ogni occorrenza di token con il relativo valore
function StrReplaceTokens(p_sStr, p_asTokens, p_asValues)
{
	var sText = p_sStr;
	for(var n=0; n<p_asTokens.length; n++)
	{
		sText = sText.replace(p_asTokens[n], p_asValues[n]);
	}
	return sText;
}


function StrItemGet(p_sStr, p_sItemName)
{
	if (p_sStr==undefined) return '';
	if (p_sItemName=='') return '';
	
	var oRE = new RegExp('(^|;)\\s*' + p_sItemName + '\\s*:\\s*([^;]*)', 'gi');

	var aTokens = oRE.exec(p_sStr);
	if (aTokens==null) return '';
	return aTokens[2];
}

function StrItemSet(p_sStr, p_sItemName, p_sItemValue)
{
	if (p_sStr==undefined) return '';
	if (p_sItemName=='') return '';
	
	var oRE = new RegExp('(^|;)\\s*' + p_sItemName + '\\s*:\\s*([^;]*)', 'gi');

	if (oRE.test(p_sStr))
		p_sStr = p_sStr.replace(oRE, '$1'+p_sItemName+':'+p_sItemValue);
	else
		p_sStr += p_sItemName+':'+p_sItemValue+';';
	
	return p_sStr; 
}


// Esegue il dump Hex/Char di una stringa di caratteri/bytes
function StrDumpHex(p_sStr)
{
	var nLen = p_sStr.length;
	var nChar;
	var sChar;
	var sLineChar;
	var sLineHex;
	var sOut = '';
	
	for(var n=0; n<nLen; n+=16)
	{
		sLineChar = '';
		sLineHex = '';
		for(var b=0; b<16; b++)
		{
			nChar = p_sStr.charCodeAt(n+b);
			sChar = p_sStr.substr(n+b, 1);
			if (nChar<32 || nChar>127) sChar = '.';
			sLineChar += sChar;
			sLineHex += DecToHex(nChar, 2)+' ';
		}
		sOut += DecToHex(n, 8) + ': ' + sLineHex + ' ' + sLineChar + '\n';
	}
	
	return sOut;
}


function StrRandom(p_nLength)
{
	var sChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var sStr = '';
	for (var i=0; i<p_nLength; i++) {
		var nNum = Math.floor(Math.random() * sChars.length);
		sStr += sChars.substring(nNum, nNum+1);
	}
	return sStr;
}




// FUNZIONI VARIE 
// -------------------------------------------------------------

// Adatta l'altezza di un iFrame in base al suo contenuto.
// Può/Deve essere chiamata sull'evento onload() dell'Iframe stesso
function IFrameAdjustHeight(p_sIframeId)
{
	oIFrame = GetItemFromId(p_sIframeId);
	if (oIFrame == null) return;
	
	// if contentDocument exists, W3C compliant (e.g. Mozilla) 
	var oFrameDoc = null;
	if (oIFrame.contentDocument)
		oFrameDoc = oIFrame.contentDocument;
	else // bad Internet Explorer  
		oFrameDoc = document.frames[p_sIframeId].document;
	if (oFrameDoc)
		SetItemHeight(oIFrame, oFrameDoc.body.offsetHeight);
}


// Mostra un messaggio in un dialog di alert
function MessageBox(p_sMessage, p_sTitle, nFlags)
{
   sTitle = unescape(p_sTitle);
   sMessage = unescape(p_sMessage);
   alert(sTitle + "\n\n" + sMessage);
}


// p_sPointer = wait|default
// NB: sugli Input di tipo 'select' non è supportato!
function SetPointer(p_sPointer)
{
	document.body.style.cursor = p_sPointer;
}


// Converte un numero esadecimale di n cifre in numero decimale
// Formati: hhhh 0xhhhh hhhhH
function HexToDec(p_sHex)
{
	return parseInt(p_sHex, 16);
}


// Converte un numero intero in esadecimale
function DecToHex(p_nDec, p_nDigits)
{
	var sDigits = "0123456789ABCDEF";
	var nNibble;
	var sOut = '';
	while(p_nDec>0)
	{
		nNibble = p_nDec & 0x0F;
		p_nDec = p_nDec >>> 4;
		sOut = sDigits.substr(nNibble,1) + sOut;
	}
	if (p_nDigits != null)
	{
		var sPad = '00000000';
		sOut = sPad.substr(0, p_nDigits-sOut.length) + sOut;
	}
	return sOut;
}


// Caricamento dinamico di un file javascript
function LoadScript(url, callback)
{
    var script = document.createElement("script")
    script.type = "text/javascript";

	if (typeof(callback)=='function')
		if (script.readyState){  //IE
			script.onreadystatechange = function(){
				if (script.readyState == "loaded" ||
						script.readyState == "complete"){
					script.onreadystatechange = null;
					callback();
				}
			};
		} else {  //Others
			script.onload = function(){
				callback();
			};
		}

    script.src = url;
    document.getElementsByTagName("head")[0].appendChild(script);
}


// Restituisce il valore di una variabile presente nella query-string dell'url
function GetQueryVariable(p_sVarName)
{
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++)
  {
    var pair = vars[i].split("=");
    if (pair[0] == p_sVarName)
	{
      return pair[1];
    }
  }
	return '';
} 

