/*
######################################
##		POPULATE COMBOS				##
######################################
*/
function populateFrom(airports) {
	// airports value can be:
	// LIN = only Linate airport
	// MXP = only Malpensa airport
	// MIL = all SEA airports
	// L = airports reached by Linate
	// M = airports reached by Malpensa
	// A = airports reached by Linate or Malpensa
	// R = reset combo (all airports are loaded, including SEA airports)

	index_from = document.aeroporti.from.selectedIndex;
	value_from = document.aeroporti.from.options[index_from].value;

	document.aeroporti.from.disabled = true;
	document.aeroporti.from.options.length = 1;
	n=1;
	if ( (airports != "A") && (airports != "L") && (airports != "M") ) {
		for (i=0; i<seaAirports.length; i++) {
			if ((seaAirports[i].cod_iata == "MIL") || (seaAirports[i].cod_iata == airports) || (airports == "R") || (airports == "MIL")) {
				document.aeroporti.from.options[n] = new Option (seaAirports[i].descr_long,seaAirports[i].cod_iata);
				document.aeroporti.from.options[n].style.background="#A3BBCC";
				if ((document.aeroporti.from.options[n].value == value_from) && (airports != "R"))
					document.aeroporti.from.selectedIndex = n;
				n++;
			}
		}
	}
	if ( (airports != "MIL") && (airports != "LIN") && (airports != "MXP") ) {
		for (i=0; i<remoteAirports.length; i++) {
			if ((remoteAirports[i].scali_sea == "A") || (remoteAirports[i].scali_sea == airports) || (airports == "R") || (airports == "A")) {
				document.aeroporti.from.options[n] = new Option (remoteAirports[i].descr_long,remoteAirports[i].cod_iata);
				if ((document.aeroporti.from.options[n].value == value_from) && (airports != "R"))
					document.aeroporti.from.selectedIndex = n;
				n++;
			}
		}
	}
	document.aeroporti.from.disabled = false;
}

function populateTo(airports) {
	// airports value can be:
	// LIN = only Linate airport
	// MXP = only Malpensa airport
	// MIL = all SEA airports
	// L = airports reached by Linate
	// M = airports reached by Malpensa
	// A = airports reached by Linate and Malpensa
	// R = reset combo (all airports are loaded, including SEA airports)

	n=1;

	index_to = document.aeroporti.to.selectedIndex;
	value_to = document.aeroporti.to.options[index_to].value;

	document.aeroporti.to.disabled = true;	
	document.aeroporti.to.options.length = 1;
	
	if ( (airports != "A") && (airports != "L") && (airports != "M") ) {
		for (i=0; i<seaAirports.length; i++) {
			if ((seaAirports[i].cod_iata == "MIL") || (seaAirports[i].cod_iata == airports) || (airports == "R") || (airports == "MIL")) {
				document.aeroporti.to.options[n] = new Option (seaAirports[i].descr_long,seaAirports[i].cod_iata);
				document.aeroporti.to.options[n].style.background="#A3BBCC";
				if ((document.aeroporti.to.options[n].value == value_to) && (airports != "R"))
					document.aeroporti.to.selectedIndex = n;
				n++;
			}
		}
	}

	if ( (airports != "MIL") && (airports != "LIN") && (airports != "MXP") ) {
		for (i=0; i<remoteAirports.length; i++) {
			if ((remoteAirports[i].scali_sea == "A") || (remoteAirports[i].scali_sea == airports) || (airports == "R") || (airports == "A")) {
				document.aeroporti.to.options[n] = new Option (remoteAirports[i].descr_long,remoteAirports[i].cod_iata);
				if ((document.aeroporti.to.options[n].value == value_to) && (airports != "R"))
					document.aeroporti.to.selectedIndex = n;
				n++;
			}
		}
	}
	
	document.aeroporti.to.disabled = false;

}



function populateAirline(airports) {

	// airports value can be:
	// L = airlines operating on Linate
	// M = airlines operating on Malpensa
	// A = airlines operating on Linate and Malpensa
	// R = reset combo (all airlines are loaded)  -- in practice R=A

	n=1;

	document.aeroporti.airline.disabled = true;
	
	document.aeroporti.airline.options.length = 1;

	for (i=0; i<seaAirlines.length; i++) {
		if ((seaAirlines[i].scali_sea == "A") || (seaAirlines[i].scali_sea == airports) || (airports == "R") || (airports == "A")) {
			document.aeroporti.airline.options[n] = new Option (seaAirlines[i].airline_name,seaAirlines[i].cod_iata);
			n++;
		}
	}
	
	document.aeroporti.airline.disabled = false;
	


}







/*
######################################
##			SET INPUTS				##
######################################
*/

function setFrom(code) {
	document.aeroporti.from.disabled = true;
	for (i=1; i<document.aeroporti.from.options.length; i++) {
		if (code == document.aeroporti.from.options[i].value)
			document.aeroporti.from.selectedIndex = i;
	}
	changeFrom();
	document.aeroporti.from.disabled = false;
}


