// NEAMB Autocomplete Logic - Hugo
/* start dhtml building blocks */
function isWithinNode(e,i,c,t,obj) 
{
    answer = false;
    te = e;
    while(te && !answer) 
    {
        if    
            ((te.id && (te.id == i)) || (te.className && (te.className == i+"Class"))
            || (!t && c && te.className && (te.className == c))
            || (!t && c && te.className && (te.className.indexOf(c) != -1))
            || (t && te.tagName && (te.tagName.toLowerCase() == t))
            || (obj && (te == obj))) 
            {
                answer = te;
        } 
        else 
        {
            te = te.parentNode;
        }
    }
    return te;
}//isWithinNode
function getEvent(event) 
{
    return (event ? event : window.event);
}//getEvent()
function getEventElement(e) 
{
    return (e.srcElement ? e.srcElement: (e.target ? e.target : e.currentTarget));
}//getEventElement()
function findElementPosX(obj) 
{
    curleft = 0;
    if (obj.offsetParent) 
    {
        while (obj.offsetParent) 
        {
            curleft += obj.offsetLeft;
            obj = obj.offsetParent;
        }
    }//if offsetParent exists
    else if (obj.x)
    {
        curleft += obj.x
    }
    return curleft;
}//findElementPosX
function findElementPosY(obj) 
{
    curtop = 0;
    if (obj.offsetParent) 
    {
        while (obj.offsetParent) 
        {
            curtop += obj.offsetTop;
            obj = obj.offsetParent;
        }
    }//if offsetParent exists
    else if (obj.y)
    {
        curtop += obj.y
    }
    return curtop;
}//findElementPosY
/* end dhtml building blocks */
function handleKeyPress(event) 
{
    e = getEvent(event);
    eL = getEventElement(e);
    upEl = isWithinNode(eL,null,"inputSearch",null,null);
    kc = e["keyCode"];
    if (siw && ((kc == 13) || (kc == 9))) 
    {
        siw.selectingSomething = true;
        if (siw.isSafari) 
        {
            siw.inputBox.blur();   //hack to "wake up" safari
        }
        siw.inputBox.focus();
        siw.inputBox.value = siw.inputBox.value.replace(/[ \r\n\t\f\s]+$/gi,' ');
        hideSmartInputFloater();
    } 
    else if (upEl && (kc != 38) && (kc != 40) && (kc != 37) && (kc != 39) && (kc != 13) && (kc != 27))
    {
        if (!siw || (siw && !siw.selectingSomething)) 
        {
            processSmartInput(upEl);
        }
    } 
    else if (siw && siw.inputBox) 
    {
        siw.inputBox.focus(); //kinda part of the hack.
    }
}//handleKeyPress()
function handleFocus(event) 
{
    e = getEvent(event);
    eL = getEventElement(e);
    if (focEl = isWithinNode(eL,null,"inputSearch",null,null)) 
    {
        if (!siw || (siw && !siw.selectingSomething))
        {
            processSmartInput(focEl);
        }
    }
}//handleFocus()

//hack this function! (HUGO)
function handleBlur(event) 
{
//    e = getEvent(event);
//    eL = getEventElement(e);
//    if (blurEl = isWithinNode(eL,null,"inputSearch",null,null)) 
//    {
//        if (siw && !siw.selectingSomething)
//        {
//            hideSmartInputFloater();
//        }
//    }
}//handleBlur()
function handleClick(event)
{
    hideSmartInputFloater();
}

function showSmartInputFloater() 
{
    if (!siw.floater.style.display || (siw.floater.style.display=="none")) 
    {
        if (!siw.customFloater) 
        {
            x = findElementPosX(siw.inputBox);
            y = findElementPosY(siw.inputBox) + siw.inputBox.offsetHeight;
            //hack: browser-specific adjustments.
            if (!siw.isGecko && !siw.isWinIE)
            { 
                x += 8;
            }
            if (!siw.isGecko && !siw.isWinIE)
            {
                y += 10;
            }
            siw.floater.style.left = 0;
            siw.floater.style.top = y;
        } 
        else 
        {
            //do additional things for the custom floater
            //beyond setting display and visibility
        }
        siw.floater.style.display="block";
        siw.floater.style.visibility="visible";
    }
}//showSmartInputFloater()
function hideSmartInputFloater() 
{
    if (siw) 
    {
        siw.floater.style.display="none";
        siw.floater.style.visibility="hidden";
    }
}//hideSmartInputFloater
function processSmartInput(inputBox) 
{
    if (!siw)
    {
        siw = new smartInputWindow();
    }
    siw.inputBox = inputBox;
    classData = inputBox.className.split(" ");
    siwDirectives = null;
    for (i=0;(!siwDirectives && classData[i]);i++) 
    {
        if (classData[i].indexOf("inputSearch") != -1)
        {
            siwDirectives = classData[i];
        }
    }
    if (siwDirectives && (siwDirectives.indexOf(":") != -1)) 
    {
        siw.customFloater = true;
        newFloaterId = siwDirectives.split(":")[1];
        siw.floater = document.getElementById(newFloaterId);
        siw.floaterContent = siw.floater.getElementsByTagName("div")[0];
    }
    setSmartInputData();
} //processSmartInputFloater

