﻿function CanAuthenticate()
{
    try
    {
        var dom = new ActiveXObject("Msxml2.DOMDocument");
        dom.async=false;
        dom.load("/util/autologin/AuthenticationTest.xml");
    }
    catch(e)
    {
        return false;
    }
    return true;
}

var req;
function AutoLoginUser()
{
    if (CanAuthenticate())
    {
        // Update UI notifying user of autologin
        document.getElementById('spanLoginTextRegion').innerHTML = "<div class='loginMessage'>LOGGING IN</div>";
        
        try
        {
            req = new ActiveXObject("Msxml2.XMLHTTP");
            req.onreadystatechange = AutoLoginStaging;
            req.open("GET", "/WorkArea/SSO/autologin.aspx?autoaddtype=Member", true);
            req.send("");
        }
        catch(e)
        {
            //fail autologin
        }
    }
}  

function AutoLoginStaging()
{
    if (req.readyState == 4)
    {
        try
        {
            req = new ActiveXObject("Msxml2.XMLHTTP");
            req.onreadystatechange = FinalizeLogin;
            req.open("GET", "/Workarea/login.aspx?autoaddtype=Member", true);
            req.send("");                    
        }
        catch(e)
        {
            //fail autologin
        }
        
    }
}

function FinalizeLogin()
{
    if (req.readyState == 4)
    {
        if (document.readyState == "complete")
        {
            // try to replace (not refresh) browser window to update control status
            window.location.replace(window.location.href);
        }
    }
}