var timeNum;
var timer;

$(function(){
	$('#div_phone_form').show();
	$('#div_bing_form').hide();
	$('#div_tips').hide();
	
	$('#bing_success_tips').hide();
	$('#un_regist_tips').hide();
	$('#orther_tips').hide();
	
	//获取验证码
	$('#getVerifyCodeBtn').click(function(){
		var data = getFormData('phone_form');
		if(data.phone!=null && data.phone.length>0){
			var phoneRe = /^(\d{2})?1[3|4|5|6|7|8|9]\d{9}$/;
			if(!phoneRe.test(data.phone)){
				$('#tips_msg').text('手机号码不合法，请重新输入');
				showTips('orther_tips',false);
				return;
			}
			$('#phone').val(data.phone);
			timeNum = 60;
			getVerifyCode(data);
		}else{
			//提示填写手机号码
			$('#tips_msg').text('请填写手机号码');
			showTips('orther_tips',false);
		}
	});
	//重新获取验证码
	$('#reGetVerifyCodeBtn').click(function(){
		var phone = $('#phone').val();
		if(phone!=null && phone.length>0){
			var phoneRe = /^(\d{2})?1[3|4|5|6|7|8|9]\d{9}$/;
			if(!phoneRe.test(phone)){
				$('#tips_msg').text('手机号码不合法，请重新输入');
				showTips('orther_tips',false);
				return;
			}
			getVerifyCode({phone:phone});
		}else{
			//提示填写手机号码
			$('#tips_msg').text('请填写手机号码');
			showTips('orther_tips',false);
		}
	});
	//绑定
	$('#bingBtn').click(function(){
//		document.bing_form.action = ctx+"/info/tiewx";
//		document.bing_form.submit();
		var data = getFormData('bing_form');
		waiting();
		doPost(ctx+"/info/tiewx?appId="+appId,data,false,function(result){	
			if(typeof result == "string"){
				result = eval('('+result+')');
			}
			if(result.error == 0){//绑定成功
				showTips('bing_success_tips',true);
			}else{//其他提示 如校验错误
				$('#tips_msg').text(result.errormsg);
				showTips('orther_tips',false);
			}
			waitingStop();
		});
	});
	//关闭
	$('.closeBtn').click(function(){
		waiting();
		if(isWeiXin()){
			wx.closeWindow();
		}else{
			window.opener=null;
			window.open('','_self');
			window.close();
		}
	});
	$('#closeModel').click(function(){
		$('#orther_tips').hide();
		$('#div_tips').hide();
	});
});

function timerFunction(){
	timeNum--;
	if(timeNum <= 0){
		$("#reGetVerifyCodeBtn").text("获取验证码");
		window.clearInterval(timer);
		timer=null;
		timeNum = 60;
	}else{
		$("#reGetVerifyCodeBtn").text(timeNum+"S");
	}
}

function getVerifyCode(data){
	if(timeNum>=60){
		waiting();
		doPost(ctx+"/info/getcheckmsg?appId="+appId,data,true,function(result){
			if(typeof result == "string"){
				result = eval('('+result+')');
			}
			if(result.error == 0){//已发送
				$('#div_phone_form').hide();
				$('#div_bing_form').show();
				if(timer==null){
					timeNum = 60;
					$('#reGetVerifyCodeBtn').text("60S");
					timer = window.setInterval("timerFunction()",1000);
				}
			}else if(result.error == 310){
				//未注册
				showTips('un_regist_tips',false);
			}else{//操作频繁
				$('#tips_msg').text('操作频繁，请稍后再试');
				showTips('orther_tips',false);
			}
			waitingStop();
		});
	}
}