﻿
// Global namespace
var Nexida = new Object();

Nexida.$browser = new Object();

(function() {
    var s = navigator.userAgent.toLowerCase();

    Nexida.$browser.IsIE = navigator.appName.indexOf("Microsoft") != -1;
    Nexida.$browser.IsIE7 = s.match(/msie (\d+)/) != null && (parseInt(s.match(/msie (\d+)/)[1], 10) >= 7);
    Nexida.$browser.IsMozilla = navigator.userAgent.indexOf("Mozilla") != -1 && navigator.userAgent.indexOf("Firefox") != -1;
    Nexida.$browser.IsSafari = typeof (s.Contains) != "undefined" ? s.Contains(' applewebkit/') : s.indexOf(' applewebkit/') != -1;
    Nexida.$browser.IsOpera = !!window.opera;
    Nexida.$browser.IsAIR = typeof (s.Contains) != "undefined" ? s.Contains(' adobeair/') : s.indexOf(' adobeair/') != -1;
    Nexida.$browser.IsMac = typeof (s.Contains) != "undefined" ? s.Contains('macintosh') : s.indexOf(' adobeair/') != -1;
    Nexida.$browser.IsGecko = (navigator.product == 'Gecko') && !Nexida.$browser.IsSafari && !Nexida.$browser.IsOpera;
    Nexida.$browser.IsGeckoLike = (Nexida.$browser.IsGecko || Nexida.$browser.IsSafari || Nexida.$browser.IsOpera);

    if (Nexida.$browser.IsGecko) {
        var geckoMatch = s.match(/rv:(\d+\.\d+)/);
        var geckoVersion = geckoMatch && parseFloat(geckoMatch[1]);

        if (geckoVersion) {
            Nexida.$browser.IsGecko10 = (geckoVersion < 1.8);
            Nexida.$browser.IsGecko19 = (geckoVersion > 1.8);
        }
    }
})();

Nexida.$getByPartialId = function(parent, partialId) {
    if (typeof (parent) != "undefined"
        && typeof (parent.childNodes) != "undefined") {
        for (var i in parent.childNodes) {
            if (typeof (parent.childNodes[i]) != "undefined"
                && typeof (parent.childNodes[i].id) != "undefined"
                && parent.childNodes[i].id.indexOf(partialId) != -1) {
                return parent.childNodes[i];
            }
        }
        for (var i in parent.childNodes) {
            var element = Nexida.$getByPartialId(parent.childNodes[i], partialId);
            if (element != null)
                return element;
        }
    }
    return null;
};
/// <summary>
/// Nexida.$getById(id)
/// Gets an element from the document by id.
/// </summary>
/// <param name="id">The element id.</param>
/// <param name="isFlashObject">if present and 'true' follows a path for retriving a flash object.</param>
/// <returns>The DOM element if found and null if not. </returns>
Nexida.$getById = function(id, isFlashObject) {
    if (typeof (isFlashObject) != "undefined" && isFlashObject != null && isFlashObject) {
        if (window.document[id]) {
            return window.document[id];
        }
        if (Nexida.$browser.IsIE) {
            if (document.embeds && document.embeds[id])
                return document.embeds[id];
        }
    }
    return document.getElementById(id);
};

/// <summary>
/// Nexida.$getByTag(tag)
/// Gets an element from the document by tag.
/// </summary>
/// <param name="tag">The element tag name.</param>
/// <returns>The DOM element if found and null if not. </returns>
Nexida.$getByTag = function(tag) {
    return document.getElementsByTagName(tag);
};

/// <summary>
/// Nexida.$getByName(name)
/// Gets an element from the document by name.
/// </summary>
/// <param name="name">The element name.</param>
/// <returns>The DOM element if found and null if not. </returns>
Nexida.$getByName = function(name) {
    return document.getElementsByName(name);
};

