$(function(){
//让用户选择协议	
$("#agreementCheckbox").change(function () {
		var checkboxTrigger = $("#checkboxTrigger");
		var submitBtn = $("#submitBtn");
		if ($(this).prop("checked")) {
			checkboxTrigger.addClass("checkbox-icon-checked");
			submitBtn.addClass("reg-submit").attr("disabled", false);
		} else {
			checkboxTrigger.removeClass("checkbox-icon-checked");
			submitBtn.removeClass("reg-submit").attr("disabled", true);
		}
	});	
	
	
//注册表单验证和提交	

//添加自定义验证规则
//手机号验证
	jQuery.validator.addMethod("phone", function(value, element) { 
		var length = value.length; 
		var phone =  /^1[34578]\d{9}$/
		return this.optional(element) || (length == 11 && phone.test(value)); 
	}, "手机号码格式错误"); 
	//邮编验证
	jQuery.validator.addMethod("code", function(value, element) { 
		var code = /^[0-9][6]$/ 
		return this.optional(element) || (code.test(value)); 
	}, "邮政编码格式错误"); 
		
		
//公共参数配置	
$.validator.setDefaults({
		
 //重写showErrors
			showErrors: function (errorMap, errorList) {
				var msg = "";
				$.each(errorList, function (i, v) {
					//msg += (v.message + "\r\n");
					//在此处用了layer的方法,显示效果更美观
					//layer.tip(v.message, v.element, {time: 2000 });
					layer.msg(v.message,{ icon:0,time: 2000 });
					$(".chkyzm").find("img").attr("src",yzm_url +'?'+Math.random());	
					return false;
				});  
			},
	 //空值检测开启，方便检测下拉框			
	ignore: "",	
	onkeyup : false,
		
});
//公共参数配置完成

	//前台用户注册表单验证
	$("#register").validate({
	
	submitHandler:function(form) {
	$(form).ajaxSubmit({
				
		beforeSubmit:function(formData, jqForm, options){
		//点击后，防止多次点击，将表单先禁用
		$('#submitBtn').attr("disabled","disabled");
		$('#submitBtn').val("正在注册验证......");
		layer.load();
		},
		
		success:function (msg, statusText) {

		if(msg){
		setTimeout(function(){
		layer.closeAll('loading');
						
						
		if(msg['type']=="1"){layer.msg(msg['info'],{ icon:msg['icon'],time: msg['times']},function(){location.href=login_url;});}
		$('#submitBtn').attr("disabled","disabled");
		$('#submitBtn').val("注册成功正在跳转");				
		if(msg['type']=="0"){
		layer.msg(msg['info'],{ icon:msg['icon'],time: msg['times']});
		$(".chkyzm").find("img").attr("src",yzm_url +'?'+Math.random());
		$('#submitBtn').removeAttr("disabled");
		$('#submitBtn').val("注册");
		}
						
						
						
		},2000);

						
					}	

					
				},
				//当有错误的情况下，提示错误原因
				error:function (event, errorText, errorType) {
				layer.closeAll('loading');
				layer.alert("操作失败，失败原因：<br>" + errorType, {icon: 0});
				$('#ew80_submit').removeAttr("disabled");	
				
					
			},
			
			});
		},
		//开始验证

		rules:{
			name:{
				required:true,//必填
				minlength:3, //最少6个字符
				maxlength:32,//最多20个字符
				remote:chk_url,
				
			},
			phone:{
				required:true,
				phone:true,
				remote:chk_url,
				
			},
			
			pwd:{
				required:true,
				minlength:3, 
				maxlength:32,
			},
			pwd01:{
				
				required:true,
				minlength:3, 
				maxlength:32,	
				equalTo:'#pwd',
			},
			yzm:{
				required:true,
				remote:chk_url,
			},
			
		},
		//错误信息提示
		messages:{
			name:{
				required:"必须填写用户帐号",
				minlength:"用户帐号至少为3个字符",
				maxlength:"用户帐至多为32个字符",
				remote: "用户帐号已存在！请更换一个。",
			},
			phone:{
				required:"请填写用户手机号",
				phone:"手机号码格式错误",
				remote: "手机号已经存在！请更换一个。",
			},
			
			pwd:{
				required:"必须填写密码",
				minlength:"密码至少为3个字符",
				maxlength:"密码至多为32个字符",
			},
			pwd01:{
				required:"必须填写密码",
				minlength:"密码至少为3个字符",
				maxlength:"密码至多为32个字符",
				equalTo:"二次填写的密码不相同",
			},
			yzm:{
				required:"还没有填写验证码",
				remote:"验证码错误！",
			},
			
		},

	});
		
})
