function login() {
    window.location.href = "./login.html";
}
function register() {
    window.location.href = "./register.html";
}
function back() {
    history.back();
}




var loginIndex = "http://demo.phpwind.net.cn/m/user/index.html"; //登陆成功的页面
var loginUrl  = "./login.html"; //登陆页面

var requestClass =  function(){
	this.urlPre = "http://demo.phpwind.net.cn/index.php/wapapi";
}




requestClass.prototype.login = function(account,password,calBack) {
	var url = this.urlPre + "/User/login/";
	var data = {
		'account' : account,
		'password' : password
	}

	$.post(url,data,function(res){
		calBack && calBack(res);
	},"json");
};




requestClass.prototype.mobilelogin = function(mobile,captcha,calBack) {
	var url = this.urlPre + "/User/mobilelogin/";
	var data = {
		'mobile' : mobile,
		'captcha' : captcha
	}

	$.post(url,data,function(res){
		calBack && calBack(res);
	},"json");
};

requestClass.prototype.register = function(email,username,password,mobile,code,calBack) {
	var url = this.urlPre + "/User/register/";
	var data = {
		'username' : username,
		'password' : password,
		'email' : email,
		'mobile' : mobile,
		'code' : code
	}

	$.post(url,data,function(res){
		calBack && calBack(res);
	},"json");
};

requestClass.prototype.sendCode = function(mobile,event,calBack) {
	var url = this.urlPre + "/Sms/send/";
	var data = {
		'mobile' : mobile,
		'event' : event
	}


	$.post(url,data,function(res){
		calBack && calBack(res);
	},"json");
};

requestClass.prototype.checkToken = function(calBack){

	var token = getCookie("token");

	var url = this.urlPre + "/Token/check/";


	$.post(url,{},function(res){
		calBack && calBack(res);
	},"json");


}



var _request = new requestClass();
var code_time = 3;
var _code_time = 3;

$(".login-btn").on("touchend",function(){
	var account = $("#phone").val();
	var password = $("#password").val();

	if(account == ""){
		layer.msg("账号不能为空");
        return; 
	}

	var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
   	if (!myreg.test(account) && !(/^1[3456789]\d{9}$/.test(account))) {
		layer.msg("账号格式错误");
        return; 
   	}



    if(!password  || password == ""){
    	layer.msg("密码不能为空");
        return; 
    }

	_request.login(account, password,function(res){
		// 登陆成功回调函数
		if(res.code == 1){
			layer.msg("登陆成功");
			var data  = {
				"user_id" :res.data.userinfo.id,
				"mobile" : res.data.userinfo.mobile,
				'token'  : res.data.userinfo.token,
				'expires_in' : res.data.userinfo.expires_in,
				'expiretime' :res.data.userinfo.expiretime
			};
			var dataStr = JSON.stringify(data);
			window.localStorage.setItem("wx_user",dataStr);
			setCookie('token',data.token);
			setCookie('uid',data.user_id);

			setTimeout(function(){
				window.location.href = loginIndex;
			},1000)
		}else{
			if(res.msg == "账户不正确"){
				layer.msg("账号不存在");
			}else{	

				layer.msg(res.msg);
			}
		}
	})
})


//注册按钮
$(".register-btn").on("touchend",function(){
	
	if($(this).hasClass('noActive')){
		return;
	}


	var email = $("#email").val();
	var mobile = $("#phone").val();
	var code = $("#code").val();
	var password = $("#password").val();
	var respassword = $("#respassword").val();


	if(mobile == ""){
		layer.msg("账号不能为空");
        return; 
	}



	var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
   	if (!myreg.test(email)) {
		layer.msg("邮箱格式错误");
        return; 
   	}

	if(!(/^1[3456789]\d{9}$/.test(mobile))){ 
		layer.msg("手机号格式错误");
        return; 
    } 

    if(password == "" || respassword == ""){
		layer.msg("密码不能为空");
		return;
    }

	if(password != respassword){
		layer.msg("两次输入密码不一致");
		return;
	}

	if(code.length != 4){
		layer.msg("验证码填写错误");
		return;
	}

	var checkbox = $("#checkbox").prop("checked");
	if(!checkbox){
		layer.msg("请认真阅读用户协议及版权声明");
		return;
	}

	var username = mobile;
	_request.register(email,username,password,mobile,code,function(res){
		//注册回调函数
		if(res.code == 1){
			layer.msg("注册成功");
			var data  = {
				"user_id" :res.data.userinfo.id,
				"mobile" : res.data.userinfo.mobile,
				'token'  : res.data.userinfo.token,
				'expires_in' : res.data.userinfo.expires_in,
				'expiretime' :res.data.userinfo.expiretime
			};
			var dataStr = JSON.stringify(data);
			window.localStorage.setItem("wx_user",dataStr);
			setCookie('token',data.token);
			setCookie('uid',data.user_id);
			setTimeout(function(){
				window.location.href = loginIndex;
			},1000)
		}else{
			layer.msg(res.msg);
		}
	
	})
})

