var ie  = document.all    ? 1 : 0 ;
var ns4	= document.layers ? 1 : 0 ;

function SetParentClass(children, parentTag, className)
{
	if (ie)
	{
		while ( children.tagName != parentTag )
			{ children = children.parentElement ; }
	}
	else
	{
		while ( E.tagName != parentTag )
			{ children = children.parentNode ; }
	}
	children.className = className ;
}

function OpenNewWindow(sURL, sWindowName, iWidth, iHeight, bToolbar, bResizable, bStatusBar)
{
	var iTop  = (screen.height - iHeight) / 2 ;
	var iLeft = (screen.width  - iWidth)  / 2 ;
	var sOption  = "location=no,menubar=no,toolbar=no";
		sOption += ",width="  + iWidth ; 
		sOption += ",height=" + iHeight ;
		sOption += ",top="  + iTop ;
		sOption += ",left=" + iLeft ;	
		sOption += ",scrollbars=" + (bToolbar ? "yes" : "no");
		sOption += ",resizable=" + (bResizable ? "yes" : "no");
		sOption += ",status=" + (bStatusBar ? "yes" : "no");
	
	var oWindow = window.open('',sWindowName,sOption)
	oWindow.moveTo(iLeft,iTop);
	oWindow.resizeTo(iWidth,iHeight);
	oWindow.location.href = sURL ;
	oWindow.focus();
}
var newWindow = null;

function closeWin(){
	if (newWindow != null){
		if(!newWindow.closed)
			newWindow.close();
	}
}

function popUpWin(url, type, strWidth, strHeight){
	
	closeWin();
	
	if (type == "fullScreen"){
		strWidth = screen.availWidth - 10;
		strHeight = screen.availHeight - 160;
	}
	
	var tools="";
	if (type == "standard" || type == "fullScreen") tools = "resizable,toolbar=yes,location=yes,scrollbars=yes,menubar=yes,width="+strWidth+",height="+strHeight+",top=0,left=0";
	if (type == "console") tools = "resizable=no,toolbar=no,location=no,scrollbars=no,width="+strWidth+",height="+strHeight+",left=0,top=0";
	newWindow = window.open(url, 'newWin', tools);
	newWindow.focus();
}

function SegnalaPagina(titoloDocumento)
{
	var sURL     = window.location.href ;
	var sPageURL = "segnala.aspx?Titolo=" + encodeUTF( titoloDocumento ) + "&URL=" + encodeUTF( sURL ) ;
	
	OpenNewWindow(sPageURL, "CRCSegnalaPagina", 500, 550) ;
}

function HelpDesk()
{
	var sURL     = window.location.href ;
	var sPageURL = "/helpdesk.aspx";
	
	OpenNewWindow(sPageURL, "CRCHelpDesk", 500, 600) ;
}

function SegnalaPaginaRiservata(titoloDocumento)
{
	var sURL     = window.location.href ;
	var sPageURL = "/riservato/segnala_ris.aspx?Titolo=" + encodeUTF( titoloDocumento ) + "&URL=" + encodeUTF( sURL ) ;
	
	OpenNewWindow(sPageURL, "CRCSegnalaPagina", 500, 500) ;
}

function InviaDocumento(sCategoria)
{
	var sURL     = window.location.href;
	var sPageURL = "/riservato/invia_documento.aspx?Categoria=" + escape ( sCategoria );
	
	OpenNewWindow(sPageURL, "CRCInviaDocumento", 500, 500);

}

function HexEncode(s){
	var	hex = '' ;
	var	i ;
	for	(i=0 ; i < s.length ; i++)
	{
		hex	+= '%' + HexFromDec( s.charCodeAt(i) ) ;
	}
	return hex ;	
}

function HexFromDec( num ) 
{
   if (num > 65535) { return ("err!") }
   var tmp = "000"+ Math.round(num).toString(16);
         // parameter is radix (default is 10)
   return tmp.substring( tmp.length-2 );
}

function encodeUTF(str)
{
	// this converts the searchtext into utf-8
	var utftext = "";
	for(var n = 0 ; n < str.length ; n++)
	{
		var c = str.charCodeAt(n);
		// all chars in range 0-127 => 1byte
		if (c<128)
		{
			utftext += '%' + c.toString(16);
		}
		// all chars in range 127 to 2047 => 2byte
		else if((c>127) && (c<2048))
		{
			utftext += '%' + ((c>>6)|192).toString(16);
			utftext += '%' + ((c&63)|128).toString(16);
		}
		// all chars in range 2048 to 66536 => 3byte
		else 
		{
			utftext += '%' + ((c>>12)|224).toString(16);
			utftext += '%' + (((c>>6)&63)|128).toString(16);
			utftext += '%' + ((c&63)|128).toString(16);
		}
	}
	return utftext;
}

// This script extends JavaScript's built-in String object by adding a trim() method to it. 
// By using JavaScript's prototype capability, we can define our own method that will work on any String object.
String.prototype.trim = function() {
	return this.replace(/^\s*(\b.*\b|)\s*$/, "$1");
}

// Check if the option value is available in a SELECT object (combo or list)
function HasOption(combo, optionValue)
{
    for (var i = 0 ; i < combo.options.length ; i++) 
    {
        if (combo.options(i).value == optionValue) return true ;
	}
	return false ;
}

// Check if the option text is available in a SELECT object (combo or list)
function HasOptionText(combo, optionText)
{
    for (var i = 0 ; i < combo.options.length ; i++) 
    {
        if (combo.options(i).innerText == optionText) return true ;
	}
	return false ;
}

// Add a new option to a SELECT object (combo or list)
function AddComboOption(combo, optionText, optionValue)
{
	var oOption = document.createElement("OPTION") ;

	combo.options.add(oOption) ;

	oOption.innerText = optionText ;
	oOption.value     = optionValue ;
	
	return oOption ;
}

function RemoveComboOptions(combo)
{
	for (var i = (combo.options.length - 1) ; i >= 0 ; i--) 
    {
        if (combo.options(i).selected) combo.options.remove(i) ;
	}
	
	return void(0) ;
}

function CheckFileName(fileName)
{
// It causes error in IE 5.0: "Unexpected Quantifier"
//	var sPattern = /^(?!^(PRN|AUX|CLOCK\$|NUL|CON|COM\d|LPT\d|\..*)(\..+)?$)[^\x00-\x1f\\?*:\";|\/]+$/i ;
//	return sPattern.test(fileName) ;
	return true ;
}

function GetFileExtension(fileName)
{
	return fileName.substring(fileName.lastIndexOf('.') + 1) ;
}

function NotAvaliable()
{
	alert('Non implementato.') ;
}

function GetCheckboxesValues(checkboxGroup, separator)
{
	if (separator == null) separator = ',' ;

	var aChecks = new Array() ;
	// If there is only one Checkbox then the property "length" is null
	if (checkboxGroup.length == null )
		aChecks.push(checkboxGroup)
	else
		aChecks = checkboxGroup ;

	var sCheckedIds = "" ;
	for (i=0 ; i < aChecks.length; i++)
	{
		if (aChecks[i].checked)
		{
			if (sCheckedIds != "") sCheckedIds += "," ;
			sCheckedIds += aChecks[i].value ;
		}
	}		
		
	return sCheckedIds ;
}
