function WDSAutoComplete(iList, iType, iMaxSize)
{
	// initialize member variables
	this.nMaxSize = iMaxSize;
	this.mList = iList;
	this.aFirstLetter = new Array;
	
	// Runtime stuff
	this.LastText = "";
	this.CurrentSelectionID = -1;
	this.LastSelectedDiv = null;
	this.CurrentItemCount = 0;
	this.mIncrement = 1;
			
	if( iType == 'ASSOC' ) { this.mIncrement=2; }

	for( var i=1; i < this.mList.length; i+=this.mIncrement ) {
		var letter = this.mList[i].substring(0,1);
		if ( !this.aFirstLetter[letter] ) this.aFirstLetter[letter] = new Array;
		this.aFirstLetter[letter].push( this.mList[i] );
	}
}

WDSAutoComplete.prototype.attach = function( oText, oDiv, oFrame )
{
	this.oText = oText;
	this.oDiv = oDiv;
	this.oFrame = oFrame;

	// 
	this.LastText = oText.value;
	this.CurrentSelectionID = -1;
	this.LastSelectedDiv = null;
	this.CurrentItemCount = 0;	

	this.oText.onkeydown = WDSAutoComplete.prototype.onKeyDown;

	// attach handlers to the text-box
	oText.WDSAutoComplete = this;
	oText.onkeyup = WDSAutoComplete.prototype.onTextChange;
	oText.onblur = WDSAutoComplete.prototype.onTextBlur;		
}

WDSAutoComplete.prototype.onTextBlur = function()
{
	this.WDSAutoComplete.onblur();
}

WDSAutoComplete.prototype.onblur = function()
{
	this.oDiv.style.visibility = "hidden";
	this.oDiv.style.display = "none";
	this.oFrame.style.display = "none";
}

WDSAutoComplete.prototype.onTextChange = function()
{
	this.WDSAutoComplete.onchange();
}

WDSAutoComplete.prototype.onDivMouseDown = function()
{
	this.WDSAutoComplete.oText.value = this.innerHTML;

	this.WDSAutoComplete.oDiv.innerHTML = "";
	this.WDSAutoComplete.oDiv.style.visibility = "hidden";

	this.WDSAutoComplete.oDiv.style.display = "none";
	this.WDSAutoComplete.oFrame.style.display = "none";		
}

WDSAutoComplete.prototype.onDivMouseOver = function()
{
	if( this.WDSAutoComplete.LastSelectedDiv != null ) {
		this.WDSAutoComplete.LastSelectedDiv.className = "WDSAutoCompleteBackground";
	}	
	this.className = "WDSAutoCompleteHighlight";
	this.WDSAutoComplete.LastSelectedDiv = this;
	
	// Find out what is this div position
	for( var i=0; i < this.WDSAutoComplete.CurrentItemCount; i++ ) {
		if( this.WDSAutoComplete.oDiv.childNodes[i] == this ) {
			this.WDSAutoComplete.CurrentSelectionID = i;
		}
	}
}

WDSAutoComplete.prototype.onDivMouseOut = function()
{
	this.className = "WDSAutoCompleteBackground";
}

