﻿function AjaxAutoComplete(){
    var timeoutId=0;
    var hoverTimeoutId=0;
    var prevText='';
    var that=this;
    
    window.onclick=onWClick;
    
    this.focusIn=function(){
        if(that.objTxt.value.length>=that.txtLength&&that.objTxt.value!=prevText){
            getResults();
        }else{
            if(that.objTxt.value.length<that.txtLength){
                gebid(that.divId).style.display='none';
            }
        }    
        prevText=that.objTxt.value;
        timeoutId=setTimeout(that.objectId+'.focusIn();',that.timeout);
    };
    this.focusOut=function(){
        if(timeoutId>0){
            clearTimeout(timeoutId);
        }
    };
    this.displayObject=function(displ){
        gebid(that.divId).style.display=displ;
    };
    this.setDivPosition=function(pos,dHeight,lDev,tDev){
        var left=0;
        var objLeft=that.objTxt.offsetLeft;
        var objTop=that.objTxt.offsetTop;
        switch(pos){
            case 1:
                left=objLeft;
                break;
            case 2:
                left=objLeft*1+1*that.objTxt.offsetWidth-that.objectTableWidth;
                break;
            default:
                left=(objLeft*1+Math.round(that.objTxt.offsetWidth/2))-Math.round(that.objectTableWidth/2);
                break;
        }
        gebid(that.divId).style.width=that.objectTableWidth+'px';
        if(!isNaN(dHeight)&&dHeight>0){
            gebid(that.divId).style.height=dHeight+'px';
            gebid(that.divId).style.overflow='auto';
        }
        gebid(that.divId).style.left=(left*1+lDev*1)+'px';
        gebid(that.divId).style.top=(objTop*1+1*that.objTxt.offsetHeight+1*tDev)+'px';
        gebid(that.divId).style.position='absolute';
        gebid(that.divId).style.zIndex=getMaxZIdx()*1+1;
        gebid(that.divId).className='searchTable';
        gebid(that.divId).onclick=onDClick;
        eval(that.objectTableId+'.finishedLoadingWithElements=function(){'+
        'var p=document.createElement(\'P\');'+
        'p.id=that.divId.replace(\'_tablePlaceHolder\',\'_pAditional\');'+
        'p.className=\'searchTable\';'+
        'var a=document.createElement(\'A\');'+
        'a.id=that.divId.replace(\'_tablePlaceHolder\',\'_aClose\');'+
        'a.href=\'jav\'+\'ascr\'+\'ipt:\'+that.objectId+\'.displayObject(\\\'none\\\')\';'+
        'a.innerHTML="Close";'+
        'p.appendChild(a);'+
        'gebid(that.divId).appendChild(p);'+
        'p=document.createElement(\'P\');'+
        'p.id=that.divId.replace(\'_tablePlaceHolder\',\'_pAditional\');'+
        'p.className=\'searchTable\';'+
        'var span=document.createElement(\'SPAN\');'+
        'span.innerHTML="Suggestions";'+
        'p.appendChild(span);'+
        'gebid(that.divId).insertBefore(p,gebid(that.divId).firstChild);'+
        'that.displayObject(\'block\');};');
        eval(that.objectTableId+'.finishedLoadingZeroElements=function(){that.displayObject(\'none\');};');
    };
    function getResults(){
        var newFilter='\'' + that.objTxt.value.replace('\'','\\\'')+ '\'';
        newFilter+=',' + that.elementsDisplayed;
        that.ajaxGrid.webMethodPopulateFilter=newFilter;
        that.ajaxGrid.initObject(true);
    };
    function onWClick(e){
        that.displayObject('none');
    };
    function onDClick(e){
        if (!e) var e = window.event;
	    e.cancelBubble = true;
	    if (e.stopPropagation) e.stopPropagation();
    };
};
function getMaxZIdx(){
    var allElems = document.getElementsByTagName?document.getElementsByTagName("*"):document.all;
    var maxZIndex = 0;
    for(var i=0;i<allElems.length;i++){
        var elem = allElems[i];
        var cStyle = null;
        if (elem.currentStyle) {cStyle = elem.currentStyle;}
        else if (document.defaultView && document.defaultView.getComputedStyle) {
            cStyle = document.defaultView.getComputedStyle(elem,"");
        }
        var sNum;
        if (cStyle){
            sNum = Number(cStyle.zIndex);
        }else{
            sNum = Number(elem.style.zIndex);
        }
        if (!isNaN(sNum)){
            maxZIndex = Math.max(maxZIndex,sNum);
        }
    }
    return maxZIndex;
};
function findPos(obj){
	var curleft=curtop=0;
    if(obj.offsetParent){
        do{
		    curleft += obj.offsetLeft;
		    curtop += obj.offsetTop;
		}while(obj=obj.offsetParent);
    }
	return[curleft,curtop];
};
