// JavaScript Document

function cercaDocumentMappa() 
{
	for (i=0; i < window.parent.frames.length; i++) 
		if (window.parent.frames[i].name == "cartografia")
		{
			return window.parent.frames[i].document;
			break;
		}
	return null;
}

function cercaDocumentMiniview() 
{
	for (i=0; i < window.parent.frames.length; i++) 
		if (window.parent.frames[i].name == "miniview")
		{
			return window.parent.frames[i].document;
			break;
		}
	return null;
}

function SendViewx(x, y, w, h) 
{
    alert("SendView");
	docApplet = cercaDocumentMiniview();
	if ( docApplet != null )
	{
		//docApplet.applets[0].SetRemoteView(x, y, w, h);
		docApplet.cercaDocumentMiniview(x, y, w, h);
	}
}

function appletCerca(tema, posizione) 
{
	docApplet = cercaDocumentMappa();
	if ( docApplet != null )
	{
		docApplet.applets[0].TrovaOggetto(tema, posizione);
	}
}

function appletCercaXY(tema, x, y) 
{
	docApplet = cercaDocumentMappa();
	if ( docApplet != null )
	{
		docApplet.applets[0].TrovaOggettoXY(tema, x, y);
	}
}

function bloccaEvento(e)
{ //prevent action from bubbling upwards
	if (typeof e!="undefined")
		e.stopPropagation()
	else
		event.cancelBubble=true
}

// Attiva/disattiva tutti i tipi di un tema
// ulId è di un CB Tema inserito in una LI contenente anche la UL che contiene tutte le LI del blocco
// ottengo quindi l'oggetto UL che contiene tutte le LI che a loro volta contengono i CB dei temi
function cbTipiAccendiSpegni(ulId, stato) 
{
	li =document.getElementById(ulId).parentNode;
	if (!li.childNodes || li.childNodes.length==0) 
	{ 
		return false; 
	}
	
	for (var iLI=0; iLI < li.childNodes.length; iLI++) 
	{
		ul = li.childNodes[iLI];
		if (ul.nodeName == "UL") 
		{
			// Iterate LIs
			for (var itemi=0; itemi < ul.childNodes.length; itemi++) 
			{
				var item = ul.childNodes[itemi];
				if (item.nodeName == "LI") 
				{
					// Iterate things in this LI
					for (var sitemi =0; sitemi < item.childNodes.length; sitemi++) 
					{
						var sitem = item.childNodes[sitemi];
						if (sitem.nodeName=="INPUT") 
						{
							sitem.checked =stato;
						}
					}
				}
			}
			break;
		}
	}
}

// Attiva il CB del tema se almeno un tipo è attivo
// ulId è di un CB Tipo inserito in una LI contenuta in una UL contenuta in una LI contenente il CB del tema
// controllo tutti i CB della UL in OR e assegno il CB del tema
function cbTemaAccendiSpegni(ulId, tema, docApplet) 
{
	statoCBCliccato = document.getElementById(ulId).checked;
	
	ulLI =document.getElementById(ulId).parentNode.parentNode;
	if (!ulLI.childNodes || ulLI.childNodes.length==0) 
	{ 
		return false; 
	}
	
	var stato =false;
	// Iterate LIs
	for (var itemi=0; itemi < ulLI.childNodes.length; itemi++) 
	{
		var item = ulLI.childNodes[itemi];
		if (item.nodeName == "LI") 
		{
			// Iterate things in this LI
			for (var sitemi =0; sitemi < item.childNodes.length; sitemi++) 
			{
				var sitem = item.childNodes[sitemi];
				if (sitem.nodeName=="INPUT") 
				{
					stato |= sitem.checked;
					
					if ( statoCBCliccato && !sitem.checked )
					{
						// Potrei avere un tema deselezionato e aver attivato un tipo
						// in questo caso l'applet non sa quali tipi devono rimanere spenti
						// per cui devo informarla
						if ((sitem.childNodes != null) && (sitem.attributes.length > 0))
						{
							for (var iAtt =0; iAtt < sitem.attributes.length; iAtt++) 
							{
								var attr = sitem.attributes[iAtt];
								if (attr.nodeName=="tipo")
								{
									docApplet.applets[0].MostraTipo(tema, attr.nodeValue, false);
								}
							}
						}
					}
				}
			}
		}
	}

	li = ulLI.parentNode;
	for (var sitemi =0; sitemi < li.childNodes.length; sitemi++) 
	{
		var sitem = li.childNodes[sitemi];
		if (sitem.nodeName=="INPUT") 
		{
			sitem.checked = stato;
			break;
		}
	}
}

