/**
 * +----------------------------------------------------------------------------+
 * |                          Jeffrey M. Hunter                                 |
 * |                      jhunter@idevelopment.info                             |
 * |                         www.idevelopment.info                              |
 * |----------------------------------------------------------------------------|
 * |      Copyright (c) 1998-2011 Jeffrey M. Hunter. All rights reserved.       |
 * |----------------------------------------------------------------------------|
 * | FILE       : DisplayPopupMessage.js                                        |
 * | CLASS      : Miscellaneous Functions                                       |
 * | PURPOSE    : Demonstrates how to display a WSH popup message box.          |
 * | PARAMETERS : None                                                          |
 * | USAGE      : cscript DisplayPopupMessage.js //NoLogo                       |
 * | NOTE       : As with any code, ensure to test this script in a development |
 * |              environment before attempting to run it in production.        |
 * +----------------------------------------------------------------------------+
 **/


// -----------------------------------------------------------------------------
//   SET GLOBAL VARIABLES AND ANY OBJECTS
// -----------------------------------------------------------------------------

var g_S_MARK = 16;          // Stop Mark
var g_Q_MARK = 32;          // Question Mark
var g_E_MARK = 48;          // Exclamation Mark
var g_I_MARK = 64;          // Information Mark

var g_BUTTON_OK  = 0;       // Ok Button
var g_BUTTON_OC  = 1;       // Ok, Cancel Button
var g_BUTTON_ARI = 2;       // Abort, Retry, Ignore
var g_BUTTON_YNC = 3;       // Yes, No, Cancel
var g_BUTTON_YN  = 4;       // Yes, No
var g_BUTTON_RC  = 5;       // Retry, Cancel

var g_DEFAULT_TIMEOUT = 5;  // Default timeout value for all popup message
                            // boxes.
var g_NO_TIMEOUT      = 0;  // Used to specify no timeout is to occur for
                            // popup message boxes.

var g_objShell;             // Global WSH Shell Object
g_objShell = new ActiveXObject("WScript.Shell");

var g_MAIN_RETURN = 0;      // Return value for Main. Used to return value
                            // to the calling program or operating environment.


// -----------------------------------------------------------------------------
//   - CALL MAIN FUNCTION
//   - THEN CLEAN UP OBJECTS AND EXIT SCRIPT
// -----------------------------------------------------------------------------

g_MAIN_RETURN = main();
g_objShell = null;
WScript.Quit(g_MAIN_RETURN);



/**
 * -----------------------------------------------------------------------------
 * timeoutExample
 *
 * Message box example that demonstrates how to timeout the dialog after a
 * amount of time (in seconds). This example uses a message box with an
 * Information Mark icon.
 *
 * Returns
 *      Returns an integer value that corresponds to the button clicked by
 *      the user.
 * 
 *      OK      =  1
 *      Timeout = -1
 * -----------------------------------------------------------------------------
 **/

function timeoutExample() {

    var strTitle   = "Message Box - (Timeout)";
    var strMessage = "Message Box Popup Examle - (Timeout)";
    var intResult;

    intResult = g_objShell.Popup(   strMessage
                                  , g_DEFAULT_TIMEOUT
                                  , strTitle
                                  , g_BUTTON_OK + g_I_MARK);
    return(intResult);
    
}


/**
 * -----------------------------------------------------------------------------
 * yesNoCancelExample
 *
 * Message box example that demonstrates how to use a Yes, No, Cancel popup
 * dialog. This example will use the Exclamation Mark for the message box icon
 * and will specify a value of 0 for the timeout - indicating that no timeout
 * should occur for this message box.
 *
 * Returns
 *      Returns an integer value that corresponds to the button clicked by
 *      the user.
 *
 *      Yes     = 6
 *      No      = 7
 *      Cancel  = 2
 * -----------------------------------------------------------------------------
 **/

function yesNoCancelExample() {

    var strTitle   = "Message Box - (Yes, No, Cancel)";
    var strMessage = "Message Box Popup Examle - (Yes, No, Cancel)";
    var intResult;

    intResult = g_objShell.Popup(   strMessage
                                  , g_NO_TIMEOUT
                                  , strTitle
                                  , g_BUTTON_YNC + g_E_MARK);
    return(intResult);
    
}


/**
 * -----------------------------------------------------------------------------
 * abortRetryIgnoreExample
 *
 * Message box example that demonstrates how to use a Abort, Retry, Ignore
 * popup dialog. This example will use the Stop Mark for the message box icon
 * and will specify a value of 0 for the timeout - indicating that no timeout
 * should occur for this message box.
 *
 * Returns
 *      Returns an integer value that corresponds to the button clicked by
 *      the user.
 *
 *      Abort   = 3
 *      Retry   = 4
 *      Ignore  = 5
 * -----------------------------------------------------------------------------
 **/

function abortRetryIgnoreExample() {

    var strTitle   = "Message Box - (Abort, Retry, Ignore)";
    var strMessage = "Message Box Popup Examle - (Abort, Retry, Ignore)";
    var intResult;

    intResult = g_objShell.Popup(   strMessage
                                  , g_NO_TIMEOUT
                                  , strTitle
                                  , g_BUTTON_ARI + g_S_MARK);
    return(intResult);
    
}


/**
 * -----------------------------------------------------------------------------
 * main
 *
 * Main function used to enclose the primary script logic.
 *
 * Returns
 *      Exit code from primary script logic.
 * -----------------------------------------------------------------------------
 **/

function main() {
    
    var intPopupResult;
    
    // -----------------------------------------
    // Timeout Example
    // -----------------------------------------
    WScript.Echo("Message Box Popup Example - (Timeout)")
    intPopupResult = timeoutExample();

    if (intPopupResult == 1) {
        WScript.Echo("You hit [OK].");
    } else if (intPopupResult == -1) {
        WScript.Echo("Timeout occured.");
    } else {
        WScript.Echo("Unexpected Exception.");
        WScript.Echo("Popup Result: " + intPopupResult);
    }

    // -----------------------------------------
    // Yes / No / Cancel Example
    // -----------------------------------------
    WScript.Echo("");
    WScript.Echo("Message Box Popup Example - (Yes, No, Cancel)")
    intPopupResult = yesNoCancelExample();

    if (intPopupResult == 6) {
        WScript.Echo("You hit [Yes].");
    } else if (intPopupResult == 7) {
        WScript.Echo("You hit [No].");
    } else if (intPopupResult == 2) {
        WScript.Echo("You hit [Cancel].");
    } else {
        WScript.Echo("Unexpected Exception.");
        WScript.Echo("Popup Result: " + intPopupResult);
    }

    // -----------------------------------------
    // Abort, Retry, Ignore
    // -----------------------------------------
    WScript.Echo("");
    WScript.Echo("Message Box Popup Example - (Abort, Retry, Ignore)")
    intPopupResult = abortRetryIgnoreExample();

    if (intPopupResult == 3) {
        WScript.Echo("You hit [Abort].");
    } else if (intPopupResult == 4) {
        WScript.Echo("You hit [Retry].");
    } else if (intPopupResult == 5) {
        WScript.Echo("You hit [Ignore].");
    } else {
        WScript.Echo("Unexpected Exception.");
        WScript.Echo("Popup Result: " + intPopupResult);
    }

    return(0);

}

