function InviaFormazione()
{
var destinatari, mail;

if (tipoInvio == 0 && !VerificaPassword()) return;

if (!ControllaFormazione()) return;

var i; var j = 0;
for (i = 1; document.getElementById("cbInc" + i) != null; i++)
if (document.getElementById("cbInc" + i).checked == true) j++;
if (j == 0) {
alert("Impossibile inviare la formazione: nessun incontro selezionato");
return;
}

var i; var x = 0; var y = 0;
for (i = 1; document.getElementById("cbInc" + i) != null; i++)

if (document.getElementById("cbInc" + i).checked == true) {
var ii = arrIncontri[document.getElementById("cbInc" + i).value];
if ((destinatari = OttieneDestinatari(ii)) == "") continue;
mail = GeneraEMail(ii);

switch(tipoInvio) {

case 0: // client-side
var url = "mailto:" + destinatari;
url += "?subject=" + escape(mail[0]);
url += "&body=" + escape(mail[1]);

document.getElementById("emailData").method = "post";
document.getElementById("emailData").action = url;
document.getElementById("emailData").submit();
break;

case 1: // server-side
var mittente = mittenteEMail != "" ? mittenteEMail : arrFantasquadre[idxFsq].Email;
if (mittente == "") {
alert("Impossibile inviare la formazione: indirizzo di e-mail del mittente non valido");
return;
}
x += 20;
y += 20;
window.open("", "ssWindow" + i, "width=600,height=190,screenX=" + x + ",screenY=" + y);

document.getElementById("edUsername").value = cFsq;
document.getElementById("edPassword").value = document.getElementById("inPwd").value;
document.getElementById("edSender").value = mittente;
document.getElementById("edRecipient").value = destinatari;
document.getElementById("edSubject").value = mail[0];
document.getElementById("edBody").value = mail[1];
document.getElementById("edGiornataDiA").value = cGio;
document.getElementById("edIdSquadra").value = cFsq;
document.getElementById("edIdIncontro").value = ii.ID
if (salvaFormazione) document.getElementById("edSaveData").value = GeneraFormazione(ii);
document.getElementById("emailData").target = "ssWindow" + i;
document.getElementById("emailData").action = sendmailURL;
document.getElementById("emailData").submit();

break;
}
}
}


function GeneraEMail(ii)
{
// Determina incontro e se prevede rigoristi
var r = (ii.IDTipo == INC_ELIMDIRETTA_DIR) || (ii.IDTipo == INC_ELIMDIRETTA_RIT) || (ii.IDTipo == INC_ELIMDIRETTA_DIR_FC);

// Ottiene nome fantasquadra e nome della competizione
var fsq = arrFantasquadre[idxFsq].Nome;
var cmp = ii.Competizione;
var fsq_cmp = fsq + (incontriValidi.length > 1 ? " [" + cmp + "]" : "");

// Costruisce il subject
var subject = "Formazione " + fsq_cmp +", " + cmp + ", " + nomelega + " [MessaggioFrmFCM:" + cGio + "]";

// Costruisce l'header del messaggio
var message1 = "Lega: " + nomelega + "\n";
message1 += "Squadra: " + fsq_cmp +"\n"
message1 += "Giornata: " + ii.Fantagiornata + "\n";
message1 += "Data e ora compilazione: " + DataOraCorrente() + "\n";

// Costruisce la parte del messaggio contenente la formazione
var n = 1;
var i, message2;
for (i = 1; i <= 11 + totaleNumeroMassimoRiserve + 2; i++) {

if (i == 1) message2 = "--- Titolari ---\n";
else if (i == 13) message2 += "--- Riserve ---\n";
else {
if (arrFormazione[n] >= 0 && arrFormazione[n] < arrInvioFormazione.length) {
var gg = arrInvioFormazione[arrFormazione[n]];
message2 += ruoli[gg.Ruolo] + " " + eval(gg.Nome) + " (" + eval(gg.SquadraDiA) + ")";
message2 += r ? " [R: " + arrRigoristi[n] + "]\n" : "\n";
}
n++;
}
}

// Costruisce la terza parte del messaggio
var message3 = "CODICI IDENTIFICATIVI FCM [*NON MODIFICARE*]\n";
message3 += "[LEGA]=" + CorreggeNomeLega(nomelega) + "-" + stagione + "-" + anno.substr(0, 4) + "\\\n";
message3 += "[IDSQUADRA]=" + cFsq + "\\\n";
message3 += "[GIORNATADIA]=" + cGio + "\\\n";
message3 += "[IDINCONTRO]=" + ii.ID + "\\\n";
message3 += "[FRMCODE]=";
var n;
for (n = 1; n <= 11 + totaleNumeroMassimoRiserve; n++) {
if (arrFormazione[n] >= 0 && arrFormazione[n] < arrInvioFormazione.length) {
var gg = arrInvioFormazione[arrFormazione[n]];
message3 += (n!= 1 ? "-" : "") + gg.ID;
message3 += r ? "R" + arrRigoristi[n] : "";
}
}
message3 += "\\\n\n";
message3 += "Comunicazioni: " + document.getElementById("taCom").value;

var message = message1 + "\n" + message2 + "\n" + message3;

return new Array(subject, message);
}


