﻿
if (typeof (this._27581059B35A4C8291B1BFB8DA5CA476) === 'undefined') {
    this._27581059B35A4C8291B1BFB8DA5CA476 = null;

    if (typeof (this.undefinedType) == "undefined")
        this.undefinedType = "undefined";

    Boolean.parseBool = function(value) {
        if (value === undefined || value === null) {
            return false;
        }

        if (typeof (value) === "string") {
            return value.toLowerCase() === "true";
        }

        return !!value;
    };

    Boolean.parseBoolean = Boolean.parseBool;

    if (typeof (this.eVision) === undefinedType) {
        this.eVision = {};
    }

    if (typeof (this.eVision.Utilities) === undefinedType) {
        this.eVision.Utilities = {};
    }

    this.eVision.Utilities.arrayConstructor = [].constructor;
    this.eVision.Utilities.functionConstructor = function() { } .constructor;
    this.eVision.Utilities.global = this;

    if (typeof (Object.prototype.isArray) == undefinedType)

    //    if (typeof(object) == undefinedType)
    //        object = function(o){
    //            function F(){};
    //            F.prototype = o;
    //            return new F();
    //        }

        this.eVision.Utilities.importMembers = function(elements) {
            if (typeof (elements) === 'string') {
                elements = eVision.Utilities.getNamespace(elements);
            }

            for (elem in elements) {
                if (elements.hasOwnProperty(elem)) {
                    this[elem] = elements[elem];
                }
            }
        };

    this.eVision.Utilities.getNamespace = function(namespaceName) {
        var recursivelyFetch = function(elements) {
            var iter = eVision.Utilities.global;

            for (elem in elements) {
                if (elements.hasOwnProperty(elem)) {
                    var value = elements[elem];
                    if (typeof (iter[value]) === undefinedType) {
                        iter[value] = {};
                    }
                    iter = iter[value];
                }
            }

            if (typeof iter.importMembers === undefinedType)
                iter.importMembers = eVision.Utilities.importMembers;

            return iter;
        };

        if (eVision.Utilities.isArray(namespaceName)) {
            return recursivelyFetch(namespaceName);
        }
        else if (typeof (namespaceName) === 'string') {
            var elements = namespaceName.split(".");
            return recursivelyFetch(elements);
        }
        else {
            throw "unexcepted parameter";
        }
    };

    this.eVision.Utilities.isArray = function(elem) {
        return elem.constructor === eVision.Utilities.arrayConstructor;
    };

    this.eVision.Utilities.Types = {
        Boolean: "boolean",
        Number: "number",
        Float: "float",
        Integer: "integer",
        String: "string",
        Object: "object",
        Function: "function"
    };

    this.eVision.Utilities.registerJQueryExtensions = function() {
        $id = function(id) {
            var element = document.getElementById(id);
            if (element !== null) {
                return jQuery(element);
            }
            else {
                return null;
            }
        };
    };

    this.eVision.Utilities.KeyCodes =
    {
        Enter : 13,
        Escape : 27
    };

    this.eVision.Utilities.Exception = function(message) {
        var that = {};
        that.Message = message;
        that.toString = function() {
            return message;
        };

        return that;
    }

    this.eVision.Utilities.addJQueryProperty = function(element, name, defaultValue, type) {
        element[name] = function(value) {
            if (value == undefined) {
                var value = element.attr(name);

                if (value === undefined) {
                    value = defaultValue;
                }

                if (type == undefined) {
                    return value;
                }
                else if (type == eVision.Utilities.Types.String) {
                    return value;
                }
                else if (type == eVision.Utilities.Types.Boolean) {
                    return Boolean.parseBoolean(value);
                }
                else if (type == eVision.Utilities.Types.Integer) {
                    return parseInt(value);
                }
                else if (type == eVision.Utilities.Types.Float) {
                    return parseFloat(value);
                }
                else {
                    throw "Unknown format";
                }
            }
            else {
                element.attr(name, value);
            }
        }
    }

    String.prototype.format = function(o) {
        return this.replace(/{([^}]*)}/g, function(a, b) {
            if (b == "{") {
                return "{";
            }
            else {
                var r = o[b];
                if (typeof (r) === eVision.Utilities.undefinedType)
                    throw "Key " + a + " not found.";
                return typeof r === 'string' || typeof r === 'number' ? r : a;
            }
        }
      );
    };

    //if (typeof Object.prototype.importMembers != 'undefined')
    //    throw '"importMembers" is already defined';

    //    Object.prototype.importMembers = function(elements) {
    //        if (typeof (elements) === 'string') {
    //            elements = eVision.Utilities.getNamespace(elements);
    //        }

    //        for (elem in elements) {
    //            if (elements.hasOwnProperty(elem)) {
    //                this[elem] = elements[elem];
    //            }
    //        }
    //    };

    this.eVision.Utilities.CIMSep = function(p, seq) {
        var mo = CMSep();
        if (!mo) return null;
        mo.setAttribute("sequence", p);
        for (var i = 0; i < p.childNodes.length; i++) {
            var iSeq = p.childNodes[i].getAttribute("sequence");
            if (iSeq) {
                if (iSeq > seq) {
                    p.childNodes[i].parentNode.insertBefore(mo, p.childNodes[i]);
                    return mo;
                }
            }
        }
        AChld(p, mo);
        return mo;
    };

    this.eVision.Utilities.EmptyValue = function(elemId, emptyText, emptyStyle, filledStyle) {
        var box = $id(elemId);
        var emptyMark = "isItEmpty";

        box.focus(function() {
            if (box.attr(emptyMark) === null || box.attr(emptyMark) === "") {
                box.val("");
                box.css(filledStyle);
            }
            else {
            }
        });

        box.blur(function() {
            if (box.val() === "") {
                box.attr(emptyMark, "");
                box.css(emptyStyle);
                box.val(emptyText);
            }
            else {
                box.attr(emptyMark, "false");
            }
        });

        box.blur();
    };

    this.eVision.Utilities.TextBoxEnter = function(textBoxId, onClickElementId) {
        var textBox = $id(textBoxId);
        var onClickElement = $id(onClickElementId);

        textBox.keydown(function(event) {
            if (event.keyCode && ((event.which == 13) || (event.keyCode == 13))) {
                onClickElement.click();
                return false;
            }
            else {
                return true;
            }
        });
    };

    this.eVision.Utilities.getNamespace("eVision.Utilities").importMembers({
        Delegate: function(instance, method) {
            return function() {
                return method.apply(instance, arguments);
            }
        },
        Guard: {
            IsObject: function(value) {
                if (typeof value === "undefined") {
                    throw "Value undefined";
                }

                if (value === null) {
                    throw "Value is null";
                }
            }
        },
        RegisterControls: function(controls) {
            var g = this.Guard.IsObject;
            $.each(controls, function() {
                g(this["Type"]);
                g(this["Parameters"]);
                
                ev.getNamespace(this["Type"])(this["Parameters"]);
            });
        }
    });

    if (typeof (jQuery) !== undefinedType) {
        this.eVision.Utilities.registerJQueryExtensions();
    }

    ev = eVision.Utilities;
}