// Funzione che disabilita il pulsante passato come parametro per evitare che l'utente clicchi ripetutamente
function fDisabilitaPulsante(NumeroForm, NumeroCampo, Messaggio) {
	document.forms[NumeroForm].elements[NumeroCampo].disabled=true
	document.forms[NumeroForm].elements[NumeroCampo].value=Messaggio
}

// Elimina gli spazi a dx e sx nelle variabili
function Strip(string) {
   var s_str = string.length
   for (var i=0; i<string.length; i++) {
      if (" " != string.substring(i,i+1)) {
         s_str = i;
         break;
      }
   }
   if (s_str == string.length) {
      return "";
   }
   for (i=string.length-1; i>=0; i--) {
      if (" " != string.substring(i,i+1)) {
         var s_end = i;
         break;
      }
   }
   return string.substring(s_str,s_end+1);
}

// Funzione che rimanda all' URL specificato
function go(sel) {
	s_valore=sel.value
	//if (s_valore != "" && (s_valore.substring(0,4) == "form" || s_valore.substring(0,6) == "elenco")) { }
	document.location.href=sel[sel.selectedIndex].value
}

// Funzione che apre una nuova finestra personalizzata
function popup(nomefile, larghezza, altezza, x, y, scrolling) {
   win_popup = window.open(nomefile,"popup","toolbar=1,location=0,directories=0,status=0,menubar=0,scrollbars=" + scrolling + ",resizable=0,copyhistory=0,width=" + larghezza + ",height=" + altezza); 
   if(x && y); {
     x = parseInt(x);
     y = parseInt(y);
     win_popup.moveTo(x, y);
  }
}


// Funzione che effettua tutti i controlli inerenti un campo FLOAT
function controlli_float(campo, s_msg) {

   var controllo="-,.0123456789"
   var verifica=0
   var punti_virgole=0
   for (var h=0 ; h<campo.length; h++) {
       variante = campo.substring(h,h+1) 
       for (var u=0 ; u<controllo.length; u++) {
           if (variante != controllo.substring(u,u+1))
              { verifica=1 }
           else
              { 
              if (variante == "." || variante == ",") { punti_virgole=punti_virgole+1; }
			  verifica=0; break
			  }
       }
       if (verifica == 1 || punti_virgole>1) { 
	   alert("Controllare il contenuto del campo " + s_msg)
	   return 1
	    }
   }
return 0
}

// Funzione che effettua tutti i controlli inerenti un campo NUMERICO
function controlli_numerico(campo, s_msg) {

   var controllo="-0123456789"
   var verifica=0
   var punti_virgole=0
   for (var h=0 ; h<campo.length; h++) {
       variante = campo.substring(h,h+1) 
       for (var u=0 ; u<controllo.length; u++) {
           if (variante != controllo.substring(u,u+1))
              { verifica=1 }
           else
              { 
              if (variante == "." || variante == ",") { punti_virgole=punti_virgole+1; }
			  verifica=0; break
			  }
       }
       if (verifica == 1 || punti_virgole>1) { 
	   alert("Controllare il contenuto del campo " + s_msg)
	   return 1
	    }
   }
return 0
}

