//var max_deb=3;
var salvataggio = null;
var voti1 = "";
var assenze1 = "";
var condotta1 = "";
var controlli = "";
var celle_condotta = "";
var celle_credito = "";
var stato_s1 = "";
var differenziata = "";
var formativi = "";
var merito = "";
var credito = "";
var old_credito="";
var correzione = "";
var maggioranza = "";
var manuale = "";
var medie = "";
var esito = "";
var esito_s = "";
var old_esito="";
var interessi = [];
var comportamenti = [];
var note = [];
var complementari = [];
var v1 = [];
var v="";
var sender = 0;
var dipartimento = "";
var dipartimento2 = "";
var dipartimento3 = "";
var ext = "";
var attivo = "";
var temp_a=[];
var db_v = "";
var db_m = "";
var db_s = "";
var db_n = "";
/*-----------------------------
var temp_a=new Array(35);
for (i=0;i<temp_a.length;i++)
  temp_a[i]=new Array(31);
-----------------------------  */

function DisattivaP() {
  var pulsante=document.getElementsByName("accedi");
  pulsante[0].disabled = true;
  }

function AccediP() {
  var t=setTimeout('DisattivaP()', 1);
  }

function AggiornaPulsante(nome) {
  var pulsante=document.getElementsByName(nome);
  pulsante[0].disabled = false;
  }

function NuovaAssenza(elemento) {
  var id=elemento.id;
  var valore=elemento.value;
  assenze1 +="#"+id+";"+valore;
  AggiornaPulsante('p_salva');
  }

function Cambio(controllo, page, elemento, ritorno) {
  var pulsante=document.getElementsByName(controllo);
  pulsante[0].disabled = true;
  window.location=page+"?selezione="+ritorno+"&"+controllo+"="+elemento.value;
  pulsante[0].disabled = false;
  }

function Giustifica(r, c, m) {
  var cf=confirm('Tutte le assenze INGIUSTIFICATE di '+m+' verranno, in automatico, segnate come GIUSTIFICATE! '+
    'Successivamente, sarà comunque necessario SALVARE la pagina per accettare le modifiche! Continuare?');
  if (cf == true)
    for (i=1; i<=r; i++) {
      var assenza=document.getElementsByName("a_mese"+i);
      for (g=0; g<c; g++) {
        if (assenza[g].value=='A') {
          assenza[g].value='a';
          assenza[g].style.color="black";
          var o=(i-1)*c+g+1;
          Piani(assenza[g], o);
          }
        }
      }
  }

function Col(ele, c) {
  if (ele.style.backgroundColor==c)
    ele.style.backgroundColor='white';
  else
    ele.style.backgroundColor=c;
  }

function CreaUser(i) {
  if (i==1) {
    var u=document.getElementsByName("alunno[alunno]");
    var un=document.getElementsByName("alunno[user]");
    var up=document.getElementsByName("alunno[pass]");
    var up2=document.getElementsByName("alunno[pass2]");
    }
  else if (i==2) {
    var u=document.getElementsByName("docente[docente]");
    var un=document.getElementsByName("docente[user]");
    var up=document.getElementsByName("docente[pass]");
    var up2=document.getElementsByName("docente[pass2]");
    }
  u=u[0].value;
  u=u.split(" ");
  u=u.join('');
  u=u+u.length+"-";
  utente="";
  for (i=1; i<=12; i++) {
    n=Math.round(Math.random()*(u.length-1));
    utente+=u.charAt(n);
    }
  utente=utente.toUpperCase();
  un[0].value=utente;
  up[0].value=utente;
  up2[0].value=utente;
  }

function CambioMese(elemento,page) {
  var pulsante=document.getElementsByName("id_mese");
  pulsante[0].disabled = true;
  var id_m=elemento.value;
  var id=elemento.id;
  argomento=id.split(";");
  window.location=page+"&ii="+argomento[0]+"&id_classe="+argomento[1]+"&cl="+argomento[2]+"&id_mese="+id_m;
  pulsante[0].disabled = false;
  }

function Tab(elemento, mese, page) {
  var pulsante=document.getElementsByName("tab");
  var id=elemento.id;
  var ttab="";
  argomento=id.split(";");
  pulsante[0].disabled = true;
  if (elemento.checked)
    ttab="&tab=1";
  window.location=page+ttab+"&id_mese="+mese+"&ii="+argomento[0]+"&id_classe="+argomento[1]+"&cl="+argomento[2];
  pulsante[0].disabled = false;
  }

function CambioAssegno(elemento,i,page) {
  var valore=elemento.value;
  if (i==1) {
    var id_dipart=document.getElementsByName("id_dipartimento");
    var id_codip=document.getElementsByName("id_codip");
    window.location=page+"&id_anno="+valore+"&id_dipartimento="
                    +id_dipart[0].value+"&id_codip="+id_codip[0].value;
    }
  else if (i==2) {
    var id_anno=document.getElementsByName("id_anno");
    var id_codip=document.getElementsByName("id_codip");
    window.location=page+"&id_dipartimento="+valore+"&id_anno="
                    +id_anno[0].value+"&id_codip="+id_codip[0].value;
    }
  else if (i==3) {
    var id_anno=document.getElementsByName("id_anno");
    var id_dipart=document.getElementsByName("id_dipartimento");
    window.location=page+"&id_codip="+valore+"&id_anno="+id_anno[0].value+
                    "&id_dipartimento="+id_dipart[0].value;
    }
  else if (i==4) {
    var id_anno=document.getElementsByName("id_anno");
    window.location=page+"&id_anno="+id_anno[0].value;
    }
  else if (i==5) {
    var id_anno=document.getElementsByName("id_anno");
    window.location=page+"&id_anno="+id_anno[0].value;
    }
  else if (i==6) {
    var id_anno=document.getElementsByName("id_anno");
    window.location=page+"&id_anno="+id_anno[0].value;
    }
  else if (i==7) {
    var id_indirizzo=document.getElementsByName("id_indirizzo1");
    window.location=page+"&id_anno="+valore+"&id_indirizzo="+id_indirizzo[0].value;
    }
  else if (i==8) {
    var id_anno=document.getElementsByName("id_anno");
    window.location=page+"&id_indirizzo="+valore+"&id_anno="+id_anno[0].value;
    }
  else if (i==9) {
    window.location=page+"&id_anno="+valore;
    }

  }

