function ShowSimpleSearchResult(keyword, language, userName) {
    $(".article-content").html("<img class=\"loadingImage\" src=\"" + GetBaseUrl() + "/Infomat/Images/progressbar.gif\" alt=\"Loading...\" />").show();

    $.post(GetBaseUrl() + "/Infomat/Modules/SearchEngine/SearchEngine.aspx?searchType=simplesearch", {
        keyword: keyword
    },
    function(data) {
    //showarticles(groupid, page);
    ShowArticles(0, 1);
        
        ShowFilteredArticleGroups(language);
    }
    );
}

function ShowSearchResult(
    SearchType,
    Language,
    UserName,
    Keyword,
    ArticleCodeTextBoxId,
    ArticleDescriptionTextBoxId,
    SearchCodeTextBoxId,
    PrintCodeTextBoxId,
    PropertyName1DropDownListId,
    PropertyValue1TextBoxId,
    PropertyName2DropDownListId,
    PropertyValue2TextBoxId,
    PropertyName3DropDownListId,
    PropertyValue3TextBoxId,
    GroupCode1DropDownListId,
    GroupCode2DropDownListId,
    GroupCode3DropDownListId,
    GroupCode4DropDownListId,
    GroupCode5DropDownListId,
    StockMonitorDropDownListId,
    SupplierArticleCodeTextBoxId,
    PromocodeDropDownlist
) {
    setInterval("CheckURL()", 250);
    $(".article-content").html("<img class=\"loadingImage\" src=\"" + GetBaseUrl() + "/Infomat/Images/progressbar.gif\" alt=\"Loading...\" />").show();

    var articleCodeTextBox = document.getElementById(ArticleCodeTextBoxId);
    var articleDescriptionTextBox = document.getElementById(ArticleDescriptionTextBoxId);
    var searchCodeTextBox = document.getElementById(SearchCodeTextBoxId);
    var printCodeTextBox = document.getElementById(PrintCodeTextBoxId);
    var propertyName1DropDownList = document.getElementById(PropertyName1DropDownListId);
    var propertyValue1TextBox = document.getElementById(PropertyValue1TextBoxId);
    var propertyName2DropDownList = document.getElementById(PropertyName2DropDownListId);
    var propertyValue2TextBox = document.getElementById(PropertyValue2TextBoxId);
    var propertyName3DropDownList = document.getElementById(PropertyName3DropDownListId);
    var propertyValue3TextBox = document.getElementById(PropertyValue3TextBoxId);

    var groupCode1DropDownList = document.getElementById(GroupCode1DropDownListId);
    var groupCode2DropDownList = document.getElementById(GroupCode2DropDownListId);
    var groupCode3DropDownList = document.getElementById(GroupCode3DropDownListId);
    var groupCode4DropDownList = document.getElementById(GroupCode4DropDownListId);
    var groupCode5DropDownList = document.getElementById(GroupCode5DropDownListId);
    var stockMonitorDropDownList = document.getElementById(StockMonitorDropDownListId);
    var supplierArticleCodeTextBox = document.getElementById(SupplierArticleCodeTextBoxId);
    var promocodeDropDownlist = document.getElementById(PromocodeDropDownlist);

    var selectedArticleCode = "";
    var selectedArticleDescription = "";
    var selectedSearchCode = "";
    var selectedPrintCode = "";
    var selectedPropertyName1 = "";
    var selectedPropertyValue1 = "";
    var selectedPropertyName2 = "";
    var selectedPropertyValue2 = "";
    var selectedPropertyName3 = "";
    var selectedPropertyValue3 = "";
    var selectedGroupCode1 = "";
    var selectedGroupCode2 = "";
    var selectedGroupCode3 = "";
    var selectedGroupCode4 = "";
    var selectedGroupCode5 = "";
    var selectedStockMonitor = "";
    var selectedSupplierArticleCode = "";
    var selectedPromocode = "";

    if (articleCodeTextBox != null) { selectedArticleCode = articleCodeTextBox.value; }
    if (articleDescriptionTextBox != null) { selectedArticleDescription = articleDescriptionTextBox.value; }
    if (searchCodeTextBox != null) { selectedSearchCode = searchCodeTextBox.value; }
    if (printCodeTextBox != null) { selectedPrintCode = printCodeTextBox.value; }
    if (propertyName1DropDownList != null) { selectedPropertyName1 = propertyName1DropDownList.options[propertyName1DropDownList.selectedIndex].value; }
    if (propertyValue1TextBox != null) { selectedPropertyValue1 = propertyValue1TextBox.value; }
    if (propertyName2DropDownList != null) { selectedPropertyName2 = propertyName2DropDownList.options[propertyName2DropDownList.selectedIndex].value; }
    if (propertyValue2TextBox != null) { selectedPropertyValue2 = propertyValue2TextBox.value; }
    if (propertyName3DropDownList != null) { selectedPropertyName3 = propertyName3DropDownList.options[propertyName3DropDownList.selectedIndex].value; }
    if (propertyValue3TextBox != null) { selectedPropertyValue3 = propertyValue3TextBox.value; }
    if (groupCode1DropDownList != null && groupCode1DropDownList.disabled == false && groupCode1DropDownList.selectedIndex != 0) { selectedGroupCode1 = GetSpecificGroupValue(GetSelectedGroupFromDropDownList(groupCode1DropDownList), 1); }
    if (groupCode2DropDownList != null && groupCode2DropDownList.disabled == false && groupCode2DropDownList.selectedIndex != 0) { selectedGroupCode2 = GetSpecificGroupValue(GetSelectedGroupFromDropDownList(groupCode2DropDownList), 2); }
    if (groupCode3DropDownList != null && groupCode3DropDownList.disabled == false && groupCode3DropDownList.selectedIndex != 0) { selectedGroupCode3 = GetSpecificGroupValue(GetSelectedGroupFromDropDownList(groupCode3DropDownList), 3); }
    if (groupCode4DropDownList != null && groupCode4DropDownList.disabled == false && groupCode4DropDownList.selectedIndex != 0) { selectedGroupCode4 = GetSpecificGroupValue(GetSelectedGroupFromDropDownList(groupCode4DropDownList), 4); }
    if (groupCode5DropDownList != null && groupCode5DropDownList.disabled == false && groupCode5DropDownList.selectedIndex != 0) { selectedGroupCode5 = GetSpecificGroupValue(GetSelectedGroupFromDropDownList(groupCode5DropDownList), 5); }
    if (stockMonitorDropDownList != null) { selectedStockMonitor = stockMonitorDropDownList.options[stockMonitorDropDownList.selectedIndex].value; }
    if (promocodeDropDownlist != null) { selectedPromocode = promocodeDropDownlist.options[promocodeDropDownlist.selectedIndex].value; }
    if (supplierArticleCodeTextBox != null) { selectedSupplierArticleCode = supplierArticleCodeTextBox.value; }

    //Open de pagina en geef alle values mee in de querystring
    $.post(GetBaseUrl() + "/Infomat/Modules/SearchEngine/SearchEngine.aspx?searchType=" + SearchType, {
        ArticleCode: selectedArticleCode,
        ArticleDescription: selectedArticleDescription,
        SearchCode: selectedSearchCode,
        PrintCode: selectedPrintCode,
        PropertyName1: selectedPropertyName1,
        PropertyValue1: selectedPropertyValue1,
        PropertyName2: selectedPropertyName2,
        PropertyValue2: selectedPropertyValue2,
        PropertyName3: selectedPropertyName3,
        PropertyValue3: selectedPropertyValue3,
        GroupCode1: selectedGroupCode1,
        GroupCode2: selectedGroupCode2,
        GroupCode3: selectedGroupCode3,
        GroupCode4: selectedGroupCode4,
        GroupCode5: selectedGroupCode5,
        StockTypeCode: selectedStockMonitor,
        SupplierArticleCode: selectedSupplierArticleCode,
        Promocode: selectedPromocode
    },
    function(data) {
        ShowArticles(0, 1);
        ShowFilteredArticleGroups(Language);
    }
    );
}

