/*邀请好友*/
$(function(){
	
	var phoneCode = false;
	var ifCode = false;
	var ifpassword = false;
	var state;
	var inviterId = GetQueryString("professorId");
	var username = GetQueryString("professorName");
	//var inviterId="6E199CFA7B034D69A7029731B6E77D4A";
	//var username="特朗普";
	
	$(".inviteTit span").text(username);
	/*控制提示框样式*/
	function bombox(textt){
		$(".bomb-box").fadeIn("slow");
		$(".bomb-box").text(textt);
		var bombwidth = $(".bomb-box").width();
	    $(".bomb-box").css({"marginLeft": -(bombwidth+25)/2 + "px"});
		setTimeout(function(){
			$(".bomb-box").fadeOut("slow");
		},4000);
	}
	
	/*校验提交按钮显示状态*/
	$('.form-group').on('keyup', "#userphone,#code,#password,#username", function() {
		if($("#userphone").val() == "" || $("#code").val() == "" || $("#password").val() == "" || $("#username").val() == "") {
			$("#regbtn").attr("disabled",true);
		} else {
			$("#regbtn").attr("disabled",false);
		}
	});
	
	/*注册按钮*/
	$("#regbtn").on('click',function() {
		var oStringLength=$("#username").val().length;
		if(oStringLength>10){
			bombox("请输入您的真实姓名");
			return;
		}
		codeVal();
		if(ifpassword && ifCode){
			completeReg();
		}
	});
		
	/*点击获取验证码*/
	$('#obtain-code').on('click',function() {
		phoneVal();
	});
	
	/*校验手机号*/
	function phoneVal() {
		var hunPhone = /^1[3|4|5|7|8]\d{9}$/;
		if(hunPhone.test($("#userphone").val())) {
			isReg();
		} else {
			bombox("请输入正确的手机号码");
			return;
		}
	}
	
	/*校验用户名是否注册*/
	function isReg() {
		$.ajax({
			url:"/ajax/isReg?key=" + $("#userphone").val(),
			dataType: 'json', //数据格式类型
			type: 'GET', //http请求类型
			timeout: 10000, //超时设置
			success: function(data) {
				if(data.data == false) {
					bombox("您的手机已被注册");
					return;
				} else {
					phoneCode = true;
					if(phoneCode){
						sendAuthentication();
					}
				}
			},
			error: function() {
				bombox("服务器链接超时");
				return;
			}
		});
	}
	
	/*手机发送验证码*/
	function sendAuthentication() {
		$.ajax({
			url:"/ajax/regmobilephone",
			data: {
				mobilePhone: $("#userphone").val()
			},
			dataType: 'json', //数据格式类型
			type: 'GET', //http请求类型
			async: false,
			timeout: 10000, //超时设置
			success: function(data) {
				//console.log(data);
				if(data.success) {
					state = data.data;
					doClick();
				}
			},
			error: function() {
				bombox("服务器链接超时");
				return;
			}
		})
	}
	
	/*30s后重新获取验证码*/
	function doClick() {
		$("#obtain-code").attr("disabled",true);
		$("#obtain-code").text("30s后重新获取");
		var clickTime = new Date().getTime();
		var Timer = setInterval(function() {
			var nowTime = new Date().getTime();
			var second = Math.ceil(30 - (nowTime - clickTime) / 1000);
			if(second > 0) {
				$("#obtain-code").text(second + "s后重新获取");
			} else {
				clearInterval(Timer);
				$("#obtain-code").attr("disabled",false);
				$("#obtain-code").text("获取验证码");
			}
		}, 1000);
	}

	
	/*校验验证码*/
	function codeVal() {
		$.ajax({
			url:"/ajax/validCode",
			data: {
				"state": state,
				"vc": $("#code").val()
			},
			dataType: 'json', //数据格式类型
			async: false,
			type: 'POST', //http请求类型
			timeout: 10000, //超时设置
			success: function(data) {
				//console.log(data.success);
				if(data.success) {
					if(data.data==false) {
						bombox("验证码不正确");
						return;
					}else{
						passwordVal();
						ifCode =true;
						return;
					}
				}else{
					//console.log(data.msg);
					if(data.msg=="验证超时"){
						bombox("验证码超时");
						return;
					}else{
						bombox("请填写正确的手机号,验证码");
						return;
					}
					
				}
			},
			error: function() {
				bombox("服务器链接超时");
				return;
			}
		})
	}
	
    /*校验注册密码*/
	function passwordVal() {
		var passwordv = $("#password").val();
	    if(passwordv.length < 6) {
			bombox("请输入由6-24 个字符组成,区分大小写");
			return;
		}else{
			ifpassword = true;
			return;
		}
	}
	
	//注册提交
	function completeReg() {
		$.ajax({
			url:"/ajax/mobileReg",
			data: {
				state: state,
				mobilePhone: $("#userphone").val(),
				validateCode: $("#code").val(),
				password: $("#password").val(),
				inviterId:inviterId,
				name:$("#username").val()
			},
			dataType: 'json', //数据格式类型
			type: 'post', //http请求类型
			async: false,
			success: function(data) {
				if(data.success) {
					bombox("注册成功");
					$(".formblock").hide();
					$(".inviteSucceed").show();
				}
			},
			error: function() {
				bombox("服务器链接超时");
			}
		});
	}
		
	
});