<% /*
     $Revision$
     $Id$

     File:          dienstselector.inc
     Description:   Algemene interface functie om een diensten suggestbox te maken
                    Deze moet je kunnen includen en aanroepen.
     parameters     fieldName:    veldnaam van het dienstKey veld. Dit veld wordt gesubmit.
                    objectName:   naam van het suggest object dat wordt aangemaakt.
                    params:       object met meegegeven parameters.
*/ %>

<%
FCLTHeader.Requires({ plugins: ["suggest", "jQuery"], js: [] });

function FCLTdienstselector(fieldName, objectName, params)
{
  if (typeof params=="undefined") params = {};
  var isMobile = ("mobile" in params) ? params.mobile : (typeof mobile != "undefined");
  if (!(params.dienstKey && params.dienstKey > 0) && params.readonly && params.suppressEmpty) return;

  var lwhenEmpty = (params.whenEmpty != null ? params.whenEmpty :  (params.readonly ? L("lcl_select_readonly") : L("lcl_select_dienst")));
  var ldienstName = "";
  var lextraInit = "";
  if (params.dienstKey && params.dienstKey > 0)
  {
    sql = "SELECT  prs_dienst_key"
        + ",  " + lcl.xsqla('prs_dienst_omschrijving', 'prs_dienst_key')
        + "     FROM prs_dienst "
        + "    WHERE prs_dienst_key = " + params.dienstKey;
    var oRs = Oracle.Execute(sql);
    if (!oRs.eof)
        ldienstName = oRs("prs_dienst_omschrijving").value;
     else
        params.dienstKey = -1;  // toch invalid
    oRs.Close(); // close the recordset
  }

  // Bouw de url voor de persoons query op
  var functieQueryUrl = rooturl + "/appl/shared/suggest/SuggestDienst.asp?x=x"
                  + (params.filtercode? "&filtcode=" + params.filtercode : "");
  if (params.label)
  {
    if (isMobile) { %>
        <label <%=params.trhidden? "style='display:none'" : ""%>><%=params.label%></label>
<%  } else { %>
    <tr <%=params.trclass?"class='"+params.trclass+"'":""%>>
    <td class="label">
      <label for="<%=fieldName%>_show"><%=params.label%></label>
    </td>
    <td>
<%  }
  } %>
  <div<%=isMobile ? ' class="input-icon-wrapper"' : ''%>>
    <input id="<%=fieldName%>_show"
           class="fld<%=!params.readonly&&params.required? " required" : ""%><%=isMobile ? MOBILE_UI_CLASSES.input : ""%>"
           value="<%=safe.htmlattr(ldienstName)%>"
           placeholder="<%=safe.htmlattr(lwhenEmpty)%>"
           <%=params.readonly? " readonly tabindex=-1" : ""%>
           <%=params.onChange && !params.readonly? " sgonChange='" + params.onChange + "'" : ""%>>
    <input type="hidden" id="<%=fieldName%>" name="<%=fieldName%>" value="<%=params.dienstKey%>">
<%
    if (params.extraParamField)
    { /* het extra parameter veld wordt door suggest object gevuld */%>
      <input type="hidden" id="<%=params.extraParamField%>" name="<%=params.extraParamField%>" value="<%=safe.htmlattr(lextraInit)%>">
<%
    }
%></div><%

  if (params.label)
  {
    if (!isMobile)
    { %>
      </td></tr>
<%  }
  }
  if (!params.readonly)
  {
    if (params.urlAdd)
    {
      var urlAddTxt = "[";
      for (var i = 0; i < params.urlAdd.length; i++)
      {
        urlAddTxt += (i > 0? ", " : "") + "{ urlParam: \"" + params.urlAdd[i].urlParam + "\", field: \"" + params.urlAdd[i].field + "\" }";
      }
      urlAddTxt += "]"
    }
%>
    <script type="text/javascript">
      $(function()
      {
        <%=objectName%> = new Suggest({ objectName: "<%=objectName%>",
                                        queryField: $("#<%=fieldName%>_show")[0],
                                        queryUrl: "<%=functieQueryUrl%>",
                                        initKey: <%=params.dienstKey? params.dienstKey : -1%>,
                                        icon: "fa-briefcase",
                                        keyField: $("#<%=fieldName%>")[0]
                                        <%=params.urlAdd? ", urlAdd: " + urlAddTxt : ""%>
                                        <%=params.extraParamField? ", extraParamField: $(\"" + "#" + params.extraParamField + "\")[0]" : ""%>
                                        <%=params.extraParamField? ", initExtraParam: \"" + safe.jsstring("" + lextraInit) + "\"": ""%>
                                        <%=params.isBad? ", isBad: true": ""%>
                                        <%=params.readonly? ", fieldReadonly: true": ""%>
                                      });
      });
    </script>
<%
  }
}
%>