
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
// Start and stop timer
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//

function AjaxCheckStartTimerForLoggedInUser() {

    // Use selection and filter choices to create new records
    var ContentType = GetSelectedContentType();
    var SelectedRecordId = GetSelectedRecordId();
    var SelectedTableId = GetSelectedRecordTableId();
    var SelectedDataType = GetSelectedDataType();
    var FilterJsonString = GetSelectedContentPaneFilterJsonString();
    var SettingsJsonString = GetSelectedContentPaneSettingsJsonString();

    var params = {
        "ContentType": ContentType,
        "SelectedDataType": SelectedDataType,
        "SelectedTableId": SelectedTableId,
        "SelectedRecordId": SelectedRecordId,
        "FilterJsonString": FilterJsonString,
        "SettingsJsonString": SettingsJsonString,
        "StopRunningTimer": "false",
    };

    $.ajax({
        url: VirtualDirectory + "/Home/AjaxCheckStartTimerForLoggedInUser",
        type: "POST",
        data: JSON.stringify(params),
        contentType: "application/json",
        success: function (response) {
            if (response.Success) {
                if (response.TimerStarted) {

                    // Timer created
                    // Edit the new record
                    OpenEditRecordDialog(true/*bNewRecord*/, response.ObjectDataType, response.ObjectTable, response.ObjectRecordId);
                }
                else {
                    // Timer not started
                    ToastError("Start Timer", response.TimerMsg);
                    PopupMessageBox("Start Timer", response.TimerMsg)
                }
            }
            else {
                ToastError("Start Timer", response.ErrorResults);
            }
        },
        error: function (xhr, status, error) {
            var errormsg = "Error " + xhr.status + ", " + xhr.statusText;
            ToastError("AjaxCheckStartTimerForLoggedInUser", errormsg);
            console.log('AJAX: ' + errormsg);
        }
    });

}


function AjaxCheckStopTimerForLoggedInUser() {

    var params = {
    };

    $.ajax({
        url: VirtualDirectory + "/Home/AjaxCheckStopTimerForLoggedInUser",
        type: "POST",
        data: JSON.stringify(params),
        contentType: "application/json",
        success: function (response) {
            if (response.Success) {

                if (response.TimerStopped) {

                    // Timer stopped
                    ToastMessage("Stop Timer", response.TimerMsg);
                    PopupMessageBox("Stop Timer", response.TimerMsg);

                    // Refresh
                    SetSelectRecordIdAfterRebuild(response.ObjectTable, response.ObjectRecordId);
                    RefreshSelectedContent();
                }
                else {
                    // Timer not stopped
                    ToastError("Stop Timer", response.TimerMsg);
                }
            }
            else {
                ToastError("Start Timer", response.ErrorResults);
            }
        },
        error: function (xhr, status, error) {
            var errormsg = "Error " + xhr.status + ", " + xhr.statusText;
            ToastError("AjaxCheckStopTimerForLoggedInUser", errormsg);
            console.log('AJAX: ' + errormsg);
        }
    });

}


function AjaxCheckTimerStatusForLoggedInUser() {

    var params = {
    };

    $.ajax({
        url: VirtualDirectory + "/Home/AjaxCheckTimerStatusForLoggedInUser",
        type: "POST",
        data: JSON.stringify(params),
        contentType: "application/json",
        success: function (response) {
            if (response.Success) {

                ToastMessage("Timer Status", response.TimerMsg);
                PopupMessageBox("Timer Status", response.TimerMsg);
            }
            else {
                ToastError("Timer Status", response.ErrorResults);
            }
        },
        error: function (xhr, status, error) {
            var errormsg = "Error " + xhr.status + ", " + xhr.statusText;
            ToastError("AjaxCheckTimerStatusForLoggedInUser", errormsg);
            console.log('AJAX: ' + errormsg);
        }
    });

}



function AjaxStartTimerFromRecordForLoggedInUser(ContentType, sDbTable, sRecordId) {

    // Use selection and filter choices to create new records
    var ContentType = GetSelectedContentType();
    var SelectedRecordId = sRecordId;
    var SelectedTableId = sDbTable;
    var SelectedDataType = GetSelectedDataType();
    var FilterJsonString = GetSelectedContentPaneFilterJsonString();
    var SettingsJsonString = GetSelectedContentPaneSettingsJsonString();

    var params = {
        "ContentType": ContentType,
        "SelectedDataType": SelectedDataType,
        "SelectedTableId": SelectedTableId,
        "SelectedRecordId": SelectedRecordId,
        "FilterJsonString": FilterJsonString,
        "SettingsJsonString": SettingsJsonString,
        "StopRunningTimer": "true",
    };

    $.ajax({
        url: VirtualDirectory + "/Home/AjaxCheckStartTimerForLoggedInUser",
        type: "POST",
        data: JSON.stringify(params),
        contentType: "application/json",
        success: function (response) {
            if (response.Success) {
                if (response.TimerStarted) {

                    // Timer created
                    ToastMessage("Timer", response.TimerMsg);
                    RefreshSelectedContent();
                }
                else {
                    // Timer not started
                    ToastError("Start Timer", response.TimerMsg);
                    PopupMessageBox("Start Timer", response.TimerMsg)
                }
            }
            else {
                ToastError("Start Timer", response.ErrorResults);
            }
        },
        error: function (xhr, status, error) {
            var errormsg = "Error " + xhr.status + ", " + xhr.statusText;
            ToastError("AjaxStartTimerFromRecordForLoggedInUser", errormsg);
            console.log('AJAX: ' + errormsg);
        }
    });

}




/**
 * Called when "Start Timer" image in Project Tasks page is clicked
 * Start a timer for this project task
 * See stdata.ProjectTaskSet where image column is created
 * */
function OnProjectTaskStartTimerImageClicked(ContentType, sDbTable, sRecordId) {

    AjaxStartTimerFromRecordForLoggedInUser(ContentType, sDbTable, sRecordId);
}


