//******** da errore se il carattere digitato non è compreso: a-z, A-Z, 1-9, .,   	
function formatChar(obj){
	var carattere=window.event.keyCode
			if (!((carattere >= 8 && carattere <= 8) || (carattere >= 10 && carattere <= 10) || (carattere >= 13 && carattere <= 13) || (carattere >= 32 && carattere <= 32) || (carattere >= 46 && carattere <= 57) || (carattere >= 65 && carattere <= 90) || (carattere >= 97 && carattere <= 122))){
				window.event.returnValue=false;
				alert("Carattere digitato non consentito in questo campo");
			}
		CharInvio();
}

//******** da errore se il carattere digitato non è un numero
function formatCharNumber(numero){
	var numero=window.event.keyCode		
			if (!((numero >= 48 && numero <= 57))) {
				window.event.returnValue=false;
				alert("Inserire solo un valore numerico");
			}			
		CharInvio();
}

//******** da errore se il carattere digitato non è un numero
function formatCharData(data){
	var data=window.event.keyCode		
			if (!(data >= 47 && data <= 57)) {
			window.event.returnValue=false;
				alert("Inserire una data in questo formato: gg/mm/aaaa");
			}			
		CharInvio();
}


//******** fa partire l'invio del form e del relativo cnotrollo alla pressione del tasto invio
function CharInvio(invio){
	var Pulsante=window.event.keyCode	   
			if ((Pulsante >= 10 && Pulsante <= 10) ||	
			(Pulsante >= 10 && Pulsante <= 13)) {
				Controllo();				
			}
}

	
function Controllo() {	
var ricerca = document.ricercaLeggi.ricerca_lib.value;
var noricerca = document.ricercaLeggi.noricerca_lib.value;
// Variabili associate ai campi del modulo
     var anno = document.ricercaLeggi.anno.value;     
     var today = new Date();
     var yyyy = today.getYear();
     var errore = 0     
     
//ricerca=Trim(ricerca);
//noricerca=Trim(noricerca);
//ricerca=trimma(ricerca);
//noricerca=trimma(noricerca);

document.ricercaLeggi.ricerca_libNas.value="";
document.ricercaLeggi.NOricerca_libNas.value="";
	if ((ricerca != "") && (ricerca != "undefined")) {				
			
			if (document.ricercaLeggi.mod_ricerca[0].checked){
        		var mod_ricerca = document.ricercaLeggi.mod_ricerca[0].value;
			}
			else if (document.ricercaLeggi.mod_ricerca[1].checked){
       		 	var mod_ricerca = document.ricercaLeggi.mod_ricerca[1].value;
    		}
    		else{
				var mod_ricerca = document.ricercaLeggi.mod_ricerca[2].value;
    		}				
	    
			var er0=/ +/gi;
			var result0=ricerca.replace(er0," ");
			document.ricercaLeggi.ricerca_lib.value=result0;
           			
  			//se ha scelto tipo2 aggiunge + ad ogni parola
  			if ((mod_ricerca == "1")) {
  					var er=/ +/gi;
  					var result=ricerca.replace(er," ");           
           			document.ricercaLeggi.ricerca_libNas.value=result; 	        			        
        	}
        	
  			if ((mod_ricerca == "2")) {
  					var er1=/ +/gi;
  					var result= "+" + ricerca.replace(er1," +");           
           			document.ricercaLeggi.ricerca_libNas.value=result; 	        			        
        	}
        	
            //se ha scelto tipo3 racchiude il testo tra virgolette
			else if ((mod_ricerca == "3")) {
           			result="\"" + ricerca + "\"";
           			document.ricercaLeggi.ricerca_libNas.value=result;		
          	}
	}
	if ((noricerca != "") && (noricerca != "undefined")) {
		var er2=/ +/gi;
		var result2= "-" + noricerca.replace(er2," -");
		document.ricercaLeggi.NOricerca_libNas.value=result2;
	}

if (((ricerca == "") || (ricerca == "undefined")) && !((noricerca == "") || (noricerca == "undefined")))  {
           alert("Per effettuare la ricerca specificando delle parole non presenti, è necessario prima specificare quelle da ricercare.");
           document.ricercaLeggi.ricerca_lib.focus();
return;
}

var annoChar=document.ricercaLeggi.anno.value;
if (annoChar=="" || annoChar=="undefined") {
   annoChar="aaaa";   
}


if (annoChar.length<4 || annoChar.length>4 || annoChar=="" || annoChar=="undefined") {
   alert("Inserire una data di quattro cifre.");
   document.ricercaLeggi.anno.value="";
   document.ricercaLeggi.anno.focus();
   return;
}

       		
   var gg=document.ricercaLeggi.data1.value;
   if (gg=="gg") {
   gg="";
   }
   var mm=document.ricercaLeggi.data2.value;
   if (mm=="mm") {
   mm="";
   }
   var aa=document.ricercaLeggi.data3.value;
   if (aa=="aaaa") {
   gg="";
   }   
   var strdata=gg+"/"+mm+"/"+aa;
   var strdata2=gg+mm+aa;
   
   
   var data = new Date(aa,mm-1,gg);
   var daa=data.getFullYear().toString();
   var dmm=(data.getMonth()+1).toString();
      dmm=dmm.length==1?"0"+dmm:dmm
   var dgg=data.getDate().toString();
      dgg=dgg.length==1?"0"+dgg:dgg
   var dddata=dgg+"/"+dmm+"/"+daa
   if (dddata!=strdata && strdata!="gg/mm/aaaa" && strdata2!="") {
      alert("Verificare la data inserita");
      document.ricercaLeggi.data1.value=gg;
      document.ricercaLeggi.data2.value=mm;
      document.ricercaLeggi.data3.value=aa;
      document.ricercaLeggi.data1.focus();
      return;
      }


// Variabili associate ai campi del modulo ricercaLeggi
//imposto l'action
document.ricercaLeggi.action = "risultati.asp";
                
//lancio il submit
document.ricercaLeggi.submit();
}

