Suggest = Class.create();
Suggest.prototype = {
initialize: function() 
{
this.name  = "";
this.queryField = "";
this.divName = "";
this.ifName = "";
this.lastVal = "";
this.val = ""
this.xmlHttp = null;
this.cache = new Object();
this.searching = false;
this.globalDiv = "";
this.divFormatted = false;
this.cfFunctionName = "";
this.selectionListener  = this.defaultSelectionListener;
this.DIV_BG_COLOR = "#FFFFFF";
this.DIV_HIGHLIGHT_COLOR = "#cccccc";
this.listStyle = "";
this.listItemStyle = "";
this.listItemKey = "";
this.listItemValue = "";
this.listWidth = "";
this.minCharToStartSearch=1;
this.showKey = true;
this.queryFieldHolds = 0;
}
,
setQueryFieldHolds: function(value)
{
this.queryFieldHolds = value;
}
,
setShowKey: function(value)
{
this.showKey = value;
}
,
setSelectionListener: function(handler)
{
this.selectionListener = handler;
}
,
setMinCharToStartSearch: function(value)
{
this.minCharToStartSearch = value;
}
,
setListWidth: function(value)
{
this.listWidth = value;
}
,
setListStyle: function(value)
{
this.listStyle = value;
}
,
setListItemStyle: function(value)
{
this.listItemStyle = value;
}
,
setListItemKey: function(value)
{
this.listItemKey = value;
}
,
setListItemValue: function(value)
{
this.listItemValue = value;
}
,
setHighlightColor: function(value)
{
this.DIV_HIGHLIGHT_COLOR = value;
}
,
setBackgroundColor: function(value)
{
this.DIV_BG_COLOR = value;
}
,
setCfFunctionName: function(value)
{
this.cfFunctionName = value;
}
,
InitQueryCode: function(name, queryFieldName)
{
this.name = name;
this.queryField = document.getElementsByName(queryFieldName).item(0);
this.queryField.onblur = suggestHideDiv;
this.queryField.onkeydown = suggestKeypressHandler;
this.queryField.autocomplete = "off";
this.divName = name + 'div';
this.ifName = "queryiframe";
this.addToCache("", new Array(), new Array());
setTimeout("suggestMainLoop()", 100);
}
,
addToCache: function(queryString, resultArray1, resultArray2)
{
this.cache[queryString] = new Array(resultArray1, resultArray2);
}
,
getDiv: function(divID)
{
if (!this.globalDiv) 
{
if (!document.getElementById(divID)) {
var newNode = document.createElement("div");
if (this.listStyle == "")  
newNode.className = "_listStyle";
else
newNode.className = this.listStyle;
newNode.setAttribute("id", divID);
document.body.appendChild(newNode);
}
this.globalDiv = document.getElementById(divID);
var x = this.queryField.offsetLeft;
var y = this.queryField.offsetTop + this.queryField.offsetHeight;
var parent = this.queryField;
while (parent.offsetParent) {
parent = parent.offsetParent;
x += parent.offsetLeft;
y += parent.offsetTop;
}
if (!this.divFormatted) 
{
this.globalDiv.style.backgroundColor = this.DIV_BG_COLOR;
if (this.listStyle == "")
{
this.globalDiv.style.fontFamily = "Verdana, Geneva, Arial, Helvetica, sans-serif";
this.globalDiv.style.fontSize = "90%";
this.globalDiv.style.padding = "4px";
this.globalDiv.style.border = "1px solid black";
}
this.globalDiv.style.position = "absolute";
this.globalDiv.style.left = x + "px";
this.globalDiv.style.top = y + "px";
this.globalDiv.style.visibility = "hidden";
this.globalDiv.style.zIndex = 10000;
if (this.listWidth != "") this.globalDiv.style.width = this.listWidth;
this.divFormatted = true;
}
}
return this.globalDiv;
}
,
showQueryDiv: function(queryString, resultArray1, resultArray2)
{
var div = this.getDiv(this.divName);
while (div.childNodes.length > 0)
div.removeChild(div.childNodes[0]);
for (var i = 0; i < resultArray1.length; i++)
{
var result = document.createElement("div");
if (this.listItemStyle != "") 
{
result.className = this.listItemStyle;
}
else
{
result.style.cursor = "pointer";
result.style.borderBottom = "2px solid #777777";
result.style.padding = "3px 0px 3px 0px"
}

this._unhighlightResult(result);
result.onmousedown = this.selectResult;
result.onmouseover = this.highlightResult;
result.onmouseout = this.unhighlightResult;
var result1 = document.createElement("span");
if (this.listItemKey == "")
result1.className = "_listItemKey";
else
result1.className = this.listItemKey;
if (this.listItemKey == "")
{
result1.style.textAlign = "left";
result1.style.fontWeight = "bold";
}
result1.innerHTML = resultArray1[i];
var result2 = document.createElement("span");
if (this.listItemValue == "")
result2.className = "_listItemValue";
else
result2.className = this.listItemValue;
if (this.listItemValue == "")
{
result2.style.textAlign = "right";
result2.style.paddingLeft = "20px";
}
result2.innerHTML = resultArray2[i];
var result3 = document.createElement("span");
result3.className = resultArray1[i] + ";" + resultArray2[i];

result.appendChild(result3);
if (this.showKey) result.appendChild(result1);
result.appendChild(result2);
div.appendChild(result);
}
var isCached = this.cache[queryString];
if (!isCached)
this.addToCache(queryString, resultArray1, resultArray2);
this.showDiv(resultArray1.length > 0);
}
,
selectResult: function()
{
selectedSuggestObject._selectResult(this);
}
,
_selectResult: function(item)
{
var spans = item.getElementsByTagName("span");
if (spans) {
var data = spans[0].className.split(";");
data[0] = data[0].trim();
data[1] = data[1].trim();
if (this.queryFieldHolds == 0) {
this.queryField.value = data[0];
} else if (this.queryFieldHolds == 1) {
this.queryField.value = data[1];
} else if (this.queryFieldHolds == 2) {
this.queryField.value = data[0] + " - " + data[1];
}

this.lastVal = val = escape(this.queryField.value);
this.searching = false;
suggestMainLoop();
this.queryField.focus();
this.showDiv(false);

var _retData = new Array(); 
_retData.KEY = data[0]; 
_retData.VALUE = data[1];					
this.selectionListener(_retData);
return;
}
}
,
highlightResult: function()
{
selectedSuggestObject._highlightResult(this);
}
,
_highlightResult: function(item)
{
item.style.backgroundColor = selectedSuggestObject.DIV_HIGHLIGHT_COLOR;
}
,
unhighlightResult: function()
{
selectedSuggestObject._unhighlightResult(this);
}
,
_unhighlightResult: function(item)
{
item.style.backgroundColor = selectedSuggestObject.DIV_BG_COLOR;
}
,
showDiv: function(show)
{
var div = this.getDiv(this.divName);
if (show)
div.style.visibility = "visible";
else
div.style.visibility = "hidden";
this.adjustiFrame();
}
,
adjustiFrame: function()
{
if (!document.getElementById(this.ifName)) 
{
var newNode = document.createElement("iFrame");
newNode.setAttribute("id", this.ifName);
newNode.setAttribute("src", "javascript:false;");
newNode.setAttribute("scrolling", "no");
newNode.setAttribute("frameborder", "0");
newNode.setAttribute("width", "0");
document.body.appendChild(newNode);
}
iFrameDiv = document.getElementById(this.ifName);
var div = this.getDiv(this.divName);
try {
iFrameDiv.style.position = "absolute";
if (this.listWidth != "") 
this.globalDiv.style.width = this.listWidth;
else
iFrameDiv.style.width = div.offsetWidth;
iFrameDiv.style.height = div.offsetHeight;
iFrameDiv.style.top = div.style.top;
iFrameDiv.style.left = div.style.left;
iFrameDiv.style.zIndex = div.style.zIndex - 1;
iFrameDiv.style.visibility = div.style.visibility;
} catch(e) {
}
}
,
getSelectedSpanNum: function(div)
{
var count = -1;
var spans = div.getElementsByTagName("div");
if (spans) 
{
for (var i = 0; i < spans.length; i++) 
{
count++;
if (spans[i].style.backgroundColor != div.style.backgroundColor)
return count;
}
}
return -1;
}
,
setSelectedSpan: function(div, spanNum)
{
var count = -1;
var thisSpan;
var spans = div.getElementsByTagName("div");
if (spans) 
{
for (var i = 0; i < spans.length; i++) 
{
if (++count == spanNum) 
{
this._highlightResult(spans[i]);
thisSpan = spans[i];
} else {
this._unhighlightResult(spans[i]);
}
}
}
return thisSpan;
}
,
defaultSelectionListener: function(result)
{
}
}