function simplify(s) 
{
    return s.toLowerCase().replace(/^[ \s\f\t\n\r]+/,'').replace(/[ \s\f\t\n\r]+$/,'');
}//simplify
function getUserInputToMatch(s) 
{
    a = s;
    fields = s.split(",");
    if (fields.length > 0)
    {
         a = fields[fields.length - 1];
    }
    return a;
}//getUserInputToMatch
function getUserInputBase() 
{
    s = siw.inputBox.value;
    a = s;
    if ((lastComma = s.lastIndexOf(",")) != -1) 
    {
        a = a.replace(/^(.*\,[ \r\n\t\f\s]*).*$/i,'$1');
    }
    else
    {
        a = "";
    }
    return a;
}//getUserInputBase()
function runMatchingLogic(userInput, standalone) 
{
    userInput = simplify(userInput);
    if (userInput.length % 3 == 0) // runs every 3 chars - Hugo
    {    
        modifySmartInputBoxContent(userInput);
        showSmartInputFloater();  
        }
       
}//runMatchingLogic
function setSmartInputData() 
{
    if (siw) 
    {
        orgUserInput = siw.inputBox.value;
        orgUserInput = getUserInputToMatch(orgUserInput);
        userInput = orgUserInput.toLowerCase().replace(/[\r\n\t\f\s]+/gi,' ').replace(/^ +/gi,'').replace(/ +$/gi,'').replace(/ +/gi,' ').replace(/\\/gi,'').replace(/\[/gi,'').replace(/\(/gi,'').replace(/\./gi,'\.').replace(/\?/gi,'');
        if (userInput && (userInput != "") && (userInput != '"')) 
        {
            runMatchingLogic(userInput);
        }//if userinput not blank and is meaningful 
    }//siw exists ... ok!
}//setSmartInputData

function modifySmartInputBoxContent(content)
{
    siw.floaterContent.innerHTML = '<div class="blur"><div class="shadow"><div id="smartInputResults" class="searchResults" ></div></div></div>';
    //post URL to verity and format it well through XSL
    getdata('http://www.neamb.com/xchg/neamb/xsl/hs.xsl/-/dynaments/Autocomplete.xml?user-input=' + content ,'smartInputResults'); 
}//modifySmartInputBoxContent()

function smartInputWindow () 
{
    this.customFloater = false;
    this.floater = document.getElementById("smartInputFloater");
    this.floaterContent = document.getElementById("smartInputFloaterContent");
    this.isGecko = (navigator.userAgent.indexOf("Gecko/200") != -1);
    this.isSafari = (navigator.userAgent.indexOf("Safari") != -1);
    this.isWinIE = ((navigator.userAgent.indexOf("Win") != -1 ) && (navigator.userAgent.indexOf("MSIE") != -1 ));
    this.showCredit = false;
}//smartInputWindow Object
function registerSmartInputListeners() 
{
    inputs = document.getElementsByTagName("input");
    texts = document.getElementsByTagName("textarea");
    allinputs = new Array();
    z = 0;
    y = 0;
    while(inputs[z]) 
    {
        allinputs[z] = inputs[z];
        z++;
    }//
    while(texts[y]) 
    {
        allinputs[z] = texts[y];
        z++;
        y++;
    }//
    for (i=0; i < allinputs.length;i++)
    {
        if ((c = allinputs[i].className) && (c == "inputSearch")) 
        {
            allinputs[i].setAttribute("autocomplete","OFF");
            allinputs[i].onclick = handleClick;
            allinputs[i].onfocus = handleFocus;
            allinputs[i].onblur = handleBlur;
            allinputs[i].onkeyup = handleKeyPress;
        }
    }//loop thru inputs
}//registerSmartInputListeners

// main 
siw = null;
registerSmartInputListeners();
document.write (
'<div id="smartInputFloater" class="floater"><div id="smartInputFloaterContent">'
+'</div></div>'
);

