﻿    var canSubmit = false;
    function loadCheckJs(){
        $('input[vtype]').each(function(){
            var vtype = $(this).attr('vtype');
            var compareId = $(this).attr('compareId');
            var text = $(this);
            $(this).initDom(vtype);
            $(this).focus(function(){
                text.edit(text);
            });
            $(this).blur(function(){
                text.check(vtype,compareId,text);
            });
        });
    }
    
    $.fn.extend({
        // 初始化dom元素
        initDom : function(vtype){
            $(this).after('<img src="images/focus.gif" style="display:none; margin-left: 8px;" />');
        },
        
        // 验证
        check: function(vtype, compareId, text){
            // 比较2个控件的value值
            if(isEmpty(compareId)){
                var val = text.val();
                if(!isEmpty(val)){
                    text.next().attr('src', 'images/no.gif').show();
                    return false;
                }
                var val2 = document.getElementById(compareId).value;
                if(val2 != val){
                    text.next().attr('src', 'images/no.gif').show();
                    return false;
                }
                $('#' + compareId).next().attr('src', 'images/yes.gif').show();
                text.next().attr('src', 'images/yes.gif').show();
                return true;
            }
            
            // 非空验证
            if(vtype == 'notNull'){
                var val = text.val();
                if(isEmpty(val)){
                    text.next().attr('src', 'images/yes.gif').show();
                    return true;
                }else{
                    text.next().attr('src', 'images/no.gif').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().attr('src', 'images/yes.gif').show();
                    return true;
                }else{
                    text.next().attr('src', 'images/no.gif').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().attr('src', 'images/yes.gif').show();
                    return true;
                }else{
                    text.next().attr('src', 'images/no.gif').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;
        $('input[vtype]').each(function(){
            canSubmit = $(this).check($(this).attr('vtype'),$(this).attr('compareId'),$(this));
            if(canSubmit != undefined && canSubmit == false){
                returnValue = false;
            }
        });
        return returnValue;
    }