﻿function pageLoad(){
  //初始化所有select
  $("#netbarreg_defquestion_tr").hide();
  Sys.Net.WebServiceProxy.invoke("Handlers/transformer.aspx","GetQuestions",false,{},function(result){
    $("#netbarreg_question").children().remove();
    $("#netbarreg_question").append("<option value=\"请选择\" selected=\"selected\">请选择</option>");
    $.each(result,function(i,item){
      $("#netbarreg_question").append("<option value='"+item.Value+"'>"+item.Name+"</option>");
    });
  },function(err){
     alert(err.get_message());
  });
  $("#netbarreg_question").bind("change",function (evt){
    if (evt.target.value=="20"){
      $("#netbarreg_defquestion_tr").show();
    }
    else{
      $("#netbarreg_defquestion_tr").hide();
    }
  });
  
  $("#netbarreg_account").bind("focus",function (evt){
    $("#accountNote").toggleClass("note");
  });
  $("#netbarreg_account").bind("blur",function (evt){
    $("#accountNote").toggleClass("note");
    if (isAccountNameValid(evt.target.value)){
      Sys.Net.WebServiceProxy.invoke("Handlers/transformer.aspx","IsUserNameExist",false,{username:evt.target.value},function(result){
         if (result==false){
           dynamicRight ($(evt.target).parent(),"username");
         }
         else{
           dynamicError ($(evt.target).parent(),"username","用户名已存在!");
           dynamicContent($(evt.target).parent(),"username_div_err","对不起,该用户名已经存在!");
         }
       },function(err){
       });
    }
    else{
      dynamicError ($(evt.target).parent(),"username","");
      dynamicContent($(evt.target).parent(),"username_div_err","用户名格式不正确!");
    }
  });
  $("#netbarreg_account").focus();
  $("#netbarreg_password").bind("focus",function (evt){
    $("#passNote").toggleClass("note");
  });
  $("#netbarreg_password").bind("keydown",function (evt){
    checkStrength(evt.target.value);
  });
  $("#netbarreg_password").bind("blur",function (evt){
    $("#passNote").toggleClass("note");
    if (!validatePassword (evt.target.value)){
       dynamicError ($(evt.target).parent(),"password","");
       dynamicContent($(evt.target).parent(),"password_div_err","对不起,密码不符合要求!");
    }
    else{
       dynamicRight ($(evt.target).parent(),"password","");
    }
  });
  $("#netbarreg_confirmpassword").bind("focus",function (evt){
    $("#pass2Note").toggleClass("note");
  });
  $("#netbarreg_confirmpassword").bind("blur",function (evt){
    if(contentIsEmpty (evt.target.value)){
       dynamicError ($(evt.target).parent(),"confirmpassword","");
       dynamicContent($(evt.target).parent(),"confirmpassword_div_err","请输入确认密码!");
    }
    else{
      var orgpass=$("#netbarreg_password").val();
      if (evt.target.value!=orgpass){
       $("#netbarreg_confirmpassword").focus();
        dynamicError ($(evt.target).parent(),"confirmpassword","");
        dynamicContent($(evt.target).parent(),"confirmpassword_div_err","密码不匹配!");
      }
      else
      {
        dynamicRight ($(evt.target).parent(),"confirmpassword","");
      }
    }
  });
  $("#netbarreg_question").bind("focus",function (evt){
    $("#secQuestionsNote2").toggleClass("note");
  });
  $("#netbarreg_question").bind("blur",function (evt){
    $("#secQuestionsNote2").toggleClass("note");
  });
  $("#netbarreg_defquestion").bind("focus",function (evt){
    $("#defQuestionNote").toggleClass("note");
  });
  $("#netbarreg_defquestion").bind("blur",function (evt){
    $("#defQuestionNote").toggleClass("note");
    if ($("#netbarreg_question").val()=="20"){
      if (contentIsEmpty (evt.target.value)){
        dynamicError ($(evt.target).parent(),"defquestion","");
        dynamicContent($(evt.target).parent(),"defquestion_div_err","请输入自定义安全保护问题!");
      }
      else{
        if(!isInScope (5,100,evt.target.value)){
          dynamicError ($(evt.target).parent(),"defquestion","");
          dynamicContent ($(evt.target).parent(),"defquestion_div_err","自定义安全保护问题限制为5-100个字符");
        }
        else{
          dynamicRight ($(evt.target).parent(),"defquestion","");
        }
      }
    }
  });
  $("#netbarreg_answer").bind("focus",function (evt){
     $("#answerNote2").toggleClass("note");
  });
  $("#netbarreg_answer").bind("blur",function (evt){
     $("#answerNote2").toggleClass("note");
     if (!isInScope (5,100,evt.target.value)){
       dynamicError ($(evt.target).parent(),"answer","");
       dynamicContent ($(evt.target).parent(),"answer_div_err","安全问题答案限制为5-100个字符");
     }
     else{
       dynamicRight ($(evt.target).parent(),"answer","");
     }
  });
  $("#netbarreg_name").bind("focus",function (evt){
    $("#nameNote").toggleClass("note");
  });
  $("#netbarreg_name").bind("blur",function (evt){
     $("#nameNote").toggleClass("note");
     if (!isInScope (4,10,evt.target.value)){
       dynamicError ($(evt.target).parent(),"name","");
       dynamicContentShowRight ($(evt.target).parent(),"netbarname_div_err","用户名限制为4-10个字符!");
     }
     else{
       if (!validateUsername (evt.target.value)){
         dynamicError ($(evt.target).parent(),"name","");
         dynamicContentShowRight ($(evt.target).parent(),"netbarname_div_err","中文用户名不能使用空格!");
       }
       else{
         dynamicRight ($(evt.target).parent(),"netbarname","");
       }
     }
  });
  $("#netbarreg_email").bind("focus",function (evt){
     $("#emailNote").toggleClass("note");
  });
  $("#netbarreg_email").bind("blur",function (evt){
    $("#emailNote").toggleClass("note");
    if (!validateEmail (evt.target.value)){
      dynamicError ($("#netbarreg_email").parent(),"email","");
      dynamicContentShowRight ($("#netbarreg_email").parent(),"email_div_err","请输入正确的email地址!");
    }
    else{
      dynamicRight ($(evt.target).parent(),"email","");
    }
  });
  $("#netbarreg_wxaccount").bind("focus",function (evt){
    $("#wxaccountNote").toggleClass("note");
  });
  $("#netbarreg_wxaccount").bind("blur",function (evt){
    $("#wxaccountNote").toggleClass("note");
    if (!isInScope (1,100,evt.target.value)){
      dynamicError ($("#netbarreg_wxaccount").parent(),"wxaccount","");
      dynamicContentShowRight ($("#netbarreg_wxaccount").parent(),"wxaccount_div_err","请输入正确的万象验证账户!");
    }
    else{
      dynamicRight ($("#netbarreg_wxaccount").parent(),"wxaccount","");
    }
  });
  $("#netbarreg_wxpassword").bind("focus",function (evt){
    $("#wxpassNote").toggleClass("note");
  });
  $("#netbarreg_wxpassword").bind("blur",function (evt){
    $("#wxpassNote").toggleClass("note");
    if (!isInScope (1,50,evt.target.value)){
      dynamicError ($("#netbarreg_wxpassword").parent(),"wxaccountpass","");
      dynamicContentShowRight ($("#netbarreg_wxpassword").parent(),"wxaccountpass_div_err","请输入正确的密码1-50个字符!");
    }
    else{
      dynamicRight ($(evt.target).parent(),"wxaccountpass","");
    }
  });
  $("#ctl00_ctl00_cphMain_cphPage_netbarreg_validatecode").bind("focus",function (evt){
    $("#codeNote").toggleClass("note");
  });
  $("#ctl00_ctl00_cphMain_cphPage_netbarreg_validatecode").bind("blur",function (evt){
    $("#codeNote").toggleClass("note");
    Sys.Net.WebServiceProxy.invoke("Handlers/transformer.aspx","ValidateCaptcha",false ,{validatecode:evt.target.value},function (result){
      if (result==false){
        dynamicError ($(evt.target).parent(),"validatecode","");
      }
      else{
        dynamicRight ($(evt.target).parent(),"validatecode","");
      }
    },function (err){});
  });
 // $addHandler($("#ctl00_page_netbarreg_confirm").get(0),"click",confirmRegister);
}

