var TAG_TREE_COLOR_CLOSE = 'F8A756'; //beige'; //lightblue'; //#AAFFFF';
var TAG_TREE_COLOR_OPEN = 'lightsteelblue';//6AA7EB'; //3185E4'; //palegoldenrod'; //#b0c4de';

var ultimo = 1;
var attesaWindow;
// *************************** FUNZIONI OPAC


// inizia la richiesta del caricamento degli elementi 
// (monografie) di una collana
var target = null;
var makeTableMorph = false;
function elencoMonografie(idDescrizione,riga, divName) {
	
	// SE RIGA=-1 VUOL DIRE dire che vengo da dettaglio + volumi
	//var div = document.getElementById('div_'+idDescrizione);
    var div = document.getElementById(divName);
	var tr = document.getElementById('riga_'+riga);
	var img = document.getElementById('img_'+riga);
    var codbibFilter;

    try{
        var dropdownIndex = parent.document.getElementById('biblio').selectedIndex;
        codbibFilter = parent.document.getElementById('biblio')[dropdownIndex].value;
    }catch(e){
        codbibFilter = '0';
    }
	if (div.style.display=='none') {
        waiting_start();
	    //parent.Attesa();
	    //target='div_'+idDescrizione;
        target=divName
	    var url = "getSecondoLivello.do?idDescrizione="+idDescrizione+"&tipoPadre=L"; // collana
	    
        if(codbibFilter!=null)
            url = url+"&codbibFilter="+codbibFilter;
            
        if (riga==-1){
          url=url+"&completo=1"
          makeTableMorph = true;
        }

        getMonografie(url);
	    try {
			tr.bgColor="#EFEFEF";
			img.src="img/close.gif";
		}
		catch (ex) {}
		
	}
	else {
	  	target=null;
	    div.innerHTML = "";
	  	div.style.display='none';
        //parent.resize_iframe();
	    try {
		  	tr.bgColor="#FFFFFF";
		  	img.src="img/open.gif";
            
		}catch (ex) {
            
        }
	}  
}

// processa la response sul caricamento degli elementi 
// (monografie) di una collana
function processElencoMonografie() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
		    var targetDiv = null;
		    if (target!="") {
			    var targetDiv = document.getElementById(target);
			    targetDiv.style.display='block';
				targetDiv.innerHTML=req.responseText;
				waiting_stop();

                if(makeTableMorph)
                    morphTable('secondLevelTable', 70);
                //parent.chiudiAttesa();
                //parent.resize_iframe();
			}   
        } else {
			alert("Errore di comunicazione col server: " + req.statusText);
		}
	}
}


// OPAC_STYLE puo' assumere i valori: Native, Minimize_Popup
function elencoNRE(idDescrizione, opacStyle, progressivo, level, pagina, maxDocsPag) {
    //alert(progressivo + "--" + level + "--" + pagina + "--" + maxDocsPag);
	var parametri='';
    if (opacStyle=='Minimize_Popup') {
	    try {
		    if(progressivo!=null)
                parametri="&progressivo="+progressivo;

		    parametri=parametri+"&level=" + level + "&pagina=" + pagina + "&maxDocsPag=" + maxDocsPag;
			self.location.href="getNRE.do?idDescrizione="+idDescrizione+parametri;
		}
		catch (e) {alert(e);}
	}
	// ATTENZIONE: QUANDO SI RIATTIVA QUESTO ELEMENTO, BISOGNA PASSARE I PARAMETRI:
	// progressivo + "--" + level + "--" + pagina + "--" + maxDocsPag
	else open_win("elencoNRE",idDescrizione);
}


/*
function elencoPiuVolumi(idDescrizione, progressivo, level, pagina, maxDocsPag) {
    //alert(progressivo + "--" + level + "--" + pagina + "--" + maxDocsPag);
    try {
	    //Attesa();
	    parametri="&piuVolumi=1&progressivo="+progressivo+"&level=" + level + "&pagina=" + pagina + "&maxDocsPag=" + maxDocsPag;
		self.location.href="getNRE.do?idDescrizione="+idDescrizione+parametri;
	}
	catch (e) {alert(e);}
}*/


