<%@language = "javascript" %>
<% /*
     $Revision$
     $Id$
*/ %>

<%
var NO_OTP_OK = true;
var EXPIRED_PASSWORD_OK = true;
var JSON_Result = true;
ANONYMOUS_Allowed = true; // niet klagen als je al uitgelogd was
%>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="./login.inc" -->

<%
var result = { success: true };

if (user_key > 0) // niet klagen als je al uitgelogd was
{
    protectRequest.validateToken();

    // FACFAC tracken we altijd
    if (user.has("WEB_FACFAC"))
        shared.trackaction("PRSLOG", user_key, L("lcl_logged_off").format(Session("ASPFIXATION").slice(-6), Session("fclt_realuser"),Session("last_ip")));
}

if (Session("org_user_key") > 0)
{
    result.return_url = rooturl + "/";
    var was_key = user_key;
    var other_key = parseInt(Session("org_user_key"), 10);
    var other_naam = user.naam();
    doLogin(other_key);
    var since = new Date(Session("org_user_key_ts"));
    var now = new Date();
    var duration = now.getTime() - since.getTime();
    result.message = L("lcl_impersonate_return").format(user.naam());
    shared.trackaction("PRSLOG", was_key, L("lcl_impersonated_end").format(user.naam(), toDateTimeString(since, true, true), Math.round(duration / 10) / 100, Session("ASPFIXATION").slice(-6)));
    shared.trackaction("PRSLOG", other_key, L("lcl_impersonatelog_end").format(other_naam, toDateTimeString(since, true, true), Math.round(duration / 10) / 100, Session("ASPFIXATION").slice(-6)));
    Session.Contents.Remove("org_user_key");
    Session.Contents.Remove("org_user_key_ts");
}
else
{
    result.return_url = S("logoff_return_url");
    var allow_auto_sso = false;
    if (Session("idp_key") > 0)
    {
        var sql = "SELECT aut_idp_remote_logouturl"
                + "  FROM aut_idp"
                + " WHERE aut_idp_key = " + Session("idp_key");
        var oRs = Oracle.Execute(sql);
        if (oRs("aut_idp_remote_logouturl").Value)
        {
            result.return_url = oRs("aut_idp_remote_logouturl").Value;
            allow_auto_sso = true;
        }
        oRs.Close();
    }

    doLogoff(allow_auto_sso);
}
Response.Write(JSON.stringify(result));

%><% ASPPAGE_END(); %>
