$(function(){
	$("#btnSendCode").bind('click',function(){
		sendMessage();
	});
	$("#register").bind('click',function(){
		do_register();
	});
	$("#perfect").bind('click',function(){
		do_perfect();
	});
	$("#get_card").bind('click',function(){
		do_get_card();
	});
});
function do_register(){
	var mobile = $("#mobile").val();
	var password = $("#password").val();
	var confirm_pwd = $("#confirm_pwd").val();
	var verify = $("#verify").val();
	var username=$("#username").val();
	var ref_mobile=$("#ref_mobile").val();
    var real_name=$("#real_name").val();
    var sex=$("#sex").val();
    var birthday=$("#birthday").val();
	var check=check_form();
	if(!check){return false;}
	var query = new Object();
	query.mobile = mobile;
	query.password = password;
	query.repassword = confirm_pwd;
	query.username = username;
    query.real_name = real_name;
    query.sex = sex;
    query.birthday = birthday;
	if(ref_mobile){query.ref_mobile = ref_mobile;}
	query.mobilecode = verify;
	$.ajax({
		url:APP+'mobile/Login/doregister',
		data:query,
		dataType:'json',
		success:function(obj){
			$.showErr(obj.info,0,'hc');
			if(obj.status==1){
				window.location.href=APP+'mobile/index/index';
			}
		}
	});
}
function do_perfect(){
	var mobile = $("#mobile").val();
	var verify = $("#verify").val();
	var mobile=$("input[name='mobile']").val();
	if(mobile==''){
		$.showErr('手机号不能为空',0,'hc');
		return false;
	}
	var register_open = $("#register_open").val();
	var recom_code = $("input[name='recom_code']").val();
	if(register_open==0){
		
		if(recom_code==''){$.showErr('推荐码不得为空',0,'hc');return false;}
	}
	if(!mobile.match(/^0?1[3|4|5|6|7|8|9][0-9]\d{8}$/)){$.showErr('手机号格式不正确',0,'hc');return false;}
	var verify = $("#verify").val();
	if(!verify){$.showErr('短信验证码不能为空',0,'hc');return false;}
	var query = new Object();
	query.mobile = mobile;
	query.mobilecode = verify;
	query.recom_code = recom_code;
	$('.zhezhao').show();
	$('#shclProgress').show();
	$.ajax({
		url:APP+'mobile/Login/doperfect',
		data:query,
		dataType:'json',
		success:function(obj){
			$('.zhezhao').hide();
			$('#shclProgress').hide();
			$.showErr(obj.info,0,'hc');
			if(obj.status==1){
				if(obj.url){
					window.location.href=obj.url;
				}else{
					window.location.href=APP+'mobile/member/index';
				}
			}else{
				window.location.href=APP+'mobile/login/perfect';
			}
		}
	});
}
function do_get_card(){
	var mobile = $("#mobile").val();
	var query = new Object();
	query.mobile = mobile;
	if(!mobile.match(/^0?1[3|4|5|6|7|8|9][0-9]\d{8}$/)){$.showErr('手机号格式不正确',0,'hc');return false;}
	$('.zhezhao').show();
	$('#shclProgress').show();
	$.ajax({
		url:APP+'mobile/Login/do_get_card',
		data:query,
		dataType:'json',
		success:function(obj){
			$('.zhezhao').hide();
			$('#shclProgress').hide();
			$.showErr(obj.info,0,'hc');
			if(obj.status==1){
				window.location.href=APP+'mobile/member/index';
			}
		}
	});
}
var InterValObj; //timer变量，控制时间
var curCount;//当前剩余秒数
function sendMessage() {
	var time = $("#true_time").val();
	var count = parseInt(time); //间隔函数，1秒执行
    curCount = count;
	//设置button效果，开始计时
    var url=APP+"Mobile/Ajax/sendsms";
    var mobile=$('#mobile').val();
    var unique=$('#unique').val();
    if(verify_mobile(mobile)===false) return false;
	var is_type = 1;
	if(is_register == 0){
		is_type = 2;
	}
    var param="mobile=" + mobile + "&type=" + is_type;
        param += "&unique=" + unique;
    var sms_reg_verify=$('#sms_reg_verify').val();
    if(sms_reg_verify) {
        var regverify = $('#regverify').val();
        param += "&regverify=" + sms_reg_verify;
    }
    if($("#btnSendCode").hasClass("submitting"))return;
    $("#btnSendCode").addClass("submitting");
    $.ajax({
        type: "POST",
        dataType: "JSON",
        url: url,
        data:param,
        success:function (data){	
            $("#btnSendCode").attr("disabled", "true");
            if(data.status==0){
                $("#btnSendCode").removeClass("submitting");
                $.showErr(data.msg,0,'hc');
                return false;
            }else{
    			$("#btnSendCode").removeAttr('onclick');
                $("#btnSendCode").html(curCount + "s后重新获取");
                InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次
            }
    }
});
}
//timer处理函数
function SetRemainTime() {
    if (curCount == 0) {
        $("#btnSendCode").removeClass("submitting");
		//$("#btnSendCode").attr('onclick',"return sendMessage()");
        window.clearInterval(InterValObj);//停止计时器
        $("#btnSendCode").removeAttr("disabled");//启用按钮
        $("#btnSendCode").html("获取验证码");
    }else {
        curCount--;
        $("#btnSendCode").html(curCount + "s后重新获取");
    }
}
function verify_mobile(phone){
    if(phone == ''){
        $.showErr('手机号不能为空',0,'hc');
        return false;
    }
    if(!phone.match(/^0?1[3|4|5|6|7|8|9]\d{9}$/)){
        $.showErr('手机号格式不正确',0,'hc');
        return false;
    }
    return true;
}
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(!mobile.match(/^0?1[3|4|5|6|7|8|9][0-9]\d{8}$/)){$.showErr('手机号格式不正确',0,'hc');return false;}
	var ref_mobile=$("input[name='ref_mobile']").val();
	if(ref_mobile&&!ref_mobile.match(/^0?1[3|4|5|6|7|8|9][0-9]\d{8}$/)){$.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='confirm_pwd']").val();
	if(password!=repassword){$.showErr('两次密码不一致',0,'hc');return false;}
	var verify = $("#verify").val();
	if(!verify){$.showErr('短信验证码不能为空',0,'hc');return false;}
	return true;
}