var REG = function(){
    function showText(obj,text){
        $(obj).parent().parent().find("span.reg_body").html(text);
    }
    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();
            if(checkRequired()){
                var sn = $("input[name='subject']:checked")[0].nextSibling.nodeValue;
                if(confirm("您报名的科目是“"+sn+"”，请牢记您的用户名和密码，报名成功后可凭此登陆查看缴费等状态，信息提交之后不可再更改，确认要提交吗？")){
                    $("#form").ajaxSubmit({
                        url: "/training/reg",
                        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{
                showWarning($(this),"&radic;");
            }
        });
        $("[name=repassword]").on("blur",function(){
            var e = $(this).val();
            var e2 = $("[name=password]").val();
            if(e!=e2){
                showWarning($(this),"两次输入密码不一致");
            }else{
                showWarning($(this),"&radic;");
                showWarning($("[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();
    $("#appreciationTaxTypeId").change(function(){
		var appreciationTaxType = $("#appreciationTaxTypeId").find("option:selected").val();
		if("1" == appreciationTaxType){
			$("#companyNameId").css("display","");
			$("#subjectNameId").css("display","");
			$("#taxpayerNumId").css("display","");
			$("#addressNameId").css("display","none");
			$("#telephoneId").css("display","none");
			$("#bankAddressId").css("display","none");
			$("#bankAccountId").css("display","none");
			$("#taxpayerNumId01").removeAttr("class");
			$("#addressNameId01").removeAttr("class"); 
			$("#telephoneId01").removeAttr("class");
			$("#bankAddressId01").removeAttr("class");
			$("#bankAccountId01").removeAttr("class");
		}else if("2" == appreciationTaxType){
			$("#companyNameId").css("display","");
			$("#subjectNameId").css("display","");
			$("#taxpayerNumId").css("display","");
			$("#addressNameId").css("display","");
			$("#telephoneId").css("display","");
			$("#bankAddressId").css("display","");
			$("#bankAccountId").css("display","");
			$("#taxpayerNumId01").attr("class","required");
			$("#addressNameId01").attr("class","required");
			$("#telephoneId01").attr("class","required");
			$("#bankAddressId01").attr("class","required");
			$("#bankAccountId01").attr("class","required");
		}else {
			$("#companyNameId").css("display","none");
			$("#subjectNameId").css("display","none");
			$("#taxpayerNumId").css("display","none");
			$("#addressNameId").css("display","none");
			$("#telephoneId").css("display","none");
			$("#bankAddressId").css("display","none");
			$("#bankAccountId").css("display","none");
			$("#taxpayerNumId01").removeAttr("class");
			$("#addressNameId01").removeAttr("class");
			$("#telephoneId01").removeAttr("class");
			$("#bankAddressId01").removeAttr("class");
			$("#companyNameId01").removeAttr("class");
			$("#subjectNameId01").removeAttr("class");
			$("#bankAccountId01").removeAttr("class");
		}
	})
	
});