// OPAC_STYLE puo' assumere i valori: Native, Minimize_Popup
// from puo' assumere i valori:
// a) prestito
// b) prenotazione
// c) tessera
/*
function login_utente(nre, collocazione, from, opacStyle, prestitoLocale, bibRitiro) {
	
	var url = "login.jsp?nre="+nre+"&collocazione="+collocazione+"&from="+from+"&prestitoLocale="+prestitoLocale+"&codBibRitiro="+bibRitiro;
	//alert(url);
	
	// in questo caso, minimize popup si intende ritardare 
	// l'apertura del popup di login
	if (opacStyle=='Minimize_Popup') {
		var schermo_h = screen.height;
		var schermo_w = screen.width;
		var pop_h = 350;
		var pop_w = 430;
		var pos_x = (schermo_w - pop_w) / 2;
		var pos_y = (schermo_h - pop_h) / 2;
	    var winName="UnibiblioX - OPAC";
	    var toolbars="0";
	    if (from=="tessera") toolbars="1";
		var winStyle="location=0,directories=0,status=0,menubar=0,toolbars=0,scrollbars="+toolbars+",resizable=0,";
	        winStyle+="width=" +pop_w+ ",height=" +pop_h+ ",left=" +pos_x+ ",top=" +pos_y;
	    myPop = window.open(url,"winName",winStyle);
        myPop.focus();
	}
	else {
		document.location.href=url;
    }
    return false;
}*/


// *************************** FUNZIONI OPAC

// check del form di catalogazione, sia in inserimento che in modifica
function checkFormCatalogazione() {
    
	var form = self.document.catalogform;
    var messaggio = '';
    
    if (form["TF_Book/T_200/T_200_a-1-1"].value=='') messaggio+="Il titolo (Tag 200 a) e' obbligatorio. \n\r";
    if (form["TF_Book/T_676/T_676_a-1-1"].value=='') messaggio+="Il codice Dewey (Tag 676 a) e' obbligatorio. \n\r"; 
	
	if (messaggio=='') form.submit();
	else alert(messaggio); 

}


function gotoPage(page) {
     
     //alert ("page  - " + page );
     document.forms.searchform.pagina.value=page;
     
     /*
     if (page==-1) document.forms.searchform.pagina.value="prev";
     else if (page==1) document.forms.searchform.pagina.value="next";
     else if (page=='first') document.forms.searchform.pagina.value="first";
     else if (page=='last') document.forms.searchform.pagina.value="last";
     */
     
     //alert ("ok+++" + document.forms.searchform.pagina.value );
     
     document.forms.searchform.operation.value="spagina";
     document.forms.searchform.template.value="search";
     
     //alert ("ok+++" + document.forms.searchform.template.value );
     
     document.forms.searchform.submit();
     
}
	     
function apriSchedeSelezionate() {

    document.forms.searchform.operation.value="vis_schede";
    document.forms.searchform.docID.value = "-1";
    document.forms.searchform.action="viewCatalogScheda.do";
    document.forms.searchform.submit();
    
}	     
	            
function truncateLastUnderscore(stringa) {

    var familyName = stringa;
	var lastUnderscoreIndx = stringa.lastIndexOf('_');
	if (lastUnderscoreIndx > 0) 
	 familyName =stringa.substring(0, lastUnderscoreIndx);
	 
	return familyName;	
}


