// Global Varibale to hold last known contents of the text boxes
var arrSearchTerms = new Array()

/// <summary>
/// Initialise Aitport Look Up, so it can all be run by calling one function
/// </summary>
function PerformSearch(strTextBoxName)
{
	// check if the autocomplete already exists
	if (document.getElementById("AutoComplete_" + strTextBoxName) == null)
	{
		// Create Div to be used as Airport Finder
		var tagAirportFinder= document.createElement("div");
		tagAirportFinder.id = "AutoComplete_" + strTextBoxName;
		tagAirportFinder.style.position="absolute";
		tagAirportFinder.style.display = "none";
		tagAirportFinder.style.backgroundColor="white";
		tagAirportFinder.style.border = "1px solid black";

		// Set the events to highlight and hide the box
		document.getElementById(strTextBoxName).onkeyup = keyPress;
		document.getElementById(strTextBoxName).onblur = textBoxBlur;

		// Append the div to teh document
		var DivContent = document.createTextNode("");
		tagAirportFinder.appendChild(DivContent);
		document.getElementById("LookUp_" + strTextBoxName).insertBefore(tagAirportFinder,document.getElementById(strTextBoxName));
		document.getElementById("LookUp_" + strTextBoxName).style.position ="relative"; 
		
			// Also if ie create an IFrame to put under the div to hide drop downs
			if (!document.getElementById("strDropCover_ " + strTextBoxName))
			{
				var elemDropCover = document.createElement("IFRAME");
				elemDropCover.id = "strDropCover_"+ strTextBoxName;
				elemDropCover.style.display = "none";
				elemDropCover.style.position = "absolute";
				elemDropCover.src = "javascript:false";
				elemDropCover.frameBorder="0";
				elemDropCover.scrolling = "no";
				document.getElementById("LookUp_" + strTextBoxName).appendChild(elemDropCover);
			}

		
		// Use Differant Size and positions for moz and ie
		if (isIE()) 
		{
			// IE
			tagAirportFinder.style.top= "21px";
			tagAirportFinder.style.width = "172px";
			
		}
		else
		{
			// moz
			tagAirportFinder.style.left ="0px";
			tagAirportFinder.style.top= "16px";
			tagAirportFinder.style.width = "172px";
		}
		
		tagAirportFinder.style.zIndex = "100";
		document.getElementById("LookUp_" + strTextBoxName).style.width = document.getElementById(strTextBoxName).offsetWidth;
		tagAirportFinder.style.width = document.getElementById(strTextBoxName).offsetWidth;
	}
	else
	{
		var tagAirportFinder = document.getElementById("AutoComplete_" + strTextBoxName);
	}
	
	var searchThing = "PerformSearch('" + strTextBoxName + "')";
	// Set the Search Running
	setTimeout(searchThing, 500);

	var strSearchBox=document.getElementById(strTextBoxName).value;	

	// Check if the Search Box has changed
	if ((strSearchBox != "") && (strSearchBox != document.getElementById(strTextBoxName).defaultValue))
	{
		if ((strSearchBox != arrSearchTerms[strTextBoxName]) || (arrSearchTerms[strTextBoxName]== undefined))
		{
			// Perform Search
			SearchForAirports(strTextBoxName);
	
			// Store the departure Airport for comparison next time
			arrSearchTerms[strTextBoxName]=document.getElementById(strTextBoxName).value;
		}
	}
}

var textBoxBlur = function ()
{
	setTimeout("HideAutoComplete(\""+ this.id + "\")", 500);
}

var keyPress= function (event)
{
	SelectionChange(event,this.id)
}

/// <summary>
/// Hide autocomplete for the airports
/// </summary>
function HideAutoComplete(strSelectAirport)
{
	//  Mark the Span and the iframe and not viewable and reset the index selected to -1
	if (document.getElementById("AutoComplete_" + strSelectAirport))
		document.getElementById("AutoComplete_" + strSelectAirport).style.display="none";

	if (document.getElementById("strDropCover_" + strSelectAirport))
		document.getElementById("strDropCover_" + strSelectAirport).style.display="none";
	intIndex=-1;
}