function setTo(code) {
	document.aeroporti.to.disabled = true;
	for (i=1; i<document.aeroporti.to.options.length; i++) {
		if (code == document.aeroporti.to.options[i].value)
			document.aeroporti.to.selectedIndex = i;
	}
	changeTo();
	document.aeroporti.to.disabled = false;
}


function setAirline(code) {
	document.aeroporti.airline.disabled = true;
	for (i=0; i<document.aeroporti.airline.options.length; i++) {
		if (code == document.aeroporti.airline.options[i].value)
			document.aeroporti.airline.selectedIndex = i;
	}
	changeAirline();
	document.aeroporti.airline.disabled = false;
}


function setDay(date) {
	document.aeroporti.day.disabled = true;
	document.aeroporti.day.value = date;
	document.aeroporti.day.disabled = false;
}


function setTimeInterval(interval) {
	document.aeroporti.time_interval.disabled = true;
	for (i=0; i<document.aeroporti.time_interval.options.length; i++) {
		if (interval == document.aeroporti.time_interval.options[i].value)
			document.aeroporti.time_interval.selectedIndex = i;
	}
	document.aeroporti.time_interval.disabled = false;
}

/*
######################################
##		COMBO CHANGE EVENT			##
######################################
*/
function changeFrom() {
	index_from = document.aeroporti.from.selectedIndex;
	value_from = document.aeroporti.from.options[index_from].value;
	index_to = document.aeroporti.to.selectedIndex;
	value_to = document.aeroporti.to.options[index_to].value;
	document.aeroporti.name_from.value = document.aeroporti.from.options[index_from].text;
	scali_sea = "";
	if (value_from == "" && value_to=="") {
		populateFrom("R");
		populateTo("R");
	}
	else if (value_from != "")
		{
		if ( (value_from == "MIL") || (value_from == "LIN") || (value_from == "MXP") ) {
			if (value_from == "MIL") {
				populateTo("A");
				populateAirline("A");
			}
			if (value_from == "LIN") {
				populateTo("L");
				populateAirline("L");
			}
			if (value_from == "MXP") {
				populateTo("M");
				populateAirline("M");
			}
		document.aeroporti.location.value = value_from;
		}
		else if (value_to=='' || ( (value_to != "MIL") && (value_to != "LIN") && (value_to != "MXP") )) {
			for (i=0; i<remoteAirports.length; i++) {
				if (remoteAirports[i].cod_iata == value_from)
					scali_sea = remoteAirports[i].scali_sea;
			}
			if (scali_sea == "A") {
				populateTo("MIL");
				for (j=0; j<document.aeroporti.to.options.length; j++)
					{
					if (document.aeroporti.to.options[j].value=="MIL")
						{
						document.aeroporti.to.selectedIndex=j;
						document.aeroporti.name_to.value = document.aeroporti.to.options[j].text;
						break;
						}
					}
			}
			if (scali_sea == "L") {
				populateTo("LIN");
				for (j=0; j<document.aeroporti.to.options.length; j++)
					{
					if (document.aeroporti.to.options[j].value=="LIN")
						{
						document.aeroporti.to.selectedIndex=j;
						document.aeroporti.name_to.value = document.aeroporti.to.options[j].text;
						break;
						}
					}
			}
			if (scali_sea == "M") {
				populateTo("MXP");
				for (j=0; j<document.aeroporti.to.options.length; j++)
					{
					if (document.aeroporti.to.options[j].value=="MXP")
						{
						document.aeroporti.to.selectedIndex=j;
						document.aeroporti.name_to.value = document.aeroporti.to.options[j].text;
						break;
						}
					}
			}
		}
	}
}

function changeTo() {
	index_to = document.aeroporti.to.selectedIndex;
	value_to = document.aeroporti.to.options[index_to].value;
	index_from = document.aeroporti.from.selectedIndex;
	value_from = document.aeroporti.from.options[index_from].value;
	document.aeroporti.name_to.value = document.aeroporti.to.options[index_to].text;
	scali_sea = "";
	if (value_to == "" && value_from == "") {
		populateFrom("R");
		populateTo("R");
	}
	else if (value_to != "") {
		if ( (value_to == "MIL") || (value_to == "LIN") || (value_to == "MXP") ) {
			if (value_to == "MIL") {
				populateFrom("A");
				populateAirline("A");
			}
			if (value_to == "LIN") {
				populateFrom("L");
				populateAirline("L");
			}
			if (value_to == "MXP") {
				populateFrom("M");
				populateAirline("M");
			}
		document.aeroporti.location.value = value_from;	
		}
		else if (value_from=='' || ( (value_from != "MIL") && (value_from != "LIN") && (value_from != "MXP") )) {
			for (i=0; i<remoteAirports.length; i++) {
				if (remoteAirports[i].cod_iata == value_to)
					scali_sea = remoteAirports[i].scali_sea;
			}
			if (scali_sea == "A") {
				populateFrom("MIL");
				for (j=0; j<document.aeroporti.from.options.length; j++)
					{
					if (document.aeroporti.from.options[j].value=="MIL")
						{
						document.aeroporti.from.selectedIndex=j;
						document.aeroporti.name_from.value = document.aeroporti.from.options[j].text
						break;
						}
					}
			}
			if (scali_sea == "L") {
				populateFrom("LIN");
				for (j=0; j<document.aeroporti.from.options.length; j++)
					{
					if (document.aeroporti.from.options[j].value=="LIN")
						{
						document.aeroporti.from.selectedIndex=j;
						document.aeroporti.name_from.value = document.aeroporti.from.options[j].text
						break;
						}
					}
			}
			if (scali_sea == "M") {
				populateFrom("MXP");
				for (j=0; j<document.aeroporti.from.options.length; j++)
					{
					if (document.aeroporti.from.options[j].value=="MXP")
						{
						document.aeroporti.from.selectedIndex=j;
						document.aeroporti.name_from.value = document.aeroporti.from.options[j].text
						break;
						}
					}
			}
		}
	}
}

