
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
// Application options functions
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//


var bAutoRefreshIsChecked = false;
var nAutoRefreshMinutes = 5;
var nAutoRefreshIntervalId = 0;

var bToasterDisplayIsChecked = true;
var nToasterDisplaySeconds = 10;

var bBarcodeClearIsChecked = true;
var nBarcodeClearSeconds = 60;

var ThisSessionTimeoutMinutes = 0;
var nSessionTimeoutIntervalId = 0;


/**
 * Show a menu for the application options
 * Example: Email setup
 * @param {any} event
 */
function ShowApplicationOptionsMenu(event) {

    var menuitems = [];

    // Email Setup
    var EmailAccountsMenuItem = {};
    EmailAccountsMenuItem.text = "Email Setup";
    EmailAccountsMenuItem.clickhandler = "ShowEmailAccountsGridView()";
    menuitems.add(EmailAccountsMenuItem);

    // Refresh Active Page
    var PeriodicRefreshMenuItem = {};
    PeriodicRefreshMenuItem.text = "Auto-refresh Active Page";
    PeriodicRefreshMenuItem.clickhandler = "OnSetupRefreshActivePage()";
    menuitems.add(PeriodicRefreshMenuItem);

    // Display Toaster Messages
    var ToasterDisplayMenuItem = {};
    ToasterDisplayMenuItem.text = "Display Toaster Messages";
    ToasterDisplayMenuItem.clickhandler = "OnSetupToasterDisplay()";
    menuitems.add(ToasterDisplayMenuItem);

    // Clear Barcode Values
    var BarcodeClearMenuItem = {};
    BarcodeClearMenuItem.text = "Clear Barcode Values";
    BarcodeClearMenuItem.clickhandler = "OnSetupBarcodeClear()";
    menuitems.add(BarcodeClearMenuItem);

    var menu = new ContextMenu(menuitems);
    menu.display(event);
}






//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
// Auto-refresh



/**
 * Load auto-refresh values from cookies
 */
function LoadAutoRefreshValuesFromCookies() {

    bAutoRefreshIsChecked = LoadAutoRefreshCheckedFromCookie();
    nAutoRefreshMinutes = LoadAutoRefreshMinutesFromCookie();
}

function SaveAutoRefreshValuesToCookies() {

    SaveAutoRefreshCheckedToCookie(bAutoRefreshIsChecked);
    SaveAutoRefreshMinutesToCookie(nAutoRefreshMinutes);
}

function CreateAutoRefreshTimer() {

    // Always clear any running interval first — even when returning early due to disabled/zero
    // Without this, disabling auto-refresh leaks the old interval and it keeps firing
    ClearAutoRefreshTimer();

    if (!bAutoRefreshIsChecked)
        return;
    if (!nAutoRefreshMinutes || nAutoRefreshMinutes <= 0)
        return;

    var seconds = (nAutoRefreshMinutes * 60);
    DeferRefreshSelectedContent(seconds);

    // A function to be executed every delay milliseconds
    // Add an extra 1000ms to allow the refresh timer to time out before calling "Defer" again
    nMilliseconds = (nAutoRefreshMinutes * 60 * 1000);
    nAutoRefreshIntervalId = setInterval(OnAutoRefreshTimerTick, nMilliseconds + 1000);
}

function ClearAutoRefreshTimer() {

    if (nAutoRefreshIntervalId > 0)
        clearInterval(nAutoRefreshIntervalId);
    nAutoRefreshIntervalId = 0;
}

/**
 * Called on an interval to refresh the selected page
 */
function OnAutoRefreshTimerTick() {

    // DO NOT clear the last deferred timeout in "Defer...()"
    // Canceling this timeout causes auto-refresh to never update
    // Why? Each time "Defer...()" is called from here, the previous timeout is cancelled, which results in no grid updates
    // Remedy: Set the ID to 0 so it is not cancelled
    AppDeferTimeoutID = 0;

    // Create another timer
    var seconds = (nAutoRefreshMinutes * 60);
    DeferRefreshSelectedContent(seconds);
}


