/* Funzionalitą generali */
function getDocumentElementById(elementId, doc)
{
	obj = (doc==null || doc=='undefined') ? window.document : doc;
	return (obj.getElementById) ? obj.getElementById(elementId) : obj.all(elementId);
}

function setFocus(objectName)
{
	var obj = getDocumentElementById(objectName);
	if(obj!=null && obj!=undefined)
		obj.focus();
	return;
}

function trim(string) { 
	return string.replace(/\s+$|^\s+/g,""); 
}

function cbUnescape(v)
{
	return trim(unescape(v)).replace(/\+/gi,' ');
}

function showPopUp(sName, sURI, iWidth, iHeight, bScrollBars, bFocus, widthIdeale, heightIdeale) {
	
	/* Se una dimensione ideale č uguale a -1 vuol dire che il popup deve utilizzare tutto lo spazio disponibile */
	var lOffset = (window.screenLeft!='undefined' && window.screenLeft>=0) ? window.screenLeft : window.screenX;
	//alert("lOffset = " + lOffset);
	var tOffset = (window.screenTop!='undefined' && window.screenTop>=0) ? window.screenTop : window.screenY;
	//alert("tOffset = " + tOffset);
	var wOffset = hOffset = 50; /* Rappresenta lo spazio minimo lasciato dai popup ideali dai bordi per lasciare in vista almeno le scrollbar */
	var wScreen = (screen.availWidth!='undefined' && screen.availWidth>0) ? screen.availWidth : screen.width;
	//alert("wScreen : " + wScreen);
	var hScreen = (screen.availHeight!='undefined' && screen.availHeight>0) ? screen.availHeight : screen.height;
	//alert("hScreen : " + hScreen);
	if(widthIdeale=='undefined') widthIdeale = iWidth;
	if(widthIdeale=='-1') widthIdeale = wScreen;
	//alert("widthIdeale : " + widthIdeale);
	if(heightIdeale=='undefined') heightIdeale = iHeight;
	if(heightIdeale=='-1') heightIdeale = hScreen;
	//alert("heightIdeale : " + heightIdeale);
	
	var w = iWidth;
	if(wScreen>widthIdeale)
		w = widthIdeale;
	else if((wScreen-wOffset-lOffset)>iWidth && (wScreen-wOffset-lOffset)<widthIdeale)
		w = wScreen-wOffset-lOffset;
	//alert("w : " + w);
	var h = iHeight;
	if(hScreen>heightIdeale) 
		h = heightIdeale;
	else if((hScreen-hOffset-tOffset)>iHeight && (hScreen-hOffset-tOffset)<heightIdeale)
		h = hScreen-hOffset-tOffset;
	//alert("h : " + h);
		
	var myPopUp = window.open(sURI, sName, "toolbar=no, resizable=no, screenX=0, screenY=0, left=" + lOffset + ", top=" + tOffset + ", scrollbars = " + bScrollBars + ", status = no, width = " + w + ", height = " + h);
	/*
	myPopUp.window.document.write("<br>screen.width : " + screen.width);
	myPopUp.window.document.write("<br>screen.height : " + screen.height);
	myPopUp.window.document.write("<br>screen.availWidth : " + screen.availWidth);
	myPopUp.window.document.write("<br>screen.availHeight : " + screen.availHeight);
	myPopUp.window.document.write("<br>IE window.screenLeft : " + window.screenLeft);
	myPopUp.window.document.write("<br>IE window.screenTop : " + window.screenTop);
	myPopUp.window.document.write("<br>FF window.screenX : " + window.screenX);
	myPopUp.window.document.write("<br>FF window.screenY : " + window.screenY);
	*/
	if(bFocus) myPopUp.focus();
	return false;
}

function zoomAndClose(sPath, iWidth, iHeight)
{
	var myPopUp = window.open(sPath, "MyZoom", "toolbar=no,resizable=no,screenX=0,screenY=0,left=100,top=100,scrollbars=no,status=no,width=" + iWidth + ",height=" + iHeight);
	if(myPopUp)
	{
		myPopUp.window.document.write("<html><head><title>Zoom</title>");
		myPopUp.window.document.write("<style media=\"all\" type=\"text/css\">body { margin:0px;border:0px;padding:0px;text-align:center;vertical-align:middle; }</style>");
		myPopUp.window.document.write("<body><a href=\"javascript:self.close();\">");
		myPopUp.window.document.write("<img src=\"" + sPath + "\" width=\"" + iWidth + "\" height=\"" + iHeight + "\" border=\"0\" alt=\"Chiudi\">");
		myPopUp.window.document.write("</a></body></head></html>");
		myPopUp.focus();
	}
	return false;
}

function getQueryString()
{
	return (location.search.substring(0,1)=="?") ? location.search.substring(1) : location.search;
}

function parseQueryString()
{
	var qs = getQueryString();
	var aKeys = aValues = new Array(1);
	
	if(qs.indexOf("&")!=-1)
	{
		var pairs = qs.split("&");
		var aKeys = new Array(pairs.length);
		var aValues = new Array(pairs.length);
		for(p=0;p<pairs.length;p++)
		{
			s = pairs[p].split("=");
			aKeys[p] = s[0];
			aValues[p] = s[1];
		}
	}
	else
	{
		s = qs.split("=");
		aKeys[0] = s[0];
		aValues[0] = s[1];
	}
		
	return new Array(aKeys,aValues);
}

function getQueryStringKey(keyName)
{
	var aData = parseQueryString();
	for(k=0;k<aData[0].length;k++)
	{
		if(aData[0][k]==keyName) return aData[1][k];
	}
	return "";
}

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;
}
