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

    File:               fac_edit_groeprechten.asp
    Description:        Toon een dialoog om rechten te bewerken
    Parameters:
        fac_groep
        fac_functie
        ins_discipline
    Context:            Popup vanuit fac_groep/fac_functie/ins_tab_discipline
    Note:
*/
 %>
<!-- #include file="../scf/scaffolding.inc" -->
<!-- #include file="../mgt/mgt_tools.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../api2/model_fac_groeprechten.inc" -->
<%
var groep_key = getQParamInt("fac_groep");
var functie_key = getQParamInt("fac_functie");

user.checkAutorisation("WEB_PRSSYS");

var sql = "SELECT * "
        + "  FROM fac_functie"
        + " WHERE fac_functie_key = " + functie_key;
var oRs = Oracle.Execute(sql);
var flevel = oRs("fac_functie_min_level").Value;
var fdisc = oRs("fac_functie_discipline").Value;
if (fdisc)
    var disc_arr_key = getQParamIntArray("ins_discipline");
var fdesc = oRs("fac_functie_omschrijving").Value;
var finfo = oRs("fac_functie_info").Value;
oRs.Close();

var sql = "SELECT * "
        + "  FROM fac_groep"
        + " WHERE fac_groep_key = " + groep_key;
var oRs = Oracle.Execute(sql);
var gdesc = oRs("fac_groep_omschrijving").Value;
var gremark = oRs("fac_groep_opmerking").Value;
oRs.Close();

var transitParam = buildTransitParam(["fac_groep", "fac_functie", "ins_discipline"]);

var prsread = 9;
var prswrite = 9;
var algread = 9;
var algwrite = 9;
var sql = "SELECT *"
        + "  FROM fac_groeprechten"
        + " WHERE fac_groep_key = " + groep_key
        + "   AND fac_functie_key = " + functie_key;
if (fdisc)
    sql += " AND ins_discipline_key = " + disc_arr_key[0];
var oRs = Oracle.Execute(sql);
if (!oRs.Eof)
{
    var prsread = oRs("fac_gebruiker_prs_level_read").Value;
    var prswrite = oRs("fac_gebruiker_prs_level_write").Value;
    var algread = oRs("fac_gebruiker_alg_level_read").Value;
    var algwrite = oRs("fac_gebruiker_alg_level_write").Value;
}
oRs.Close();


function radioTD(name, LOV, val)
{
    Response.Write("\n<td style='width:130px'>");
    for (var i in LOV)
    {
        Response.Write("\n<label><input id=\"" + name + "\" name=\"" + name + "\" type=\"radio\" value=\"" + i + "\""
                      + ((i == val)?" checked":"")
                      +">");
        Response.Write(safe.html(LOV[i]) + "</label><br>");

    }
    Response.Write("</td>");
}
%>
<html>
  <head>
