function Sparepartdetail() {

    var self = {};
    var current_machine = null;
    var current_userid = null;
    var current_sparepart_nr = null;





    self.requestSparePartInfo = function () {
        current_sparepart_id = $.cookie("currently_selected_sparepartid");
        current_sparepart_number = $.cookie("currently_selected_sparepartnumber");
        current_machine = $.cookie("currently_selected_machine");
        current_userid = $.cookie("user_id");
        var current_session_token = $.cookie("session_token");

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


        var requester = new AjaxRequest({
            data: {
                "userid": current_userid, "sparepart_id": current_sparepart_id,
                "machine_id": current_machine, "sparepart_nr": current_sparepart_number,
                "session_token": current_session_token
            },
            success: self.SparePartInfoRequestComplete,
            error: self.SparePartInfoRequestIncomplete,
            url: "RequestSparePartInformationForNewConsumption"
        });
        requester.ExecuteRequest();

    }

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

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

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

            $("#sparepartdetail_generic_popup_button").unbind("click").click(function () {
                $("#sparepartdetail_generic_popup").popup("close");
            });

            $("#sparepartdetail_generic_popup").popup().popup("open");
        }
        else {
            console.log(data.d.SparePartInformation);
            var sparePartInfo = data.d.SparePartInformation;
            /* 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 += '<li class="list-element-class" 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 += '<li class="list-element-class" 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('sparepartdetail row 108: Failed to retrieve spare part information. Unable to reach web service.');
        DbCommunicationErrorHandler(WEBSERVICE_FAULT_JSONREQUEST);
    }




    self.attachEventToActivityList = function () {
        $('#sparepartdetail_storelist').on('click', 'li', function () {
            //alert($(this).text());
            var selectedStock = $(this).data("stockid");

            if ((selectedStock !== undefined) && (selectedStock == 0)) {
                $.cookie("stock_selected_for_new_consumption", selectedStock);
                StoreSelectedSemaphore = STORE_NOT_SELECTED;

                //var stockIDFromCookie = $.cookie("stock_selected_for_new_consumption");

                $(":mobile-pagecontainer").pagecontainer("change", "#page_createnewconsumption", { role: "page", reverse: false, changeHash: false });
            }
            else if ((selectedStock !== undefined) && (selectedStock > (0))) {
                $.cookie("stock_selected_for_new_consumption", selectedStock);
                StoreSelectedSemaphore = STORE_SELECTED;

                //var stockIDFromCookie = $.cookie("stock_selected_for_new_consumption");

                $(":mobile-pagecontainer").pagecontainer("change", "#page_createnewconsumption", { role: "page", reverse: false, changeHash: false });
                e3('sparepartdetail row 136');
            }
        });

    }




    return self;
}

var sparePartDetail = Sparepartdetail();




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

    sparePartDetail.attachEventToActivityList();

}
  );

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

    if (SessionTokenStatus == true) {
        $("#sparepartdetail_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');
        });
        sparePartDetail.requestSparePartInfo();
    }
    else {
        helper.ForcedLogout();
    }

});



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

//    if (SessionTokenStatus == true) {
//        $("#sparepartdetail_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');
//        });
//        sparePartDetail.requestSparePartInfo();
//    }
//    else {
//        helper.ForcedLogout();
//    }

//});