function appletMostraTema(e, tema, el) 
{
	bloccaEvento(e);
	cb =document.getElementById(el);
	if ( cb != null )
	{
		cbTipiAccendiSpegni(el, cb.checked);
		docApplet = cercaDocumentMappa();
		if ( docApplet != null )
		{
			docApplet.applets[0].MostraTema(tema, cb.checked);
		}
	}
}

function appletMostraTemaConDB(e, tema, el) 
{
	bloccaEvento(e);
	cb =document.getElementById(el);
	if ( cb != null )
	{
		cbTipiAccendiSpegni(el, cb.checked);
		docApplet = cercaDocumentMappa();
		if ( docApplet != null )
		{
			docApplet.applets[0].MostraTemaConDB(tema, cb.checked);
		}
	}
}

function appletMostraTipo(tema, tipo, el) 
{
	docApplet = cercaDocumentMappa();
	cb =document.getElementById(el);
	if ( cb != null )
	{
		if ( docApplet != null )
		{
			docApplet.applets[0].MostraTipo(tema, tipo, cb.checked);
		}
		// Devo farlo dopo perchè esegue l'eventuale disattivazione dei tipi spenti
		// visto che MostraTipo, quando il tema era disattivo, accende tutti i tipi,
		cbTemaAccendiSpegni(el, tema, docApplet);
	}
}

function expandIt(el) 
{
	whichEl =document.getElementById(el + "Child");
	if (whichEl.style.visibility == "hidden") 
	{
		whichEl.style.visibility="visible";
		whichEl.style.display="block";
	}
	else 
	{
		whichEl.style.visibility="hidden";
		whichEl.style.display="none";
	}
	arrange();
}

// Crea le frecce di scorrimento avanti e indietro nei blocchi di risultati
function jsFrecceScorrimento(startRec, nRec, righeMax, thisFile, sParam)
{
	if ((startRec > 0) || (nRec > startRec + righeMax))
	{
		document.write("<p><div\">");
		
		hrefInizio = "<a class=ResultTitolo href=\"" + thisFile + "?" + sParam + "&startRec=";
		if ( startRec > 1 )
		{
			document.write(hrefInizio + 1 						+ "\"><<  </a>");
			document.write(hrefInizio + (startRec - righeMax) 	+ "\"><-- </a>");
		}
		if ( nRec >= startRec + righeMax )
		{
			document.write(hrefInizio + (startRec + righeMax) 	+ "\"> --></a>");
			document.write(hrefInizio + (nRec - righeMax + 1) 	+ "\">  >></a>");
		}
		
		document.write("</div></p>");
	}
}

function avvisoCampoVuoto(campo)
{
    alert("Va inserito un valore nel campo: " + campo);
}

function ControllaPIVA(pi)
{
	if( pi == '' )  return '';
	if( pi.length != 11 )
		return "La lunghezza della partita IVA non è\n" +
			"corretta: la partita IVA dovrebbe essere lunga\n" +
			"esattamente 11 caratteri.\n";
	validi = "0123456789";
	for( i = 0; i < 11; i++ ){
		if( validi.indexOf( pi.charAt(i) ) == -1 )
			return "La partita IVA contiene un carattere non valido `" +
				pi.charAt(i) + "'.\nI caratteri validi sono le cifre.\n";
	}
	s = 0;
	for( i = 0; i <= 9; i += 2 )
		s += pi.charCodeAt(i) - '0'.charCodeAt(0);
	for( i = 1; i <= 9; i += 2 ){
		c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) );
		if( c > 9 )  c = c - 9;
		s += c;
	}
	if( ( 10 - s%10 )%10 != pi.charCodeAt(10) - '0'.charCodeAt(0) )
		return "La partita IVA non è valida:\n" +
			"il codice di controllo non corrisponde.\n";
	return '';
}

function ControllaCF(cf)
{
	var validi, i, s, set1, set2, setpari, setdisp;
	if( cf == '' )  return '';
	cf = cf.toUpperCase();
	if( cf.length != 16 )
		return "La lunghezza del codice fiscale non è\n"
		+"corretta: il codice fiscale dovrebbe essere lungo\n"
		+"esattamente 16 caratteri.\n";
	validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	for( i = 0; i < 16; i++ ){
		if( validi.indexOf( cf.charAt(i) ) == -1 )
			return "Il codice fiscale contiene un carattere non valido `" +
				cf.charAt(i) +
				"'.\nI caratteri validi sono le lettere e le cifre.\n";
	}
	set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
	s = 0;
	for( i = 1; i <= 13; i += 2 )
		s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
	for( i = 0; i <= 14; i += 2 )
		s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
	if( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) )
		return "Il codice fiscale non è corretto:\n"+
			"il codice di controllo non corrisponde.\n";
	return "";
}