function pulisciField(nomeCampo) {

    //nomeCampo += "-1";
    
    //alert("il parametro vale: "+nomeCampo);
    
 	var idx2 = nomeCampo.lastIndexOf('-');
 	var idx1 = nomeCampo.indexOf('-');
 	
    var dopo = "";
    if (idx1 == idx2) { 
    	dopo = "-1";
    }	

    //alert(name + dopo);
    
    var destField=document.forms.catalogform[nomeCampo];

    if (destField.value=="") {
    
	    if (idx1 != idx2) nomeCampo = nomeCampo.substring(0, idx2);
	    
	    for (var p = 1; (destField!=undefined) ; p++ ) { 
	        
	        //alert("Dentro il for" + nomeCampo + "-" + p);
	        
	      	destField=document.forms.catalogform[nomeCampo+ "-" +p];
	      	if (destField!=undefined) 
			    if (destField.value!="") {
			        //alert("campo:" + destField);
			    	destField.value='';
			    	break;
			    }
		}
	
	}
	else destField.value = '';
	//alert("Assegnato : " + destField.value);

    

}
// aggiunge un singolo campo al tag di primo livello....
function addField(name) {

	 //alert("OGGETTO PLACE: place_" + name);
	 
	 tdPlace = document.getElementById("place_"+name);
	 
	 ultimo = 1;
	 
	 var menouno="-1";
     
     
     // le textarea sono un punto avanti rispetto ai place/tag
	 var idx2 = name.lastIndexOf('-');
	 var idx1;
	 var prima;
	 var dopo;

	 if (idx2 > 0) {
	 		 	
        //alert("entrata: " + name);
        idx1 = 	name.indexOf('-');
        
        prima = name.substring(0, idx1);
        //alert("prima: " + prima);
        
        dopo  = name.substring(idx2+1);
        //alert("dopo: " + dopo);	 	

	 	var numero = Number(dopo) + 1;
	 	name = prima + "-" + numero;
	 	
	 }
	 else name = name + menouno;
	 
	 //alert ("devo prendere : " + "TF_" + name + "-" + ultimo);
	 
	 // INTANTO PRENDO IL PRIMO !!
	 tdOpt = document.getElementById("TF_"+name+"-"+ultimo);
	 tdOpt_ori = document.getElementById("TF_"+name+"-"+ultimo);
	 
	 var loopName;
	 while (tdOpt!=null) {
	 
	        ultimo++;
		    loopName=name+"-"+ultimo;
		 	
		 	tdOpt = document.getElementById("TF_"+loopName);
	 }
	 
	 
	 var opt = tdOpt_ori.cloneNode(true);
	 
	 
	 opt.name=loopName;
	 opt.id="TF_"+loopName;
	 opt.value="";
     tdPlace.appendChild(opt);  

}

