function tritemio()
{
	/* Legge il testo in chiaro dalla pagina */
	testo_chiaro = document.Tritemio.Chiaro.value;
	
	/* Variabile che conterra' l'eventuale testo cifrato */
	testo_segreto = "";								
	
	/* Leggo l'eventuale parola chiave */
	chiave = document.Tritemio.Chiave.value;
	lung_chiave = chiave.length;
	
	/* Cerco quale Criptosistema sia stato scelto dall'utente... */
	for(i=0;i<document.Tritemio.Criptosistemi.length;i++)
	{
		if (document.Tritemio.Criptosistemi[i].checked=="1")
		{
			criptoscelta = document.Tritemio.Criptosistemi[i].value;	
			if (criptoscelta=="Quar_Cript_I")
				for (j=0;j<document.Tritemio.Quaranta_Criptosistemi_I.length;j++)
					if (document.Tritemio.Quaranta_Criptosistemi_I.options[j].selected == true)
					{
						criptosistema=document.Tritemio.Quaranta_Criptosistemi_I.options[j].text;
						sistema = j+1;
					}
			if (criptoscelta=="Quar_Cript_II")
				for (j=0;j<document.Tritemio.Quaranta_Criptosistemi_II.length;j++)
					if (document.Tritemio.Quaranta_Criptosistemi_II.options[j].selected == true)
					{
						criptosistema=document.Tritemio.Quaranta_Criptosistemi_II.options[j].text;
						sistema = j+21;
					}
			if (criptoscelta=="Altr_Cript")
				for (j=0;j<document.Tritemio.Altri_Criptosistemi.length;j++)
					if (document.Tritemio.Altri_Criptosistemi.options[j].selected == true)
					{
						criptosistema=document.Tritemio.Altri_Criptosistemi.options[j].text;
						sistema = j+41;
					}
		}
	}

	/* Costruisco gli Alfabeti per eventuali traslazioni */
	alf_completo_min = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
	alf_completo_max = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
	alf_tritemio_min = ["a","b","c","d","e","f","g","h","i","k","l","m","n","o","p","q","r","s","t","u","x","z"];
	alf_tritemio_max = ["A","B","C","D","E","F","G","H","I","K","L","M","N","O","P","Q","R","S","T","U","X","Z"];
	alf_num = ["1","2","3","4","5","6","7","8","9","0"];

	/* Guardo che Alfabeto ha scelto l'utente e quindi scelgo tra gli array qui sopra*/
	for(i=0;i<document.Tritemio.Alfabeto.length;i++)
		if (document.Tritemio.Alfabeto[i].checked=="1")
		{
			alfabeto = document.Tritemio.Alfabeto[i].value;
			if (alfabeto=="Alfabeto_Completo")
			{
				alf_min = alf_completo_min;
				alf_max = alf_completo_max;
			}
			if (alfabeto=="Alfabeto_Tritemio")
			{
				alf_min = alf_tritemio_min;
				alf_max = alf_tritemio_max;
			}
		}	
	
	/* Inizio a cercare un messaggio nascosto... */
	inizio_parola=1;
	num_parola=0;
	ind=1; /* Queste tre variabili mi servono per i Criptosistema XV */
	old=0;
	mul=1;
	for (i=0;i<testo_chiaro.length;i++)
	{	
		if (inizio_parola==1)
		{
			if (testo_chiaro.charAt(i)!=" ")
			{
				/* Sta considerando la prima lettera della parola */
				if (sistema==1)
				{
					testo_segreto=testo_segreto.concat(testo_chiaro.charAt(i));
				}
				if (sistema==2)
				{
					if (pari(num_parola)==1)
						testo_segreto=testo_segreto.concat(testo_chiaro.charAt(i));
				}
				if (sistema==3)
				{
					if (pari(num_parola)==0)
						testo_segreto=testo_segreto.concat(testo_chiaro.charAt(i));
				}
				if (sistema==4 || sistema==5)
				{
					if (ter(num_parola)==1)
						testo_segreto=testo_segreto.concat(testo_chiaro.charAt(i));
				}
				if (sistema==6 || sistema==7)
				{
					if (due(num_parola)==1)
						testo_segreto=testo_segreto.concat(testo_chiaro.charAt(i));
				}
				if (sistema==8)
				{
					if (tre(num_parola)==0)
						testo_segreto=testo_segreto.concat(testo_chiaro.charAt(i));
				}
				if (sistema==9)
				{
					if (tre(num_parola)==1)
						testo_segreto=testo_segreto.concat(testo_chiaro.charAt(i));
				}
				if (sistema==10 || sistema==11)
				{
					if (quattro(num_parola)==1)
						testo_segreto=testo_segreto.concat(testo_chiaro.charAt(i));
				}
				if (sistema==12)
				{
					if (cinque(num_parola)==1)
						testo_segreto=testo_segreto.concat(testo_chiaro.charAt(i));
				}
				if (sistema==13)
				{
					if (chiave=="") 
					{
						alert("In questo Criptosistema devi inserire una chiave!");
						break;
					}
					else if (key(num_parola)==1)
						testo_segreto=testo_segreto.concat(testo_chiaro.charAt(i));
				}
				if (sistema==14)
				{
					if (num_parola>=2)
						testo_segreto=testo_segreto.concat(testo_chiaro.charAt(i));
				}
				if (sistema==15)
				{
					if (cresce(num_parola)==0)
						testo_segreto=testo_segreto.concat(testo_chiaro.charAt(i));
				}
				if (sistema==16)
				{
					if (cresce(num_parola)==1)
						testo_segreto=testo_segreto.concat(testo_chiaro.charAt(i));
				}
				if (sistema==17)
				{
					testo_segreto = testo_segreto.concat(transpose(testo_chiaro.charAt(i),-1));
				}
				if (sistema==18)
				{
					if (pari(num_parola)==1)
						testo_segreto = testo_segreto.concat(transpose(testo_chiaro.charAt(i),-1));
				}
				if (sistema==19)
				{
					if (ter(num_parola)==1)
						testo_segreto = testo_segreto.concat(transpose(testo_chiaro.charAt(i),-1));
				}
				if (sistema==20)
				{
					if (due(num_parola)==1)
						testo_segreto = testo_segreto.concat(transpose(testo_chiaro.charAt(i),-1));
				}
				if (sistema==21)
				{
					if (pari(num_parola)==0)
						testo_segreto = testo_segreto.concat(transpose(testo_chiaro.charAt(i),-2));
				}
				if (sistema==22)
				{
					if (due(num_parola)==1)
						testo_segreto = testo_segreto.concat(transpose(testo_chiaro.charAt(i),-3));
				}
				if (sistema==23)
				{
					if (pari(num_parola)==0)
						testo_segreto = testo_segreto.concat(transpose(testo_chiaro.charAt(i),-4));
				}
				if (sistema==24)
				{
					testo_segreto = testo_segreto.concat(transpose(testo_chiaro.charAt(i),-5));
				}
				if (sistema==25)
				{
					testo_segreto = testo_segreto.concat(transpose(testo_chiaro.charAt(i),-6));
				}
				if (sistema>=26 && sistema<=40)
				{
					if (pari(num_parola)==0)
						testo_segreto = testo_segreto.concat(transpose(testo_chiaro.charAt(i),-(sistema-19)));
				}
				if (sistema==41)
				{
					testo_segreto = testo_segreto.concat(transpose(testo_chiaro.charAt(i),1));
				}
				if (sistema==42)
				{
					if (pari(num_parola)==0)
						testo_segreto = testo_segreto.concat(transpose(testo_chiaro.charAt(i),2));
				}
				if (sistema>=43 && sistema<=46)
				{
					testo_segreto = testo_segreto.concat(transpose(testo_chiaro.charAt(i),(sistema-40)));
				}
				if (sistema>=47 && sistema<=50)
				{
					testo_segreto = testo_segreto.concat(transpose(testo_chiaro.charAt(i),(sistema-39)));
				}
				num_parola++;
			}			
			inizio_parola=0;
		}
		if (testo_chiaro.charAt(i)==" ")
			inizio_parola=1;
	}
	
	/* Scrivo finalmente il testo cifrato finale!!! */
	document.Tritemio.Segreto.value = testo_segreto;
	pari(2,3);
}

