// An autosuggest textbox control.

// Known bugs: Safari fires onkeydown twice.
// Known bugs: Safari doesn't change div layer background color on mouseover.

// To Do: Relevance Count

	function ax_suggest(oTextbox, oProvider)
		{
		this.cur = -1 // The currently selected suggestions.
		this.layer = null // The dropdown list layer.
		this.provider = oProvider // Suggestion Provider.
		this.textbox = oTextbox
		this.currentTimeout = null
		this.init()
		}

	ax_suggest.prototype.autosuggest = function (aSuggestions, bTypeAhead)
		{
		if (this.currentTimeout)
			clearTimeout(this.currentTimeout)

		if (aSuggestions.length > 0)
			{
			this.showSuggestions(aSuggestions)

			if (bTypeAhead)
				{
			//	this.typeAhead(aSuggestions[0])
				var rFunctRef = this.callLaterTypeAhead (this, aSuggestions[0])
				this.currentTimeout = setTimeout (rFunctRef, 1000)
				}
			}
		else
			this.hideSuggestions()
		}

	ax_suggest.prototype.callLaterTypeAhead = function (oThis, suggestion)
		{
		return (function(){oThis.typeAhead(suggestion)})
		}

	ax_suggest.prototype.createDropDown = function ()
		{
		var txtkeys

		var oThis = this

		this.layer = document.createElement("div")
		this.layer.className = "suggestions"
		this.layer.style.visibility = "hidden"
		this.layer.style.width = (this.textbox.offsetWidth - 2) + "px"

		this.layer.onmousedown =
		this.layer.onmouseup =

		this.layer.onmouseover = function (oEvent)
			{
			oEvent = oEvent || window.event
			oTarget = oEvent.target || oEvent.srcElement

			if (oEvent.type == "mousedown")
				{
			// The user clicked the mouse. Populate textbox and submit the form.
			//	oThis.textbox.value = oTarget.firstChild.nodeValue

			//	oThis.textbox.value = oTarget.firstChild.nodeValue
			//	oThis.textbox.value = oTarget.innerHTML.replace('<STRONG>','').replace('</STRONG>','').replace('<strong>','').replace('</strong>','')

				txtkeys = oTarget.innerHTML.replace('&amp;','&').replace('&nbsp;','')

				if (txtkeys.indexOf("<DIV") == -1 && txtkeys.indexOf("<div") == -1)
					{
					oThis.textbox.value = txtkeys
					oThis.hideSuggestions()
					oThis.SubmitMyParentForm()
					}
				}
			else if (oEvent.type == "mouseover")
				oThis.highlightSuggestion(oTarget)
			else
				oThis.textbox.focus()
			}

		this.layer.style.display = "none" // Added Code: Hide the layer so it does not show in the bottom of the page.
		document.body.appendChild(this.layer)
		}

	ax_suggest.prototype.SubmitMyParentForm = function ()
		{
		var obj = this.textbox

		// The user either hit the Enter key or clicked the mouse in an option, so launch the form to do the keyword search.

		while (obj.nodeName != 'FORM' && obj.nodeName != 'form' && obj)
			obj = obj.parentNode

		if (obj) obj.submit()
		}

	ax_suggest.prototype.getLeft = function ()
		{
		// Return in units of pixels (without the "px") the x coordinate of the top left corner of the dropdowns with respect to the top left corner of the browser window.

		var obj = this.textbox

		var curleft = 0
		if (obj.offsetParent)
			{
			while (obj.offsetParent)
				{
				curleft += obj.offsetLeft
				obj = obj.offsetParent
				}
			}
		else if (obj.x)
			curleft += obj.x

		return curleft
		}

	ax_suggest.prototype.getTop = function ()
		{
		// Return in units of pixels (without the "px") the y coordinate of the top left corner of the dropdowns with respect to the top left corner of the browser window.

		var obj = this.textbox

		var curtop = 0
		if (obj.offsetParent)
			{
			while (obj.offsetParent)
				{
				curtop += obj.offsetTop
				obj = obj.offsetParent
				}
			}
		else if (obj.y)
			curtop += obj.y

		return curtop
		}

	ax_suggest.prototype.handleKeyDown = function (oEvent)
		{
		if (this.currentTimeout)
			clearTimeout(this.currentTimeout)

		this.layer.style.display = "block" // Added Code: Unhide the layer that was set to display:none in createDropDown().

		switch(oEvent.keyCode)
			{
			case 38: // up arrow
				this.previousSuggestion()
				break
			case 40: // down arrow
				this.nextSuggestion()
				break
			case 13: // enter
				this.hideSuggestions()
				break
			}
		}

	ax_suggest.prototype.handleKeyUp = function (oEvent)
		{
		if (this.currentTimeout)
			clearTimeout(this.currentTimeout)

		var iKeyCode = oEvent.keyCode

		// For backspace (8) and delete (46), shows suggestions without typeahead.
		if (iKeyCode == 8 || iKeyCode == 46)
			{
			// Make sure not to interfere with non-character keys.
			this.provider.requestSuggestions(this, false)
			}
		else if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode < 46) || (iKeyCode >= 112 && iKeyCode <= 123))
			{
			// Ignore
			}
		else
			this.provider.requestSuggestions(this, true)
		}

	ax_suggest.prototype.hideSuggestions = function ()
		{
		this.layer.style.visibility = "hidden"
		}

	ax_suggest.prototype.highlightSuggestion = function (oSuggestionNode)
		{
		var i

		for (i=0; i < this.layer.childNodes.length; i++)
			{
			var oNode = this.layer.childNodes[i]
			if (oNode == oSuggestionNode)
				{
				oNode.className = "current"
				}
			else if (oNode.className == "current")
				oNode.className = ""
			}
		}

	ax_suggest.prototype.init = function ()
		{
		var oThis = this

		this.textbox.onkeyup = function (oEvent)
			{
			if (!oEvent) oEvent = window.event
			oThis.handleKeyUp(oEvent)
			}

		this.textbox.onkeydown = function (oEvent)
			{
			if (!oEvent) oEvent = window.event
			oThis.handleKeyDown(oEvent)
			}

		this.textbox.onblur = function ()
			{
			oThis.hideSuggestions()
			}

		window.onresize = function ()
			{
			oThis.handleWindowResize()
			}

		this.createDropDown()
		}

	ax_suggest.prototype.nextSuggestion = function ()
		{
		var cSuggestionNodes = this.layer.childNodes

		if (cSuggestionNodes.length > 0 && this.cur < cSuggestionNodes.length-1)
			{
		// The user hit a down arrow key to another option, thus highlighting it.
			var oNode = cSuggestionNodes[++this.cur]
			this.highlightSuggestion(oNode)
		//	this.textbox.value = oNode.firstChild.nodeValue
		//	this.textbox.value = oNode.innerHTML.replace('<STRONG>','').replace('</STRONG>','').replace('<strong>','').replace('</strong>','')
			this.textbox.value = oNode.innerHTML.replace('&amp;','&').replace('&nbsp;','')
			}
		}

	ax_suggest.prototype.previousSuggestion = function ()
		{
		var cSuggestionNodes = this.layer.childNodes

		if (cSuggestionNodes.length > 0 && this.cur > 0)
			{
		// The user hit an up arrow key to another option, thus highlighting it.
			var oNode = cSuggestionNodes[--this.cur]
			this.highlightSuggestion(oNode)
		// this.textbox.value = oNode.firstChild.nodeValue
		//	this.textbox.value = oNode.innerHTML.replace('<STRONG>','').replace('</STRONG>','').replace('<strong>','').replace('</strong>','')
			this.textbox.value = oNode.innerHTML.replace('&amp;','&').replace('&nbsp;','')
			}
		}

	ax_suggest.prototype.selectRange = function (iStart, iLength)
		{
	// The textbox is being populated with the text in the first dropdown option
	// just after the user pressed a key in the textbox. The funcion typeAhead
	// does that and then calls this function.

	// This function highlights the text in the textbox with the remaining letters.
	// For example: Suppose the user types "ca" and "cargo" is the first option.
	// "CARGO" would appear in the textbox with "RGO" being highlighted.

		if (this.textbox.createTextRange)
			{
			// IE
			var oRange = this.textbox.createTextRange()
			oRange.moveStart("character", iStart)
			oRange.moveEnd("character", iLength - this.textbox.value.length)
			oRange.select()
			}
		else if (this.textbox.setSelectionRange)
			{
			// Mozilla
			this.textbox.setSelectionRange(iStart, iLength)
			}
		this.textbox.focus()
		}

	ax_suggest.prototype.showSuggestions = function (aSuggestions)
		{
		var oDiv = null
		this.layer.innerHTML = ""
		var iTextBoxLen = this.textbox.value.replace(/\s*$/g,"").length

		// The user entered text in the textbox that created options in the dropdown.
		// In the following loop, populate the dropdown options.

		for (var i=0; i < aSuggestions.length; i++)
			{
			oDiv = document.createElement("div")
			oDiv.style.width = (this.textbox.offsetWidth - 2) + "px"

		//	oDiv.appendChild(document.createTextNode(aSuggestions[i]))

		// In the statement below:
		// aSuggestions[i].substr(0, iTextBoxLen) contains the characters the user typed, such as "can". The first letter becomes uppercase resulting with "Can".
		// aSuggestions[i].substr(iTextBoxLen) contains the characters appended, such as "ada" resulting in "Canada" as the entire word.

		// The Word Prompter was originally written so that the text the user typed is in bold in the dropdown.
		// The problem is that when the mouse is over the bold text, the remaining text is not recognized, the highlight doesn't show, and on mouseclick only the bold text is launched for the search.
		//	oDiv.innerHTML = "<STRONG>" + unescape(aSuggestions[i].substr(0, iTextBoxLen)) + "</STRONG>" + unescape(aSuggestions[i].substr(iTextBoxLen))

			oDiv.innerHTML = "&nbsp;" + unescape(aSuggestions[i].substr(0, iTextBoxLen)) + unescape(aSuggestions[i].substr(iTextBoxLen))
			this.layer.appendChild(oDiv)
			}

		this.handleWindowResize()
		this.layer.style.visibility = "visible"
		this.handleWindowResize()
		}

