// -------- immette il codice aeroporto in un form digitando nel form la cittā----------
// --- Iniziato 31 Marzo 2008 Mauro Carlo ----

function immetti_aeroporto(Input, e)
{	
	var trova=-1;
	var i=0;
	var numero = "";
	var indice =0;
	var aerop = "";
	var trasfaerop = "";
	var Riquadro = document.getElementById("Riquadro");
	var Lista = document.getElementById("Lista");
	var evento = "";
	var codascii ="";
	var coloreCorrente = "";
	var cittaUno = document.getElementById("Cittauno");	//per posizionare correttamente il riquando in caso di dest. multiple
	var cittaDue = document.getElementById("Cittadue");
	if(document.getElementById("Cittatre")) {
		var cittaTre = document.getElementById("Cittatre");
		var cittaQuattro = document.getElementById("Cittaquattro");
		var cittaCinque = document.getElementById("Cittacinque");
		var cittaSei = document.getElementById("Cittasei");
		var cittaSette = document.getElementById("Cittasette");
		var cittaOtto = document.getElementById("Cittaotto");
	}
	
	Input.style.backgroundColor = "#ffff00";	// Individuiamo la casella di testo su cui l'utente sta lavorando
	if(!(document.getElementById("Cittatre"))) {	// elimina le incompatibiltā nei browser sulla codifica dei colori	
		if(cittaUno.style.backgroundColor.charAt(0) == "r") {
			coloreCorrente = "rgb(255, 255, 0)";
		}
		else {
			coloreCorrente = "#ffff00";
		}
	} else {
		if(cittaUno.style.backgroundColor.charAt(0) == "r" ||
			cittaDue.style.backgroundColor.charAt(0) == "r" ||
			cittaTre.style.backgroundColor.charAt(0) == "r" ||
			cittaQuattro.style.backgroundColor.charAt(0) == "r" ||
			cittaCinque.style.backgroundColor.charAt(0) == "r" ||
			cittaSei.style.backgroundColor.charAt(0) == "r" ||
			cittaSette.style.backgroundColor.charAt(0) == "r" ||
			cittaOtto.style.backgroundColor.charAt(0) == "r"
		) {
			coloreCorrente = "rgb(255, 255, 0)";	//ok per IE
		} else {
			coloreCorrente = "#ffff00";	// ok per Mozilla
		}
	}
	Riquadro.style.left = Input.offsetLeft+ "px";
	if(!(document.getElementById("Cittatre"))) {
			Riquadro.style.top = (Input.offsetTop + 22) + "px";
	} else {
		if(cittaUno.style.backgroundColor == coloreCorrente || cittaDue.style.backgroundColor == coloreCorrente) {
			Riquadro.style.top = (Input.offsetTop + 22) + "px";
		} else if(cittaTre.style.backgroundColor == coloreCorrente || cittaQuattro.style.backgroundColor == coloreCorrente) {
			Riquadro.style.top = (Input.offsetTop + 88) + "px";
		} else if(cittaCinque.style.backgroundColor == coloreCorrente || cittaSei.style.backgroundColor == coloreCorrente) {
			Riquadro.style.top = (Input.offsetTop + 154) + "px";
		} else {
			Riquadro.style.top = (Input.offsetTop + 220) + "px";
		}
	}
	
	codascii = e.keyCode;
	
	if (Input.value.length >= 2) {
		
		if (codascii == 8) { // quando cancello l'ultimo carattere lo sottraggo alla stringa
			numero = Input.value.substr(0, Input.value.length - 1).toLowerCase();
		} else if (codascii == 9 || codascii == 40 || codascii == 13 || codascii == 16 || codascii == 38){
			alert("USARE IL MOUSE PER SCORRERE L'ELENCO E SCEGLIERE L'AEROPROTO, NON USARE I TASTI");
			numero = Input.value.substr(0, Input.value.length - 1).toLowerCase();
		} else {
			numero = Input.value.toLowerCase()+ String.fromCharCode(codascii).toLowerCase();	// Legge il testo presente nella casella di input e lo trasforma in caratteri minuscoli
		}
		Lista.options.length = 0;	// Vuota la lista
		if (codascii != 8) {	// escludo il tasto cancella
			for(indice in aeroporti) {	// La riempie con le opzioni desiderate
				trasfaerop = aeroporti[indice][0].toLowerCase();
				trova = trasfaerop.indexOf(numero);
				if(trova!=-1) {
					aerop = aeroporti[indice][0]+" - " + aeroporti[indice][1]+" - "+aeroporti[indice][2]+" - "+indice;
					Lista.options[i] = new Option(aerop, indice);
					i=i+1;
				}
				if (i > 16) {
					Lista.size = 16;
				} else {
					Lista.size = i+1;
				}
				Lista.selectedItem;
				Riquadro.style.display = "block";
			}
		}
	} else {
		NascondiRiquadro();
	}
}

