//验证控件
function ValidateForm(id, vt) {
    var result = true;
    if (document.getElementById(id)) {
        var objs = "#" + id + " input[type=text],#" + id + " input[type=password],#" + id + " input[type=hidden]";

        $(objs).each(function () {
            var att = $(this).attr("attr");
            if (att != "" && att != "{}") {
                var attr = eval("(" + $(this).attr("attr") + ")");
                if (attr != null) {
                    if (attr.isnull != undefined && attr.isnull == false) {
                        result = ValidateMsg(ValidateIsNull($(this).val()), obj, attr, vt);
                    } else if (attr.regex != undefined && attr.regex != "") {
                        result = ValidateMsg(ValidateRegex($(this).val(), attr.regex), obj, attr, vt);
                    }
                }
            }
        });
    }
    return result;
}

//验证错误提示方式
function ValidateMsg(result, obj, attr, vt) {
    switch (vt) {
        default:
            if (result == false) {
                alert(attr.msg);
                obj.select();
            }
            break;
    }

    return result;
}

//验证是否为空
function ValidateIsNull(val) { return ($.trim(val).length == 0); }

//正则匹配验证
function ValidateRegex(val, f) {
    var r;
    if (f.charAt(0) == "/")
        r = eval(f);
    else
        r = new RegExp(f);
    return !r.test($.trim(val));
}

//输出结果
function SubmitResult(data) {
    var result = eval("(" + data + ")");
    if (result != null) {
        if (result.result == "success") {
            if (result.msg != "")
                alert(result.msg);
            if (result.href != "")
                location.href = result.href;
        } else {
            if (result.msg != "")
                alert(result.msg);
        }
    } else { alert(data); }
}

//输出结果
function SubmitResultAjax(data) {
    var result = eval("(" + data + ")");
    if (result != null) {
        if (result.result == "success") {
            $("<div style=\"position:fixed; top:150px; left:50%; background-color:green; color:#fff; z-index:99;padding:3px 10px;\" >" + result.ajaxmsg + "</div>").appendTo($("body")).fadeOut(3000);
        } else {
            $("<div style=\"position:fixed; top:150px; left:50%; background-color:#CC3300; color:#fff; z-index:99;padding:3px 10px;\" >" + result.msg + "</div>").appendTo($("body")).fadeOut(3000);
        }
    } else { alert(data); }
}