// -------------------------------------------
// Richiamo asp gestione legenda
// -------------------------------------------
function gestioneLegenda(numMax) {
	strTitolo=joinArray(listaLayer,'titolo');
	strTipo=joinArray(listaLayer,'tipo');
	strTitoloCat=joinArray(listaLayerCat,'titolo');
	strTipoCat=joinArray(listaLayerCat,'tipo');
	Pagina = 'CWGestioneLegenda.asp?Livelli='+strTitolo+'&Tipo='+strTipo+'&LivelliCat='+strTitoloCat+'&CatTipo='+strTipoCat+'&NumMax='+numMax+'&Stile='+stile;
	Opzioni = 'status=yes, toolbar=yes, menubar=no, width=800, height=400, resizable=yes, scrollbars=no';
	WinGestLeg = window.open(Pagina, 'GestioneLegenda', Opzioni)
	WinGestLeg.focus();
return;
}


function gestioneLegendaBD(numMax) {
	strTitolo=joinArray(listaLayer,'titolo');
	strTipo=joinArray(listaLayer,'tipo');
	Pagina = 'CWGestioneLegendaBD.asp?Livelli='+strTitolo+'&Tipo='+strTipo+'&CodApplicazione='+CodApplicazione+'&NumMax='+numMax+'&Stile='+stile;
	Opzioni = 'status=yes, toolbar=yes, menubar=no, width=800, height=400, resizable=yes, scrollbars=no';
	WinGestLeg = window.open(Pagina, 'GestioneLegenda', Opzioni)
	WinGestLeg.focus();
return;
}


//--------------------------------------------------------------------------------------------
//-- Funziona aggiornaMappa - richiamata dal modulo CwGestioneLegenda.asp
//-- Aggiunge/Toglie i livelli da mappa e legenda
//--------------------------------------------------------------------------------------------
function aggiornaMappa(livTitoloPresenti,livTitoloNuovi) {
	livTitoloRimasti=livTitoloPresenti.split(',');
//---------------------------------------------------------------
//-- Levo i livelli dalla mappa e aggiorno l'array dei livelli
//---------------------------------------------------------------
	listaLayerAppo = new Array(); 
	var k=0;
	for(var i=0;i<listaLayer.length;i++){
		flagLevare=1; 
		for(j=0;j<livTitoloRimasti.length;j++){
			if (listaLayer[i].titolo==livTitoloRimasti[j]){
				flagLevare=0;
			}
		}
		if (flagLevare==1) { 
			if (document.ECWView1.DeleteLayer(listaLayer[i].titolo) < 0 ) {
				alert('Cancellazione Livello ' + document.ECWView1.GetLastErrorText());
			}
//-- Se il livello è di tipo "find" nascondo il livello find 
			if (livFind==listaLayer[i].titolo){
				if (document.ECWView1.DeleteLayer('find') < 0 ) {
					alert('Cancellazione Livello Find' + document.ECWView1.GetLastErrorText());
				}
			}
//-- Se il livello è di tipo "find2" nascondo il livello find2
			if (livFind2==listaLayer[i].titolo){
				if (document.ECWView1.DeleteLayer('find2') < 0 ) {
					alert('Cancellazione Livello Find2' + document.ECWView1.GetLastErrorText());
				}
			}		
		}		
		else {
			listaLayerAppo[k]=listaLayer[i];
			k=k+1;
		}
	}
//---------------------------------------------------------------
//-- Aggiungo i nuovi livelli e aggiorno l'array dei livelli
//---------------------------------------------------------------
// Array definitivo
	listaLayer = new Array();
// Array dei livelli da aggiungere
	listaLayerAggiungere = new Array();
	livTitoloAggiungere=livTitoloNuovi.split(',');
// Carico l'array definitivo con quelli rimasti da prima (listaArrayAppo) 
	for(var i=0;i<listaLayerAppo.length;i++){
		listaLayer[i]=listaLayerAppo[i];
	}
// carico l'array dei livelli nuovi leggendo i dati dagli array del catalogo
	if (livTitoloAggiungere!=''){
		for(i=0;i<livTitoloAggiungere.length;i++){
			for(j=0;j<listaLayerCat.length;j++){
				if (livTitoloAggiungere[i]==listaLayerCat[j].titolo) {
					listaLayerAggiungere[i]=listaLayerCat[j];
				}
			}
			if (listaLayerAggiungere[i].tipo!='ECW') {
				inizializzaVettoriale(listaLayerAggiungere[i].titolo,listaLayerAggiungere[i].name);
			}
	// inizializzo l'array contentente lo stato dei livelli (acceso/spento)
			if ((listaLayerAggiungere[i].scMin<parseInt(strScala))&&(listaLayerAggiungere[i].scMax>=parseInt(strScala))) 
			{
				listaLayerAggiungere[i].stato=1;
			}
			else {
				listaLayerAggiungere[i].stato=3;
			}
	// aggiorno l'array dei livelli - (valeria: aggiunto controllo livello queryable)
			listaLayer[listaLayer.length]=listaLayerAggiungere[i];
			if (livAttivo==-1){
				//alert(listaLayerAggiungere[i].titolo+' '+listaLayerAggiungere[i].queryable);
				if (listaLayerAggiungere[i].queryable==1){
					livAttivo=listaLayer.length-1;
				}
			}
		}
// aggiorno la mappa con i nuovi livelli
	aggiornaLivelli(listaLayerAggiungere);
	}
//riscrivo la legenda con il nuovo array
	scriviLegenda();
// Controllo livello con logo - se non c'è rendo il DIV trasparente
	for(i=0;i<listaLayer.length;i++){
//alert(listaLayer[i].titolo+' '+listaLayer[i].livHidden);
		flagLogo=false;
		if (listaLayer[i].titolo==livLogo){
			flagLogo=true;
		}
	}
//	if (flagLogo) document.getElementById('LOGO').style.display='block';
//	else document.getElementById('LOGO').style.display='none';
}