//	ax_suggest.prototype.handleWindowResize = function ()
//		{
//		// This original function does not handle Safari properly. In Safari, the pulldown menu is a few pixels
//		// under the input text box instead of being attached to it.
//		// The function is replaced with the one below it.
//		var safari = 0
//		if (navigator.appVersion.indexOf("Safari") != -1) safari = 11
//		this.layer.style.left = this.getLeft() + "px"
//		this.layer.style.top = (this.getTop()+this.textbox.offsetHeight) + safari + "px"
//		}

	ax_suggest.prototype.handleWindowResize = function ()
		{
		var safari = 0
		var thetop

	// The user entered text in the textbox. Move the dropdown layer to be in the
	// correct position just in case the browser window was resized.

		if (navigator.userAgent.indexOf("Safari") != -1) safari = 0

		this.layer.style.left = this.getLeft() + "px"
		thetop = (this.getTop() + this.textbox.offsetHeight) + safari

		try {thetop = thetop - document.getElementById("ResultsContainer").scrollTop}
		catch(er) {}

		this.layer.style.top = thetop + "px"
		}

	ax_suggest.prototype.typeAhead = function (sSuggestion)
		{
		if (this.textbox.createTextRange || this.textbox.setSelectionRange) //	Check for support of typeahead functionality.
			{
		// The textbox is being populated with the text in the first dropdown option
		// just after the user pressed a key in the textbox.
			var iLen = this.textbox.value.length

			this.textbox.value = unescape(sSuggestion)
			this.selectRange(iLen, sSuggestion.length)
			}
		}

