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

    File:           bez/bez_import_bezoeker.asp
    Description:    Importeer bezoekers
    Parameters:
    Context:        Puur clientside handige manier om te vullen
    Note:


*/ %>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../BEZ/bez_flexkenmerk.inc" -->
<!-- #include file="../Shared/json2.js" -->

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

var novervang = getQParamInt("novervang", 0) == 1;

// Clientside, autorisatie is niet relevant

// De kenmerken van bezoekers ophalen.
var sql = getSqlBezFlex(-1);
var oRs = Oracle.Execute(sql);
var flexk = [];
var nr = 0;
while (!oRs.eof)
{
  if (oRs("kenmerk_toonbaar").Value != 1 && oRs("kenmerk_kenmerktype").Value != "Q")
  {
    nr++;
    switch (oRs("kenmerk_kenmerktype").Value)
    { // We ondersteunen de text, numerieke en datum velden.
      case "C":
      case "N":
      case "D": flexk.push({oms: oRs("kenmerk_omschrijving").Value, ktype: oRs("kenmerk_kenmerktype").Value, nr: nr}); break;
      default:  break;
    }
  }
  oRs.MoveNext();
}

%>
  <html>
  <head>
<%
      FCLTHeader.Generate();
%>
      <script type="text/javascript">
        var flexk = <%Response.Write("eval('(" + safe.jsstring(JSON.stringify(flexk)) + ")')"); %>;
        jQuery(document).ready(function()
                               {
                                 FcltMgr.fixActiveelementError();
                                 $("#bezimport").focus();
                               });

        var timerID=null;
        function delayed(delay, fn)
        {
            if (timerID) clearTimeout(timerID);
            timerID = setTimeout(fn, delay);
        }
        function bez_recalc()
        {
            delayed(250, bez_analyze);
        }
        function trim(s)
        {
            return s.replace(/^\s+|\s+$/g,"");
        }
        // Decodeer de ingevoerde bezoekers
        function bez_analyze(noResize)
        {
            var txt = $("#bezimport").val();
            var replace = $("#bez_replace").prop("checked");
            var data = { bezoekers: [], replace: replace};
            // Welke scheider zullen we gebruiken?
            var isep = $("#select_sep").val();
            if (isep == "0")
            {   // autofilter
                var bed_sep="\t";
                var bed_cnt=txt.split(bed_sep).length;
                if (txt.split(";").length > bed_cnt)
                {
                    var bed_sep=";";
                    var bed_cnt=txt.split(bed_sep).length;
                }
                if (txt.split(",").length > bed_cnt)
                {
                    var bed_sep=",";
                    var bed_cnt=txt.split(bed_sep).length;
                }
            }
            else
                var bed_sep = { "1": ";", "2": ",", "3": "\t"}[isep];

            var regels = txt.split(/[\n\r]/); // newline scheiders
            var i;
            var bed_cnt = 0;
            for (i in regels)
            {
                var regel = regels[i];
                var spl = regel.split(bed_sep);
                if (trim(spl[0]) != "")
                {
                  var bez = {};
                  bez.naam = trim(spl[0]); // De eerste
                  if (spl.length > 1 && trim(spl[1]) != "")
                  {
                      bez.bedrijf = trim(spl[1]); // De tweede
                      bed_cnt++;
                  }

                  // Nu de flexkenmerken.
                  // Alle flexkenmerken toevoegen m.b.v. een deep copy i.v.m Objecten (references) in de array (Copying array by value).
                  // Daarom extend gebruiken i.p.v. slice.
                  bez.flex = $.extend(true, [], flexk);
                  if (spl.length > 2)
                  {
                    for (var j = 0; j < bez.flex.length; j++)
                    {
                      if (spl.length <= j + 2)
                        break;
                      else if (trim(spl[j + 2]) != "")
                        bez.flex[j].value = trim(spl[j + 2]);
                    }
                  }
                  data.bezoekers.push(bez);
                }
            }
            $("#fldcount").text(L("lcl_bez_import_cntbez") + data.bezoekers.length + L("lcl_bez_import_cntbed") + bed_cnt);
            if (!noResize) // FSN#21812: anders problemen vanuit bez_submit
                FcltMgr.resized()
            return data;
        }

        function bez_submit()
        {
            var data = bez_analyze(true);
            FcltMgr.closeDetail(window, data);
            return true; // disable button
        }
        function bez_cancel()
        {
          FcltMgr.closeDetail(window, { cancel: true } );
        }

      </script>
    </head>

    <body class="modal" id="mod_impbezoekers">
      <div id="bezoek">
        <form name="u2">
<%    BLOCK_START("bezInput", L("lcl_bez_imp_frame"));
if (!novervang) {
%>
        <tr>
          <td class="label"><label><%=L("lcl_bez_imp_vervangen")%>: </label></td>
          <td>
            <input type="checkbox" class="fldcheck" name="bez_replace" id="bez_replace" value="1" "checked">
          </td>
        </tr>
<%
}
        RWTEXTAREATR("bezimport", "fldremark", L("lcl_bez_bezoekers"), "",
                        {html: "rows='20' onkeyup='bez_recalc()'"});
%>
        <tr>
          <td class="label"><label><%=L("lcl_bez_importsep")%>: </label></td>
          <td>
            <select id="select_sep" onchange='bez_recalc()'>
              <option value="0"><%=L("lcl_bez_importsepauto")%></option>
              <option value="1"><%=L("lcl_bez_importsepsemi")%></option>
              <option value="2"><%=L("lcl_bez_importsepcomma")%></option>
              <option value="3"><%=L("lcl_bez_importseptab")%></option>
            </select>
          </td>
        </tr>
<%
%>
        <tr><td colspan="2"></td></tr>
<%
      BLOCK_END();
%>
      <div id="fldcount"><br></div>
<%
        var buttons =  [{ title: L("lcl_submit"), icon: "opslaan.png", action: "bez_submit()", singlepress: true, id: "btn_import_submit" },
                        { title: L("lcl_cancel"), icon: "undo.png", action: "bez_cancel()" }];
        CreateButtons(buttons);
%>
        </form>
      </div>
    </body>
  </html>