function AutoCompletePropertyTextBox(DropDownListControlId, TextBoxControlId, values) {
    $().ready(function() {
        var dropDownList = document.getElementById(DropDownListControlId);
        if (dropDownList != null) {
            var dataArray = values.split("||");
            filteredDataArray = new Array();

            for (i = 0; i < dataArray.length; i++) {
                if (dataArray[i].split("##")[0] == dropDownList.options[dropDownList.selectedIndex].value) {
                    filteredDataArray.push(dataArray[i].split("##")[1]);
                }
            }

            $("#" + TextBoxControlId).val("");
            $("#" + TextBoxControlId).flushCache();
            $("#" + TextBoxControlId).autocomplete(filteredDataArray, {
                matchContains: true,
                minChars: 0
            });
        }
    });
}

function FilterArticleGroups(changingLevel, allGroupsDropDownListId, group1DropDownListId, group2DropDownListId, group3DropDownListId, group4DropDownListId, group5DropDownListId) {
    $().ready(function() {
        var allGroupsDropDownList = document.getElementById(allGroupsDropDownListId);
        var group1DropDownList = document.getElementById(group1DropDownListId);
        var group2DropDownList = document.getElementById(group2DropDownListId);
        var group3DropDownList = document.getElementById(group3DropDownListId);
        var group4DropDownList = document.getElementById(group4DropDownListId);
        var group5DropDownList = document.getElementById(group5DropDownListId);

        switch (changingLevel) {
            case 0:
                FilterArticleGroupsForLevel(allGroupsDropDownList, 1, "", group1DropDownList);
                break;
            case 1:
                FilterArticleGroupsForLevel(allGroupsDropDownList, 2, GetSelectedGroupFromDropDownList(group1DropDownList), group2DropDownList);
                ClearDropDownList(group3DropDownList);
                ClearDropDownList(group4DropDownList);
                ClearDropDownList(group5DropDownList);
                break;
            case 2:
                FilterArticleGroupsForLevel(allGroupsDropDownList, 3, GetSelectedGroupFromDropDownList(group2DropDownList), group3DropDownList);
                ClearDropDownList(group4DropDownList);
                ClearDropDownList(group5DropDownList);
                break;
            case 3:
                FilterArticleGroupsForLevel(allGroupsDropDownList, 4, GetSelectedGroupFromDropDownList(group3DropDownList), group4DropDownList);
                ClearDropDownList(group5DropDownList);
                break;
            case 4:
                FilterArticleGroupsForLevel(allGroupsDropDownList, 5, GetSelectedGroupFromDropDownList(group4DropDownList), group5DropDownList);
                break;
        }

        DisableEmptyDropDownLists(group1DropDownList);
        DisableEmptyDropDownLists(group2DropDownList);
        DisableEmptyDropDownLists(group3DropDownList);
        DisableEmptyDropDownLists(group4DropDownList);
        DisableEmptyDropDownLists(group5DropDownList);
    });
}

