﻿; (function ($) {
    //表单提交
    $.fn.Check_AjaxSubmint = function (o) {
        var getthis = $(this);
        FormPost = function () {
            if ($CheckForm(getthis, o)) {
                window.top.$.submitbg(1);
            }
            else {
                return false;
            }
        };
        FormBack = function (obj) {
            window.top.$.submitbg(0);
            var lastdiv = null;
            if (window.top.$._Array.length > 0) lastdiv = window.top.$._Array[window.top.$._Array.length - 1];
            try {
                var data = eval('(' + obj + ')');
                if (data.state) {
                    if (data.type == 1) {
                        if(data.message!="")
                        {
                           window.top.$.alert(data.message, 1, function(){ window.top.location.href = data.url; });
                        }
                        else
                        {
                           window.top.location.href = data.url;
                        }       
                        
                    }
                    else {
                        if (data.type == 2) {
                            var frame = window.top.frames.eval(data.url);
                            if (frame != null) frame.location.reload();
                        }
                        if (data.message) {
                            if (lastdiv) {
                                window.top.$.alert(data.message, 1, function () { window.top.$.unxBox({ id: lastdiv }); });
                            }
                            else {
                                window.top.$.alert(data.message, 1);
                            }
                        }
                    }
                }
                else {
                    window.top.$.alert(data.message, 0);
                }
            }
            catch (ex) {}
        };
        var option = { beforeSubmit: FormPost, success: FormBack };
        getthis.ajaxForm(option);
    };
    //验证表单，2种方式表现
    $CheckForm = function (obj, o) {
        var s = true;
        $('input[tip],select[tip]', obj).each(function () {
            var _self = this;
            var f = FormCheckThis(_self);
            if (!f.state) s = false;
            if (o == true) {
                showmessagediv(_self, f);
            }
            else {
                if (!f.state) {
                    showmessage($(_self).attr('tip')+f.msg, 0);
                    return false;
                }
            }
        });
        return s;
    }
    FormCheckThis = function (obj) {
        var c = true;
        var _this = $(obj);
        var msgstr = '';
        var defaults = {
            smin: 0, //最小值,0不做判断
            smax: 0, //最大值,0不做判断
            isnumber: false, //判断数字
            mobil: false, //判断手机
            checkinput: '', //验证输入是否一致
            select: false, //判断选择
            ajaxpost: '', //ajax验证
            email: false//判断邮箱
        };
        var tip = _this.attr('tip');
        if (tip != undefined) {
            var rule = _this.attr('rules');
            if (rule != undefined) {
                var val = $.trim(_this.val());
                var rules = eval('(' + rule + ')');
                rules = $.extend({}, defaults, rules || {}); //合并参数
                //判断最小
                if (rules.smin > 0 && c) {
                    if (val.length > 0) {
                        if (GetStrLength(val) < rules.smin) {
                            c = false;
                            msgstr = '不能小于' + rules.smin + '个字符!';
                        }
                    }
                    else {
                        c = false;
                        msgstr = '不能为空！';
                    }
                }
                //判断最大
                if (rules.smax > 0 && c) {
                    if (GetStrLength(val) > rules.smax) {
                        c = false;
                        msgstr = '不能大于' + rules.smax + '个字符!'; 
                    }
                }
                //判断数字
                if (rules.isnumber && c) {
                    if (isNaN(val)) {
                        c = false;
                        msgstr = '数字格式不正确!';
                    }
                }
                //判断手机
                if (rules.mobil && c) {
                    if (isNaN(val) || val.length != 11) {
                        c = false;
                        msgstr = '手机格式不正确!';
                    }
                }
                //判断邮箱
                if (rules.email && c) {
                    if (!/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(val)) {
                        c = false;
                        msgstr = 'Email格式不正确!';
                    }
                }
                //判断输入
                if (rules.checkinput != '' && c) {
                    var v = $.trim($('#' + rules.checkinput).val());
                    if (val != v) {
                        c = false;
                        msgstr = '两次输入不一致!';
                    }
                }
                //判断选择
                if (rules.select && c) {
                    var so = obj.options ? obj.options.selectedIndex : -1;
                    if (so <= 0) {
                        c = false;
                        msgstr = '请选择' + tip + '!';
                    }
                }
                //ajax验证
                if (rules.ajaxpost != '' && c) {
                   var ajaxback = $(obj).attr('ajaxback');
                   var isajax=true;
                   if(ajaxback)
                   {
                      if(ajaxback=='1') isajax = false;
                   }
                   if(isajax){
                      var urlstr = rules.ajaxpost + val;
                      var data = $.ajax({
                         type:"POST",
                         async: false,//设置成同步调用
                         cache: false,//不缓存页面
                         //contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                         url: urlstr.split('?')[0], 
                         data: urlstr.split('?')[1],
                         timeout: 1000 // 设置请求超时时间
                      }).responseText;
                      if(data=='True'){
                         c = false;
                         msgstr = tip + '已经被使用!';
                         $(obj).removeAttr("ajaxback");
                      }
                      else{
                         $(obj).attr("ajaxback","1");
                      }
                   }
                }
            }
        }
        return { state: c, msg: msgstr };
    };
    //获取字符串长度
    GetStrLength = function(str){
       var valLength = 0;
       for (var i = 0; i < str.length; i++) {
          var word = str.substring(i, i+1);
           if (/[^\x00-\xff]/g.test(word)) {
             valLength += 2;
           } else {
             valLength++;
           }
       }
       return valLength;
    };
    //
    $.fn.Ajax_FormCheck = function () {
        var getthis = $(this);
        $('input[tip],select[tip]', getthis).each(function () {
            $(this).bind('blur',function(){
               $(this).removeAttr("ajaxback");
               showmessagediv(this, FormCheckThis(this));
            });
        });
    };
    //弹出消息层
    showmessage = function (msg, f) {
        window.top.$.alert(msg, f);
    };
    //验证消息框
    showmessagediv = function (obj, f) {
        var div = $('#' + $(obj).attr('id') + '_Msg');
        div.removeClass();
        if (f) {
            if (f.state) {
                div.addClass('r_right')
            }
            else {
                div.addClass('r_err')
            }
            div.text(f.msg);
        }
    };    
})(jQuery);