function confirmRegister(){
     var account,password,name,mail,wxaccount,wxpass,question,questionid,answer,confirmpass,validatecode;
     account=$("#netbarreg_account").val();
     password=$("#netbarreg_password").val();
     questionid =$("#netbarreg_question").val();
     question =$("#netbarreg_question :selected").html();
     confirmpass =$("#netbarreg_confirmpassword").val();
     if(questionid =="20"){
       question =$("#netbarreg_defquestion").val();
     }
     answer =$("#netbarreg_answer").val();
     name=$("#netbarreg_name").val();
     mail =$("#netbarreg_email").val();
     wxaccount =$("#netbarreg_wxaccount").val();
     wxpass =$("#netbarreg_wxpassword").val();
     validatecode =$("#ctl00_page_netbarreg_validatecode").val();
     var preperok=false;
     if (isAccountNameValid(account)){
      Sys.Net.WebServiceProxy.invoke("Handlers/transformer.aspx","IsUserNameExist",false,{username:account},function(result){
         if (result==false)
         {
            dynamicRight ($("#netbarreg_account").parent(),"username","");
            if (!validatePassword(password))
            {
               dynamicError ($("#netbarreg_password").parent(),"password","");
               dynamicContent($("#netbarreg_password").parent(),"password_div_err","对不起,密码不符合要求!");
               return false;
            }
            if(contentIsEmpty(confirmpass))
            {
               dynamicError ($("#netbarreg_confirmpassword").parent(),"confirmpassword","");
               dynamicContent($("#netbarreg_confirmpassword").parent(),"confirmpassword_div_err","请输入确认密码!");
               return false;
            }
            if (password !=confirmpass)
            {
                dynamicError ($("#netbarreg_confirmpassword").parent(),"confirmpassword_err","");
                dynamicContent($("#netbarreg_confirmpassword").parent(),"confirmpassword_div_err","密码不匹配!");
                return false;
            }
            if (questionid =="请选择")
            {
              dynamicError ($("#netbarreg_question").parent(),"question","");
              dynamicContentShowRight ($("#netbarreg_question"),"question_div_err","必须选择一个问题,或者设置您自己的密码问题!");
              return false;
            }
            if (questionid =="20") 
            {
              if (contentIsEmpty (question ))
              {
                dynamicError ($("#netbarreg_defquestion").parent(),"defquestion","");
                dynamicContent($("#netbarreg_defquestion").parent(),"defquestion_div_err","请输入自定义安全保护问题!");
                return false;
              }
              else
              {
                if (!isInScope (5,100,question ))
                {
                  dynamicError ($("#netbarreg_defquestion").parent(),"defquestion","");
                  dynamicContent($("#netbarreg_defquestion").parent(),"defquestion_div_err","自定义安全保护问题限制为5-100个字符!");
                  return false;
                }
                
              }
            }
            if (!isInScope (5,100,answer)){
               dynamicError ($("#netbarreg_answer").parent(),"answer","");
               dynamicContent ($("#netbarreg_answer").parent(),"answer_div_err","安全问题答案限制为5-100个字符");
               return false;
             }
            if (!isInScope (4,10,name)){
               dynamicError ($("#netbarreg_name").parent(),"name","");
               dynamicContentShowRight ($("#netbarreg_name").parent(),"netbarname_div_err","用户名限制为4-10个字符!");
               return false;
            }
            else{
               if (!validateUsername (name)){
                 dynamicError ($("#netbarreg_name").parent(),"name","");
                 dynamicContentShowRight ($("#netbarreg_name").parent(),"netbarname_div_err","中文用户名不能使用空格!");
                 return false;
               }
             }
            if (!validateEmail(mail)){
              dynamicError ($("#netbarreg_email").parent(),"email","");
              dynamicContentShowRight ($("#netbarreg_email").parent(),"email_div_err","请输入正确的email地址!");
              return false;
            }
            if (!isInScope (1,100,wxaccount)){
               dynamicError ($("#netbarreg_wxaccount").parent(),"wxaccount","");
               dynamicContentShowRight ($("#netbarreg_wxaccount").parent(),"wxaccount_div_err","请输入正确的万象验证账户!");
               return false;
            }
            if (!isInScope (1,50,wxpass)){
              dynamicError ($("#netbarreg_wxpassword").parent(),"wxaccountpass","");
              dynamicContentShowRight ($("#netbarreg_wxpassword").parent(),"wxaccountpass_div_err","请输入正确的密码1-50个字符!");
              return false;
            }
            Sys.Net.WebServiceProxy.invoke("Handlers/transformer.aspx","ValidateCaptcha",false ,{validatecode:validatecode},function (result){
            //alert(result);
              if (result==false){
                dynamicError ($("#ctl00_page_netbarreg_validatecode").parent(),"validatecode","");
                dynamicContentShowRight ($("#ctl00_page_netbarreg_validatecode").parent(),"validatecode_div_err","请输入正确的验证码!");
                return false;
              }
              else{
                // __doPostBack("ctl00$page$netbarreg_confirm");     
                Sys.Net.WebServiceProxy.invoke("Handlers/transformer.aspx","NetBarUserRegister",false,{account:account,password:password,name:name,mail:mail,wxaccount:wxaccount,wxpass:wxpass,question:question,questionid:questionid,answer:answer},function(result){
                //alert(result);
                  if (result.Status==true){
                    __doPostBack("ctl00$page$netbarreg_confirm");
                    preperok =true;
                  }
                  else{
                    dynamicContentShowRight ($("#ctl00_page_netbarreg_validatecode").parent(),"error",result.Prompt);
                  }
                },function (err){alert(err.get_message());});
              }
            },function (err){alert(err.get_message());});
         }
         else{
           dynamicError ($("#netbarreg_account").parent(),"username","用户名已存在!");
           dynamicContent($("#netbarreg_account").parent(),"username_div_err","对不起,该用户名已经存在!");
           preperok =false ;
         }
       },function(err){
         preperok =false;
       });
    }
    else{
      dynamicError ($("#netbarreg_account").parent(),"username","");
       dynamicContent($("#netbarreg_account").parent(),"username_div_err","用户名格式不正确!");
      preperok =false ;
    }
    return preperok;
}
var mailcount = 0;//发送激活邮件数量
function SendActivationAccountMail(account,password)
{
 if(mailcount <3)
 {
Sys.Net.WebServiceProxy.invoke("Handlers/transformer.aspx","AgainActivationAccount",false,{account:account,password:password},function(result){
         if (result==true){
         mailcount++;
           alert("激活邮件已重新发送。");
         }
         else{
         alert("激活邮件发送失败。");
         }
       },function(err){
       });
       }
       else
       {
        alert("已经连续重复发送3次，请稍后尝试。");
       }
}
function validateTelNumber(value){
   return /^\d{2,4}-\d{7,8}$/.test(value);  
}
function validateUsername(value){
  if(/[\u4e00-\u9fa5]/.test(value)){
    if (/\s/.test(value)){
      return false;
    }
    else{
      return true;
    }
  }
  else{
    return true;
  }
}
function checkIDCardNumber(value){
  return /^\w{4,20}$/.test(value);
}
function validateEmail(value){
  var pattern=/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
  return pattern.test(value);
  
}
function contentIsEmpty(value){
  value=$.trim(value);
  if (value==null || value.length <=0)
    return true;
  return false;
}
function isNumericalNumber(value){
  return /^([0-9])+$/.test(value);
}
function checkIDCardNumber(value){
  return /^\w{4,20}$/.test(value);
}
function dynamicRight(target,id,title){
   var div=document.createElement("div");
   $(div).addClass("dynamicright");
   div.id=id;
   $(div).css({left:$(target).position().left+$(target).width()-20});
   div.title=title;
   if(document.getElementById(div.id)){
     $("#"+div.id).remove();
   }
   $(target).append(div);
}
function dynamicContent(target,id,content){
  var div=document.createElement ("div");
  $(div).addClass("dynamiccontent");
  div.id=id;
  $(div).html(content);
  $(div).css({position:"absolute",top:$(target).position().top+33,left:$(target).position().left+2});
  if(document.getElementById (div.id)){
  }
  else{
    $(target).append(div);
    setTimeout (function(){
       $(div).hide("slow");
       setTimeout (function(){
         $(div).remove();
       },1000);  
    },2000);
  } 
}
function dynamicContentShowRight(target,id,content){
  var div=document.createElement ("div");
  $(div).addClass("dynamiccontent");
  div.id=id;
  $(div).html(content);
  $(div).css({position:"absolute",top:$(target).position().top+3,left:$(target).position().left+170});
  if(document.getElementById (div.id)){
  }
  else{
    $(target).append(div);
    setTimeout (function(){
       $(div).hide("slow");
       setTimeout (function(){
         $(div).remove();
       },2000);  
    },2000);
  } 
}
function validateMobile(value){
  return /^(13|15|18)\d{9}$/.test(value);
}
function isWhitespaceChar(value){
  return /\s+/.test(value);
}
function dynamicError(target,id,title){
   var div=document.createElement("span");
   $(div).addClass("dynamicerror");
   div.id=id;
   div.title=title;
   $(div).css({left:$(target).position().left+$(target).width()-20});
   if(document.getElementById(div.id)){
    $("#"+div.id).remove();
   }
   $(target).append(div);
}
function isInScope (minlength,maxlength,content)
{
    content=$.trim(content);
    if (content ==null || content.length<=0) {
      return false;
    }  
     var icount = content.replace(/[^\x00-\xff]/g,"**").length;
     if(icount>=minlength && icount<=maxlength){
         return true;
     }
     return false;
}
function checkIPAddress(value){
  var ipreg=/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
  return ipreg.test(value);
}
function validatePassword(value){
  if(/[\u4e00-\u9fa5]/.test(value)){
    return false;
  }
  else{
    if (/\s/.test(value)){
      return false;
    }
    if (value.length<6 || value.length>16)
    {
      return false;
    }
    else  
      return true;
  }
}
function isAccountNameValid(name){
    var pattern = /^([a-z0-9_\u4E00-\u9FA5])+$/;
    if(name == null || name == "") return false;
    
    if(getHanLength(name) < 4 || getHanLength(name) > 20) return false;
    
    return pattern.test(name);
}
function getHanLength(str){return str.replace(/[\u4E00-\u9FA5]/g,"**").length;}