// aggiunge un intero tag di primo livello....
function addTag(nomeOggetto) {
     
	var place = "TABLEDIV_" + nomeOggetto;
	var name = "TABLE_" + nomeOggetto;
	 
	//alert(place + "---" + name);
	 
	tdPlace = document.getElementById(place);
	 
	//alert ("place="+tdPlace);
	 
	ultimo = 0;
	 
	// INTANTO PRENDO IL PRIMO !!
	tdOpt_ori = document.getElementById(name);
	tdOpt = document.getElementById(name);

	var loopName;
	while (tdOpt!=null) {
	    ultimo++;
	    loopName=name+"-"+ultimo;
		//alert ("nome table_" + loopName) ;	
	 	tdOpt = document.getElementById(loopName);
	}
	//alert("ultimo=" + ultimo);
	
	
	var opt = tdOpt_ori.cloneNode(true);
	 
	opt.name=loopName;
	opt.id=loopName;
 
	// Serve per rinominare il div in modo che funzioni 
	// l'apertura/chiusura delle aree
	var elementColl = opt.getElementsByTagName("DIV");
	for (ecCount=0; ecCount < elementColl.length; ecCount++) {
	
		if(elementColl[ecCount].className=='div_albero') {
			    
	        //alert("prima: "+elementColl[ecCount].id);
			elementColl[ecCount].id=nomeOggetto+"-"+ultimo;
			//alert("dopo: "+elementColl[ecCount].id);
					
		}
		else if (elementColl[ecCount].className=='tag_tree') {
	        //alert("tag_tree prima: "+elementColl[ecCount].id);
			elementColl[ecCount].id="tag_"+nomeOggetto+"-"+ultimo;
			//alert("tag_tree dopo: "+elementColl[ecCount].id);		
		}
	}    
    
    // Serve per rinominare le textarea del nuovo tag per gestire
	// il salvataggio dei dati
	var elementColl = opt.getElementsByTagName("TEXTAREA");
	//var appID;
	var prima;
	var dopo;
	var idx1;
	var idx2;
	var textareaUltimo=ultimo;
	
	// le textarea sono un punto + avanti degli altri riferimenti
	textareaUltimo = ultimo+1; 
	
	for (ecCount=0; ecCount < elementColl.length; ecCount++) {
	        
	        // esempio: un tag T_200_a-1-1 diventa T_200_a-2-1 oppure
	        //                 T_200_a-4-1 diventa T_200_a-5-1 oppure
	        
	        //alert("entrata: " + elementColl[ecCount].id);
	        idx1 = 	elementColl[ecCount].id.indexOf('-');
	        idx2 = 	elementColl[ecCount].id.lastIndexOf('-');
	        
	        prima = elementColl[ecCount].id.substring(0, idx1);
	        //alert("prima: " + prima);
	        
	        dopo  = elementColl[ecCount].id.substring(idx2+1);
	        //alert("dopo: " + dopo);
	        
            elementColl[ecCount].id=prima+"-"+textareaUltimo+"-"+dopo;
 
 	        idx1 = 	elementColl[ecCount].name.indexOf('-');
	        idx2 = 	elementColl[ecCount].name.lastIndexOf('-');
	        
	        prima = elementColl[ecCount].name.substring(0, idx1);
	        //alert("prima: " + prima);
	        
	        dopo  = elementColl[ecCount].name.substring(idx2+1);           
            
            elementColl[ecCount].name=prima+"-"+textareaUltimo+"-"+dopo;
            elementColl[ecCount].value="";
	}    
     
    // Serve per rinominare le <a> del nuovo tag per gestire
	// il salvataggio dei dati
	elementColl = opt.getElementsByTagName("A");
	for (ecCount=0; ecCount < elementColl.length; ecCount++) {
       
        if ((elementColl[ecCount].title.substring(0,4)=='Apri') ||
            (elementColl[ecCount].title.substring(0,7)=='Pulisci')) {
            
            var idx1;
            idx1 = 	elementColl[ecCount].type.indexOf('-');
            appID = elementColl[ecCount].type.substring(0, idx1);
			elementColl[ecCount].type=appID+"-"+textareaUltimo+"-"+dopo;
			//alert(elementColl[ecCount].title + " -- " + elementColl[ecCount].type);
			
		}
		else {
	        appID = elementColl[ecCount].type;
			elementColl[ecCount].type=appID+"-"+ultimo;		
		}
	}         

    // Serve per rinominare le <TD class="bordi2"> del nuovo tag per gestire
	// il place dove eventualmente aggiungere i field ripetuti
	elementColl = opt.getElementsByTagName("TD");
	for (ecCount=0; ecCount < elementColl.length; ecCount++) {
	
    	if(elementColl[ecCount].className=='bordi2') {
    	  try {
    		if (elementColl[ecCount].id!=undefined) {
	    	    //alert(elementColl[ecCount].id);
		        appID = elementColl[ecCount].id;
				elementColl[ecCount].id=appID+"-"+ultimo;
				//alert(elementColl[ecCount].id);
			}
		  } catch(e) {}
		}

	} 
	
	
	
    tdPlace.appendChild(opt);  

}


