function Sparepartinfo() {

    var self = {};
    self.current_userid = null;
    self.current_sparepart_nr = null;
    self.currentSparePartInfo = 'at the moment it is empty';
    self.stockTableLayoutLight = true;
    self.supplierTableLayoutOdd = true;





    self.requestSparePartInfo = function () {
        self.current_sparepart_nr = $.cookie("sparepart_nr_for_info");
        self.current_userid = $.cookie("user_id");
        var current_session_token = $.cookie("session_token");
        c3('test');


        //var machine_search_param = $("#selectmachine_machineid_input").val();


        var requester = new AjaxRequest({
            data: {"userid": self.current_userid, "sparepart_nr": self.current_sparepart_nr, "session_token": current_session_token},
            success: self.SparePartInfoRequestComplete,
            error: self.SparePartInfoRequestIncomplete,
            url: "RequestSparePartInformationExtended"
        });
        requester.ExecuteRequest();

    }

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

        if (data.d.SessionTokenValid == false) {
            //$.mobile.navigate("#dialog_tokenexpired", { role: "dialog" });
            $("#sparepartinfo_generic_popup_header").html(ReturnPhraseOnKey("dialogue_tokenexpired_message1"));
            $("#sparepartinfo_generic_popup_text").html(ReturnPhraseOnKey("dialogue_tokenexpired_message1"));

            $("#sparepartinfo_generic_popup").off().on("popupafterclose", function (event, ui) {
                $(":mobile-pagecontainer").pagecontainer("change", "#page_login", { role: "page", reverse: false, changeHash: false });
                e3('sparepartinfo row 42');
            });

            $("#sparepartinfo_generic_popup_button").unbind("click").click(function () {
                $("#sparepartinfo_generic_popup").popup("close");
            });

            $("#sparepartinfo_generic_popup").popup().popup("open");
        }
        else {

            self.currentSparePartInfo = data.d;

            self.generateSparePartDetails();
            self.generateSparePartStockList();
            self.generateSparePartSupplierList();

            /* Update info section of the screen */

        //    var sparePartNumber = sparePartInfo.SparePartList[0].SparePartNumber;
        //    var sparePartName = sparePartInfo.SparePartList[0].SparePartName;

        //    var sparePartInfoHTML = '<p><b>';
        //    sparePartInfoHTML += ReturnPhraseOnKey('sparepartdetail_sparepartname');
        //    sparePartInfoHTML += '<br></b>';
        //    sparePartInfoHTML += sparePartName;
        //    sparePartInfoHTML += '</p><p><strong>';
        //    sparePartInfoHTML += ReturnPhraseOnKey('sparepartdetail_sparepartnr');
        //    sparePartInfoHTML += '</strong><br>';
        //    sparePartInfoHTML += sparePartNumber;
        //    sparePartInfoHTML += '</p>';

        //    $("#sparepart_detail_details").empty();
        //    $("#sparepart_detail_details").html(sparePartInfoHTML);

        //    /* Update list of stocks valid for selection for the job */
        //    var stockListHTML = '<li class="list-header-class" role="heading" data-role="list-divider">';
        //    stockListHTML += ReturnPhraseOnKey('sparepartdetail_stockheader');
        //    stockListHTML += '</li>';

        //    /* Add empty list element for scenario where spare part stock is NOT to be used */
        //    stockListHTML += '<li class="list-element-class" data-theme="c" data-stockid = "0"><a href="#dummy" data-transition="slide">';
        //    stockListHTML += ReturnPhraseOnKey('sparepartdetail_nostock');
        //    stockListHTML += '</a></li>';

        //    /* List valid stocks for the job */
        //    var receivedStockList = data.d.StockList;

        //    jQuery.each(receivedStockList.StockElementList, function () {
        //        stockListHTML += '<li class="list-element-class" data-theme="c" data-stockid ="';
        //        stockListHTML += this.StockId;
        //        stockListHTML += '">';
        //        stockListHTML += '<a href="#" data-transition="slide">'
        //        stockListHTML += this.StockStore;
        //        stockListHTML += '<span class="ui-li-count">';
        //        stockListHTML += this.StockInStore;
        //        stockListHTML += '</span></a></li>';
        //    });

        //    $("#sparepartdetail_storelist").empty();
        //    $("#sparepartdetail_storelist").html(stockListHTML);
        //    $("#sparepartdetail_storelist").listview().listview("refresh");
        }

    }

    self.SparePartInfoRequestIncomplete = function (data, status, xhr) {
        c1('sparepartinfo row 102: Failed to retrieve spare part information. Unable to reach web service.');
        DbCommunicationErrorHandler(WEBSERVICE_FAULT_JSONREQUEST);
    }

    self.generateSparePartDetails = function () {
        c1('sparepartinfo row 110: Generating sparepart detail table');

        var sp_name = unspecifiedFromPM5(self.currentSparePartInfo.SparePartName);
        var sp_nr = unspecifiedFromPM5(self.currentSparePartInfo.SparePartNo);
        var sp_type_nr = unspecifiedFromPM5(self.currentSparePartInfo.SparePartTypeNo);
        var sp_group = unspecifiedFromPM5(self.currentSparePartInfo.SparePartGroupName);
        var sp_manufacturer = unspecifiedFromPM5(self.currentSparePartInfo.ManufacturerName);
        var sp_price = unspecifiedFromPM5(self.currentSparePartInfo.SparePartCostPrice);
        var sp_dimensions = unspecifiedFromPM5(self.currentSparePartInfo.SparePartDimension);
        var sp_unit = unspecifiedFromPM5(self.currentSparePartInfo.SparePartUnit);
        var sp_note = unspecifiedFromPM5(self.currentSparePartInfo.SparePartNote);



        var sparepartInfoTableHTML = '<table id="sparepartinfo_detail_table">';
        sparepartInfoTableHTML += '<tr><td class="label">';
        sparepartInfoTableHTML += ReturnPhraseOnKey('sp_info_name');
        sparepartInfoTableHTML += '</td><td>';
        sparepartInfoTableHTML += sp_name;
        sparepartInfoTableHTML += '</td></tr>';
        sparepartInfoTableHTML += '<tr><td class="label">';
        sparepartInfoTableHTML += ReturnPhraseOnKey('sp_info_nr');
        sparepartInfoTableHTML += '</td><td>';
        sparepartInfoTableHTML += sp_nr;
        sparepartInfoTableHTML += '<tr><td class="label">';
        sparepartInfoTableHTML += ReturnPhraseOnKey('sp_type_nr');
        sparepartInfoTableHTML += '</td><td>';
        sparepartInfoTableHTML += sp_type_nr;
        sparepartInfoTableHTML += '</td></tr>';
        sparepartInfoTableHTML += '<tr><td class="label">';
        sparepartInfoTableHTML += ReturnPhraseOnKey('sp_info_group');
        sparepartInfoTableHTML += '</td><td>';
        sparepartInfoTableHTML += sp_group;
        sparepartInfoTableHTML += '</td></tr>';
        sparepartInfoTableHTML += '<tr><td class="label">';
        sparepartInfoTableHTML += ReturnPhraseOnKey('sp_info_manufacturer');
        sparepartInfoTableHTML += '</td><td>';
        sparepartInfoTableHTML += sp_manufacturer;
        sparepartInfoTableHTML += '</td></tr>';
        sparepartInfoTableHTML += '<tr><td class="label">';
        sparepartInfoTableHTML += ReturnPhraseOnKey('sp_info_price');
        sparepartInfoTableHTML += '</td><td>';
        sparepartInfoTableHTML += sp_price;
        sparepartInfoTableHTML += '</td></tr>';
        sparepartInfoTableHTML += '<tr><td class="label">';
        sparepartInfoTableHTML += ReturnPhraseOnKey('sp_info_dimensions');
        sparepartInfoTableHTML += '</td><td>';
        sparepartInfoTableHTML += sp_dimensions;
        sparepartInfoTableHTML += '</td></tr>';
        sparepartInfoTableHTML += '<tr><td class="label">';
        sparepartInfoTableHTML += ReturnPhraseOnKey('sp_info_unit');
        sparepartInfoTableHTML += '</td><td>';
        sparepartInfoTableHTML += sp_unit;
        sparepartInfoTableHTML += '</td></tr>';
        sparepartInfoTableHTML += '<tr><td class="label">';
        sparepartInfoTableHTML += ReturnPhraseOnKey('sp_info_note');
        sparepartInfoTableHTML += '</td><td>';
        sparepartInfoTableHTML += sp_note;
        sparepartInfoTableHTML += '</td></tr>';

        sparepartInfoTableHTML += '</table>';

        $("#sparepart_info_details").html(sparepartInfoTableHTML);
    }

    self.generateSparePartStockList = function () {
        c1('sparepartinfo row 183: Generating sparepart stocks table');

        var stockInfoSectionHTML = '';

        if (self.currentSparePartInfo.StockList.StockElementList.length == 0) {
            c1('sparepartinfo row 188: no stocks detected');
            stockInfoSectionHTML += '<h4>' + ReturnPhraseOnKey('sp_info_no_stocks_defined') + '</h4>';
            c1('sparepartinfo row 190: ' + stockInfoSectionHTML);
        }
        else {

            jQuery.each(self.currentSparePartInfo.StockList.StockElementList, function (i, stock_element) {

                var storeName = unspecifiedFromPM5(stock_element.StockStore);
                var stockQty = unspecifiedFromPM5(stock_element.StockInStore);
                var position_I = unspecifiedFromPM5(stock_element.StockPositionI);
                var position_II = unspecifiedFromPM5(stock_element.StockPositionII);
                var position_III = unspecifiedFromPM5(stock_element.StockPositionIII);



                var stockElementHTML = '<table class="sparepartinfo_stocks_table_light">';
                stockElementHTML += '<tr><td class="label">';
                stockElementHTML += ReturnPhraseOnKey('stockmanagementreplenish_store');
                stockElementHTML += '</td><td>';
                stockElementHTML += storeName;
                stockElementHTML += '</td></tr>';
                stockElementHTML += '<tr><td class="label">';
                stockElementHTML += ReturnPhraseOnKey('stockmanagementreplenish_area');
                stockElementHTML += '</td><td>';
                stockElementHTML += position_I;
                stockElementHTML += '</td></tr>';
                stockElementHTML += '<tr><td class="label">';
                stockElementHTML += ReturnPhraseOnKey('stockmanagementreplenish_section');
                stockElementHTML += '</td><td>';
                stockElementHTML += position_II;
                stockElementHTML += '</td></tr>';
                stockElementHTML += '<tr><td class="label">';
                stockElementHTML += ReturnPhraseOnKey('stockmanagementreplenish_position');
                stockElementHTML += '</td><td>';
                stockElementHTML += position_III;
                stockElementHTML += '</td></tr>';
                stockElementHTML += '<tr><td class="label">';
                stockElementHTML += ReturnPhraseOnKey('stockmanagementreplenish_qty_label');
                stockElementHTML += '</td><td>';
                stockElementHTML += stockQty;
                stockElementHTML += '</td></tr>';
                stockElementHTML += '</table>';

                stockInfoSectionHTML += stockElementHTML;

                stockElementHTML = '';
            });

        }

        $("#sparepart_stocks_details").html(stockInfoSectionHTML);

    }

    self.generateSparePartSupplierList = function () {
        c1('sparepartinfo row 245: Generating sparepart supplier table');

        var stockInfoSupplierHTML = '';

        if (self.currentSparePartInfo.SupplierList.HasContent == false) {
            c1('sparepartinfo row 250: no suppliers detected');
            stockInfoSupplierHTML += '<h4>' + ReturnPhraseOnKey('sp_info_no_suppliers_defined') + '</h4>';
            c1('sparepartinfo row 252: ' + stockInfoSupplierHTML);
        }
        else {
            

            jQuery.each(self.currentSparePartInfo.SupplierList.SparePartSuppliers, function (i, supplier_element) {

                var supplierName = unspecifiedFromPM5(supplier_element.SupplierName);
                var supplierAddress = unspecifiedFromPM5(supplier_element.SupplierAddress);
                var supplierZipCode = unspecifiedFromPM5(supplier_element.SupplierZipcode);
                var supplierPostalDistrict = unspecifiedFromPM5(supplier_element.SupplierPostalDistrict);
                var supplierPhone = unspecifiedFromPM5(supplier_element.SupplierPhone);
                var supplierFax = unspecifiedFromPM5(supplier_element.SupplierFaxNumber);

                var supplierElementHTML = self.supplierTableLayoutOdd ? '<div class="sparepartinfo_supplier_post_odd">' : '<div class="sparepartinfo_supplier_post_even">';

                self.supplierTableLayoutOdd = !self.supplierTableLayoutOdd;

                supplierElementHTML += '<b>' + supplierName + '</b>';
                if (supplier_element.SupplierPreferred == true) {
                    supplierElementHTML += " " + ReturnPhraseOnKey('sp_info_sup_preferred');
                }
                
                supplierElementHTML += '<br>';

                if (supplierAddress == ReturnPhraseOnKey('global_unspecified')) {
                    supplierElementHTML += ReturnPhraseOnKey('sp_info_sup_address') + ' ' + ReturnPhraseOnKey('global_unspecified').toLowerCase();
                }
                else {
                    supplierElementHTML += supplierAddress;
                }

                supplierElementHTML += '<br>';

                if (supplierZipCode == ReturnPhraseOnKey('global_unspecified')) {
                    supplierElementHTML += ReturnPhraseOnKey('sp_info_sup_zipcode') + ' ' + ReturnPhraseOnKey('global_unspecified').toLowerCase();
                    supplierElementHTML += '<br>';
                }
                else {
                    supplierElementHTML += supplierZipCode;
                    supplierElementHTML += ' ';
                }

                

                if (supplierPostalDistrict == ReturnPhraseOnKey('global_unspecified')) {
                    supplierElementHTML += ReturnPhraseOnKey('sp_info_sup_postal_district') + ' ' + ReturnPhraseOnKey('global_unspecified').toLowerCase();
                }
                else {
                    supplierElementHTML += supplierPostalDistrict;
                }

                supplierElementHTML += '<br>';

                if (supplierPhone == ReturnPhraseOnKey('global_unspecified')) {
                    supplierElementHTML += ReturnPhraseOnKey('sp_info_sup_phone') + ' ' + ReturnPhraseOnKey('global_unspecified').toLowerCase();
                }
                else {
                    supplierElementHTML += ReturnPhraseOnKey('sp_info_sup_phone') + ' ' + supplierPhone;
                }

                supplierElementHTML += '<br>';

                if (supplierFax == ReturnPhraseOnKey('global_unspecified')) {
                    supplierElementHTML += ReturnPhraseOnKey('sp_info_sup_fax') + ' ' + ReturnPhraseOnKey('global_unspecified').toLowerCase();
                }
                else {
                    supplierElementHTML += ReturnPhraseOnKey('sp_info_sup_fax') + ' ' + supplierFax;
                }



                supplierElementHTML += '</div>';

                stockInfoSupplierHTML += supplierElementHTML;

                supplierElementHTML = '';
            });

        }

        $("#sparepart_info_suppliers_details").html(stockInfoSupplierHTML);
    }


    return self;
}

