function Faultdetailhistorical() {

    var self = {};

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


    self.requestFaultInfo = function () {
        var fault_search_param = $.cookie("currently_selected_fault");
        var current_session_token = $.cookie("session_token");

        var requester = new AjaxRequest({
            data: { "fault_id": fault_search_param, "session_token": current_session_token },// This is for the json request
            success: self.FaultInfoReceptionSuccess,
            error: self.FaultInfoReceptionFailure,
            url: "RequestSelectedFaultInfo"
        });
        requester.ExecuteRequest();

    }

    self.requestFaultStopList = function () {
        var fault_search_param = $.cookie("currently_selected_fault");
        c2('From requestFaultStopList');
        c2('Fault ID: ' + fault_search_param);
        var requester = new AjaxRequest({
            data: { "fault_id": fault_search_param }, // This is for the json request
            success: self.FaultStopInfoReceptionSuccess,
            error: self.FaultStopInfoFailure,
            url: "RequestStopPeriodsForFault"
        });
        requester.ExecuteRequest();
    }



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

        $('#faultpagehistorical_details').empty();
        $("#faultdetailhistorical_description_content").empty();
        $("#faultdetailhistorical_solution_content").empty();



        if (data.d.HasContent == true && data.d.SessionTokenValid == true) {

            console.log(data.d);

            var faultID = data.d.FaultID;
            var jobID = data.d.JobID;
            var discoveredBy = data.d.DiscoveredBy;
            var discoveredOn = data.d.DiscoveredOn;
            var faultCause = data.d.FaultCause;
            if (faultCause == 'Unspecified') {
                faultCause = ReturnPhraseOnKey('global_unspecified');
            }

            var faultType = data.d.FaultType;
            var machineName = data.d.MachineName;
            var machineNumber = data.d.MachineNumber;
            var resolvedOn = data.d.ResolvedOn;

            if ((resolvedOn == 'Unspecified') || (resolvedOn == '')) {
                resolvedOn = ReturnPhraseOnKey('global_unspecified');
            }

            var signingPerson = data.d.SigningPerson;
            if (signingPerson == 'Unspecified') {
                signingPerson = ReturnPhraseOnKey('global_unspecified');
            }

            var faultDescription = data.d.FaultDescription;
            var faultSolution = data.d.FaultSolution;

            var selectedPriorityCode = data.d.SelectedPriorityCode;
            var cleanupStatus = data.d.CleanupFlag;
            var selectedJobID = data.d.JobID;

            $("#faultdetailhistorical_description_content").html(faultDescription);
            $("#faultdetailhistorical_solution_content").html(faultSolution);

            var tableTest = '<table id="faultdetail_table">';
            tableTest += '<tr><td class="label">';
            tableTest += ReturnPhraseOnKey('faultdetailhistorical_jobid');
            tableTest += '</td><td>';
            tableTest += jobID;
            tableTest += '</td></tr>';
            tableTest += '<tr><td class="label">';
            tableTest += ReturnPhraseOnKey('faultdetailhistorical_machinename');
            tableTest += '</td><td>';
            tableTest += machineName;
            tableTest += '</td></tr>';
            tableTest += '<tr><td class="label">';
            tableTest += ReturnPhraseOnKey('faultdetailhistorical_machinenumber');
            tableTest += '</td><td>';
            tableTest += machineNumber;
            tableTest += '</td></tr>';
            tableTest += '<tr><td class="label">';
            tableTest += ReturnPhraseOnKey('faultdetailhistorical_discoveredon');
            tableTest += '</td><td>';
            tableTest += discoveredOn;
            tableTest += '</td></tr>';
            tableTest += '<tr><td class="label">';
            tableTest += ReturnPhraseOnKey('activitydetailwithfunction_selectedprioritycode');
            tableTest += '</td><td>';
            if (selectedPriorityCode.length == '') {
                tableTest += ReturnPhraseOnKey('global_unspecified');
            }
            else {
                tableTest += selectedPriorityCode;
            }
            tableTest += '</td></tr><tr><td class="label">';
            tableTest += ReturnPhraseOnKey('faultdetailhistorical_discoveredby');
            tableTest += '</td><td>';
            tableTest += discoveredBy;
            tableTest += '</td></tr>';
            tableTest += '<tr><td class="label">';
            tableTest += ReturnPhraseOnKey('faultdetailhistorical_faulttype');
            tableTest += '</td><td>';
            tableTest += faultType;
            tableTest += '</td></tr>';
            tableTest += '<tr><td class="label">';
            tableTest += ReturnPhraseOnKey('faultdetailhistorical_faultcause');
            tableTest += '</td><td>';
            tableTest += faultCause;
            tableTest += '</td></tr>';
            tableTest += '<tr><td class="label">';
            tableTest += ReturnPhraseOnKey('faultdetailhistorical_resolvedon');
            tableTest += '</td><td>';
            tableTest += resolvedOn;
            tableTest += '</td></tr>';
            tableTest += '<tr><td class="label">';
            tableTest += ReturnPhraseOnKey('faultdetailhistorical_signingperson');
            tableTest += '</td><td>';
            tableTest += signingPerson;
            tableTest += '</td></tr>';
            if (CLEANUP_ACTIVE == true) {

                tableTest += '<tr><td class="label">' + ReturnPhraseOnKey('activitydetailwithfunction_cleanup_status') + '</td><td>';
                if (cleanupStatus == true) {
                    tableTest += ReturnPhraseOnKey('global_yes');
                }
                else if (cleanupStatus == false) {
                    tableTest += ReturnPhraseOnKey('global_no');
                }
                else {
                    tableTest += ReturnPhraseOnKey('global_unspecified');
                }
                tableTest += '</td></tr>';
            }
            tableTest += '</table>'

            $('#faultdetailhistorical_details').html(tableTest);
        }
        else if (data.d.SessionTokenValid == false) {
            //$.mobile.navigate("#dialog_tokenexpired", { role: "dialog" });
            $("#faultdetailhistorical_generic_popup_header").html(ReturnPhraseOnKey("dialogue_tokenexpired_message1"));
            $("#faultdetailhistorical_generic_popup_text").html(ReturnPhraseOnKey("dialogue_tokenexpired_message1"));

            $("#faultdetailhistorical_generic_popup").off().on("popupafterclose", function (event, ui) {
                $(":mobile-pagecontainer").pagecontainer("change", "#page_login", { role: "page", reverse: false, changeHash: false });
                e2('faultdetailhistorical row 173');
            });

            $("#faultdetailhistorical_generic_popup_button").unbind("click").click(function () {
                $("#faultdetailhistorical_generic_popup").popup("close");
            });

            $("#faultdetailhistorical_generic_popup").popup().popup("open");
        }
        else if (data.d.HasContent == false) {
            c1("faultdetailhistorical row 182: No unfinished faults for this machine.");
        }
    }

    self.FaultInfoReceptionFailure = function (data, status, xhr) {
        $('#faultdetailhistorical_details').empty();
        c1('faultdetailhistorical row 188: Unable to retrieve fault information from database.');
        DbCommunicationErrorHandler(WEBSERVICE_FAULT_JSONREQUEST);
    }

    self.FaultStopInfoReceptionSuccess = function (data, status, xhr) {
        c1('faultdetailwithfunction row 396: received stop list');
        console.log(data.d);
        var stoplist = data.d.StopListForFault;
        if (stoplist.length > 0) {

            c4('stop list present');
            $('#faultdetailhistorical_stoplist').html(self.BuildFaultStopSection(data.d));

            if ($('#faultdetailhistorical_fault_stoplist').is(":hidden")) {
                $('#faultdetailhistorical_fault_stoplist').show();
            }


        }
        else {
            c4('no machine stops registered');
        }
    }

    self.FaultStopInfoReceptionFailure = function (data, status, xhr) {
        c1('faultdetailhistorical row 211: Unable to retrieve stop period from database.');
        DbCommunicationErrorHandler(WEBSERVICE_FAULT_JSONREQUEST);
    }

    self.BuildFaultStopSection = function (incomingData) {
        c2('++++++');
        c2(incomingData);
        //stoplistHTML += '<tr><td class="label">' + 'Start' + '</td><td class="label">' + 'End' + '</td><td class="label">' + 'Stop Type' + '</td><td class="label">' + 'Machine Only' + '</td></tr>'
        stoplistHTML = '';

        jQuery.each(incomingData.StopListForFault, function () {
            var startdate = localizeDatePickerValue(this.StartDate);
            var starttime = this.StartTime;
            var enddate = localizeDatePickerValue(this.EndDate);
            var endtime = this.EndTime;
            var machineOnly = this.MachineOnly;
            var stopType = this.StopType;
            stoplistHTML += '<table id="stop_detail_table" class="job_info_table">';
            stoplistHTML += '<tr><td class="label">';
            stoplistHTML += ReturnPhraseOnKey('stop_period_start');
            stoplistHTML += '</td><td>';
            stoplistHTML += startdate;
            stoplistHTML += ' ';
            stoplistHTML += starttime
            stoplistHTML += '</td></tr>';
            stoplistHTML += '<tr><td class="label">';
            stoplistHTML += ReturnPhraseOnKey('stop_period_stop');
            stoplistHTML += '</td><td>';
            stoplistHTML += enddate;
            stoplistHTML += ' ';
            stoplistHTML += endtime
            stoplistHTML += '</td></tr>';
            stoplistHTML += '<tr><td class="label">';
            stoplistHTML += ReturnPhraseOnKey('stop_period_selected_machine_only');
            stoplistHTML += '</td><td>';
            if (machineOnly == true) {
                stoplistHTML += ReturnPhraseOnKey('global_yes');
            }
            else {
                stoplistHTML += ReturnPhraseOnKey('global_no');
            }
            stoplistHTML += '</td></tr>';
            stoplistHTML += '<tr><td class="label">';
            stoplistHTML += 'Stop type';
            stoplistHTML += '</td><td>';
            if (stopType == 'Unspecified') {
                stoplistHTML += ReturnPhraseOnKey('global_unspecified');
            }
            else {
                stoplistHTML += ReturnPhraseOnKey('stop_period_type');
            }
            stoplistHTML += '</td></tr></table><br>';

        });


        console.log(stoplistHTML);
        return stoplistHTML;
    }

    return self;
}

