// JavaScript Document
/*
LINK srl: 31/10/2003 - util.js - procedure e funzioni di utilità 
elenco: formatta, formatta1, trimLeadingBlanksCrLf, blanksToblank, LtrimBlanks, clearBlank, allStrOK, 
replaceCar, rimuoviblankECrLf, valida_numero, valida_numero_gen, openwin,
MM_swapImgRestore, MM_preloadImages, MM_findObj, MM_swapImage, ShowProgress,
mostraOnOff, as_gg, disabilita, sel_tutti, mod_tutti, verifica_selezione,
verifica_cerca, verifica_cerca_ejournal, verifica_salva, verifica_elenco, leggiform, 
mostra_img, pulisci_ejournal, verifica_select, verifica_visto, verifica_salva_ejournal
*/
    

/* funzione che formatta un campo di testo togliendo gli spazi bianchi e CrLf all'inizio e alla fine
e sostituendo più spazi bianchi con un solo spazio bianco */
   function formatta (f,flag) {
     trimLeadingBlanksCrLf(f);
     blanksToblank(f);
	 if (flag && (f.value.length > 0)) f.value=f.value.charAt(0).toUpperCase()+f.value.slice(1)
   }
   
/* funzione che formatta un campo di testo togliendo gli spazi bianchi e CrLf all'inizio e alla fine
e sostituendo più spazi bianchi o CrLf interni con un solo spazio bianco */
   function formatta1 (f,flag,flag1) {
     trimLeadingBlanksCrLf(f);
	 rimuoviblankECrLf(f,flag1);
	 if (flag && (f.value.length > 0)) f.value=f.value.charAt(0).toUpperCase()+f.value.slice(1)
   }

//funzione che toglie gli spazi bianchi e gli CrLf all'inizio e alla fine di un campo di testo
  function trimLeadingBlanksCrLf(f) {
//toglie i blank e i CrLf all'inizio
    if (f.value.length > 0) {
	   for (i=0; i < f.value.length; i++) {
		   carat=f.value.charAt(i);
		   if ((carat != ' ') && (carat != '\r') && (carat != '\n')&& (carat != '\t')) {
			   f.value=f.value.slice(i);
		       break
		   }
	   }
	   if (i==f.value.length) f.value='';
	}
//toglie i blank e CrLf alla fine
    if (f.value.length > 0) {
	   for (i=f.value.length-1; i > -1; i--) {
		   carat=f.value.charAt(i);
		   if ((carat != ' ') && (carat != '\r') && (carat != '\n') && (carat != '\t')) {
			   f.value=f.value.slice(0,i+1);
		       break
		   }
	   }
	}
  }
  

//funzione che sostituisci più spazi bianchi con uno spazio bianco
  function blanksToblank(f){
    if (f.value.length > 0) {
       firstBlank = f.value.indexOf(' ');
       lastBlank = f.value.lastIndexOf(' ');
       if (firstBlank < lastBlank) {
          newstr = '';
     	  tmpstr = f.value;
          iBlank = firstBlank;
     	  while (iBlank > 0) { 
     	        newstr = newstr + tmpstr.substring(0,iBlank);
     	        tmpstr = LtrimBlanks(tmpstr.substring(iBlank,tmpstr.length));
     	        newstr = newstr + ' ';
     	        iBlank = tmpstr.indexOf(' ');
     	  }
          newstr = newstr + tmpstr;
          f.value = newstr
       }
    }
  }

//funzione che toglie gli spazi bianchi all'inizio di una stringa
  function LtrimBlanks(str) {
//toglie i blank
    if (str.length > 0) {
       carat=str.charAt(0);	
       while (carat == ' ' ) {
             str = str.substring(1,str.length);	
             carat = str.charAt(0)
       }
    }
    return str
  }
  
//funzione che togli tutti gli spazi bianchi
  function clearBlank(f) {
    if (f.value.length > 0) {	
       firstBlank = f.value.indexOf(' ');
       lastBlank = f.value.lastIndexOf(' ');
       if (firstBlank < lastBlank) {
          newstr = '';
     	  tmpstr = f.value;
          iBlank = firstBlank;
     	  while (iBlank > 0) { 
     	        newstr = newstr + tmpstr.substring(0,iBlank);
     	        tmpstr = LtrimBlanks(tmpstr.substring(iBlank,tmpstr.length));
     	        iBlank = tmpstr.indexOf(' ')
     	  }
          newstr = newstr + tmpstr;
          f.value = newstr
       }
    }
  }
  
