function Config() {
    /// <summary>This "class" is used for some global config variables such as the WMS url</summary>
    /// <returns type="Object">Config object</returns>

    /// <field name="webServiceURL" type="String">WMS url</field>

    var self = {};

    //---------------MABE development and debug paths
    //self.webServiceURL = "http://localhost/PM5MobileDBService/Service1.asmx"; //new PC office local configuration
    //self.webServiceURL = "http://localhost/WebServiceBravoBis/Service1.asmx"; //old PC office local configuration
    //self.webServiceURL = "http://192.168.0.103/PM5MobileWebService/Service1.asmx"; //home newtork configuration
    //self.webServiceURL = "http://localhost/PM5MobileWebService/Service1.asmx"; //home local configuration
    //-------------------------------------------------------

    //*****************************************************************************************************
    //YOU ARE TO EDIT THIS PATH SO IT POINTS TO THE WEB SERVICE THAT COMMUNICATES WITH DATABASE

    self.webServiceURL = "/PM5Backend/Service1.asmx";

    //*****************************************************************************************************


    self.readPersonalSettings = function () {
        var rx = new RegExp(/^[-+]?\d*$/);

        //        var currentFromCookie = $.cookie("timeperiod_current");

        var currentPastFromCookie = $.cookie("timeperiod_current_past");
        var currentFutureFromCookie = $.cookie("timeperiod_current_future");

        var recentFromCookie = $.cookie("timeperiod_recent");
        var futureFromCookie = $.cookie("timeperiod_future");
        var defaultStoreFromCookie = $.cookie("default_sp_store");
        var defaultReplenishmentReason = $.cookie("default_replenishmentreason");
        var defaultAdjustmentReason = $.cookie("default_adjustmentreason");
        var defaultAccount = $.cookie("default_account");
        var jobListSortingOrder = $.cookie("joblist_sorting_direction");
        var rememberPersonalFilterSettings = $.cookie("remember_personal_filter_settings");



        //        console.log(currentFromCookie);

        //if ((defined(currentFromCookie)) && (rx.test(currentFromCookie)) && (currentFromCookie > 0)) {
        //    console.log(DATERANGE_CURRENT);
        //    DATERANGE_CURRENT = currentFromCookie;
        //    console.log(DATERANGE_CURRENT);
        //}
        //else {
        //    console.log('current is not defined');
        //}


        if ((defined(currentPastFromCookie)) && (rx.test(currentPastFromCookie)) && (currentPastFromCookie > 0)) {
            if (currentPastFromCookie > 0) {
                DATERANGE_CURRENT_PAST = currentPastFromCookie * (-1);
            }
            else {
                DATERANGE_CURRENT_PAST = currentPastFromCookie;
            }

        }
        else {
            c1('config row 62: current past is not defined');
        }

        if ((defined(currentFutureFromCookie)) && (rx.test(currentFutureFromCookie)) && (currentFutureFromCookie > 0)) {
            DATERANGE_CURRENT_FUTURE = currentFutureFromCookie;

        }
        else {
            c1('config row 70: current past is not defined');
        }

        if ((defined(recentFromCookie)) && (rx.test(recentFromCookie)) && (recentFromCookie > 0)) {
            DATERANGE_RECENT = recentFromCookie * (-1);
        }
        else {
            c1('config row 77: recent is not defined');
        }

        if ((defined(futureFromCookie)) && (rx.test(futureFromCookie)) && (futureFromCookie > 0)) {
            DATERANGE_UPCOMING = futureFromCookie;
        }
        else {
            c1('config row 84: upcoming is not defined');
        }

        if ((defined(defaultStoreFromCookie)) && (rx.test(defaultStoreFromCookie)) && (defaultStoreFromCookie > 0)) {
            DEFAULT_SELECTED_STORE = defaultStoreFromCookie;
        }
        else {
            DEFAULT_SELECTED_STORE = NO_STORE_SELECTED;
            c1('config row 92: value from cookie read in config is pootch - ' + DEFAULT_SELECTED_STORE);
        }

        if (defined(defaultReplenishmentReason)) {
            DEFAULT_REPLENISHMENT_STOCK_CHANGE_REASON = defaultReplenishmentReason;
        }
        else {
            DEFAULT_REPLENISHMENT_STOCK_CHANGE_REASON = NO_REASON_SELECTED;
        }

        

        if (defined(defaultAdjustmentReason)) {
            DEFAULT_ADJUSTMENT_STOCK_CHANGE_REASON = defaultAdjustmentReason;
        }
        else {
            DEFAULT_ADJUSTMENT_STOCK_CHANGE_REASON = NO_REASON_SELECTED;
        }

        //Here

        if (defined(jobListSortingOrder)) {
            JOB_LIST_SORTING_ORDER = jobListSortingOrder;
        }
        else {
            JOB_LIST_SORTING_ORDER = JOB_LIST_SORTING_ORDER_ASCENDING;
        }

        if (defined(defaultAccount)) {
            DEFAULT_ACCOUNT = defaultAccount;
            SELECTED_ACCOUNT = DEFAULT_ACCOUNT;
        }
        else {
            DEFAULT_ACCOUNT = NO_ACCOUNT_SELECTED;
            SELECTED_ACCOUNT = DEFAULT_ACCOUNT;
        }

        if (defined(rememberPersonalFilterSettings)) {
            REMEMBER_PERSONAL_FILTER_BETWEEN_SESSIONS = rememberPersonalFilterSettings;
        }
        else {
            REMEMBER_PERSONAL_FILTER_BETWEEN_SESSIONS = "false";
        }
    }

    self.setTechnicalGroupsInJobList = function () {
        var requester = new AjaxRequest({
            data: {},
            success: function (data, status, xhr) {
                //c_objects(data.d);
                var techGroupListElement = '<option value="0">';
                techGroupListElement += ReturnPhraseOnKey('mainpersonallist_no_technical_group_selected');
                techGroupListElement += '</option>';
                if (defined(data.d.AvailableTechnicalGroups) && (data.d.AvailableTechnicalGroups.length > 0)) {
                    jQuery.each(data.d.AvailableTechnicalGroups, function () {
                        techGroupListElement += '<option value="' + this.TechnicalGroupId + '">' + this.TechnicalGroupName + '</option>'
                    });

                    $("#joblistpanel-techgroup-combo").html(techGroupListElement);
                    $("#joblistpanel-techgroup-combo").selectmenu().selectmenu("refresh");
                    $("#joblistpanel-techgroup-combo").val(0);
                    $("#joblistpanel-techgroup-combo").selectmenu("refresh");
                }

            },
            error: function (request, status, error) {
                c1('config row 140: Unable to retrieve stock change reason list from database.');
                DbCommunicationErrorHandler(WEBSERVICE_FAULT_JSONREQUEST);
            },
            url: "RequestTechnicalGroupsList"
        });

        requester.ExecuteRequest();
    }

    self.setTaskTypesInJobList = function () {
        var requester = new AjaxRequest({
            data: {},
            success: function (data, status, xhr) {
                var taskTypeElement = '<option value="0">';
                taskTypeElement += ReturnPhraseOnKey('mainpersonallist_no_task_type_selected');
                taskTypeElement += '</option>';
                if (defined(data.d.AvailableTaskTypes) && (data.d.AvailableTaskTypes.length > 0)) {
                    jQuery.each(data.d.AvailableTaskTypes, function () {
                        taskTypeElement += '<option value="' + this.TaskTypeId + '">' + this.TaskTypeName + '</option>'
                    });

                    $("#joblistpanel-tasktype-combo").html(taskTypeElement);
                    $("#joblistpanel-tasktype-combo").selectmenu().selectmenu("refresh");
                    $("#joblistpanel-tasktype-combo").val(0);
                    $("#joblistpanel-tasktype-combo").selectmenu("refresh");
                }

            },
            error: function (request, status, error) {
                c1('config row 170: Unable to retrieve stock change reason list from database.');
                DbCommunicationErrorHandler(WEBSERVICE_FAULT_JSONREQUEST);
            },
            url: "RequestTaskTypeList"
        });

        requester.ExecuteRequest();
    }

    self.setFaultTypesInJobList = function () {
        var requester = new AjaxRequest({
            data: {},
            success: function (data, status, xhr) {
                var faultTypeElement = '<option value="0">';
                faultTypeElement += ReturnPhraseOnKey('mainpersonallist_no_task_type_selected');
                faultTypeElement += '</option>';
                if (defined(data.d.AvailableFaultTypes) && (data.d.AvailableFaultTypes.length > 0)) {
                    jQuery.each(data.d.AvailableFaultTypes, function () {
                        faultTypeElement += '<option value="' + this.FaultTypeId + '">' + this.FaultTypeName + '</option>'
                    });

                    $("#joblistpanel-faulttype-combo").html(faultTypeElement);
                    $("#joblistpanel-faulttype-combo").selectmenu().selectmenu("refresh");
                    $("#joblistpanel-faulttype-combo").val(0);
                    $("#joblistpanel-faulttype-combo").selectmenu("refresh");
                }

            },
            error: function (request, status, error) {
                c1('config row 198: Unable to retrieve stock change reason list from database.');
                DbCommunicationErrorHandler(WEBSERVICE_FAULT_JSONREQUEST);
            },
            url: "RequestFaultTypeListInPlainFormat"
        });

        requester.ExecuteRequest();
    }

    self.setPriorityTypesInJobList = function () {
        var requester = new AjaxRequest({
            data: {},
            success: function (data, status, xhr) {
                var priorityCodeElement = '<option value="0">';
                priorityCodeElement += ReturnPhraseOnKey('mainpersonallist_no_priority_selected');
                priorityCodeElement += '</option>';
                if (defined(data.d.AvailablePriorityCodeList) && (data.d.AvailablePriorityCodeList.length > 0)) {
                    jQuery.each(data.d.AvailablePriorityCodeList, function () {
                        priorityCodeElement += '<option value="' + this.PriorityCodeId + '">' + this.PriorityCodeName + '</option>'
                    });
                }

                $("#joblistpanel-jobpriorities-combo").html(priorityCodeElement);
                $("#joblistpanel-jobpriorities-combo").selectmenu().selectmenu("refresh");
                $("#joblistpanel-jobpriorities-combo").val(0);
                $("#joblistpanel-jobpriorities-combo").selectmenu("refresh");

            },
            error: function (request, status, error) {
                c1('config row 227: Unable to retrieve stock change reason list from database.');
                DbCommunicationErrorHandler(WEBSERVICE_FAULT_JSONREQUEST);
            },
            url: "RequestAllActivePriorityCodes"
        });

        requester.ExecuteRequest();
    }


    self.populateSigningPersonelFilter = function () {
        var requester = new AjaxRequest({
            data: {},
            //As first element always add currently logged in user
            //As second element always add no selection
            //Thereafter add all users that are currently having jobs that are accessible by logged in user based on his location access credentials and are unfinished
            success: function (data, status, xhr) {
                var signingPersonElement = '<option value="';
                signingPersonElement += $.cookie("user_id");
                signingPersonElement += '">'
                signingPersonElement += ReturnPhraseOnKey('mainpersonallist_personal_list_combo_element');
                signingPersonElement += '</option>';

                signingPersonElement += '<option value="0">';
                signingPersonElement += ReturnPhraseOnKey('mainpersonallist_no_signing_person_selected');
                signingPersonElement += '</option>';

                if (defined(data.d.FaultSignatories) && (data.d.FaultSignatories.length > 0)) {
                    jQuery.each(data.d.FaultSignatories, function () {
                        if (parseInt(this.UserId) !== parseInt($.cookie("user_id"))) {
                            signingPersonElement += '<option value="' + this.UserId + '">' + this.Initials + '</option>'
                        }
                    });
                }

                $("#joblistpanel-signingperson-combo").html(signingPersonElement);
                $("#joblistpanel-signingperson-combo").selectmenu().selectmenu("refresh");
                $("#joblistpanel-signingperson-combo").val($.cookie("user_id"));
                $("#joblistpanel-signingperson-combo").selectmenu("refresh");

            },
            error: function (request, status, error) {
                c1('config row 239: Unable to retrieve stock change reason list from database.');
                DbCommunicationErrorHandler(WEBSERVICE_FAULT_JSONREQUEST);
            },
            url: "RequestUserListForJobFilter"
        });

        requester.ExecuteRequest();
    }


    self.initializeJobTypesFilter = function () {

        var jobTypeToShow_UserSelection = '<option value="0">' + ReturnPhraseOnKey('mainpersonallist_jobtype_combo_activities_and_faults') + '</option>';
        jobTypeToShow_UserSelection += '<option value="1">' + ReturnPhraseOnKey('mainpersonallist_jobtype_combo_activities_only') + '</option>';
        jobTypeToShow_UserSelection += '<option value="2">' + ReturnPhraseOnKey('mainpersonallist_jobtype_combo_faults_only') + '</option>';

        $("#joblistpanel-jobtype-combo").html(jobTypeToShow_UserSelection);
        $("#joblistpanel-jobtype-combo").selectmenu().selectmenu("refresh");
        $("#joblistpanel-jobtype-combo").val(0);
        $("#joblistpanel-jobtype-combo").selectmenu("refresh");
    }

    self.checkForMachineAccountExistence = function () {
        var requester = new AjaxRequest({
            data: { "session_token": $.cookie("session_token") },
            success: function (data, status, xhr) {

                if (defined(data.d.TrueOrFalse) && isnull(data.d.TrueOrFalse) == false) {
                    ACCOUNTS_EXIST = data.d.TrueOrFalse;
                }
            },
            error: function (request, status, error) {
                c1('config row 271: Unable to retrieve PM5 client settings from database');
                DbCommunicationErrorHandler(WEBSERVICE_FAULT_JSONREQUEST);
            },
            url: "CheckForMachineAccountExistence"
        });

        requester.ExecuteRequest();

    }



    self.requestPM5ClientSettings = function () {
        var requester = new AjaxRequest({
            data: { "user_id": $.cookie("user_id"), "session_token": $.cookie("session_token") },
            success: function (data, status, xhr) {

                if (defined(data.d.Pm5NavigationTreeHeaderName) && isnull(data.d.Pm5NavigationTreeHeaderName) == false) {
                    if (data.d.Pm5NavigationTreeHeaderName.length > 0) {
                        TOP_NODE_NAME = data.d.Pm5NavigationTreeHeaderName;
                    }
                    else {
                        TOP_NODE_NAME = DEFAULT_TOP_NODE_NAME;
                        c1('config row 294: didn\'t find top node name');
                    }
                }
            },
            error: function (request, status, error) {
                c1('config row 299: Unable to retrieve PM5 client settings from database');
                DbCommunicationErrorHandler(WEBSERVICE_FAULT_JSONREQUEST);
            },
            url: "RequestPM5ClientSettings"
        });

        requester.ExecuteRequest();
    }


    return self;
}



