﻿/* ********************************************************** */
/* AllUserTabSwitch command                                    */        
/* ********************************************************** */
(function () {
    $(document).ready(function() {
        // Retrieve.
        var command = GuiController.getCommand('AllUserTabSwitch');
        if (!command) {
            return;
        }

        // validateInvoke handler.        
        command.validateInvoke = function() {
            var result = -1;
            if (ECBCalculator.tabManager.validateActiveTab() === false) {
                result = 0;
                command.data.cancel = true;
            }
            return result;
        };
    });
})();

/* ********************************************************** */
/* AllUserTabNavigateToNext command                                    */        
/* ********************************************************** */
(function () {
    $(document).ready(function() {
        // Retrieve.
        var command = GuiController.getCommand('AllUserTabNavigateToNext');
        if (!command) {
            return;
        }

        // validateInvoke handler.        
        command.validateInvoke = function() {
            var result = -1;
            if (ECBCalculator.tabManager.validateActiveTab() === false) {
                result = 0;
            }
            return result;
        };
        
        // onInvoke handler.        
        command.onInvoke = function() {
            ECBCalculator.tabManager.navigateToNext();
        };
    });
})();

/* ********************************************************** */
/* NavigatePrevTab command                                    */        
/* ********************************************************** */
(function () {
    $(document).ready(function() {
        // Retrieve.
        var command = GuiController.getCommand('AllUserTabNavigateToPrevious');
        if (!command) {
            return;
        }

        // validateInvoke handler.        
        command.validateInvoke = function() {
            var result = -1;
            if (ECBCalculator.tabManager.validateActiveTab() === false) {
                result = 0;
            }
            return result;
        };
        
        // onInvoke handler.        
        command.onInvoke = function() {
            ECBCalculator.tabManager.navigateToPrevious();
        };
    });
})();

/* ********************************************************** */
/* AllUserSave command                                     */        
/* ********************************************************** */
(function () {
    $(document).ready(function() {
        // Retrieve.
        var command = GuiController.getCommand('AllUserSave');
        if (!command) {
            return;
        }

        // validateInvoke handler.        
        command.validateInvoke = function() {
            var result = -1;
            if (ECBCalculator.tabManager.validateActiveTab() === false) {
                result = 0;
            }
            return result;
        };
        
        // refreshUI handler.        
        command.refreshUI = function() {
            var menuItem = _calculatorToolbar.GetItemByName(command.name);
            if (menuItem) { 
                menuItem.SetEnabled(command.state);
            }            
        };
        command.refreshUI();
    });
})();

/* ********************************************************** */
/* AllUserValidateQuestionnaire command                       */        
/* ********************************************************** */
(function () {
    $(document).ready(function() {
        // Retrieve.
        var command = GuiController.getCommand('AllUserValidateQuestionnaire');
        if (!command) {
            return;
        }

        // validateInvoke handler.        
        command.validateInvoke = function() {
            var result = -1;
            if (ECBCalculator.tabManager.validateActiveTab() === false) {
                result = 0;
            }
            else if (ECBCalculator.isStateDraft() === true && 
                     ECBCalculator.tabManager.areQuestionnaireTabsLoaded() === false) {
                result = 1;
            }
            return result;
        };
        
        // refreshUI handler.        
        command.refreshUI = function() {
            var menuItem = _calculatorToolbar.GetItemByName(command.name);
            if (menuItem) {
                menuItem.SetEnabled(command.state);
            }
        };
        command.refreshUI();
    });
})();

/* ********************************************************** */
/* AllUserUploadImage command                               */
/* ********************************************************** */
(function() {
    $(document).ready(function() {
        // Retrieve.
        var command = GuiController.getCommand('AllUserUploadImage');
        if (!command) {
            return;
        }

        // onInvoke handler.        
        command.onInvoke = function() {
            _imageFileUploader.Upload();
        };

        // validateInvoke handler.        
        command.validateInvoke = function() {
            var result = -1;
            var fileName;
            var extension;
            // Ensure all tabs are valid.
            fileName = _imageFileUploader.GetText();
            if (fileName === "") {
                result = 1;
            }
            else {
                extension = KeaneUtils.getFileExtension(fileName);
                if (extension !== "GIF" & 
                    extension !== "JPG" &
                    extension !== "BMP" & 
                    extension !== "PNG" & 
                    extension !== "TIFF") {
                    result = 2;
                }
            }
            return result;
        };        
        
        // refreshUI handler.        
        command.refreshUI = function() {
            if (window['_imageFileUploaderButton']) {
                _imageFileUploaderButton.SetEnabled(command.state);
                _imageFileUploaderButton.SetVisible(command.state);
                _imageFileUploader.SetVisible(command.state);
            }
        };
        command.refreshUI();
    });
})();