function display_div2(div) {			    			    
	//var tagName = "tag_" + div;
	//alert(div + "---" +document.getElementById(div));
	
	// carico tutti gli oggetti di tipo DIV del documento
 	var elementColl = document.getElementsByTagName("DIV");
	var lunghezza= div.length;
	var strConfronto;	
	var aperto=true;
	// memorizzo come ? il primo div della serie
	if (document.getElementById(div).style.display=='none') 
		aperto = false;
		
		//Ciclo su tutti gli oggetti di tipo DIV
		for (ecCount=0; ecCount < elementColl.length; ecCount++) {
		    
		    // Se l'oggetto ? un DIV_ALBERO
			if(elementColl[ecCount].className=='div_albero') {
				// CONTROLLO se ? quello che devo chiudere, la parte iniziale 
				// della stringa rappresenta il tag, la parte seguente (ignorata)
				// indica l'elemento
			    strConfronto = elementColl[ecCount].id.substr(0,lunghezza);
				if (strConfronto==div) {
				    //Se il primo elemento della serie di tag ? chiuso
					if(aperto)
						elementColl[ecCount].style.display='none';
					else	
						elementColl[ecCount].style.display='block';
				}
			}
		}      
        
        // Chiusi i div, devo cambiare il colore del tag...
        // quindi prendo tutti gli oggetti di dipo TD
		elementColl = document.getElementsByTagName("TD");
		for (ecCount=0; ecCount < elementColl.length; ecCount++) {
			// Se ? un TD di classe "tag_tree"
			if(elementColl[ecCount].className=='tag_tree') {
				// Confronto dal quarto carattere in poi, perch? i 
				// primo 4 contengono "tag_" che va scartato
			    strConfronto = elementColl[ecCount].id.substr(4,lunghezza+4);
			    // Se lo trovo cambio il colore
				if (strConfronto==div) {
				    //Se il primo elemento della serie di tag ? chiuso
					if(aperto)
						elementColl[ecCount].style.backgroundColor=TAG_TREE_COLOR_CLOSE;
					else	
						elementColl[ecCount].style.backgroundColor=TAG_TREE_COLOR_OPEN;
				}
			}
		}


		/*
		try {
			document.getElementById(tagName).style.backgroundColor=TAG_TREE_COLOR_CLOSE;
		} catch(e) {}	
		*/
		/*
		try {
			document.getElementById(tagName).style.backgroundColor=TAG_TREE_COLOR_OPEN;
		} catch(e) {}
		*/	
}



function display_div(div) {			    			    
	var tagName = "tag_" + div;
	 //alert(div + "---" +document.getElementById(div));
	if(document.getElementById(div).style.display=='none')
    {
		document.getElementById(div).style.display='block';
		try {
			document.getElementById(tagName).style.backgroundColor=TAG_TREE_COLOR_OPEN;
		} catch(e) {}
	}
    else
    {
		document.getElementById(div).style.display='none';
		try {
			document.getElementById(tagName).style.backgroundColor=TAG_TREE_COLOR_CLOSE;
		} catch(e) {}	
	}
}

var GLMostra = false;

function display_hidden(bottone) {
	var elementColl = document.getElementsByTagName("TABLE");
	var hiddenColl = new Array();
	
	var mostra=GLMostra;
    /*
    alert(bottone.src);
    id="TD_HIDE" title="Mostra Campi Nascosti"
    
    
    alert(bb.src);
    */
    var bb = document.getElementById("TD_HIDE");
    var bb1 = document.getElementById("bb_nascondi");
	if (mostra) {
		GLMostra = false;
		bb.title="Nascondi Campi";
		bb1.src="img/hide.gif";
	}
	else {
	    GLMostra = true;
	    bb.title="Mostra Campi Nascosti";
		bb1.src="img/show.gif";
	}
	
	for (ecCount=0; ecCount < elementColl.length; ecCount++)
	{
	  if (elementColl[ecCount].className == "hidden") {
		hiddenColl[hiddenColl.length] = elementColl[ecCount]
	  };
	};
	
	if (mostra)
	{
		for (hcCount = 0; hcCount < hiddenColl.length; hcCount++) {
		  hiddenColl[hcCount].style.display='block';
		};	
	}
	else
	{
		for (hcCount = 0; hcCount < hiddenColl.length; hcCount++) {
		  hiddenColl[hcCount].style.display='none';
		};				    
	}
}

