﻿//
// AutoCompletion.js
//
// by Vito Plantamura (http://vitoplantamura.com/), VPC Technologies (http://www.vpctechnologies.com/).
//

function AutoCompletion_getSelection (n, def)
{
    for( i=0; i<10; i ++ )
    {
        var tr = document.getElementById( n + "tr" + i );
        if ( tr == null )
            return def;
        else if ( tr.style.color == "#ffffff" )
            return i;
    }
    
    return def;
}

function AutoCompletion_putSelection (n, s)
{
    for( i=0; i<10; i ++ )
    {
        var tr = document.getElementById( n + "tr" + i );
        if ( tr == null )
            return;
        
        if ( s == i )
        {
            tr.style.backgroundColor = "#0040D0";
            tr.style.color = "#ffffff";
        }
        else
        {
            tr.style.backgroundColor = "#ffffff";
            tr.style.color = "#000000";
        }
    }
}

function AutoCompletion_getNum (n)
{
    var i;
    for( i=0; i<10; i ++ )
    {
        var tr = document.getElementById( n + "tr" + i );
        if ( tr == null )
            return i;
    }
    return i;
}

function AutoCompletion_processResponse (n)
{
    var req = document.getElementById( n + "div" ).req;
    if ( req.readyState == 4 && req.status == 200 )
    {
        document.getElementById( n + "div" ).style.display = "block";
        document.getElementById( n + "div" ).innerHTML = req.responseText;
    }
}

function AutoCompletion_setEditText (n)
{
    var sel = AutoCompletion_getSelection (n);
    if ( sel == null )
        return;

    document.getElementById( n ).value = document.getElementById( n + "span" + sel ).innerHTML;
    document.getElementById( n + "div" ).style.display = "none";
    document.getElementById( n ).focus ();
}

function AutoCompletion_onEditBoxKey (n, key)
{
    if ( key == 27 ) // esc.
    {
        document.getElementById( n + "div" ).style.display = "none";
        event.returnValue = false;
        return;
    }    

    if ( key == 38 ) // up.
    {
        var s = AutoCompletion_getSelection (n, 0) - 1;
        if ( s < 0 )
            s = AutoCompletion_getNum(n) - 1;
        AutoCompletion_putSelection( n, s );
        event.returnValue = false;
    }
    else if ( key == 40 ) // down.
    {
        var s = AutoCompletion_getSelection (n, AutoCompletion_getNum(n)-1) + 1;
        if ( s >= AutoCompletion_getNum (n) )
            s = 0;
        AutoCompletion_putSelection( n, s );
        event.returnValue = false;
    }
    else if ( key == 13 ) // enter.
    {
        AutoCompletion_setEditText (n);
        event.returnValue = false;
    }
}

function encodeHtml ( s )
{
     s = escape(s);
     s = s.replace(/\//g,"%2F");
     s = s.replace(/\?/g,"%3F");
     s = s.replace(/=/g,"%3D");
     s = s.replace(/&/g,"%26");
     s = s.replace(/@/g,"%40");
     return s;
} 

function AutoCompletion_onEditBoxKeyChange (base, n, key)
{
    if ( key == 8 || (key >= 32 && key != 38 && key != 40) )
    {
        var url = base + "?str=" + encodeHtml( document.getElementById( n ).value ) + "&rnd=" + Math.floor(Math.random()*1000000000) + "&n=" + n;
    
        var req = new ActiveXObject("Microsoft.XMLHTTP");
        document.getElementById( n + "div" ).req = req;
        req.onreadystatechange = new Function ( "AutoCompletion_processResponse('" + n + "');" );
        req.open ( "GET", url, true );
        req.send ();
    }
}