//--------------------------------------------------------------------------------------------
//-- Funzione aggiungiLayer - richiamata dal modulo _AggiungiCarte.asp del Repertorio Cartografico
//-- Aggiunge i livelli corrispondenti ad una carta
//--------------------------------------------------------------------------------------------
function aggiungiLayer(livTitoloPresenti,stringaXML) {
	livTitoloRimasti=livTitoloPresenti.split(',');
//---------------------------------------------------------------
//-- Levo i livelli dalla mappa e aggiorno l'array dei livelli
//---------------------------------------------------------------
	listaLayerAppo = new Array(); 
	var k=0;
	for(var i=0;i<listaLayer.length;i++){
		flagLevare=1; 
		for(j=0;j<livTitoloRimasti.length;j++){
			if (listaLayer[i].titolo==livTitoloRimasti[j]){
				flagLevare=0;
			}
		}
		if (flagLevare==1) { 
			if (document.ECWView1.DeleteLayer(listaLayer[i].titolo) < 0 ) {
				alert('Cancellazione Livello ' + document.ECWView1.GetLastErrorText());
			}
//-- Se il livello è di tipo "find" nascondo il livello find 
			if (livFind==listaLayer[i].titolo){
				if (document.ECWView1.DeleteLayer('find') < 0 ) {
					alert('Cancellazione Livello Find' + document.ECWView1.GetLastErrorText());
				}
			}
//-- Se il livello è di tipo "find2" nascondo il livello find2
			if (livFind2==listaLayer[i].titolo){
				if (document.ECWView1.DeleteLayer('find2') < 0 ) {
					alert('Cancellazione Livello Find2' + document.ECWView1.GetLastErrorText());
				}
			}		
		}		
		else {
			listaLayerAppo[k]=listaLayer[i];
			k=k+1;
		}
	}
// Array definitivo
	listaLayer = new Array();
// Carico l'array definitivo con quelli rimasti da prima (listaArrayAppo) 
	for(var i=0;i<listaLayerAppo.length;i++){
		listaLayer[i]=listaLayerAppo[i];
	}
//---------------------------------------------------------------
//-- aggiungo i nuovi livelli
//---------------------------------------------------------------
//alert(stringaXML);

// sostituisco il carattere & con il carattere @ perchè altrimenti il parser xml non funziona	
	stringaXML=stringaXML.replace(/&/gi,'@')
// Parsing della stringa XML e costruzione dell'array di oggetti layer
	if (window.ActiveXObject) 
	{
		var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async="false";
		xmlDoc.loadXML(stringaXML);
	}
	else
	{
		var parser=new DOMParser();
		var xmlDoc=parser.parseFromString(stringaXML,"text/xml");
	}
	layers=xmlDoc.getElementsByTagName("Layer");
	listaLayerAggiungere = new Array();
	indLiv=0;
	for(var i=0;i<layers.length;i++){
		LivTitolo=layers[i].getAttribute('titolo').replace(/@/gi,'&');
// Controllo che il livello non sia già presente nel qual caso non lo carico
		LivelloPresente=false;
		for(var j=0;j<listaLayer.length;j++){
			if (listaLayer[j].titolo==LivTitolo){
				LivelloPresente=true;
			}
		}
		if (!LivelloPresente){
			LivName=layers[i].getAttribute('name').replace(/@/gi,'&');
			LivUrl=layers[i].getAttribute('url').replace(/@/gi,'&');
			LivTipo=layers[i].getAttribute('tipo').replace(/@/gi,'&');
			LivLegImg=layers[i].getAttribute('legImg').replace(/@/gi,'&');
			LivLegTipo=layers[i].getAttribute('legTipo').replace(/@/gi,'&');
			LivQueryable=layers[i].getAttribute('queryable').replace(/@/gi,'&');
			LivInfoOutput=layers[i].getAttribute('infoOutput').replace(/@/gi,'&');
			LivInfoUrl=layers[i].getAttribute('infoUrl').replace(/@/gi,'&');
			LivSldUrl=layers[i].getAttribute('sldUrl').replace(/@/gi,'&');
			LivScMin=layers[i].getAttribute('scMin');
			LivScMax=layers[i].getAttribute('scMax');
			LivTipoGeom=layers[i].getAttribute('tipoGeom');
			LivStyleCurrent=layers[i].getAttribute('styleCurrent');
			LivGroup=layers[i].getAttribute('grouped');
			LivUID=layers[i].getAttribute('uid');
			LivColLabel=layers[i].getAttribute('colLabel');
			LivDesLabel=layers[i].getAttribute('desLabel');
			LivLogoImg=layers[i].getAttribute('logoImg');
			LivStato='0';
			LivTrasp='100';
			LivOrdine=i;
			LivHidden='0';
			Filtro='';
			xmlRules=layers[i].getElementsByTagName("Rule");
			listaRule = new Array();
			for(var j=0;j<xmlRules.length;j++){
				ruleName =  xmlRules[j].getAttribute('name').replace(/@/gi,'&');
				ruleUrl =  xmlRules[j].getAttribute('url').replace(/@/gi,'&');
				listaRule[j] = new rule(ruleName,ruleUrl);
			}
			LivNumClassi=xmlRules.length
			listaLayerAggiungere[indLiv] = new layer(LivTitolo,LivName,LivUrl,LivTipo,LivLegImg,LivLegTipo,LivQueryable,LivInfoOutput,LivInfoUrl,LivSldUrl,LivScMin,LivScMax,LivOrdine,LivStato,LivTrasp,LivNumClassi,listaRule,LivHidden,LivTipoGeom,LivStyleCurrent,LivGroup,LivUID,LivColLabel,LivDesLabel,Filtro,LivLogoImg);
			if ((listaLayerAggiungere[indLiv].scMin<parseInt(strScala))&&(listaLayerAggiungere[indLiv].scMax>=parseInt(strScala))) 
			{
				listaLayerAggiungere[indLiv].stato=1;
			}
			else {
				listaLayerAggiungere[indLiv].stato=3;
			}
			if (listaLayerAggiungere[indLiv].tipo!='ECW') {
				inizializzaVettoriale(listaLayerAggiungere[indLiv].titolo);
			}
			indLiv++;
		}
	}	
// aggiorno la mappa con i nuovi livelli
	aggiornaLivelli(listaLayerAggiungere);
// aggiungo i nuovi livelli nell'array dei livelli
	for(var i=0;i<listaLayerAggiungere.length;i++){
		listaLayer[listaLayer.length]=listaLayerAggiungere[i];
	}
//riscrivo la legenda con il nuovo array
	scriviLegenda();
	inizializzaLogo();	

}


