// Helper function
function forEach(array, f)
{
	for(var i = 0; i < array.length; i++)
	{
		f(array[i]);
	}
}

var DIRECTION_COLUMN = 5;
var ESTIMATED_COLUMN = 6;

// Called on form load
function init()
{
	makeSelect("airlines", 1, "All Airlines");
	makeInput("flight", 2);
	makeSelect("cities", 3, "All Cities");
//	makeRadios("day", 4, "0");
//	makeRadios("direction", DIRECTION_COLUMN, "A");
	filter2(document.getElementById("flight"));
}

// Initialise radio buttons
function makeRadios(radioName, dataColumn, defaultId)
{
	var isOneSelected = false;

	forEach(document.getElementsByName(radioName), function(input)
	{
		var cookie = getCookie(dataColumn + "/" + input.id);
		
		input.onclick = filter;
		input.dataColumn = dataColumn;
		
		if (cookie != null)
		{
			if (cookie.toUpperCase() == "TRUE")
			{
				input.checked = true;
				isOneSelected = true;
				filter2(input);
			}
		}
	});

	if (!isOneSelected)
	{
		var input = document.getElementById(defaultId);

		input.checked = true;
		filter2(input);
	}
}

// Initialise check boxes
function makeCheckboxes(checkboxName, dataColumn)
{
	forEach(document.getElementsByName(checkboxName), function(input)
	{
		var cookie = getCookie(dataColumn + "/" + input.id);
		
		input.onclick = filter;
		input.dataColumn = dataColumn;
		
		if (cookie != null)
		{
			input.checked = (cookie.toUpperCase() == "TRUE");
			filter2(input);
		}
	});
}

function makeInput(inputId, dataColumn)
{
	var input = document.getElementById(inputId);

	input.onkeyup = filter;
	input.dataColumn = dataColumn;
	
	if (getCookie(dataColumn) != null)
	{
		input.value = getCookie(dataColumn);
		filter2(input);
	}
}

function makeSelect(selectId, dataColumn, selectAll)
{
	var select = document.getElementById(selectId);
	var tbody = document.getElementById("flights");
	var selectionArray = [];
	var selectionSet = {};
	var cookie = getCookie(dataColumn);

	forEach(tbody.getElementsByTagName("tr"), function(tr)
	{
		forEach(tr.getElementsByTagName("td")[dataColumn].getElementsByTagName("div"), function(div)
		{
			var selection = div.innerHTML;
			
			if (!selectionSet[selection])
			{
				selectionSet[selection] = 1;
				selectionArray.push(selection);
			}
		});
	});
	
	selectionArray.sort(function(x, y) {
		var a = String(x).toUpperCase();
		var b = String(y).toUpperCase();
		
		if (a > b) return 1 
		if (a < b) return -1 
		return 0; 
	});
	
	addOption(select, selectAll);
	
	forEach(selectionArray, function(selection)
	{
		addOption(select, selection, cookie);
	});
	
	select.onchange = filter;
	select.dataColumn = dataColumn;

	if (cookie != null)
	{
		filter2(select);
	}
}

function addOption(select, text, cookie)
{
	var option = document.createElement("option");

	option.innerHTML = text;
	select.appendChild(option);

	if (comparable(text) == cookie)
	{
		option.selected = true;
	}
}

function comparable(text)
{
	return text.toUpperCase().replace(/ /g, "").replace(/&NBSP;/g, "");
}

function filter(eventArg)
{
	var ie = !window.Event;
	var control = (ie ? event.srcElement : eventArg.target);

	filter2(control);
}

function filter2(control)
{
	var ie = !window.Event;
	var tbody = document.getElementById("flights");
	var row = 0;
	var tagName = control.tagName.toUpperCase();
	var tagType = control.type.toUpperCase();
	var selected = "";
	var rowCount = 0;
	
	if (tagName == "SELECT")
	{
		selected = comparable(control.options[control.selectedIndex].innerHTML);
		setCookie(control.dataColumn, selected);
	}
	else if (tagType == "CHECKBOX")
	{
		// There can be several checkboxes with the same name
		forEach(document.getElementsByName(control.name), function(input)
		{
			selected += (input.checked ? "[" + input.id + "]" : "");
		});

		setCookie(control.dataColumn + "/" + control.id, control.checked);
	}
	else if (tagType == "RADIO")
	{
		// There can be several radios with the same name
		forEach(document.getElementsByName(control.name), function(input)
		{
			selected += (input.checked ? "[" + input.id + "]" : "");
			setCookie(control.dataColumn + "/" + input.id, input.checked);
		});
	}
	else
	{
		selected = comparable(control.value);
		setCookie(control.dataColumn, selected);
	}

	forEach(tbody.getElementsByTagName("tr"), function(tr)
	{
		var hide = true;
		
		forEach(tr.getElementsByTagName("td")[control.dataColumn].getElementsByTagName("div"), function(div)
		{
			var value = comparable(div.innerHTML);
			
			if (tagName == "SELECT")
			{
				if (value == selected || selected == comparable(control.options[0].innerHTML))
				{
					hide = false;
				}
			}
			else if (tagType == "CHECKBOX" || tagType == "RADIO")
			{
				if (selected.indexOf("[" + value + "]") >= 0)
				{
					hide = false;
				}
			}
			else
			{
				if (value.indexOf(selected) >= 0 || selected == "")
				{
					hide = false;
				}
			}
		});
		
		if (!tr.hidden)
		{
			tr.hidden = {};
		}
		
		tr.hidden[control.dataColumn] = hide;
		
		var properties = 0;
		
		for (var p in tr.hidden)
		{
			properties += (tr.hidden[p] ? 1 : 0);
		}
		
		if (properties > 0)
		{
			tr.style.display = "none";
		}
		else
		{
			row = 1 - row;
			rowCount++;
			tr.style.display = (ie ? "block" : "table-row");

			forEach(tr.getElementsByTagName("td"), function(td)
			{
				td.className = "tr" + tr.getElementsByTagName("td")[DIRECTION_COLUMN].getElementsByTagName("div")[0].innerHTML + row;
			});
		}
	});
	
	document.getElementById("no-flights").style.display = (rowCount > 0 ? "none" : (ie ? "block" : "table-row"));
	
	// Callback to HTML author to set labels accordingly
	stateChanged();
}

// Set a cookie
function setCookie(name, value)
{
	document.cookie = name + "=" + value + "; path=/";
}

// Get a cookie
function getCookie(name)
{
	var nameEquals = name + "=";
	var cookies = document.cookie.split(";");

	for (var i = 0; i < cookies.length; i++)
	{
		var cookie = cookies[i];
		
		while (cookie.charAt(0) == " ")
		{
			cookie = cookie.substring(1, cookie.length);
		}
		
		if (cookie.indexOf(nameEquals) == 0)
		{
			return cookie.substring(nameEquals.length, cookie.length);
		}
	}
	
	return null;
}

