﻿var lblTryTip  = null;
var lblTrySucessTip  = null;
var lblTryError = null;
var txtNickName = null; // 姓名输入框
var txtTryMobile = null; // 手机号码输入框
var txtTryEmail = null; // 手机号码输入框
var txtMobileCode = null; // 手机验证码输入框
var btnSendMobile = null; // 发送验证码按钮
var btnTry = null; // 发送验证码按钮

$(function () {
    lblTryTip = $("#lblTryTip");
    lblTrySucessTip = $("#lblTrySucessTip");
    lblTryError = $("#lblTryError");
    txtNickName = $('#txtNickName');
    txtTryMobile = $('#txtTryMobile');
    txtTryEmail = $('#txtTryEmail');
    txtMobileCode = $('#txtMobileCode');
    btnSendMobile = $('#btnSendMobile');
    btnTry = $('#btnTry');

    btnSendMobile.click(function () {
        sendMobileCode();
    });
    btnTry.click(function () {
        freeTry();
    });
});

/**
/* 检查手机号码是否已注册过
**/
function isMobileExist() {
    var mobile = txtTryMobile.val();

    if (mobile == '') {
        return false;
    }
    if (mobile != '') {
        if (!checkMobile(mobile)) {
            pf_error('txtTryMobile', '请输入有效的手机号码');
            return false;
        }
    }

    clear_error('txtTryMobile');
    lblTryError.hide();

    disabledBtn('btnSendMobile');
    $.get("/pufang/MobileCode.aspx?action=check&mobile=" + mobile + '&v=' + new Date().getTime(), function (result) {
        if (result != null && result.Status == 1 && mobileIntervalCount == 0) {
            enableBtn('btnSendMobile');
        }
        else {
            pf_error('txtTryMobile', result.Message);
        }

    }, "json");
}

/**
/* 发送验证码
**/
function sendMobileCode() {
    var mobile = txtTryMobile.val();
    var sex = $("input[name='sex']:checked").val();
    var nickName = txtNickName.val();

    sex = encodeURI(sex);
    nickName = encodeURI(nickName);

    if (btnSendMobile.attr('disabled')) {
        return;
    }

    if (nickName == '') {
        pf_error('txtNickName', '请您输入姓名');
        return false;
    }
    if (mobile == '') {
        pf_error('txtTryMobile', '手机号码不能为空');
        return false;
    }
    if (mobile != '') {
        if (!checkMobile(mobile)) {
            pf_error('txtTryMobile', '请输入有效的手机号码');
            return false;
        }
    }

    clear_error('txtTryMobile');
    lblTryError.hide();

    mobileInterval();
    $.get("/pufang/MobileCode.aspx?action=send&nickName=" + nickName + "&sex=" + sex + "&mobile=" + mobile + '&v=' + new Date().getTime(), function (result) {
        if (result != null && result.Status == 1) {
            lblTryTip.text("验证码已发送，请及时查收输入，5分钟内有效");
        }
        else if (result != null) {
            lblTryError.text(result.Message);
            lblTryError.show();
            lblTryTip.hide();
            clearInterval(mobileIntervalId);
            mobileIntervalId = undefined;
            enableBtn('btnSendMobile', '获取验证码');
        }

    }, "json");
}

/**
/* 确认体验
**/
function freeTry() {
    var mobile = txtTryMobile.val();
    var email = txtTryEmail.val();
    var mobileCode = txtMobileCode.val();
    var sex = $("input[name='sex']:checked").val();
    var nickName = txtNickName.val();

    sex = encodeURI(sex);
    nickName = encodeURI(nickName);

    if (btnTry.attr('disabled')) {
        return;
    }
    if (nickName == '') {
        pf_error('txtNickName', '请您输入姓名');
        return false;
    }
    if (mobile == '') {
        pf_error('txtTryMobile', '请您输入手机号');
        return false;
    }
    if (mobile != '') {
        if (!checkMobile(mobile)) {
            pf_error('txtTryMobile', '请您输入有效的手机号码');
            return false;
        }
    }
    if (email != '') {
        if (!checkEmail(email)) {
            pf_error('txtTryEamil', '请您输入有效的邮箱');
            return false;
        }
    }
    if (mobileCode == '') {
        pf_error('txtMobileCode', '请您输入验证码');
        return false;
    }
    if (mobileCode != '') {
        if (!checkCode(mobileCode)) {
            pf_error('txtMobileCode', '请您输入6位数的验证码');
            return false;
        }
    }

    clear_error('txtNickName', 'txtTryMobile', 'txtMobileCode');
    lblTryError.hide();
    disabledBtn('btnTry');

    $.get("/pufang/MobileCode.aspx?action=freeTry&nickName=" + nickName + "&sex=" + sex + "&mobile=" + mobile + "&email=" + email + "&code=" + mobileCode + "&v=" + new Date().getTime(), function (result) {
        btnTry.attr("disabled", true);
        btnTry.css({ 'background': 'gray' });

        if (result != null && result.Status == -1) {
            lblTryError.text(result.Message);
            lblTryError.show();
            lblTryTip.hide();
            enableBtn('btnTry');
        }
        else if (result != null && result.Status == 1) {
            location.href = "/pufang/t.aspx?mobile=" + result.Data + "&code=" + mobileCode + "&v=" + new Date().getTime();
        }

    }, "json");

}

/**
/* 验证码倒计时
**/
var mobileIntervalId = undefined;
var mobileIntervalCount = 0;
function mobileInterval() {
    btnSendMobile.text('倒计时60秒');
    disabledBtn('btnSendMobile');
    mobileIntervalId = setInterval(function () {
        mobileIntervalCount = mobileIntervalCount + 1;
        btnSendMobile.text('倒计时' + (60 - mobileIntervalCount) + '秒');
        if (mobileIntervalCount == 60 && mobileIntervalId != undefined) {
            clearInterval(mobileIntervalId);
            mobileIntervalCount = 0;
            enableBtn('btnSendMobile', '获取验证码');
        }
    }, 1000);
}

function enableBtn(el, text, background) {
    $('#' + el).removeAttr("disabled");
    $('#' + el).css({ 'background': background || '#3366bf' });
    if (text != undefined && text != null) {
        $('#' + el).text(text);
    }
}

function disabledBtn(el) {
    $('#' + el).attr('disabled', true);
    $('#' + el).css({ 'background': 'gray' });
}