WDSAutoComplete.prototype.onKeyDown = function(ev)
{
	// The user pressed a key in the dropdown... check if it is an arrow key.
	var lKeyCode;
	if (ev) { lKeyCode = ev.keyCode; } else if (window.event) { lKeyCode = window.event.keyCode;}
	
	// Do nothing if the Div is not visible 
	if( this.WDSAutoComplete.oDiv.style.visibility != 'visible' ) { return; }
	
	if( lKeyCode == 40 ) {
		this.WDSAutoComplete.CurrentSelectionID++;
		if( this.WDSAutoComplete.CurrentSelectionID >= this.WDSAutoComplete.CurrentItemCount ) {
			this.WDSAutoComplete.CurrentSelectionID = this.WDSAutoComplete.CurrentItemCount-1;
		}
	} else if ( lKeyCode == 38 ) {
		this.WDSAutoComplete.CurrentSelectionID--;
		if( this.WDSAutoComplete.CurrentSelectionID < 0 ) {
			this.WDSAutoComplete.CurrentSelectionID = 0;
		}
	} else if ( (lKeyCode == 9 || lKeyCode == 13) && this.WDSAutoComplete.LastSelectedDiv != null ) {
		this.WDSAutoComplete.oText.value = this.WDSAutoComplete.LastSelectedDiv.innerHTML;
	
		this.WDSAutoComplete.oDiv.innerHTML = "";
		this.WDSAutoComplete.oDiv.style.visibility = "hidden";
		this.WDSAutoComplete.oDiv.style.display = "none";
		this.WDSAutoComplete.oFrame.style.display = "none";

		this.WDSAutoComplete.LastText = this.WDSAutoComplete.oText.value.toUpperCase();
		
		return;
	} else {
		return;
	}

	if( this.WDSAutoComplete.LastSelectedDiv != null ) {
		this.WDSAutoComplete.LastSelectedDiv.className = "WDSAutoCompleteBackground";
	}

	var lDiv = this.WDSAutoComplete.oDiv.childNodes[this.WDSAutoComplete.CurrentSelectionID];
	lDiv.className = "WDSAutoCompleteHighlight";
	
	this.WDSAutoComplete.LastSelectedDiv = lDiv;
}

WDSAutoComplete.prototype.onchange = function()
{
	var txt = this.oText.value.toUpperCase();
	var nCount = 0;

	// If nothing has really changed... Do nothing
	if( this.LastText == txt ) { return; }
	this.LastText = txt;	

// clear the popup-div.
	while ( this.oDiv.hasChildNodes() )
		this.oDiv.removeChild(this.oDiv.firstChild);

	this.LastSelectedDiv = null;
	this.CurrentSelectionID = -1;
	this.CurrentItemCount = 0;

	var letter = txt.substring(0,1);

	var lNmb = txt.length;
	for( var i in this.aFirstLetter[letter] ) {
		if( this.aFirstLetter[letter][i].substr( 0, lNmb ).toUpperCase() == txt ) {
			var oDiv = document.createElement('div');
			this.oDiv.appendChild(oDiv);
			oDiv.innerHTML = this.aFirstLetter[letter][i];
			oDiv.onmousedown = WDSAutoComplete.prototype.onDivMouseDown;
			oDiv.onmouseover = WDSAutoComplete.prototype.onDivMouseOver;
			oDiv.onmouseout = WDSAutoComplete.prototype.onDivMouseOut;			
			oDiv.WDSAutoComplete = this;
			nCount++;
		}
		if( nCount > this.nMaxSize ) break;
	}			

	if( nCount > this.nMaxSize || nCount == 0) // hide the popup-div if there are too many propositions, of if there is less than 2
	{
		this.oDiv.innerHTML = "";
		this.oDiv.style.visibility = "hidden";

    this.oDiv.style.display = "none";
    this.oFrame.style.display = "none";

	} else {
		this.CurrentItemCount = nCount;
		this.oDiv.style.visibility = "visible";
				
    this.oDiv.style.display = "";
    this.oFrame.style.width = this.oDiv.offsetWidth;
    this.oFrame.style.height = this.oDiv.offsetHeight;
    this.oFrame.style.top = this.oDiv.style.top;
    this.oFrame.style.left = this.oDiv.style.left;
    this.oFrame.style.zIndex = this.oDiv.style.zIndex - 1;
    this.oFrame.style.display = "";				
	}
}


WDSAutoComplete.prototype.associate = function(iForm, iOrigin, iTarget)
{
	var Airport = WDSCommon.getTagValue( eval( "document." + iForm + "." + iOrigin) );
	var Code = "";

	for( var i=0;  i < this.mList.length; i += 2 ) {
		if( Airport.toUpperCase() == this.mList[i] ) {
			Code = Airport.toUpperCase();
		}
		if( Airport.toUpperCase() == this.mList[i+1].toUpperCase() ) {
			Code = this.mList[i];
		}
	}	
	WDSCommon.updateTag( eval("document." + iForm), iTarget, Code);	
}