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

    File:               edit_bezetting.asp
    Description:        Toon het popupschermpje om een bezetting te laten bewerken.
    Parameters:
        pwp_key         existing prj_perslidwerkplek.prj_perslidwerkplek
                        (NOOT: Voor de hand ligt om alle bezettingen van die werkplek te doen?)
    Context:            Verhuizingen, wijzig bezetting in 'nieuwe bezetting' tabblad
                        Altijd in context/als IFRAME van ModalForm.asp
*/ %>

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

<%
FCLTHeader.Requires({ js: ["num2curr.js"]})

var autfunction = "WEB_PRJBOF";
var authparams = user.checkAutorisation(autfunction);

%>
<html>
  <head>
<%  FCLTHeader.Generate(); %>

<%  var pwp_key = getQParamInt("pwp_key");           // Werkplek key
    var doUpdate = getFParamInt("doUpdate", 0) == 1; // Moet ik bijwerken? (dan autoclose)
    var sql = "";

    if (doUpdate)
    {
      protectRequest.validateToken();
      var wp_bez = getFParamInt("wp_bez");
      var wp_oms = getFParam("wp_oms");
      var wp_seq = getFParamInt("wp_seq");
      var wp_key = getFParamInt("wp_key");
      var wp_vast = (getFParam("wp_vast") == "vast")? "1" : "null";
      var wp_opp = "null";
      if (wp_vast == "1")
      {
          var wp_opp = getFParamFloat("wp_opp");
          if (isNaN(wp_opp)) wp_opp = "null";
      }

      try
      {
          sql = "UPDATE PRJ_PERSLIDWERKPLEK"
              + "   SET PRJ_PERSLIDWERKPLEK_BEZETTING = " + wp_bez
              + "   WHERE PRJ_PERSLIDWERKPLEK_KEY = " + pwp_key;
          Oracle.Execute(sql);

          sql = "UPDATE PRJ_WERKPLEK"
              + "   SET PRJ_WERKPLEK_OMSCHRIJVING = " + safe.quoted_sql(wp_oms) + ", "
              + "       PRJ_WERKPLEK_VOLGNR = " + wp_seq + ","
              + "       PRJ_WERKPLEK_vastopp = " + wp_vast + ","
              + "       PRJ_WERKPLEK_opp = " + wp_opp
              + "   WHERE PRJ_WERKPLEK_KEY = " + wp_key;
          Oracle.Execute(sql);
      }
      catch( e1 )
      {
          %><div id=myerror>Wijzigen niet gelukt: <%=e1.description%></div><script>alert(myerror.innerHTML)</script><%
      }

      %><script>FcltMgr.closeDetail(window, { refresh: true } );</script><%
      Response.End();
    }
%>
  </head>

  <body class="modal" id="prjeditbezet"
        onLoad="document.forms.u2.wp_bez.focus();document.forms.u2.wp_bez.select();">
<%

    // Nu wat info er bij/omheen zoeken
    sql = "SELECT    w.prj_werkplek_key, prj_perslidwerkplek_key, prj_perslidwerkplek_bezetting, "
        + "          prj_werkplek_volgnr, prj_werkplek_omschrijving,"
        + "          prj_werkplek_vastopp,prj_werkplek_opp,wpopp,"
        + "          alg_l.alg_locatie_omschrijving,"
        + "          alg_g.alg_gebouw_omschrijving,"
        + "          alg_v.alg_verdieping_omschrijving,"
        + "          prj_r.prj_ruimte_nr,"
        + "          prj_r.prj_ruimte_omschrijving,"
        +            S("prs_pers_string") + " naam, "
        +            S("prs_dep_string") + " afdnaam"
        + "  FROM alg_locatie alg_l, "
        + "       alg_gebouw alg_g, "
        + "       alg_verdieping alg_v, "
        + "       prj_ruimte prj_r, "
        + "       prj_werkplek w, "
        + "       prj_perslidwerkplek pw, "
        + "       prs_perslid p, "
        + "       prs_v_afdeling d, "
        + "       prj_v_werkplek_oppervlakte pwo "
        + " WHERE pw.prj_perslidwerkplek_key = " + pwp_key
        + "   AND pw.prs_perslid_key = p.prs_perslid_key(+) "
        + "   AND alg_v.alg_verdieping_key = prj_r.alg_verdieping_key "
        + "   AND alg_g.alg_gebouw_key = alg_v.alg_gebouw_key "
        + "   AND alg_l.alg_locatie_key = alg_g.alg_locatie_key "
        + "   AND prj_r.prj_ruimte_key = w.prj_ruimte_key "
        + "   AND w.prj_werkplek_key = pwo.prj_werkplek_key "
        + "   AND w.prj_werkplek_key = pw.prj_werkplek_key "
        + "   AND w.prj_werkplek_verwijder IS NULL "
        + "   AND d.prs_afdeling_key = p.prs_afdeling_key"

    oRs = Oracle.Execute( sql );
