<%@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" -->

<%
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" && oRs("kenmerk_volgnr").Value >= 0)
  {
    nr++;
    switch (oRs("kenmerk_kenmerktype").Value)
    { // We ondersteunen de text, numerieke, datum en berekening velden.
      case "B":
      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();
}
oRs.Close();

//build format string
var data_header = [ { id: "naam",     desc: L("lcl_bez_imp_naam") }
                  , { id: "bedrijf",  desc: L("lcl_bez_imp_bedrijf") }
                  ];
var data_colarr = [ { id: "telefoon", desc: L("lcl_bez_imp_telefoon") }
                  , { id: "email",    desc: L("lcl_bez_imp_email") }
                  , { id: "kenteken", desc: L("lcl_bez_imp_kenteken") }
                  , { id: "flex",     desc: L("lcl_bez_imp_flex") }
                  ];
if(S("bez_import_column3") != 0){
  data_header = data_header.concat(data_colarr[S("bez_import_column3") - 1]);
}
if(S("bez_import_column4") != 0){
  data_header = data_header.concat(data_colarr[S("bez_import_column4") - 1]);
}
if(S("bez_import_column5") != 0){
  data_header = data_header.concat(data_colarr[S("bez_import_column5") - 1]);
}
var formatstring_oms = "";
var formatstring_id = ""
for (var i=0; i<data_header.length; i++)
{
    formatstring_id += data_header[i].id + ";";
    formatstring_oms += data_header[i].desc + ";";
}

%>
  <html>
  <head>
<%
      FCLTHeader.Generate();
%>
      <script type="text/javascript">

        var flexk = JSON.parse("<%=safe.jsstring(JSON.stringify(flexk))%>");
        $(function()
        {
          $("#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 formatstring = "<%=formatstring_id%>";
            var data = { format: formatstring
                       , format_oms: "<%=formatstring_oms%>"
                       , 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);

                var splColumns = formatstring.split(";");
                splColumns.pop(); // Get the last element out of there, it's empty

                if (spl.length && trim(spl[0]) != "")
                {
                    var flexIndex = 0;
                    var bez = {};
                    bez.flex = $.extend(true, [], flexk);
                    for (var index = 0; index < spl.length && index < splColumns.length; index++)
                    {
                        var col = splColumns[index];
                        if (trim(spl[index]) != "" && col != "")
                        {
                            if (col == "flex")
                            {
                                if (bez.flex[flexIndex])
                                {
                                    bez.flex[flexIndex].value = trim(spl[index]);
                                    flexIndex++;
                                }
                            }
                            else
                            {
                                bez[col] = trim(spl[index]);
                            }
                            if (col == "bedrijf")
                            {
                                bed_cnt++;
                            }
                        }
                    }
                    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="fclt-modal" id="mod_impbezoekers">
    <% MODAL_START(); %>
        <form name="u2">
<%    MODAL_BLOCK_START("bezInput", L("lcl_bez_imp_frame"), { "icon": "fa-file-import" });
if (!novervang) {
%>
        <tr>
          <td class="label"><label for="bez_replace"><%=L("lcl_bez_imp_vervangen")%></label></td>
          <td>
            <input type="checkbox" class="fldcheck" name="bez_replace" id="bez_replace" value="1" "checked">
          </td>
        </tr>
        <tr>
          <td></td>
          <td><%=formatstring_oms.slice(0, -1)%></td>
        </tr>
<%
}
        RWTEXTAREATR("bezimport", "fldtxt", L("lcl_bez_bezoekers"), "",
                        {html: "rows='20' onkeyup='bez_recalc()'"});
%>
        <tr>
          <td class="label"><label for="select_sep"><%=L("lcl_bez_importsep")%></label></td>
          <td>
            <select id="select_sep" class="fld" 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>
          <div id="fldcount"><br></div>
<%
      MODAL_BLOCK_END();
        var buttons =  [{ title: L("lcl_submit"), icon: "fa-fclt-save", action: "bez_submit()", singlepress: true, id: "btn_import_submit", importance: 1 },
                        { title: L("lcl_cancel"), icon: "fa-fclt-cancel", action: "bez_cancel()", importance: 3 }];
        SIMPLE_BLOCK_START();
        CreateButtons(buttons);
        SIMPLE_BLOCK_END();
%>
        </form>
      <% MODAL_END(); %>
    </body>
  </html>
<% ASPPAGE_END(); %>
