function Jobsearchonid() {

    var self = {};

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

    self.init = function () {
        self.clearErrorMsg();
        $("#jobselectonid_jobid_input").val("");
        self.update();
    };

    self.update = function () {
        initScanner(self);
    };

    self.handleScannedValues = function (barcode) {
        keyboardbuffer.set('');
    };

    self.verifyIncomingSearchParameter = function () {
        self.clearErrorMsg();
        var incomingParameter = $("#jobselectonid_jobid_input").val();

        if (incomingParameter == '') {
            self.setErrorMsg(ReturnPhraseOnKey('jobselectonid_errormessage_search_param_must_be_entered'), false);
        }
        else if (isInt(incomingParameter) != true) {
            self.setErrorMsg(ReturnPhraseOnKey('jobselectonid_errormessage_search_param_must_be_number'), true);
        }
        else if (parseInt(incomingParameter) > MAX_ALLOWED_JOB_ID) {
            self.setErrorMsg(ReturnPhraseOnKey('scan_barcode_error_scanned_job_id_is_not_valid'), true);
        }
        else {
            self.checkForJobExistence(incomingParameter);
        }
    };

    self.checkForJobExistence = function (idToVerify) {
        var current_session_token = $.cookie("session_token");

        var requester = new AjaxRequest({
            data: { "jobid": idToVerify, "userid": $.cookie("user_id") ,"session_token": current_session_token }, // This is for the json request
            success: self.JobExistenceRequestComplete,
            error: self.JobExistenceRequestIncomplete,
            url: "CheckForJobExistenceOnJobId"
        });
        requester.ExecuteRequest();

    }

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

        if (data.d.TrueOrFalse == false) {
            self.setErrorMsg(ReturnPhraseOnKey('jobselectonid_errormessage_no_job_with_such_id'), true);
        }
        else if (currently_selected_application_section == USER_SECTION) {
            if (data.d.StringToPass == "Finished") {
                var errorMessageToShow = ReturnPhraseOnKey('jobselectonid_errormessage_job_with_id_finished_1') + $("#jobselectonid_jobid_input").val() +
                    ReturnPhraseOnKey('jobselectonid_errormessage_job_with_id_finished_2')
                self.setErrorMsg(errorMessageToShow, true);
            }
            else if (data.d.StringToPass == "NoAccess") {
                var errorMessageToShow = ReturnPhraseOnKey('jobselectonid_errormessage_job_with_id_noaccess_1') + $("#jobselectonid_jobid_input").val() +
                    ReturnPhraseOnKey('jobselectonid_errormessage_job_with_id_noaccess_2')
                self.setErrorMsg(errorMessageToShow, true);
            }
            else {
                if (data.d.StringToPass == 'Activity') {

                    var selectedActivityID = data.d.ValueToPass;
                    var selectedFaultID = 0;

                    if (selectedActivityID !== undefined) {
                        $.cookie("currently_selected_activity", selectedActivityID);
                        $.cookie("currently_selected_fault", selectedFaultID);
                        $.cookie("currently_selected_machine", -1);
                        $("#jobselectonid_jobid_input").val("");
                        $(":mobile-pagecontainer").pagecontainer("change", "#page_activitydetailwithfunction", { role: "page", reverse: false, changeHash: false });
                        e1('jobsearchonid row 80');
                        self.clearErrorMsg();
                    }
                }
                else if (data.d.StringToPass == 'Fault') {

                    var selectedFaultID = data.d.ValueToPass;
                    var selectedActivityID = 0;


                    if (selectedFaultID !== undefined) {
                        $.cookie("currently_selected_fault", selectedFaultID);
                        $.cookie("currently_selected_activity", selectedActivityID);
                        $.cookie("currently_selected_machine", -1);
                        $("#jobselectonid_jobid_input").val("");
                        $(":mobile-pagecontainer").pagecontainer("change", "#page_faultdetailwithfunction", { role: "page", reverse: false, changeHash: false });
                        e1('jobsearchonid row 96');
                        self.clearErrorMsg();
                    }
                }
            }
        }
        else if (currently_selected_application_section == SPAREPARTS_SECTION) {
            $.cookie("currently_selected_jobid", $("#jobselectonid_jobid_input").val());
            $.mobile.navigate("#page_consumption_on_job", { role: "page" });
            e1('jobsearchonid row 105');
            $("#jobselectonid_jobid_input").val("");
        }

    }

    self.JobExistenceRequestIncomplete = function (data, status, xhr) {
        $("#jobselectonid_jobid_input").val("");
        c1("jobsearchonid row 112: Unable to reach web service. Can't verify presence of job search result.");
        DbCommunicationErrorHandler(WEBSERVICE_FAULT_JSONREQUEST);
    }

    self.setErrorMsg = function (msg, markCurrentValue) {

        $('#jobselectinid_errormessage').html(msg);
        $('#jobselectinid_errormessage').show();

        if (markCurrentValue == true) {
            $("#jobselectonid_jobid_input").focus();
            $("#jobselectonid_jobid_input").select();

        }
    }

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

    self.goToCameraScan = function (e) {
        initScanner(undefined);
        CURRENT_JOBID_SEARCH_MODE = JOBID_SEARCH_INPUT_BARCODE;
        SCAN_PAGE_TARGET = SCAN_JOB_ID;
        //$.mobile.navigate("#page_jobid_barcode_cam", { role: "page" });
        $.mobile.navigate("#page_scantest", { role: "page" });
        c1('jobsearchonid row 137 - navigating to barcode scan for direct job id scan');
    }

    return self;

}