function pari(x)
{
	if (x%2==0)
		return 1;
	else
		return 0;
}

function ter(x)
{
	if (x%3>0)
		return 1;
	else
		return 0;
}

function due(x)
{
	if (x%4>1)
		return 1;
	else
		return 0;
}

function tre(x)
{
	if (x%6>2)
		return 1;
	else
		return 0;
}

function quattro(x)
{
	if (x%8>3)
		return 1;
	else
		return 0;
}

function cinque(x)
{
	if (x%10>4)
		return 1;
	else
		return 0;
}

function key(x)
{
	tot = 5 + lung_chiave;
	if (x%tot>4)
		return 1;
	else
		return 0;
}

function cresce(x)
{
	if (x-old==2*ind)
	{
		old = old + 2*ind;
		ind+=mul;
	}
	
	if (ind==0 || ind==9)
	{
		mul*=-1;
		ind+=mul;
	}
	
	if ((x-old)%(2*ind)>=ind)
		return 1;
	else
		return 0;
}

function transpose(lettera,numero)
{
	res = check_letter(lettera);
	switch(res)
	{
		case 0: /* E' una lettera maiuscola */
			for (t=0;t<alf_max.length;t++)
				if (lettera==alf_max[t])
					ret = alf_max[(t+numero+4*alf_max.length)%(alf_max.length)];
			break;
		case 1:	/* E' una lettera minuscola */
			for (t=0;t<alf_min.length;t++)
				if (lettera==alf_min[t])
					ret = alf_min[(t+numero+4*alf_min.length)%(alf_min.length)];
			break;
		case 2: /* E' un numero */ 
			for (t=0;t<alf_num.length;t++)
				if (lettera==alf_num[t])
					ret = alf_num[(t+numero+4*alf_num.length)%(alf_num.length)];
			break;
		case 3: /* E' un altro carattere... */
			ret = lettera;
			break;
	}
	return ret;
}

function check_letter(lettera)
{
	if (lettera.charCodeAt(0)>=65 && lettera.charCodeAt(0)<=90)
		return 0;
	else if (lettera.charCodeAt(0)>=97 && lettera.charCodeAt(0)<=122)
		return 1;
	else if (lettera.charCodeAt(0)>=48 && lettera.charCodeAt(0)<=57)
		return 2;
	else
		return 3;
}