%>
    <script type="text/javascript">
      function doSubmit()
      {
        if ($("#wp_bez").val() != "")
          document.forms.u2.submit();
      }
      function checkOccupation(evt)
      {
        evt = evt || window.event;
        var obj = evt.srcElement || evt.target;
        var anum=/(^\d+$)/
        var retVal = anum.test(obj.value);
        if (retVal)
        {
          var ival = parseInt(obj.value,10);

          if (!isNaN(ival) && ival >= 0 && ival <= 100)
            retVal = true;
          else
            retVal = false;
        }
        if (!retVal)
        {
          alert(L("lcl_prs_wp_bad_occupation"));
          obj.focus();
        }
        return retVal;
      }

      function enableArea()
      {
          if ($("#wp_vast:checked").val() == "vast")
          {
            $("#wp_opp").attr("readonly", false);
          }
          else
          {
            $("#wp_opp").attr("readonly", true);
            $("#wp_opp").val("");
          }
      }

      function checkSeq(evt)
      {
        evt = evt || window.event;
        var obj = evt.srcElement || evt.target;
        if( obj.value == "" || !isGoodNumber(obj.value,true,true,-1,-1))
        {
          alert(L("lcl_estate_wp_bad_seq"));
          obj.focus();
          return false;
        }
        return true;
      }

      function checkArea()
      {
        if ($("#wp_opp").val() != ""
             && !isGoodNumber($("#wp_opp").val(),false,true,-1,-1)
             && parseInt($("#wp_opp").val(),10) > 0
             && parseInt($("#wp_opp").val(),10) < 999999.99)
        {
          alert(L("lcl_estate_wp_bad_area"));
          return false;
        }
        return true;
      }
    </script>

    <form name="u2" method="post">
      <INPUT type='hidden' name='pwp_key' id='pwp_key' value='<%=pwp_key%>'>
      <INPUT type='hidden' name='wp_key' id='wp_key' value='<%=oRs("prj_werkplek_key")%>'>
      <INPUT type='hidden' name='doUpdate' id='doUpdate' value='1'>
<%    BLOCK_START("prjBezetting", L("lcl_prj_perslidwerkplek_edit")); %>
      <tr>
        <td width="50%">
          <table width="100%" border='0'>

<%
            ROFIELDTR("fld", L("lcl_building"), oRs("alg_gebouw_omschrijving").value);
            ROFIELDTR("fld", L("lcl_floor"), oRs("alg_verdieping_omschrijving").value);
            ROFIELDTR("fld", L("lcl_room"), oRs("prj_ruimte_nr").value + " (" + oRs("prj_ruimte_omschrijving").Value + ")");

            RWFIELDTR("wp_seq", "fldshort", L("lcl_estate_wp_seq"), oRs("prj_werkplek_volgnr").value, {html: 'onblur="return checkSeq(event);"', reuired: true, datatype: "number"});
            RWFIELDTR("wp_oms", "fld", L("lcl_workplace"), oRs("prj_werkplek_omschrijving").value, {maxlength: 30});
%>
            <tr>
              <!-- Vast oppervlakte -->
              <td class="label"><label><%=L("lcl_estate_wp_fixed")%></label></td>
              <td><input type="radio" id="wp_var" name="wp_vast" value="var" onclick="enableArea()" <%=(oRs("prj_werkplek_vastopp").value == null)? "CHECKED" : ""%>><%=L("lcl_estate_wp_fixed_v")%>
                  <input type="radio" id="wp_vast" name="wp_vast" value="vast" onclick="enableArea()" <%=(oRs("prj_werkplek_vastopp").value == null)? "" : "CHECKED"%>><%=L("lcl_estate_wp_fixed_f")%>
              </td>
            </tr>
<%
            if (oRs("prj_werkplek_vastopp").value==null)
              RWFIELDTR("wp_opp", "fldcntopp", L("lcl_estate_wp_area"), oRs("wpopp").value, {readonly: true});  // kan enabled worden, dus geen rofieldtr()
            else
              RWFIELDTR("wp_opp", "fldcntopp", L("lcl_estate_wp_area"), oRs("wpopp").value, {html: 'onblur="return checkArea();"'});
%>
          </table>
        </td>
        <td>
          <table width="100%" border='0'>
<%
            ROFIELDTR("fld", L("lcl_name"), oRs("naam").Value);
            ROFIELDTR("fld", L("lcl_department"), oRs("afdnaam").Value);
            RWFIELDTR("wp_bez", "fldprocent", L("lcl_occupation_perc"), oRs("prj_perslidwerkplek_bezetting").Value, {html: 'onblur="return checkOccupation(event);"'});
%>
          </table>
        </td>
      </tr>
<%
      oRs.close();
      BLOCK_END();
      buttons = [{ title: L("lcl_submit"), action: "doSubmit()"},
                 { title: L("lcl_cancel"), action:"FcltMgr.closeDetail(window, {refresh: false})"} ];
      CreateButtons(buttons)
IFACE.FORM_END();
%>
    </form>
  </body>
</html>