function Trim(StrToTrim)
{
// CONTROLLA CHE IL VALORE IN INPUT SIA DI TIPO STRING
if (typeof StrToTrim != "string")
{
return StrToTrim;
}

// CATTURA IL PRIMO CARATTERE DELLA STRINGA
// PER CONTROLLARE CHE NON SIA UNO SPAZIO VUOTO
var StrBlank = StrToTrim.substring(0, 1);

// ELIMINA LO SPAZIO VUOTO DALLA PRIMA POSIZIONE DELLA STRINGA
while (StrBlank == " ")
{
StrToTrim = StrToTrim.substring(1, StrToTrim.length);
StrBlank = StrToTrim.substring(0, 1);
}

// CATTURA L'ULTIMO CARATTERE DELLA STRINGA
// PER CONTROLLARE CHE NON SIA UNO SPAZIO VUOTO
StrBlank = StrToTrim.substring(StrToTrim.length - 1, StrToTrim.length);

// ELIMINA LO SPAZIO VUOTO DALL'ULTIMA POSIZIONE DELLA STRINGA
while (StrBlank == " ")
{
StrToTrim = StrToTrim.substring(0, StrToTrim.length-1);
StrBlank = StrToTrim.substring(StrToTrim.length-1, StrToTrim.length);
}

// ELIMINA POTENZIALI SPAZI VUOTI MULTIPLI
// ALL'INIZIO ED ALLA FINE DI UNA STRINGA
while (StrToTrim.indexOf("") != -1)
{
StrToTrim = StrToTrim.substring(0, StrToTrim.indexOf(""));
StrToTrim += StrToTrim.substring(StrToTrim.indexOf("") + 1, StrToTrim.length);
}

// RESTITUISCE IL VALORE FINALE SENZA SPAZI VUOTI DI CONTORNO
return StrToTrim;
}