// funzione che verifica che il campo di testo sia composto caratteri compresi nella stringa assegnata
  function allStrOK(val,strOK) {
     for (i = 0;  i < val.length; i++) {
	     if (strOK.indexOf(val.charAt(i)) == -1) return false
     }
     return true
  } 

// funzione che sostituisce nella stringa "str" la sequenza di caratteri "oldCar" con la nuova srquenza "newCar"
  function replaceCar(str,oldCar,newCar)
  {
     var iCar, firstCar, lastCar, tmpnewstr, tmpstr, carat;
     firstCar = str.indexOf(oldCar);
     lastCar = str.lastIndexOf(oldCar);
     if (firstCar <= lastCar) {
        tmpnewstr = '';
        tmpstr = str;
        iCar = firstCar;
        while (iCar >= 0) { 
     	  tmpnewstr = tmpnewstr + tmpstr.substring(0,iCar) + newCar;
     	  if (tmpstr.charAt(iCar+1) == '\n') {
     	      tmpstr = tmpstr.substring(iCar+2,tmpstr.length)
     	  }
     	  else {
     	      tmpstr = tmpstr.substring(iCar+1,tmpstr.length)
          }
     	  iCar = tmpstr.indexOf(oldCar);
        }
        return tmpnewstr + tmpstr
     }
     return str
  }

//funzione che sostituisce gli spazi bianchi e CrLf con uno spazio bianco
  function rimuoviblankECrLf(f,flag)
  {
    if (f.value.length > 0) {
       f.value=replaceCar(f.value,'\r',' ');
       f.value=replaceCar(f.value,'\n',' ');
	   f.value=replaceCar(f.value,'\t',' ');
	   if (flag) f.value=replaceCar(f.value,' ','')
	   else blanksToblank(f)
    }
  }
  
// funzione che verifica che il campo di testo "f" per la form numero "nf" sia composto da determinati valori e sia compreso nell'intervallo e1 <= f <= e2
  function valida_numero (f,e1,e2) {
    formatta(f,0)
    if (f.value.length==0) {
        document.asPage.reset()
        return false
    } 
    if (! isNaN(f.value)) {
        f.value = parseInt(f.value);
        if (f.value < 1) {
            alert('ATTENZIONE!\r\nIl valore digitato non pu'+unescape('%F2')+' essere minore di '+1+'.');
            document.asPage.reset();
            return false
        } 
        if (f.value > e2) {
            alert('ATTENZIONE!\r\nIl valore digitato non pu'+unescape('%F2')+' essere maggiore di '+e2+'.');
            document.asPage.reset();
            return false
        } 
    }
    else {
        alert ('ATTENZIONE!\r\nDigitare solo cifre.')
        document.asPage.reset();
        return false
    }
    document.asPage.submit();
    return true
  }
  
  // funzione che verifica che il campo di testo "f" per la form numero "nf" sia composto da determinati valori e sia compreso nell'intervallo e1 <= f <= e2
  function valida_numero_gen (f,e1,e2) {
    formatta(f,0)
    if (f.value.length > 0) {
        if (! isNaN(f.value)) {
           f.value = parseInt(f.value);
           if (f.value < e1) {
               alert('ATTENZIONE!\r\nIl valore digitato non pu'+unescape('%F2')+' essere minore di '+e1+'.');
               f.focus();
               return false
           } 
           if (f.value > e2) {
              alert('ATTENZIONE!\r\nIl valore digitato non pu'+unescape('%F2')+' essere maggiore di '+e2+'.');
              f.focus();
              return false
           } 
        }
        else {
           alert ('ATTENZIONE!\r\nDigitare solo cifre.')
           f.focus();
           return false
        }
	}
    return true
  }
  
// funzione di apertura di una nuova finestra
  function openwin (theurl,scroll,larga,alta) {
    var features = 'toolbar=no,location=no,resizable=no,scrollbars='+ scroll + ',status=0,width='+ larga + ',height=' + alta + ',innerWidth=' + larga +',innerHeight=' + alta + ',top=20,left=20,screenX=20,screenY=20'; 
    finestra=window.open(theurl,'finestra',features);
    finestra.focus()
  }

