$(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 id = $("#id").val();
	var password = $("#password").val();
	var confirm_pwd = $("#confirm_pwd").val();
	var verify = $("#verify").val();
	var username=$("#username").val();
	var province=$("#province").val();
	var city_id=$("#city_id").val();
	var district=$("#district").val();
	var ref_mobile=$("#ref_mobile").val();
    var real_name=$("#real_name").val();
    // var sex=$("input[name='sex']").val();
    var sex=$('#sex').val();
    var birthday=$("#birthday").val();
	var check=check_form();
	if(!check){return false;}
	var xuanz = $(".xuanz");
    for(var c=0;c<xuanz.length;c++){
        var c_dui = $(xuanz[c]).attr('vaid');
        if(xuanz[c].checked == true){
            var xuan_name = $("#name_"+c_dui).val();
            if(xuan_name){
                $("#name_"+c_dui).val(xuan_name+$(xuanz[c]).val()+",");
            }else{
                $("#name_"+c_dui).val(","+$(xuanz[c]).val()+",");
            }
        }
    }

	var query = new Object();
	query.mobile = mobile;
	query.id = id;
	query.password = password;
	query.repassword = confirm_pwd;
	query.province = province;
	query.city_id = city_id;
	query.district = district;
	query.username = username;
    query.real_name = real_name;
    query.sex = sex;
    query.birthday = birthday;
    query.name = new Object();
    var val_class = $(".val_class");
	for(var i=0;i<val_class.length;i++){
		var dui = $(val_class[i]).attr('vaid');
		if(dui != null){
			query.name[dui] = $(val_class[i]).val();
		}
	}
	if(ref_mobile){query.ref_mobile = ref_mobile;}
	query.mobilecode = verify;
    
	// alert(JSON.stringify(query));return false;
	$('.zhezhao').show();
	$('#shclProgress').show();
	$.ajax({
		type: "POST",
		url:APP+'mobile/Member/activation',
		data:query,
		dataType:'json',
		success:function(obj){
			// alert(JSON.stringify(obj));
			if(obj.status == 1){
				$.showErr(obj.msg,0,'hc');
				window.location.href=APP+'mobile/member/index';
			}else{
				if(obj.msg.indexOf('推荐人不能是自') != -1){
					$("#ref_mobile").val('');
					$("#ref_mobile").removeAttr('readonly');
				}
				$.showErr(obj.msg,0,'hc');
			}
		}
	});
}
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 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=" + sms_reg_verify;
    }
    $.ajax({
        type: "POST",
        dataType: "JSON",
        url: url,
        data:param,
        success:function (data){	
        if(data.status==0){
            $.showErr(data.msg,0,'hc');
            return false;
        }else{
			$("#btnSendCode").removeAttr('onclick');
            $("#btnSendCode").attr("disabled", "true");
            $("#btnSendCode").html(curCount + "s后重新获取");
            InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次
        }
    }
});
}
//timer处理函数
function SetRemainTime() {
    if (curCount == 0) {
		$("#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][0-9]\d{8}$/)){
        $.showErr('手机号格式不正确',0,'hc');
        return false;
    }
}
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 birthday=$("input[name='birthday']").val();
	if(birthday==''){
		$.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();
	return true;
}
var ob;
function choose_attr(id,obj){
    var choose_id = $("#choose_id").val();
    ob = obj;
    if(choose_id != id){
        $("#choose_id").val(id);
        get_attr_list(id);//显示
        $('.choose_attr').show();
    }else{
        $('.choose_attr').show();
    }
}
function get_attr_list(id){
    $('.choose_goods_main').html('');
    var query = new Object();
    query.id = id;
    var url = APP+"Mobile/UserInfo/search_attr";
    $.ajax({
        url: url,
        data: query,
        type: "post",
        dataType: "json",
        success: function (ret) {
            if(ret.status){
                var html = ht_input = '';
                var xd_ob = $(ob).next(".xuan_show").children();
                if(ret.data){
                    for (var i = 0; i < ret.count; i++){
                        var is_have = 0;
                        for (var n = 0; n < xd_ob.length; n++){
                            if(ret.data[i].name == $(xd_ob[n]).val()){
                                is_have = 1;
                            }
                        }
                        if(ret.data[i].name){
                            if(ret.data[i].is_show || is_have){
                                ht_input = '<input type="checkbox"  checked="true" value="'+ret.data[i].name+'" name="choose_attr"  class="f_l" />';
                            }else{
                                ht_input = '<input type="checkbox" value="'+ret.data[i].name+'" name="choose_attr"  class="f_l" />';
                            }
                            html +='<li class="choose_goods_item clearfix" title="'+ret.data[i].name+'"><label>'+ht_input+ret.data[i].name+'</li>';
                        }
                    }
                }
                $('.choose_goods_main').html(html);
            }
        }
    });
}
function add_span(){
    var html = '';
    var obj_input = $(".choose_goods_main input");
    var choose_id = $("#choose_id").val();
    var choose = $("#name_"+choose_id+"");
    var choose_val = '';
    for (var i = 0; i < obj_input.length; i++){
        if(obj_input[i].checked == true){
            var cont = $(obj_input[i]).val();
            html += "<input type='hidden' value='"+cont+"'/>"+cont+"&nbsp;&nbsp;&nbsp;";
            choose_val += ","+cont;
            choose.val(choose_val);
        }
    }
    $(ob).next(".xuan_show").html(html);
    $('.choose_attr').hide();
}
function update_date(id){
    var currYear = (new Date()).getFullYear();  
    var opt={};
    opt.date = {preset : 'date'};
    opt.datetime = {preset : 'datetime'};
    opt.default = {
        theme: 'android-ics light', //皮肤样式
        display: 'modal', //显示方式 
        mode: 'scroller', //日期选择模式
        dateFormat: 'yy-mm-dd',
        lang: 'zh',
        startYear: currYear - 100, //开始年份
        endYear: currYear + 100, //结束年份
        onSelect:function(valueText,inst){}
    };
    $("#appDate"+id).mobiscroll($.extend(opt['date'], opt['default']));
}
function close_choose(){
    $('.choose_attr').hide();
}