
//Reload the LNF Agent data on page load
function reloadAgentDetails()
{
    if (document.getElementById(g_control_prefix + "DummyDDLSelectedItemIndex").value!="")
    {
        reloadAgentData();  
    }
   displayAgentInformation();
}

//Load data from the hidden field into the drop down list
function reloadAgentData()
{
    var agentData=document.getElementById(g_control_prefix + "DummyDDLSearchedAgent").value;
    var agentDataRows=agentData.split("#");
    
    if (agentDataRows.length>0)
    {
        agentDropDown=document.getElementById(g_control_prefix + "AgentDropDownList");
        //agentDropDown.textContent="";
        agentDropDown.innerHTML="";
        for(var iCount=0; iCount<agentDataRows.length; iCount++)
        {
            var agentDataRow=agentDataRows[iCount].split("^");
            optionItem = new Option(agentDataRow[0],agentDataRow[1],false, false);
            agentDropDown.options[agentDropDown.length] = optionItem;
        }
        
        hideDisplayDiv("WorkingWithAgentDiv",true);
        hideDisplayDiv("WorkingWithAgentDivDDL",false);
        
        if(document.getElementById(g_control_prefix + "DummyDDLSelectedItemIndex").value!="")
        {
         for (var count = agentDropDown.options.length-1; count >-1; count--)
         {
           if(agentDropDown.options[count].value == document.getElementById(g_control_prefix + "DummyDDLSelectedItemIndex").value)
           {
              agentDropDown.options[count].selected=true;
           }

         }
       }
     }
     else
     {
        hideDisplayDiv("WorkingWithAgentDivDDL",true);
        hideDisplayDiv("WorkingWithAgentDiv",false);
     }
}

//Get agent list for the First name and Last Name entered		 
function agentSearch()
{
    if(document.getElementById(g_control_prefix + "AgentFirstNameTextBox").value=="" 
       && document.getElementById(g_control_prefix + "AgentLastNameTextBox").value=="" )
    {
        alert("Please enter the agent name");
    }
    else
    {
        //Get the data from the database through ajax call for the entered parameter
        ajaxAgentSearch(document.getElementById(g_control_prefix + "AgentFirstNameTextBox").value,document.getElementById(g_control_prefix + "AgentLastNameTextBox").value);
    }
}

//Get agent list for the First name and Last Name entered for we Care form	
function agentSearchWeCare()
{
    if(document.getElementById(g_control_prefix + "AgentFirstNameTextBox").value=="" 
       && document.getElementById(g_control_prefix + "AgentLastNameTextBox").value=="" )
    {
        alert("Please enter the agent name");
    }
    else
    {
        //Get the data from the database through ajax call for the entered parameter
        
        ajaxAgentSearchWeCare(document.getElementById(g_control_prefix + "AgentFirstNameTextBox").value,document.getElementById(g_control_prefix + "AgentLastNameTextBox").value);
    }
}
//Get agent list for the First name and Last Name entered for we Care form	
function agentSearchWeCareSecond()
{
    if(document.getElementById(g_control_prefix + "AgentFirstNameTextBox").value=="" 
       && document.getElementById(g_control_prefix + "AgentLastNameTextBox").value=="" )
    {
        alert("Please enter the agent name");
    }
    else
    {
        //Get the data from the database through ajax call for the entered parameter
        
        ajaxAgentSearchWeCareSecond(document.getElementById(g_control_prefix + "AgentFirstNameTextBox").value,document.getElementById(g_control_prefix + "AgentLastNameTextBox").value);
    }
}
			 
			 
//function to populate the agent search result
function agentSearch_Callback(result)
{ 
   var response = result.value;
   var agentDropDown = document.getElementById(g_control_prefix + "AgentDropDownList");
    
    //Clears the combo box contents.
    agentDropDown.innerHTML="";
    //agentDropDown.textContent="";
    
    if (response)
    {
    if(response.Rows.length==0)
    {
        alert("No Agents found for the search criteria");
        hideDisplayDiv("WorkingWithAgentDivDDL",true);
        hideDisplayDiv("WorkingWithAgentDiv",false);
    }
    else
    {
        var searchedAgentData="";
        for(var i=0; i<response.Rows.length; i++)
        {
            optionText=response.Rows[i].AgentOffice;
            optionValue=response.Rows[i].agent_number + '|' + response.Rows[i].fName 
                       + '|' + response.Rows[i].lName + '|' + response.Rows[i].email;
            
            //Save the agentSearchData into local variable
            searchedAgentData=searchedAgentData + optionText + "^" + optionValue + "#";          
            
            optionItem = new Option(optionText,optionValue,false, false);
            agentDropDown.options[agentDropDown.length] = optionItem;
        }
        
        document.getElementById(g_control_prefix + "DummyDDLSearchedAgent").value=searchedAgentData;
        
        hideDisplayDiv("WorkingWithAgentDiv",true);
        hideDisplayDiv("WorkingWithAgentDivDDL",false);
  }
  }
}
		    
 //function to hide LNF agent search information on screen
