
function AutoCompleteDB()
{ 
	this.nCount = 0;
	this.aStr = [];
}

AutoCompleteDB.prototype.add = function(str)
{ 
	this.nCount++;
	this.aStr.push(str); 
}
 
String.prototype.trim = function() 
{ return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');}

AutoCompleteDB.prototype.getStrings = function(str1, str2, outStr)
{ 
	for ( var i in this.aStr )
	{
		 if (this.aStr[i].toLowerCase().indexOf(str1.trim().toLowerCase())>=0)
		 outStr.push(this.aStr[i]);
	}  
} 

function AutoComplete(aStr, oText, oDiv, nMaxSize)
{
	// initialize member variables
	this.oText = oText;
	this.oDiv = oDiv;
	this.nMaxSize = nMaxSize;
	
	// preprocess the texts for fast access
	this.db = new AutoCompleteDB();
	var i, n = aStr.length;
	for ( i = 0; i < n; i++ )
	{
		this.db.add(aStr[i]);
	}
			
	// attach handlers to the text-box
	oText.AutoComplete = this;
	oText.onkeyup = onkeyupAC;
	oText.onblur = AutoComplete.prototype.onTextBlur;
}

AutoComplete.prototype.onTextBlur = function()
{
	this.AutoComplete.onblur();
}

AutoComplete.prototype.onblur = function()
{
	this.oDiv.style.visibility = "hidden";
}

AutoComplete.prototype.onTextChange = function()
{
	this.AutoComplete.onchange();
}

AutoComplete.prototype.onDivMouseDown = function()
{
	this.AutoComplete.oText.value = this.innerHTML;
}

AutoComplete.prototype.onDivMouseOver = function()
{
	this.className = "AutoCompleteHighlight";
}

AutoComplete.prototype.onDivMouseOut = function()
{
	this.className = "AutoCompleteBackground";
}

AutoComplete.prototype.onchange = function()
{
	var txt = this.oText.value; 
	
	// clear the popup-div.
	while ( this.oDiv.hasChildNodes() )
		this.oDiv.removeChild(this.oDiv.firstChild);
		
	// get all the matching strings from the AutoCompleteDB
	var aStr = new Array();
	this.db.getStrings(txt, "", aStr);
	
	// add each string to the popup-div
	var i, n = aStr.length;
	for ( i = 0; i < n && i<this.nMaxSize; i++ )
	{
		var oDiv = document.createElement('div');
		this.oDiv.appendChild(oDiv);
		oDiv.innerHTML = aStr[i];
		oDiv.onmousedown = AutoComplete.prototype.onDivMouseDown;
		oDiv.onmouseover = AutoComplete.prototype.onDivMouseOver;
		oDiv.onmouseout = AutoComplete.prototype.onDivMouseOut;
		oDiv.AutoComplete = this;			
	}
	
	if(aStr.length>0)
	{ 
		this.oDiv.style.visibility = "visible";
	}
	else // hide the popup-div
	{
		this.oDiv.innerHTML = "";
		this.oDiv.style.visibility = "hidden";
	}
}


var currAutoCompleteDiv=-1;
var currAutoCompletePressKey=-1;
function onkeyupAC(e)
{ 
	myAutoComplete.onchange();
	currAutoCompletePressKey=(typeof event!='undefined')?window.event.keyCode:e.keyCode;
		 
	if(currAutoCompletePressKey==40)
	{	
		currAutoCompleteDiv++;
		if(myAutoComplete.oDiv.childNodes.length>0 && currAutoCompleteDiv<myAutoComplete.oDiv.childNodes.length)
		{
			 myAutoComplete.oDiv.childNodes[currAutoCompleteDiv].className = "AutoCompleteHighlight";
		}
	}
	else if(currAutoCompletePressKey==38)
	{	
		currAutoCompleteDiv--;
		if(myAutoComplete.oDiv.childNodes.length>0 && currAutoCompleteDiv>-1)
		{
			 myAutoComplete.oDiv.childNodes[currAutoCompleteDiv].className = "AutoCompleteHighlight";
		}
	}
	else if(currAutoCompletePressKey==13)
	{	 
		if(myAutoComplete.oDiv.childNodes.length>0 && currAutoCompleteDiv>-1 && currAutoCompleteDiv<myAutoComplete.oDiv.childNodes.length)
		{ 
			myAutoComplete.oText.value = myAutoComplete.oDiv.childNodes[currAutoCompleteDiv].innerHTML; 
		} 
	}
	else currAutoCompleteDiv=-1; 
}