var jobSearchOnId = Jobsearchonid();

$(document).one("pageshow", "#page_jobsearch_onid", function (e) {
    $('#jobselectonid_jobid_input').bind("triggerJobSearchOnJobID", function (e) {

        if (currently_selected_application_section == USER_SECTION || currently_selected_application_section == SPAREPARTS_SECTION) {
            jobSearchOnId.verifyIncomingSearchParameter();
        }
        else {
            //If user somehow got here unintentionally, go back to main menu
            //Add a fault message in main menu!!!

            currently_selected_application_section = NO_SECTION_SELECTED;
            $.mobile.navigate("#page_mainmenu", { role: "page" });
            e1('jobsearchonid row 158');
        }
    });

    $('#jobselectonid_jobid_input').keyup(function (e) {
        if (e.keyCode == 13) {
            $(this).trigger("triggerJobSearchOnJobID");
        }
    });

    $("#jobselectonid_searchbutton").click(function (event) {
        jobSearchOnId.verifyIncomingSearchParameter();
        e1('jobsearchonid row 171');
    });

    $('#jobsearch_onid_backbutton').click(function () {
        if (currently_selected_application_section == USER_SECTION) {
            $(":mobile-pagecontainer").pagecontainer("change", "#page_mainpersonallist", { role: "page", reverse: false, changeHash: true });
            e1('jobsearchonid row 177');
        }
        else if (currently_selected_application_section == SPAREPARTS_SECTION) {
            SELECTED_SPAREPART_JOB_TYPE = NO_STOCK_JOB_TYPE_SELECTION;
            $(":mobile-pagecontainer").pagecontainer("change", "#page_stores_functionselection", { role: "page", reverse: false, changeHash: true });
            //$.mobile.navigate("#page_sparepartstoreselect", { role: "page" });
            e1('jobsearchonid row 183');
        }
        else {
            c1('jobsearchonid row 181: Unable to decide what page to navigate to, returning to main personal list');
            currently_selected_application_section = USER_SECTION;
            $(":mobile-pagecontainer").pagecontainer("change", "#page_mainpersonallist", { role: "page", reverse: false, changeHash: true });
            e1('jobsearchonid row 189');
        }
    });

});

$(document).on("pagebeforeshow", "#page_jobsearch_onid", function (e) {
    if (SessionTokenStatus == true) {
        $("#jobselectinid_errormessage").hide();
        $("#jobselectonid_jobid_input").val("");
        $("#jobselectonid_jobid_input").focus();

        jobSearchOnId.init();
    }
});

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

    CURRENT_JOBID_SEARCH_MODE = JOBID_SEARCH_INPUT_MANUAL;

    if (SessionTokenStatus == true) {
        
        //****************************************
        //20200401 - moved to pagebeforeshow event

        //$("#jobselectinid_errormessage").hide();
        //$("#jobselectonid_jobid_input").val("");
        //$("#jobselectonid_jobid_input").focus();

        //jobSearchOnId.init();
        //****************************************

        $("#jobselectonid_camera").unbind().click(function (event) {
            jobSearchOnId.goToCameraScan(event);
            e1('jobsearchonid row 209');
        });

        //possible solution for reset of search parameter, may be premature
        //$.removeCookie('machinesearchparam');

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

$(document).on("pagehide", "#page_jobsearch_onid", function (e) {
    if (SessionTokenStatus == true) {
        $("#jobselectinid_errormessage").hide();
        $("#jobselectonid_jobid_input").val("");
        $("#jobselectonid_jobid_input").focus();

        jobSearchOnId.init();
    }
});