// inizio funzioni create con Dreamwearer utilizzate per il rollover delle immagini
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
// fine funzioni create con Dreamwearer

// funzione creata da ASPUPLOAD per mostrare la barra di caricamento di un file
function ShowProgress(f,pagina)
{
  strAppVersion = navigator.appVersion;
  if (f.linklocal.value != "")
  {
    if (strAppVersion.indexOf('MSIE') != -1 && strAppVersion.substr(strAppVersion.indexOf('MSIE')+5,1) > 4)
    {
      winstyle = "dialogWidth=460px; dialogHeight:140px; center:yes;";
      window.showModelessDialog(pagina+'&b=IE',null,winstyle);
    }
    else
    {
      window.open(pagina+'b=NN','','width=420,height=130', true);
    }
  }
  return true;
}


// funzione che mostra o nasconde una sezione di pagina utilizzando STYLE.DISPLAY
  function mostraOnOff (v_ele) {
	if (v_ele.length == 1) {
	   if (document.getElementById(v_ele[0]).style.display=='block') document.getElementById(v_ele[0]).style.display='none'
	   else document.getElementById(v_ele[0]).style.display='block'
	}
	else {
	  if (document.getElementById(v_ele[0]).style.display=='block') {
		 for (i=0; i < v_ele.length; i++) document.getElementById(v_ele[i]).style.display='none'
	  }
      else {
		    document.getElementById(v_ele[0]).style.display='block';
	        document.getElementById(v_ele[1]).style.display='block';
	        for (i=2; i < v_ele.length; i++) document.getElementById(v_ele[i]).style.display='none'
	  }
	}
  }
  
/* funzione che riporta, esaminando campi di tipo radio, il valore 0 se l'elemento è selezionato e appartiene a un gruppo di un solo elemento, 
la posizione dell'elemento nell'ambito del gruppo di elementi identificati con lo stesso nome se questo è selezionato, -1 se nessun elemento è selezionato
*/
  function as_gg(f) {
     if (isNaN(f.length)) {
		 if (f.checked) {
	        return 0
		 }
     }
	 else {
	    for (j=0; j<f.length; j++) {
		   if (f[j].checked) {
	          return j
		   }
		}
	 }
	 return -1
  }
 
// funzione che rimuove la selezione di un elemento in un campo radio
  function disabilita(f,i) {
	 if (isNaN(f.length)) {
		if ((f.checked) && (i==radio_index)) {
	        f.checked=!f.checked;
		}
	 }
	 else {
	   for (j=0; j<f.length; j++) {
		   if ((f[j].checked) && (i==radio_index)) {
			  f[j].checked=!f[j].checked;
			  break
		   }
	   }
	 }
  }  
  
function sel_tutti(f,f1) {
   var i;
   if (f.length>0) {
       for (i=0;i<f.length;i++) f[i].checked=f1.checked
   }
   else {
	  f.checked=f1.checked
   }
}

function mod_tutti(f,f1) {
	var i;
	if (f.length>0) {
	   for (i=0;i<f.length;i++) {
		 if (!f[i].checked) {
			 f1.checked=false;
			 break
		 }
	   }
	   if (i==f.length) f1.checked=true
	 }
	 else {
	   f1.checked=f.checked
	 }
}

function verifica_cerca (f) {
	if (whilef.value==f.elements[0].value) {
		f.elements[11].value=1;
		return true
	}
	formatta(f.elements[2],false)
	if ((f.elements[2].value.length == 0) && (f.elements[4].options[f.elements[4].selectedIndex].value.length == 0) && (f.elements[5].options[f.elements[5].selectedIndex].value == -1)) {
	   alert ('ATTENZIONE!\r\nBisogna digitare dei criteri di ricerca');
	   return false
    }
	f.elements[11].value=2;
	return true
}

function verifica_cerca_ejournal (f,r) {
	
	if (whilef.value==f.elements[0].value) {
		f.elements[11].value=1;
		return true
	}
	formatta(f.elements[2],false)
	if (r!='ED') {
	   if ((f.elements[2].value.length == 0) && (f.elements[4].options[f.elements[4].selectedIndex].value.length == 0) && (f.elements[5].options[f.elements[5].selectedIndex].value == -1)) {
	      alert ('ATTENZIONE!\r\nBisogna digitare dei criteri di ricerca');
	      return false
       }
	}
    else {
	   if ((f.elements[2].value.length == 0) && (f.elements[5].options[f.elements[5].selectedIndex].value == -1)) {
	      alert ('ATTENZIONE!\r\nBisogna digitare dei criteri di ricerca');
	      return false
       }
    }
	f.elements[11].value=2;
	return true
}

