var REG = function () {
    function showText(obj, text) {
        $(obj).parent().parent().find("span.reg_body").html("<b style='color:green;'>" + text + "</b>");
    }

    function showWarning(obj, text) {
        $(obj).parent().parent().find("span.reg_body").html("<b style='color:red'>" + text + "</b>");
    }

    function checkRequired() {
        var success = true;
        $(".required").each(function (n, v) {
            if ($.trim($(this).val()) == '') {
                showWarning($(this), "请填写此信息");
                success = false;
            }
        });
        return success;
    }

    function ac1() {
        $("#submit").on("click", function () {
            $(".message").html();
            var province = $('#JSheng option:selected').text();
            var city = $('#JShi option:selected').text();
            var district = $('#JQu option:selected').text();
            if (checkRequired()) {
                var sn = $("input[name='subject']:checked")[0].nextSibling.nodeValue;
                if (confirm("您报名的科目是“" + sn + "”，请牢记您的用户名和密码，报名成功后可凭此登陆查看缴费等状态，信息提交之后不可再更改，确认要提交吗？")) {
                    $("#form").ajaxSubmit({
                        url: "/training/reg?province=" + province + "&chengshi=" + city.replace('市辖区', '').replace('县', '').replace('请选择', '') + "&district=" + district.replace('请选择', ''),
                        type: "post",
                        dataType: "json",
                        success: function (data) {
                            if (data.success) {
                                location.href = "/training/infop";
                            } else {
                                $(".message").html(data.info);
                            }
                        }
                    });
                }
            }

        });
        $("#submit2").on("click", function () {
            $(".message").html();
            if (checkRequired()) {
                var sn = $("input[name='subject2']:checked")[0].nextSibling.nodeValue;
                if (confirm("您报名的科目是“" + sn + "”，请牢记您的用户名和密码，报名成功后可凭此登陆查看缴费等状态，信息提交之后不可再更改，确认要提交吗？")) {
                    $("#form2").ajaxSubmit({
                        url: "/training/reg2",
                        type: "post",
                        dataType: "json",
                        success: function (data) {
                            if (data.success) {
                                location.href = "/training/infop";
                            } else {
                                $(".message").html(data.info);
                            }
                        }
                    });
                }
            }

        });
        $(".required").on("blur", function () {
            if ($.trim($(this).val()) == '') {
                showWarning($(this), "请填写此信息");
            } else {
                showText($(this), "&radic;");
            }
        });
        $("#reset").on("click", function () {
            location.href = '/training/regp';
        });
        $("#login").on("click", function () {
            location.href = '/training/loginp';
        });
        $("#reset2").on("click", function () {
            location.href = '/training/regp2';
        });
        $("#login2").on("click", function () {
            location.href = '/training/loginp2';
        });
        $("[name=email]").on("blur", function () {
            var e = $("[name=email]").val();
            // if (new RegExp("^\\w+@qq\.com$").test(e)) {
            //     showWarning($("[name=email]"), "请勿使用QQ邮箱注册!");
            //     return;
            // }
            var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            if (!filter.test(e)) {
                showWarning($("[name=email]"), "邮箱格式不正确!");
            } else {

                var type = $("[name=subject]").length > 0;
                $.get("/training/checkEmail?email=" + e + "&regType=" + type, function (data) {
                    if (data.success) {
                        showWarning($("[name=email]"), "邮箱被占用!");
                    } else {
                        showText($("[name=email]"), "&radic;");
                    }
                }, 'json');
            }
        });
        $("[name=password]").on("blur", function () {
            var e = $(this).val();
            if (e.length < 6 || e.length > 20 || !new RegExp("\^\\w+\$").test(e)) {
                showWarning($(this), "密码格式不正确（6-20位数字、字母或下划线）");
            } else {
                showText($(this), "&radic;");
            }
        });
        $("[name=repassword]").on("blur", function () {
            var e = $(this).val();
            var e2 = $("[name=password]").val();
            if (e != e2) {
                showWarning($(this), "两次输入密码不一致");
            } else {
                showText($(this), "&radic;");
                showText($("[name=password]"), "&radic;");
                $("[name=password]").blur();
            }
        });
        $("[name=taxpayerNum]").on("blur", function () {
            var code = $(this).val();
            if (!checkSocialCreditCode(code)) {
                showWarning($(this), "不是有效的纳税人识别号！");
            }
        });
    }

    return {
        init: function () {
            ac1();
            $("[name=birthday]").datepicker({
                changeYear: true,
                defaultDate: '-30y',
                minDate: '-100y',
                yearRange: "-100:+0"
            });
            $("[name=dateOfBirth]").datepicker({
                changeYear: true,
                defaultDate: '-30y',
                minDate: '-100y',
                yearRange: "-100:+0"
            });
        }
    }
}();
$(function () {
    REG.init();

    JGetSheng();

    $("#appreciationTaxTypeId").change(function () {
        var appreciationTaxType = $("#appreciationTaxTypeId").find("option:selected").val();
        if ("1" == appreciationTaxType) {
            $("#addressNameId").css("display", "none");
            $("#telephoneId").css("display", "none");
            $("#bankAddressId").css("display", "none");
            $("#bankAccountId").css("display", "none");
            $(".reg_body_duli").css("display", "none");
            $(".xuantian").css("display", "");
            $(".bitian").css("display", "none");
            $("#addressNameId01").removeAttr("class");
            $("#telephoneId01").removeAttr("class");
            $("#bankAddressId01").removeAttr("class");
            $("#bankAccountId01").removeAttr("class");
        } else if ("2" == appreciationTaxType) {
            $("#addressNameId").css("display", "");
            $("#telephoneId").css("display", "");
            $("#bankAddressId").css("display", "");
            $("#bankAccountId").css("display", "");
            $(".xuantian").css("display", "none");
            $(".bitian").css("display", "");
            $(".reg_body_duli").css("display", "");
            $("#addressNameId01").attr("class", "required");
            $("#telephoneId01").attr("class", "required");
            $("#bankAddressId01").attr("class", "required");
            $("#bankAccountId01").attr("class", "required");
        }
    })

});