function ConvalidaAssenze(elemento) {
  if ((elemento.value =='A') || (elemento.value =='a') || (elemento.value =='R') || (elemento.value =='r') || (elemento.value =='')) {
    if ((elemento.value =='A') || (elemento.value =='R'))
      elemento.style.color="red";
    else if ((elemento.value =='a')  || (elemento.value =='r'))
      elemento.style.color="black";
    return 1;
    }
  else {
    elemento.value="";
    alert("Attenzione! \n Inserire solo uno dei seguenti valori:\n\n a = assenza giustificata\n\n"+
    " A = assenza non giustificata\n\n r = ritardo giustificato\n\n R = ritardo con AMMONIZIONE\n\n");
    return 0;
    }
  }

function Assegno(ele, i, no, k, n) {
  var ore=document.getElementsByName("ore");
  var materia=document.getElementsByName("materia");
  var id=ele.id;
  if (ele.checked) {
    ore[i-1].value=(ore[i-1].value)*1 + parseInt(no);
    materia[k].id=parseInt(materia[k].id) +1;
    if (materia[k].id==1)
      v1[n]="#"+id+";1;1";
    else if (materia[k].id>1)
      v1[n]="#"+id+";4;1";
    }
  else {
    ore[i-1].value=(ore[i-1].value)*1 - parseInt(no);
    materia[k].id=parseInt(materia[k].id) -1;
    v1[n]="#"+id+";1;0";
    }
  if (ore[i-1].value == 0)
    ore[i-1].style.color="#C0C0C0";
  else if ((ore[i-1].value >0) && (ore[i-1].value <18))
    ore[i-1].style.color="#0000FF";
  else if (ore[i-1].value == 18)
    ore[i-1].style.color="#008000";
  else
    ore[i-1].style.color="#FF0000";

  if (materia[k].id==0)
    materia[k].style.backgroundColor="#E1E1E1";
  else if (materia[k].id==1)
    materia[k].style.backgroundColor="#80FF00";
  else if (materia[k].id==2)
    materia[k].style.backgroundColor="#FFC000";
  else
    materia[k].style.backgroundColor="#FF0000";
  AggiornaPulsante('p_salva');
  }

function AssenzaMensile(elemento, ord) {
  if (ConvalidaAssenze(elemento)) {
    Piani(elemento, ord);
    }
  }

function VPiani(old, elemento, ord, i, k) {
  var id=elemento.id;
  var valore=elemento.value;
  var somma=document.getElementsByName("somma");
  var ore=document.getElementsByName("ore");
  if (valore>0) {
    elemento.title=valore;
    somma[i-1].value=1*(somma[i-1].value)+1*(valore);
    ore[k].value=1*(ore[k].value)+1*(valore);
    }
  else {
    if (isNaN(elemento.title))
      tol=old;
    else
      tol=elemento.title;
    somma[i-1].value=1*(somma[i-1].value)-1*(tol);
    ore[k].value=1*(ore[k].value)-1*(tol);
    elemento.title="";
    }
  v1[ord]="#"+id+";"+valore;
  elemento.style.backgroundColor="#FFFF00";
  controlli +="#"+id;
  AggiornaPulsante('p_salva');
  }

function Piani(elemento, ord) {
  var id=elemento.id;
  var valore=elemento.value;
  //var somma=document.getElementsByName("somma");
  //alert(somma[ord]);
  v1[ord]="#"+id+";"+valore;
  elemento.style.backgroundColor="#FFFF00";
  controlli +="#"+id;
  AggiornaPulsante('p_salva');
  }

function NCognitivi(elemento) {
  var id=elemento.id;
  var valore=elemento.value;
  var nome=elemento.name;
  var selezione=elemento.checked;
  if (nome=='interesse')
    interessi[id]="#"+id+";"+valore;
  else if (nome=='comportamento')
    comportamenti[id]="#"+id+";"+valore;
  else if (nome=='note')
    note[id]="#"+id+";"+valore;
  else if (nome=='complementare')
    complementari[id]="#"+id+";"+selezione;
  AggiornaPulsante('p_salva');
  }

function ComputoAssenze(i, ordine, nuovo, vecchio) {
  var tot_a=document.getElementsByName("tot_a");
  var tot_r=document.getElementsByName("tot_r");
  var tot_am=document.getElementsByName("tot_am");
  var tot_rm=document.getElementsByName("tot_rm");
  var nuovo=nuovo.value;
  if (temp_a[ordine]!=undefined)
    vecchio=temp_a[ordine];
  if (((nuovo=="A") || (nuovo=="a")) && ((vecchio=="R") || (vecchio=="r"))) {
    tot_a[i-1].value++;
    tot_am[i-1].value++;
    tot_r[i-1].value--;
    tot_rm[i-1].value--;
    }
  else if (((nuovo=="A") || (nuovo=="a")) && (vecchio=="")) {
    tot_a[i-1].value++;
    tot_am[i-1].value++;
    }
  else if (((nuovo=="R") || (nuovo=="r")) && ((vecchio=="A") || (vecchio=="a"))) {
    tot_a[i-1].value--;
    tot_am[i-1].value--;
    tot_r[i-1].value++;
    tot_rm[i-1].value++;
    }
  else if (((nuovo=="R") || (nuovo=="r")) && (vecchio=="")) {
    tot_r[i-1].value++;
    tot_rm[i-1].value++;
    }
  else if ((nuovo=="") && ((vecchio=="A") || (vecchio=="a"))) {
    tot_a[i-1].value--;
    tot_am[i-1].value--;
    }
  else if ((nuovo=="") && ((vecchio=="R") || (vecchio=="r"))) {
    tot_r[i-1].value--;
    tot_rm[i-1].value--;
    }
  temp_a[ordine]=nuovo;
  tot_a[i-1].value=(tot_a[i-1].value>0 ? tot_a[i-1].value : "");
  tot_am[i-1].value=(tot_am[i-1].value>0 ? tot_am[i-1].value : "");
  tot_r[i-1].value=(tot_r[i-1].value>0 ? tot_r[i-1].value : "");
  tot_rm[i-1].value=(tot_rm[i-1].value>0 ? tot_rm[i-1].value : "");
  }

