
function autoCompleteDB()
{
 this.aNames=new Array();
 this.aNameIds=new Array();
}
autoCompleteDB.prototype.assignArray=function(aList,aIdsList)
{
this.aNames=aList;this.aNameIds=aIdsList;
};
autoCompleteDB.prototype.getMatches=function(str,aList,aIdList,maxSize)
{
    var ctr=0;
    for(var i=0;i<this.aNames.length;i++)
    {
        if(this.aNames[i].toLowerCase().indexOf(str.toLowerCase())==0) /*looking for case insensitive matches */
        {
        aList.push(this.aNames[i]);
        aIdList.push(this.aNameIds[i]);
        ctr++;
        }
        if(ctr==(maxSize-1)) /* counter to limit no of matches to maxSize */
        break;
    }
};

function autoComplete(aNames,aNameIds,oText,ohtxts,oDiv,FillMethodName,CallBackTo,SetFocusTo,maxSize,MinPrefix,IsServiceCalled)
{
    this.oText=oText;
    this.ohtxt=ohtxts;
    this.oDiv=oDiv;
    this.maxSize=maxSize;
    this.MinPrefix=MinPrefix;
    this.cur=-1;
    this.chIds=aNameIds;
    this.FillMethodName=FillMethodName;
    this.CallBackTo=CallBackTo;
    this.SetFocusTo=SetFocusTo;
    if(!IsServiceCalled)
        this.searchKey='';
    else
        this.searchKey=oText.value;
        this.IsServiceCalled=IsServiceCalled;
        this.db=new autoCompleteDB();
        this.db.assignArray(aNames,aNameIds);
        oText.onkeyup=this.keyUp;
        oText.onkeydown=this.keyDown;
        oText.autoComplete=this;
        oText.onblur=this.hideSuggest;
        var oThis=this;
        var txt=this.oText.value;
        
    if(txt.length>=MinPrefix)
    {
        while(this.oDiv.hasChildNodes())
        this.oDiv.removeChild(this.oDiv.firstChild);
        var aStr=new Array();
        var aIdStr=new Array();
        this.chIds=aIdStr;
        this.db.getMatches(txt,aStr,aIdStr,this.maxSize);
        this.chIds=aIdStr;
        this.positionSuggest();
        if(aStr.length>0)
        {
        this.oDiv.className ="div2"
        }    
        else
        {
        this.oDiv.innerHTML="";
        this.oDiv.className ="div"
        }
        for(var i=0;i<aStr.length;i++)
        {
            var oNew=document.createElement('div');
            oNew.style.width= '';//document.getElementById(_CurTxtId).offsetWidth +10 + 'px';modified by satya
            this.oDiv.appendChild(oNew);
            oNew.onmouseover=
            oNew.onmouseout=
            oNew.onmousedown=function(oEvent)
            {
            oEvent=window.event || oEvent;
            oSrcDiv=oEvent.target || oEvent.srcElement;
            if(oEvent.type=="mousedown")
                {
                if(this.innerText)
                oThis.oText.value=this.innerText;
                else
                oThis.oText.value=this.textContent;
                oThis.CheckSelection();
                }
            else if(oEvent.type=="mouseover")
                {
                this.className="over";
                }
            else if(oEvent.type=="mouseout")
                {
                this.className="";
                }
            else
                {
                this.oText.focus();
                }
            };
            oNew.innerHTML=aStr[i];
            this.oDiv.appendChild(oNew);
        }    
    }
    else
    {
    this.oDiv.innerHTML="";
    this.oDiv.className ="div"
    }
}
autoComplete.prototype.hideSuggest=function()
{
    this.autoComplete.oDiv.className="div";
    this.autoComplete.CheckSelection();
    if(this.autoComplete.CallBackTo!="")
    {
    eval(this.autoComplete.CallBackTo + "();");
    }
};
autoComplete.prototype.selectText=function(iStart,iEnd)
{
    if(this.oText.createTextRange) /* For IE */
    {
    var oRange=this.oText.createTextRange();
    oRange.moveStart("character",iStart);
    oRange.moveEnd("character",iEnd-this.oText.value.length);
    oRange.select();
    }
    else if(this.oText.setSelectionRange) /* For Mozilla */
    {
    this.oText.setSelectionRange(iStart,iEnd);
    }
    this.oText.focus();
};
autoComplete.prototype.textComplete=function(sFirstMatch)
{

    if(this.oText.createTextRange || this.oText.setSelectionRange)
    {
    var iStart=this.oText.value.length;
    this.oText.value=sFirstMatch;
    this.selectText(iStart,sFirstMatch.length);
    }
};
autoComplete.prototype.keyDown=function(oEvent)
{
    oEvent=window.event || oEvent;
    iKeyCode=oEvent.keyCode;
    switch(iKeyCode)
    {
    case 38: 
        this.autoComplete.moveUp();
        break;
    case 40: 
        this.autoComplete.moveDown();
        break;
    case 13: 
        this.autoComplete.CheckSelection();
        return false;
        break;
    }
};
autoComplete.prototype.CheckSelection=function()
{
    if(this.oText.value!="")
    {
        var tid=this.db.getId(this.oText.value)
        if(tid!=null && tid!="")
        {
            var Valarr=tid.split(',');
            var Contarr=this.ohtxt.split(',');
            for(var i=0;i<Valarr.length;i++)
            {
                if(document.getElementById(Contarr[i]) && Valarr[i])
                {
                document.getElementById(Contarr[i]).value = Valarr[i];
                }
            }
            if(this.SetFocusTo!="")
            {
            eval("document.getElementById('" + this.SetFocusTo + "').focus()");
            }
            if(this.CallBackTo!="")
            {
            eval(this.CallBackTo + "();");
            }
        }
        else if(this.IsServiceCalled)
        {
            var Contarr=this.ohtxt.split(',');
            for(var i=0;i<Contarr.length;i++)
            {
                if(document.getElementById(Contarr[i]))
                {
                document.getElementById(Contarr[i]).value = "";
                }
            }
        }
    }
    else
    {
        var Contarr=this.ohtxt.split(',');
        for(var i=0;i<Contarr.length;i++)
        {
            if(document.getElementById(Contarr[i]))
            {
            document.getElementById(Contarr[i]).value = "";
            }
        }
    }
}
autoComplete.prototype.moveDown=function()
{
    if(this.oDiv.childNodes.length>0 && this.cur<(this.oDiv.childNodes.length-1))
    {
        ++this.cur;
        for(var i=0;i<this.oDiv.childNodes.length;i++)
        {
        if(i==this.cur)
        {
            this.oDiv.childNodes[i].className="over";
            if(this.oDiv.childNodes[i].innerText)
                 this.oText.value=this.oDiv.childNodes[i].innerText;
            else
                //this.oDiv.childNodes[i].textContent;
                this.oText.value=this.oDiv.childNodes[i].textContent;
                var Valarr=this.chIds[i].split(',');
                var Contarr=this.ohtxt.split(',');
                for(var j=0;j<Valarr.length;j++)
                {
                    if(document.getElementById(Contarr[j]) && Valarr[j])
                    {
                    document.getElementById(Contarr[j]).value = Valarr[j];
                    }
                }
        }
        else
        {
        this.oDiv.childNodes[i].className="";
        }
        }
    }
};
autoComplete.prototype.moveUp=function()
{
    if(this.oDiv.childNodes.length>0 && this.cur>0)
    {
        --this.cur;
        for(var i=0;i<this.oDiv.childNodes.length;i++)
        {
            if(i==this.cur)
            {
            this.oDiv.childNodes[i].className="over";
            if(this.oDiv.childNodes[i].innerText)
            this.oText.value=this.oDiv.childNodes[i].innerText;
            else
            this.oText.value=this.oDiv.childNodes[i].textContent;
            var Valarr=this.chIds[i].split(',');
            var Contarr=this.ohtxt.split(',');
            for(var j=0;j<Valarr.length;j++)
            {
                if(document.getElementById(Contarr[j]) && Valarr[j])
                {
                document.getElementById(Contarr[j]).value = Valarr[j];}
                }
            }
            else
            {
            this.oDiv.childNodes[i].className="";
            }
        }
    }
};
autoComplete.prototype.keyUp=function(oEvent)
{
    oEvent=oEvent || window.event;
    var iKeyCode=oEvent.keyCode;
    if(iKeyCode==8 || iKeyCode==46)
    {
   
    this.autoComplete.onTextChange(false);/* without autocomplete */
    }
    else if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode <= 46) || (iKeyCode >= 112 && iKeyCode <= 123)) 
    {
    } 
    else 
    {
    this.autoComplete.onTextChange(true);/* with autocomplete */
    }
};
autoComplete.prototype.positionSuggest=function() /* to calculate the appropriate poistion of the dropdown */
    {
//        var oNode=this.oText;
//        
//        //var x=0,y=oNode.offsetHeight;
//        var x=0,y=0;
//        while(oNode.offsetParent && oNode.offsetParent.tagName.toUpperCase() != 'BODY')
//        {
//            if(oNode.style.zIndex >0)
//            {
//                break;
//            }
//        x+=oNode.offsetLeft;
//        y+=oNode.offsetTop;
//        oNode=oNode.offsetParent;
//        }
        
//        if(oNode.style.zIndex <=0)
//        {
//            x+=oNode.offsetLeft;
//            y+=oNode.offsetTop;
//        }



//        document.getElementById("divCompanyContent").style.left=x-40+"px";
//        document.getElementById("divCompanyContent").style.top=y+22+"px";

//           var resolution = screen.width+' x '+screen.height           

//            if (resolution == "1024 x 768")
//            {
//                this.oDiv.style.top=(y+22)+"px";
//                this.oDiv.style.left=(x-75)+"px";   
//            }
//            else if (resolution == "1152 x 864")
//            {
//               this.oDiv.style.top=(y+22)+"px";
//               this.oDiv.style.left=(x-125)+"px";   
//            }
//            else if (resolution == "1280 x 1024")
//            {
//               this.oDiv.style.top=(y+22)+"px";
//               this.oDiv.style.left=(x-175)+"px";   
//            }
//            else
//            {
//                this.oDiv.style.top=(y+22)+"px";
//                this.oDiv.style.left=(x-40)+"px";   
//            }


//        x+=oNode.offsetLeft;
//         y+=oNode.offsetTop;
//        this.oDiv.style.top=(y+22)+"px";
//        this.oDiv.style.left=(x-25)+"px";      
        }
