retrievedUrls=new Array()
sgss=new Array()
var docSelection = '';
function getSugByInputId(iid) {
	var so=null
	var i=0
	while(so==null && i<sgss.length) {
		if (iid==sgss[i].ied) so=sgss[i]
		i++
	}
	return so 
}
function getUrlByInputId(iid) {
	var so=null
	var i=0
	while(so==null && i<sgss.length) {
		docSelection = retrievedUrls[iid];
		if (iid==sgss[i].ied) so=sgss[i]
		i++
	}
	return so 
}
function Suggester(ied,feeder) {
	this.ied=ied 
	this.req=null
	this.feeder=feeder
	this.numberOfSuggestions=9
	this.align='right'
	this.dir='rtl'
	this.width=''
	this.divClass=''
	this.selectionClass=''
	this.cSgNm=0
	this.curSel=-1
	this.prevVal=''
	this.sugFcs=0
	sgss.push(this)

}
Suggester.prototype.initReq=function() {
  if(window.ActiveXObject) this.req=new ActiveXObject('Microsoft.XMLHTTP')
	else if(window.XMLHttpRequest) try {
		this.req=new XMLHttpRequest()
	} catch(e) {
		this.req=false
	}	
}
Suggester.prototype.attach=function() {

	var ipt=document.getElementById(this.ied)
	var brObj=document.createElement('BR')
	var posObj=document.createElement('DIV')
	posObj.style.position='absolute';
	if (ipt.nextSibling) {
		ipt.parentNode.insertBefore(posObj,ipt.nextSibling)
		ipt.parentNode.insertBefore(brObj,posObj)
	}
	else {
		ipt.parentNode.appendChild(brObj)
		ipt.parentNode.appendChild(posObj)
	}
	var iptW = ipt.offsetWidth
	if (parseInt(iptW)==0) iptW = ipt.currentStyle.width
	iptW = 163
	var topPos='-1px'
	var leftPos='0px'
	if (this.align=='right') leftPos='-'+iptW+'px'
	var _width=parseInt(iptW)-1
	if (this.width != '') _width = this.width 
  posObj.innerHTML = "<div id='sugbox"+this.ied+"' for='"+this.ied+"' class="+this.divClass+" dir="+this.dir+" style='top:"+topPos+";left:"+leftPos+";width:"+_width+";display:none'>&nbsp;</div>"
  if (!ipt.addEventListener) {
		ipt.attachEvent('onkeyup',this.suggest)
//		ipt.attachEvent('onkeydown',this.suggest)
//		ipt.attachEvent('onkeypress',this.suggest)
		ipt.attachEvent('onblur',this.blur)
		ipt.attachEvent('onfocus',this._focus)
	} else {
		ipt.addEventListener('keyup',this.suggest,false)
		ipt.addEventListener('blur',this.blur,false)
		ipt.addEventListener('focus',this._focus,false)
	}
	ipt.setAttribute('autocomplete','off')
}
Suggester.prototype.selectItem=function(iN) {
	if(iN>=0) {
		if(iN<this.cSgNm) {
			this.getbox().childNodes.item(iN).className=this.selectionClass
			if (this.curSel!=-1) this.getbox().childNodes.item(this.curSel).className=''
			this.curSel=iN
		} else if(this.cSgNm>0) this.selectItem(0)
	}
}
Suggester.prototype.copySel=function() {
	if(this.curSel>-1) {
		var si=this.getbox().childNodes[this.curSel]
		if(si) {
			document.getElementById(this.ied).value=(si.innerText)?si.innerText:si.textContent
			this.resetBox()
		}
	}
}
Suggester.prototype.loadThePage=function(lineNum) {
	if(this.curSel>-1) {
		var si=this.getbox().childNodes[this.curSel]
		if(si) {
			document.getElementById(this.ied).value=(si.innerText)?si.innerText:si.textContent
			docSelection = retrievedUrls[lineNum]
			re = /http:/i;
			if (docSelection.search(re) == -1)
				window.self.location = docSelection;
			else
				window.self.location = docSelection;
		}
	}
}
Suggester.prototype.resetBox=function() {
	this.getbox().innerHTML=''
	this.getbox().style.display='none'
	this.cSgNm=0
	this.curSel=-1
	this.prevVal=''
}
Suggester.prototype.suggest=function(e) {
	var ipt=e.target?e.target:event.srcElement
	var keyp=e.keyCode
	var iptId=ipt.id
	var sugObj=getSugByInputId(iptId)
	if(sugObj) {
		if(keyp==13) {
			// Enter been pressed
			var box=sugObj.getbox()
			var iptId=box.getAttribute('for')	
			var urlObj=getUrlByInputId(iptId)
			if(urlObj)
				urlObj.loadThePage(sugObj.curSel)
			if(sugObj) sugObj.copySel()
		} else if(keyp==40) {
			// DownArrow been pressed
			sugObj.selectItem(sugObj.curSel+1)
		} else if(keyp==38) {
			// UpArrow been pressed
			sugObj.selectItem(sugObj.curSel-1)
		} else if(keyp==27) {
			// Esc been pressed
			sugObj.resetBox()
		} else {
			if(ipt.value.length==0) { 
				sugObj.resetBox()
			} else {	//	if(ipt.value!=sugObj.prevVal && sugObj) {
				sugObj.prevVal=ipt.value
				sugObj.getFeed(ipt.value)
				sugObj.sugFcs=0
				sugObj.selectItem(sugObj.curSel)
			}
		}
	}
}
Suggester.prototype.blur=function(e) {
	var ipt=e.target?e.target:event.srcElement
	var sugObj=getSugByInputId(ipt.id)
	if(sugObj && sugObj.sugFcs==0) sugObj.resetBox()
}
Suggester.prototype._focus=function(e) {
	var ipt=e.target?e.target:event.srcElement
	var sugObj=getSugByInputId(ipt.id)
	if(sugObj) {
		sugObj.getFeed(ipt.value)
		sugObj.sugFcs=0
	}
}
Suggester.prototype.getFeed=function(search) {
   if (this.req == null) {
    this.initReq()
		if (this.req) {
	    var rn=Math.round(Math.random()*1000000+1)
	    this.req.onreadystatechange=this.callback
	    this.req.open('GET', this.feeder+'?search='+encodeURIComponent(search)+'&num='+this.numberOfSuggestions+'&RN='+rn)
	    this.req.send(null)
		} 
	}
}
Suggester.prototype.getbox=function() {
	return document.getElementById('sugbox'+this.ied)
}
Suggester.prototype.OMV=function(e) {
	var line=e.target?e.target:event.srcElement
	while(line && line.name!='line') line=line.parentNode
	if(line) {
		var lineNo=line.getAttribute('lineno')
		var box=line.parentNode
		var iptId=box.getAttribute('for')	
		var sugObj=getSugByInputId(iptId)
		if(sugObj) {
			sugObj.sugFcs = 1
			sugObj.selectItem(lineNo)		
		}
	}
}
Suggester.prototype.OMO=function(e) {
	var line=e.target?e.target:event.srcElement
	while(line && line.name!='line') line=line.parentNode
	if(line) {
		var lineNo=line.getAttribute('lineno')
		var box=line.parentNode
		var iptId=box.getAttribute('for')	
		var sugObj=getSugByInputId(iptId)
		if(sugObj) sugObj.sugFcs=0			
	}
}
Suggester.prototype.OC=function(e) {
	var line=e.target?e.target:event.srcElement
	while(line && line.name!='line') {
		line = line.parentNode
	}
	if(line) {
		var lineNo=line.getAttribute('lineno')
		var box=line.parentNode
		var iptId=box.getAttribute('for')	
		var sugObj=getSugByInputId(iptId)
		var urlObj=getUrlByInputId(iptId)
		if(urlObj)
			urlObj.loadThePage(lineNo)
		if(sugObj) sugObj.copySel()
	}
}
Suggester.prototype.callback=function() {
	var k=0;
	while(k<sgss.length) {
		var curSug=sgss[k]
	  if(curSug.req && curSug.req.readyState==4 && curSug.req.status==200) {
			var sugs=curSug.req.responseXML.getElementsByTagName('sug')
			var urls=curSug.req.responseXML.getElementsByTagName('theURL')
			curSug.getbox().innerHTML=''
			curSug.getbox().style.display=(sugs.length>0)?'block':'none'
			retrievedUrls = null;
			retrievedUrls = new Array (sugs.length)
			for(var i=0;i<sugs.length;i++) {
				var sugData=sugs[i].childNodes.item(0).data
				var theUrlData=urls[i].childNodes.item(0).data
				retrievedUrls[i] = theUrlData;
				var uNum=document.getElementById(curSug.ied).value.length
				var uDat='<strong>'+sugs[i].childNodes.item(0).data.substr(0,uNum)+'</strong>'
				var rDat=sugs[i].childNodes.item(0).data.substr(uNum,sugData.length)
				var nc=document.createElement('DIV')
				nc.style.cursor='pointer'
				nc.style.padding='2px'
				nc.name='line'
				nc.setAttribute('lineno',i)
			  if(!nc.addEventListener) {
					nc.attachEvent('onmouseover',curSug.OMV)
					nc.attachEvent('onmouseout',curSug.OMO)
					nc.attachEvent('onclick',curSug.OC)
				} else {
					nc.addEventListener('mouseover',curSug.OMV,false)
					nc.addEventListener('mouseout',curSug.OMO,false)
					nc.addEventListener('click',curSug.OC,false)
				}
				nc.innerHTML=uDat+rDat
				curSug.getbox().appendChild(nc)
			}
			curSug.cSgNm=sugs.length
			curSug.curSel=-1
			if(sugs.length>0) curSug.selectItem(0)
			curSug.req.abort()
			curSug.req=null
			break
	  }		
		k++
	}
}