// RICHIEDE getNodesByType.js		//probabilmente ELIMINABILE con un ciclo sulla prima e l'if .className
var GLExpandAll = true;
function tree(bottone) {			    			    
    var elementColl = document.getElementsByTagName("DIV");
    //var elementColl2 = document.getElementsByClassName("tag_tree");

    expandAll=GLExpandAll;

	var elementColl2 = document.getNodesByType(
	  Node.ELEMENT_NODE,
	  function(n) {
		return (n.className == "tag_tree");
	  }
	);
    
    var bb=document.getElementById("TD_TREE");
    var bb1=document.getElementById("bb_tree");

	if(expandAll) {
	    GLExpandAll = false;
		
		
		bb.title="Comprimi rami";
		
		bb1.src="img/tree_collapse.gif"
		
		for (ecCount=0; ecCount < elementColl.length; ecCount++) {
			/*
			if((elementColl[ecCount].id != 'schema_lab_current')
				&& (elementColl[ecCount].id != 'schema_lab_combo')
				&& (elementColl[ecCount].id != 'schema_current')
				&& (elementColl[ecCount].id != 'schema_combo'))
				elementColl[ecCount].style.display='block';
			*/
				if(elementColl[ecCount].className=='div_albero') {
				    
				    if ( (elementColl[ecCount].id!="Book/Header")
				      && (elementColl[ecCount].id!="Book/T_909") )
							elementColl[ecCount].style.display='block';
					
				}
				//alert(elementColl[ecCount].appearance); non funziona !!
		}
		for (ec2Count=0; ec2Count < elementColl2.length; ec2Count++)
		{
		
			if ( (elementColl2[ec2Count].id!="tag_Book/Header")
			  && (elementColl2[ec2Count].id!="tag_Book/T_909") )
				elementColl2[ec2Count].style.backgroundColor=TAG_TREE_COLOR_OPEN;
			else elementColl2[ec2Count].style.backgroundColor=TAG_TREE_COLOR_CLOSE;
			
		}	
	}
	else {
	    GLExpandAll = true;
	    
	    bb.title="Espandi rami";
	    bb1.src="img/tree_expand.gif"
		for (ecCount=0; ecCount < elementColl.length; ecCount++) {
			/*
			if((elementColl[ecCount].id != 'Book') 
				&& (elementColl[ecCount].id != 'preview') 
				&& (elementColl[ecCount].id != 'test_id')
				&& (elementColl[ecCount].id != 'schema_lab_current')
				&& (elementColl[ecCount].id != 'schema_lab_combo')
				&& (elementColl[ecCount].id != 'schema_current')
				&& (elementColl[ecCount].id != 'schema_combo'))
				elementColl[ecCount].style.display='none';
			*/
			if(elementColl[ecCount].className=='div_albero')
				elementColl[ecCount].style.display='none';
		}
		for (ec2Count=0; ec2Count < elementColl2.length; ec2Count++)
		{
			if(elementColl2[ec2Count].id != 'Book')
				elementColl2[ec2Count].style.backgroundColor=TAG_TREE_COLOR_CLOSE;
		}
	}
}

var myPop = 0;
var lastURL = "";
function open_win(myWin,param1,param2) {

	var schermo_h = screen.height;
	var schermo_w = screen.width;
	var pop_h = 400;
	var pop_w = 350;
	var pos_x = schermo_w - pop_w -1;
	var pos_y = schermo_h - pop_h -1;
    var winName="help_win";
	var url = "";
	var winStyle="location=0,directories=0,status=0,menubar=0,toolbars=0,scrollbars=0,resizable=0,";
	
	if (myWin == "selectAuthority") {
		url = "categoriaAuthority.do?command=LOAD_ELENCO&popup=true&nomeAutorita="+param1+"&nomeCampo="+param2;
		winStyle="location=0,directories=0,status=0,menubar=0,toolbars=0,scrollbars=1,resizable=0,";
		var pop_w = 450;
		pop_h = 550;
		pos_y = schermo_h - pop_h -1;		
	} else if (myWin == "selectDizionario") { 
		url = "categoriaAuthority.do?command=LOAD_DIZIONARIO&nomeAutorita="+param1+"&nomeCampo="+param2;
		// Nel caso di Materia, devo passare il parametro NATURA al metodo lato server, affinch? 
		// mi restituisca solo le MATERIE della NATURA corrente.
	    if (param2=="Book/T_939/T_939_b-1") {
	    	var natura = self.document.catalogform["TF_Book/T_939/T_939_a-1-1"];
	    	if (natura != undefined) {
	    		url = url + "&searchString="+natura.value;
	    	}
	    }

		winStyle="location=0,directories=0,status=0,menubar=0,toolbars=0,scrollbars=1,resizable=0,";
		var pop_w = 450;
		pop_h = 550;
		pos_y = schermo_h - pop_h -1;
		
				
	} else if (myWin == "linkAuthority") {
		url = "categoriaAuthority.do?command=SELEZIONA_AUTHORITY&campo="+param1;
		pop_h = 500;
		pop_w = 500;
		pos_x = (schermo_w - pop_w) / 2;
		pos_y = (schermo_h - pop_h) / 2;		
		winName="lonkAuth_win";
	} else if (myWin == "addAuthority") {
		url = "gestioneAuthority.do?command=NUOVA&name="+param1;
		pop_h = 500;
		pop_w = 500;
		pos_x = (schermo_w - pop_w) / 2;
		pos_y = (schermo_h - pop_h) / 2;		
		winName="addAuth_win";
	}	
	else if (myWin == "elencoNRE") {
		winStyle="location=0,directories=0,status=0,menubar=0,toolbars=0,scrollbars=1,resizable=0,";
	    url = "getNRE.do?idDescrizione="+param1;
		pop_h = 700;
		pop_w = 800;
		pos_x = (schermo_w - pop_w) / 2;
		pos_y = (schermo_h - pop_h) / 2;		
		winName="Elenco NRE";
	}
	myPop = window.open(url,winName,winStyle + "width=" +pop_w+ ",height=" +pop_h+ ",left=" +pos_x+ ",top=" +pos_y);
    myPop.focus();
}