autoCompleteDB.prototype.getId=function(str)
{
    for(var i=0;i<this.aNames.length;i++)
    {
        if(str.toLowerCase()==this.aNames[i].toLowerCase())
        return this.aNameIds[i];
     }
}
autoComplete.prototype.onTextChange=function(bTextComplete)
{
    var txt=this.oText.value;
    var oThis=this;this.cur=-1;
    var csk=txt;
    if( (this.searchKey.toLowerCase()!=csk.toLowerCase() && txt.length>=this.MinPrefix) || (!this.IsServiceCalled && txt.length>=this.MinPrefix))
    {
        this.searchKey=txt;
        this.IsServiceCalled=true;if(txt.indexOf("'") > 0)
        txt = txt.replace("'","");
        eval(this.FillMethodName + "('" + txt + "');");
    }
    else
    {
        if(txt.length>=this.MinPrefix)
        {
            while(this.oDiv.hasChildNodes())
            this.oDiv.removeChild(this.oDiv.firstChild);
            var aStr=new Array();
            var aIdStr=new Array();
            this.chIds=aIdStr;
            this.db.getMatches(txt,aStr,aIdStr,this.maxSize);
            this.chIds=aIdStr;
            this.positionSuggest();
            if(aStr.length>0)
            {
            this.oDiv.className ="div2"
            }
            else
            {
            this.oDiv.innerHTML="";this.oDiv.className ="div"
            }
        for(var i=0;i<aStr.length;i++)
        {
            var oNew=document.createElement('div');
            oNew.style.width= '';//document.getElementById(_CurTxtId).offsetWidth - 3 + 'px'; modified by satya
            oNew.onmouseover=
            oNew.onmouseout=
            oNew.onmousedown=function(oEvent)
            {
                oEvent=window.event || oEvent;
                oSrcDiv=oEvent.target || oEvent.srcElement;
                if(oEvent.type=="mousedown")
                {
                    if(this.innerText)
                    oThis.oText.value=this.innerText;
                    else
                    oThis.oText.value=this.textContent;
                    oThis.CheckSelection();
                }
                else if(oEvent.type=="mouseover")
                {
                this.className="over";
                }
                else if(oEvent.type=="mouseout")
                {
                this.className="";
                }
                else
                {
                this.oText.focus();
                }
            };
            oNew.innerHTML=aStr[i];
            oNew.style.color = "black";
            this.oDiv.appendChild(oNew);
        }
        }
        else
        {
            this.oDiv.innerHTML="";
            this.oDiv.className ="div"
        }
    }
};
    var _CurTxtId;
    var _CurDiv;
    var _CurHtxtIds;
    var _FillMethodName;
    var _CallBackTo;
    var _SetFocusTo;
    var _MaxCount;
    var _DataTextField;
    var _DataValueField;
    var _MinPrefix;