//tabellone 2° quadrimestre -------------------------
function Differenziata(elemento, i, nclasse) {
  var stato=elemento.checked;
  var id=elemento.id;
  if (nclasse>2) {
    var credito=document.getElementsByName("credito");
    var merito=document.getElementsByName("merito");
    var formativi=document.getElementsByName("formativi");
    var old_c=document.getElementsByName("old_c");
    var correzione=document.getElementsByName("correzione");
    var tot_c=document.getElementsByName("tot_c");
    if (elemento.checked) {
      merito[i-1].disabled=true;
      formativi[i-1].disabled=true;
      old_c[i-1].disabled=true;
      correzione[i-1].disabled=true;
      tot_c[i-1].disabled=true;
      credito[i-1].disabled=true;
      }
    else if (!elemento.checked) {
      merito[i-1].disabled=false;
      formativi[i-1].disabled=false;
      old_c[i-1].disabled=false;
      correzione[i-1].disabled=false;
      tot_c[i-1].disabled=false;
      credito[i-1].disabled=false;
      }
    }
  if (stato)
    stato=1;
  else
    stato=0;
  differenziata += "#"+id+";"+stato;
  AggiornaPulsante('p_salva');
  }

function Formativi(elemento) {
  var stato=elemento.checked;
  var id=elemento.id;
  if (stato)
    stato=1;
  else
    stato=0;
  formativi += "#"+id+";"+stato;
  AggiornaPulsante('p_salva');
  }

function Merito(elemento) {
  var stato=elemento.checked;
  var id=elemento.id;
  if (stato)
    stato=1;
  else
    stato=0;
  merito += "#"+id+";"+stato;
  AggiornaPulsante('p_salva');
  }

function Correzione(elemento, i) {
  var valore=elemento.value;
  var id=elemento.id;
  var tot_c=document.getElementsByName("tot_c");
  var old_c=document.getElementsByName("old_c");
  var credito=document.getElementsByName("credito");
  var esito=document.getElementsByName("esito");
  correzione += "#"+id+";"+valore;
  AggiornaPulsante('p_salva');
  tot_c[i-1].value=valore*1 + 1*old_c[i-1].value + 1*credito[i-1].value;
  }

function Maggioranza(elemento, sospesi) {
  var stato=elemento.checked;
  var id=elemento.id;
  if (sospesi) {
    if (stato)
      stato=3;
    else
      stato=2;
    }
  else {
    if (stato)
      stato=1;
    else
      stato=0;
    }
  maggioranza += "#"+id+";"+stato;
  AggiornaPulsante('p_salva');
  }

function Manuale(elemento) {
  var stato=elemento.checked;
  var id=elemento.id;
  if (stato)
    stato=1;
  else
    stato=0;
  manuale += "#"+id+";"+stato;
  AggiornaPulsante('p_salva');
  }

function ComputoCredito(nclasse, i) {
  var cf = false;
  var media = "";
  var credito=document.getElementsByName("credito");
  var debiti=document.getElementsByName("ndebiti");
  var medie=document.getElementsByName("media");
  var differenziata=document.getElementsByName("differenziata");
  var merito=document.getElementsByName("merito");
  var formativi=document.getElementsByName("formativi");
  var esito=document.getElementsByName("esito");
  credito[i-1].style.backgroundColor="lightgrey";
  if (differenziata[i-1].checked)   //DIFFERENZIATA
    return credito[i-1].value = "";
  if ((debiti[i-1].value>0) && (nclasse!=5)) { //NUMERO DEBITI
    return credito[i-1].value = "";
    }
  if (medie[i-1].value)
    media = medie[i-1].value;
  if ((merito[i-1].checked) || (formativi[i-1].checked))
    cf = true;
  if (nclasse<5) {
    if (media>9) {
      return credito[i-1].value=8;
      }
    if (media>=8.5) {
      return credito[i-1].value=7;
      }
    if (media>8) {
      if (cf) return credito[i-1].value=7;
      return credito[i-1].value=6;
      }
    if (media>=7.5) {
      return credito[i-1].value=6;
      }
    if (media>7) {
      if (cf) return credito[i-1].value=6;
      return credito[i-1].value=5;
      }
    if (media>=6.5) {
      return credito[i-1].value=5;
      }
    if (media>6) {
      if (cf) return credito[i-1].value=5;
      return credito[i-1].value=4;
      }
    if (media==6) {
      if (cf) return credito[i-1].value=4;
      return credito[i-1].value=3;
      }
    else
      return credito[i-1].value="";
    }
  else if (nclasse==5) {
    if (esito[i-1].value=="Non Ammesso/a")
      return credito[i-1].value = "";
    if (media>9) {
      return credito[i-1].value=9;
      }
    if (media>=8.5) {
      return credito[i-1].value=8;
      }
    if (media>8) {
      if (cf) return credito[i-1].value=8;
      return credito[i-1].value=7;
      }
    if (media>=7.5) {
      return credito[i-1].value=7;
      }
    if (media>7) {
      if (cf) return credito[i-1].value=7;
      return credito[i-1].value=6;
      }
    if (media>=6.5) {
      return credito[i-1].value=6;
      }
    if (media>6) {
      if (cf) return credito[i-1].value=6;
      return credito[i-1].value=5;
      }
    if (media==6) {
      if (cf) return credito[i-1].value=5;
      return credito[i-1].value=4;
      }
    else
      return credito[i-1].value="";
    }
  }

function Credito(i) {
  var new_credito="";
  var cellcredito=document.getElementsByName("credito");
  var valore=cellcredito[i-1].value;
  var id=cellcredito[i-1].id;
  var tot_c=document.getElementsByName("tot_c");
  var old_c=document.getElementsByName("old_c");
  var correzione=document.getElementsByName("correzione");
  var esito=document.getElementsByName("esito");
  new_credito="#"+id+";"+valore;
  if (new_credito!=old_credito) {
    credito += new_credito;
    old_credito=new_credito
    }
  celle_credito +="#"+i;
  AggiornaPulsante('p_salva');
  tot_c[i-1].value=valore*1 + 1*old_c[i-1].value + 1*correzione[i-1].value;
  if (cellcredito[i-1])
    cellcredito[i-1].style.backgroundColor="#FFFF00";
  }

