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

    File:               productsearch.asp
    Description:        zoeken (Mobile version)
    Parameters:

    Note:

*/
%>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="./resultset_table.inc" -->
<!-- #include file="./mobile.inc" -->
<!-- #include file="./iface.inc" -->
<!-- #include file="../FAC/prodsearch.inc" -->

<%
var qrc = getQParamInt("qrc", 0) != 0;
var urole = getQParamSafe("urole", "fe");
var searchstr = getQParamUTF8("s", "");
var emptyitemsearch = getQParamInt("emptyitemsearch", 0);

var locatie_key = getQParamInt("locatie_key", user.alg_locatie_key({withcurrent:true})); // Locatie
var gebouw_key = getQParamInt("gebouw_key", user.alg_gebouw_key({withcurrent:true})); // Gebouw, kan -1 zijn, dan niet gebruiken
var verdieping_key = getQParamInt("verdieping_key", user.alg_verdieping_key({withcurrent:true}));

var subject = L("lcl_mobile_srch_zoeker") + " " + CurrentPosition;
var fac_key = 0;

//var inc_queries = ["mlddisc", "mldstdm", "resruimte", "resartikel", "resdeel", "bescatalogus", "besartikelgroep", "besartikel", "faq", "prsperslid"];
var inc_queries = ["mldstdm", "resruimte", "resartikel", "resdeel", "faq"];

var filter = {  s:     { val: getQParam("s",""),       lcl: L("lcl_mobile_search") }
             ,  soort: { val: getQParam("soort", ""),  lcl: L("lcl_srch_entiteit") }
             ,  locatieKey: locatie_key
             ,  gebouwKey: gebouw_key
             ,  verdiepingKey: verdieping_key
             };