var faultDetailHistorical = Faultdetailhistorical();

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

    $("#faultdetailhistorical_backbutton").click(function () {
        $(":mobile-pagecontainer").pagecontainer("change", "#page_machinedetail", { role: "page", reverse: false, changeHash: true });
        e2('faultdetailhistorical row 204');
    });


    $('#faultdetailhistorical_fault_description').bind('collapsibleexpand', function () {
        var position = $('#faultdetailhistorical_fault_description').offset().top;
        $.mobile.silentScroll(position);

    }).bind('collapsiblecollapse', function () {});

    $('#faultdetailhistorical_fault_solution').bind('collapsibleexpand', function () {
        var position = $('#faultdetailhistorical_fault_solution').offset().top;
        $.mobile.silentScroll(position);

    }).bind('collapsiblecollapse', function () {});
});


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

    $('#faultdetailhistorical_fault_stoplist').hide();
    $('#faultdetailhistorical_errortext').hide();

    if (SessionTokenStatus == true) {
        faultDetailHistorical.requestFaultInfo();
        faultDetailHistorical.requestFaultStopList();
    }
    else {
        helper.ForcedLogout();
    }
});


//************************************************************************
//20200401 - moved to pagebeforeshow event
//$(document).on("pageshow", "#page_faultdetailhistorical", function (e) {
//    if (SessionTokenStatus == true) {
//        $('#faultdetailhistorical_errortext').hide();

//        faultDetailHistorical.requestFaultInfo();
//    }
//    else {
//        helper.ForcedLogout();
//    }
//});
//*************************************************************************


$(document).on("pagehide", "#page_faultdetailhistorical", function () {
    $('#faultdetailhistorical_fault_description').collapsible("collapse");
    $('#faultdetailhistorical_fault_solution').collapsible("collapse");
});