function switch_titolo_dx(caller)
{			    			    
    var divColl = document.getElementsByTagName("DIV");
	for (ecCount=0; ecCount < divColl.length; ecCount++)
	{
		if(divColl[ecCount].className=='div_titolo_dx')
		{
			if (divColl[ecCount].id == caller)
			{
				divColl[ecCount].style.display='block';				
			}
			else
			{
				divColl[ecCount].style.display='none';
			}
		}
	}
}

function swiccia(caller)
{
	/*
	destForm = self.top.document.catalogform;
	thisForm = document.testform;
	var selectedColumn = thisForm.selector;
	var selectedIndex = selectedColumn.selectedIndex;
	*/
	
	if(caller == 0)
	{
		document.getElementById("preview").style.display='none';
		document.getElementById("id_test").style.display='block';
	}
	else
	{
		document.getElementById("id_test").style.display='none';
		document.getElementById("preview").style.display='block';
		switch_titolo_dx("titolo_dx_preview");
	}
}


function nand(nome) {
	try {
	if(document.getElementById(nome).style.visibility == 'hidden') {
		document.getElementById(nome).style.visibility='visible';
		//settiamo anche il padre com nonNascosto
		var fatherName = getFatherName(nome);
		document.getElementById(fatherName).className='nonHidden';
		//settiamo le label visibili
		document.getElementById(getLabelName(nome)).className='nonHidden';
		//settiamo le ImgHelp visibili
		document.getElementById(getImgHelpName(nome)).className='nonHidden';
		document.getElementById(getImgHelpName(nome)).style.display='block';
	}
	else
	{
		document.getElementById(nome).style.visibility='hidden';
		document.getElementById(nome).rows=1;
		var familyName = getFamilyName(nome);
		if(!checkFratelli(familyName)) //non ci sono fratelli visibili settiamo il padre nascosto
		{			
			var fatherName = getFatherName(nome);
			document.getElementById(fatherName).className='hidden';
		}
		//settiamo le label invisibili
		document.getElementById(getLabelName(nome)).className='hidden';
		//settiamo le ImgHelp invisibili
		document.getElementById(getImgHelpName(nome)).className='hidden';
		document.getElementById(getImgHelpName(nome)).style.display='none';
	}
	if (document.getElementById("autohide").checked)
		display_hidden(false);
	} catch(e) {};
}

function getFamilyName(nome)
{
	var lastUnderscoreIndx = nome.lastIndexOf('_');
	var familyName = nome.substring(0, lastUnderscoreIndx);
	//alert("familyName "+familyName);
	return familyName;	
}

function getFatherName(nome)
{
	var familyName = getFamilyName(nome);
	var underscoreIndx = familyName.indexOf('_');
	var lastSlashIndx = familyName.lastIndexOf('/');
	var fatherName = familyName.substring(underscoreIndx+1, lastSlashIndx);
	fatherName = "TABLE_"+fatherName
	//alert("fatherName "+fatherName);
	return fatherName;	
}

