﻿var sQueryID = "";
var iHighlightDiv = -1;
var sFullName = "";

function filldiv(strText, strSpanID)
{
    iHighlightDiv = -1;
    if(document.getElementById(strSpanID))
    {
        if(navigator.appName == "Microsoft Internet Explorer")// tested for IE7
        {
            document.getElementById(strSpanID).style.top = "700px";
            document.getElementById(strSpanID).style.left = ((document.body.clientWidth / 2) - 150) + "px";
        }
        else// tested for FF3
        {
            document.getElementById(strSpanID).style.top = "640px";
            document.getElementById(strSpanID).style.left = ((document.body.clientWidth / 2) - 240) + "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)
				        {
			                if(element.item(0) != null)
			                {
			                    if(element.item(0).firstChild != null)
			                    {
            				        document.getElementById(strSpanID).innerHTML = element.item(0).firstChild.data;
            				        //document.getElementById(strSpanID).style.visibility = "visible";
            				        togglediv(sQueryID, strSpanID);
            				    }
            				    else
            				    {
            				        document.getElementById(strSpanID).innerHTML = "";
            				        //document.getElementById(strSpanID).style.visibility = "hidden";
            				        togglediv(sQueryID, strSpanID);
            				    }
        		            }
        		            else
        		            {
        				        document.getElementById(strSpanID).innerHTML = "A";
        		            }
    			        }
    			        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;
}