function hideAgentSearch()
{
    var agentDropDown = document.getElementById(g_control_prefix + "AgentDropDownList");
    //Clears the combo box contents.
    //agentDropDown.textContent="";
    agentDropDown.innerHTML="";
    hideDisplayDiv("WorkingWithAgentDivDDL",true);
    hideDisplayDiv("WorkingWithAgentDiv",false);
    clearLnfAgentControls();
}
		    
//Function to hide or show the agent name information on screen
function displayAgentInformation()
{
    if(document.getElementById(g_control_prefix + "LnFAgentRadioButton").checked)
    {
        if (document.getElementById(g_control_prefix + "DummyDDLSelectedItemIndex").value!="")
        {
            hideDisplayDiv("WorkingWithAgentDiv",true);
            hideDisplayDiv("WorkingWithAgentDivDDL",false);
        }
        else
        {
            hideDisplayDiv("WorkingWithAgentDivDDL",true);
            hideDisplayDiv("WorkingWithAgentDiv",false);
        }
        clearNonLnfAgentControls();
        hideDisplayDiv("WorkingWithNonLnFAgentDiv",true);
    }
    else if(document.getElementById(g_control_prefix + "NoRadioButton").checked==true)
    {
        clearLnfAgentControls();
        clearNonLnfAgentControls();
        hideDisplayDiv("WorkingWithAgentDivDDL",true);
        hideDisplayDiv("WorkingWithAgentDiv",true);
        hideDisplayDiv("WorkingWithNonLnFAgentDiv",true);
    }
    else if(document.getElementById(g_control_prefix + "NonLnFAgentRadioButton"))
    {
        if(document.getElementById(g_control_prefix + "NonLnFAgentRadioButton").checked)
        {
            clearLnfAgentControls();
            hideDisplayDiv("WorkingWithAgentDivDDL",true);
            hideDisplayDiv("WorkingWithAgentDiv",true);
            hideDisplayDiv("WorkingWithNonLnFAgentDiv",false);
        }
    }

}
	
function hideDisplayDiv(elementId,bHide)
{
    var element=document.getElementById(elementId);    
    if (element)
    {
        if (bHide)
        {
            element.style.visibility = 'hidden';
            element.style.display='none';
        }
        else
        {
            element.style.visibility = 'visible';
            element.style.display='block';
        } 
    }
}

function clearLnfAgentControls()
{
    document.getElementById(g_control_prefix + "AgentFirstNameTextBox").value="";
    document.getElementById(g_control_prefix + "AgentLastNameTextBox").value="";
    document.getElementById(g_control_prefix + "DummyDDLSelectedItemIndex").value="";
    document.getElementById(g_control_prefix + "DummyDDLSearchedAgent").value="";
    //document.getElementById(g_control_prefix + "AgentDropDownList").textContent="";
    document.getElementById(g_control_prefix + "AgentDropDownList").innerHTML=""; 
}

function clearNonLnfAgentControls()
{
    if (document.getElementById(g_control_prefix + "NonLnFAgentNameTextBox")!=null)
    {
        document.getElementById(g_control_prefix + "NonLnFAgentNameTextBox").value="";
        document.getElementById(g_control_prefix + "CompanyNameTextBox").value="";
    }
    else if(document.getElementById(g_control_prefix + "NonLnFAgentFirstNameTextBox")!=null)
    {
        document.getElementById(g_control_prefix + "NonLnFAgentFirstNameTextBox").value="";
        document.getElementById(g_control_prefix + "NonLnFAgentLastNameTextBox").value="";
        document.getElementById(g_control_prefix + "CompanyNameTextBox").value="";
    }
}

//Function to autotab telephone fields		
function autoTab(input,len) 
{
	if(input.value.length >= len) 
	{
		input.form[(getIndex(input)+1) % input.form.length].focus();
	}
	function getIndex(input) 
	{
		var index = -1, i = 0, found = false;
		while (i < input.form.length && index == -1)
			if (input.form[i] == input)
				index = i;
			else
				i++; 
			return index;
	}
}

