function uctlAutoSuggest(p_oTextBox) {
	this.textbox = p_oTextBox;
	this.suggestionBox = null;
	
	this.init();
	//this.requestSuggestion();
}

uctlAutoSuggest.prototype.requestSuggestion = function(p_sKeyword) {
	var oThis = this;
	//Ajaxified
	var ShowResult = function (p_oXml) 	{		
		if (p_oXml.responseText.length > 0) {
			oThis.suggestionBox.innerHTML = p_oXml.responseText;
			
			for (iCtr = 0; iCtr < oThis.suggestionBox.childNodes.length; iCtr++) {
			/*
				oNode = oThis.suggestionBox.childNodes[iCtr];
				oNode.onmouseover = function () {
					//oNode.className += "_hover";
					oNode.className = "suggestionCell_hover";
					//alert(oNode.className);
				};
				oNode.onmouseout = function() {
					//oNode.className = oNode.className.replace("_hover", "");
					oNode.className = "suggestionCell";
					//alert(oNode.className);
				};
			*/
				oNode = oThis.suggestionBox.childNodes[iCtr];
				oNode.onmousedown = 
				oNode.onmouseup = 
				oNode.onmouseover = function (oEvent) {
					oEvent = oEvent || window.event;
					oTarget = oEvent.target || oEvent.srcElement;
					if (oEvent.type == "mousedown") {
						if (oTarget.parentNode.className == "suggestionCell") {
							//var oEventNode = oTarget.parentNode.firstChild;//;= oTarget.parentNode;
							var oEventNode = null;//;= oTarget.parentNode;
							var iElementIndex= 0;
							for ( var i = 0; i < oTarget.parentNode.childNodes.length; i++) {
								oEventNode = oTarget.parentNode.childNodes[i];//;= oTarget.parentNode;
								if (oEventNode.nodeType != 1) {
									//oEventNode = oEventNode.nextSibling;
								} else {
									//oEventNode = oTarget.parentNode.childNodes[i];
									iElementIndex = i; //element of valid index
								}
							}
							oEventNode = oTarget.parentNode.childNodes[iElementIndex];
							oThis.textbox.value = oEventNode.innerHTML;
						} //testSuggestionCell className

					} else if (oEvent.type == "mouseover") {
						//oThis.highlightSuggestion(oTarget);
						oThis.textbox.focus();
					} else {
						oThis.textbox.focus();
					}
				};	
			
			}
			
			oThis.suggestionBox.style.visibility="visible";
		}
		else { //if its already displayed then hide it.
			oThis.suggestionBox.style.visibility="hidden";
		}
	};

	var sPostStr = "Keyword=" + encodeURIComponent(p_sKeyword);
	oThis.suggestionBox.innerHTML = "<div>Searching...</div>"
	doAJAXCall("getPhoneBook.asp", "POST", sPostStr, ShowResult);		
	//Ajaxified

};
uctlAutoSuggest.prototype.createSuggestionBox = function () {
	var oThis = this;
	this.suggestionBox = document.createElement("div");
	this.suggestionBox.className = "suggestionBox";
	this.suggestionBox.style.position = "absolute";
	this.suggestionBox.style.visibility = "hidden";
	this.suggestionBox.style.width =  "300px";
	/*
	this.suggestionBox.onmousedown = 
    this.suggestionBox.onmouseup = 
    this.suggestionBox.onmouseover = function (oEvent) {
        oEvent = oEvent || window.event;
        oTarget = oEvent.target || oEvent.srcElement;

        if (oEvent.type == "mousedown") {
			oThis.textbox.value = oTarget.parentNode.firstChild.innerHTML;
			oThis.suggestionBox.style.visibility="hidden";
            //oThis.hideSuggestions();
        } else if (oEvent.type == "mouseover") {
            //oThis.highlightSuggestion(oTarget);
            oThis.textbox.focus();
        } else {
            oThis.textbox.focus();
        }
    };	
	
	*/
	oThis.textbox.parentNode.appendChild(this.suggestionBox);
};

uctlAutoSuggest.prototype.init = function () {
	var oThis = this;
		oThis.createSuggestionBox();
	this.textbox.onkeyup = function(oEvent) {
        if (!oEvent) {
            oEvent = window.event;
        }    
		var iKeyCode = oEvent.keyCode;

		//for backspace (8) and delete (46), shows suggestions without typeahead
		if (iKeyCode == 8 || iKeyCode == 46) {
			//this.provider.requestSuggestions(this, false);
			oThis.requestSuggestion(oThis.textbox.value);
			
		//make sure not to interfere with non-character keys
		} else if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode < 46) || (iKeyCode >= 112 && iKeyCode <= 123)) {
			//ignore
		} else {
			oThis.requestSuggestion(oThis.textbox.value);
		}	
	};
	
	this.textbox.onblur = function() {
		if (oThis.suggestionBox != null) {
			oThis.suggestionBox.setfocus;
			oThis.suggestionBox.style.visibility="hidden";
		}
	};	
	
};