function mapTool() 
{
	if ( !navigator.javaEnabled() )
	{
		document.write("<div align=center>Abilitare Java " +
					   "(<b>Strumenti/Opzioni</b>, pagina <b>Contenuti</b>)</div>" );
		return;
	}
	
    var _info = navigator.userAgent;
    //alert(_info);
    var _ns = false;
    var _ns6 = false;
    var _ie = (_info.indexOf("MSIE") > 0 && _info.indexOf("Win") > 0 && _info.indexOf("Windows 3.1") < 0);
    var _ns = (navigator.appName.indexOf("Netscape") >= 0 && ((_info.indexOf("Win") > 0 && _info.indexOf("Win16") < 0 && java.lang.System.getProperty("os.version").indexOf("3.5") < 0) || (_info.indexOf("Sun") > 0) || (_info.indexOf("Linux") > 0) || (_info.indexOf("AIX") > 0) || (_info.indexOf("OS/2") > 0) || (_info.indexOf("IRIX") > 0)));
    var _ns6 = ((_ns == true) && (_info.indexOf("Mozilla/5") >= 0));

	document.writeln('<object codetype="application/java" \
					 classid="java:webmap.WebMapApplet.class" \
					 ARCHIVE = "doc/WebMap.jar" \
					 WIDTH = 500 \
					 HEIGHT = 470 \
					 NAME = "WebMap" \
					 ALIGN = middle \
					 VSPACE = 0 \
					 HSPACE = 0> \
					  <param name="FileSfondo" value="sfondo_grigio.jpg"> \
					  <param name="EnableDebugMode" value="true"> \
					  <param name="ColorBkApplet" value="255,255,255"> \
					  <param name="Porta" value="8080"> \
					  <param name="XMLParam" value="mappa_comuni.xml"> \
					  <param name="RectMappa" value="0,0,500,450"> \
					  <param name="xRectMostra" value="400,5,90,90"> \
					</object>');

/*
    if (_ie == true) 
	{
		document.writeln('<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" 
						 WIDTH = 950 HEIGHT = 500 NAME = "ProjectTool" ALIGN = middle 
						 VSPACE = 0 HSPACE = 0  codebase="http://java.sun.com/update/1.5.0/jinstall-1_5-windows-i586.cab#Version=5,0,0,5"><noembed><xmp>');
    else if (_ns == true && _ns6 == false) document.writeln('<embed ' +
	    'type="application/x-java-applet;version=1.5" \
            CODE = "webmat.setmodulo.WMSetModuloApplet.class" \
            ARCHIVE = "WebMat.jar" \
            NAME = "ProjectTool" \
            WIDTH = 950 \
            HEIGHT = 500 \
            ALIGN = middle \
            VSPACE = 0 \
            HSPACE = 0 \
            MAYSCRIPT = true \
            FileSfondo ="sfondo_grigio.jpg" \
            EnableDebugMode ="true" \
            ColorBkApplet ="255,255,255" \
            JSSelModulo ="selectInstallationLayout" \
            JSSelMateriale ="selectItem" \
            URLServlet ="http://www.hh-househome.com/catalog-hh/hh/basketImport.do" \
            Porta ="80" \
            URLGetTesti ="http://www.hh-househome.com/catalog-hh/webmat/applet_it.properties" \
            sessionCookieName ="JSESSIONID" \
            sessionCookieValue ="6224BB723F9677F833BA8790EA2B3585" ' +
	    'scriptable=false ' +
	    'pluginspage="http://java.sun.com/products/plugin/index.html#download"><noembed><xmp>');
	*/
	/*
	document.writeln('<applet  \
					 CODE = "webmap.WebMapApplet.class" \
					 ARCHIVE = "doc/WebMap.jar" \
					 WIDTH = 500 \
					 HEIGHT = 470 \
					 NAME = "WebMap" \
					 ALIGN = middle \
					 VSPACE = 0 \
					 HSPACE = 0 \
					 MAYSCRIPT = true></xmp> \
      <param name="FileSfondo" value="sfondo_grigio.jpg"> \
      <param name="EnableDebugMode" value="true"> \
      <param name="ColorBkApplet" value="255,255,255"> \
      <param name="Porta" value="80"> \
      <param name="XMLParam" value="mappa_comuni.xml"> \
      <param name="RectMappa" value="0,0,500,450"> \
      <param name="xRectMostra" value="400,5,90,90"> \
    </applet>');
	*/
	//document.writeln('</noembed>');
	//document.writeln('</embed>');
	//document.writeln('</object>');
}