// Funzione che controlla la validità della data singola
function controlladatasingola(d_data, msg){

// Array che contiene gli errori
err = new Array(18)
err[1]  = "Errore nella " + msg + ": Gennaio ha al massimo 31 giorni  ";
err[2]  = "Errore nella " + msg + ": Febbraio ha al massimo 29 giorni (Anno bisestile)";
err[3]  = "Errore nella " + msg + ": Febbraio ha al massimo 28 giorni (Anno non bisestile)";
err[4]  = "Errore nella " + msg + ": Marzo ha al massimo 31 giorni    ";
err[5]  = "Errore nella " + msg + ": Aprile ha al massimo 30 giorni   ";
err[6]  = "Errore nella " + msg + ": Maggio ha al massimo 31 giorni   ";
err[7]  = "Errore nella " + msg + ": Giugno ha al massimo 30 giorni   ";
err[8]  = "Errore nella " + msg + ": Luglio ha al massimo 31 giorni   ";
err[9]  = "Errore nella " + msg + ": Agosto ha al massimo 31 giorni   ";
err[10] = "Errore nella " + msg + ": Settembre ha al massimo 30 giorni";
err[11] = "Errore nella " + msg + ": Ottobre ha al massimo 31 giorni  ";
err[12] = "Errore nella " + msg + ": Novembre ha al massimo 30 giorni ";
err[13] = "Errore nella " + msg + ": Dicembre ha al massimo 31 giorni ";
err[14] = "Errore nella " + msg + ": Mese errato                      ";
err[15] = "Errore nella " + msg + ": Giorno errato                    ";
err[16] = "Errore nella " + msg + ": Anno errato                      ";
err[17] = msg + " errata: formato richiesto gg-mm-aaaa                ";
err[18] = "Inserire la " + msg;

// Inizializza le variabili
data               = d_data
giorno             = data.substring(0,2)
mese               = data.substring(3,5)
anno               = data.substring(6,10)
ultimeduecifreanno = data.substring(8,10)

// Non deve essere vuota
if (data.length == 0 ) { alert(err[18]); return 1 }

// Deve contenere 2 lineette (-), come per la data nel formato italiano
contatore = 0
for (i=0; i<data.length; i++) {
   if (data.charAt(i) == "-") { contatore++ }
}
if ( contatore != 2 ) { alert(err[17]); return 1 }

// Deve essere lunga 10 caratteri (gg-mm-aaaa)
if (data.length != 10) { alert(err[17]); return 1 }

// Controllo se il giorno è valido
if ( giorno == "00" ) { alert(err[15]); return 1 }

// Controllo se il mese è valido
if (parseInt(mese) > 12 || mese == "00") { alert(err[14]); return 1 }

// Controllo se l'anno è valido
if (parseInt(anno) < 1000)  { alert(err[16]); return 1 }

// Controlli sulla correttezza della data inserita
if ( mese == "01" && parseInt(giorno) > 31) { alert(err[1]); return 1  }
if ( mese == "02" && parseInt(giorno) > 29 && ultimeduecifreanno == "00" && parseInt(anno)%400 == 0 ) { alert(err[2]); return 1 }
if ( mese == "02" && parseInt(giorno) > 29 && ultimeduecifreanno != "00" && parseInt(anno)%4   == 0 ) { alert(err[2]); return 1 }
if ( mese == "02" && parseInt(giorno) > 28 && ultimeduecifreanno == "00" && parseInt(anno)%400 != 0 ) { alert(err[3]); return 1 }
if ( mese == "02" && parseInt(giorno) > 28 && ultimeduecifreanno != "00" && parseInt(anno)%4   != 0 ) { alert(err[3]); return 1 }
if ( mese == "03" && parseInt(giorno) > 31) { alert(err[4]); return 1  }
if ( mese == "04" && parseInt(giorno) > 30) { alert(err[5]); return 1  }
if ( mese == "05" && parseInt(giorno) > 31) { alert(err[6]); return 1  }
if ( mese == "06" && parseInt(giorno) > 30) { alert(err[7]); return 1  }
if ( mese == "07" && parseInt(giorno) > 31) { alert(err[8]); return 1  }
if ( mese == "08" && parseInt(giorno) > 31) { alert(err[9]); return 1  }
if ( mese == "09" && parseInt(giorno) > 30) { alert(err[10]); return 1 }
if ( mese == "10" && parseInt(giorno) > 31) { alert(err[11]); return 1 }
if ( mese == "11" && parseInt(giorno) > 30) { alert(err[12]); return 1 }
if ( mese == "12" && parseInt(giorno) > 31) { alert(err[13]); return 1 }

return 0;
}