$("#respassword").on("blur",function(){
	var password = $("#password").val();
	var respassword = $("#respassword").val();
	if(password != respassword){
		layer.msg("两次输入密码不一致");
	}

})



$(".form-group").on("blur","input",function(){
	var email = $("#email").val();
	var phone = $("#phone").val();
	var code = $("#code").val();
	var respassword = $("#respassword").val();
	var password = $("#password").val();
	var checkbox = $("#checkbox").prop("checked");

	var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
   	if (!myreg.test(email) || !(/^1[3456789]\d{9}$/.test(phone)) || code == "" || respassword == "" || password == "" || !checkbox) {
	   	$(".register-btn").addClass('noActive');
        return; 
   	}
   	$(".register-btn").removeClass('noActive');

})



$("#checkbox").on("click",function(){
	var email = $("#email").val();
	var phone = $("#phone").val();
	var code = $("#code").val();
	var respassword = $("#respassword").val();
	var password = $("#password").val();
	var checkbox = $("#checkbox").prop("checked");
	var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;

   	if (!myreg.test(email) || !(/^1[3456789]\d{9}$/.test(phone)) || code == "" || respassword == "" || password == "" || !checkbox) {
	   	$(".register-btn").addClass('noActive');
        return; 
   	}
   	$(".register-btn").removeClass('noActive');

})


$(".sendCode").on("touchend",function(){
	var val = $(".sendCode").html();
	if(val != "发送验证码") {
		return;
	}
	var account = $("#phone").val();
	var event = "register";
	if(!(/^1[3456789]\d{9}$/.test(account))){ 
		layer.msg("手机号格式错误");
        return; 
    } 
	if(account && account != "" ){
		_request.sendCode(account, event,function(res){
			console.log(res);
			// 注册验证码回调函数
				if(res.msg){
					layer.msg(res.msg)
				}
				if(res.code == 1){
					codeTimer();
				}
		})
	}else{
		layer.msg("手机号不能为空");
	}
	
})




function codeTimer (){
	_code_time--;
	console.log(_code_time);
	if(_code_time < 1 ){
		$('.sendCode').html('发送验证码');
		_code_time = code_time;
		return;	
	} 
	$('.sendCode').html(_code_time);

	setTimeout(function(){
		codeTimer();
	}, 1000)
}


window.onload = function(){
	_request.checkToken(function(res){
		if(res.code != 1){
			window.location.href = loginUrl;
		}else{
			window.location.href = loginIndex;
			/*var usreInfo = localStorage.getItem("wx_user");
			if(!usreInfo && usreInfo == null){
				window.localStorage.setItem("wx_user","NoLoad");
				window.location.href = loginUrl;
				return;
			}
			if(userinfo != "NoLoad" && userinfo != undefined){
				var userinfo = JSON.parse(usreInfo);
				var end_time = userinfo.expiretime + userinfo.expires_in;
				//登录过去期判断
				if(userinfo.expiretime > end_time){
					window.location.href = loginUrl;
				}
			}*/
		}
	});


	

}

function setCookie(name, value) { 
    var exp = new Date(); 
    exp.setTime(exp.getTime() + 60 * 60 * 1000); 
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";path=/"; 
}

function getCookie(objName) {//获取指定名称的cookie的值
    var arrStr = document.cookie.split("; ");
    for (var i = 0; i < arrStr.length; i++) {
        var temp = arrStr[i].split("=");
        if (temp[0] == objName) return unescape(temp[1]);  //解码
    }
    return "";
}