<%  FCLTHeader.Generate()
%>
    <script>
      function grp_submit()
      {
        if (!validateForm("u2"))
          return false;

        $.post($("form[name=u2]")[0].action, $("[name=u2]").serialize(), FcltCallbackClose, "json");
        return true; // disable button
      }
      function auth_advanced(adv)
      {
        window.location.href = "fac_edit_groeprechten.asp?advanced=" + adv + "<%=transitParam%>";
      }
      function onchange ()
        {
            var val = $(this).val();
            switch (this.id)
            {
                case "allread":
                    if ($("[name=allwrite]:checked").val() < val)
                        $("[name=allwrite][value="+val+"]").prop('checked', true); // FireFox doet hier niets als ik #prswrite gebruik?
                    break;
                case "allwrite":
                    if ($("[name=allread]:checked").val() > val)
                        $("[name=allread][value="+val+"]").prop('checked', true);
                    break;
                case "prsread":
                    if ($("[name=prswrite]:checked").val() < val)
                        $("[name=prswrite][value="+val+"]").prop('checked', true); // FireFox doet hier niets als ik #prswrite gebruik?
                    if (val == 9)
                        $("#algread[value=9],#algwrite[value=9]").prop('checked', true);
                    if (val < 9)
                    {
                        var algread = $("[name=algread]:checked").val();
                        if (algread == 9)
                            $("[name=algread][value=-1]").prop('checked', true);
                    }
                    break;
                case "prswrite":
                    if ($("[name=prsread]:checked").val() > val)
                    {
                        var prs = $("[name=prsread][value="+val+"]").prop('checked', true);
                        onchange.apply(prs[0]);
                    }
                    if (val < 9)
                    {
                        var algwrite = $("[name=algwrite]:checked").val();
                        if (algwrite == 9)
                        {
                            var algread = $("[name=algread]:checked").val()
                            var alg = $("[name=algwrite][value=" + algread + "]").prop('checked', true);
                            onchange.apply(alg[0]);
                        }
                    }
                    else
                        $("[name=algwrite][value=9]").prop('checked', true);
                    break;
                case "algread":
                    if (val < 9)
                    {
                        var prsread = $("[name=prsread]:checked").val();
                        if (prsread == 9)
                            $("[name=prsread][value=-1]").prop('checked', true);
                    }
                    if ($("[name=algwrite]:checked").val() < val)
                        $("[name=algwrite][value="+val+"]").prop('checked', true);
                    if (val == 9)
                        $("#prsread[value=9],#prswrite[value=9]").prop('checked', true);
                    break;
                case "algwrite":
                    if ($("[name=algread]:checked").val() > val)
                    {
                        var alg = $("[name=algread][value="+val+"]").prop('checked', true);
                        onchange.apply(alg[0]);
                    }
                    if (val < 9)
                    {
                        var prswrite = $("[name=prswrite]:checked").val();
                        if (prswrite == 9)
                        {
                            var prsread = $("[name=prsread]:checked").val()
                            var prs = $("[name=prswrite][value=" + prsread + "]").prop('checked', true);
                            onchange.apply(prs[0]);
                        }
                    }
                    else
                        $("[name=prswrite][value=9]").prop('checked', true);
                    break;
            }

        }
      $(function ()
        {   // Zorg dat er altijd zinvolle combinaties overblijven
            $("#prsread,#prswrite,#algread,#algwrite,#allread,#allwrite").change(onchange)
        }
      )
    </script>
  </head>
  <body class="modal" id="mod_grprecht">
    <form name="u2" action="fac_edit_groeprechten_save.asp?x=x<%=transitParam%>" method="post">