function getLabelName(nome)
{
	//var familyName = getFamilyName(nome);
	var underscoreIndx = nome.indexOf('_');
	var labelName = nome.substring(underscoreIndx+1, nome.length);
	labelName = "TABLE_"+labelName
	//alert("labelName "+labelName);
	return labelName;	
}

function getImgHelpName(nome)
{
	//var familyName = getFamilyName(nome);
	var underscoreIndx = nome.indexOf('_');
	var imgHelpName = nome.substring(underscoreIndx+1, nome.length);
	imgHelpName = "IMGHELP_"+imgHelpName
	//alert("labelName "+labelName);
	return imgHelpName;	
}

function checkFratelli(nome)	//true se c'e' almeno un fratello visibile
{
	//INPUT
	//var inputColl = document.getElementsByTagName("INPUT");
	//TEXTAREA
	var inputColl = document.getElementsByTagName("TEXTAREA");
	for (var i=0; i<inputColl.length; i++)
	{
		if((inputColl[i].id.indexOf(nome) != -1) && (!(inputColl[i].style.visibility == 'hidden')))	
		{
			return true;
		}
	}
	return false;
}


var MIN_ROWS = 1 ;
var MAX_ROWS = 30 ;
var MIN_COLS = 1 ;
var MAX_COLS = 65 ;

function cambiaTextAreaLength (e2, maxSize) {
	//alert(e);
	try {
	var e = document.getElementById(e2);
	var txtLength = e.value.length;
    var numRows = 0 ;
    //Split the textarea value at each linebreak.
    var arrNewLines = e.value.split("\n");
    
    // per cambiare i default

	  var L_MIN_COLS = 0 ;
	  var L_MAX_COLS = 0 ;    
	
	
    if (maxSize==undefined) {
    	L_MAX_COLS = MAX_COLS;
    	L_MIN_COLS = MIN_COLS;
    }
    else {
    	L_MAX_COLS = maxSize;
    	L_MIN_COLS = maxSize; 
    }

    
    for(var i=0; i<arrNewLines.length; i++)
    {
    	// for each element in the array we add 1 row to the TEXTAREA..
        //numRows++;
        if(arrNewLines[i].length >= L_MAX_COLS) 
		{
			// Within each element in our array, determine
			// whether the length of text is greater that our
			// L_MAX_COLS value.  If so, then we need another
			// row for each time that the length is greater than
			// the L_MAX_COLS value.
		
			//Splitta la riga su ogni spazio
			var arrSpazi = arrNewLines[i].split(" ");
			var nonUtile = 0;
			var numRowNonUtile = 0;
			for(var j=0; j<arrSpazi.length; j++)
			{
				if(arrSpazi[j].length >= L_MAX_COLS) 
				{
				  nonUtile = nonUtile + arrSpazi[j].length;
				  numRowNonUtile++;
				}
			}
			incremento = Math.ceil((arrNewLines[i].length - nonUtile)/L_MAX_COLS);
			if (numRowNonUtile != 0)
				numRowNonUtile++;
			numRows = numRows + incremento + numRowNonUtile;
		}
     }   
    
    if(txtLength == 0) {
	    // If there is no text in the TEXTAREA we default
	    // to our minimum values.
	    
       	e.cols = L_MIN_COLS ;
        e.rows = MIN_ROWS ;
    }
    else {
	    // If there is only 1 row, then all we need to
	    // determine is how many COLS we need.  It will be
	    // somewhere between our L_MIN_COLS & L_MAX_COLS 
	    // values.
        if(numRows <= 1) {
            e.cols = (txtLength % L_MAX_COLS) + 1 >= L_MIN_COLS 
                ? ((txtLength % L_MAX_COLS) + 1) 
                : L_MIN_COLS ;
		e.style.height = "20px";
            //alert("1 righa");
        }else{
	        // If there is more than 1 row then we immediately
	        // default to our L_MAX_COLS value, and then determine
	        // how many ROWS we need.
            e.cols = L_MAX_COLS ;    
            e.rows = (numRows > MAX_ROWS) ? MAX_ROWS : numRows ;		
						var num = eval(e.rows)*20; 	
            e.style.height = num+"px";
        }
    }
	} catch(e) {
		alert(e);
	}
	//e.value = e.value + arrNewLines.length+' ';
}