function FilterArticleGroupsForLevel(allGroupsDropDownList, level, groupId, dropDownListToFilter) {
    if (dropDownListToFilter != null) {
        ClearDropDownList(dropDownListToFilter);

        var optn = document.createElement("OPTION");
        optn.text = "";
        optn.value = "";
        dropDownListToFilter.options.add(optn);

        for (i = 0; i < allGroupsDropDownList.options.length; i++) {
            switch (level) {
                case 1:
                    var groupSplitter = allGroupsDropDownList.options[i].value.split(";")

                    if (groupSplitter[1] + "" == "" && groupSplitter[2] + "" == "" && groupSplitter[3] + "" == "" && groupSplitter[4] + "" == "") {
                        AddItemToDropDownList(allGroupsDropDownList, dropDownListToFilter, i);
                    }

                    break;
                case 2:
                    var groupSplitter = allGroupsDropDownList.options[i].value.split(";")

                    if (groupSplitter[0] + "" == GetSpecificGroupValue(groupId, 1) && groupSplitter[1] + "" != "" && groupSplitter[2] + "" == "" && groupSplitter[3] + "" == "" && groupSplitter[4] + "" == "") {
                        AddItemToDropDownList(allGroupsDropDownList, dropDownListToFilter, i);
                    }

                    break;
                case 3:
                    var groupSplitter = allGroupsDropDownList.options[i].value.split(";")

                    if (groupSplitter[0] + "" == GetSpecificGroupValue(groupId, 1) && groupSplitter[1] + "" == GetSpecificGroupValue(groupId, 2) && groupSplitter[2] + "" != "" && groupSplitter[3] + "" == "" && groupSplitter[4] + "" == "") {
                        AddItemToDropDownList(allGroupsDropDownList, dropDownListToFilter, i);
                    }

                    break;
                case 4:
                    var groupSplitter = allGroupsDropDownList.options[i].value.split(";")

                    if (groupSplitter[0] + "" == GetSpecificGroupValue(groupId, 1) && groupSplitter[1] + "" == GetSpecificGroupValue(groupId, 2) && groupSplitter[2] + "" == GetSpecificGroupValue(groupId, 3) && groupSplitter[3] + "" != "" && groupSplitter[4] + "" == "") {
                        AddItemToDropDownList(allGroupsDropDownList, dropDownListToFilter, i);
                    }

                    break;
                case 5:
                    var groupSplitter = allGroupsDropDownList.options[i].value.split(";")

                    if (groupSplitter[0] + "" == GetSpecificGroupValue(groupId, 1) && groupSplitter[1] + "" == GetSpecificGroupValue(groupId, 2) && groupSplitter[2] + "" == GetSpecificGroupValue(groupId, 3) && groupSplitter[3] + "" == GetSpecificGroupValue(groupId, 4) && groupSplitter[4] + "" != "") {
                        AddItemToDropDownList(allGroupsDropDownList, dropDownListToFilter, i);
                    }

                    break;
            }
        }
        EnableDropDownList(dropDownListToFilter);
    }
}

function DisableEmptyDropDownLists(dropDownList) {
    if (dropDownList != null) {
        if (dropDownList.options.length <= 1) {
            dropDownList.disabled = true;
        }
    }
}

function EnableDropDownList(dropDownList) {
    if (dropDownList.options.length > 1) {
        dropDownList.disabled = false;
    }
}

function GetSelectedGroupFromDropDownList(dropDownList) {
    var returnValue = dropDownList.options[dropDownList.selectedIndex].value;
    return returnValue;
}

function GetSpecificGroupValue(groupId, level) {
    return groupId.split(";")[level - 1];
}

function AddItemToDropDownList(allGroupsDropDownList, dropDownList, itemToAdd) {
    var optn = document.createElement("OPTION");
    optn.text = allGroupsDropDownList.options[itemToAdd].text;
    optn.value = allGroupsDropDownList.options[itemToAdd].value;
    dropDownList.options.add(optn);
}

function ClearDropDownList(dropDownList) {
    if (dropDownList != null) {
        dropDownList.options.length = 0;
        DisableEmptyDropDownLists(dropDownList);
    }
}