function JGetSheng() {
    $("#JSheng").empty();
    $("#JSheng").append("<option value='0'>请选择</option>");
    $.get("/resource/front/sheng_shi_qu.txt", function (data) {
        var GetData = eval(data);
        //console.log(GetData); 每个人下载的json格式可能会不一样，可以这样去看看格式为怎么样的，下面好取数据
        $.each(GetData, function (i, item) {
            if (item.level == "1") {
                $("#JSheng").append("<option value='" + item.sheng + "'>" + item.name + "</option>");
            }
        })
    })
    JGetShi();
}

function JGetShi() {
    var sheng = $("#JSheng").val();
    $("#JShi").empty();
    $("#JShi").append("<option value='0'>请选择</option>");
    $.get("/resource/front/sheng_shi_qu.txt", function (data) {
        var GetData = eval(data);
        $.each(GetData, function (i, item) {
            if (item.sheng == sheng && item.level == "2") {
                $("#JShi").append("<option value='" + item.di + "'>" + item.name + "</option>");
            }
        })

    })
    JGetQu();
}

function JGetQu() {
    var sheng = $("#JSheng").val();
    var shi = $("#JShi").val();
    $("#JQu").empty();
    $("#JQu").append("<option value='0'>请选择</option>");
    $.get("/resource/front/sheng_shi_qu.txt", function (data) {
        var GetData = eval(data);
        $.each(GetData, function (i, item) {
            if (item.sheng == sheng && item.di == shi && item.level == "3") {
                $("#JQu").append("<option value='" + item.xian + "'>" + item.name + "</option>");
            }
        })
    })
}

