var symac_getSuggestionsUrl="/apps/data?pid=symautocomplete&Query=";
var symac_inputKeyword="";
var symac_sentKeyword="";
var symac_resultKeyword="";
var symac_totalpositions=0;
var symac_suggestionMaxLength=200;
var symac_delaySendTimer=null;
var symac_position=-1;
var symac_oCache=new Object();
var symac_cachesize=100;
var symac_httpRequestor=createAJAXRequestor();
var symac_delaySendMsec=100;
var symac_defaultText="Symbol, Site, News Search";
var symac_skipSendCount=0;
var symac_skipSendMax=5;
var newsSearchURL="http://search.bloomberg.com/search?site=wnews&client=wnews&output=xml_no_dtd&ie=UTF-8&oe=UTF-8&filter=p&getfields=wnnis&sort=date:D:S:d1&lr=-lang_ja&proxystylesheet=wnews&q=";

function symac_handleFocus(){
    var a=document.getElementById("symac_keyword");
    a.value="";
    a.className="symac_normalText";
    symac_inputKeyword="";
    symac_resultKeyword="";
    symac_position=-1
}

function symac_setPrompt(){
    var a=document.getElementById("symac_keyword");
    a.value=symac_defaultText;
    a.className="symac_grayText"
}

function symac_delayhandleLoseFocus(a){
    symac_setPrompt();setTimeout("symac_hideSuggestions()",500)
}

function symac_addToCache(a,d){
    var b=0;
    for(var c in symac_oCache){
        b++
    }
    if(b>=symac_cachesize){
        symac_oCache=new Object()
    }
    symac_oCache[a]=d
}

function symac_changeResults(){
    if(symac_resultKeyword==symac_inputKeyword){return}
    if(symac_inputKeyword==""){
        symac_hideSuggestions();
        symac_resultKeyword=symac_inputKeyword;return
    }
    var a=symac_oCache[symac_inputKeyword];
    if(a){
        symac_displayResults(a);
        symac_resultKeyword=symac_inputKeyword;return
    }
    if(symac_delaySendTimer!=null){
        clearTimeout(symac_delaySendTimer)
    }
    symac_delaySendTimer=setTimeout("symac_sendRequest()",symac_delaySendMsec)
}

function symac_sendRequest(){
    symac_delaySendTimer=null;if(symac_inputKeyword==""){return}
    if(symac_skipSendCount++<symac_skipSendMax&&symac_httpRequestor&&(symac_httpRequestor.readyState>0&&symac_httpRequestor.readyState<4)){return}
    symac_skipSendCount=0;
    symac_sentKeyword=symac_inputKeyword;
    symac_directRequest(symac_sentKeyword)
}

function symac_directRequest(a){
    if(window.bbabsurls){
        document.domain="bloomberg.com";
        document.getElementById("symac_ajaxRequestorFrame").contentWindow.symacproxy_sendRequest(a)
    }else{
        sendAJAXRequest(symac_httpRequestor,"GET",symac_getSuggestionsUrl+encodeURIComponent(a),null,function(){
            passAJAXResponse(symac_httpRequestor,symac_gotResponse)
        })
    }
}

function symac_gotResponse(a){
    if(a.indexOf("symac_suggestTable")>0){
        symac_addToCache(symac_sentKeyword,a)
    }symac_changeResults()
}

function symac_displayResults(b){
    var c=document.getElementById("symac_suggest");
    c.innerHTML=b;
    c.style.visibility="visible";
    var a=document.getElementById("symac_suggestTable");
    symac_totalpositions=a.rows.length;symac_position=document.getElementById("symac_hltposition").value;symac_highlightKeyword(symac_position)
}

function symac_handleKeyUp(b){
    b=(!b)?window.event:b;
    target=(!b.target)?b.srcElement:b.target;
    if(target.nodeType==3){
        target=target.parentNode
    }
    code=(b.charCode)?b.charCode:((b.keyCode)?b.keyCode:((b.which)?b.which:0));
    if(b.type=="keyup"){
        if(code==13){
            var a=document.getElementById("symac_keyword").value;
            symac_delayhandleLoseFocus(a);
            document.getElementById("symac_keyword").blur();
            if(symac_position>=0){
                window.location.href=document.getElementById("symac_a"+symac_position).href
            }else{
                if(symac_trim(a)!=""){
                    window.location.href=newsSearchURL+encodeURIComponent(a)
                }
            }
        }else{
            if(code==40){
                oldTR=document.getElementById("symac_tr"+symac_position++);
                if(symac_position==symac_totalpositions){
                    symac_position=0
                }
                newTR=document.getElementById("symac_tr"+symac_position);
                symac_highlightRow(oldTR,false);
                symac_highlightRow(newTR,true)
            }else{
                if(code==38){
                    oldTR=document.getElementById("symac_tr"+symac_position--);
                    if(symac_position==-1){
                        symac_position=symac_totalpositions-1
                    }
                    newTR=document.getElementById("symac_tr"+symac_position);
                    symac_highlightRow(oldTR,false);
                    symac_highlightRow(newTR,true)
                }else{
                    var a=document.getElementById("symac_keyword").value;
                    a=symac_normalizeKeyword(a);
                    if(a==symac_inputKeyword){return}
                    symac_inputKeyword=a;
                    symac_changeResults()
                }
            }
        }
    }
}

function symac_deselectAll(){
    for(var b=0;b<symac_totalpositions;b++){
        var a=document.getElementById("symac_tr"+b);
        symac_highlightRow(a,false)
    }
}

function symac_handleOnMouseOver(a){
    symac_deselectAll();symac_highlightRow(a,true);
    symac_position=a.id.substring(8,a.id.length)
}

function symac_hideSuggestions(){
    var a=document.getElementById("symac_suggest");
    a.style.visibility="hidden"
}

function symac_highlightKeyword(a){
    symac_deselectAll();
    symac_position=a;
    var b=document.getElementById("symac_tr"+a);symac_highlightRow(b,true)
}

function symac_highlightRow(c,b){
    var a=c.className.indexOf("_highlight");
    if(b&&a<0){
        c.className=c.className+"_highlight"
    }else{
        if(!b&&a>0){
            c.className=c.className.substring(0,a)
        }
    }
}

function symac_normalizeKeyword(a){
    a=a.replace(/ +/g," ");
    a=a.replace(/^ +/,"");
    a=a.replace(/ +$/,"");
    a=a.toUpperCase();
    return a
}

function symac_trim(a){
    a=a.replace(/^\s+|\s+$/g,"");return a
};