function verifica_salva (f) {
	if (verifica_salva.arguments.length > 1) {
		nmax=(verifica_salva.arguments[1]+1)*5;
	    for (i=0; i<nmax; i=i+5) {
		   if (f.elements[i+1].value.length == 0) {
			  alert ('ATTENZIONE!\r\n\"La denominazione\" ' + unescape('%E8') + ' obbligatoria');
			  f.elements[i+1].focus();
			  return false
		   }
	    }
		f.elements[nmax+3].value=4
	}
	else {
	    for (i=0; i<20; i=i+4) {
		   if ((f.elements[i+1].value.length == 0) && ((f.elements[i].value.length > 0) || (f.elements[i+2].value.length > 0))) {
			  alert ('ATTENZIONE!\r\n\"La denominazione\" ' + unescape('%E8') + ' obbligatoria');
			  f.elements[i+1].focus();
			  return false
		   }
	    }
	}
	return true
}

function verifica_elenco(f,v) {
	//submit modifica
	if (whilef.value=='modifica') {
	  if (f.tutti0.disabled) return false
	  numero=0
      if (isNaN(f.selmod.length)) {
	     if (f.selmod.checked) numero++
	  }
	  else { 
	     for (i=0; i<f.selmod.length;i++) {
	         if (f.selmod[i].checked) numero++
	     }
	  }
      if (numero==0)  {
	     alert('ATTENZIONE!\r\nBisogna selezionare almeno un elemento');
         return false
	  }
	  f.livello.value=3
	}
	//submit conferma stato
	if (whilef.value=='conferma stato') {
		c=-1;
		for (i=0; i < f.length; i++) {
			nome=f.elements[i].name;
			if (nome.indexOf('selst') > -1) {
				c++;
				e=f.elements[i].options[f.elements[i].selectedIndex].value.split('-')
				if (e[1]!= v[c]) {
					f.livello.value=2;
					return true
				}
			}
			if (c==v.length-1) return false
		}
		f.livello.value=2
	}	
	return true
}

  //funzione che assegna da sistema i dati in una form e la invia al server
function leggiform(f,v) {
	f.asNp.value=v;
	f.submit();
	return true
}

// funzione esegue un cambiamento di img
function mostra_img (n_ele,vettore) {
   n_img = document.getElementById(n_ele).src;
   if(n_img.indexOf('frecciagiu') > -1) {
	  document.getElementById(n_ele).setAttribute('src','images/frecciadxoff.gif');
	   vettore[parseInt(n_ele.slice(1))]='frecciadx'
   }
   else {
	   document.getElementById(n_ele).setAttribute('src','images/frecciagiuoff.gif');
	   vettore[parseInt(n_ele.slice(1))]='frecciagiu'
   }
  }
  
function pulisci_ejournal(v1,v2,v3,r) {
	if (r != 'ED') document.getElementById('editor').innerHTML=v1;
	document.getElementById('ateco').innerHTML=v2;
	document.getElementById('cis').innerHTML=v3;
    document.getElementById('tdpub').style.display='none';
	document.getElementById('tdpub1').style.display='none';
}

function verifica_select(f,e) {
	v=new Array();
	k=0;
	for (i=0; i<f.options.length; i++) {
		if (f.options[i].selected) {
			v[k]=i;
			k++
		}
	}
	if (v.length > 1) {
		for (i=0; i<v.length; i++) {
			for (j=0; j<e.length; j++) {
			   if (f.options[v[i]].value==e[j][0]) {
				   alert('ATTENZIONE!\r\nNon ' + unescape('%E8') + ' possibile selezionare ' +  e[j][1] + ' insieme con altri valori');
				   f.options[v[i]].selected=false;
				   return false
			   }
			}
		}
	}
	return true
}

function verifica_visto(f,imax) {
   if (f.type == 'hidden') {
	  if ((f.value==1) || (f.value==2)) colore='#000000'
	  else colore='#E6E5FC'
   }
   else {
     if ((f.options[f.selectedIndex].value==1) || (f.options[f.selectedIndex].value==2)) colore='#000000'
     else colore='#E6E5FC'
   }
   for(i=0; i<=imax; i++) document.getElementById('mostra'+ i).style.color=colore
}

