function autoComplete(txtID,words,contentStyle){
	var txt=document.getElementById(txtID);
	var curWord=null;
	var totWords=0;
	var rows;
	txt.onkeyup= (function(o) { return function(e) {o.show_words(e)}})(this);
	//trik per evitare che selectPrvW porti il cursore all'inizo del testo
	txt.onkeydown= (function(o) { return function(e) {return o.getKeys(e)}})(this);
	
	tpos=getPos(txt);
	tw=parseInt(getStyle(txt,"width"));
	var cont=document.createElement('div');

	cont.style.cssText='position:absolute;visibility:hidden;width:'+tw+'px;top:'+(tpos.y+25)+'px;left:'+tpos.x+'px;'+contentStyle;
	
	document.body.appendChild(cont);
	
	function selectRow(cur,next){
		if(rows[cur]!=null){
			rows[cur].style.backgroundColor='white';
			rows[cur].style.color='#000';
		}
			
		rows[next].style.backgroundColor='#cc0000';
		rows[next].style.color="#fff";
	}
	
	function selectPrvWord(){
		if(curWord== null)curWord=1;

		if(curWord==0)return;
		var pindex= curWord-1;
		selectRow(curWord,pindex);
		curWord=pindex;
	}
			
	function selectNextWord(){
		if(curWord== null)curWord=-1;

		if(curWord==(totWords-1))return;

		var pindex= curWord+1;
		
		selectRow(curWord,pindex);
		curWord=pindex;

	}
		
	this.selectWord=function (c){
		selectRow(curWord,c);
		curWord=c;
		txt.focus();		
	}
	
	this.loadWord=function (){
	
		if(rows[curWord]==null)return;
		var w=rows[curWord].childNodes[0].nodeValue;
		
		txt.value=w;
		cont.style.visibility='hidden';
		curWord=null;
	}
	
	
	this.getKeys=function (e){ //trik per evitare che selectPrvW porti il cursore all'inizo del testo
		if(!e)e=window.event; //IE #$@
		if(e.keyCode == 38 || e.keyCode==40 || e.keyCode==13){
			if(e.keyCode == 38) // UP Arrow
				selectPrvWord();
			if(e.keyCode == 40) // DOWN Arrow
				selectNextWord();
			if(e.keyCode == 13){
				if(curWord==null)return true;
				this.loadWord();
				
			}
			return false;
		}
		return true;
	}
	
	
	this.show_words=function (e){
		if(!e)e=window.event; //IE #$@
		
		if(e.keyCode == 38 || e.keyCode==40 || e.keyCode==13){
	
			return;
		}
		var v = txt.value;
		if(v==""){
			cont.style.visibility='hidden';
			return;
		}
		cont.style.visibility='visible';
		var cnt=0;
		cont.innerHTML="";
		rows=new Array();
		for(a=0; a < words.length; a++){
			if(words[a].substr(0,v.length).toLowerCase() == v.toLowerCase()){
				row=document.createElement('div');
				row.appendChild(document.createTextNode(words[a]));
				row.onmouseover=(function(o,cnt) { return function() {o.selectWord(cnt)}})(this,cnt);
				row.onclick=(function(o) { return function() {o.loadWord()}})(this);
				row.style.cssText="cursor:default";
				cont.appendChild(row);
				rows[cnt]=row;
				cnt++;
			}
		}
		if(cnt==0)
			cont.style.visibility='hidden';
		
		totWords=cnt;
	}
	
	function getPos(obj){
	  var curleft = 0;
	  var curtop = 0;

		if (obj.offsetParent){

			while (obj.offsetParent){

				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;

				obj = obj.offsetParent;

			}

		}

		else if (obj.x){

			curleft += obj.x;
			curtop += obj.y;
		}

		return {'x':curleft,'y':curtop};

	}	


	function getStyle(el,styleProp){
		var x = el;//document.getElementById(el);
		if (x.currentStyle)
			var y = x.currentStyle[styleProp];
		else if (window.getComputedStyle)
			var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
		return y;
	}

	
}
