$(document).ready(function () {

    //切换省份
    $("#sel_province").change(function () {
        $.ajax({
            url: '/ajax/AreaHandler.ashx?pid=' + $(this).val(),
            type: 'GET',
            dataType: 'text',
            cache: false,
            async: false,
            timeout: 3000,
            error: function () {
                alert("调用服务器出错！");
            },
            success: function (result) {
                switch (result) {
                    case "-1":
                        break;
                    default:
                        FillCity("sel_city", result);
                        $("#sel_area").get(0).options.length = 0;
                        $("#sel_area").prepend("<option value='999'>--请选择--</option>");
                        break;
                }
            }
        });
    });

    $("#sel_city").change(function () {
        $.ajax({
            url: '/ajax/AreaHandler.ashx?pid=' + $(this).val(),
            type: 'GET',
            dataType: 'text',
            cache: false,
            async: false,
            timeout: 3000,
            error: function () {
                alert("调用服务器出错！");
            },
            success: function (result) {
                switch (result) {
                    case "-1":
                        break;
                    default:
                        FillCity("sel_area", result);
                        break;
                }
            }
        });
    });

    function FillCity(selectid, city) {
        $("#" + selectid).get(0).options.length = 0;
        $("#" + selectid).prepend("<option value='999'>--请选择--</option>");   //为Select插入一个Option(第一个位置) 
        var dataObj = eval("(" + city + ")");   //转换为json对象
        $.each(dataObj, function (idx, item) {
            $("#" + selectid).append("<option value='" + item.AreaID + "'>" + item.AreaName + "</option>");
        });
    }

    //修改用户信息
    $("#formUserInfo").validate({
        rules: {
            txtEmail: {
                required: true,
                email: true,
                checkemail: true
            },
            txtQQMsn: {
                required: true
            },
            txtMobile: {
                required: true,
                checkmobile: true
            }
        },
        messages: {
            txtEmail: {
                required: "请输入Email",
                email: "Email格式不正确"
            },
            txtQQMsn: {
                required: "请输入QQ或MSN"
            },
            txtMobile: {
                required: "请输入手机号码"
            }
        },
        submitHandler: function () { //验证通过才提交表单
            art.dialog({
                content: '确定要修改吗？',
                ok: function () {
                    var form = $("#formUserInfo");
                    $.post(
                            form.attr("action"),
                            form.serialize(),
                            function (result, status) {
                                art.dialog({
                                    icon: 'succeed',
                                    id: 'succeedArt',
                                    title: '提示',
                                    content: result.Message,
                                    skin: 'default',
                                    ok: function () {
                                        return true;
                                    }
                                });
                            }, "json");
                },
                okVal: '是',
                cancelVal: '否',
                cancel: true
            });
            //            if (confirm("确定要修改吗？")) {
            //                var form = $("#formUserInfo");
            //                $.post(
            //                            form.attr("action"),
            //                            form.serialize(),
            //                            function (result, status) {
            //                                alert(result.Message);
            //                            }, "json");
            //            }
        },
        invalidHandler: function (form, validator) {  //不通过回调
            return false;
        },
        errorElement: "em",
        success: function (label) {
            label.text("OK!").addClass("success");
        }
    });

    //修改密码
    $("#formPwd").validate({
        rules: {
            txtOldPwd: {
                required: true,
                minlength: 6,
                maxlength: 30
            },
            txtNewPwd: {
                required: true,
                minlength: 6,
                maxlength: 30
            },
            txtNewPwd2: {
                required: true,
                minlength: 6,
                maxlength: 30,
                equalTo: "#txtNewPwd"
            }
        },
        messages: {
            txtOldPwd: {
                required: "请输入原始密码",
                minlength: "至少6位",
                maxlength: "不超过16位"
            },
            txtNewPwd: {
                required: "请输入新密码",
                minlength: "至少6位",
                maxlength: "不超过16位"
            },
            txtNewPwd2: {
                required: "请输入确认新密码",
                minlength: "至少6位",
                maxlength: "不超过16位",
                equalTo: "两次密码不一致"
            }
        },
        submitHandler: function () { //验证通过才提交表单
            art.dialog({
                content: '确定要修改吗？',
                ok: function () {
                    var form = $("#formPwd");
                    $.post(
                        form.attr("action"),
                        form.serialize(),
                        function (result, status) {
                            art.dialog({
                                icon: 'succeed',
                                id: 'succeedArt',
                                title: '提示',
                                content: result.Message,
                                skin: 'default',
                                ok: function () {
                                    return true;
                                }
                            });
                        }, "json");
                },
                okVal: '是',
                cancelVal: '否',
                cancel: true
            });
//            if (confirm("确定要修改吗？")) {
//                var form = $("#formPwd");
//                $.post(
//                        form.attr("action"),
//                        form.serialize(),
//                        function (result, status) {
//                            alert(result.Message);
//                        }, "json");
//            }
        },
        invalidHandler: function (form, validator) {  //不通过回调
            return false;
        },
        errorElement: "em",
        success: function (label) {
            label.text("OK!").addClass("success");
        }
    });

    //修改用户帐户信息
    $("#formAccount").validate({
        rules: {
            txtPayeeName: {
                required: true
            },
            txtPayeeIDCode: {
                required: true,
                checkidcode: true
            },
            txtSettleAccount: {
                required: function () {
                    var selID = $("#sel_SettleMethod").val();
                    if (selID == 99) { return false; } else { return true; }
                }
            },
            txtBankAccount: {
                required: function () {
                    var selID = $("#sel_SettleMethod").val();
                    if (selID == 99) { return true; } else { return false; }
                }
            }
        },
        messages: {
            txtPayeeName: {
                required: "请输入收款人姓名"
            },
            txtPayeeIDCode: {
                required: "请输入收款人身份证号码"
            },
            email: {
                required: "请输入帐号"
            },
            txtBankAccount: {
                required: "请输入帐号卡号"
            }
        },
        submitHandler: function () { //验证通过才提交表单
            art.dialog({
                content: '确定要修改吗？',
                ok: function () {
                    var form = $("#formAccount");
                    $.post(
                        form.attr("action"),
                        form.serialize(),
                        function (result, status) {
                            art.dialog({
                                icon: 'succeed',
                                id: 'succeedArt',
                                title: '提示',
                                content: result.Message,
                                skin: 'default',
                                ok: function () {
                                    return true;
                                }
                            });
                        }, "json");
                },
                okVal: '是',
                cancelVal: '否',
                cancel: true
            });
//            if (confirm("确定要修改吗？")) {
//                var form = $("#formAccount");
//                $.post(
//                        form.attr("action"),
//                        form.serialize(),
//                        function (result, status) {
//                            alert(result.Message);
//                        }, "json");
//            }
        },
        invalidHandler: function (form, validator) {  //不通过回调
            return false;
        },
        errorElement: "em",
        success: function (label) {
            label.text("OK!").addClass("success");
        }
    });

    //检查手机格式是否正确
    $.validator.addMethod(
                "checkmobile",
                function (value, element) {
                    var reg = /^(\+86)?(1[0-9]{10})$/;
                    return reg.test(value);
                },
                '手机号码格式不正确，请重新输入'
            );

    //检查Email是否存在
    $.validator.addMethod(
            "checkemail",
            function (value, element) {
                bResult = false;
                //比较是否已修改
                var oldEmail = $.trim($("#hdOldEmail").val());
                var newEmail = $.trim($("#txtEmail").val());
                if (oldEmail == newEmail) return true;

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

                    }
                });

                return !bResult;
            },
            '此Email已被注册使用，请重新输入'
        );

    //检查身份证格式是否正确
    $.validator.addMethod(
                "checkidcode",
                function (value, element) {
                    return checkCodeID(value);
                },
                '身份证格式不正确，请重新输入'
            );

    //身份证验证
    function checkCodeID(sCode) {
        if (sCode.length != 18 && sCode.length != 15) {
            return false;
        }
        for (i = 0; i < sCode.length - 1; i++) {
            if (isNaN(parseInt(sCode.charAt(i)))) {
                return false;
            }
        }

        var lastIDNum;

        if (sCode.length == 18) {
            lastIDNum = sCode.charAt(17);
        }
        else {
            lastIDNum = sCode.charAt(14);
        }
        if (isNaN(parseInt(sCode.charAt(i))) && lastIDNum.toLowerCase() != 'x') {
            return false;
        }
        return true;
    }
});