﻿$.validator.setDefaults({
    submitHandler: function() { 
        
        if($("#loading").html()=="")
        {
        	var result = $.ajax({
              url: "GeneralProcedures/RegisterMember.ashx",
              data: "userName="+escape($("#txtUserName").val())+"&passWord="+escape($("#txtPassword").val())+"&email="+escape($("#txtEMail").val()),
              async: false,
              dataType: "json",
              cache: false,
              timeout:5000
             }).responseText; 
            if(result != "0" & result != "1")
            {
                document.location="FirstRegister.aspx";
                return true;
            }
            else if(result == "0")
            {
                dialog.info('Email已经被人注册.');
                $("#txtEMail").val("");
                $("#loading").empty();
                return false;
            }
            else
            {
                dialog.info('该用户名不可用,请更换用户名.');
                $("#txtUserName").val("");
                $("#loading").empty();
                return false;
            }
        }
        else if($("#loading").html()=="正在对用户名进行合法性校验,请稍候...")
        {
            dialog.info("正在对用户名进行合法性校验,请稍候...");
        }
        else
        {
            dialog.info('请重新输入新的用户名.');
            $("#txtUserName").val("");
            $("#loading").empty();
        }
     }
});
$(document).ready(function() {
    
    var v = $("#myForm").validate({

        rules: { 
            txtUserName:{
                required:true,
                minlength:6,
                number: true 
                        },
            txtPassword:{
                required:true,
                minlength:6
                          },
            txtConfirmPassword:{
            required: true,
            equalTo: "#txtPassword"
            },
            txtEMail:
            {
               required: true ,
               email: true
               
               //compare:$("#txtEMail").attr("value")
            },
            txtValidate: {
				    required: true,
				    equal: $("#ValidateKey img:first").attr("value").toLowerCase() 
					
				}
        },
        messages: {
            txtUserName:{required:"请输入用户名",minlength:"长度必须是6至20位",number:"用户名只能由英文字母或数字或下划线组成"},
            txtPassword:{required:"请输入登录密码",minlength:"长度必须是6至20位"},
            txtConfirmPassword:{
            required: "请输入确认密码",
            equalTo: "两次输入的密码不一致"
            
            },
            txtEMail:
            {
                required:"请输入邮箱地址",
                email:"输入的邮箱地址不正确"
               
            },
            txtValidate:{
               required: "<br />请输入验证码",
               equal:"<br />验证码不正确"
               }
        }
    });
    $("#btnReset").click(function() {
        v.resetForm();
       $("#loading").empty();
        $("#tip1").show();
        $("#tip2").show();
        $("#tip3").show();
        $("#tip4").show();
        $("#check1").hide();
        $("#check2").hide();
        $("#check3").hide();
        $("#check4").hide();
    });
    $("#btuSubmit").click(function() {
        $("#tip1").hide();
        $("#tip2").hide();
        $("#tip3").hide();
        $("#tip4").hide();
    });
     $("#txtUserName").blur(function(){
         var numletter=/^\w+$/; 
          if($("#txtUserName").val()==0||$("#txtUserName").attr("value").length<6||!numletter.test($("#txtUserName").attr("value")))
          {return $("#tip1").hide()&&$("#check1").hide()&&$("#loading").html("")}
          else {return $("#tip1").hide()}
        });
        $("#txtPassword").blur(function(){
        if($("#txtPassword").val()==0||$("#txtPassword").attr("value").length<6)
         {return $("#tip2").hide()&&$("#check2").hide()}
         else {$("#check2").show()&&$("#tip2").hide()}
        });
        
    $("#txtConfirmPassword").blur(function(){
         if($("#txtPassword").attr("value")!=$("#txtConfirmPassword").attr("value")||$("#txtConfirmPassword").val()==0)
        {return $("#tip3").hide()&&$("#check3").hide()}
         else {return $("#tip3").hide()&&$("#check3").show()}
        });
        
        $("#txtEMail").blur(function(){
          var email = /^\w+([-+]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/ ;
            if(!email.test($("#txtEMail").attr("value"))||$("#txtEMail").val()==0)
            {return $("#tip4").hide()&&$("#check4").hide()}
            else {return $("#tip4").hide()&&$("#check4").show()}
        });
    $("#txtUserName").blur(function(){
         var numletter=/^\w+$/; 
        if($("#txtUserName").val()!=""&& $("#txtUserName").attr("value").length>5&&numletter.test($("#txtUserName").attr("value")))
        {
            $.ajax({
	                type: "get",
	                url: "GeneralProcedures/ValidateUserExist.ashx",
	                data: "userName="+$("#txtUserName").val(),
	                cache:false,
	                timeout:5000,
	                async:true,
	                beforeSend: function(){
		                $("#loading").html("正在对用户名进行合法性校验,请稍候...");
	                },
	                success: function(data){
	                    if(data=="0")
	                    {
	                        $("#loading").html("该用户名不可用,请更换用户名.");
	                        $("#check1").hide();
	                        
	                    }
	                    else
	                    {
	                        $("#loading").empty();
	                        var numletter=/^\w+$/;
	                        if($("#txtUserName").attr("value").length<6||!numletter.test($("#txtUserName").attr("value")))
	                        {
	                            return $("#tip1").hide()&&$("#check1").hide();
	                        }
	                         else 
	                         {
	                            $("#check1").show()&&$("#tip1").hide();
	                         }
	    
	                    }
	                    
	                },
	                complete: function(){                    
	                },
	                error: function(){
	                    $("#loading").html("服务器没有返回数据,可能服务器忙,请重试");
	                }
            });

        }
    });
});
 $.validator.methods.equal = function(value, element, param) {
		//return value == param;
		return value.toLowerCase() == $("#ValidateKey img:first").attr("value").toLowerCase();
	};
$.validator.methods.compare = function(value){
    var email = /^\w+([-+]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;  
   return email.test(value);
};