var sparePartInfo = Sparepartinfo();


$(document).on("pagebeforeshow", "#page_sparepartinfo", function (e) {

    sparePartInfo.stockTableLayoutLight = true;
    sparePartInfo.supplierTableLayoutOdd = true;

    if (SessionTokenStatus == true) {
        $("#sparepartinfo_errortext").hide();

        //$("#sparepartconsumption_back_button").unbind().click(function () {
        //    $(":mobile-pagecontainer").pagecontainer("change", "#page_finishjobwizard_1", { role: "page", reverse: false, changeHash: false });
        //    e3('sparepartdetail row 167');
        //});


        sparePartInfo.requestSparePartInfo();
    }
    else {
        helper.ForcedLogout();
    }

});

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

    c2('sparepartinfo row 150 - running one time event hookups');

});


//20200401 - all functionality moved to pagebeforeshow event
//$(document).on("pageshow", "#page_sparepartinfo", function (e) {

//    if (SessionTokenStatus == true) {
//        $("#sparepartinfo_errortext").hide();

//        //$("#sparepartconsumption_back_button").unbind().click(function () {
//        //    $(":mobile-pagecontainer").pagecontainer("change", "#page_finishjobwizard_1", { role: "page", reverse: false, changeHash: false });
//        //    e3('sparepartdetail row 167');
//        //});


//        sparePartInfo.requestSparePartInfo();
//    }
//    else {
//        helper.ForcedLogout();
//    }

//});

