Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(updateUserBox);
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequest);

var cntErr = 0;
var updateInterval = 120000;
var postbackElement;

function beginRequest(sender, args) {
    postbackElement = args.get_postBackElement();
}
function updateUserBox()
{
    if(ERAI_New == null || ERAI_New.WebMethods == null)return;
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    if (prm.get_isInAsyncPostBack() ) {
        if (typeof(postbackElement) === "undefined")return;
        if(
                (
                    postbackElement.id.toLowerCase().indexOf('btnsend') > -1 ||
                    postbackElement.id.toLowerCase().indexOf('btndelete') > -1 ||
                    postbackElement.id.toLowerCase().indexOf('btnarchive') > -1
                ) && document.location.href.toLowerCase().indexOf('mailsystem.aspx') > -1
            )
        {
            ERAI_New.WebMethods.UserBoxUpdate(OnSuccess, OnFail);
        }
    }
    else
    {
        ERAI_New.WebMethods.UserBoxUpdate(OnSuccess, OnFail);
    }
}

function signOut()
{
    if(ERAI_New == null || ERAI_New.WebMethods == null)return;
    ERAI_New.WebMethods.SignOut(OnSuccessSignOut, OnFail);
}

function OnSuccessSignOut(result)
{
    top.location.href = result;
}

function OnSuccess(result)
{
    _OnSuccess(result);
    if(document.location.href.toLowerCase().indexOf('mailsystem.aspx') == -1)setTimeout("updateUserBox();", updateInterval);
}

function OnFail(result)
{
    cntErr++;
    //alert("Error: " + result.get_message());
    if(cntErr < 3 && document.location.href.toLowerCase().indexOf('mailsystem.aspx') == -1)setTimeout("updateUserBox();", updateInterval);
}
function _OnSuccess(result)
{
    cntErr = 0;
    if(result == null)
    {
        //signOut();
        return;
    }
    if($get("spPInbox2"))$get("spPInbox2").innerHTML = "(" + result.PInbox + (result.PInboxUnread > 0 ? "/<font style='color:red'>".concat(result.PInboxUnread, "</font>") : "") + ")";
    if($get("spPOutbox2"))$get("spPOutbox2").innerHTML = "(" + result.POutbox + (result.POutboxUnread > 0 ? "/<font style='color:red'>" + result.POutboxUnread + "</font>" : "") + ")";
    if($get("spPEscrows2"))$get("spPEscrows2").innerHTML = "(" + result.PEscrows + (result.PEscrowsUnread > 0 ? "/<font style='color:red'>" + result.PEscrowsUnread + "</font>" : "") + ")";
    if($get("spPPI2"))$get("spPPI2").innerHTML = "(" + result.PPI + (result.PPIUnread > 0 ? "/<font style='color:red'>" + result.PPIUnread + "</font>" : "") + ")";
    if($get("spSInbox2"))$get("spSInbox2").innerHTML = "(" + result.SInbox + (result.SInboxUnread > 0 ? "/<font style='color:red'>" + result.SInboxUnread + "</font>" : "") + ")";
    if($get("spSOutbox2"))$get("spSOutbox2").innerHTML = "(" + result.SOutbox + (result.SOutboxUnread > 0 ? "/<font style='color:red'>" + result.SOutboxUnread + "</font>" : "") + ")";
    if($get("spSEscrows2"))$get("spSEscrows2").innerHTML = "(" + result.SEscrows + (result.SEscrowsUnread > 0 ? "/<font style='color:red'>" + result.SEscrowsUnread + "</font>" : "") + ")";
    if($get("spSPO2"))$get("spSPO2").innerHTML = "(" + result.SPO + (result.SPOUnread > 0 ? "/<font style='color:red'>" + result.SPOUnread + "</font>" : "") + ")";
    if($get("spRReceived2"))$get("spRReceived2").innerHTML = "(" + result.RReceived + (result.RReceivedUnread > 0 ? "/<font style='color:red'>" + result.RReceivedUnread + "</font>" : "") + ")";
    if($get("spRRequested2"))$get("spRRequested2").innerHTML = "(" + result.RRequested + (result.RRequestedUnread > 0 ? "/<font style='color:red'>" + result.RRequestedUnread + "</font>" : "") + ")";

    if($get("spPInbox3"))$get("spPInbox3").innerHTML = "(" + result.PInbox + (result.PInboxUnread > 0 ? "/<font style='color:red'>".concat(result.PInboxUnread, "</font>") : "") + ")";
    if($get("spPOutbox3"))$get("spPOutbox3").innerHTML = "(" + result.POutbox + (result.POutboxUnread > 0 ? "/<font style='color:red'>" + result.POutboxUnread + "</font>" : "") + ")";
    if($get("spPEscrows3"))$get("spPEscrows3").innerHTML = "(" + result.PEscrows + (result.PEscrowsUnread > 0 ? "/<font style='color:red'>" + result.PEscrowsUnread + "</font>" : "") + ")";
    if($get("spPPI3"))$get("spPPI3").innerHTML = "(" + result.PPI + (result.PPIUnread > 0 ? "/<font style='color:red'>" + result.PPIUnread + "</font>" : "") + ")";
    if($get("spSInbox3"))$get("spSInbox3").innerHTML = "(" + result.SInbox + (result.SInboxUnread > 0 ? "/<font style='color:red'>" + result.SInboxUnread + "</font>" : "") + ")";
    if($get("spSOutbox3"))$get("spSOutbox3").innerHTML = "(" + result.SOutbox + (result.SOutboxUnread > 0 ? "/<font style='color:red'>" + result.SOutboxUnread + "</font>" : "") + ")";
    if($get("spSEscrows3"))$get("spSEscrows3").innerHTML = "(" + result.SEscrows + (result.SEscrowsUnread > 0 ? "/<font style='color:red'>" + result.SEscrowsUnread + "</font>" : "") + ")";
    if($get("spSPO3"))$get("spSPO3").innerHTML = "(" + result.SPO + (result.SPOUnread > 0 ? "/<font style='color:red'>" + result.SPOUnread + "</font>" : "") + ")";
    if($get("spRReceived3"))$get("spRReceived3").innerHTML = "(" + result.RReceived + (result.RReceivedUnread > 0 ? "/<font style='color:red'>" + result.RReceivedUnread + "</font>" : "") + ")";
    if($get("spRRequested3"))$get("spRRequested3").innerHTML = "(" + result.RRequested + (result.RRequestedUnread > 0 ? "/<font style='color:red'>" + result.RRequestedUnread + "</font>" : "") + ")";

    var  imbMS = $get(s1);
    var  lblNewMail = $get('lblNewMail2');
    var  lblMailSystem = $get(s2);
    
    if ((result.RRequestedUnread + result.RReceivedUnread + result.SPOUnread + result.SEscrowsUnread + result.SOutboxUnread + result.SInboxUnread + result.PPIUnread + result.PInboxUnread + result.POutboxUnread + result.PEscrowsUnread) == 0)
    {
        imbMS.src = "Images/Icons/mail.png";
        lblNewMail.style.visibility = "hidden";
        lblMailSystem.style.fontWeight = "normal";
        lblMailSystem.style.color = "black";
    }
    else
    {
        imbMS.stc = "Images/Icons/mail-edit.png";
        lblNewMail.style.visibility = "visible";
        lblMailSystem.style.fontWeight = "bold";
        lblMailSystem.style.color = "green";
    }
}

