$(function(){
	$("#btnSendCode").attr('style',"cursor:pointer;");
	$("input[name='username']").bind("focus change",function(){
		var username=$("input[name='username']").val(); 
		var re1=/[a-zA-Z]|[0-9]/g;
		var re2=/[\u4e00-\u9fa5]/g;
		var length=0;
		var length2=0;
		if(!username){
			$("#username_hc").html('用户名不能为空');
		}
		if(username.match(re1)){
			var letter=username.match(re1);
			length+=letter.length;
			length2+=letter.length;
		}
		if (username.match(re2)) {
		      var chinese = username.match(re2);
		      length += chinese.length;
		      length2 = length2+chinese.length*2;
		} 
		if (length == username.length) {
		      if (length2>=6 && length2<=16) {
		            $("#username_hc").html('通过验证');
	   	            }else{
		              $("#username_hc").html('请输入6-16位字符,可包含汉字、字母、数字');
		            }
		          
		}else{
		              $("#username_hc").html('请输入6-16位字符,可包含汉字、字母、数字');
		}
	});
	$("input[name='mobile']").bind("focus change",function(){
		var mobile=$("input[name='mobile']").val();
		if(mobile==''){
			$("#mobile_hc").html('手机号不能为空');return;
		}
		if(!$.checkMobile(mobile)){
			$("#mobile_hc").html('手机号格式不正确');
		}else{
			$("#mobile_hc").html('通过验证');
		}
	});
	$("input[name='ref_mobile']").bind("change",function(){		
		var ref_mobile=$("input[name='ref_mobile']").val();
		if(ref_mobile&&!$.checkMobile(ref_mobile)){
			$("#ref_mobile_hc").html('手机号格式不正确');
		}else if(ref_mobile){
			$("#ref_mobile_hc").html('通过验证');
		}
	});
	$("input[name='password']").bind("focus change",function(){
		var password=$("input[name='password']").val();
		if(!password.match(/^[\w]{6,12}$/)){
			$("#password_hc").html('请输入6-16位字符,可包含字母、数字、下划线');
		}else{
			$("#password_hc").html('通过验证');
		}
	});
	$("input[name='repassword']").bind("focus change",function(){
		var password=$("input[name='password']").val();
		if(!password){
			$("#repassword_hc").html('密码不能为空');return;
		}
		var repassword=$("input[name='repassword']").val();
		if(password!=repassword){
			$("#repassword_hc").html('两次密码不一致');
		}else{
			$("#repassword_hc").html('通过验证');
		}
	});
});

function login_re(){
	var check=check_form();
	if(!check){
		//$.showErr('请填写正确的注册信息',0,'hc');
		return false;
	}
    var query= new Object();
    query = $("form[name='form_register']").serialize();
    $.ajax({
        type: "POST",
        url: APP+"Home/Login/to_register",
        data: query,
        success: function(data){
            if(data.status == 1){
                $.showSuccess(data.info,0,'hc');
                window.location.href=data.url;
            }else{
                $.showErr(data.info,0,'hc');
            }
        }
    });
}
var InterValObj; //timer变量，控制时间
var curCount;//当前剩余秒数
function sendMessage() {
	var time = $("#true_time").val();
	var count = parseInt(time); //间隔函数，1秒执行
    curCount = count;
	//设置button效果，开始计时
    var url=APP+"Home/Ajax/sendsms";
    var mobile=$('#phone').val();
    var unique=$('#unique').val();
    if(verify_mobile(mobile)==false) return false;
    var param="mobile=" + mobile + "&type=" + 1;
        param += "&unique=" + unique;
    var sms_reg_verify=$('#sms_reg_verify').val();
    if(sms_reg_verify) {
        var regverify = $('#regverify').val();
        param += "&regverify=" + regverify;
    }
    $.ajax({
        type: "POST",
        dataType: "JSON",
        url: url,
        data:param,
        success:function (data){
        if(data.status==0){
            $.showErr(data.msg,0,'hc');
            //$('#changevalidate').find('img').attr('src',$('#changevalidate').find('img').attr('src')+'&time='+Math.random())
            return false;
        }else{
			$("#btnSendCode").removeAttr('onclick');
			$("#btnSendCode").attr('style',"cursor:not-allowed");
            $("#btnSendCode").attr("disabled", "true");
            $("#btnSendCode").html(curCount + "s后重新获取");
            InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次
        }
    }
});
}
//timer处理函数
function SetRemainTime() {
    if (curCount == 0) {
        window.clearInterval(InterValObj);//停止计时器
		$("#btnSendCode").attr('style',"cursor:pointer;");
		$("#btnSendCode").attr('onclick',"return sendMessage()");
        $("#btnSendCode").removeAttr("disabled");//启用按钮
        $("#btnSendCode").html("获取验证码");
    }else {
        curCount--;
        $("#btnSendCode").html(curCount + "s后重新获取");
    }
}
function check_form(){
	var username=$("input[name='username']").val();
	if(!username){
		$.showErr('用户名不能为空',0,'hc');
		return false;
	}
	var re1=/[a-zA-Z]|[0-9]/g;
	var re2=/[\u4e00-\u9fa5]/g;
	var length=0;
	var length2=0;
	if(username.match(re1)){
		var letter=username.match(re1);
		length+=letter.length;
		length2+=letter.length;
	}
	if (username.match(re2)) {
		var chinese = username.match(re2);
		length += chinese.length;
		length2 = length2+chinese.length*2;
	} 
	if (length == username.length) {
		if (length2<6 || length2>16) {
		      $.showErr('用户名格式不正确',0,'hc');
		       return false;
		}      
	}else{
		      $.showErr('用户名格式不正确',0,'hc');
		       return false;
	}
	var mobile=$("input[name='mobile']").val();
	if(mobile==''){
		$.showErr('手机号不能为空',0,'hc');
		return false;
	}
	if(!$.checkMobile(mobile)){$.showErr('手机号格式不正确',0,'hc');return false;}
	var ref_mobile=$("input[name='ref_mobile']").val();
	if(ref_mobile&&!$.checkMobile(ref_mobile)){$.showErr('推荐人手机号格式不正确',0,'hc');return false;}
	var password=$("input[name='password']").val();
	if(!password.match(/^[\w]{6,12}$/)){$.showErr('密码格式不正确',0,'hc');return false;}
	var repassword=$("input[name='repassword']").val();
	if(password!=repassword){$.showErr('两次密码不一致',0,'hc');return false;}
	return true;
}
function verify_mobile(phone){
    if(phone == ''){
        $.showErr('手机号不能为空',0,'hc');
        return false;
    }
    if(!$.checkMobile(phone)){
        $.showErr('手机号格式不正确',0,'hc');
        return false;
    }
}