$.extend(validatePrompt, {
    username: {
        onFocus: "",
        succeed: "",
        isNull: "请输入邮箱/用户名/已验证手机",
        error: "不存在此用户名"
    }
});
$.extend(validateFunction, {
    username: function (option) {
        validateSettings.succeed.run(option);
    },
    pwd: function (option) {
        validateSettings.succeed.run(option);
    },

    FORM_validate: function () {
        if($("#chkOpenCtrl").attr("checked")==true){
            var srcValue = $("#JD_Verification1").attr("src");
            if (!srcValue) {
                srcValue = $("#JD_Verification1").attr("src2");
            }
            var uuid = srcValue.split("&uid=")[1].split("&")[0];
            $.ajax({
                url: "../uc/srand?r="+Math.random() + "&uuid=" + uuid,
                type: "GET",
                async: false,
                success: function(result){
                    if(result){
                        var obj = eval(result);
                        if (obj.failure) {
                            $("#loginpwd_error").html("页面异常，请刷新后重新提交").show().attr({ "class": "error" });
                            return false;
                        }
                        pgeditor.pwdSetSk(obj.info);
                    }
                }
            });
            $("#loginpwd").val(pgeditor.pwdResult());
            try{
                $("#machineNet").val(pgeditor.machineNetwork());
                $("#machineCpu").val(pgeditor.machineCPU());
                $("#machineDisk").val(pgeditor.machineDisk());
            }catch(e){}
        } else {
            $("#loginpwd").val($("#nloginpwd").val());
        }
        $("#loginname").jdValidate(validatePrompt.username, validateFunction.username, true);
        $("#loginpwd").jdValidate(validatePrompt.pwd, validateFunction.pwd, true);
        return validateFunction.FORM_submit(["#loginname", "#loginpwd"]);
    }
});
setTimeout(function () {
    if (!$("#loginname").val()) {
        $("#loginname").get(0).focus();
    }
}, 0);

$("#loginname").jdValidate(validatePrompt.username, validateFunction.username);
$("#loginpwd").jdValidate(validatePrompt.empty, validateFunction.pwd);
$("#nloginpwd").jdValidate(validatePrompt.empty, validateFunction.pwd);
$("#authcode").jdValidate(validatePrompt.empty, validateFunction.authcode);
function verc() {
    $("#JD_Verification1").click();
}

$("#loginsubmit").click(function () {
    var loginUrl = "../uc/loginService";
    var flag = validateFunction.FORM_validate();
    if (flag) {
        var uuid = $("#uuid").val();
        $(this).attr({ "disabled": "disabled" });
        $.ajax({
            type: "POST",
            url: loginUrl + "?uuid=" + uuid + "&" + location.search.substring(1) + "&r=" + Math.random(),
            contentType: "application/x-www-form-urlencoded; charset=utf-8",
            data: $("#formlogin").serialize(),
            error: function () {
                $("#nloginpwd").attr({ "class": "text highlight2" });
                $("#loginpwd_error").html("网络超时，请稍后再试").show().attr({ "class": "error" });
                $("#loginsubmit").removeAttr("disabled");
                $this.removeAttr("disabled");
            },
            success: function (result) {
                if (result) {
                    var obj = eval(result);
                    if (obj.success) {
                        window.location = obj.success;
                        return;
                    }
                    if (obj.transfer) {
                        window.location = obj.transfer + window.location.search;
                        return;
                    }
                    if (obj.resetpwd) {
                        window.location = "http://safe.jd.com/resetPwd/reset.action?username=" + obj.resetpwd;
                        return;
                    }
                    $("#loginsubmit").removeAttr("disabled");
                    verc();

                    if (obj.verifycode || obj.authcode1 || obj.authcode2) {
                        $("#bgDiv").removeClass().addClass("mc lheight");
                        $("#o-authcode").show();
                    }
                    if (obj.authcode2) {
                        $("#loginname").attr({ "class": "text highlight2" });
                        $("#loginname_error").html("您的账号有安全隐患，建议您登录后修改为复杂密码").show().attr({ "class": "message" });
                    }
                    if (obj.username) {
                        $("#loginname").attr({ "class": "text highlight2" });
                        $("#loginname_error").html(obj.username).show().attr({ "class": "error" });
                    }
                    if (obj.pwd) {
                        $("#nloginpwd").attr({ "class": "text highlight2" });
                        $("#loginpwd_error").html(obj.pwd).show().attr({ "class": "error" });
                    }
                    if (obj.emptyAuthcode) {
                        $("#bgDiv").removeClass().addClass("mc lheight");
                        $("#o-authcode").show();
                        $("#authcode").attr({ "class": "text text-1 highlight2" });
                        $("#authcode_error").html(obj.emptyAuthcode).show().attr({ "class": "error" });
                    }
                }
            }
        });
    }
});

function loginNameOk() {
    var loginName = $("#loginname").val();
    if (validateRules.isNull(loginName) || loginName == '用户名/邮箱/已验证手机') {
        $("#loginname").attr({ "class": "text highlight2" });
        $("#loginname_error").html("请输入用户名/邮箱/已验证手机").show().attr({ "class": "error" });
        return false;
    }

    return true;
}

