﻿/* ********************************************************** */
/* Extension methods                                          */        
/* ********************************************************** */
/* ********************************************************** */
/* Object extension:  protoypal factory method                */        
/* See http://javascript.crockford.com/prototypal.html        */        
/* ********************************************************** */
if (typeof Object.create !== 'function') {
    Object.create = function (o) {
        function F() {}
        F.prototype = o;
        return new F();
    };
}

/* ********************************************************** */
/* Object extension:  is array determination                  */        
/* ********************************************************** */
if (typeof Object.isArray !== 'function') {
    Object.isArray = function (value) {
        return 
            Object.prototype.toString.apply(value) === '[object Array]';
    };
}

/* ********************************************************** */
/* Utility methods declaration                                */        
/* ********************************************************** */
if (!this.KeaneUtils) {
    KeaneUtils = {};
}
(function() {

    // Toggles the visibilty state of a dom element
    if (typeof KeaneUtils.toggleVisibilityState !== 'function') {
        KeaneUtils.toggleVisibilityState = function(elementID, state) {
            var domElement = document.getElementById(elementID);
            if (domElement) {
                if (state) {
                    domElement.style.display = 'inline';
                }
                else {
                    domElement.style.display = 'none';
                }
            }
        };
    }

    // Redirects the browser to the passed url                    
    if (typeof KeaneUtils.redirectToUrl !== 'function') {
        KeaneUtils.redirectToUrl = function(url) {
            if ((url !== null) &&
                (url.toString().length > 0)) {
                window.location = url;
            }
        };
    }

    // Opens a new browser window at the passed url               
    if (typeof KeaneUtils.openUrl !== 'function') {
        KeaneUtils.openUrl = function(url) {
            if ((url !== null) &&
                (url.toString().length > 0)) {
                window.open(url, "_blank", "");
            }
        };
    }

    // Throws an error                                                    
    if (typeof KeaneUtils.throwError !== 'function') {
        KeaneUtils.throwError = function(message, title) {
            var error = new Error();
            error.description = message;
            error.message = message;
            error.name = title;
            throw (error);
        };
    }

    // Returns the file extenstion of the passed file name
    if (typeof KeaneUtils.getFileExtension !== 'function') {
        KeaneUtils.getFileExtension = function(fileName) {
            var extensionRegExp = /.+\.([^.]+)$/;
            var result = "";
            if (fileName !== "") {
                var matches = extensionRegExp.exec(fileName);
                if (matches.length > 0) {
                    result = matches[1];
                }
            }
            return result.toString().toUpperCase();
        };
    }

    // disable control
    if (typeof KeaneUtils.initIndexControl !== 'function') {
        KeaneUtils.initIndexControl = function(controlId, index) {
            var i = parseInt(index);
            var control = window[controlId];
            if (control !== null && control !== undefined) {
                if (i >= 0 && i < control.GetItemCount()) {
                    control.SetSelectedIndex(i);
                }
            }
        };
    }

    // disable control
    if (typeof KeaneUtils.enableItemControl !== 'function') {
        KeaneUtils.enableItemControl = function(controlId, state) {
            var control = window[controlId];
            if (control !== null && control !== undefined) {
                control.SetEnabled(state);
            }
        };
    }

    // cache the list item control and remove it from the commbo
    if (typeof KeaneUtils.removeItemAt !== 'function') {
        KeaneUtils.removeItemAt = function(controlId, index) {
            var control = window[controlId];
            if (control !== null && control !== undefined) {
                var itemControl = control.GetItem(index);
                if (itemControl !== null && control !== itemControl) {
                    // controlKey is the name of the combo
                    if (!_cacheData.Contains(controlId)) {
                        _cacheData.Set(controlId, itemControl.text);
                    }
                    control.RemoveItem(index);
                }
            }
        };
    }

    // cache the list item control and remove it from the commbo
    if (typeof KeaneUtils.removeItemAtName !== 'function') {
        KeaneUtils.removeItemAtName = function(controlId, index, name) {
            var control = window[controlId];
            if (control !== null && control !== undefined) {
                var itemControl = control.GetItem(index);
                if (itemControl !== null && control !== itemControl) {
                    // controlKey is the name of the combo
                    if (!_cacheData.Contains(name)) {
                        _cacheData.Set(name, itemControl.text);
                    }
                    control.RemoveItem(index);
                }
            }
        };
    }

    // Retrieve the itemControl text from the cache and insert it back in the combo
    if (typeof KeaneUtils.insertItemAtName !== 'function') {
        KeaneUtils.insertItemAtName = function(controlId, index, value, name) {
            var control = window[controlId];
            if (control !== null && control !== undefined) {
                var textControlItem = _cacheData.Get(name);
                if (textControlItem !== null && textControlItem !== undefined) {
                    control.InsertItem(index, textControlItem, value, null);
                    _cacheData.Remove(name);
                }
            }
        };
    }
        
    // Retrieve the itemControl text from the cache and insert it back in the combo
    if (typeof KeaneUtils.insertItemAt !== 'function') {
        KeaneUtils.insertItemAt = function(controlId, index, value) {
            var control = window[controlId];
            if (control !== null && control !== undefined) {
                var textControlItem = _cacheData.Get(controlId);
                if (textControlItem !== null && textControlItem !== undefined) {
                    control.InsertItem(index, textControlItem, value, null);
                    _cacheData.Remove(controlId);
                }
            }
        };
    }

    // returns the selected index of a specific combo
    if (typeof KeaneUtils.GetSelectedIndex !== 'function') {
        KeaneUtils.GetSelectedIndex = function(controlId) {
            var control = window[controlId];
            var result = -1;
            if (control !== null && control !== undefined) {
                result = control.GetSelectedIndex();
            }
            return result;
        };
    }

})();
