﻿
function loadOffices()
{
    wsAgent.GetOffices( function(categories)
                             {
                                addListBoxItem(_ddlCategory, '[Select Office]', -1);

                                if (categories)
                                {
                                    for(var i = 0; i < categories.length; i++)
                                    {
                                        addListBoxItem(_ddlCategory, categories[i].Name, categories[i].OfficeID);
                                    }
                                }

                                $addHandler(_ddlCategory, 'change', onCategoryChange);
                              }
                            );
}

function pageUnload(sender, e)
{
    $removeHandler(_ddlCategory, 'change', onCategoryChange);
}

function onCategoryChange(e)
{
    if (parseInt(_ddlCategory.options[_ddlCategory.selectedIndex].value) > -1)
    {
        loadAgents();
    }
}

function loadAgents()
{
    //$get('divMessage').style.display = '';
    var startIndex = (_pager.get_pageIndex() * _pager.get_pageSize());
    _message.style.visibility="visible";
    wsAgent.GetAgents(parseInt(_ddlCategory.options[_ddlCategory.selectedIndex].value), startIndex, _pager.get_pageSize(), onAgentLoadSuccess);
}

function onAgentLoadSuccess(result)
{
    _pager.set_recordCount(result.Total);
    _dlAgents.set_dataSource(result.Rows);
    _dlAgents.dataBind();

    _message.style.visibility = 'hidden';
}

function onItemDataBound(sender, e)
{
    var item = e.get_item();

    if (item.get_isDataItemType())
    {
        var agent = item.get_dataItem();

        var agentLink = item.findControl('agentLink');
        var agentImgLink = item.findControl('agentImgLink');
        var spnphone_main = item.findControl('spnphone_main');
        var spnphone_fax = item.findControl('spnphone_fax');
        var imgAgent = item.findControl('imgAgent');

        setLink(agentLink, agent.fname + ' ' + agent.lname, agent.mls_agent_id);
        agentImgLink.href += '?agentmls=' + agent.mls_agent_id;
        
        setText(spnphone_main, agent.phone_main);
        if (agent.phone_fax != '')
            setText(spnphone_fax, agent.phone_fax + ' (fax)');
        else
            setText(spnphone_fax, agent.phone_fax);
            
        setImg(imgAgent, agent.picture);
    }
}

function addListBoxItem(listBox, text, value)
{
    var opt = document.createElement('option');

    listBox.appendChild(opt);

    setText(opt, text);

    if (value != null)
    {
        opt.value = value;
    }
}

//function pageChanged(sender, e)
//{
//    alert("HELLO");
//    _pager.set_pageIndex(e.get_newPageIndex());
//    loadAgents();
//    Sys.Application.get_history().addHistoryPoint({pageClientState: e.get_newPageIndex() + 1}, top.document.title);
//}

function setText(element, text)
{
    if (typeof element.innerText != 'undefined' && text != null)
    {
        element.innerText = text;
    }
    else if (typeof element.textContent != 'undefined' && text != '')
    {
        element.textContent = text;
    }
    else
    {
        element.style.display='none';
    }
}

function setImg(element, text)
{     
    if ((text != null) && (text != ''))
    {
        element.src = text;
    }
}

function setLink(element, text, agentId)
{
 if (typeof element.innerText != 'undefined' && text != null)
    {
        element.innerText = text;
        element.href += '?agentmls=' + agentId;
        
    }
    else if (typeof element.textContent != 'undefined' && text != '')
    {
        element.textContent = text;
        element.href += '?agentmls=' + agentId;
        
    }
}

function setEmail(element, text)
{
 if (typeof element.innerText != 'undefined' && text != null)
    {
        element.href += text;   
    }
    else if (typeof element.textContent != 'undefined' && text != '')
    {
        element.href += text;   
    }
}
// Add Agent
// From agent details page
function agentSelect(agentId) {
    if (Sys.Services.AuthenticationService.get_isLoggedIn() == true)
    {        
        wsAgent.AddAgent_ForUser(agentId, onSaveAgentSuccess);
    }
    else
    {
        _agentMsg.style.display="block";
    }
}
// From agent page
function onAgentSelect(sender, e)
{
    if (Sys.Services.AuthenticationService.get_isLoggedIn() == true)
    {        
        var agentId = e.get_item().get_dataItem().AgentID;
        wsAgent.AddAgent_ForUser(agentId, onSaveAgentSuccess);
    }
    else
    {
        window.location = "../mywt/default.aspx";
    }
}

function agentDetailsSelectAgent(agentId)
{
    if (Sys.Services.AuthenticationService.get_isLoggedIn() == true)
    {        
        wsAgent.AddAgent_ForUser(agentId, onSaveAgentSuccess);
    }
    else
    {
        window.location = "../mywt/default.aspx";
    }
}

function onSaveAgentSuccess()
{
    window.location.href="/agents/";
}