function OnSetupRefreshActivePage() {

    // Load from cookies
    LoadAutoRefreshValuesFromCookies();

    // Add a checkbox
    // Show special controls in the prompt-for-value dialog
    var sHtml = "<input type='checkbox' id='igDialogPromptForValueAutoRefreshCheckbox' name='igDialogPromptForValueAutoRefreshCheckbox' onclick='OnSetupRefreshActivePageChecked' ";
    if (bAutoRefreshIsChecked)
        sHtml += "checked=checked";
    sHtml += " >";
    sHtml += "<label for='igDialogPromptForValueAutoRefreshCheckbox'> Automatically refresh the active page</label><br>";
    $("#igDialogPromptForValueSpecialControls").html(sHtml);
    bHasSpecialPromptControls = true;

    // Prompt for new values
    var nMinutesBetweenRefreshes = nAutoRefreshMinutes;
    var prompt = { sTitle: "Refresh Active Page", sPrompt: "Minutes between page refreshes", sValue: nMinutesBetweenRefreshes, MbButtons: 1, MbIcon: 32/*StMessageBoxIcon.Question*/, bCollectValue: true, };
    OpenPromptForValueDialog(prompt, OnSetupRefreshActivePageOkCallback, null/*proceedobject*/);
}

function OnSetupRefreshActivePageChecked() {
}

/**
 * OK button clicked
 * Return the number of minutes between automatic page refreshes
 * @param {any} prompt
 * @returns
 */
function OnSetupRefreshActivePageOkCallback(prompt) {

    if (!prompt)
        return;
    if (!prompt.sValue)
        return;
    if (!jQuery.isNumeric(prompt.sValue))
        return;

    bAutoRefreshIsChecked = ($('#igDialogPromptForValueAutoRefreshCheckbox').is(':checked'));
    nAutoRefreshMinutes = parseInt(prompt.sValue);
    if (nAutoRefreshMinutes <= 0)
        bAutoRefreshIsChecked = false;

    SaveAutoRefreshValuesToCookies();
    CreateAutoRefreshTimer();

    if (bAutoRefreshIsChecked)
         ToastMessage("Refresh Active Page", "" + nAutoRefreshMinutes + " minutes");
    else ToastMessage("Refresh Active Page", "Off");
}






//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
// Toaster messages



/**
 * Load auto-refresh values from cookies
 */
function LoadToasterDisplayValuesFromCookies() {

    bToasterDisplayIsChecked = LoadToasterDisplayCheckedFromCookie();
    nToasterDisplaySeconds = LoadToasterDisplaySecondsFromCookie();
}

function SaveToasterDisplayValuesToCookies() {

    SaveToasterDisplayCheckedToCookie(bToasterDisplayIsChecked);
    SaveToasterDisplaySecondsToCookie(nToasterDisplaySeconds);
}



function OnSetupToasterDisplay() {

    // Load from cookies
    LoadToasterDisplayValuesFromCookies();

    // Add a checkbox
    // Show special controls in the prompt-for-value dialog
    var sHtml = "<input type='checkbox' id='igDialogPromptForValueToasterDisplayCheckbox' name='igDialogPromptForValueToasterDisplayCheckbox' ";
    if (bToasterDisplayIsChecked)
        sHtml += "checked=checked";
    sHtml += " >";
    sHtml += "<label for='igDialogPromptForValueToasterDisplayCheckbox'> Display toaster messages</label><br>";
    $("#igDialogPromptForValueSpecialControls").html(sHtml);
    bHasSpecialPromptControls = true;

    // Prompt for new values
    var prompt = { sTitle: "Toaster Messages", sPrompt: "Message duration (in seconds)", sValue: nToasterDisplaySeconds, MbButtons: 1, MbIcon: 32/*StMessageBoxIcon.Question*/, bCollectValue: true, };
    OpenPromptForValueDialog(prompt, OnSetupToasterDisplayOkCallback, null/*proceedobject*/);
}



/**
 * OK button clicked
 * Return the number of seconds to display toaster messages
 * @param {any} prompt
 * @returns
 */
