function addOption(select,index,text,value)
{
	var selIndex = select.selectedIndex;
	
	for (i = select.options.length; i > index; i--)
	{
		if (select.options[i] == null)
		{
			select.options[i] = new Option(select.options[i - 1].text, select.options[i - 1].value);
		}
		else
		{
			select.options[i].text = select.options[i - 1].text;
			select.options[i].value = select.options[i - 1].value;
		}
	}
	
	select.options[index] = new Option(text, value);

	if (selIndex > index)
	{
		select.selectedIndex++;
	}
}

function clearOptions(select) {
	for (var i = select.length - 1; i >= 0; i--) select.options[i] = null;
}

function injectAirports(select, firstItem, preSelect, bothDirections) {
	var index = 0;
	if (firstItem != '') {
		index = 1;
		addOption(select, 0, firstItem, '');
	}
	for (var i = 0; i < from.length; i++) {
		if (from[i].length > 2)
			addOption(select, index++, from[i][1], from[i][0]);
		else if (bothDirections)
			if (window['t' + from[i][0]] != null) addOption(select, index++, from[i][1], from[i][0]);
		if (from[i][0] == preSelect) select.selectedIndex = index - 1;
	}
}

function injectTo(to, code, preSelectTo) {
	var toList = eval(code);
	var index = 0;
	var firstItem = to.options[0].text;
	var hasFirstItem = to.options[0].value == '';
	var selectedTo = to.options[to.selectedIndex].value;
	clearOptions(to);
	if (code == '')
		injectAirports(to, firstItem, selectedTo, true);
	else {
		if (hasFirstItem) {
			index = 1;
			addOption(to, 0, firstItem, '');
		}
		for (var i = 3; i < toList.length; i++) {
			addOption(to, index++, eval(eval(toList[i][3])[0])[1], eval(toList[i][3])[0]);
			if (preSelectTo == eval(toList[i][3])[0]) to.selectedIndex = index - 1;
		}
	}
}

function injectFrom(from, code, preSelectFrom) {
	var fromList
	if (window['t' + code] != null)
		fromList = eval('t' + code);
	else
		fromList = eval(code);
	var index = 0;
	var firstItem = from.options[0].text;
	var hasFirstItem = from.options[0].value == '';
	var selectedFrom = from.options[from.selectedIndex].value;
	clearOptions(from);
	if (code == '')
		injectAirports(from, firstItem, selectedFrom, false);
	else {
		if (hasFirstItem) {
			index = 1;
			addOption(from, 0, firstItem, '');
		}
		for (var i = 3; i < fromList.length; i++) {
			addOption(from, index++, eval(eval(fromList[i][3])[0])[1], eval(fromList[i][3])[0]);
			if (preSelectFrom == eval(fromList[i][3])[0]) from.selectedIndex = index - 1;
		}
	}
}
