function Machinesearchresult() {

    var self = {};

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

    self.requestMachineList = function () {

        c8('machinesearchlist row 11 - refreshing machine search list');

        var machine_search_param = $.cookie("machinesearchparam");
        var machine_typeid_search_param = $.cookie("machinetypeidsearchparam");
        var current_session_token = $.cookie("session_token");
        var current_user_id = $.cookie("user_id");


        var requester = new AjaxRequest({
            data: {
                "machine_search_param": machine_search_param,
                "machine_type_id_search_param": machine_typeid_search_param,
                "search_all_locations": machineSearch.includeAllMachinesInSearchResult,
                "start_location_index": locationNavigation.currentLocationId,
                "user_id": current_user_id,
                "session_token": current_session_token
            }, // This is for the json request
            success: self.MachineListReceptionSuccess,
            error: self.MachineListReceptionFailure,
            url: "MachineSearchResult"
        });
        requester.ExecuteRequest();

    }

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

        var checkValidation = data.d.SessionTokenValid;
        var listData = data.d.AvailableMachines;



        if (checkValidation == false) {
            //$.mobile.navigate("#dialog_token_expired", { role: "dialog" });

            $("#machinelist_generic_popup_header").html(ReturnPhraseOnKey("dialogue_tokenexpired_message1"));
            $("#machinelist_generic_popup_text").html(ReturnPhraseOnKey("dialogue_tokenexpired_message1"));

            $("#machinelist_generic_popup").off().on("popupafterclose", function (event, ui) {
                $(":mobile-pagecontainer").pagecontainer("change", "#page_login", { role: "page", reverse: false, changeHash: false });
                e1('machinesearchlist row 48');
            });

            $("#machinelist_generic_popup_button").unbind("click").click(function () {
                $("#machinelist_generic_popup").popup("close");
            });

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

            jQuery.each(listData, function () {
                var machineLink = '<li class="list-header-class" role="heading" data-role="list-divider">';
                machineLink += this.MachineNumber;
                machineLink += '</li>';
                //machineLink += '<li class="list-element-class" data-theme="c" data-machinenumber="';
                machineLink += '<li class="list-element-class" data-machinenumber="';
                machineLink += this.MachineNumber;
                machineLink += '"><a href="#page9" data-transition="slide">';
                machineLink += this.MachineName;
                machineLink += '</a></li>';

                $("#machinelist_page_machine_list").append(machineLink);
            });

            $("#machinelist_page_machine_list").listview('refresh');
        }
    }

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

        c1('machinesearchlist row 79: Unable to retrieve search result from web service.');
        DbCommunicationErrorHandler(WEBSERVICE_FAULT_JSONREQUEST);
    }

    self.attachEventToList = function () {

        $('#machinelist_page_machine_list').on('click', 'li', function () {
            //alert($(this).text());
            var m_nr = $(this).data("machinenumber");
            if (m_nr !== undefined) {
                $.cookie("currently_selected_machine", m_nr);
                $.mobile.navigate("#page_machinedetail", { role: "page" });
                e1('machinesearchlist row 90');
            }
        });

    }


    return self;

}

var machineSearchResult = Machinesearchresult();

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

    machineSearchResult.attachEventToList();
});

$(document).on("pagebeforeshow", "#page_machinelist", function (e) {
    if (SessionTokenStatus == true) {
        $("machinelist_errormessage").hide();
        $("#machinelist_page_machine_list").empty();
        machineSearchResult.requestMachineList();
    }
    else {
        e.preventDefault();
        e.stopPropagation();
        helper.ForcedLogout();
    }

});

//$(document).on("pageshow", "#page_machinelist", function (e) {
//    if (SessionTokenStatus == true) {
//        $("machinelist_errormessage").hide();
//        $("#machinelist_page_machine_list").empty();
//        machineSearchResult.requestMachineList();
//    }
//    else {
//        helper.ForcedLogout();
//    }

//});