function OnSetupToasterDisplayOkCallback(prompt) {

    if (!prompt)
        return;
    if (!prompt.sValue)
        return;
    if (!jQuery.isNumeric(prompt.sValue))
        return;

    bToasterDisplayIsChecked = ($('#igDialogPromptForValueToasterDisplayCheckbox').is(':checked'));
    nToasterDisplaySeconds = parseInt(prompt.sValue);
    if (nToasterDisplaySeconds <= 0)
        bToasterDisplayIsChecked = false;

    SaveToasterDisplayValuesToCookies();
    LoadToasterDisplayValuesFromCookies();

    // Show new choice
    if (bToasterDisplayIsChecked)
        ToastMessage("Toaster Display", "" + nToasterDisplaySeconds + " seconds");
    else {
        bToasterDisplayIsChecked = true;
        SaveToasterDisplayValuesToCookies();
        ToastMessage("Toaster Display", "Off");
        bToasterDisplayIsChecked = false;
        SaveToasterDisplayValuesToCookies();
    }
}







//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
// Barcode clear



/**
 * Load auto-refresh values from cookies
 */
function LoadBarcodeClearValuesFromCookies() {

    bBarcodeClearIsChecked = LoadBarcodeClearCheckedFromCookie();
    nBarcodeClearSeconds = LoadBarcodeClearSecondsFromCookie();
}

function SaveBarcodeClearValuesToCookies() {

    SaveBarcodeClearCheckedToCookie(bBarcodeClearIsChecked);
    SaveBarcodeClearSecondsToCookie(nBarcodeClearSeconds);
}



function OnSetupBarcodeClear() {

    // Load from cookies
    LoadBarcodeClearValuesFromCookies();

    // Add a checkbox
    // Show special controls in the prompt-for-value dialog
    var sHtml = "<input type='checkbox' id='igDialogPromptForValueBarcodeClearCheckbox' name='igDialogPromptForValueBarcodeClearCheckbox' ";
    if (bBarcodeClearIsChecked)
        sHtml += "checked=checked";
    sHtml += " >";
    sHtml += "<label for='igDialogPromptForValueBarcodeClearCheckbox'> Clear barcode values</label><br>";
    $("#igDialogPromptForValueSpecialControls").html(sHtml);
    bHasSpecialPromptControls = true;

    // Prompt for new values
    var prompt = { sTitle: "Barcode Clear", sPrompt: "Clear barcode values (in seconds)", sValue: nBarcodeClearSeconds, MbButtons: 1, MbIcon: 32/*StMessageBoxIcon.Question*/, bCollectValue: true, };
    OpenPromptForValueDialog(prompt, OnSetupBarcodeClearOkCallback, null/*proceedobject*/);
}



/**
 * OK button clicked
 * Return the number of seconds to display toaster messages
 * @param {any} prompt
 * @returns
 */
function OnSetupBarcodeClearOkCallback(prompt) {

    if (!prompt)
        return;
    if (!prompt.sValue)
        return;
    if (!jQuery.isNumeric(prompt.sValue))
        return;

    bBarcodeClearIsChecked = ($('#igDialogPromptForValueBarcodeClearCheckbox').is(':checked'));
    nBarcodeClearSeconds = parseInt(prompt.sValue);
    if (nBarcodeClearSeconds <= 0)
        bBarcodeClearIsChecked = false;

    SaveBarcodeClearValuesToCookies();
    LoadBarcodeClearValuesFromCookies();

    // Show new choice
    if (bBarcodeClearIsChecked)
         ToastMessage("Barcode Clear", "" + nBarcodeClearSeconds + " seconds");
    else ToastMessage("Barcode Clear", "Off");
}





//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
// Session timeout after 15 minutes





function ResetSessionTimeoutTimer() {

    if (!ThisSessionTimeoutMinutes || ThisSessionTimeoutMinutes <= 0)
        return;

    // Clear the old interval timer
    ClearSessionTimeoutTimer();

    nMilliseconds = (ThisSessionTimeoutMinutes * 60 * 1000);
    nSessionTimeoutIntervalId = setInterval(OnSessionTimeoutTimerTick, nMilliseconds);
}

function ClearSessionTimeoutTimer() {

    if (nSessionTimeoutIntervalId > 0)
        clearInterval(nSessionTimeoutIntervalId);
    nSessionTimeoutIntervalId = 0;
}

function OnSessionTimeoutTimerTick() {

    ClearSessionTimeoutTimer();
    AjaxLogout();

    setTimeout(function () { OpenLoginDialogFromCookieValues(); }, 1000);
}