var selectedSuggestObject = null;
function onSuggestFieldFocus(object)
{
selectedSuggestObject = object;
}
function suggestHideDiv()
{
selectedSuggestObject.searching = false;
selectedSuggestObject.showDiv(false);
}
function suggestKeypressHandler(evt)
{
var div = selectedSuggestObject.getDiv(selectedSuggestObject.divName);
if (div.style.visibility == "hidden")
return true;
if(!evt && window.event) 
{
evt = window.event;
}
var key = evt.keyCode;
var KEYUP = 38;
var KEYDOWN = 40;
var KEYENTER = 13;
var KEYTAB = 9;
if ((key != KEYUP) && (key != KEYDOWN) && (key != KEYENTER) && (key != KEYTAB))
return true;
var selNum = selectedSuggestObject.getSelectedSpanNum(div);
var selSpan = selectedSuggestObject.setSelectedSpan(div, selNum);
if ((key == KEYENTER) || (key == KEYTAB)) 
{
if (selSpan)
{
selectedSuggestObject._selectResult(selSpan);
}
evt.cancelBubble=true;
return false;
} else {
if (key == KEYUP)
selSpan = selectedSuggestObject.setSelectedSpan(div, selNum - 1);
if (key == KEYDOWN)
selSpan = selectedSuggestObject.setSelectedSpan(div, selNum + 1);
if (selSpan)
selectedSuggestObject._highlightResult(selSpan);
}
selectedSuggestObject.showDiv(true);
return true;
}
function suggestMainLoop() 
{
val = escape(selectedSuggestObject.queryField.value);
if (val.length >= selectedSuggestObject.minCharToStartSearch)
{
if (selectedSuggestObject.lastVal != val && selectedSuggestObject.searching == false)
{
var cacheResult = selectedSuggestObject.cache[val];
if (cacheResult)
selectedSuggestObject.showQueryDiv(val, cacheResult[0], cacheResult[1]);
else
getData(val, selectedSuggestObject.cfFunctionName);
selectedSuggestObject.lastVal = val;
}
}
setTimeout("suggestMainLoop()", 100);
return true;
}