﻿/// <summary>
/// FileName : CustomValidator.js
/// Author : 
/// Description : 自定义ASP.NET Ajax客户端组件，用于自定义数据格式的有效性验证
/// ClassName : NBBS.CustomControl.CustomValidator
/// Namespace : NBBS.CustomControl
/// Inheritee : Sys.UI.Control
/// </summary>

Type.registerNamespace('NBBS.CustomControl');

NBBS.CustomControl.CustomValidator = function(associatedElement) {
    NBBS.CustomControl.CustomValidator.initializeBase(this, [associatedElement]);
}

NBBS.CustomControl.CustomValidator.prototype = {
    /// <summary>
    /// 私有成员变量
    /// </summary>
    /// <param name="_data">待验证数据</param>
    /// <param name="_isPass">是否通过验证</param>
    /// <param name="_allowEmpty">数据是否允许空值</param>
    /// <param name="_tip">提示信息</param>
    /// <param name="_errorMessage">验证无法通过时的信息</param>
    /// <param name="_textBox">绑定到的文本框</param>
    /// <param name="_regularExpression">自定义正则表达式</param>
    _data: null,
    _isPass :false,
    _allowEmpty :false,
    _tip :null,
    _errorMessage :null,
    _textBox :null,
    _regularExpression :null,
    
    /// <summary>
    /// 属性
    /// </summary>
    get_data: function() {
        return this._data;
    },

    set_data: function(value) {
        if (this._data !== value) {
            this._data = value;
            this.raisePropertyChanged('data');
        }
    },
    
    get_isPass: function() {
    return this._isPass;
    },

    set_isPass: function(value) {
        if (this._isPass !== value) {
            this._isPass = value;
            this.raisePropertyChanged('isPass');
        }
    },
    
    get_allowEmpty: function() {
    return this._allowEmpty;
    },

    set_allowEmpty: function(value) {
        if (this._allowEmpty !== value) {
            this._allowEmpty = value;
            this.raisePropertyChanged('allowEmpty');
        }
    },
    
    get_tip: function() {
    return this._tip;
    },

    set_tip: function(value) {
        if (this._tip !== value) {
            this._tip = value;
            this.raisePropertyChanged('tip');
        }
    },
    
    get_errorMessage: function() {
    return this._errorMessage;
    },

    set_errorMessage: function(value) {
        if (this._errorMessage !== value) {
            this._errorMessage = value;
            this.raisePropertyChanged('errorMessage');
        }
    },
    
    get_textBox: function() {
    return this._textBox;
    },

    set_textBox: function(value) {
        if (this._textBox !== value) {
            this._textBox = value;
            this.raisePropertyChanged('textBox');
        }
    },
    
    get_regularExpression: function() {
    return this._regularExpression;
    },

    set_regularExpression: function(value) {
        if (this._regularExpression !== value) {
            this._regularExpression = value;
            this.raisePropertyChanged('regularExpression');
        }
    },
    
    /// <summary>
    /// 构造函数
    /// </summary>
    initialize: function() {
        NBBS.CustomControl.CustomValidator.callBaseMethod(this, 'initialize');
    
        var stringBuilder = new Sys.StringBuilder;
        stringBuilder.append("<font color=\"#CCCCCC\">");
        stringBuilder.append(this.get_tip());
        stringBuilder.append("</font>"); 
        this.get_element().innerHTML=stringBuilder.toString();
        
        if(this.get_allowEmpty() === true && this.get_textBox().get_text() == "")
        {
            this.set_isPass(true);
        }
    },

    /// <summary>
    /// 析构函数
    /// </summary>
    dispose: function() {
        if(this.get_element())
        {
            this.get_element().innerHTML="";
        }
    
        NBBS.CustomControl.CustomValidator.callBaseMethod(this, 'dispose');
    },

    /// <summary>
    /// 对数据进行验证
    /// </summary>
    validate: function() {
        if(this.get_allowEmpty() === true && this.get_textBox().get_text() == "")
        {
            this.initialize();
        }
        else
        {
            this.get_element().innerHTML="<img src=\"Icons/loading_validate.gif\" alt=\"\" />&nbsp;<font color=\"CCCCCC\">正在验证……</font>";
            Validate.ValidateCustom(this.get_data(),this.get_regularExpression(),this.onSucceeded,this.onFailed,this);
        }
    },

    /// <summary>
    /// 验证正常进行时的回调函数
    /// </summary>
    onSucceeded: function(result,validator) {
        if(result==0)
        {
            validator.get_element().innerHTML="<img src=\"Icons/icon_ok.gif\" alt=\"\" />";
            validator.set_isPass(true);
        }
        else if(result==1)
        {
            var stringBuilder = new Sys.StringBuilder;
            stringBuilder.append("<font color=\"#FF0000\">");
            stringBuilder.append(validator.get_errorMessage());
            stringBuilder.append("</font>"); 
            validator.get_element().innerHTML=stringBuilder.toString();
            
            validator.set_isPass(false);
        }
    },
    
    /// <summary>
    /// 验证出现异常，无法进行时的回调
    /// </summary>
    onFailed: function(error,validator) {
        validator.get_element().innerHTML="<font color=\"#FF0000\">内部错误，无法完成检验，请与管理员联系</font>";
    }
}

NBBS.CustomControl.CustomValidator.registerClass('NBBS.CustomControl.CustomValidator', Sys.UI.Control);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();
