function Sparepartsearchresultonstore() {

    var self = {};

    self.tmp = null;
    self.selection = {};
    self.UserId = null;

    self.requestSparePartList = function () {
        var sparepart_search_param = $.cookie("sparepartsearchparam");
        var sparepartgroupid_search_param = $.cookie("spareparttypesearchparam");
        var current_session_token = $.cookie("session_token");


        var requester = new AjaxRequest({
            data: { "incomingSearchParam": sparepart_search_param, "incomingSparepartGroupIdSearchParam": sparepartgroupid_search_param, "incomingStoreId": CURRENT_SELECTED_STORE, "session_token": current_session_token }, // This is for the json request
            success: self.SparepartListReceptionSuccess,
            error: self.SparepartListReceptionFailure,
            url: "RequestSparePartListOnSearchParamAndStore"
        });
        requester.ExecuteRequest();

    }

    self.SparepartListReceptionSuccess = function (data, status, xhr) {

        if (data.d.SessionTokenValid == false) {
            c1("sparepartsearchlistonstore row 28: token not valid, moving to dialog");
            $.mobile.navigate("#dialog_tokenexpired", { role: "dialog" });
            e1('sparepartsearchlistonstore row 30');
        }
        else {

            var listData = data.d.SparePartList;

            jQuery.each(listData, function () {
                var sparepartLink = '<li role="heading" class="list-header-class" data-role="list-divider">';
                sparepartLink += this.SparePartNumber;
                sparepartLink += '</li>';
                //sparepartLink += '<li class="list-element-class" data-theme="c" data-sparepartnumber="';
                sparepartLink += '<li class="list-element-class" data-sparepartnumber="';
                sparepartLink += this.SparePartNumber;
                sparepartLink += '" data-sparepartid="';
                sparepartLink += this.SparePartId;
                sparepartLink += '"><a href="#dummy" data-transition="slide">';
                sparepartLink += this.SparePartName;
                sparepartLink += '</a></li>';

                $("#sparepartlistonstore_page_sparepart_list").append(sparepartLink);
            });

            $("#sparepartlistonstore_page_sparepart_list").listview('refresh');
        }
    }

    self.SparepartListReceptionFailure = function (data, status, xhr) {

        c1('sparepartsearchlistonstore row 56: Failed to retrieve spare part search list. Unable to reach web service.');
        DbCommunicationErrorHandler(WEBSERVICE_FAULT_JSONREQUEST);
    }


    self.StoreExistFork = function () {
        self.CheckStockExistenceForSelectedSparePart();

    }

    self.attachEventToList = function () {
        $('#sparepartlistonstore_page_sparepart_list').on('click', 'li', function () {
            //alert($(this).text());
            var sparepart_nr = $(this).data("sparepartnumber");
            var sparepart_id = $(this).data("sparepartid");

            if (sparepart_nr !== undefined) {
                $.cookie("currently_selected_sparepartnumber", sparepart_nr);
                $.cookie("currently_selected_sparepartid", sparepart_id);

                if (SELECTED_SPAREPART_JOB_TYPE == STOCK_ADJUSTMENT) {

                    $(":mobile-pagecontainer").pagecontainer("change", "#page_stockmanagementadjust", { role: "page", reverse: false, changeHash: true });
                    e1('sparepartsearchlistonstore row 79');
                }
                else if (SELECTED_SPAREPART_JOB_TYPE == STOCK_REPLENISHMENT) {

                    $(":mobile-pagecontainer").pagecontainer("change", "#page_stockmanagementreplenish", { role: "page", reverse: false, changeHash: true });
                    e1('sparepartsearchlistonstore row 84');
                }
            }
        });
    }

    return self;

}

var sparepartSearchResultOnStore = Sparepartsearchresultonstore();

$(document).one("pageshow", "#page_sparepartlistonstore", function (e) {

    $("#sparepartlistonstore_backbutton").click(function () {
        $(":mobile-pagecontainer").pagecontainer("change", "#page_sparepartselectinstore", { role: "page", reverse: false, changeHash: false });
        e1('sparepartsearchlistonstore row 100');
    });

    console.info("attaching event to spare part search result list");
    sparepartSearchResultOnStore.attachEventToList();
});


$(document).on("pagebeforeshow", "#page_sparepartlistonstore", function (e) {
    if (SessionTokenStatus == true) {
        console.info("in spare part list form");

        $("#sparepartlistonstore_errormessage").hide();
        $("#sparepartlistonstore_page_sparepart_list").empty();
        sparepartSearchResultOnStore.requestSparePartList();
    }
    else { helper.ForcedLogout(); }
});


$(document).on("pageshow", "#page_sparepartlistonstore", function (e) {

    $("#sparepartlistonstore_backbutton").unbind().click(function () {
        //$(":mobile-pagecontainer").pagecontainer("change", "#page_sparepartselectinstore", { role: "page", reverse: false, changeHash: false });

        if (CURRENT_SPAREPART_SEARCH_MODE == SPAREPART_SEARCH_INPUT_MANUAL) {
            $(":mobile-pagecontainer").pagecontainer("change", "#page_sparepartselectinstore", { role: "page", reverse: false, changeHash: true });
            e1('sparepartsearchlistonstore row 116');
        }
        else if (CURRENT_SPAREPART_SEARCH_MODE == SPAREPART_SEARCH_INPUT_BARCODE) {
            SCAN_PAGE_TARGET = SCAN_SPAREPART;
            $(":mobile-pagecontainer").pagecontainer("change", "#page_scantest", { role: "page", reverse: false, changeHash: true });
            e1('sparepartsearchlistonstore row 120');
        }
    });


    //**************************************************************
    //20200401 - moved to pagebeforeshow event
    //if (SessionTokenStatus == true) {
    //    console.info("in spare part list form");

    //    $("#sparepartlistonstore_errormessage").hide();
    //    $("#sparepartlistonstore_page_sparepart_list").empty();
    //    sparepartSearchResultOnStore.requestSparePartList();
    //}
    //else { helper.ForcedLogout(); }
    //**************************************************************

});