/// <summary>
/// Nexida.$validators
/// An object that access the page validators
/// </summary>
Nexida.$validators = {


    /// <summary>
    /// Nexida.$validators.exists()
    /// Varifies if the page validators exist on this document.
    /// </summary>
    /// <returns>true if exists, false if not. </returns>
    exists: function() {
        return (typeof (Page_Validators) != "undefined");
    },

    /// <summary>
    /// Nexida.$validators.getById(id)
    /// Gets an validator by id.
    /// </summary>
    /// <param name="id">The validator's id.</param>
    /// <returns>The validator as a DOM object.</returns>
    getById: function(id) {
        if (this.exists) {
            for (var i in Page_Validators) {
                if (Page_Validators[i].id == id)
                    return Page_Validators[i];
            }
        }
        return null;
    }
};

/// <summary>
/// Nexida.$validatorOnLoad()
/// A custom function for loading validator. This function 
/// is used to replace the standard "ValidatorOnLoad"
/// </summary>
Nexida.$validatorOnLoad = function() {
    if (typeof (Page_Validators) == "undefined")
        return;
    var i, val;
    for (i = 0; i < Page_Validators.length; i++) {
        val = Page_Validators[i];
        if (typeof (val.evaluationfunction) == "string") {
            eval("val.evaluationfunction = " + val.evaluationfunction + ";");
        }
        if (typeof (val.isvalid) == "string") {
            if (val.isvalid == "False") {
                val.isvalid = false;
                Page_IsValid = false;
            }
            else {
                val.isvalid = true;
            }
        } else {
            val.isvalid = true;
        }
        if (typeof (val.enabled) == "string") {
            val.enabled = (val.enabled != "False");
        }

        var registry = Nexida.TP.JavaScript.AutoComplete.Registry;
        var autocompleteId = registry.GetFieldOwener(val.controltovalidate);

        if (autocompleteId == null) {
            if (typeof (val.controltovalidate) == "string") {
                ValidatorHookupControlID(val.controltovalidate, val);
            }
            if (typeof (val.controlhookup) == "string") {
                ValidatorHookupControlID(val.controlhookup, val);
            }
        }
    }
    Page_ValidationActive = true;
};

/// <summary>
/// Nexida.$customValidatorEvaluateIsValid(validator)
/// A custom function for validating the custom validators.
/// This function is used to replace the standard 
/// "CustomValidatorEvaluateIsValid"
/// </summary>
/// <param name="val">The validator.</param>
///
/// DO NOT USE
///
Nexida.$customValidatorEvaluateIsValid = function(val) {
    var value = "";
    if (typeof (val.controltovalidate) == "string") {
        value = ValidatorGetValue(val.controltovalidate);
        var registry = Nexida.TP.JavaScript.AutoComplete.Registry;
        var autocompleteId = registry.GetFieldOwener(val.controltovalidate);
        if ((ValidatorTrim(value).length == 0) && ((typeof (val.validateemptytext) != "string") || (val.validateemptytext != "true"))) {
            if (autocompleteId != null) {
                var fileds = registry.GetFields(autocompleteId);
                return val.id != fileds.Validator.id;
            }
        }
    }
    var args = { Value: value, IsValid: true };
    if (typeof (val.clientvalidationfunction) == "string") {
        eval(val.clientvalidationfunction + "(val, args) ;");
    }
    return args.IsValid;
};

Nexida.$webServices = new Object();

