<% /*
     $Revision$
     $Id$

     File:    fac/header.asp
     Context: iframe vanuit appl/fac/Facilitor.asp

*/
%>
<!-- #include file="../Shared/persoonselector.inc" -->
<!-- #include file="../Shared/productsearch.inc" -->
<%
FCLTHeader.Requires({ plugins: [], js: [] });

// Kijk in welke omgeving we volgens de database zitten,
// en toon dat prominent als dat niet de productie is.
var sql = "SELECT COALESCE(fac_version_otap, 'P') otap, "
        + "       fac.getdbversion() dbversion,"
        + "       fac_version_cust, "
        + "       fac_version_schema "
        + "  FROM fac_version";
var oRs = Oracle.Execute(sql);
// Dit kan onduidelijke problemen geven met putorders en genexport. Controleer daar hier op.
if (!oRs("fac_version_cust").Value)
    abort_with_warning("fac_version_cust is niet gezet in de database. Doe dat eerst.");

var otap = oRs("otap").value;
if (otap == "P") otap = "";
var dbversion = oRs("dbversion").value;
var schemaversion = oRs("fac_version_schema").Value;
var facilitorversion = FCLTVersion + "@" + dbversion;
oRs.Close();

function todayString() {
    var d=new Date()

    return calendar_names.days[d.getDay()] + "&nbsp;" + d.getDate() + "&nbsp;"
            + calendar_names.months[d.getMonth()] + "&nbsp;" + d.getFullYear();
}

