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

    File:               shared/loadrooms.asp
    Description:        Fetch rooms for 'current' floor
    Parameters:
        user_key        ??Overkill??
        autfunction     a fac_functioncode (eg. "WEB_ALGUSE")
        readmode        0:use ALGreadlevel, 1:use ALGwritelevel
    Context:            LocalScripts.depselect.js/updateRooms()
    Note:               File is called in hidden workframe

*/ %>

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

<html>
  <body onLoad=loaded();>
<%
    var autfunction = getQParam("autfunction", "*");
    var readmode = getQParamInt("readmode", 0);
    var no_room_res = getQParamInt("no_room_res", 0) == 1;
    var room_key = getQParamInt("room_key", -1);
    var add_occupation = getQParamInt("add_occupation", 0) == 1;

    __Log('autfunction ' + autfunction );
    __Log('readmode :' + readmode);

    // De functie checkAutorisation(autfunction) geeft een Response.End indien er geen autorisatie voor autfunction wordt gevonden
    var authparams = user.checkAutorisation(autfunction);
%>  
    <script type="text/javascript">
      room_key = <%=room_key%>;

      function newRoom(f,r,d,b)
      {
        window.parent.Rooms[window.parent.Rooms.length] = new window.parent.makeC(f,r,d,b);
      }

      function loaded()
      {
<%      floor  = getQParamInt("floor", -1);
        if (floor > 0)
        {
          if (no_room_res)
          {
            var v_ruimte_table = ""
                               + "(SELECT r.* "
                               + " FROM alg_v_aanwezigruimte r, "
                               + "   (SELECT alg_ruimte_key "
                               + "    FROM   res_alg_ruimte "
                               + "    WHERE  res_alg_ruimte_verwijder IS NULL "
                               + "    GROUP BY alg_ruimte_key) rr "
                               + " WHERE r.alg_ruimte_key = rr.alg_ruimte_key(+) "
                               + " AND rr.alg_ruimte_key IS NULL)";
          }
          else
          {
            var v_ruimte_table = "ALG_v_aanwezigRUIMTE";
          }

          if (   (autfunction == '' || typeof(autfunction) =='undefined')
                ||(readmode? (ALGreadlevel < 5) : (ALGwritelevel < 5))) // We always have a readable floor!!
          {
            sql = "SELECT alg_verdieping_key"
                + ", alg_ruimte_key"
                + ", alg_ruimte_nr ||"
                  + " ' (' || alg_ruimte_omschrijving || ')' oms"
                + ", COALESCE ((SELECT SUM (ra2.prs_ruimteafdeling_bezetting)"
                  + " FROM prs_ruimteafdeling ra2"
                  + " WHERE ra2.alg_ruimte_key = R.alg_ruimte_key"
                  + " AND ra2.prs_ruimteafdeling_verwijder IS NULL), 0) bezetting"
                + " FROM " + v_ruimte_table + " R"
                + " WHERE alg_verdieping_key = " + floor
                + " ORDER BY 3";
          }
          else
          { // ALGreadlevel==5, we really, really have to use authorisation
            sql = "SELECT alg_verdieping_key "
                + ",      r.alg_ruimte_key "
                + ",      alg_ruimte_nr ||"
                + " ' (' || alg_ruimte_omschrijving || ')'"
                + ", COALESCE ((SELECT SUM (ra2.prs_ruimteafdeling_bezetting)"
                  + " FROM prs_ruimteafdeling ra2"
                  + " WHERE ra2.alg_ruimte_key = R.alg_ruimte_key"
                  + " AND ra2.prs_ruimteafdeling_verwijder IS NULL), 0) bezetting"
	              + " FROM fac_v_my_rooms fr "
	              + ",  " + v_ruimte_table + " R "
	              + " WHERE fr.prs_perslid_key = " + user_key
	              + "   AND fr.niveau = " + (readmode ? ALGreadlevel : ALGwritelevel)
	              + "   AND r.alg_ruimte_key = fr.alg_ruimte_key"
	              + "   AND alg_verdieping_key = " + floor
	              + " ORDER BY 3";
          }
          oRs = Oracle.Execute(sql);
          while (!oRs.eof)
          {
            var bezet = oRs("bezetting");
            var bezetstr = "";
            if (add_occupation)
              bezetstr = " (" + (oRs("bezet").value > 100? 0 : 100 - oRs("bezet").value) + "%)', '" + (oRs("bezet").value > 100? 0 : 100 - oRs("bezet").value);
%>          newRoom(<%=oRs("alg_verdieping_key")%>,
                    <%=oRs("alg_ruimte_key")%>,
                    '<%=replace(oRs("oms"),"'","\\'")%><%=bezetstr%>');
<%
            oRs.MoveNext();
          }
          oRs.close();
%>
          window.parent.rooms_loaded[<%=floor%>] = true;
          window.parent.inc_cc();
          window.parent.SelChange('floor','rooms',window.parent.Rooms,true, ((!isNaN(room_key) && room_key!=-1)?room_key:window.parent.my_room));
          window.parent.updateWorkpl();
          if (window.parent.onRoomsLoaded)
            window.parent.onRoomsLoaded();
<%      } %>
        window.parent.dec_cc();
      }
    </script>
  </body>
</html>
