//
// login.js
//
// Helper functions for handling the login form and the signin / signout buttons.
//

function OnSignin()
{
    $find("LoginPopupExtender").show();
    document.getElementById("ctl00_ctl00_UserName").focus();
}

function DisplayLoginError(message)
{
    var errorSpan = document.getElementById("MasterLoginFailure");
    errorSpan.innerHTML = message;
}

function RefreshSigninButtons(loggedIn, userName)
{
    var elIn = document.getElementById("ctl00_Header1_WhenLoggedIn");
    var elOut = document.getElementById("ctl00_Header1_WhenNotLoggedIn");
    
    if(loggedIn)
    {
        elOut.style.display = "none";
        elIn.style.display = "block";
        
        var elName = document.getElementById("ctl00_Header1_UserName");
        elName.innerText = userName;
    }
    else
    {
        elIn.style.display = "none";
        elOut.style.display = "block";
    }
}

function OnLoginSucceeded(valid, redirectUrl, methodName)
{
    Widigo.AjaxProgress.StopProgress("MasterLoginSubmitButton");
    if(!valid)
    {
        DisplayLoginError("Login Failed.");
        return;
    }

    if(typeof(OnLoggedInStateChange) != "undefined")
    {
        OnLoggedInStateChange(true);
    }

    if((typeof(redirectUrl) != "undefined") && (redirectUrl != null))
    {
        document.location.href = redirectUrl;
    }
    else
    {
        $find("LoginPopupExtender").hide();
    }
}

function OnLoginFailed(error, redirectUrl, methodName)
{
    Widigo.AjaxProgress.StopProgress("MasterLoginSubmitButton");
    DisplayLoginError(error.get_message());
}

function OnTryLogin()
{
    // Clear the login error.
    DisplayLoginError("");
    
    // Start the progress GIF, and do the true login asynchronously so the
    // user has some time to see it.
    Widigo.AjaxProgress.StartProgress("MasterLoginSubmitButton");
    window.setTimeout(OnTryLogin2, 100);
}

function GetRedirectUrl()
{
    if(typeof(LoginRedirectUrl) != "undefined")
    {
        return LoginRedirectUrl;
    }
    else
    {
        return null;
    }
}

function OnTryLogin2()
{
    var name = GetASPServerElement("LoginUserNameId").value;
    var password = GetASPServerElement("LoginPasswordId").value;
    var remember = GetASPServerElement("LoginRememberMeId").checked;
    ProfileMine.WebServices.LoginHandler.Login(name, password, remember, OnLoginSucceeded, OnLoginFailed, GetRedirectUrl());
}

function OnLogoutCompleted(redirectUrl, methodName)
{
    Widigo.AjaxProgress.StopProgress("MasterSignoutButton");

    if(typeof(OnLoggedInStateChange) != "undefined")
    {
        OnLoggedInStateChange(false);
    }
    
    if((typeof(redirectUrl) != "undefined") && (redirectUrl != null))
    {
        document.location.href = redirectUrl;
    }
}

function OnLogoutFailed(error, userContext, methodName)
{
    Widigo.AjaxProgress.StopProgress("MasterSignoutButton");
    alert("Logout failed.  Cause: '" + error.get_message() + "'");
}

function OnTryLogout()
{
    if(typeof(OnPreLogout) != "undefined")
    {
        if(!OnPreLogout())
        {
            return;
        }
    }
    
    Widigo.AjaxProgress.StartProgress("MasterSignoutButton");
    window.setTimeout(OnTryLogout2, 100);
}

function OnTryLogout2()
{
    ProfileMine.WebServices.LoginHandler.Logout( OnLogoutCompleted, OnLogoutFailed );
}

function OnLoginLoad()
{
    if(typeof(OnLoggedInStateChange) == "undefined")
    {
        OnLoggedInStateChange = function(loggedIn) { RefreshSigninButtons(loggedIn, GetASPServerElement("LoginUserNameId").value); };
    }
}

Sys.Application.add_load(OnLoginLoad);
