/*
*本插件原作者Vanadium,原文请移步前往http://vanadiumjs.com/查看
*本插件由Mr.Think中文整理,Mr.Think的博客:http://MrThink.net/
*转载及使用请务必注明原作者.
*/
//弹出信息样式设置
Vanadium.config = {
    valid_class: 'inputSus',//验证正确时表单样式
    invalid_class: 'erroT',//验证失败时该表单样式
    message_value_class: 'msgvaluecss',//这个样式是弹出信息中调用值的样式
    advice_class: 'erroT',//验证失败时文字信息的样式
    prefix: ':',
    separator: ';',
    reset_defer_timeout: 100
}
//验证类型及弹出信息设置
Vanadium.Type = function(className, validationFunction, error_message, message, init) {
    this.initialize(className, validationFunction, error_message, message, init);
};
Vanadium.Type.prototype = {
    initialize: function(className, validationFunction, error_message, message, init) {
        this.className = className;
        this.message = message;
        this.error_message = error_message;
        this.validationFunction = validationFunction;
        this.init = init;
    },
    test: function(value) {
        return this.validationFunction.call(this, value);
    },
    validMessage: function() {
        return this.message;
    },
    invalidMessage: function() {
        return this.error_message;
    },
    toString: function() {
        return "className:" + this.className + " message:" + this.message + " error_message:" + this.error_message
    },
    init: function(parameter) {
        if (this.init) {
            this.init(parameter);
        }
    }
};
Vanadium.setupValidatorTypes = function() {
    Vanadium.addValidatorType('empty', function(v) {
        return  ((v == null) || (v.length == 0));
    });
    //***************************************以下为验证方法,使用时可仅保留用到的判断
    Vanadium.addValidatorTypes([

        //是否为空
        ['required', function(v) {
            return !Vanadium.validators_types['empty'].test(v);
        }, '此项不可为空!'],
        //强制选中 
        ['accept', function(v, _p, e) {
            return e.element.checked;
        }, '必须接受!'],

        //邮箱验证
        ['email', function (v) {
            return (Vanadium.validators_types['empty'].test(v) || /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(v))
        }, '格式错误,例如xxx@xxx.xxx'],
        //输入固定长度字符串
        ['length',
        function (v, p) {
            if (p === undefined) {
                return true
            } else {
                return v.length == parseInt(p)
            }
        ;
        },
        function (_v, p) {
            return '输入字符长度等于<span class="' + Vanadium.config.message_value_class + '">' + p + '</span>个.'
        }
        ],
        //
        ['min_length',
        function (v, p) {
            if (p === undefined) {
                return true
            } else {
                return v.length >= parseInt(p)
            }
        ;
        },
        function (_v, p) {
            return '输入字符长度不低于<span class="' + Vanadium.config.message_value_class + '">' + p + '</span>个.'
        }
        ],
        ['max_length',
        function (v, p) {
            if (p === undefined) {
                return true
            } else {
                return v.length <= parseInt(p)
            }
        ;
        },
        function (_v, p) {
            return '输入字符长度不大于<span class="' + Vanadium.config.message_value_class + '">' + p + '</span>个.'
        }
        ],
        //判断密码是相同
        ['same_as',
        function (v, p) {
            if (p === undefined) {
                return true
            } else {
                var exemplar = document.getElementById(p);
                if (exemplar)
                    return v == exemplar.value;
                else
                    return false;
            }
        ;
        },
        function (_v, p) {
            var exemplar = document.getElementById(p);
            if (exemplar)
                return '两次密码输入不相同.';
            else
                return '没有可参考值!'
        },
        "",
        function(validation_instance) {
            var exemplar = document.getElementById(validation_instance.param);
            if (exemplar){
                jQuery(exemplar).bind('validate', function(){
                    jQuery(validation_instance.element).trigger('validate');
                });
            }
        }
        ],
        //ajax判断是否存在值
        ['ajax',
        function (v, p, validation_instance, decoration_context, decoration_callback) {
            if (Vanadium.validators_types['empty'].test(v)) return true;
            if (decoration_context && decoration_callback) {
                jQuery.getJSON(p, {
                    value: v, 
                    id: validation_instance.element.id
                }, function(data) {
                    decoration_callback.apply(decoration_context, [[data], true]);
                });
            }
            return true;
        }]
        ])
    if (typeof(VanadiumCustomValidationTypes) !== "undefined" && VanadiumCustomValidationTypes) Vanadium.addValidatorTypes(VanadiumCustomValidationTypes);
};
        