$("#loginsubmitframe").click(function () {
    var flag = validateFunction.FORM_validate();
    if (flag) {
        var srcValue = $("#JD_Verification1").attr("src");
        if (!srcValue) {
            srcValue = $("#JD_Verification1").attr("src2");
        }
        var uuid = srcValue.split("&uid=")[1].split("&")[0];
        $(this).attr({ "disabled": "disabled" });
        $.ajax({
            type: "POST",
            url: "../uc/loginService?nr=1&uuid=" + uuid + "&" + location.search.substring(1) + "&r=" + Math.random(),
            contentType: "application/x-www-form-urlencoded; charset=utf-8",
            data: $("#formloginframe").serialize(),
            error: function () {
                $("#nloginpwd").attr({ "class": "text highlight2" });
                $("#loginpwd_error").html("网络超时，请稍后再试").show().attr({ "class": "error" });
                $("#loginsubmitframe").removeAttr("disabled");
            },
            success: function (result) {
                if (result) {
                    var obj = eval(result);
                    if (obj.success || obj.transfer) {

                        if (obj.notnr) {
                            window.parent.jdModelCallCenter.init();
                            return;
                        }

                        try {
                            $.ajax({
                                type: "GET",
                                url: obj.success,
                                dataType: "jsonp",
                                timeout: 1000,
                                success: function (result) {
                                    window.parent.jdModelCallCenter.init();
                                    return;
                                }
                            });
                        } catch (e) {
                            window.parent.jdModelCallCenter.init();
                            return;
                        }
                    }

                    if (obj.resetpwd) {
                        window.parent.location = "http://safe.jd.com/resetPwd/reset.action?username=" + obj.resetpwd;
                        return;
                    }
                    $("#loginsubmitframe").removeAttr("disabled");
                    verc();

                    if (obj.verifycode || obj.authcode1 || obj.authcode2) {
                        $("#o-authcode").show();
                    }
                    if (obj.authcode2) {
                        $("#loginname").attr({ "class": "text highlight2" });
                        $("#loginname_error").html("您的账号有安全隐患，建议您登录后修改为复杂密码").show().attr({ "class": "message" });
                    }
                    if (obj.username) {
                        $("#loginname").attr({ "class": "text highlight2" });
                        $("#loginname_error").html(obj.username).show().attr({ "class": "error" });
                    }
                    if (obj.pwd) {
                        $("#nloginpwd").attr({ "class": "text highlight2" });
                        $("#loginpwd_error").html(obj.pwd).show().attr({ "class": "error" });
                    }
                    if (obj.emptyAuthcode) {
                        $("#authcode").attr({ "class": "text text-1 highlight2" });
                        $("#authcode_error").html(obj.emptyAuthcode).show().attr({ "class": "error" });
                    }
                }
            }
        });
    }
});
$("#loginname,#nloginpwd, #authcode").bind('keyup', function (event) {
    if (event.keyCode == 13) {
        $("#loginsubmitframe").click();
    }
});
var authcodeShowed = false;
function preCheck() {
    var pin = $("#loginname").val();
    if (pin != null && pin.length > 0 && pin != '用户名/邮箱/已验证手机') {
        var url = "../user/preCheck.action?pin=" + escape(pin) + "&r=" + Math.random();
        $.getJSON(url, function (result) {
            if (result) {
                var obj = eval(result);
                if (obj.authcode1 || obj.authcode2) {
                    if (!authcodeShowed) {
                        verc();
                        $("#o-authcode").show();
                        authcodeShowed = true;
                    }
                }
                if (obj.authcode2) {
                    //$("#loginname").attr({ "class":"text highlight2" });
                    $("#loginname_error").html("您的账号有安全隐患，建议您登录后修改为复杂密码").show().attr({ "class": "message" });
                }
            }
        })
    }
}

function showBox() {
    if(pgeditor.getLinkHtml()=="nonsupport"){
        jQuery.jdThickBox({
            type:"text",
            title:"提示",
            width:350,
            height:90,
            source:"<div class=\"t-error\"><div class=\"mc\"><i></i><strong>抱歉，本控件仅支持window系统。</strong><span>用户点击“确定”可以关闭弹层。</span><div class=\"clr\"></div><div class=\"btn\" onclick='jdThickBoxclose();'>确定</div></div>",
            _autoReposi:true
        });
        return;
    }
    var dwl = "<a href=\"http://static.360buyimg.com/securityctl/"+pgeditor.getLinkHtml()+"\">立即下载</a>";
    jQuery.jdThickBox({
        type:"text",
        title:"提示",
        width:350,
        height:130,
        source:"<div class=\"step step-0\"><div class=\"mc\"><p>安全控件可提高账户安全性，加密保护您的密码。<a href=\"#help/question-61.html\" style=\"color:#005EA7;cursor:pointer\" target=\"_blank\">了解更多&gt;</a></p><ul><li>立即下载</li><li>手动安装</li><li>刷新页面，继续购物</li></ul><div class=\"btn\">"+dwl+"</div></div></div>",
        _autoReposi:true
    });
}

function inputSelect(){
    if(!pgeditor.checkInstall()){
        showBox();
    } else {
        if($("#chkOpenCtrl").attr("checked")){
             $("#nloginpwd").hide();
             $("#sloginpwd").show();
             $("#nloginpwd").val("");
        } else {
             $("#sloginpwd").hide();
             $("#nloginpwd").show();
             pgeditor.pwdclear();
        }
    }
}

function updateCtl(){
    $("#sloginpwd").hide();
    $("#nloginpwd").show();
    showBox();
}

$("#nloginpwd,#_ocx_password").bind('focus',function(){
    $("#loginpwd_error").empty();
    $("#loginpwd_error").removeClass().addClass("hide");
});