function sendCodeBindClick(ajaxUrl, validate) {
    $('#send').bind('click', {ajaxUrl: ajaxUrl, validate: validate}, clickSendCode);
}

function clickSendCode(event) {
    var ajaxUrl = event.data.ajaxUrl;
    var validate = event.data.validate;
    var phone = $('#phone').val().trim();
    if (phone.length == 11) {
        var self = $(this);
        self.unbind('click');
        ajaxSendCode(ajaxUrl, self, phone, validate);
    } else {
        $('#notice').html('<span>请输入11位手机号</span>');
        $('#myModal').modal('show');
    }
}

function countDown(ajaxUrl, self, validate) {
    var second = 60;
    self.text(second + '秒后重发');
    var interval = setInterval(function () {
        second--;
        self.text(second + '秒后重发');
        if (second == 0) {
            window.clearInterval(interval);
            self.text('发送验证码');
            self.bind('click', {ajaxUrl: ajaxUrl, validate: validate}, clickSendCode);
        }
    }, 1000);
}

function ajaxSendCode(ajaxUrl, self, phone, validate) {
    var notice = $('#notice');
    var myModal = $('#myModal');
    $.ajax({
        type: 'POST',
        url: ajaxUrl + '/wp-admin/admin-ajax.php/',
        data: 'action=send_code&phone=' + phone + '&validate=' + validate,
        success: function (data) {
            switch (data.code) {
                case 2000:
                    countDown(ajaxUrl, self, validate);
                    break;
                case 2001:
                    self.bind('click', {ajaxUrl: ajaxUrl, validate: validate}, clickSendCode);
                    notice.html('<span>发送验证码失败</span>');
                    myModal.modal('show');
                    break;
                case 2002:
                    notice.html('<span>请输入有效的手机号</span>');
                    myModal.modal('show');
                    self.bind('click', {ajaxUrl: ajaxUrl, validate: validate}, clickSendCode);
                    break;
                default:
                    break;
            }
        }
    });
}

Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S": this.getMilliseconds()
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};