if (emptyitemsearch == 0)
{
    if (filter.s.val.match(/^[mcMC][a-z,A-Z]*[0-9]+/)) // Meldingen en contracten kunnen extra letters hebben
        Server.Transfer("../FAC/itemsearch.asp"); // die doet dat maar
    if (filter.s.val.match(/^[braBRA][0-9]+/)) // BES, RES en Afspraak alleen numeriek
        Server.Transfer("../FAC/itemsearch.asp");
    if (filter.s.val.match(/^#+/)) // objecten
        Server.Transfer("../FAC/itemsearch.asp");
}

var counter_key = -1;
if (filter.s.val)
  counter_key = shared.registeraction("search", { info: filter.s.val });

supper = filter.s.val.toUpperCase();
var showall = getQParamInt("showall", 0) == 1;

if (!filter.soort.val)
  filter.soort.val = getQParam("autosoort","");

var w_discs = write_discs();
var queries = prodsearch_queries(w_discs, filter);
var q_items = [];
if (filter.soort.val)
{
  for (item in queries[filter.soort.val].histogram)
  {
    q_items[item] = getFParam(item, "");
  }
}

function searchstr_to_message(s)
{
    var ttl = "";
    var supper = s.toUpperCase().replace(/^\s+|\s+$/g,"");
    var letter = supper.substring(0,1);
    var fullreference = supper.substring(1);

    if (letter == "#")
    {
        ttl = L("lcl_ins_object");
    }
    else
    {
        var volgnr = -1; // Mag met punt of slash
        var arr = fullreference.split(/[\/.]/)
        if (arr.length > 1)
        {
            fullreference = arr[0];
            volgnr = parseInt(arr[1], 10) || -1;
        }
        var reference_key = parseInt(fullreference, 10);
        if (!isNaN(reference_key) || letter == "C")
        {
            switch (letter)
            {
                case "B": ttl = L("lcl_bes_bestelling");
                          break;
                case "M": if (volgnr == -1)
                            ttl = L("lcl_complain"); // De melding zelf.
                          else
                            ttl = L("lcl_mld_opdr_number"); // Een specifieke opdrachtnummer bij een melding.
                          break;
                case "R": ttl = L("lcl_reservation");
                          break;
                case "A": ttl = L("lcl_vis_appointment");
                          break;
                case "C": ttl = L("lcl_contract"); // Merk op dat we alleen numerieke concractnummers kunnen vinden!
                          break;
                case "X": break;
            }
        }
    }
    if (ttl)
        s = ttl + ": " + fullreference;

    return s;
}


function fncolIcon(oRs)
{
   var module = oRs("soort").value;
   var icon = "";
   var foto = oRs("foto").value;
   switch (module) {
      case "mlddisc":
      case "mldstdm":
          icon = foto && foto.match(/^fa-/) ? foto : "fa-fclt-edit";
          if (foto != null && !foto.match(/^fa-/))
            icon = S("mlds_image_path") + foto;
          break;
      case "resruimte":
          icon = foto && foto.match(/^fa-/) ? foto : "fa-home";
          if (foto != null && !foto.match(/^fa-/))
            icon = S("res_image_path") + foto;
          break;
      case "resartikel":
          icon = foto && foto.match(/^fa-/) ? foto : "fa-utensils-alt";
          if (foto != null && !foto.match(/^fa-/))
            icon = S("res_image_path") + "/artikel/" + foto;
          break;
      case "resdeel":
          icon = foto && foto.match(/^fa-/) ? foto : "fa-projector";
          if (foto != null && !foto.match(/^fa-/))
            icon = S("res_image_path") + "/deel/" + foto;
          break;
      case "faq":
          icon = foto && foto.match(/^fa-/) ? foto : "fa-book";
          break;
      case "bescatalogus":
      case "besartikelgroep":
      case "besartikel":
          icon = foto && foto.match(/^fa-/) ? foto : "fa-question";
          break;
      case "prsperslid":
    }
    if (icon.match(/^fa-/) && oRs("kleur").value) {
      icon = "<i class='fal fa-fw " + ICONS.placeholder(icon) + "' style='background-color: " + oRs("kleur").value + ";'></i>";
    }
    return icon;
}

function fncolLink(oRs)
{
  var module = oRs("soort").value;
  var url= "";  // om zoiets toe te gaan voegen
  switch (module)
  {
    case "mlddisc":
    case "mldstdm":
      url = "../pda/melding.asp?stdm_key="+oRs("product_key").value;
      break;
    case "resruimte":
      url = "../pda/reserveringen.asp?res_ruimte_key="+oRs("product_key").value;
      break;
    case "resartikel":
      url = "reservering.asp?restype=C&res_artikel="+oRs("product_key").value;
      break;
    case "resdeel":
      url = "reserveringen.asp?restype=CV&res_deel_key="+oRs("product_key").value;
      break;
    case "faq":
      url = "faq.asp?faq_key="+oRs("product_key").value;
      break;
    case "bescatalogus":
    case "besartikelgroep":
    case "besartikel":
    case "prsperslid":
      break;
  }
  if (url != "")
    url += "&from_search_key=" + counter_key;
  return url;
}

function fnArtikelsoort(oRs)
{
  var srt = oRs("soort").value;
  txt = queries[srt].intern;
  if (oRs("extra").value != null)
  {
    var re;
    var resstr = safe.html(shorttxt(oRs("extra").value, S("rs_mobile_maxchar")));
    if (!filter.s.val)
      return resstr;
    var wrds = filter.s.val.split(" ");
    var i = 0;
    while (i < wrds.length)
    {
      if (wrds[i].length)
      {
        re = new RegExp(escapeRegExp(wrds[i]), 'ig');
        resstr = resstr.replace (re, "<strong>$&</strong>");
      }
      i ++;
    }
    txt = resstr;
  }
  return  "<div class='listbodytext'>"+ txt + "</div>";
}

function fnProduct(oRs)
{
  return "<span class='faqp'>"+ safe.html(oRs("product").value)+"</span>";
}

function fnPrijs(oRs)
{
  if (oRs("prijs").value)
    return S("currency_pref") + safe.curr(oRs("prijs").value);
  else
    return "";
}

function fnDetail(oRs)
{
  return oRs("soort").value;
}


if (searchstr != "")
{
    var sqln = prodsearch_sql(filter, queries, w_discs, q_items, inc_queries);
    var rst = new ResultsetTable({ keyColumn: "product_key",
                                   sql: sqln,
                                   ID: "prodtable",
                                   layout: "card",
                                   iconColumn: fncolIcon,
                                   linkColumn: fncolLink,
                                   headerColumn: fnProduct,
                                   asideColumn: fnPrijs,
                                   subheaderColumn: fnArtikelsoort,
                                   contentColumn: fnArtikelsoort, //"extra",
                                   buildHistogram: true,
                                   noSearch: true,
                                   showAll: true
                                  });
}

%>
<html>
  <head>
<%  FCLTMHeader.Generate({title:  subject }); %>
  </head>
  <body>
<%
      PAGE_START({id: "page-1-"+fac_key});
        HEADER({ title: subject
               , back:!qrc
               , home: !qrc
               });
        CONTENT_START();

// Altijd forcesave omdat we geen doublecheck aankunnen
%>
<script type="text/javascript">
  function faq_search()
  {
    document.forms.fac.submit();
  };
</script>
        <form name="fac" id="fac" action="../pda/productsearch.asp" method="get" onsubmit="faq_search()" data-ajax='false' accept-charset="utf-8">
          <input type="hidden" name="mobiel" id="mobiel" value="1">
          <input type="hidden" name="forcesave" id="forcesave" value="0">
          <input type="hidden" name="urole" id="urole" value="fe">

<%
          RWFIELD("s", L("lcl_mobile_srch_zoeker_label"), searchstr, {datatype: "search" });
          BUTTON(L("lcl_mobile_srch_zoeker_go"), {id: "search_submit", click: "faq_search()", icon: "fa-search"});
%><br><%
          if (emptyitemsearch == 1)
          {
            var msg = searchstr_to_message(filter.s.val);
            var emptySetString = "<p class='prodsrchmess'>"+L("lcl_fac_emptyprodset1") + "<cite>"+ safe.html(msg) + "</cite>" + L("lcl_fac_emptyprodset2") +"</p>";
            Response.Write("<div id='rstable'><TABLE><tr><td>" + emptySetString + "</td></tr></div>");
          }
          else
          {
            if (searchstr != "")
            {
              var histogram = rst.processResultset();
              __Log(histogram);
              var reginfo = { info: filter.s.val } ;

     	      if (counter_key > 0 && histogram && typeof histogram == "object" && "SOORT" in histogram)
	          {
	            reginfo.result = JSON.stringify(histogram["SOORT"]);
                shared.registeraction("searchresult", { refkey: counter_key,
                                                        info: JSON.stringify(histogram["SOORT"])
                                                      });
              }
            }
          }
%>
        </form>
<%
        CONTENT_END();
        FOOTER();
      PAGE_END();
PDA_PAGE_END(); %>
	</body>
</html>
<% ASPPAGE_END(); %>
