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

    File:           prs_perslid_otp.asp
    Description:    Genereert en toont de *bestaande* QRcode voor de Google Authenticator
    Parameters:
    Context:
    Note:           Vooralsnog niet bedoeld voor het grote publiek
*/ %>

<!--#include file="../Shared/common.inc"-->
<!--#include file="../Shared/iface.inc"-->
<!--#include file="../Shared/login.inc"-->
<!--#include file="../Shared/encoding.inc"-->
<%
var otpsecret = user.otpsecret();
if (!otpsecret)
{
    Response.Redirect("prs_perslid_otp_new.asp");
    Response.End;
}
var otpresult = otpcodes(otpsecret);
%>
<html>
  <head>
<%  FCLTHeader.Generate();
    var progressWidth = 200;
 %>

    <script type='text/javascript'>
        function updateProgress(ppos)
        {
            var pp = $("#progress");
            var pw = <%=progressWidth%>;
            var ww = Math.max(1, ppos);
            if (ww >= pw)
                FcltMgr.reload();
            pp.css('borderLeftWidth', ww + "px");
            pp.width(pw - ww);
            setTimeout("updateProgress("+(ppos+1)+")", 1000 * <%=otpresult.otpstep%> / pw);
        }
        function clear_otp()
        {
            if (confirm(L("lcl_R_U_sure")))
            {
                var data = { };
                <% protectRequest.dataToken("data"); %>
                $.post("prs_perslid_otp_clear.asp",
                       data,
                       FcltCallbackClose,
                       "json");
            }
        }
        function new_otp()
        {
            window.location.href = "prs_perslid_otp_new.asp";
        }
    </script>
    <style>
        geenhekjeaanbeginregel,#progress {
            width: <%=progressWidth%>px;
            border: 1px solid #888;
        }
        span.otptokencurrent span.otpnumber {
            font-size: 3em;
            line-height: 1em;
        }
    </style>
  </head>

  <body class="modal" id="mod_authQR">
<%
    BLOCK_START("", L("lcl_otp_authenticatie_activate"));
    var secret32 = binary_to_base32(hex_to_binary(otpresult.otpseed));
%>

    <div id="myQR"><%=L("lcl_otp_auth_header").format(secret32)%></div>
        <center><img class="QRC" src='./qrcodeotp.asp?size=4'><br></center>
<%
if (user.checkAutorisation("WEB_FACFAC", true))
{
    Response.Write("Omdat je WEB_FACFAC hebt zie je onderstaande:<table><tr><td>");
    for (var i = 0; i < otpresult.codes.length; i++)
    {
        var otpshould = otpresult.codes[i].otpshould;
        var dtsfrom = otpresult.codes[i].dtsfrom;
        var dtsto = otpresult.codes[i].dtsto;
        var cls = 'otptoken';
        if (otpresult.codes[i].offset == 0)
        {
            cls = 'otptokencurrent';
        }
        //Response.Write("<span class='{3}'>OTP from {0} to {1} should be <span class='otpnumber' style='font-weight:bold'>{2}</span></span>{4}<br>".format(toTimeString(dtsfrom, true), toTimeString(dtsto, true), otpshould, cls));
        Response.Write("<span class='{1}'><span class='otpnumber' style='font-weight:bold'>{0}</span></span><br>".format(otpshould, cls));
    }
    Response.Write("</td><td style='vertical-align:middle'><div id='progress'>&nbsp;</div></td></tr></table>");
    BLOCK_END();

}

    var buttons =  [ { title: L("lcl_clear_otp"), action: "clear_otp()" },
                     { title: L("lcl_new_otp"), action: "new_otp()" },
                     { title: L("lcl_cancel"), icon: "cancel.png", action: "gen_cancel()" }
                   ];
    CreateButtons(buttons);

    IFACE.FORM_END();

%>
    <script>
        updateProgress(<%=Math.floor(otpresult.tokenpassed * progressWidth)%>);
    </script>
  </body>
</html>
