﻿Type.registerNamespace("Framework.Controls");

Framework.Controls.WebFormValidator = function(element) {
    Framework.Controls.WebFormValidator.initializeBase(this, [element]);
    this._validators = new Array();
    this._validationData = null;
    this.valid = true;
    this._SummaryControlID = "";
    this._summaryControl = null;
    this._summaryControlErrors = null;
    this._SummaryControlErrorsID = "";
    this._summaryHide = 0;
};
Framework.Controls.WebFormValidator.prototype =
{
    get_vc: function() { return this._validationData; },
    set_vc: function(value) { this._validationData = eval(value); },
    get_SummaryControlClientID: function() { return this._SummaryControlID; },
    set_SummaryControlClientID: function(value) { this._SummaryControlID = value; },
    get_SummaryControlErrorsClientID: function() { return this._SummaryControlErrorsID; },
    set_SummaryControlErrorsClientID: function(value) { this._SummaryControlErrorsID = value; },
    get_SummaryHide: function() { return this._summaryHide; },
    set_SummaryHide: function(value) { this._summaryHide = value; },

    initialize: function() {
        Framework.Controls.WebFormValidator.callBaseMethod(this, "initialize");

        // Get summary control
        if (this._SummaryControlID) {
            this._summaryControl = $get(this._SummaryControlID);
            this._summaryControlErrors = $get(this._SummaryControlErrorsID);
        }

        // Init all validation items and create behaviours
        for (var i = 0; i < this._validationData.length; i++) {
            var _validator = new Framework.Controls.WebFormValidatorData(this._validationData[i]);
            _validator.initialize();
            _validator.host = this;
            this._validators.push(_validator);
            if (_validator._data.fi) _validator.validate();
        }
    },
    dispose: function() {
        for (var i = 0; i < this._validators.length; i++) {
            this._validators[i].dispose();
        }
        Framework.Controls.WebFormValidator.callBaseMethod(this, "dispose");
    },
    isValid: function() {
        this.showSummary();
        this.valid = true;
        for (var i = 0; i < this._validators.length; i++) {
            if (!this._validators[i].validate()) {
                this.valid = false;
            }
        }
        return this.valid;
    },
    showSummary: function() {
        // Clear validation summary
        var errControls = new Array();
        if (this._summaryControlErrors) {
            this._summaryControlErrors.innerHTML = "";
        }

        this.valid = true;
        for (var i = 0; i < this._validators.length; i++) {
            if (!this._validators[i].isValid()) {
                // Add error message to validation summary
                if (this._summaryControlErrors) {
                    // Don't show duplicated error messages
                    var exists = false;
                    for (var j = 0; j < errControls.length; j++) {
                        if (errControls[j] == this._validators[i]._data.control.id) {
                            exists = true;
                            break;
                        }
                    }
                    // Show error message
                    if (!exists) {
                        var err = document.createElement("SPAN");
                        err.innerHTML = this._validators[i].getErrorMessage();
                        this._summaryControlErrors.appendChild(err);
                        errControls.push(this._validators[i]._data.control.id);
                    }
                }
                this.valid = false;
            }
        }

        // Show validation summary
        if (this._summaryControl) {
            if (this.valid) {
                this._summaryControl.style.display = "none";
            } else {
                this._summaryControl.style.display = "";
                if (this._summaryHide > 0) {
                    setTimeout(Function.createDelegate(this, this.fadeSummary), this._summaryHide);
                }
            }
        }
    },
    fadeSummary: function() {
        $(this._summaryControl).slideUp("slow");
    }
}
Framework.Controls.WebFormValidator.registerClass("Framework.Controls.WebFormValidator", AjaxControlToolkit.BehaviorBase);