/// <summary>
/// Display autocomplete for the airports
/// </summary>

// holds the current deslayed information
var arrAirportList;
function DisplayAutoComplete(strTextBoxName, strAirports)
{
	// Reset the Auto Complete Box
	BlankSection(strTextBoxName);

	// Split the Aiport List
	arrAirportList = strAirports.split("|");

	if (arrAirportList.length >1)
	{
		// Loop through the airports
		for (count=0; count<(arrAirportList.length-1); count++)
		{
			//Create Link Element
			var lnkAirports = document.createElement("span");

			lnkAirports.style.display="block";
			lnkAirports.style.width="100%";
			lnkAirports.style.cursor="pointer";
			lnkAirports.style.fontFamily="Arial, Helvetica, sans-serif";
			lnkAirports.style.fontSize="9pt";
			lnkAirports.style.color="black";
			lnkAirports.id = count/2;

			lnkAirports.name=strTextBoxName

			lnkAirports.onmouseover = MouseOverIndex;
			lnkAirports.onmousedown = MouseClickIndex;

			// Increment again so we can get the airport name
			count ++;
			
			// Regular expressions for commas
			var strAirportName  = new String(arrAirportList[count]);
			arrAirportList[count] = strAirportName.replace(",   ,", ",");

			// Add the airport name inside the span
			var lnkAirportName = document.createTextNode(arrAirportList[count]);
			lnkAirports.appendChild(lnkAirportName);
			document.getElementById("AutoComplete_" + strTextBoxName).appendChild(lnkAirports);
		}
	}
	else
	{
			//Create Link Element
			var lnkAirports = document.createElement("span");
			var lnkAirportName = document.createTextNode("No Airports Found");
			lnkAirports.style.width="100%";
			lnkAirports.style.display="block";
			lnkAirports.style.fontFamily="Arial, Helvetica, sans-serif";
			lnkAirports.style.fontSize="8pt";
			lnkAirports.appendChild(lnkAirportName);
			document.getElementById("AutoComplete_" + strTextBoxName).appendChild(lnkAirports);
	}

	document.getElementById("AutoComplete_" + strTextBoxName).style.display ="block";

	// to make it work in IE we have to use a iframe to cover drop downs
	if (document.getElementById("strDropCover_"+ strTextBoxName))
	{
		
		document.getElementById("strDropCover_" + strTextBoxName).style.top = document.getElementById("AutoComplete_" + strTextBoxName).style.top;
		document.getElementById("strDropCover_" + strTextBoxName).style.left ="0px"
		document.getElementById("strDropCover_" + strTextBoxName).style.zIndex = document.getElementById("AutoComplete_" + strTextBoxName).style.zIndex -1;
		document.getElementById("strDropCover_" + strTextBoxName).style.width = document.getElementById("AutoComplete_" + strTextBoxName).offsetWidth;
		document.getElementById("strDropCover_" + strTextBoxName).style.height = document.getElementById("AutoComplete_" + strTextBoxName).offsetHeight;
		document.getElementById("strDropCover_" + strTextBoxName).style.display = document.getElementById("AutoComplete_" + strTextBoxName).style.display;
	}
}

/// <summary>
/// Remove the Child Nodes
/// </summary>
function BlankSection(strTextBoxName)
{
	// Loop through the airports
	if (document.getElementById("AutoComplete_" + strTextBoxName).hasChildNodes())
	{
		var childs = document.getElementById("AutoComplete_" + strTextBoxName);
		var arrLength = childs.childNodes.length;
		for (count=0; count<arrLength; count++)
		{
			try
			{
				// Blank the auto complete
				var obj = childs.firstChild;
				childs.removeChild(obj);
			}
			catch(e)
			{
			}
		}
	}
}

/// <summary>
/// Shortcut to highlight on MouseOver
/// </summary>
var MouseOverIndex = function()
{
	HightLightIndex(this.id, this.name);
}

