var divSelected = "";
var content;
var ns4 = (document.layers)? true:false
var ie4 = (document.all)? true:false
var Menu = new Array();
var SottoMenu = new Array();
var ArrayBanner = new Array()
var livello1 = ""
var livello2 = "";
var MenuSelected = -1;
var indexBanner = 0

function CalcolaLunghezzaMenu(index)
{
	var output = 0
	if (index != -1)
		for (var k=0; k<index; k++)
		{
			output += String(Menu[k].split('^')[0]).length
			if (k<index-1)
				output += 15
		}	
	return (output*3)	 
}

function wrMenu(curSelLevel1,curSelLevel2)
{  
	var str     = ""
	var curNome = ""
	var curLink = ""
	var curAttivo = ""
	var classSubMenu = ""
	
    if ((curSelLevel1!=""))
	{   
		if (SottoMenu[curSelLevel1])
		{			
			var AryDatiMenu = SottoMenu[curSelLevel1].split("#")
			var parent = AryDatiMenu[0]
			var AryMenulevel2 = AryDatiMenu[1].split("^")
			var lunghezzaSottoMenu = 0
			var splitVoce
			for (var k=0;k<AryMenulevel2.length;k++) 
			{  
				splitVoce = AryMenulevel2[k].split("||")
				if (splitVoce[2] == 1)
					classSubMenu = "testMenuLinkSelected"
				else
					classSubMenu = "testMenuLink"
					
				str += '<td nowrap><a href="javascript:load(' + splitVoce[1] + ')" class="' + classSubMenu + '">'
				str += splitVoce[0] + '</a>&nbsp;</td>'

				lunghezzaSottoMenu += String(curNome).length
				if (splitVoce[0] != "")
					if (k < AryMenulevel2.length-1)
					{
						str += '<td valign="middle" align="center"><img src="../images/ico_menu2.gif">&nbsp;</td>'
						lunghezzaSottoMenu += 20
					}
			}		
			
			lunghezzaSottoMenu *= 3 			
			var position = CalcolaLunghezzaMenu(curSelLevel1)
			while ((position + lunghezzaSottoMenu) > 700)
				position -= 30

			str = '<td height=20 width=' + position + '>&nbsp;</td>' + str
			str = '<table border=0 cellspacing=0 cellpadding=0><tr>' + str
			str = '<tr><td nowrap BGCOLOR="#96BBD8">' + str
			str = str + '</tr></table></td></tr>'
			if (document.getElementById("td_SecondoLivello").style.backgroundColor!="#96BBD8")
				document.getElementById("td_SecondoLivello").style.backgroundColor="#96BBD8";  
		}
		else
			if (document.getElementById("td_SecondoLivello").style.backgroundColor!="#96BBD8")
				document.getElementById("td_SecondoLivello").style.backgroundColor="#96BBD8";
	}	
    return str;
}

function TopMenu() 
{	
    var s =''
	var datiImg = ""

	s+='<tr><td BGCOLOR=#4D82AE><table summary="Menu orizzontale" border=0 cellspacing=0 cellpadding=0><caption style="display:none;">Menu orizzontale</caption><tr>'	
	for (var i = 0;i<Menu.length;i++) 
	{		
		datiImg = Menu[i].split("^")
		s += '<td '
		if (datiImg[2] == 1)
		{		
			MenuSelected = i 
			s += 'style="background-Color:firebrick; color:white" '
		}
		s+=' nowrap align=center valign="top" id="td_' + i + '" onmouseover="scriviLayer(wrMenu(\''+i+'\',\''+livello2+'\'));coloraTD(this)">&nbsp;<a href = "javascript:load(' + datiImg[1] + ')" class=LinkChiaro>' + datiImg[0] + '</a>&nbsp;</td>';
			
		if (i < Menu.length-1)
			s+='<td width=1 valign="middle" align="center"><img src="../images/ico_menu1.gif"></td>'
    }
	s+='</tr></table></td></tr>'
	s+='<tr><td id="td_SecondoLivello" BGCOLOR="#96BBD8" height="25px">'
	s+='<div id="layerContent" name="layerContent"></div>'
	s+='</td></tr>'

	document.write(s)
	
	if (MenuSelected != -1)
		scriviLayer(wrMenu(MenuSelected,1))
}

function ricercasito()
{	
	if ((String(document.getElementById("testoRicercaVisible").value).trim()=="") || (String(document.getElementById("testoRicercaVisible").value).trim().length < 3))
	{
		alert("E' necessario inserire un termine da ricercare, di lunghezza superiore a tre caratteri!")
		document.getElementById("testoRicercaVisible").value = ""
		document.getElementById("testoRicercaVisible").focus();
	}
	else
	{
		document.frmRicerca.testoRicerca.value=document.getElementById("testoRicercaVisible").value
		document.frmRicerca.action="../Search/search.asp"
		document.frmRicerca.submit();
	}	
}

function manageInvio()
{
	if ((window.event.keyCode==13) && (String(document.activeElement.id)=="testoRicercaVisible"))
		ricercasito()
}	

function coloraTD(tabella)
{	
	var id = String(tabella.id).substring(3,tabella.id.length)
	if ((divSelected != "") && (divSelected.id != ("td_" + MenuSelected)))
		DecoloraTD (divSelected)	
	if (tabella.id != ("td_" + MenuSelected))
	{
		tabella.style.backgroundColor="#96BBD8";
		tabella.style.Color="#F9DC8D";	
	}
	divSelected = tabella		
}

function DecoloraTD(tabella)
{
	tabella.style.backgroundColor="";
	tabella.style.color="";
}

function load(idModulo)
{
	/*document.frmNavigazione.idModulo.value=idModulo;
	document.frmNavigazione.action = "../Content/Content.asp";
	document.frmNavigazione.submit();*/
	document.location.href = "../Content/Content.asp?idModulo=" + idModulo
}

function scriviLayer(testo) 
{	
	if (ie4) 
		content = document.all["layerContent"].innerHTML=testo
	else 
		if (ns4) 
		{
			with (content = document.layers["layerContent"].document) 
			{
				write(testo);
				close();
			}  
		}
		else 
			content=document.getElementById('layerContent').innerHTML=testo
}

String.prototype.trim = function()
{
	return this.replace(/(^\s*)|(\s*$)/g, "");
}
function nocharRicerca(campo, checkStr, caratteri) 
{
	if (campo.value != "")
	{
		caratteri += '"' ;
		for (var i=0; i<=caratteri.length-1; i++) 
		{
			if (checkStr.indexOf(caratteri.substring(i,i+1))!="-1") 
			{
				alert("Il carattere \"" + caratteri.substring(i,i+1) + "\" non è un carattere consentito.");
		        campo.focus(); break 
		    } 
		} 
	}
}

function onlyCharRicerca()
{
	var x = window.event.keyCode;
	if (!(((x>=65) && (x<=90)) || ((x>=97) && (x<=122)) || (x==39) || (x==32) || (x==44) || (x==46) ))
		event.returnValue = false
}

function changeBanner()
{
	document.getElementById("td_banner").innerHTML = ArrayBanner[indexBanner]
	if (indexBanner < ArrayBanner.length-1)
		indexBanner = indexBanner + 1
	else
		indexBanner	= 0	
}
