// Client Lookup object

function ClientLookup(clientSelectCallback,controlID, txtClientNameControlID, ExUID, ExID, listType, loadingString)
{
    this.ClientNameControlID = txtClientNameControlID;
    this.ControlID = controlID;
    this.errorNotified=false;
    this.searchString="";
    this.searchResult=null;
    this.stepCount=0;
    this.enableFX = false;
    this.ExUID=ExUID;
    this.ExID=ExID;
    this.listType = listType;
    this.clientSelectionCallback = clientSelectCallback;

    this.SelectedClientID= -1;

    this.resultsDiv=document.getElementById("clientQuickLookupResults"+this.ControlID);
    this.controlsContent = document.getElementById("lookupControls"+this.ControlID).innerHTML;
    this.resultsWindow = document.getElementById("lookupControls"+this.ControlID);

    this.loadingString = loadingString;
}


ClientLookup.prototype.setFocus = function()
{
    eval("document.getElementById(\"" +this.ClientNameControlID+"\").focus();");
}

ClientLookup.prototype.closeResults = function()
{
    this.resultsWindow.style.display='none';

    if (this.enableFX) FXCore_ResetFX("clientQuickLookupResults"+this.ControlID);
}

ClientLookup.prototype.beginLookup = function(searchString)
{

    searchString = searchString.replace("\'","");
    searchString = searchString.replace("\"","");

    //begin lookup for each key press, wait before performing lookup to allow use time to type
    this.searchString=searchString;

    setTimeout("clientlookup"+this.ControlID+".performLookup(\""+searchString+"\")",200);

    if (this.enableFX) FXCore_BeginFX("lookupControls"+this.ControlID,0.5);
}

ClientLookup.prototype.performLookup = function(searchStr)
{
    //each time we start performLookup we check to see if the search string we have
    //is the same as the one set when we last took a keypress, if not we return and wait until it is, otherwise we would do multiple lookups while the user typed

    //to ensure we only search when user has finished typing
    //don't process until we get a call with the expected search string
    if (this.searchString != searchStr) return;

    //reset style of text box ready to set confirmed input style once lookup complete
    eval("document.forms[0]."+this.ClientNameControlID+".className='';");

    if (this.searchString.length<3)
    {
        if (this.searchString.length==0)
        {
            this.resultsWindow.style.display='none';
        }
        else
        {
            this.resultsDiv.innerHTML=  this.searchString+" (At least 3 characters required to search)..";
            this.resultsWindow.style.display='block';
        }
    }
    else
    {
        this.resultsDiv.innerHTML= this.loadingString;
        this.resultsWindow.style.display='block';

        this.searchResult=null;

        this.displayProgress();

        //perform lookup via Ajax.net and send results via control specific processResults function to displayResults
        GETS.Web.Controls.Client.ClientQuickLookup.GetClientList(this.searchString,this.ExUID,this.ExID,this.listType,eval("processResults"+this.ControlID));
    }
}

ClientLookup.prototype.displayProgress = function()
{
    if (this.searchResult==null) {

        if (this.resultsDiv.innerHTML!=this.loadingString) this.resultsDiv.innerHTML = this.loadingString;

        setTimeout("clientlookup"+this.ControlID+".displayProgress()",200);
    }
}


ClientLookup.prototype.completeSelection =function(selectedClientName, selectedClientID)
{
    eval("document.forms[0]."+this.ClientNameControlID+".className='confirmedInput';");
    eval("document.forms[0]."+this.ClientNameControlID+".value=\""+selectedClientName+"\";");

    if (selectedClientID !=null) {
        document.getElementById(this.ControlID+"_selectedClientID").value=selectedClientID;
        this.SelectedClientID=selectedClientID;
    }

}

ClientLookup.prototype.completePostback = function()
{
    if (this.SelectedClientID!=-1 && this.SelectedClientID!=null )
        setTimeout('__doPostBack(\''+this.ControlID+'\',\'\')', 0);
}

ClientLookup.prototype.displayResults =function(lookupResponse)
{
      displayResults(lookupResponse,false); 
}

ClientLookup.prototype.displayResults =function(lookupResponse,IsAdmin)
{
    this.searchResult=lookupResponse;

    if (lookupResponse.error != null && lookupResponse.error!=false){

        if (this.errorNotified==false)
        {
            this.resultsDiv.innerHTML += "(Error:"+lookupResponse.error.Message+")";
            this.errorNotified=true;
            alert('Your session has expired. Please Sign In again.');
            this.closeResults();
        }
        return;
    }
    
    var resultstable=lookupResponse.value;

    this.resultsDiv.innerHTML="";

    this.SelectedClientID= -1;

    if (resultstable.Rows.length==0 || resultstable==null)
    {
        this.resultsDiv.innerHTML += "(no matching clients found)";
    }
    else
    {
        var alternateRow=false;
        var classStr="";
        for (var i =0;i<resultstable.Rows.length;i++)
        {
            var companyName = resultstable.Rows[i].CompanyName;
            var rating = resultstable.Rows[i].ExchangeRating;
            var reference = resultstable.Rows[i].ClientReference;
                        
            companyName = companyName.replace("\'","&rsquo;");
            companyName = companyName.replace("\'","");

            reference = companyName +" ("+reference+")";
            
            if (alternateRow){
                if(IsAdmin == "True"){
                    classStr= "class='Positive'";
                }
                else{ 
                    classStr= "";
                }
                alternateRow=false;
            }
            else {
                 alternateRow=true;
                 if(IsAdmin=="True"){
                    classStr= "class='Positive'";
                 }
                 else {
                    classStr= "";
                 }
            }
            
            if (rating)
            {
                if(IsAdmin=="True")
                {
                    if (rating==25) classStr="class='Negative'";
                    if (rating==50) classStr="class='Neutral'";
                    if (rating==75) classStr="class='Positive'";
                }
                else
                {
                    if (rating==25) classStr="";
                    if (rating==50) classStr="";
                    if (rating==75) classStr="";
                }
            }
            else
            {
                if(IsAdmin=="True")
                {
                    classStr="class='Neutral'"; 
                }
                else
                {
                    classStr=""; 
                }
            }

            if (this.clientSelectionCallback!=null)
                this.resultsDiv.innerHTML+="<a "+classStr+" href=\"#\" onclick=\"clientlookup"+this.ControlID+".clientSelectionCallback("+resultstable.Rows[i].ID+",'"+companyName+"','"+this.ClientNameControlID+"');clientlookup"+this.ControlID+".closeResults();\" title='"+reference+"'>"+companyName+"</a>";
            else
                this.resultsDiv.innerHTML+="<a "+classStr+" href=\"#\" onclick=\"clientlookup"+this.ControlID+".closeResults();\" title='"+reference+"'>"+companyName+"</a>";

        }
    }
    this.resultsWindow.style.display='block';
}