function generateHeader()
{
%>
<script type="text/javascript">
    function setGlobalUser (prs_key, prs_naam) {
        FcltMgr.setCaller({prs_key: prs_key, prs_naam: prs_naam});
        if (prs_key > 0)
            FcltMgr.openDetail("appl/fac/fac_user.asp?prs_key=" + prs_key);
    }
<%
    if (S("puo_loglevel") > 1 && Application("otap_environment") != "O" && user.has("WEB_FACTAB"))
    {
        %>
          $(function () { $.toast({ text: "<%="Warning: puo_loglevel={0}".format(S("puo_loglevel"))%>", icon: "warning", position : 'top-left'}); });
        <%
    }
    if (Application("otap_environment") == "O" || Session("logging") > 0)
    {%>
        function startLogging(silent)
        {
            if (!silent)
                $.toast({ text: "Logging is now on", icon: "success", position : 'top-left'});
            $("#loggingon").hide();
            $("#loggingoff,#loggingshow").show();
        }
        function stopLogging()
        {
            $.post("../shared/wislog.asp?logging=0");
            $.toast({ text: "Logging is now off", icon: "success", position : 'top-left'});
            $("#loggingoff,#loggingshow").hide();
            $("#loggingon").show();
        }
     <%
        if (Session("logging") > 0)
            Response.Write("$(function() {startLogging(true) });");
    }
%>
</script>

<div id="headerblok" style='height:100%'>
<%
    if (Application("otap_environment") == "O" || Session("logging") > 0)
    {%>
        <div id="loggers">
            <button id='loggingoff' onclick='stopLogging()'>Stop logging</button>
            <a href='../shared/wislog.asp?logging=1' target='_new'><button id='loggingon' onclick='startLogging()'>Start logging</button></a>
            <% if (Application("otap_environment") == "O") { %>
                <a href='./fac_StreamFile.asp?logger=0' target='_new'><button id='loggingshow'>Open logging</button></a>
            <% } %>
        </div>
     <%
    }
    if (Session("org_user_key") > 0)
    {
        var other_key = parseInt(Session("org_user_key"), 10);
        var other = new Perslid(other_key);
        Response.Write("<div id='userimperson'>" + L("lcl_impersonate_active").format(other.naam() + "</div>"));
    }
    else if (L("lcl_facilitor_header_prefix") || otap)
        Response.Write("<div id='headerprefix'>" + otap+" "+L("lcl_facilitor_header_prefix") + "</div>");

    var klikker = "";
    if (user.has("WEB_FACTAB"))
    {
        klikker = "style='cursor:pointer' onclick='FcltMgr.openDetail(\"appl/fac/fac_admtracking_search.asp\", L(\"lcl_menu_admtracking_search\"))'";
    }
%>
 <div id="headerdate" <%=klikker%> title="<%=L("lcl_facilitor_appl")+' '+facilitorversion%>"><%=todayString()%></div>

<%
  if (user_key != -1) {


    if (parseInt(schemaversion,10) < parseInt(FCLTMinDatabaseSchema,10))
    {
        if (user.has("WEB_FACTAB"))
            Response.Write("<div id='badschema' title='"+Oracle.RealConnection.Properties("User Name")+'@'+Oracle.RealConnection.Properties("Data source")+"'>BAD SCHEMA. Need DB"+FCLTMinDatabaseSchema+" have DB"+schemaversion+"</div>");
        // else: gewone users niet direct lastigvallen.
        //       tijdens een (nieuw ingrijpende) hot-upgrade staan we het gewoon eventjes toe.
    }
    var authparams = user.checkAutorisation("WEB_PHSRCH", true);
    if (S("show_header_productsearch") || (authparams && authparams.PRSreadlevel < 9))
    {
      Response.Write("<div class='headerToggle' onclick='toggleHeaderSearch()' title='"+L("lcl_search")+"'>"
                    + "<i class='fa fa-search fa-fw'></i><i class='fa fa-fw fa-arrow-right'></i>"
                    + "</div>");
    }
    %><div id="headersuggest"><%
      // Suggest om een persoon te vinden. Gebruikt de scope van de telefoongids
      if (authparams && authparams.PRSreadlevel < 9) {
          FCLTpersoonselector("sName_key", "sgPerson", { perslidKey: -1,
                moreinfo: true,
                no_table: true,
                filtercode: "PHB",
                whenEmpty: L("lcl_select_persoon_generic"),
                onChange: "setGlobalUser",
                autlevel: authparams.PRSreadlevel
              });
      } else {
        // spacer
        %><div class="hsspacer">&nbsp;</div><%
      }
%>
</div>
<%
  if (S("show_header_productsearch"))
  {
    Response.Write("<div id=\"headersearch\">");
    FCLTproductsearch ();
    Response.Write("</div>");
  }



   generateHeaderFunctions();
}
   %></div>
   <%
}
function generateHeaderFunctions (params)
{
    %>
  <script type="text/javascript">
    function toggleHeaderSearch()
    {
        var duration = 400;
        var setWidth, setRigth, setVis;
        var show = $(".headerToggle .fa-search").is(":visible");
        if (show)
        {
            setWidth = "340px";
            setRight = "350px";
            setVis = "visible";
            $("#headersuggest .fldpers").css("visibility", setVis);
            $("#headersearch .fldsrch").css("visibility", setVis).focus();
            $("#headersuggest .suggestklikker").css("visibility", setVis);
            $("#headersearch .productklikker").css("visibility", setVis);
            $(".headerToggle .fa-arrow-right").toggle();
            $(".headerToggle .fa-search").toggle();
        }
        else
        {
            setWidth = "0px";
            setRight = "0px";
            setVis = "hidden";
        }

        $("#headersuggest .fldpers").animate({ width: setWidth }, { queue: false, duration: duration });
        $("#headersearch .fldsrch").animate({ width: setWidth }, { queue: false, duration: duration });
        $(".headerToggle").animate({ right: setRight }, duration, function()
        {
            if (!show)
            {
                $("#headersuggest .fldpers").css("visibility", setVis);
                $("#headersearch .fldsrch").css("visibility", setVis);
                $("#headersuggest .suggestklikker").css("visibility", setVis);
                $("#headersearch .productklikker").css("visibility", setVis);
                $(".headerToggle .fa-arrow-right").toggle();
                $(".headerToggle .fa-search").toggle();
            }
        });
    }
    function openHelp()
    {
        if (FcltMgr._pageManager.name == "tabbedManager")
            var src = FcltMgr._pageManager._activeTab().src;
        else
            src = window.location.href;

        var url = 'appl/fac/help.asp?url='+escape(src);
        if (<%=S("help_window")?"false":"true"%>)
            FcltMgr.openDetail(url,'<%=L("lcl_help")%>');
        else
            var newWindow = window.open('../../'+url);
    }

    function myInfo()
    {
        FcltMgr.openDetail('appl/fac/fac_user_info.asp?');
    }

    function toMobile()
    {
          parent.location.href="../../default.asp?pda=1";
    }

    function logOffCallback(json, textStatus)
    {
        parent.location.href = json.return_url;
    };

    function logOff()
    {
      var data = {};
      <% protectRequest.dataToken("data"); %>
      $.post("../shared/LogOff.asp",
             data,
             FcltCallbackAndThen(logOffCallback),
             "json");
    }

    function changeLanguage(lang)
    {
      var data = {};
      <% protectRequest.dataToken("data"); %>
      $.post("./set_language.asp?lang="+lang,
             data,
             FcltCallbackRefresh,
             "json");
    }
    $(document).ready(function()
    {
        $("#headersuggest .suggestklikker").css("visibility", "hidden");
        $("#headersearch .productklikker").css("visibility", "hidden");
        $("#headersuggest .fldpers").css("width", "0px");
        $("#headersearch .fldsrch").css("width", "0px");
        $("#headersuggest .fldpers").css("visibility", "hidden");
        $("#headersearch .fldsrch").css("visibility", "hidden");
        $(".headerToggle").css("right", "0px");
        $(".headerToggle .fa-arrow-right").toggle();
        $("#headersearch .productklikker").on('click', function() {
            $("form#p").trigger("submit");
        });
    });

  </script>
  <div id="headerfunctions">
    <div id="loggedinuser"><%=L("lcl_loggedin_as") + '<span onclick="myInfo()">'+ safe.html(user.naam())+ (user.alg_locatie_key({withcurrent:true})>-1 ? I("fa-map-marker")+(new Locatie(user.alg_locatie_key({withcurrent:true})).locatie_string()) : "") +'</span>' %></div>
    <%
    if (S("language_toggles").length > 1)
    {
    %><div id="langtoggle"><%
       for (lang in S("language_toggles"))
       {
         var code = S("language_toggles")[lang];
         var desc = lcl.languages[code];
       %><img onClick="changeLanguage('<%=code%>');" title="<%=desc%>" src=../pictures/<%=code%>.png><%
       }
    %></div><%
    }
    if (params && params.touch && S("mobile_enabled") == 1)  // we voorkomen nu nog dat je via desktop naar Mobile gaat; touch-only
    {
        %><div id="touch2mobile" onclick="toMobile()"><% = I("fa-mobile")+L("lcl_mobile_mobilemode") %></div> <%
    }
    if (S("helppage_url") != "") {
        %><div id="headerhelp" onclick="openHelp()"><% = I("fa-question-circle")+L("lcl_help") %></div> <%
    }
    %><div id="headerlogout" onclick="logOff()"><% = I("fa-sign-out")+L("lcl_logoff") %></div></div><%
  }
%>