Framework.Controls.WebFormValidatorData = function(data) {
    this._data = data;
    this.valid = true;
    this._onBlurHandler = Function.createDelegate(this, this._lostFocus);
    this._onClickHandler = Function.createDelegate(this, this._clickValidate);
    this._popup = null;
    this.host = null;
};
Framework.Controls.WebFormValidatorData.prototype =
{
    initialize: function() {
        this._data.control = $get(this._data.id);
        this._data.valid = true;
        this._data.control.oldClassName = this._data.control.className;
        $addHandler(this._data.control, "blur", this._onBlurHandler);
        $addHandler(this._data.control, "mousedown", this._onClickHandler);
    },
    dispose: function() {
        $removeHandler(this._data.control, "blur", this._onBlurHandler);
        $removeHandler(this._data.control, "mousedown", this._onClickHandler);
    },
    _clickValidate: function() {
        if (this._data.control && this._data.control.value != "") {
            this.validate();
        }
    },
    _isVisible: function() {
        var _parent = this._data.control;
        while (_parent) {
            if (_parent.style && _parent.style.display == 'none') {
                return false;
            }
            _parent = _parent.parentNode;
        }
        return true;
    },
    isValid: function() {
        // Check if control is visible
        if (!this._isVisible()) return true;

        if (this._data.fi) {
            // Remove force invalid on second validation attempt
            this._data.fi = false;
            return false;
        }
        switch (this._data.control.nodeName) {
            case "INPUT":
                this.valid = this._validateInput();
                break;
            case "SELECT":
                this.valid = this._validateSelect();
                break;
            case "TEXTAREA":
                this.valid = this._validateInput();
                break;
        }

        return this.valid;
    },
    _lostFocus: function() {
        this.validate();
    },
    validate: function() {
        this.valid = this.isValid();

        if (!this.valid && this._data.tip) {
            if (this._data.timeoutid) {
                clearTimeout(this._data.timeoutid);
            }
            if (!this._popup) {
                // Check if we have duplicated behaviors
                var behaviour = $find(this._data.control.id);
                if (behaviour) {
                    // User existing error popup
                    behaviour._message = this._data.msg;
                    this._popup = behaviour;
                } else {
                    // Create error popup
                    if (this._data.tcss != "") {
                        this._popup = $create(Framework.Controls.Tooltip, {
                            "_message": this._data.msg,
                            //"delay": 0,
                            "targetControlID": this._data.control.id,
                            "persist": true,
                            "align": this._data.align,
                            "width": "150px",
                            "xoffset": this._data.x,
                            "yoffset": this._data.y,
                            "Position": this._data.p,
                            "cssClass": this._data.tcss,
                            "width": this._data.w
                        }, null, null, this._data.control);
                    }
                    else {
                        this._popup = $create(Framework.Controls.Tooltip, {
                            "_message": this._data.msg,
                            //"delay": 0,
                            "targetControlID": this._data.control.id,
                            "persist": true,
                            "align": this._data.align,
                            "width": "150px",
                            "xoffset": this._data.x,
                            "yoffset": this._data.y,
                            "Position": this._data.p,
                            "width": this._data.w
                        }, null, null, this._data.control);
                    }
                }
            }
            else {
                this._popup.show();
            }
            if (this._data.hide > 0) {
                this._data.timeoutid = setTimeout(Function.createDelegate(this, this._hide), this._data.hide);
            }
            if (this._data.errcss != "") {
                this._data.control.className = this._data.errcss;
            }
        }
        else {
            if (this._popup) {
                this._popup.hide();
            }
            this._data.control.className = this._data.control.oldClassName;
        }
        return this.valid;
    },
    _validateInput: function() {
        if (this._data.req && ((this._data.defVal == "" && this._data.control.value == "") || (this._data.defVal != "" && this._data.defVal == this._data.control.value))) {
            return false;
        }
        if (this._data.control.value != "" && this._data.regex != "") {
            return this._regexValid(this._data.regex, this._data.control.value);
        }
        if (this._data.compareid != "") {
            return this._compare(this._data.compareid);
        }
        return true;
    },
    _validateSelect: function() {
        if (this._data.req && ((this._data.defVal == "" && this._data.control.options.length > 0 && this._data.control.options[this._data.control.selectedIndex].value == "") || (this._data.defVal != "" && this._data.defVal == this._data.control.options[this._data.control.selectedIndex].value))) {
            return false;
        }
        if (this._data.regex != "" && this._data.control.options.length > 0) {
            return this._regexValid(this._data.regex, this._data.control.options[this._data.control.selectedIndex].value);
        }
        if (this._data.compareid != "") {
            return this._compare(this._data.compareid);
        }
        return true;
    },
    _compare: function(compareid) {
        var compControl = $get(compareid);
        if (compControl) {
            var _val;
            switch (this._data.control.nodeName) {
                case "INPUT":
                    _val = this._data.control.value;
                    break;
                case "SELECT":
                    _val = this._data.control.options[this._data.control.selectedIndex].value
                    break;
                case "TEXTAREA":
                    _val = this._data.control.value;
                    break;
            }
            if (_val) {
                switch (compControl.nodeName) {
                    case "INPUT":
                        if (_val == compControl.value) return true;
                        break;
                    case "SELECT":
                        if (_val == compControl.options[compControl.selectedIndex].value) return true;
                        break;
                    case "TEXTAREA":
                        if (_val == compControl.value) return true;
                        break;
                }
            }
        }
        return false;
    },
    _regexValid: function(regex, value) {
        var re = new RegExp(regex);
        return value.match(re);
    },
    _hide: function() {
        if (this._data.timeoutid) {
            clearTimeout(this._data.timeoutid);
        }
        if (this._popup) {
            this._popup.set_FadeTime(250);
            this._popup.hide();
            this._data.control.className = this._data.control.oldClassName;
        }
    },
    getErrorMessage: function() {
        return this._data.msg;
    }
}
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();