$(function(){
	$("#btnSendCode").attr('style',"cursor:pointer;");
	$("input[name='mobile']").bind("focus change",function(){
		var mobile=$("input[name='mobile']").val();
		if(mobile==''){
			$("#mobile_hc").html('手机号不能为空');return;
		}
		if(!mobile.match(/^0?1[3|4|5|8][0-9]\d{8}$/)){
			$("#mobile_hc").html('手机号格式不正确');
		}else{
			$("#mobile_hc").html('通过验证');
		}
	});
	$("input[name='password']").bind("focus change",function(){
		var password=$("input[name='password']").val();
		if(!password.match(/^[\w]{6,12}$/)){
			$("#password_hc").html('请输入6-16位字符,可包含字母、数字、下划线');
		}else{
			$("#password_hc").html('通过验证');
		}
	});
	$("input[name='repassword']").bind("focus change",function(){
		var password=$("input[name='password']").val();
		if(!password){
			$("#repassword_hc").html('密码不能为空');return;
		}
		var repassword=$("input[name='repassword']").val();
		if(password!=repassword){
			$("#repassword_hc").html('两次密码不一致');
		}else{
			$("#repassword_hc").html('通过验证');
		}
	});
});
function getpassword(){

    var query= new Object();
    query = $("form[name='form_getpwd']").serialize();
    $.ajax({
        type: "POST",
        url: APP+"Home/Login/resetpwd",
        data: query,
        success: function(data){
            if(data.status == 1){
				$("#findpwd").hide();
				$("#repwd").show();
				$("input[name='mid']").val(data.mid);
            }else{
                $.showErr(data.info,0,'hc');
				//$('#verify_img').attr('src',APP+"Login/verify.html#"+Math.random());
            }
        }
    });
}
function resetpwd(){
	var check=check_form();
	if(!check){
		return false;
	}
    var query= new Object();
    query = $("form[name='form_repwd']").serialize();
    $.ajax({
        type: "POST",
        url: APP+"Home/Login/resetpwd",
        data: query,
        success: function(data){
            if(data.status == 1){
               $.showSuccess(data.info,0);
			   if(data.url){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+"Home/Ajax/sendsms";
    var mobile=$('#phone').val();
    var unique=$('#unique').val();
    if(verify_mobile(mobile)==false) return false;
    var param="mobile=" + mobile + "&type=" + 2;
        param += "&unique=" + unique;
    var sms_reg_verify=$('#sms_reg_verify').val();
    if(sms_reg_verify) {
        var regverify = $('#regverify').val();
        param += "&regverify=" + regverify;
    }
    $.ajax({
        type: "POST",
        dataType: "JSON",
        url: url,
        data:param,
        success:function (data){
        if(data.status==0){
            $.showErr(data.msg,0,'hc');
            //$('#changevalidate').find('img').attr('src',$('#changevalidate').find('img').attr('src')+'&time='+Math.random())
            return false;
        }else{
            $("#btnSendCode").removeAttr('onclick');
			$("#btnSendCode").attr('style',"cursor:not-allowed");
            $("#btnSendCode").html(curCount + "s后重新获取");
            InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次
        }
    }
});
}
//timer处理函数
function SetRemainTime() {
    if (curCount == 0) {
        window.clearInterval(InterValObj);//停止计时器
		$("#btnSendCode").attr('style',"cursor:pointer;");
		$("#btnSendCode").attr('onclick',"return sendMessage()");
        $("#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|8][0-9]\d{8}$/)){
        $.showErr('手机号格式不正确',0,'hc');
        return false;
    }
}
function check_form(){
	var password=$("input[name='password']").val();
	if(!password.match(/^[\w]{6,12}$/)){$.showErr('密码格式不正确',0,'hc');return false;}
	var repassword=$("input[name='repassword']").val();
	if(password!=repassword){$.showErr('两次密码不一致',0,'hc');return false;}
	return true;
}