Nexida.StartUp = function() {
    if (typeof (WebSite) != "undefined" && typeof (WebSite.Nexida) != "undefined" && typeof (WebSite.Nexida.Services) != "undefined")
        this.$webServices = WebSite.Nexida.Services;

    if (typeof (ValidatorOnLoad) == "function")
        ValidatorOnLoad = this.$validatorOnLoad;

    //if (typeof (CustomValidatorEvaluateIsValid) == "function")
    //      CustomValidatorEvaluateIsValid = this.$customValidatorEvaluateIsValid;

    if (Nexida.$browser.IsMozilla) {
        HTMLElement.prototype.__defineSetter__("innerText", function(sText) {
            this.innerHTML = sText.replace(/\&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
        });
        HTMLElement.prototype.__defineGetter__("innerText", function() {
            var r = this.ownerDocument.createRange();
            r.selectNodeContents(this);
            return r.toString();
        });
        var _emptyTags = {
            "IMG": true,
            "BR": true,
            "INPUT": true,
            "META": true,
            "LINK": true,
            "PARAM": true,
            "HR": true
        };

        HTMLElement.prototype.__defineGetter__("outerHTML", function() {
            var attrs = this.attributes;
            var str = "<" + this.tagName;
            for (var i = 0; i < attrs.length; i++)
                str += " " + attrs[i].name + "=\"" + attrs[i].value + "\"";

            if (_emptyTags[this.tagName])
                return str + ">";

            return str + ">" + this.innerHTML + "</" + this.tagName + ">";
        });
        HTMLElement.prototype.__defineSetter__("outerHTML", function(sHTML) {
            var r = this.ownerDocument.createRange();
            r.setStartBefore(this);
            var df = r.createContextualFragment(sHTML);
            this.parentNode.replaceChild(df, this);
        });
        HTMLElement.prototype.__defineSetter__("outerText", function(sText) {
            this.outerHTML = sText.replace(/\&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
        });
        HTMLElement.prototype.insertAdjacentHTML = function(sWhere, sHTML) {
            var df;   // : DocumentFragment
            var r = this.ownerDocument.createRange();

            switch (String(sWhere).toLowerCase()) {  // convert to string and unify case
                case "beforebegin":
                    r.setStartBefore(this);
                    df = r.createContextualFragment(sHTML);
                    this.parentNode.insertBefore(df, this);
                    break;

                case "afterbegin":
                    r.selectNodeContents(this);
                    r.collapse(true);
                    df = r.createContextualFragment(sHTML);
                    this.insertBefore(df, this.firstChild);
                    break;

                case "beforeend":
                    r.selectNodeContents(this);
                    r.collapse(false);
                    df = r.createContextualFragment(sHTML);
                    this.appendChild(df);
                    break;

                case "afterend":
                    r.setStartAfter(this);
                    df = r.createContextualFragment(sHTML);
                    this.parentNode.insertBefore(df, this.nextSibling);
                    break;
            }
        };
        HTMLElement.prototype.insertAdjacentText = function(sWhere, sText) {
            sText = sText.replace(/\&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
            this.insertAdjacentHTML(sWhere, sText);
        };
    }
};
/// <summary>
/// The Nexida.TP namespace.
/// </summary>
Nexida.TP = new Object();


/// <summary>
/// The Nexida.TP.JavaScript namespace.
/// </summary>
Nexida.TP.JavaScript = new Object();

/// <summary>
/// The Nexida.TP.JavaScript.RegitryClass class.
/// </summary>
Nexida.TP.JavaScript.RegistryClass = function() {
    var bag = new Array();

    /// <summary>
    /// The count of elements registerd.
    /// </summary>
    this.Count = 0;

    /// <summary>
    /// Contains(key)
    /// Tests if the key is in the bag.
    /// </summary>
    /// <param name="key">The key to find.</param>
    /// <returns>true if the key exists in the bag, false if not.</returns>
    this.Contains = function(key) {
        for (var item in bag) {
            if (bag[item] != null && bag[item].Key == key) {
                return true;
            }
        }
        return false;
    };

    /// <summary>
    /// GetByKey(key)
    /// Gets an element by key.
    /// </summary>
    /// <param name="key">The key pair of the objet to return.</param>
    /// <returns>The elemet as an object. If the key is not in the bag returns null.</returns>
    this.GetByKey = function(key) {
        for (var item in bag) {
            if (bag[item].Key == key) {
                return bag[item].Obj;
            }
        }
        return null;
    };

    this.Clear = function() {
        for (var item in bag) {
            bag[item] = null;
        }
        this.Count = 0;
    };
    this.Modify = function(key, obj) {
        for (var item in bag) {
            if (bag[item].Key == key) {
                bag[item].Obj = obj;
                return;
            }
        }
    };

    /// <summary>
    /// GetByIndex(index)
    /// Gets an element by index.
    /// </summary>
    /// <param name="index">The pair's index from the bag.</param>
    /// <returns>The pair {Key, Obj} from the bag.</returns>
    this.GetByIndex = function(index) {
        if (index >= 0 && index < bag.length)
            return bag[index];
        return null;
    };

    /// <summary>
    /// OnRegister(sender, arg)
    /// The event that triggers after an element is registered.
    /// </summary>
    /// <param name="sender">Is this object</param>
    /// <param name="arg">The pair {Key, Obj} from the bag.</param>
    this.OnRegister = null;

    /// <summary>
    /// Register(key, obj)
    /// Add a pair {Key, Obj} in the registry bag.
    /// </summary>
    /// <param name="key">The object's key</param>
    /// <param name="obj">The object to register.</param>
    this.Register = function(key, obj) {
        if (this.Contains(key)) {
            throw 'The key "' + key + '" already exists!';
        }
        bag[this.Count++] = {
            Key: '',
            Obj: null
        };
        bag[this.Count - 1].Key = key;
        bag[this.Count - 1].Obj = obj;
        if (this.OnRegister != null && typeof (this.OnRegister) == "function")
            this.OnRegister(this, bag[this.Count - 1]);
    };
};
/// <summary>
/// The Nexida.TP.JavaScript.AutoComplete namespace.
/// </summary>
Nexida.TP.JavaScript.AutoComplete = new Object();

/// <summary>
/// The Nexida.TP.JavaScript.AutoComplete.Registry 
/// instance. It isn't an instance of the 
/// Nexida.TP.JavaScript.RegitryClass class.
/// </summary>
Nexida.TP.JavaScript.AutoComplete.Registry = new function() {
    var $getById = Nexida.$getById;
    var $validators = Nexida.$validators;

    var bag = new Array();

    /// <summary>
    /// The count of the lements in the bag.
    /// </summary>
    this.Count = 0;

    /// <summary>
    /// Nexida.TP.JavaScript.AutoComplete.Registry.Contains(autoCompleteExtenderClientId)
    /// Tests if the autoCompleteExtenderClientId is in the bag.
    /// </summary>
    /// <param name="autoCompleteExtenderClientId">The autoCompleteExtenderClientId to find.</param>
    /// <returns>true if the autoCompleteExtenderClientId exists in the bag, false if not.</returns>
    this.Contains = function(autoCompleteExtenderClientId) {
        for (var item in bag) {
            if (bag[item].autoCompleteExtenderClientId == autoCompleteExtenderClientId) {
                return true;
            }
        }
        return false;
    };

    /// <summary>
    /// Nexida.TP.JavaScript.AutoComplete.Registry.GetFieldOwener(fieldClientId)
    /// Gets the autoCompleteExtenderClientId from the bag for the fieldClientId 
    /// (the field if the key field or the text field)
    /// </summary>
    /// <param name="fieldClientId">The fieldClientId regitered in the bag.</param>
    /// <returns>If the fieldClientId is registered in the registery returns 
    /// the autoCompleteExtenderClientId, else null</returns>
    this.GetFieldOwener = function(fieldClientId) {
        for (var item in bag) {
            if (bag[item].KeyFieldClientId == fieldClientId
                || bag[item].TextFieldClientId == fieldClientId) {
                return bag[item].autoCompleteExtenderClientId;
            }
        }
        return null;
    };


    /// <summary>
    /// Nexida.TP.JavaScript.AutoComplete.Registry.Register(autoCompleteExtenderClientId,
    ///     keyFieldClientId, textFieldClientId, valueForTextServiceContract,
    ///     valueForTextServiceMethod, requiredValidatorId)
    /// Adds in the registry information for the key-value autocomplete state of the textField
    /// </summary>
    /// <param name="autoCompleteExtenderClientId">The autoCompleteExtenderClientId.</param>
    /// <param name="keyFieldClientId">The keyFieldClientId is the id of an hidden field where is stored the key of the value.</param>
    /// <param name="textFieldClientId">The textFieldClientId is the id of the textBox that is on the screen.</param>
    /// <param name="valueForTextServiceContract">The valueForTextServiceContract is the contract of the service from where the keyField is filled when the text is not selected from the auto complete.</param>
    /// <param name="valueForTextServiceMethod">The valueForTextServiceMethod is the service method that provides the value for the keyField from the text writen in the textBox.</param>
    /// <param name="requiredValidatorId">The requiredValidatorId.</param>
    this.Register = function(autoCompleteExtenderClientId, keyFieldClientId, textFieldClientId,
                        valueForTextServiceContract, valueForTextServiceMethod, requiredValidatorId) {
        if (this.Contains(autoCompleteExtenderClientId)) {
            throw 'The AutoCompleteEexcender id "' + autoCompleteExtenderClientId + '" already exists!';
        }
        if (this.GetFieldOwener(keyFieldClientId) != null) {
            throw 'The "' + keyFieldClientId + '" is already owned by "' + this.GetFieldOwener(keyFieldClientId) + '" AutoCompleteExtender!';
        }
        if (this.GetFieldOwener(textFieldClientId) != null) {
            throw 'The "' + textFieldClientId + '" is already owned by "' + this.GetFieldOwener(textFieldClientId) + '" AutoCompleteExtender!';
        }
        bag[this.Count++] = {
            autoCompleteExtenderClientId: '',
            KeyFieldClientId: '',
            TextFieldClientId: '',
            ValueForTextServiceContract: '',
            ValueForTextServiceMethod: '',
            RequiredValidatorId: null
        };
        bag[this.Count - 1].autoCompleteExtenderClientId = autoCompleteExtenderClientId;
        bag[this.Count - 1].KeyFieldClientId = keyFieldClientId;
        bag[this.Count - 1].TextFieldClientId = textFieldClientId;
        bag[this.Count - 1].ValueForTextServiceContract = valueForTextServiceContract;
        bag[this.Count - 1].ValueForTextServiceMethod = valueForTextServiceMethod;
        bag[this.Count - 1].RequiredValidatorId = requiredValidatorId;
    };

    var GetFomBagByOwner = function(autoCompleteExtenderClientId) {
        for (var item in bag) {
            if (bag[item].autoCompleteExtenderClientId == autoCompleteExtenderClientId) {
                return bag[item];
            }
        }
    };

    //Nexida.TP.JavaScript.AutoComplete.Registry.StartGetKeyForText("", function)
    this.StartGetKeyForText = function(fieldOwnerClientId, successCallback) {
        if (fieldOwnerClientId == null) return;
        var bagItem = GetFomBagByOwner(fieldOwnerClientId);
        if (typeof (Nexida.$webServices[bagItem.ValueForTextServiceContract]) == "undefined")
            return;
        var textToValueService = new Nexida.$webServices[bagItem.ValueForTextServiceContract]();
        if (typeof (textToValueService[bagItem.ValueForTextServiceMethod]) == "undefined")
            return;

        var fields = this.GetFields(fieldOwnerClientId);
        var userContext = { Fields: fields };
        textToValueService[bagItem.ValueForTextServiceMethod](fields.TextField.value, successCallback, null, userContext);
    };

    //Nexida.TP.JavaScript.AutoComplete.Registry.GetFields("")
    this.GetFields = function(autoCompleteExtenderClientId) {
        for (var item in bag) {
            if (bag[item].autoCompleteExtenderClientId == autoCompleteExtenderClientId) {
                return {
                    KeyField: $getById(bag[item].KeyFieldClientId),
                    TextField: $getById(bag[item].TextFieldClientId),
                    Validator: $validators.exists() && (bag[item].RequiredValidatorId != null) ? $validators.getById(bag[item].RequiredValidatorId) : null
                };
            }
        }
    };
};

//Nexida.TP.JavaScript.AutoComplete.DoClientItemSelection(obj, obj)
Nexida.TP.JavaScript.AutoComplete.DoClientItemSelection = function(source, eventArgs) {
    var registry = Nexida.TP.JavaScript.AutoComplete.Registry;
    if (!registry.Contains(source._id))
        throw 'The AutoCompleteExtender id "' + source._id + '" is not registered!';
    var fields = registry.GetFields(source._id);
    var keyField = fields.KeyField;
    var textField = fields.TextField;
    keyField.value = eventArgs.get_value();
    textField.value = eventArgs.get_text();
};

//Nexida.TP.JavaScript.AutoComplete.RequiredFieldValidationHandler(objSender, objArgs)
Nexida.TP.JavaScript.AutoComplete.RequiredFieldValidationHandler = function(source, eventArgs) {
    var registry = Nexida.TP.JavaScript.AutoComplete.Registry;
    var fieldOwnerClientId = registry.GetFieldOwener(source.controltovalidate);
    if (fieldOwnerClientId != null) {
        var fields = registry.GetFields(fieldOwnerClientId);
        eventArgs.IsValid = typeof (fields.KeyField.value) != "undefined"
            && fields.KeyField.value != "undefined"
            && fields.KeyField.value != "";
    }
};

//Nexida.TP.JavaScript.AutoComplete.TextFieldOnKeyDown(ctrl, eventarg)
Nexida.TP.JavaScript.AutoComplete.TextFieldOnKeyDown = function(sender, e) {
    var registry = Nexida.TP.JavaScript.AutoComplete.Registry;
    var fieldOwnerClientId = registry.GetFieldOwener(sender.id);
    if (fieldOwnerClientId != null) {
        var fields = registry.GetFields(fieldOwnerClientId);
        fields.KeyField.value = "";
    }
};

//Nexida.TP.JavaScript.AutoComplete.TextFieldOnChange(ctrl, eventarg)
Nexida.TP.JavaScript.AutoComplete.TextFieldOnChange = function(sender, e) {
    var registry = Nexida.TP.JavaScript.AutoComplete.Registry;
    var fieldOwnerClientId = registry.GetFieldOwener(sender.id);
    registry.StartGetKeyForText(fieldOwnerClientId, Nexida.TP.JavaScript.AutoComplete.TextFieldOnChangeSuccessed);
};
//Nexida.TP.JavaScript.AutoComplete.TextFieldOnChangeSuccessed(
//      result /*{"1", "2"}*/
//      , userContext /*{ Fields: { KeyField: object, TextField: object } }*/);
Nexida.TP.JavaScript.AutoComplete.TextFieldOnChangeSuccessed = function(result, userContext) {
    if (typeof (userContext) == "undefined"
        || typeof (userContext.Fields) == "undefined")
        return;
    if (result.length == 1) {
        userContext.Fields.KeyField.value = result[0];
        return;
    }
    userContext.Fields.KeyField.value = "";
};
Nexida.TP.JavaScript.NotifyPanel = new Object();

Nexida.TP.JavaScript.NotifyPanel.Registry = new Nexida.TP.JavaScript.RegistryClass();

Nexida.TP.JavaScript.NotifyPanel.ExpandPanel = function(expander) {
    for (var i = 0; i < this.Registry.Count; i++) {
        if (this.Registry.GetByIndex(i).Obj.Expander == expander) {
            var panelToExpand = Nexida.$getById(this.Registry.GetByIndex(i).Key);
            panelToExpand.style.display = "inline";
            var obj = this.Registry.GetByIndex(i).Obj;
            var expanderControl = Nexida.$getById(obj.Expander);
            expanderControl.style.display = "none";
            var unexpanderControl = Nexida.$getById(obj.Unexpander);
            unexpanderControl.style.display = "inline";
            if (typeof (obj.HideThisToo) != "undefined") {
                var hideThisToo = Nexida.$getById(obj.HideThisToo);
                hideThisToo.style.display = "none";
            }
            return;
        }
    }
};
Nexida.TP.JavaScript.NotifyPanel.UnexpandPanel = function(unexpander) {
    for (var i = 0; i < this.Registry.Count; i++) {
        if (this.Registry.GetByIndex(i).Obj.Unexpander == unexpander) {
            var panelToExpand = Nexida.$getById(this.Registry.GetByIndex(i).Key);
            panelToExpand.style.display = "none";
            var obj = this.Registry.GetByIndex(i).Obj;
            var expanderControl = Nexida.$getById(obj.Expander);
            expanderControl.style.display = "inline";
            var unexpanderControl = Nexida.$getById(obj.Unexpander);
            unexpanderControl.style.display = "none";
            if (typeof (obj.HideThisToo) != "undefined") {
                var hideThisToo = Nexida.$getById(obj.HideThisToo);
                hideThisToo.style.display = "inline";
            }
            return;
        }
    }
};
Nexida.TP.JavaScript.ValidateDate = function(source, argument) {
    argument.IsValid = true;
    if (Sys) {
        argument.IsValid = false;
        var shortDatePattern = Sys.CultureInfo.CurrentCulture.dateTimeFormat.ShortDatePattern;
        var d = Date.parseLocale(argument.Value, shortDatePattern);
        argument.IsValid = d != null;
    }
};
Nexida.TP.JavaScript.ShowModalPopup = function(sender, popupBehavoirId) {
    if (Sys) {
        var popupBehavoir = $find(popupBehavoirId);
        popupBehavoir.show();
    }
};
Nexida.TP.JavaScript.HideModalPopup = function(sender, popupBehavoirId) {
    if (Sys) {
        var popupBehavoir = $find(popupBehavoirId);
        popupBehavoir.hide();
    }
};
Nexida.TP.JavaScript.Exception = function(message, innerException) {
    _message = message;
    _innerException = innerException;

    this.get_message = function() {
        return _message;
    };
    this.get_innerException = function() {
        return _innerException;
    };
};
Nexida.TP.JavaScript.CompanyTypeValidatorHandler = function(source, args) {
    args.IsValid = !(args.Value == "5" || args.Value == "11" || args.Value == "17");
};
Nexida.TP.JavaScript.DateTimeComparerRegistry = new Nexida.TP.JavaScript.RegistryClass();
/*
* key: string //validatorId
* obj: {
*   comparer1Id: string
*   comparer2Id: string
*   comparationType: "1>2", "1<2", "1>=2", "1<=2", "1<>2"
*   validationGroup: string
*   validatedByOther: bool
* }
**/
Nexida.TP.JavaScript.ValidateCompareDate = function(sender, args) {
    var reg = Nexida.TP.JavaScript.DateTimeComparerRegistry;
    if (reg.Contains(sender.id)) {
        var obj = reg.GetByKey(sender.id);
        var comparer1 = Nexida.$getById(obj.comparer1Id);
        var comparer2 = Nexida.$getById(obj.comparer2Id);
        var comparer1Date = Date.parseLocale(comparer1.value, Sys.CultureInfo.CurrentCulture.dateTimeFormat.ShortDatePattern);
        var comparer2Date = Date.parseLocale(comparer2.value, Sys.CultureInfo.CurrentCulture.dateTimeFormat.ShortDatePattern);
        if (!isNaN(comparer1Date) && !isNaN(comparer2Date)) {
            switch (obj.comparationType) {
                case "1>2":
                    args.IsValid = comparer1Date > comparer2Date;
                    break;
                case "1<2":
                    args.IsValid = comparer1Date < comparer2Date;
                    break;
                case "1>=2":
                    args.IsValid = comparer1Date >= comparer2Date;
                    break;
                case "1<=2":
                    args.IsValid = comparer1Date <= comparer2Date;
                    break;
                case "1<>2":
                default:
                    args.IsValid = comparer1Date != comparer2Date;
                    break;
            }
        }
        else
            args.IsValid = false;
        if (typeof (ValidatorValidate) != "undefined" && !obj.validatedByOther) {
            for (var i = 0; i < reg.Count; i++) {
                var r = reg.GetByIndex(i);
                var val = Nexida.$getById(r.Key);
                if (r.Key != sender.id && val != null) {
                    r.Obj.validatedByOther = true;
                    ValidatorValidate(val, obj.validationGroup);
                    r.Obj.validatedByOther = false;
                }
            }
        }
    }
};
