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




function SubmitTimesheetForApproval() {
    AjaxBeginSubmitTimesheet();
}



function AjaxBeginSubmitTimesheet() {

    var filterjsonstring = LoadSelectedPaneFilterJsonStringFromCookie();
    var settingsjsonstring = LoadSelectedPaneSettingsJsonStringFromCookie();

    var params = {
        "FilterJsonString": filterjsonstring,
        "SettingsJsonString": settingsjsonstring
    };

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

            if (response.Success) {

                // Setup complete?
                if (response.SetupSubmitTimesheetComplete) {
                    PopupYesNoWithCallback(response.PromptTitle, response.PromptMessage, FinishSubmitTimesheetAfterDelay);
                    return;
                }
                else if (response.PromptTitle && response.PromptMessage) {
                    PopupErrorMessage(response.PromptTitle, response.PromptMessage);
                    return;
                }

            }
            else {
                ToastError("AjaxBeginSubmitTimesheet", response.ErrorResults);
            }
        },
        error: function (xhr, status, error) {
            var errormsg = "Error " + xhr.status + ", " + xhr.statusText;
            ToastError("AjaxBeginSubmitTimesheet", errormsg);
            console.log('AJAX: ' + errormsg);
        }
    });

}



/**
 * HACK: This is necessary because popup modal dialogs take a while to close
 *       If a second popup occurs soon after a previous one closes, everything is messed up
 * */
function FinishSubmitTimesheetAfterDelay() {
    setTimeout(function () { AjaxFinishSubmitTimesheet(); }, 1000);
}


function AjaxFinishSubmitTimesheet() {

    console.log("AjaxFinishSubmitTimesheet: top of function");

    var filterjsonstring = LoadSelectedPaneFilterJsonStringFromCookie();
    var settingsjsonstring = LoadSelectedPaneSettingsJsonStringFromCookie();

    var params = {
        "FilterJsonString": filterjsonstring,
        "SettingsJsonString": settingsjsonstring
    };

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

            console.log("AjaxFinishSubmitTimesheet: success response: " + response.Success);

            if (response.Success) {

                console.log("AjaxFinishSubmitTimesheet: success prompt: " + response.PromptTitle + ", " + response.PromptMessage);

                // Show results
                if (response.PromptTitle && response.PromptMessage) {
                    console.log("AjaxFinishSubmitTimesheet: success PopupInfoMessage: " + response.PromptTitle + ", " + response.PromptMessage);
                    PopupInfoMessage(response.PromptTitle, response.PromptMessage);
                    return;
                }
            }
            else {
                var sMsg = response.ErrorResults;
                if (!sMsg)
                    sMsg = "Hmmm, an unknown error occurred!";
                ToastError("AjaxFinishSubmitTimesheet", sMsg);
            }
        },
        error: function (xhr, status, error) {
            var errormsg = "Error " + xhr.status + ", " + xhr.statusText;
            ToastError("AjaxFinishSubmitTimesheet", errormsg);
            console.log('AJAX: ' + errormsg);
        }
    });
}