// ---- Fine scelta aeroporto -----

// ---- scelta voce ---------

function SceltaVoce(Lista) {
	var ColoreCorrente = "";
	var CittaCorrente = "";
	var CittaUno = document.getElementById("Cittauno");
	var CittaDue = document.getElementById("Cittadue");
	if(document.getElementById("Cittatre")) {	// per destinazioni multiple
		var CittaTre = document.getElementById("Cittatre");
		var CittaQuattro = document.getElementById("Cittaquattro");
		var CittaCinque = document.getElementById("Cittacinque");
		var CittaSei = document.getElementById("Cittasei");
		var CittaSette = document.getElementById("Cittasette");
		var CittaOtto = document.getElementById("Cittaotto");
	}
	if(!(document.getElementById("Cittatre"))) {	// elimina le incompatibiltā nei browser sulla codifica dei colori	
		if(CittaUno.style.backgroundColor.charAt(0) == "r") {
			ColoreCorrente = "rgb(255, 255, 0)";
		}
		else {
			ColoreCorrente = "#ffff00";
		}
	} else {
		if(CittaUno.style.backgroundColor.charAt(0) == "r" ||
			CittaDue.style.backgroundColor.charAt(0) == "r" ||
			CittaTre.style.backgroundColor.charAt(0) == "r" ||
			CittaQuattro.style.backgroundColor.charAt(0) == "r" ||
			CittaCinque.style.backgroundColor.charAt(0) == "r" ||
			CittaSei.style.backgroundColor.charAt(0) == "r" ||
			CittaSette.style.backgroundColor.charAt(0) == "r" ||
			CittaOtto.style.backgroundColor.charAt(0) == "r"
		) {
			ColoreCorrente = "rgb(255, 255, 0)";	//ok per IE
		} else {
			ColoreCorrente = "#ffff00";	// ok per Mozilla
		}
	}
	if(!(document.getElementById("Cittatre"))) {
		if(CittaUno.style.backgroundColor == ColoreCorrente) {	// Rileva la casella di testo che l'utente sta modificando
				CittaCorrente = CittaUno;
		} else {
				CittaCorrente = CittaDue;
		}
	} else {
		if(CittaUno.style.backgroundColor == ColoreCorrente) {
			CittaCorrente = CittaUno;
		} else if(CittaDue.style.backgroundColor == ColoreCorrente) {
			CittaCorrente = CittaDue;
		} else if(CittaTre.style.backgroundColor == ColoreCorrente) {
			CittaCorrente = CittaTre;
		} else if(CittaQuattro.style.backgroundColor == ColoreCorrente) {
			CittaCorrente = CittaQuattro;
		} else if(CittaCinque.style.backgroundColor == ColoreCorrente) {
			CittaCorrente = CittaCinque;
		} else if(CittaSei.style.backgroundColor == ColoreCorrente) {
			CittaCorrente = CittaSei;
		} else if(CittaSette.style.backgroundColor == ColoreCorrente) {
			CittaCorrente = CittaSette;
		} else {
			CittaCorrente = CittaOtto;
		}
	}
	CittaCorrente.value = Lista.options[Lista.selectedIndex].value; //text	// Imposta quella voce nella casella di input
	NascondiRiquadro();
}

// ---- Fine scelta voce ---------

// ---  Nasconde il riquaro ---------
function NascondiRiquadro() {
	var Riquadro = document.getElementById("Riquadro");
	document.getElementById("Cittauno").style.backgroundColor = "";
	document.getElementById("Cittadue").style.backgroundColor = "";
	if(document.getElementById("Cittatre")) {
		document.getElementById("Cittatre").style.backgroundColor = "";
		document.getElementById("Cittaquattro").style.backgroundColor = "";
		document.getElementById("Cittacinque").style.backgroundColor = "";
		document.getElementById("Cittasei").style.backgroundColor = "";
		document.getElementById("Cittasette").style.backgroundColor = "";
		document.getElementById("Cittaotto").style.backgroundColor = "";
	}
	Riquadro.style.display="none";
}
// ---  Fine Nasconde il riquaro ---------