function Esito(i, sospesi) {
  var new_esito="";
  var esiti=document.getElementsByName("esito");
  var valore=esiti[i-1].value;
  var id=esiti[i-1].id;
  new_esito="#"+id+";"+valore;
  if (new_esito!=old_esito) {
    if (sospesi)
      esito_s += new_esito;
    else
      esito += new_esito;
    old_esito=new_esito;
    }
  AggiornaPulsante('p_salva');
  }

//----------------------------------------------
function CambiaVoti(i, elemento, q, classe, max_deb, sospesi, rel, con) {
  Medie(i, q, classe, max_deb, sospesi, rel, con);
  Modificato(elemento);
  Media(i);
  }

function Media(i) {
  var media=document.getElementsByName("media");
  var elemento=media[i-1];
  var id=elemento.id;
  var valore=elemento.value;
  medie +="#"+id+";"+valore;
  }

function Medie(i, q, classe, max_deb, sospesi, rel, con) {
  var media=document.getElementsByName("media");
  var ndebiti=document.getElementsByName("ndebiti");
  var condotta=document.getElementsByName("condotta");
  var religione=document.getElementsByName("religione");
  var voto=document.getElementsByName("voto"+i);
  var somma=0;
  var n_m=0;
  var v_m=0;
  var n_d=0;
  var mat="";
  var vl=voto.length;
  for (n=0; n<vl; n++) {
    if (voto[n].value) {
      if (voto[n].value<6) {
        n_d++;
        var id=voto[n].id;
        var materia=id.split(";", 1);
        mat += materia[0] + ", ";
        }
      somma+=parseInt(voto[n].value);
      n_m++;
      }
    }
  if ((con) && (condotta[i-1].value)) {
    somma+=parseInt(condotta[i-1].value);
    n_m++;
    }
  if ((rel) && (religione[i-1].value)) {
    somma+=DaRe(religione[i-1].value);
    n_m++;
    }
  v_m=(somma)/(n_m);
  v_m=v_m.toFixed(2);
  if (v_m!="NaN") {
    media[i-1].value=v_m;
    }
  else
    media[i-1].value="";
  ndebiti[i-1].value=n_d;
  if (q==1) {
    var nota=document.getElementsByName("nota");
    nota[i-1].value=mat;
    }
  else {
    ComputoEsito(i, classe, max_deb, n_d, mat);
    Esito(i, sospesi)
    if (classe>2) {
      ComputoCredito(classe, i);
      Credito(i);
      }
    }
  }

function ComputoEsito(i, classe, max_deb, debiti, mat) {
  var esito=document.getElementsByName("esito");
  var media=document.getElementsByName("media");
  var manuale=document.getElementsByName("manuale");
  var a_ore=document.getElementsByName("a_ore");
/*  if ((a_ore[i-1].value>25) && (!manuale[i-1].checked)) {
    esito[i-1].style.color="red";
    esito[i-1].style.fontWeight="normal";
    return esito[i-1].value="Non Ammesso/a";
    } */
  if (!media[i-1].value)  {
    esito[i-1].style.color="#ACA899";
    esito[i-1].style.fontWeight="normal";
    return esito[i-1].value="Non Scrutinato/a";
    }
  if (classe!=5) {
    if (manuale[i-1].checked) {
      esito[i-1].style.color="blue";
      esito[i-1].style.fontWeight="bold";
      return esito[i-1].value=mat;
      }
    if (debiti==0) {
      esito[i-1].style.color="black";
      esito[i-1].style.fontWeight="normal";
      return esito[i-1].value="Ammesso/a";
      }
    else if (debiti>max_deb) {
      esito[i-1].style.color="red";
      esito[i-1].style.fontWeight="normal";
      return esito[i-1].value="Non Ammesso/a";
      }
    else if (debiti<=max_deb) {
      esito[i-1].style.color="black";
      esito[i-1].style.fontWeight="bold";
      return esito[i-1].value=mat;
      }
    }
  else if (classe==5) {
    if (manuale[i-1].checked) {
      esito[i-1].style.color="blue";
      esito[i-1].style.fontWeight="bold";
      return esito[i-1].value="Ammesso/a";
      }
    if (debiti==0) {
      esito[i-1].style.color="black";
      esito[i-1].style.fontWeight="normal";
      return esito[i-1].value="Ammesso/a";
      }
    else if (debiti>0) {
      esito[i-1].style.color="red";
      esito[i-1].style.fontWeight="normal";
      return esito[i-1].value="Non Ammesso/a";
      }
  //L'AMMISSIONE DAL 2009/2010 E' CONDIZIONATA ALLA SUFFICIENZA IN TUTTE LE DISCIPLINE
/*    if (media[i-1].value<6) {
      esito[i-1].style.color="red";
      esito[i-1].style.fontWeight="normal";
      return esito[i-1].value="Non Ammesso/a";
      }
    else if (media[i-1].value>=6) {
      esito[i-1].style.color="black";
      esito[i-1].style.fontWeight="normal";
      return esito[i-1].value="Ammesso/a";
      }  */
    }
  }

function CambiaCondotta(elemento) {
  var id=elemento.id;
  var valore=elemento.value;
  AggiornaPulsante('p_salva');
  condotta1 +="#"+id+";"+valore;
  elemento.style.backgroundColor="#FFFF00";
  celle_condotta +="#"+id;
  }

function EvidenziaDoc(i) {
  var assegnazione=document.getElementsByName("assegnazione"+i);
  var ore=document.getElementsByName("ore");
  n_a=assegnazione.length;
  if (ore[i-1].id==0) {
    for (n=0; n<n_a; n++) {
      assegnazione[n].disabled=false;
      assegnazione[n].style.backgroundColor="#8B8B8B";    //"#C0C0C0";
      }
    ore[i-1].id=1;
    }
  else {
    for (n=0; n<n_a; n++) {
      assegnazione[n].style.backgroundColor="#E9E9E9";      //"#E9E9E9";
      assegnazione[n].disabled="true";
      }
    ore[i-1].id=0;
    }
  }

