/*
    $Revision$
    $Id$

    File:        productsearch.js
*/
var $moduleselector;
var $productsearch;
var $modules;
$(function () {
    $moduleselector = $("span.moduleselector");
    $productsearch = $("input#productsearch");
    if ($productsearch.length > 0) {
        $modules = $("div.modules");
        $moduleselector.attr("data-module", localStorage.getItem("productsearch_module") || "ALL");
        $productsearch.attr("placeholder", safe._htmlDecode(search_modules[$moduleselector.attr("data-module")].text));
        // select module
        $("div.module").on("click",
            function (event) {
                let mod = $(this).attr("data-module");
                event.preventDefault();
                localStorage.setItem("productsearch_module", mod);
                $modules.hide();
                $moduleselector.attr("data-module", mod);
                $productsearch.attr("placeholder", safe._htmlDecode(search_modules[mod].text));
            }
        );
        // hide select module
        $("body").on("click",
            function () {
                $modules.hide();
            }
        );
        // toggle select module
        $moduleselector.on("click",
            function (event) {
                event.preventDefault();
                event.stopPropagation();
                $modules.toggle();
            }
        );
    }
});


function dosearch() {

    var showall = "";
    var mod_prefix = "";
    var module;
    var s = $("#productsearch").val();
    var first_char = s.charAt(0).toLowerCase();
    for (module in search_modules) {
        if (first_char == search_modules[module].prefix) {                  // Kijk of het eerste karakter een valide prefix is
            if ((first_char.match(/[mc]/) && (s.match(/^[mc][0-9]+/i) ||    // Meldingen en contracten kunnen extra letters hebben, maar als INS geselecteerd is, weegt die zwaarder
                    ($moduleselector.attr("data-module") !== "INS" && s.match(/^[mc][a-z]*[0-9]+/i)))) ||
                (first_char.match(/[bra]/) && s.match(/^[bra][0-9]+/i)) ||  // BES, RES en Afspraak alleen numeriek
                (first_char === "#")) {                                     // INS (objecten) mag alles zoeken (term wordt ingevuld bij 'identificatie' op ins_search.asp)
                mod_prefix = first_char;
                break;
            }
        }
    }
    if ($moduleselector.attr("data-module") == "ALL" && mod_prefix == "") {
        showall = "&showall=1";
    } else {
        mod_prefix = (mod_prefix == first_char ? "" : search_modules[$moduleselector.attr("data-module")].prefix);
        if (!(mod_prefix + s).match(/^[mc][a-z]*[0-9]+/i) &&    // Meldingen en contracten kunnen extra letters hebben
            !(mod_prefix + s).match(/^[bra][0-9]+/i) &&         // BES, RES en Afspraak alleen numeriek
            !(mod_prefix === "#"))                              // INS (objecten) mag alles zoeken (term wordt ingevuld bij 'identificatie' op ins_search.asp)
        {
            mod_prefix = "";
        }
    }
    if (window.toggleHeaderSearch) toggleHeaderSearch();
    FcltMgr.openDetail("appl/fac/prodsearch.asp?s=" + myEncode(mod_prefix + s) + showall, L("lcl_search_result") + " " + $("#productsearch")[0].value);
}