/*  */

var autoCompleteList = new Array();
var AutoCompleteDataRequested = 0;
var objInputBox;
var selectedItem = 0;

function AutoComplete_GetLeft(element)
{
    var curNode = element;
    var left    = 0;

    do {
        if (curNode.offsetLeft > 900) break;
        left += curNode.offsetLeft;
        curNode = curNode.offsetParent;
        if (!curNode) break;
    } while(curNode.className != 'col1');

    return left;
}


/**
* Gets top coord of given element
*
* @param object element The element to get the top coord for
*/
function AutoComplete_GetTop(element)
{
    var curNode = element;
    var top    = 0;

    do {

        if (curNode.offsetTop > 900) break;
        top += curNode.offsetTop;
        curNode = curNode.offsetParent;
        if (!curNode) break;

    } while(curNode.id != 'col1wrap');

    return top + 25;
}

function autoCompleteSetText (evt, text, objId)
{
    var div = document.getElementById(objId);
    if (div)
    {
        div.value = text;
        clearAutoComplete();
    }
}

function showAutoComplete (inputBox)
{
    var div = document.getElementById("divAutoComplete");
    if (div)
    {
        if (div.style.display == "none")
        {
            var inputText = inputBox.value;
            var left = AutoComplete_GetLeft(inputBox);
            var top = AutoComplete_GetTop(inputBox);
            var i = 0;
            var oTbl;
            var oRow;
            var oCell;
            var compareLen = inputText.length;
            var listCount = 0;
            var Compare;

            /* populate list */
            div.innerHTML = "";
            oTbl = document.createElement("Table");
            oTbl.id = "tblAutoComplete";
            oTbl.width = "210px";
            oTbl.className = "tbl_auto";

            for (i = 0; i < autoCompleteList.length; i++)
            {
                if (listCount > 10) break;  // too many items in the list
//                alert(autoCompleteList[i].substring(0,compareLen) + " - " + compareLen + " - " + inputText);
                Compare = autoCompleteList[i].substring(0,compareLen);
                if (Compare.toLowerCase() != inputText.toLowerCase()) continue;
                listCount++;
                oRow = oTbl.insertRow(-1);
                oCell = document.createElement('td');
                oCell.innerHTML = autoCompleteList[i];
                if (listCount == 0) oCell.className = "ac_sel";
                else oCell.className = "light";
                oRow.onclick = new Function ("evt", "autoCompleteSetText(evt, \"" + autoCompleteList[i] + "\", \"" + inputBox.id + "\");");
                oRow.style.cursor = "pointer";
                oRow.appendChild(oCell);
            }
            div.appendChild(oTbl);
            div.style.display = "block";
            /* set the position of the div to underneath the textbox we are auto completing on (inputBox) */
            div.style.left = left + "px";
            div.style.top = top + "px";
        }
    }
}

function cb_autoCompleteList (List)
{
    var inputList = eval("(" + List + ")");
    var i = 0;
//    alert(inputList.length);
    autoCompleteList.length = 0; // make sure the list is cleared first
    for (i = 0; i < inputList.length; i++)
    {
        autoCompleteList[i] = inputList[i];
    }
    showAutoComplete(objInputBox);
}

function clearAutoComplete ()
{
    var div = document.getElementById("divAutoComplete");
    if (div)
    {
        div.style.display = "none";
    }
    autoCompleteList.length = 0;
    AutoCompleteDataRequested = 0;
}

function autoComplete (e, inputBox)
{
    var text = inputBox.value;
    var textLen = text.length;
    var oTbl;
    var oRow;
    var oCell;
    var Compare;
    var compareLen = 0;
    var listCount = 0;
    
    if (textLen > 2)
    {
        if (AutoCompleteDataRequested == 0)
        {
            objInputBox = inputBox;
            AutoCompleteDataRequested = 1;
            x_get_auto_complete(text, cb_autoCompleteList);
        }
        else
        {
            // go through the table and remove any items that don't match the new text..
            oTbl = document.getElementById("tblAutoComplete");
            if (oTbl)
            {
                for(i = oTbl.rows.length - 1; i > -1; i--)
                {
                    oTbl.deleteRow(i);
                }
                compareLen = textLen;
                selectedItem = 0;
                for (i = 0; i < autoCompleteList.length; i++)
                {
                    if (listCount > 10) break;  // too many items in the list
                    Compare = autoCompleteList[i].substring(0,compareLen);
                    if (Compare.toLowerCase() != text.toLowerCase()) continue;
                    oRow = oTbl.insertRow(-1);
                    oCell = document.createElement('td');
                    if (listCount == 0) oCell.className = "ac_sel";
                    else oCell.className = "light";
                    oCell.innerHTML = autoCompleteList[i];
                    oRow.onclick = new Function ("evt", "autoCompleteSetText(evt, \"" + autoCompleteList[i] + "\", \"" + inputBox.id + "\");");
                    oRow.style.cursor = "pointer";
                    oRow.appendChild(oCell);
                    listCount++;
                }
            }
        }
    }
    else
    {
        clearAutoComplete();
    }
}