$(document).ready(function () {

    //检查帐号是否存在
    $.validator.addMethod(
                "checkuser",
                function (value, element) {
                    bResult = false;
                    //输出-1=为空，-2格式不正确，1存在，0不存在
                    $.ajax({
                        url: encodeURI('/ajax/RegisterHandler.ashx?type=1&value=' + value),
                        type: 'GET',
                        dataType: 'text',
                        cache: false,
                        async: false,
                        timeout: 3000,
                        error: function () {
                            alert("调用服务器出错！");
                            bResult = false;
                        },
                        success: function (result) {
                            switch (result) {
                                case "-1":
                                    bResult = false;
                                    break;
                                case "-2":
                                    bResult = false;
                                    break;
                                case "1":
                                    bResult = true;
                                    break;
                                case "0":
                                    bResult = false;
                                    break;
                            }

                        }
                    });

                    return bResult;
                },
                '此帐号不存在，请重新输入'
            );

    //检查验证码是否正确
    $.validator.addMethod(
                "checkcode",
                function (value, element) {
                    bResult = false;
                    //输出-1=不正确，0=正确
                    $.ajax({
                        url: encodeURI('/ajax/RegisterHandler.ashx?type=3&value=' + value),
                        type: 'GET',
                        dataType: 'text',
                        cache: false,
                        async: false,
                        timeout: 3000,
                        error: function () {
                            alert("调用服务器出错！");
                            bResult = false;
                        },
                        success: function (result) {
                            switch (result) {
                                case "-1":
                                    bResult = false;
                                    break;
                                case "0":
                                    bResult = true;
                                    break;
                            }

                        }
                    });

                    return bResult;
                },
                '验证码不正确，请重新输入'
            );

    $("#form1").validate({
        rules: {
            LoginName: {
                required: true,
                checkuser: true
            },
            Email: {
                required: true,
                email: true
            },
            CheckCode: {
                minlength: 4,
                required: true,
                checkcode: true
            }
        },
        messages: {
            LoginName: {
                required: "请输入帐号"
            },
            Email: {
                required: "请输入Email",
                email: "Email格式不正确"
            },
            CheckCode: {
                minlength: "必须为4位",
                required: "请输入验证码"
            }
        },
        errorElement: "em",
        success: function (label) {
            label.text("OK!").addClass("success");
        }
    });


});

function reloadCode() {
    document.getElementById("imgCode").src = "CheckCode.aspx?ts=" + Math.random().toString(16);
}


