﻿var sQueryID = "";
var iHighlightDiv = -1;
var sFullName = "";

function filldiv(strText, strSpanID)
{
    var data = "";
    iHighlightDiv = -1;
    if(document.getElementById(strSpanID))
    {
        if(navigator.appName == "Microsoft Internet Explorer")// tested for IE7
        {
            document.getElementById(strSpanID).style.top = "715px";
            document.getElementById(strSpanID).style.left = ((document.body.clientWidth / 2) - 160) + "px";
        }
        else// tested for FF3
        {
            document.getElementById(strSpanID).style.top = "717px";
            document.getElementById(strSpanID).style.left = ((document.body.clientWidth / 2) - 145) + "px";
        }
        if(strText.length > 1)
        {
            var xhr;
            if (window.XMLHttpRequest)
            { // Mozilla, Safari, ...
                xhr = new XMLHttpRequest();
                if (xhr.overrideMimeType)
                {
                    xhr.overrideMimeType('text/xml');
                }
            }
            else if (window.ActiveXObject)
            { // IE
                try
                {
                    //xhr = new ActiveXObject("Msxml2.XMLHTTP"); // Does not retrieve XML file
                    xhr = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e)
                {
                    
                }
            }
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4) {
                    if (xhr.status == 200) {
                        var doc = xhr.responseXML;
                        var element = doc.getElementsByTagName('innerhtml');
                        if (element != null) {
                            for (i = 0; i < 20; i++) {
                                if (element.item(i) != null) {
                                    if (element.item(i).firstChild != null) {
                                        data += element.item(i).firstChild.data;
                                    }
                                }

                            }
                            document.getElementById(strSpanID).innerHTML = data;
                            togglediv(sQueryID, strSpanID);
                        }
                        else {
                            // normal failure
                            status = "Invalid XML returned.";
                            document.getElementById(strSpanID).innerHTML = "B";
                        }
                    }
                    else {
                        status = "AJAX error: " + xhr.status;
                        document.getElementById(strSpanID).innerHTML = "C";
                    }
                }

            };
            xhr.open("GET", "/util/asnamematch.aspx?sQ=" + escape(strText),  true);
            xhr.send(null);
        }
        else
        {
            document.getElementById(strSpanID).style.visibility = "hidden";
        }
    }
    else
    {
        status = "Cannot find control : " + strSpanID;
        document.getElementById(strSpanID).innerHTML = "D";
    }
}

function keyhandler(e, queryID, divID)
{
    var keynum;
    if(window.event) //IE
    {
        keynum = e.keyCode;
    }
    else
    {
        keynum = e.which;
    }
    keychar = String.fromCharCode(keynum);
    switch(keynum)
    {
        case 13: // enter
            //return false;
            break;
        case 8: // backspace
        case 37: // left
        case 38: // up
        case 39: // right
        case 40: // down
            return true;
            break;
        default:
            if(keynum != 0)
            {
                filldiv(document.getElementById(queryID).value + keychar,divID);
                //document.getElementById('spanTest').innerHTML = keynum;
            }
            return true;
            break;
    }
}
function arrowhandler(e, queryID, divID)
{
    var iPrevDiv = iHighlightDiv;
    var keynum;
    if(window.event) //IE
    {
        keynum = e.keyCode;
    }
    else
    {
        keynum = e.which;
    }
    switch(keynum)
    {
        case 13: // enter
            //status = sFullName;
            //return false;
            break;
        case 38: // up
            if(iHighlightDiv > 0)
            {
                if(document.getElementById("nearmatchlist"+(iHighlightDiv-1)))
                {
                    iHighlightDiv--;
                    highlight(document.getElementById("nearmatchlist"+iHighlightDiv),iHighlightDiv);
                    document.getElementById(sQueryID).value = document.getElementById("hFullName"+iHighlightDiv).value;
                }
                if(document.getElementById("nearmatchlist"+iPrevDiv))
                {
                    unhighlight(document.getElementById("nearmatchlist"+iPrevDiv));
                }
            }
            break;
        case 40: // down
            if(iHighlightDiv == -1)
            {
                iHighlightDiv++;
                highlight(document.getElementById("nearmatchlist"+iHighlightDiv),iHighlightDiv);
                document.getElementById(sQueryID).value = document.getElementById("hFullName"+iHighlightDiv).value;
            }
            else
            {
                if(document.getElementById("nearmatchlist"+(iHighlightDiv+1)))
                {
                    iHighlightDiv++;
                    highlight(document.getElementById("nearmatchlist"+iHighlightDiv),iHighlightDiv);
                    document.getElementById(sQueryID).value = document.getElementById("hFullName"+iHighlightDiv).value;
                    if(document.getElementById("nearmatchlist"+iPrevDiv))
                    {
                        unhighlight(document.getElementById("nearmatchlist"+iPrevDiv));
                    }
                }
            }
            break;
        case 8: //backspace
            backspacehandler(queryID,divID);
            break;
    }
    //status = iHighlightDiv;
}
function backspacehandler(queryID, divID)
{
    filldiv(document.getElementById(queryID).value.substring(0, document.getElementById(queryID).value.length - 1),divID);
}

function togglediv(queryID, divID)
{
    if(document.getElementById(queryID).value.length < 2 || document.getElementById(divID).innerHTML == "")
    {
        document.getElementById(divID).style.visibility = "hidden";
    }
    else
    {
        document.getElementById(divID).style.visibility = "visible";
    }
}
function hidediv(divID)
{
    document.getElementById(divID).style.visibility = "hidden";
}
function highlight(objDiv, iDivIndex)
{
    objDiv.style.backgroundColor="#cceeff";
    iHighlightDiv = iDivIndex;
}
function unhighlight(objDiv)
{
    objDiv.style.backgroundColor="#FFFFFF";
}
function copyselection(sFullName)
{
    document.getElementById(sQueryID).value = sFullName;
}
