/* Funzionalità generali */
function getDocumentElementById(elementId, doc)
{
	obj = (doc==null || doc=='undefined') ? window.document : doc;
	return (obj.getElementById) ? obj.getElementById(elementId) : obj.all(elementId);
}
function showHideForceTo(sName, displayStatus)
{
	var tag = getDocumentElementById(sName);
	
	/* Questa patch permette ai browser compatibili W3C DOM di gestire 
	   correttamente l'attributo display sulle righe delle tabelle poiché 
	   IE erroneamente usa 'block' invece dell'indicazione W3C DOM 'table-row' 
	   http://www.w3schools.com/htmldom/prop_style_display.asp
	*/
	var viewAttribute = 'block';
	if(tag=='[object HTMLTableRowElement]') { 
		viewAttribute = 'table-row';
	} else if(tag=='[object HTMLImageElement]') { 
		viewAttribute = 'inline';
	}
	if(tag!=null && tag!=undefined)
		tag.style.display = (displayStatus=='none') ? 'none' : viewAttribute;
	return;
}
function trim(string) { 
	return string.replace(/\s+$|^\s+/g,""); 
}
function cbUnescape(v)
{
	return trim(unescape(v)).replace(/\+/gi,' ');
}
/* Gestione generale delle SELECT (o liste) */	
function selectAddOption(listDestination, listPosition, itemValue, itemText)
{
	if(listDestination)
	{
		var opt = document.createElement('option');
		opt.text = itemText;
		opt.value = itemValue;
		if(listPosition>0)
			listDestination.options.add(opt,listPosition);
		else
			listDestination.options.add(opt);
		return true;
	}
	return false;
}
function selectRemoveOption(list, itemPosition)
{
	if(list)
	{
		list.options[itemPosition] = null;
		return true;
	}
	return false;
}
function selectHasOptions(list)
{
	if(list)
		return list.options!=null;
	return false;
}
function selectSortOptions(listName)
{
	var list = getDocumentElementById(listName);
	
	if(list)
	{
		var o = new Array();
		if(!selectHasOptions(list)) 
			return false;
		
		for(var i=0; i<list.options.length; i++)
		{
			o[o.length] = new Option(list.options[i].text, list.options[i].value, list.options[i].defaultSelected, list.options[i].selected);
		}
		if(o.length==0) 
			return false;
		
		o = o.sort(sortCompare);
		if(selectRemoveAllOptions(list))
		{
			for(var i=0; i<o.length; i++)
			{
				list.options.add(o[i]);
			}
			return true;
		}
		else
			return false;		
	}
	else
		return false;
}
function sortCompare(itemA, itemB)
{
	if((itemA.text+"")<(itemB.text+"")) return -1;
	if((itemA.text+"")>(itemB.text+"")) return 1;
	return 0;
}
function selectRemoveAllOptions(list)
{
	if(list)
	{
		list.options.length = 0;
		return true;
	}
	return false;
}
/* Fine della gestione generale delle (SELECT o liste) */
function isInt(x) {
 var y=parseInt(x);
 if (isNaN(y)) return false;
 return x==y && x.toString()==y.toString();
} 