function changeAirline() {
	index_airline = document.aeroporti.airline.selectedIndex;
	document.aeroporti.name_airline.value = document.aeroporti.airline.options[index_airline].text;
}

/*
######################################
##			DATE CHECKING			##
######################################
*/

function valid_date(year,month,day) {
	if (
		(day>31) ||
		((day>30) && ((month==4) || (month==6) || (month==9) || (month==11))) ||
		((day>29) && (month==2)) ||
		((day>28) && (month==2) && ((year % 4) != 0))
	)
		return false;
	else
		return true;
}

/*
######################################
##			FORM CHECKING			##
######################################
*/
function check_flights_search(form) {
	if ((form.from.value == "") && (form.to.value == "")) {
		alert(user_message["AIRPORTS_NOT_SELECTED"]);
		return false;
	}
	else {
		if ((form.from.value == "") && (form.to.value != "MIL") && (form.to.value != "LIN") && (form.to.value != "MXP")) {
			setFrom("MIL");
		}
		if ((form.to.value == "") && (form.from.value != "MIL") && (form.from.value != "LIN") && (form.from.value != "MXP")) {
			setTo("MIL");
		}
	}
	user_date = document.aeroporti.day.value.split("/");
	user_day = parseInt(user_date[0], 10);
	user_month = parseInt(user_date[1], 10);
	user_year = parseInt(user_date[2], 10);
	if (!valid_date(user_year,user_month,user_day)) {
		alert(user_message["DATE_INVALID"]);
		return false;
	}
	user_date = new Date(user_year,user_month-1,user_day);
	user_date_ms = user_date.getTime();
	today = new Date();
	today_ms = today.getTime();
	upper_limit_ms = today_ms + (365 * 86400 * 1000); // 1 year after
	lower_limit_ms = today_ms - (2 * 86400 * 1000); // 2 days before
	// N.B.: this means if user date is 1 (not 2!!) day before
	if (user_date_ms < lower_limit_ms) {
		alert(user_message["DATE_TOO_LOW"]);
		return false;
	}
	if (user_date_ms > upper_limit_ms) {
		alert(user_message["DATE_TOO_HIGH"]);
		return false;
	}
	return true;
}

function check_seasonal_flights_search(form) {
	if (form.from.value == "") {
		alert(user_message["SEARCH_FROM_NOT_SELECTED"]);
		return false;
	}
	if (form.to.value == "") {
		alert(user_message["SEARCH_TO_NOT_SELECTED"]);
		return false;
	}
	return true;
}

function populate_onload()
	{
	if (typeof(document.aeroporti)!='undefined')
		{
		index_to = document.aeroporti.to.selectedIndex;
		value_to = document.aeroporti.to.options[index_to].value;
		index_from = document.aeroporti.from.selectedIndex;
		value_from = document.aeroporti.from.options[index_from].value;
		index_airline = document.aeroporti.airline.selectedIndex;
		value_airline = document.aeroporti.airline.options[index_airline].value;
		if (value_from=='MIL') {populateTo('A');populateAirline('A');}
		else if(value_from=='LIN') {populateTo('L');populateAirline('L');}
		else if (value_from=='MXP') {populateTo('M');populateAirline('M');}
		if (value_to=='MIL') {populateFrom('A');populateAirline('A');}
		else if(value_to=='LIN') {populateFrom('L');populateAirline('L');}
		else if (value_to=='MXP') {populateFrom('M');populateAirline('M');}
		if (value_airline!='') 
			{
			for (i=0; i<document.aeroporti.airline.options.length; i++) 
				{
				if (value_airline == document.aeroporti.airline.options[i].value)
					{
					document.aeroporti.airline.selectedIndex = i; break;
					}
				}
			}
		}
	}

addOnload(populate_onload);

