function stampaMain() {
   top.mainFrame.focus()
   top.mainFrame.print()
}

function attivaHelp(tipo) {
   var help=window.open("help/"+tipo+".html","help","width=450 height=360 resizable=yes scrollbars=yes")
   help.focus()
}

function aggiornaRight(tabDaAprire,apriChiudi) {
	if (mode=="indici") {
		if (numeroModulo==0) {
			alert("Scegli un modulo dall'indice di sinistra.")
			return
		} else if (top.numeroUnit==0) {
			alert("Scegli un'unitā dall'indice di sinistra.")
			return
		} else if (top.codiceEsercizio=="") {
			alert("Scegli un esercizio dall'indice di sinistra.")
			return
		}
	}
	if (mode=="valutazione") {
		alert("Quest'area non č disponibile durante la consultazione della valutazione complessiva.")
		return
	}
	if (tabDaAprire==right_tabAttivo && apriChiudi) {
		tabDaAprire=""
	}
	right_tabAttivo=tabDaAprire
	top.rightTopFrame.location.reload()
	if (tabDaAprire=="indice") tabDaAprire="indiceRight"
	if (tabDaAprire=="") tabDaAprire="right"
	top.rightFrame.location=tabDaAprire+".html"
}

function indiceModulo(numero) {
	numeroModulo=numero
	numeroUnit=0
	tipoEsercizio=""
	codiceEsercizio=""
	right_tabAttivo=""
	right_indice=""
	right_teoria=""
	right_report=""
	bottom_stampa=false
	bottom_conferma=false
	bottom_teoria=false
	mode="indici"

	top.topFrame.location.reload()
	top.mainFrame.location="indiceModulo.html"
	top.rightFrame.location="right.html"
	top.rightTopFrame.location.reload()
	top.leftFrame.location.reload()
	top.mainBottomFrame.location.reload()
}

function indiceUnit(numMod,numUn) {
	numeroModulo=numMod
	numeroUnit=numUn
	codiceEsercizio=""
	tipoEsercizio=""
	right_tabAttivo=""
	right_indice=""
	right_teoria=""
	right_report=""
	bottom_stampa=false
	bottom_conferma=false
	bottom_teoria=false
	mode="indici"

	top.topFrame.location.reload()
	top.mainFrame.location="indiceModulo.html"
	top.rightFrame.location="right.html"
	top.rightTopFrame.location.reload()
	top.leftFrame.location.reload()
	top.mainBottomFrame.location.reload()
}

function esercizio(codice,metadata,numero) {
	codiceEsercizio=codice
	codiceMetadata=metadata
	numeroEsercizio=numero
	bottom_stampa=true
	bottom_conferma=true

	reportAttivo=""
	risultatiRicerca=[]
	mode="esercizi"
	primoTentativo="0"
	right_tabAttivo="indice"
	top.numeroCorrezioni=0
	
	top.mainFrame.location="esercizio.html"
	top.rightTopFrame.location.reload()
	top.rightFrame.location="indiceRight.html"
	top.topFrame.location.reload()
}

function valutazioneComplessiva() {
	var val=leggiElementoCookie('','sessione')
	if (val.charAt(0)=="/") val=val.substr(1);
	if (val=="_noCookie" || val=="") {
		alert("Nella sessione di lavoro in corso non hai ancora svolto alcun esercizio!\nLa scheda riassuntiva della tua valutazione non contiene dati.")
	} else {
		oldMode=mode
		mode="valutazione"
		bottom_stampa=true
		top.mainBottomFrame.location.reload()
		top.topFrame.location.reload()
		top.leftFrame.location.reload()
		top.rightTopFrame.location.reload()
		top.rightBottomFrame.location.reload()
		top.rightFrame.location="right.html"
		top.mainFrame.location="reportComplessivo.html"
	}
}

function chiudiValutazione() {
	mode=oldMode
	if (numeroModulo==0) {
		top.location="indexEsercizi.html"
	} else if (numeroUnit==0) {
		indiceModulo(numeroModulo)
	} else if (codiceEsercizio=="") {
		indiceUnit(numeroModulo,numeroUnit)
	} else if (codiceEsercizio!="") {
		esercizio(codiceEsercizio,codiceMetadata,numeroEsercizio)
	}
	top.rightBottomFrame.location.reload()
}

function cercaTeoria(input) {
	if (mode!="esercizi" && mode!="cerca") {
		alert("Nella sezione ESERCIZI, la funzione CERCA permette di trovare\n le informazioni teoriche utili allo svolgimento degli esercizi stessi.\nČ necessario quindi selezionare un esercizio prima di accedere alla funzione.\n\nSe vuoi effettuare una ricerca senza limitazioni vai alla sezione GRAMMATICA.")
		return
	}
   var t=titoliTeoria
   var parole=input.split(/\s+/)
   var listaFound=[]
   var provv= new RegExp("","g")
   
   for (var i=0;i<t.length;i++) {
      var occ=0
      for (var j=0;j<parole.length;j++) {
	     provv.compile("\\b"+parole[j]+"\\b","gi")
         if (t[i][2].match(provv)!=null) {
		    occ++
		 }
	  }
	  if (occ>0) listaFound[listaFound.length]=[t[i][0],t[i][1],occ]
   }
	if (listaFound.length==0) {
		alert("Non vi sono corrispondenze per la ricerca effettuata.")
		return
	}

	listaFound=ordinaLista(listaFound)
	risultatiRicerca=listaFound
	right_teoriaCerca=""
	mode="cerca"
	aggiornaRight("teoria")
}

function ordinaLista(lista) {
   var listaOrdinata=[[],[],[],[],[],[],[],[],[],[]]
   for (var i=0;i<lista.length;i++) {
      var ord=Math.max(0,10-lista[i][2])
	  listaOrdinata[ord][listaOrdinata[ord].length]=lista[i]
   }
   var listaOut=[]
   for (var i=0;i<10;i++) {
      listaOut=listaOut.concat(listaOrdinata[i])
   }
 return listaOut
}

function terminaRicerca() {
	risultatiRicerca=[]
	mode="esercizi"
	aggiornaRight("teoria")
}

function apriCercaTeoria (codice) {
	right_teoriaCerca=codice
	aggiornaRight("teoria")
}

function chiudiTeoriaCerca () {
	right_teoriaCerca=""
	aggiornaRight("teoria")
}