function ControllaFormazione()
{
// Controlla titolari inseriti
if (titolariInseriti != 11) {
alert("Impossibile inviare la formazione: uno o più titolari non inseriti");
return false;
}

// Controlla riserve inserite
if (totaleRiserveInserite < totaleNumeroMassimoRiserve)
if (!confirm("La formazione è incompleta, vuoi inviarla comunque?")) return;

// Determina se incontri selezionati richiedono rigoristi
if (!rigoristi) return true;
var r = false;
var i;
for (i = 1; document.getElementById("cbInc" + i) != null; i++)
if (document.getElementById("cbInc" + i).checked == true) {
var ii = arrIncontri[document.getElementById("cbInc" + i).value];
if ((ii.IDTipo == INC_ELIMDIRETTA_DIR) || (ii.IDTipo == INC_ELIMDIRETTA_RIT) || (ii.IDTipo == INC_ELIMDIRETTA_DIR_FC)) r = true;
}
if (!r) return true;

// Genera array rigoristi e lo controlla
for (i = 1; i <= 11 + totaleNumeroMassimoRiserve; i++) {
arrRigoristi[i] = document.getElementById("f_rigorista" + i).value;
if (arrFormazione[i] != -1) arrInvioFormazione[arrFormazione[i]].Rigorista = arrRigoristi[i];
}

var j;
for (i = 1; i <= 11 + totaleNumeroMassimoRiserve - 1; i++)
for (j = i + 1; j <= 11 + totaleNumeroMassimoRiserve; j++)
if (arrFormazione[i] != -1 && arrFormazione[j] != -1 && arrRigoristi[i] == arrRigoristi[j]) {
alert("Impossibile inviare la formazione: ordine rigoristi non valido (duplicato)");
return false;
}

if (regolaRigoristi)
for (i = 1; i <= 11 + totaleNumeroMassimoRiserve; i++)
if (arrFormazione[i] != -1 && arrRigoristi[i] < 11 && arrInvioFormazione[arrFormazione[i]].Ruolo == 1) {
alert("Impossibile inviare la formazione: ordine rigoristi non valido (portiere < 11°)");
return false;
}

return true;
}


function OttieneDestinatari(ii)
{
var destinatari = new Array();

// Webmaster
if (document.getElementById("cbDest2").checked) arrayAdd(destinatari, WebmasterEmail);

// Presidente di lega
if (document.getElementById("cbDest4").checked) arrayAdd(destinatari, presidenteDiLegaEMail);

// Tutti i presidenti
if (document.getElementById("cbDest5").checked)
for (f = 1; f < arrFantasquadre.length; f++) arrayAdd(destinatari, arrFantasquadre[f].Email);
else {
// Se stessi
if (document.getElementById("cbDest3").checked) arrayAdd(destinatari, arrFantasquadre[idxFsq].Email);

// Avversario
if (document.getElementById("cbDest1").checked && ii.IDTipo != INC_GRANPREMIO) {
var idAvv = ii.IDSquadre.Casa == cFsq ? ii.IDSquadre.Fuori : ii.IDSquadre.Casa;
for (f = 1; f < arrFantasquadre.length; f++) if (arrFantasquadre[f].ID == idAvv) break;
arrayAdd(destinatari, arrFantasquadre[f].Email);
}
}

if (destinatari.length == 0) {
alert("Impossibile inviare la formazione: nessun destinatario valido selezionato");
return "";
}

return destinatari.join("; ");
}


function GeneraFormazione(ii)
{
// Determina incontro e se prevede rigoristi
var r = (ii.IDTipo == INC_ELIMDIRETTA_DIR) || (ii.IDTipo == INC_ELIMDIRETTA_RIT) || (ii.IDTipo == INC_ELIMDIRETTA_DIR_FC);

// Costruisce la string di formazione
var formazione = new Array();
var idLega = ii.IDSquadre.Casa == cFsq ? ii.IDLegaSquadre.Casa : ii.IDLegaSquadre.Fuori;
var i;
for (i = 0; i < arrRosa.length; i++) {
var gg = arrInvioFormazione[arrRosa[i]];
var pos;
if (gg.Formazione == 0) pos = -1;
else if (gg.Formazione <= 11) pos = 0;
else pos = gg.Formazione - 11;
formazione.push(ii.ID + "," + cFsq + "," + idLega + "," + gg.Nome + "," + gg.SquadraDiA + "," + gg.Ruolo + "," + pos + "," + ((r && pos >= 0) ? gg.Rigorista : 0));
}

return formazione.join("|");
}


function VerificaPassword()
{
var password = document.getElementById("inPwd").value;
var crypted = Javacrypt.crypt("jd", password);

if (crypted[0] != passwords[arrFantasquadre[idxFsq].ID]) {
alert("Impossibile inviare la formazione: password non valida");
return false;
}

return true;
}


function DataOraCorrente()
{
var dataOra = new Date();

var g = "0" + dataOra.getDate();
g = g.substr(g.length - 2, 2);
var m = "0" + (dataOra.getMonth() + 1);
m = m.substr(m.length - 2, 2);
var a = "000" + dataOra.getFullYear();
a = a.substr(a.length - 4, 4);

var hh = "0" + dataOra.getHours();
hh = hh.substr(hh.length - 2, 2);
var mm = "0" + dataOra.getMinutes();
mm = mm.substr(mm.length - 2, 2);
var ss = "0" + dataOra.getSeconds();
ss = ss.substr(ss.length - 2, 2);

return g + "/" + m + "/" + a + " " + hh + "." + mm + "." + ss;
}

function arrayAdd(arr, el)
{
if (el == "") return;

var i;
for (i = 0; i < arr.length; i++)
if (arr[i] == el) return;

arr.push(el);
}

function CorreggeNomeLega(nomeLega)
{
return nomeLega.replace(/[\*"'\\\/:\|\?]/g, "_");
}