// JScript 文件
//CharMode函数
//测试某个字符是属于哪一类.
function CharMode(iN)
{
    if (iN>=48 && iN <=57) //数字
        return 1;
    if (iN>=65 && iN <=90) //大写字母
        return 2;
    if (iN>=97 && iN <=122) //小写
        return 4;
    else
        return 8; //特殊字符
}

//bitTotal函数
//计算出当前密码当中一共有多少种模式
function bitTotal(num)
{
    modes=0;
    for (i=0;i<4;i++)
    {
        if (num & 1) modes++;
        num>>>=1;
    }
    return modes;
}


//checkStrong函数
//返回密码的强度级别

function checkStrong(sPW)
{
    if (sPW.length <= 2)
        return 0; //密码太短
    Modes = 0;
    for (i = 0; i < sPW.length; i++){
       //测试每一个字符的类别并统计一共有多少种模式.
        Modes|=CharMode(sPW.charCodeAt(i));
    }
    return bitTotal(Modes);
}

//pwStrength函数
//当用户放开键盘或密码输入框失去焦点时,根据不同的级别显示不同的颜色

function checkStrength(pwd){
    O_color = "#eeeeee";
    L_color = "#FF0000";
    M_color = "#FF9900";
    H_color = "#33CC00";
    
    if (pwd == null || pwd == ""){
        //checkPassWordLength(pwd, "#ErrorPassWord", "密码不能为空！");
        Lcolor = Mcolor = Hcolor = O_color;
        return false;
    }else{
        //checkPassWordLength(pwd, "#ErrorPassWord", "密码长度必须介于6-16位！");
        
        S_level = checkStrong(pwd);
        
        switch(S_level){
            case 0:
                Lcolor=Mcolor=Hcolor=O_color;
                break;
            case 1:
                Lcolor=L_color;
                Mcolor=Hcolor=O_color;
                break;
            case 2:
                Lcolor=Mcolor=M_color;
                Hcolor=O_color;
                break;
            default:
                Lcolor=Mcolor=Hcolor=H_color;
                break;
        }
    }
    
    document.getElementById("strength_L").style.background = Lcolor;
    document.getElementById("strength_M").style.background = Mcolor;
    document.getElementById("strength_H").style.background = Hcolor;
    
    return false;
}

//检查密码长度
function checkPassWordLength(sPW, sErrorSpan, sErrorMsg){
    if (sPW.length<6 || sPW.length>16)
    {
        $(sErrorSpan).html(sErrorMsg);
    }
    else
    {
        $(sErrorSpan).html("");
    }
    return (sPW.length>=6 && sPW.length<=16);
}