/// <summary>
/// Shortcut to highlight on MouseOver
/// </summary>
var MouseClickIndex = function()
{
	SelectIndex(this.id, this.name)
}

/// <summary>
/// Select the appropriate from the drop down
/// </summary>
var intIndex=-1;
function SelectionChange(KeyPressed, strTextBoxName)
{
	var key = "";
	if (isIE())
	{
		key  = event.keyCode;
	}
	else
	{
		key= KeyPressed.keyCode;
	}
	
	if (key == 40)
	{
		// Move Selection Down so increment the index
		intIndex++;
		HightLightIndex(intIndex,strTextBoxName);
	}
	else if (key == 38)
	{
		// Move Selection Up so decrement the index
		intIndex--;
		HightLightIndex(intIndex,strTextBoxName);
	}
	else if (key == 13)
	{
		// Selection has been made so select the index
		// When enter is pressed
		SelectIndex(intIndex, strTextBoxName);

		//  Hide the auto complete
		HideAutoComplete(strTextBoxName);
	}
	else if (key == 27)
	{
		//  Hide the auto complete
		HideAutoComplete(strTextBoxName);
	}
	else if (key == 9)
	{
		HideAutoComplete(this.id)
	}
	else
	{
		intIndex = -1;
	}
}

/// <summary>
/// Select the item from the autocomplete
/// </summary>
function SelectIndex(intIndex, strTextBoxName)
{
	// Set the appropriate sections of the page
	if (intIndex>=0)
	{
		if (arrAirportList[(intIndex*2)+1] != null)
		{
			document.getElementById(strTextBoxName).value = arrAirportList[(intIndex*2)+1];
			arrSearchTerms[strTextBoxName] = arrAirportList[(intIndex*2)+1];
		}
	}

	HideAutoComplete(strTextBoxName);
	intIndex = -1;
	lastIndex = -1;
}

/// <summary>
/// Change the index of the auto  complete
/// </summary>
var LastIndex = -1;
function HightLightIndex(intIndex, strTextBoxName)
{
	var autoComplete = document.getElementById("AutoComplete_" + strTextBoxName);
	// Check the index is valid
	if ((LastIndex >=0) && (LastIndex < autoComplete.childNodes.length))
	{
		autoComplete.childNodes[LastIndex].style.backgroundColor="white";
		autoComplete.childNodes[LastIndex].style.color="black";
	}

	if ((intIndex >=0) && (intIndex < autoComplete.childNodes.length))
	{
		// hightlight index
		autoComplete.childNodes[intIndex].style.backgroundColor="#06AFF6";
		autoComplete.childNodes[intIndex].style.color="white";
	}
	LastIndex = intIndex;
}


/// <summary>
/// Search For Available Airport Departures
/// </summary>
var arrTextBoxCache= new Array(); // Holds the cache for each textbox
function SearchForAirports(strTextBoxName)
{
	// Get Cache Array from TextBoxCache
	var arrResultCache = arrTextBoxCache[strTextBoxName];
	
	// If it doesnt exist create new cache
	if (arrResultCache == undefined)
	{
		arrResultCache = new Array();
	}
	
	var strAirportValue = document.getElementById(strTextBoxName).value
	if (arrResultCache[strAirportValue]  == null)
	{
		// Perform the Search For Departures
		var xmlhttp=GetXml()
		xmlhttp.open("GET", "/TC.DLL/Lookup?mode=departure&airportcode=" + strAirportValue,true);
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4)
			{
				var strAirports = xmlhttp.responseText;
				arrResultCache[strAirportValue] = strAirports;
				
				// Return cache to textbox cache 
				arrTextBoxCache[strTextBoxName] = arrResultCache;

				// Fill in the Departures Aiports
				DisplayAutoComplete(strTextBoxName, strAirports);
				xmlhttp = null;
			}
		}

		xmlhttp.send(null);
	}
	else
	{
		DisplayAutoComplete(strTextBoxName, arrResultCache[strAirportValue]);
	}
}