function EvidenziaRiga(i) {
  var si_sel="#DCD4BB";
  var no_sel_voto="#E9E9E9";
  var no_sel_condotta="lightgrey";
  var no_sel_debito="silver";
  var no_sel_assenza="#F8F8F8";
  var differenziata=document.getElementsByName("differenziata");
  var religione=document.getElementsByName("religione");
  var voto=document.getElementsByName("voto"+i);
  var alunno=document.getElementsByName("alunno");
  var assenza=document.getElementsByName("assenza"+i);
  var media=document.getElementsByName("media");
  var debito=document.getElementsByName("debito");
  var formativi=document.getElementsByName("formativi");
  var merito=document.getElementsByName("merito");
  var condotta=document.getElementsByName("condotta");
  var nota=document.getElementsByName("nota");
  var esito=document.getElementsByName("esito");
  var maggioranza=document.getElementsByName("maggioranza");
  var manuale=document.getElementsByName("manuale");
  var credito=document.getElementsByName("credito");
  var old_c=document.getElementsByName("old_c");
  var correzione=document.getElementsByName("correzione");
  var tot_c=document.getElementsByName("tot_c");
  if (alunno[i-1]) {
    if (alunno[i-1].id=="") {
      alunno[i-1].id=1;
      alunno[i-1].style.backgroundColor=si_sel;
      if (differenziata[i-1])
        differenziata[i-1].style.backgroundColor=si_sel;
      if (religione[i-1])
        religione[i-1].style.backgroundColor=si_sel;
      for (n=0; n<=voto.length; n++) {
        if (voto[n])
          voto[n].style.backgroundColor=si_sel;
        if (assenza[n])
          assenza[n].style.backgroundColor=si_sel;
        }
      if (condotta[i-1])
        condotta[i-1].style.backgroundColor=si_sel;
      if (media[i-1])
        media[i-1].style.backgroundColor=si_sel;
      if (debito[i-1])
        debito[i-1].style.backgroundColor=si_sel;
      if (formativi[i-1])
        formativi[i-1].style.backgroundColor=si_sel;
      if (merito[i-1])
        merito[i-1].style.backgroundColor=si_sel;
      if (credito[i-1])
        credito[i-1].style.backgroundColor=si_sel;
      if (old_c[i-1])
        old_c[i-1].style.backgroundColor=si_sel;
      if (correzione[i-1])
        correzione[i-1].style.backgroundColor=si_sel;
      if (tot_c[i-1])
        tot_c[i-1].style.backgroundColor=si_sel;
      if (nota[i-1])
        nota[i-1].style.backgroundColor=si_sel;
      if (esito[i-1])
        esito[i-1].style.backgroundColor=si_sel;
      if (maggioranza[i-1])
        maggioranza[i-1].style.backgroundColor=si_sel;
      if (manuale[i-1])
        manuale[i-1].style.backgroundColor=si_sel;
      }
    else {
      alunno[i-1].id="";
      alunno[i-1].style.backgroundColor=no_sel_assenza;
      if (differenziata[i-1])
        differenziata[i-1].style.backgroundColor=no_sel_voto;
      if (religione[i-1])
        religione[i-1].style.backgroundColor=no_sel_voto;
      for (n=0; n<=voto.length; n++) {
        if (voto[n])
          voto[n].style.backgroundColor=no_sel_voto;
        if (assenza[n])
          assenza[n].style.backgroundColor=no_sel_assenza;
        }
      if (condotta[i-1])
        condotta[i-1].style.backgroundColor=no_sel_condotta;
      if (media[i-1])
        media[i-1].style.backgroundColor=no_sel_condotta;
      if (debito[i-1])
        debito[i-1].style.backgroundColor=no_sel_debito;
      if (formativi[i-1])
        formativi[i-1].style.backgroundColor=no_sel_voto;
      if (merito[i-1])
        merito[i-1].style.backgroundColor=no_sel_voto;
      if (credito[i-1])
        credito[i-1].style.backgroundColor=no_sel_condotta;
      if (old_c[i-1])
        old_c[i-1].style.backgroundColor=no_sel_condotta;
      if (correzione[i-1])
        correzione[i-1].style.backgroundColor=no_sel_condotta;
      if (tot_c[i-1])
        tot_c[i-1].style.backgroundColor=no_sel_condotta;
      if (nota[i-1])
        nota[i-1].style.backgroundColor=no_sel_assenza;
      if (esito[i-1])
        esito[i-1].style.backgroundColor=no_sel_assenza;
      if (maggioranza[i-1])
        maggioranza[i-1].style.backgroundColor=no_sel_voto;
      if (manuale[i-1])
        manuale[i-1].style.backgroundColor=no_sel_voto;
      }
    }
  }

function Modificato(elemento) {
  var id=elemento.id;
  var valore=elemento.value;
  if (elemento.name=="religione")
    Religione(elemento);
  if ((valore < 6) && (valore > 0)) {
    elemento.style.color="red";
    }
  else {
    elemento.style.color="black";
    }
  elemento.style.backgroundColor="#FFFF00";
  controlli +="#"+id;
  voti1 +="#"+id+";"+valore;
  AggiornaPulsante('p_salva');
  }

function Religione(elemento) {
  if (elemento.value>=9)
    return elemento.value="O";
  if (elemento.value>=8)
    return elemento.value="B";
  if (elemento.value>=7)
    return elemento.value="DS";
  if (elemento.value>=6)
    return elemento.value="S";
  if (elemento.value>=5)
    return elemento.value="M";
  if ((elemento.value<5) && (elemento.value>0))
    return elemento.value="I";
  }

function DaRe(ele) {
  if ((ele=="O") || (ele=="o"))
    return 9;
  else if ((ele=="B") || (ele=="b"))
    return 8;
  else if ((ele=="DS") || (ele=="ds"))
    return 7;
  else if ((ele=="S") || (ele=="s"))
    return 6;
  else if ((ele=="M") || (ele=="m"))
    return 5;
  else if ((ele=="I") || (ele=="i"))
    return 4;
  else
    return 0;
  }

function StatoScrutinio(elemento) {
  var stato=elemento.checked;
  var id=elemento.id;
  if (stato)
    stato=1;
  else
    stato=0;
  stato_s1=id+";"+stato;
  AggiornaPulsante('p_salva');
  }

function Stampa() {
  if ((voti1) || (assenze1) || (condotta1) || (stato_s1) || (differenziata) || (formativi) ||
      (merito) || (credito) || (correzione) || (maggioranza) || (manuale) || (esito) || (esito_s)) {
    var cf=confirm('Salvare le modifiche?');
    if (cf == true)
      Salva(1);
    }
  print();
  }

