﻿    var $j = jQuery.noConflict();
    var canSubmit = false;
    function loadCheckJs(){
        $j('input[vtype]').each(function(){
            var vtype = $j(this).attr('vtype');
            var compareId = $j(this).attr('compareId');
            var text = $j(this);
            $j(this).initDom(vtype);
            $j(this).focus(function(){
                text.edit(text);
            });
            $j(this).blur(function(){
                text.check(vtype,compareId,text);
            });
        });
    }
    
    $j.fn.extend({
        // 初始化dom元素
        initDom : function(vtype){
            //$j(this).after('<img src="images/focus.gif" style="display:none; margin-left: 8px;" />');
            $j(this).after('<font color="red" style="margin-left: 4px;"></font>');
        },
        
        // 验证
        check: function(vtype, compareId, text){
            // 比较2个控件的value值
            if(isEmpty(compareId)){
                var val = text.val();
                if(!isEmpty(val)){
                    text.next().html(text.attr('msg')).show();
                    return false;
                }
                var val2 = document.getElementById(compareId).value;
                if(val2 != val){
                    text.next().html(text.attr('msg')).show();
                    return false;
                }
                $j('#' + compareId).next().hide();
                text.next().html(text.attr('msg')).hide();
                return true;
            }
            
            // 非空验证
            if(vtype == 'notNull'){
                var val = text.val();
                if(isEmpty(val)){
                    text.next().html(text.attr('msg')).hide();
                    return true;
                }else{
                    //text.next().attr('src', 'images/no.gif').show();
                    text.next().html(text.attr('msg')).show();
                    return false;
                }
            }
            // email验证
            if(vtype == 'email'){
                var regEmail = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
                var email = text.val();
                if(isEmpty(email) && regEmail.test(email) && email.length <= 50){
                    text.next().hide();
                    return true;
                }else{
                    text.next().html(text.attr('msg')).show();
                    return false;
                }
            }
            // 数字验证            
            if(vtype == 'integer'){
                var regInteger = /^[0-9]*[1-9][0-9]*$/;
                var val = text.val();
                if(isEmpty(val) && regInteger.test(val) && val.length < 30){
                    text.next().html(text.attr('msg')).hide();
                    return true;
                }else{
                    text.next().html(text.attr('msg')).show();
                    return false;
                }
            }
            
            if(vtype == 'telPhone'){
                var reg = /(\d{3}-\d{3}-\d{4})|(\d{5,})/;
                var val = text.val();
                if(isEmpty(val) && reg.test(val) && val.length < 30){
                    text.next().html(text.attr('msg')).hide();
                    return true;
                }else{
                    text.next().html(text.attr('msg')).show();
                    return false;
                }
            }
        },
        
        // 编辑样式
        edit : function(text){
            //text.next().attr('src', 'images/focus.gif').show();
        }
    });
    
    // 验证非空，非空时返回true
    function isEmpty(val){
        if(val != undefined && val != '' && val != null){
            return true;
        }
        return false;
    }
    
    // 表单提交验证
    function checkForm(){
        var returnValue = true;
        $j('input[vtype]').each(function(){
            canSubmit = $j(this).check($j(this).attr('vtype'),$j(this).attr('compareId'),$j(this));
            if(canSubmit != undefined && canSubmit == false){
                returnValue = false;
            }
        });
        return returnValue;
    }