this.gotFocus=function(ObjtxtId,ObjCurDiv,Objhtxt,FillMethodName,CallBackTo,SetFocusTo,MaxCount,DataTextField,DataValueField,MinPrefix)
{
    var aNames =new Array();
    var aNameIds=new Array();
    _CurTxtId=ObjtxtId.id;
    _CurDiv=ObjCurDiv;
    _CurHtxtIds=Objhtxt;
    _FillMethodName=FillMethodName;
    _CallBackTo=CallBackTo;
    _SetFocusTo=SetFocusTo;
    _MaxCount=MaxCount;
    _DataTextField=DataTextField;
    _DataValueField=DataValueField;
    _MinPrefix=MinPrefix;
    new autoComplete(aNames,aNameIds,document.getElementById(_CurTxtId),_CurHtxtIds,ObjCurDiv,FillMethodName,CallBackTo,SetFocusTo,MaxCount,MinPrefix,false);}
function createAutoComplete(response)
{
    this.aNames =new Array();
    this.aNameIds=new Array();
    var _Name="";
    var _Id="";
    if(response!=null)
    {
        var xmlDoc;
        if (window.ActiveXObject)
        {
            xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.loadXML(response);
        }
        else if (document.implementation && document.implementation.createDocument)
        {
            var parser=new DOMParser();
            xmlDoc=parser.parseFromString(response,"text/xml");
        }
        if(xmlDoc != null)
        {
            var NoOfItems=xmlDoc.getElementsByTagName("Table").length;
            for(var ItemNum=0;ItemNum<NoOfItems;ItemNum++)
            {
                _Id=xmlDoc.getElementsByTagName(_DataValueField)[ItemNum].firstChild.nodeValue;
                _Name=xmlDoc.getElementsByTagName(_DataTextField)[ItemNum].firstChild.nodeValue;aNames.push(_Name.trim());
                aNameIds.push(_Id);
            }
            new autoComplete(aNames,aNameIds,document.getElementById(_CurTxtId),_CurHtxtIds,_CurDiv,_FillMethodName,_CallBackTo,_SetFocusTo,_MaxCount,_MinPrefix,true);}
        }     
    }
    String.prototype.trim = function() 
    {
    return this.replace(/^\s+|\s+$/, '');
};