function ChiudiS(indirizzo,page) {
  if ((voti1) || (assenze1) || (condotta1) || (stato_s1) || (differenziata) || (formativi) ||
      (merito) || (credito) || (correzione) || (maggioranza) || (manuale) || (esito) || (esito_s)) {
    var cf=confirm('Salvare le modifiche?');
    if (cf == true)
      Salva(1);
    }
  window.location=page+"&id_indirizzo="+indirizzo;
  }

function CE(indirizzo, sel, mes) {
  var cf=confirm(mes);
  if (cf == true)
    window.location=indirizzo+sel;
  }

function Aggiorna(indirizzo, elemento) {
  var valore=elemento.value;
  window.location=indirizzo+"="+valore;
  }

function Pubblica(classe, ii, cl, sospesi, page, sele) {
  if ((voti1) || (assenze1) || (condotta1) || (stato_s1) || (differenziata) || (formativi) ||
      (merito) || (credito) || (correzione) || (maggioranza) || (manuale) || (esito) || (esito_s)) {
    var cf=confirm('Salvare le modifiche?');
    if (cf == true)
      Salva(1);
    }
  window.location=page+"?selezione="+sele+"&09bca1239="+classe+"&ii="+ii+"&cl="+cl;
  }

function Dipartimento(elemento, n) {
  var id=elemento.id;
  var valore=elemento.value;
  if (n==1)
    dipartimento +="#"+id+";"+valore;
  else if (n==2)
    dipartimento2 +="#"+id+";"+valore;
  else if (n==3)
    dipartimento3 +="#"+id+";"+valore;
  else if (n==4)
    ext +="#"+id+";"+valore;
  AggiornaPulsante('p_salva');
  }

function Attivo(elemento) {
  var id=elemento.id;
  var valore=elemento.checked;
  if (valore) valore=1; else valore=0;
  attivo +="#"+id+";"+valore;
  AggiornaPulsante('p_salva');
  }

function File(elemento) {
  var id=elemento.id;
  var valore=elemento.value;
  v1[id]="#"+id+";"+valore;
  AggiornaPulsante('p_salva');
  }

