$(function(){
	$("#login").bind('click',function(){
		do_login();
	});
    $("#btnSendCode").bind('click',function(){
		sendMessage();
	});
});
document.onkeydown=function(event){
    var e = event || window.event || arguments.callee.caller.arguments[0];
    if (e && e.keyCode == 13) {
        do_login();
    } 
}
function do_login(){
	var mobile = $("#mobile").val();
    var verify = $("#verify").val();
	if(!verify){$.showErr('短信验证码不能为空',0,'hc');return false;}
	var password = $("#password").val();
	if(!mobile){
		$.showErr('请输入手机号',0,'hc');
		return false;
	}
	var query = new Object();
	query.mobile = mobile;
	query.verify = verify;
	query.unverify=1;
	$.ajax({
		url:APP+'mobile/Login/dologin',
		data:query,
		dataType:'json',
		success:function(data){
			if(data.status==1){
				$.showSuccess(data.info,0,'hc');
				window.location.href = data.url;
			}else{
                $.showErr(data.info,0,'hc');
            }
		}
	});
}
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 ;
    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;
    }
    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][0-9]\d{8}$/)){
        $.showErr('手机号格式不正确',0,'hc');
        return false;
    }
    return true;
}