﻿
$().ready(function() {


$("#btnCanncel").click(function(){
    if(confirm("您确定要重置么？")){
        $("form:first")[0].reset();
     }
    
});

$("#txtCode").keydown(function(e){
     e = e || window.event;
     
      if (e.keyCode == 13) {
        $("#btnSubmit").click();
      
      }

})

    $("#txtCode").click(function() {

    $("#imgVerify").show();
    });
    $("#txtCode").focus(function() {

         $("#imgVerify").show();
    });


$("#txtUserName").focus();


    $("#btnSubmit").click(function() {
    
      
        if ($("#commentForm").validate().form()) {
        } else {
         //alert("验证未通过");
            return false;
        }
        //alert("验证通过");

        var userName = $.trim($("#txtUserName").val());
        
        var userPassword = $.trim($("#txtUserPassword").val());
        var code = $("#txtCode").val();
         $("#btnSubmit").attr("disabled", "disabled");
        $.ajax({
            type: "GET",
            cache: false,
            //contentType: "application/text",
            dataType: "json",
            url: root + "Server/Login.ashx",
            data: { userName: userName, userPassword: userPassword, code: code,dt:Date()},
            success: function(msg) {
               
                var isSuccess = true;
                var senderId="";
                 $("#btnSubmit").attr("disabled", false);
                if (msg != null && msg != "" && msg != "null") {

                    var _content = $("#msgContent");
                    msg = eval(msg);
                   
                    
                    if (msg.length > 0) {
                    if(msg[0].sId!=undefined)
                    { 
                          senderId=msg[0].sId;
                         //alert(senderId);
                     }
                        if (msg[0].success != null && msg[0].success != undefined) {
                            
                            
                            if (msg[0].success == false) {
                                isSuccess = false;
                                var switchCase = msg[0].errorCode;
                                if (msg[0].msg != undefined && msg[0].msg != "") {
                                    //alert("msg[0].msg="+msg[0].msg);
                                    alert(GetText(HTMLDecode(msg[0].msg)));
                                    
                                    $("#imgVerify").click();
                                }
                                 
                                
                                switch (switchCase) {
                                    case 5:
                                        //用户名不存在
                                        $("#txtUserName").focus();
                                        break;
                                    case 6:
                                        //密码不正确
                                        $("#txtUserPassword").focus();
                                        break;
                                    case 7:
                                        //验证码不正确
                                        $("#txtCode").focus();
                                        break;
                                    default:
                                        break;
                                }
                                
                                $("#btnSubmit").attr("disabled", false);
                                return false;
                            } 
                        }
                    }

                    //登录成功

                    //top.location.href = root + "default.htm";

                }
                if (isSuccess) {
                    // top.location.href = root + "Server.htm?uId="+senderId;
                    
                     top.location.href = root + "users/main.html";
                } 
            }
        });


    });
    $("#btnSubmit").validate();
    // validate signup form on keyup and submit
    $("#commentForm").validate({
        rules: {

            txtUserName: {
                required: true,
                stringCheck: true,
                byteRangeLength: [3, 15]
                //                ,
                //                remote: "/web/HandlerCheckUser.ashx"
            },
            txtUserPassword: {
                required: true,
                minlength: 6
            },
            txtCode: {
                required: true,
                remote: root+"server/CheckCode.ashx"
            }
        },
        messages: {
            txtUserName: {
                required: "* 用户名不能为空",
                minlength: " × 用户名长度应不小于2个字符",
                stringCheck: " × 用户名只能包括中文字、英文字母、数字和下划线",
                //remote: "当前用户名已经存在",
                byteRangeLength: " × 用户名必须在3-15个字符之间(一个中文字算2个字符)"
            },
            txtUserPassword: {
                required: "* 密码不能为空",
                minlength: " × 密码由6—20英文字母（区分大小写）或数字组成"
            }
             ,
            txtCode: {
                required: "* 验证码不能为空"
                ,
                remote: " × 验证码输入不正确"
            }
        }
    });

});