<%  BLOCK_START("grpRechten", L("lcl_auth_header"));

        ROFIELDTR("fldtxt", L("fac_groep"), gdesc);
        ROFIELDTR("fldtxt", L("fac_groep_opmerking"), gremark, { suppressEmpty: true} );
        ROFIELDTR("fldtxt", L("fac_functie"), fdesc);
        ROFIELDTR("fldtxt", L("fac_functie_info"), finfo);
        if (fdisc)
        {
            if (disc_arr_key.length == 1)
                ROFIELDTR("fldtxt", L("ins_discipline_key"), Oracle.Get("ins_discipline_omschrijving", "ins_tab_discipline", "ins_discipline_key", disc_arr_key[0]));
            else
                ROFIELDTR("fldtxt", L("ins_discipline_key"), "(" + disc_arr_key.length + ")");
        }
        var prsLOV = api2.splitLOV(buildPrsLevelLOV());
        var algLOV = api2.splitLOV(buildAlgLevelLOV());
        var jnLOV = { "-1": L("lcl_Yes"), "9": L("lcl_No") };

        //  0 is Ja/Nee
        //  1 is Ja/Nee (alleen FAC_FAQUSE en PRS_MODAAN)
        //  3 is Lees Ja/Nee en Schrijf Ja/Nee
        //  7 is PRS lezen/schrijven WEB_FACMAN en WEB_BEZPAF
        // 11 is ALG lezen/schrijven diversen
        // 13 is PRS+ALG lezen WEB_???BAC
        // 15 is PRS+ALG lezen/schrijven diversen
        // Ofwel: +1 is bedacht als afzonderlijke leesrechten. Die is wat onzinnig
        //        +2 is afzonderlijke schrijfrechten
        //        +4 is PRS-scope
        //        +8 is ALG-scope
        var advanced = getQParamInt("advanced", S("auth_edit_advanced")?1:0) == 1;
        if (   prsread != algread
            || prswrite != algwrite
            || prsread != -1 && prsread != 9
            || algread != -1 && algread != 9
           )
        {
            advanced = true;
            var no_simpel = true;
        }

        if (!advanced)
        {
            Response.Write("<input type='hidden' name='simple_mode' value='1'>");
            if (flevel > 1)
                ROFIELDTR("fldtxt", L("fac_functie_min_level"), api2.splitLOV(L("fac_functie_min_levelLOV"))[flevel]);
            Response.Write("<tr><td></td><td><table>");
            if (flevel & 2)
                Response.Write("<tr><th>" + L("fac_gebruiker_lezen") + "</th><th>" + L("fac_gebruiker_schrijven") + "</th></tr>");
            else
                Response.Write("<tr><th>" + L("fac_gebruiker_toegang") + "</th></tr>");
            radioTD("allread", jnLOV, algread)
            if (flevel & 2)
                radioTD("allwrite", jnLOV, algread)
            Response.Write("</tr></table></td></tr>");
        }
        else
        {
            if (flevel == 15)
                Response.Write("<tr><td colspan='2'><table><tr>");
            else
                Response.Write("<tr><td></td><td><table><tr>");
            if (!(flevel & (4 | 8))) // niet gescoped
            {
                if (flevel & 2) // Schrijfrechten
                    Response.Write("<tr><th>" + L("fac_gebruiker_lezen") + "</th><th>" + L("fac_gebruiker_schrijven") + "</th></tr>");
                radioTD("prsread", jnLOV, algread);
                if (flevel & 2) // Schrijfrechten
                    radioTD("prswrite", jnLOV, algwrite);
            }
            else
            {
                if (flevel & (4 | 8)) // scope kopjes
                {
                    Response.Write("<tr>");
                    var spanner = "";
                    if (flevel & 2) // read/write kopjes
                        spanner = " colspan='2' style='text-align:center'";
                    if (flevel & 4)
                        Response.Write("<th" + spanner + ">" + L("lcl_prs_organisatie") + "</th>");
                    if (flevel & 8)
                        Response.Write("<th" + spanner + ">" + L("lcl_place") + "</th>");
                    Response.Write("</tr>");
                }
                if (flevel & 2) // read/write kopjes
                {
                    Response.Write("<tr>");
                    Response.Write("<th>" + L("fac_gebruiker_lezen") + "</th><th>" + L("fac_gebruiker_schrijven") + "</th>");
                    if ((flevel & 4) && (flevel & 8))
                        Response.Write("<th>" + L("fac_gebruiker_lezen") + "</th><th>" + L("fac_gebruiker_schrijven") + "</th>");
                    Response.Write("</tr>");
                }

                if (flevel & 4) // PRS
                {
                    radioTD("prsread", prsLOV, prsread);
                    if (flevel & 2) // Schrijfrechten
                        radioTD("prswrite", prsLOV, prswrite);
                }
                if (flevel & 8) // ALG
                {
                    radioTD("algread", algLOV, algread);
                    if (flevel & 2) // Schrijfrechten
                        radioTD("algwrite", algLOV, algwrite);
                }
            }
            Response.Write("</tr></table></td></tr>");
        }
    BLOCK_END();
    buttons = [];
    if (flevel > 1)
    {
        if (advanced)
        {
            if (!no_simpel)
                buttons.push ( {title: L("lcl_auth_simple"),  action:"auth_advanced(0)" } );
        }
        else
            buttons.push ( {title: L("lcl_auth_advanced"),  action:"auth_advanced(1)" } );
        }
    buttons.push({ title: L("lcl_submit"), action: "grp_submit()" });
    buttons.push({ title: L("lcl_cancel"), action: "gen_cancel()" });

    CreateButtons(buttons);
    IFACE.FORM_END();
%>
    </form>
  </body>
</html>