// ---  Controllo correttezza date inserite e formato triletterale cittā ---------

function contrdatecitta()	{
	var datapart = document.getElementById('sel1').value;
	var datarit  = document.getElementById('sel3').value;
	var DueDest = document.getElementById('Ritorno').checked;
	var UnaDest = document.getElementById('Andata').checked;
	var CittaPart = document.getElementById('Cittauno').value;
	var CittaArr = document.getElementById('Cittadue').value;
	var ok=0;
	var anteriore=0;
	
	if(datapart[6] =="a")	{	// controlla che la data partenza sia inserita correttamente sempre
		alert("Inserire data partenza");
		return false;
	}
	ok=controllaformatodata1(datapart);
	if(ok ==0) {
		return false;
	}
	if(DueDest==true)	{	// se selezionata andata - ritorno controlla formato data ritorno
		if(datarit[6] =="a")	{
			alert("Inserire data di ritorno");
			return false;
		}
		ok=controllaformatodata1(datarit);
		if(ok ==0) {
			return false;
		}
		anteriore = confrontadate(datapart, datarit);	// date consecutive
		if(anteriore==0)	{
			alert("Data ritono anteriore alla data di partenza");
			return false;
		}
	}
	if(CittaPart.length == 0)	{
		alert("Inserire la citta' di partenza");
		return false;
	}
	if(CittaArr.length == 0)	{
		alert("Inserire la citta' di arrivo");
		return false;
	}
	
	if(CittaPart.length!=3 || CittaArr.length!=3)	{
		alert("Scrivere all'intereno dei riquadri citta' partenza e destinazione il nome della citta' o parte del nome della citta'. SCRIVERE IN ITALIANO PER CITTA' ITALIANE ed in INGELSE, o in lingua locale, per CITTA' ESTERE. Selezionare infine la voce che interessa dall'elenco. Apparira' nel riquadro il codice triletterale corrispondente alla scelta effettuata");
		return false;
	}
	
}
// ---  Fine Controllo correttezza date inserite e formato triletterale cittā  ---------

// ------- Controlla che il formato data inserito sia gg/mm/aaaa ------
function controllaformatodata1(data)
{
	var aaaainput=data.substr(6,4);	
	if (aaaainput.length<4) {
		alert ('La data deve essere in formato GG/MM/AAAA');
		return 0;
	}
	var regex = new RegExp("[/-]");
	var date = data.split(regex);
	var nbJours = new Array('',31,28,31,30,31,30,31,31,30,31,30,31);
	
	if ( date['2']%4 == 0 && date['2']%100 > 0 || date['2']%400 == 0 ) {
		nbJours['2'] = 29;
	}
	if( isNaN(date['2']) ) {
		alert ('La data deve essere in formato GG/MM/AAAA');
		return 0;
	}
	if ( isNaN(date['1']) || date['1'] > 12 || date['1'] < 1 ) {
		alert ('La data deve essere in formato GG/MM/AAAA');
		return 0;
	}
	if ( isNaN(date['0']) || date['0'] > nbJours[Math.round(date['1'])] || date['0'] < 1 ) {
		alert ('La data deve essere in formato GG/MM/AAAA');
		return 0;
	}
	return 1; //ok successo
}
// ------- Fine Controlla che il formato data inserito sia gg/mm/aaaa ------

// ------- Confronta consecutivitā di due date ------
function confrontadate(datanascita, datariferimento) //formato date gg/mm/aaaa 1 se datanascita č anteiore a datarif.
{	
	var consecutive =0;
	var ggnasc=datanascita.substr(0,2);
	var mmnasc=datanascita.substr(3,2);
	var aaaanasc=datanascita.substr(6,4);
	var ggrif=datariferimento.substr(0,2);
	var mmrif=datariferimento.substr(3,2);
	var aaaarif=datariferimento.substr(6,4);
	
	if(aaaarif < aaaanasc)	{
		return 0;
	} else if(aaaarif>aaaanasc)	{
		return 1;
	}
	if(mmrif < mmnasc)	{
		return 0;
	} else if(mmrif > mmnasc)	{
		return 1;
	}
	if(ggrif < ggnasc)	{
		return 0;
	}
	return 1;
}
// ------- Fine Confronta consecutivitā di due date ------