function verifica_salva_ejournal(f,r, r1) {
   var visto=0;
   if (f.titolo.value.length == 0) {
	   alert('ATTENZIONE\r\nIl titolo ' + unescape('%E8') + ' obbligatorio');
	   f.titolo.focus();
	   return false
   }
   if (r != r1) {
	   if (f.veditor.value.length == 0) {
		  alert('ATTENZIONE\r\nL\'editor ' + unescape('%E8') + ' obbligatorio');
		  f.sel_ed.focus();
		  return false
       }
	   visto=f.visto.options[f.visto.selectedIndex].value;
   }
   if ((visto == 1) || (visto == 2) || (r == r1)) {	
       if ((visto == 1) || (visto == 2)) {
	      if (f.veditor_v.value==0) {
		     alert('ATTENZIONE\r\nNon ' + unescape('%E8') + ' possibile convalidare il titolo corrente perch' + unescape('%E9') +' l\'editor selezionato non ' + unescape('%E8') +' stato ancora convalidato');
		     f.sel_ed.focus();
		     return false
	      }
	   }
	   if (f.edpub[1].checked) {
		  if (f.publisher.value.length == 0) {
		     alert('ATTENZIONE\r\nIl publisher ' + unescape('%E8') + ' obbligatorio');
		     f.publisher.focus();
		     return false
          }
	   }
	   if (f.url.value.length == 0) {
		  alert('ATTENZIONE\r\nL\'URL ' + unescape('%E8') + ' obbligatorio');
		  f.url.focus();
		  return false
	   }
	   if (f.lingua.selectedIndex == -1) {
	      alert('ATTENZIONE\r\nLa lingua ' + unescape('%E8') + ' obbligatoria');
		  f.lingua.focus();
		  return false
	   }
	   if (f.formato.selectedIndex == -1) {
	      alert('ATTENZIONE\r\nIl formato ' + unescape('%E8') + ' obbligatorio');
		  f.formato.focus();
		  return false
	   }
	   if (f.periodo.selectedIndex <= 0) {
	      alert('ATTENZIONE\r\nBisogna specificare la periodicit' + unescape('%E0'));
		  f.periodo.focus();
		  return false
	   }
	   if (f.accesso.selectedIndex <= 0) {
	      alert('ATTENZIONE\r\nBisogna specificare la modalit' + unescape('%E0') + ' di accesso');
		  f.accesso.focus();
		  return false
	   }

	   if (f.descrizione.value.length == 0) {
		  alert('ATTENZIONE\r\nLa descrizione ' + unescape('%E8') + ' obbligatoria');
		  f.descrizione.focus();
		  return false
       }
	   if (f.vateco.value.length == 0) {
		  alert('ATTENZIONE\r\nI codici ATECO/NACE sono obbligatori');
		  return false
       }
	   if (f.vcis.value.length == 0) {
		  alert('ATTENZIONE\r\nI descrittori CIS sono obbligatori');
		  return false
       }
	}
	return true
}

function bordocontent(par1,par2) {
   if (navigator.userAgent.indexOf('Netscape') == -1) {
	   var altezza=document.getElementById(par1[0]).clientHeight;
	   for (var i=1; i<par1.length; i++) altezza = Math.max(altezza,document.getElementById(par1[i]).clientHeight)
	   document.getElementById(par2).style.height=altezza
   }
}

function colorAOn(v) {
	document.getElementById('livA'+v).style.color='#ff0000';
}

function colorAOff(v) {
	document.getElementById('livA'+v).style.color='';
}

function colorBOn(v) {
	document.getElementById('livB'+v).style.color='#ff0000';
}
function colorBOff(v) {
	document.getElementById('livB'+v).style.color='';
}
function colorAAOn(v) {
	document.getElementById('livAA'+v).style.color='#ff0000';
}

function colorAAOff(v) {
	document.getElementById('livAA'+v).style.color='';
}

function colorBBOn(v) {
	document.getElementById('livBB'+v).style.color='#ff0000';
}
function colorBBOff(v) {
	document.getElementById('livBB'+v).style.color='';
}

var radio_index=-1;
var whilef='';
var img1=new Array();
var finestra;