function GetXMLHttpRequest(handler) {
  var
    XHR = null,
    BU = navigator.userAgent.toUpperCase();

  if (typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object")  //Supporto nativo
    XHR = new XMLHttpRequest();
  else if (window.ActiveXObject && BU.indexOf("MSIE 4") < 0) {
    if (BU.indexOf("MSIE 5") < 0)
      XHR = new ActiveXObject("Msxml2.XMLHTTP");
    else
      XHR = new ActiveXObject("Microsoft.XMLHTTP");
    }
    XHR.onreadystatechange = handler;

  return XHR;
  }

function Salva(i) {
  var pulsante=document.getElementsByName("p_salva");
  sender=i;
  salvataggio = GetXMLHttpRequest(PostSalva);
  salvataggio.open("post", "da_script.inc.php", true);
  salvataggio.setRequestHeader("content-type", "application/x-www-form-urlencoded");
  salvataggio.setRequestHeader("connection", "close");
  if (i==1) {
    salvataggio.send("q=1"+"&voti1="+voti1+"&assenze1="+assenze1+"&condotta1="+condotta1+"&stato_s1="+stato_s1+
    "&differenziata="+differenziata+"&formativi="+formativi+"&merito="+merito+"&credito="+credito+"&correzione="+correzione+
    "&maggioranza="+maggioranza+"&manuale="+manuale+"&esito="+esito+"&esito_s="+esito_s+"&medie="+medie);
    }
  else if (i==2) {
    var a_mese=v1.join("");
    salvataggio.send("a_mese="+a_mese);
    }
  else if (i==3) {
    var s_interessi=interessi.join("");
    var s_comportamenti=comportamenti.join("");
    var s_note=note.join("");
    var s_complementari=complementari.join("");
    salvataggio.send("interessi="+s_interessi+"&comportamenti="+s_comportamenti+"&note="+s_note+"&complementari="+s_complementari);
    }
  else if (i==4) {
    var piani=v1.join("");
    salvataggio.send("piani="+piani);
    }
  else if (i==5) {
    f=v1.join("");
    salvataggio.send("dipartimento="+dipartimento+"&dipartimento2="+dipartimento2+"&dipartimento3="+dipartimento3+
                     "&attivo="+attivo+"&f="+f+"&ext="+ext);
    }
  else if (i==6) {
    var asse=v1.join("");
    salvataggio.send("asse="+asse);
    }
  else if (i==7) {
    var ore=v1.join("");
    salvataggio.send("ore="+ore);
    }
  pulsante[0].disabled = true;
  }

function PostSalva() {
  if (salvataggio.readyState == 4 && salvataggio.status == 200) {
    if (sender==1) {
      voti1 = "";
      assenze1 = "";
      condotta1 = "";
      stato_s1 = "";
      differenziata = "";
      formativi = "";
      merito = "";
      credito = "";
      correzione = "";
      maggioranza = "";
      manuale = "";
      esito = "";
      esito_s = "";
      medie = "";
      controllo=controlli.split("#");
      var len=controllo.length;
      for(i=1; i<len; i++) {
        var cella=document.getElementById(controllo[i]);
        cella.style.backgroundColor="#E9E9E9";
        }
      controlli = "";
      cella_condotta=celle_condotta.split("#");
      var len=cella_condotta.length;
      for(i=1; i<len; i++) {
        var cella=document.getElementById(cella_condotta[i]);
        cella.style.backgroundColor="lightgrey";
        }
      celle_condotta = "";
      cella_credito=celle_credito.split("#");
      var len=cella_credito.length;
      for(i=1; i<cella_credito.length; i++) {
        var cella=document.getElementsByName("credito");
        cella(cella_credito[i]-1).style.backgroundColor="lightgrey";
        }
      celle_credito = "";
      }
    else if (sender==2) {
      v1 = [];
      //temp_a=[];
      controllo=controlli.split("#");
      var len=controllo.length;
      for(i=1; i<len; i++) {
        var cella=document.getElementById(controllo[i]);
        cella.style.backgroundColor="#F8F8F8";
        }
      controlli = "";
      }
    else if (sender==3) {
      interessi = [];
      comportamenti = [];
      note = [];
      complementari = [];
      }
    else if (sender==4) {
      v1=[];
      controllo=controlli.split("#");
      var len=controllo.length;
      for(i=1; i<len; i++) {
        var cella=document.getElementById(controllo[i]);
        cella.style.backgroundColor="#F8F8F8";
        }
      }
    else if (sender==5) {
      dipartimento="";
      dipartimento2="";
      dipartimento3="";
      attivo="";
      v1=[];
      ext="";
      }
    else if (sender==6 || sender==7) {
      v1=[];
      }
    alert(salvataggio.responseText);
    }
  else if (salvataggio.readyState == 4 && salvataggio.status != 200) {
    alert("Attenzione! Per problemi di comunicazione con il server, il salvataggio non è stato effettuato: Riprovare!");
    AggiornaPulsante('p_salva');
    }
  }

function MostraMessaggio(oggetto) {
  var oggetto=document.getElementById(oggetto);
  var visibile = oggetto.style.visibility;
  var res = "";
  if (visibile=="visible")
    res = "hidden";
  else
    res = "visible";
  oggetto.style.visibility = res;
  }
//*************
function colore1() {
  codice="<FONT style=BACKGROUND-COLOR:yellow>RETTIFICHE</font>";
  if(document.getElementById) {
    document.getElementById("testo").innerHTML = codice;
    }
  window.setTimeout('colore2()',500);
  }

function colore2() {
  codice="<FONT style=BACKGROUND-COLOR:lime>DEFINITIVO</font>";
  if(document.getElementById) {
    document.getElementById("testo").innerHTML =codice;
    }
  window.setTimeout('colore1()',500);
  }

function avvia() {
  var testo=document.getElementById("testo");
  if (testo)
    window.setTimeout('colore1()',100);
  }

function ValidaPass() {
  var user=document.getElementById("user");
  var pass1=document.getElementById("pss1");
  var pass2=document.getElementById("pss2");
  var old=document.getElementById("old");
  if (!user.value) {
    alert("Digitare l'username!");
    return;
    }
  else if ((!pass1.value) && (!old.value)) {
    alert("Inserire la password!");
    return;
    }
  else if ((pass1.value) && (pass1.value.length<10)) {
    alert("La password deve contenere almeno 10 caratteri alfanumerici!");
    return;
    }
  else if ((pass1.value) && (!pass2.value)) {
    alert("Digitare la password anche in 'Conferma Password'!");
    return;
    }
  else if (pass1.value!=pass2.value) {
    alert("Le due password non sono uguali!");
    return;
    }
  else
    document.f.submit();
  }

function ValidaSonda() {
  var nr=document.getElementById("nr");
  var vx=document.getElementById("vx");
  var o1=document.getElementById("o1");
  var o2=document.getElementById("o2");
  o1=o1.options[o1.selectedIndex].value;
  o2=o2.options[o2.selectedIndex].value;
  if ((!nr.value) || (isNaN(nr.value))) {
    alert("Digitare un numero per la domanda!");
    return;
    }
  else if (!vx.value) {
    alert("Inserire la domanda!");
    return;
    }
  else if ((o1==0) && (o2==0)) {
    alert("Selezionare almeno le prime due opzioni!");
    return;
    }
  else
    document.m.submit();
  }

function Aggiusta(stringa) {
  stringa = stringa.replace(/à/g, "a");
  stringa = stringa.replace(/è/g, "e");
  stringa = stringa.replace(/é/g, "e");
  stringa = stringa.replace(/ì/g, "i");
  stringa = stringa.replace(/ò/g, "o");
  stringa = stringa.replace(/ù/g, "u");
  stringa = stringa.replace(/\x27/g, "");  //sostituisce apice
  return stringa.replace(/-/g, "");
  }

function CreaCF() {
  var pulsante=document.getElementsByName("ccf");  //Inserire tutti i dati daleggere con get element
  var alunno=document.getElementsByName("alunno[alunno]"); alunno=Aggiusta(alunno[0].value);
  var giorno=document.getElementsByName("alunno[giorno]"); giorno=giorno[0].value;
  var mese=document.getElementsByName("alunno[mese]");     mese=mese[0].value;
  var anno=document.getElementsByName("alunno[anno]");     anno=anno[0].value;  var data=giorno+mese+anno;
  var sesso=document.getElementsByName("alunno[sesso]");    sesso=(sesso[0].checked ? "M" : "F");
  var citta=document.getElementsByName("alunno[n_luogo]");     citta=Aggiusta(citta[0].value);
  var pr=document.getElementsByName("alunno[n_pr]");     pr=pr[0].value;

  alu=alunno.split(" ");   cognome=alu[0];
  nome="";
  var len=alu.length;
  for(i=1; i<len; i++)
    nome+=alu[i]+" ";
//  alert("data"+data+"sesso"+sesso+"citta"+citta+"pr"+pr+"cognome"+cognome+"nome"+nome);
  salvataggio = GetXMLHttpRequest(PostCCA);
  salvataggio.open("post", "cf.php", true);
  salvataggio.setRequestHeader("content-type", "application/x-www-form-urlencoded");
  salvataggio.setRequestHeader("connection", "close");
  salvataggio.send("cognome="+cognome+"&nome="+nome+"&data="+data+"&sesso="+sesso+"&citta="+citta+"&pr="+pr);
  pulsante[0].disabled = true;
  }

function PostCCA() {
  var pulsante=document.getElementsByName("ccf");
  var cf=document.getElementsByName("alunno[cfiscale]");
  if (salvataggio.readyState == 4 && salvataggio.status == 200) {
    cf[0].value=salvataggio.responseText;
    //alert(salvataggio.responseText);
    }
  else if (salvataggio.readyState == 4 && salvataggio.status != 200) {
    alert("Errore riprovare!");
    }
  pulsante[0].disabled = false;
  }

function InviaPW(ute) {
  var pulsante=document.getElementsByName("invpw");
  var usr=document.getElementsByName(ute+"[user]");
  var psw=document.getElementsByName(ute+"[pass]");
  var eml=document.getElementsByName(ute+"[email]");
  var alu=document.getElementsByName(ute+"["+ute+"]");
  salvataggio = GetXMLHttpRequest(PostINV);
  salvataggio.open("post", "inviopw.php", true);
  salvataggio.setRequestHeader("content-type", "application/x-www-form-urlencoded");
  salvataggio.setRequestHeader("connection", "close");
  salvataggio.send("usr="+usr[0].value+"&psw="+psw[0].value+"&eml="+eml[0].value+"&alu="+alu[0].value+"&ute="+ute);
  pulsante[0].disabled = true;
  }

function PostINV() {
  if (salvataggio.readyState == 4 && salvataggio.status == 200) {
    var dti=document.getElementsByName("inviop");
    var d = new Date();
    dti[0].value=d.getDate()+"/"+(d.getMonth()+1)+"/"+d.getFullYear();
    alert(salvataggio.responseText);
    }
  else if (salvataggio.readyState == 4 && salvataggio.status != 200) {
    alert("Attenzione, messaggio non inviato: Riprovare!");
    var pulsante=document.getElementsByName("invpw");
    pulsante[0].disabled = false;
    }
  }

function Disattiva(ele) {
  var stringa="";
  n=document.f.length-8;
  stop=false;
  if (!ele.checked)
    stop=true;
  for (i=0; i<n; i++)
    stringa+=document.f.elements[i].disabled = stop;
//  alert(stringa);
  }

function clic(e) {
  e.style.backgroundColor='#D4D0C8';
  }

function via(e) {
  if (e.checked)
    e.style.backgroundColor='';
  }

function Segna(elemento, i) {
  var rec=document.getElementsByName("record");
  var val=elemento.value;
  var n=elemento.name;
  if (n==1)
    rec.value=val;
    rec.id=i;
  }

function Inter(elemento, o) {
  var rec=document.getElementsByName("record");
  var ass=document.getElementsByName("a_ore");
  var ap=document.getElementsByName("a_orep");
  var pulsante=document.getElementsByName("salva");
  var sta=document.getElementsByName("stampa");
  var val=elemento.value;
  var id=elemento.id;
  var n=elemento.name;
  v+="#"+n+";"+id+";"+val;
  pulsante[0].disabled = false;
  sta[0].disabled = true;
  if (n==1) {
    ass[rec.id-1].value=(ass[rec.id-1].value) - (rec.value) + val*1;
    var perc=(ass[rec.id-1].value)/(o)*100;
    ap[rec.id-1].value=perc.toFixed(1)+"%";
    }
  }

function Registra(elemento, i) {
  var rec=document.getElementsByName("record");
  var val=elemento.value;
  rec.value=val;
  rec.id=i;
  }

function Ass_tot(elemento, o) {
  var rec=document.getElementsByName("record");
  var ap=document.getElementsByName("a_orep");
  var ass=document.getElementsByName("a_ore");
  var val=elemento.value;
  var id=elemento.id;
  ass[rec.id-1].value=(ass[rec.id-1].value) - (rec.value) + val*1;
  var perc=(ass[rec.id-1].value)/(o)*100;
  ap[rec.id-1].value=perc.toFixed(1)+"%";
  if (perc>=25)
    ap[rec.id-1].style.color="red";
  else
    ap[rec.id-1].style.color="black";
  }

function inters() {
  var pulsante=document.getElementsByName("salva");
  var sta=document.getElementsByName("stampa");
  salvataggio = GetXMLHttpRequest(Posti);
  salvataggio.open("post", "da_script.inc.php", true);
  salvataggio.setRequestHeader("content-type", "application/x-www-form-urlencoded");
  salvataggio.setRequestHeader("connection", "close");
  salvataggio.send("inter="+v);
  pulsante[0].disabled = true;
  sta[0].disabled = false;
  }

function Posti() {
  var pulsante=document.getElementsByName("salva");
  if (salvataggio.readyState == 4 && salvataggio.status == 200) {
    alert(salvataggio.responseText);
    v="";
    }
  else if (salvataggio.readyState == 4 && salvataggio.status != 200) {
    alert("Errore, riprovare!");
    pulsante[0].disabled = false;
    }
  }

function Avviso(b) {
  alert(b);
  }

function VaiA(pagina) {
  window.location=pagina;
  }

function Ora(ele, nc) {
  var idd=ele.id;
  var id=idd.split(";");
  var valore=ele.value;
  if (!db_v) {
    if (valore) {
      ele.value="";
      if (!db_n) {
        ele.style.backgroundColor='#FFFF00';
        db_n=nc;
        }
      else
        ele.style.backgroundColor='#D7D7D7';
      db_v=valore;
      db_m=id[1];
      db_s=id[2];
      NewOre(id[0], "", "", "");
      }
    }
  else {
    if (!valore) {
      ele.value=db_v;
      ele.style.fontWeight="bold";
      NewOre(id[0], db_m, db_s, db_v);
      db_v="";
      db_m="";
      db_s="";
      if (nc==db_n)
        db_n="";
      }
    else {
      ele.value=db_v;
      ele.style.fontWeight="bold";
      NewOre(id[0], db_m, db_s, db_v);
      db_v=valore;
      db_m=id[1];
      db_s=id[2];
      }
    }
  }

function NewOre(id, mat, str, ele) {
  v1[id]="#"+id+";"+mat+";"+str+";"+ele;
  AggiornaPulsante('p_salva');
  }

function Plus_o(ele) {
  var rec=document.getElementsByName("record");
  var idd=ele.id;
  var id=idd.split(";");
  var valore=ele.value;
    if (valore=="D" || valore=="P" || (valore=="" && (rec.value=="D" || rec.value=="P"))) {
      ele.style.backgroundColor='#C0C0C0';
      ele.style.fontWeight="bold";
      ele.style.color=(valore=="D" ? "#FF0000" : "#0000FF");
      NewOre(id[0], "", "", valore);
      }
  }


function Colonna (n, d) {
  for(i=0; i<d; i++) {
    k=n+i*36;
    var cla=document.getElementById('classe'+k);
    cla.style.backgroundColor='#CFCFCF';
    }
  }

function Riga(i) {
  var rg=document.getElementById('riga'+i);
  if (rg.style.backgroundColor=='lightgrey') {
    rg.style.backgroundColor=rg.name;
    }
  else {
    rg.style.backgroundColor='lightgrey';
    }
  }


/*
function Conferma(b, g) {
  var cf=confirm(b);
  if (cf == true)
    document.ora.submit();
  else {
    if (b)
      window.location=g;
    return;
    }
  } */


