function AddFile(path, tp) {
    if (tp == "js") {
        var fileref = document.createElement('script');
        fileref.setAttribute("type", "text/javascript");
        fileref.setAttribute("src", path);
        document.getElementsByTagName("head")[0].appendChild(fileref);
    }
    if (tp == "css") {
        var fileref = document.createElement('link');
        fileref.setAttribute("type", "text/css");
        fileref.setAttribute("href", path);
        fileref.setAttribute("rel", "stylesheet");
        document.getElementsByTagName("head")[0].appendChild(fileref);
    }
}

function ShowMsg(msg) {
    alert(msg);
}
function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=")
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) {
                c_end = document.cookie.length;
            }
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return null
}
function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}
function delCookie(c_name) { setCookie(c_name, "", -1); }

var phone_getcode_default_msg = "";
window.onload = function () {
    if (typeof $ != 'function') { AddFile("/api/script.ashx?script.wjq.js", "js") }
    phone_getcode_default_msg = $(".phone_getcode").val();

    /***  页面被刷新   ********/
    var cookie_code = getCookie("scoolie_phone");
    var scoolie_wait = getCookie("scoolie_wait");
    if (cookie_code != null && scoolie_wait != null && scoolie_wait != "end") {
        if (cookie_code > 1) {
            CallSuccecc();
        }
    }

    $(".phone_getcode").click(GetCode)
    $(".phone_submit").click(BindPhone)
    /**  解绑  **/
    $(".unphone_getcode").click(GetUNCode)
    $(".unphone_submit").click(UnBindPhone)
}

function GetCode() {
    re = /^[1]{1}[1-9]{1}\d{9}$/;
    re.multiline = false;
    var phone_numbe = $(".phone_phone").val();
    if (!re.test(phone_numbe)) {
        ShowMsg("手机号码不符合规则！");
        return;
    }
    $(".phone_getcode").attr("disabled", "disabled").css({ "cursor": "default" }).val("验证码发送中...");

    $.ajax({
        url: "/api/sendphonecode.ashx?f=" + Math.random(),
        data: { "phone": phone_numbe },
        error: function () { $(".phone_getcode").removeAttr("disabled").css({ "cursor": "pointer" }).val(phone_getcode_default_msg); ShowMsg("发送错误，请刷新页面后重试！"); },
        success: function (call) {
            if (call == "200" || call == 200) {
                ShowMsg("验证码已经发送到您的手机，请注意查收！");
                CallSuccecc();
            } else {
                ShowMsg(call);
                $(".phone_getcode").removeAttr("disabled").css({ "cursor": "pointer" }).val(phone_getcode_default_msg);
                return;
            }
        }
    })
}

function CallSuccecc() {
    if (typeof default_wait == "undefined") { default_wait = 10; }

    var on_use = default_wait;
    var cookie_code = getCookie("scoolie_phone")
    if (cookie_code != null) {
        on_use = cookie_code;
    }
    var IntervalIDx = setInterval(function () {
        on_use = on_use - 1;
        if (on_use <= 0) {
            clearInterval(IntervalIDx);
            $(".phone_getcode").removeAttr("disabled").css({ "cursor": "pointer" }).val(phone_getcode_default_msg);
            setCookie("scoolie_phone", default_wait, "10");
            setCookie("scoolie_wait", "end", "10");
            return;
        }
        setCookie("scoolie_wait", "wait", "10");
        setCookie("scoolie_phone", on_use, "10");
        $(".phone_getcode").attr("disabled", "disabled").css({ "cursor": "default" }).val(on_use + "秒后重发");

    }, 1000);
}


function BindPhone() {
    var code_user_input = $(".phone_code").val();
    $.ajax({
        url: "/api/webaction.ashx?t=" + Math.random(),
        data: { "coder": code_user_input, "posttype": "userbindphone" },
        error: function () { },
        success: function (msg) {
            if (msg == "200") {
                ShowMsg("恭喜您！绑定安全手机成功！");
                window.location.reload();
            } else {
                ShowMsg(msg);
            }
        }
    })
}

/********   解绑手机    *******************************************************************************************/
function UnBindPhone() {
    var code_user_input = $(".unphone_code").val();
    if (code_user_input.length != 6) {
        ShowMsg("验证码长度错误");
        return;
    }
    re = /^\d{6}$/;
    if (!re.test(code_user_input)) {
        ShowMsg("验证码格式错误");
        return;
    }
    $.ajax({
        url: "/api/webaction.ashx?t=" + Math.random(),
        data: { "coder": code_user_input, "posttype": "userbindphoneonun" },
        error: function () { },
        success: function (msg) {
            if (msg == "200") {
                ShowMsg("解绑成功！您现在可以绑定新的手机了！");
                window.location.reload();
            } else {
                ShowMsg(msg);
            }
        }
    })
}
function GetUNCode() {
    $(".unphone_getcode").attr("disabled", "disabled").css({ "cursor": "default" }).val("验证码发送中...");
    $.ajax({
        url: "/api/sendphonecode.ashx?f=" + Math.random(),
        data: { "uncoder": "uncoder" },
        error: function () { $(".unphone_getcode").removeAttr("disabled").css({ "cursor": "pointer" }).val("免费获取验证码"); ShowMsg("发送错误，请刷新页面后重试！"); },
        success: function (call) {
            if (call == "200" || call == 200) {
                ShowMsg("验证码已经发送到您的手机，请注意查收！");
                if (typeof default_wait == "undefined") { default_wait = 10; }
                var on_use = default_wait;
                var cookie_code = getCookie("scoolie_phone")
                if (cookie_code != null) {
                    on_use = cookie_code;
                }
                var IntervalIDx = setInterval(function () {
                    on_use = on_use - 1;
                    if (on_use <= 0) {
                        clearInterval(IntervalIDx);
                        $(".unphone_getcode").removeAttr("disabled").css({ "cursor": "pointer" }).val("免费获取验证码");
                        setCookie("scoolie_phone", default_wait, "10");
                        setCookie("scoolie_wait", "end", "10");
                        return;
                    }
                    setCookie("scoolie_wait", "wait", "10");
                    setCookie("scoolie_phone", on_use, "10");
                    $(".unphone_getcode").attr("disabled", "disabled").css({ "cursor": "default" }).val(on_use + "秒后重发");
                }, 1000);
            } else {
                ShowMsg(call);
                $(".unphone_getcode").removeAttr("disabled").css({ "cursor": "pointer" }).val("免费获取验证码");
                return;
            }
        }
    })


}