function Storeselect() {

    var self = {

        get SelectedStoreName() {
            if ($("#storeselect_selectable option:selected") == undefined || ($("#storeselect_selectable option:selected").text).length == 0) {
                return "undefined";
            }
            return $("#storeselect_selectable option:selected").text();
        }
    };


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

    self.requestStoreList = function () {
        var currentUserId = $.cookie("user_id");

        var requester = new AjaxRequest({
            data: {"userid": currentUserId}, // This is for the json request
            success: self.requestStoreListSuccess,
            error: self.requestStoreListFailure,
            url: "RequestStoreListOnUserId"
        });
        requester.ExecuteRequest();

    }

    self.requestStoreListSuccess = function (data, status, xhr) {
        console.log(data.d.StoreList);
        self.updateStoreList(data.d.StoreList);
    }



    self.requestStoreListFailure = function (request, status, error) {
        DbCommunicationErrorHandler(WEBSERVICE_FAULT_JSONREQUEST);
    }

    self.updateStoreList = function (storeListElements) {

        var sparePartStoreElements = '<option>';
        sparePartStoreElements += ReturnPhraseOnKey("selectstore_placeholdernode");
        sparePartStoreElements += '</option>';

        jQuery.each(storeListElements, function () {
            sparePartStoreElements += '<option value="'

            //MABE 20200917 - the -50 value is an arbitrarily selected value selected by me to indicate stores without any stocks
            if (this.StockCount == 0) {
                sparePartStoreElements += '-50" class="nostocksmarker">';
            }
            else {
                sparePartStoreElements += this.StoreId;
                sparePartStoreElements += '">';
            }
            sparePartStoreElements += this.StoreName;
            sparePartStoreElements += '</option>';
        });

        $("#storeselect_selectable").html(sparePartStoreElements);
        $("#storeselect_selectable").selectmenu().selectmenu("refresh");

        var exists = false;
        $('#storeselect_selectable  option').each(function () {

            if (this.value == DEFAULT_SELECTED_STORE) {
                exists = true;
            }
        });


        if (exists) {
            $('#storeselect_selectable').val(DEFAULT_SELECTED_STORE).selectmenu('refresh');
        }
    }

    self.setErrorMsg = function (msg) {

        $('#sparepartstorelist_errormessage').html(msg);
        $('#sparepartstorelist_errormessage').show();
    }

    self.clearErrorMsg = function (msg) {
        $('#sparepartstorelist_errormessage').html('');
        $('#sparepartstorelist_errormessage').hide();
    }



    return self;
}

var storeSelect = Storeselect();

$(document).one("pageshow", "#page_sparepartstoreselect", function (e) {
    $("#storeselect_selectable").change(function () {

        storeSelect.clearErrorMsg();
        c1($(this).val());
        //MABE 20200917 - the -50 value is an arbitrarily selected value selected by me to indicate stores without any stocks
        if ($(this).val() == -50) {
            CURRENT_SELECTED_STORE = NO_STORE_SELECTED;
            storeSelect.setErrorMsg(ReturnPhraseOnKey('sparepartstoreselect_store_empty_message'));
        }
        else
        {

            storeSelect.clearErrorMsg();


            CURRENT_SELECTED_STORE = $(this).val();

            $(":mobile-pagecontainer").pagecontainer("change", "#page_stores_functionselection", { role: "page", reverse: false, changeHash: true });
        }
    });


});



$(document).on("pageshow", "#page_sparepartstoreselect", function (e) {
    initScanner(undefined);
    if (SessionTokenStatus == true) {
        $("#sparepartstorelist_errormessage").hide();
        storeSelect.requestStoreList();
    }
    else { helper.ForcedLogout();}


});