var config = Config();

$(document).ready(function () {
    c1('config row 317: Executing Initialization');
    //$.mobile.popup.prototype.options.history = false;
    config.readPersonalSettings();

    //$(function () { $('#jstree_demo_div').jstree(); });

    //$('#jstree_demo_div').jstree({
    //    'core': {
    //        'data': [
    //           'Simple root node',
    //           {
    //               'text': 'Root node 2',
    //               'state': {
    //                   'opened': true,
    //                   'selected': true
    //               },
    //               'children': [
    //                 { 'text': 'Child 1' },
    //                 'Child 2'
    //               ]
    //           }
    //        ]
    //    }
    //});


    $(".return-to-main").unbind().click(function (event) {
        $(":mobile-pagecontainer").pagecontainer("change", "#page_mainmenu", { role: "page", reverse: false, changeHash: false });
        e1('config.js row 345');
    });
});

//Handles save of edits for activity/fault description texts when page is closed prematurely

$(document).on("pagecontainerbeforeshow", function (event, ui) {
    var currentPageId = ui.prevPage.attr('id') /* reset value of .foo element on current page */
    if ((currentPageId == 'page_activitydetailwithfunction') && (activityDetailWithFunction.EditFlag)) {
        c2('config row 353 - triggering pagecontainerbeforeshow for activitydetailwithfunction');
        var textToShow = ReturnPhraseOnKey("activityfault_saverequest_text_b")
        var r = confirm(textToShow);
        if (r == true) {
            activityDetailWithFunction.SaveActivityNoteEdit();
            activityDetailWithFunction.ActivityNoteReadOnly();
            activityDetailWithFunction.EditFlag = false;

        } else {
            activityDetailWithFunction.ActivityNoteReadOnly();
            activityDetailWithFunction.EditFlag = false;
        }
        swapButton($('#activitydetailwithfunction_noteedit_toggle_button'));
    }

    if (currentPageId == 'page_faultdetailwithfunction' && (faultDetailWithFunction.FaultDescriptionEditFlag == true || faultDetailWithFunction.FaultSolutionEditFlag == true)) {
        c2('config row 353 - triggering pagecontainerbeforeshow for faultdetailwithfunction');
        var textToShow = ReturnPhraseOnKey("activityfault_saverequest_text_b")
        var r = confirm(textToShow);
        if (r == true) {

            if (faultDetailWithFunction.FaultDescriptionEditFlag == true) {

                faultDetailWithFunction.SaveFaultDescriptionEditNote();
                faultDetailWithFunction.FaultDescriptionReadOnly();
                faultDetailWithFunction.FaultDescriptionEditFlag = false;
            }
            if (faultDetailWithFunction.FaultSolutionEditFlag == true) {
                faultDetailWithFunction.SaveFaultSolutionEditNote();
                faultDetailWithFunction.FaultSolutionReadOnly();
                faultDetailWithFunction.FaultSolutionEditFlag = false;

            }

            swapButton($('#afaultdetailwithfunction_descriptionnote_toggle_button'));
        }
        else {
            if (faultDetailWithFunction.FaultDescriptionEditFlag == true) {

                faultDetailWithFunction.FaultDescriptionReadOnly();
                faultDetailWithFunction.FaultDescriptionEditFlag = false;
            }
            if (faultDetailWithFunction.FaultSolutionEditFlag == true) {
                faultDetailWithFunction.FaultSolutionReadOnly();
                faultDetailWithFunction.FaultSolutionEditFlag = false;

            }

            swapButton($('#faultdetailwithfunction_solutionnote_toggle_butto'));

        }

    }
});
