var isRegistered = true;

function showInfo(n,s){
	var ido = document.getElementById(n+"InfoDiv");
	if(typeof s == 'undefined'){
		ido.className = "TrueBox";
		ido.innerHTML = "填写正确！";
	}else{
		ido.className = "FalseBox";
		ido.innerHTML = s;
	}
}

function pwdshowInfo(n,s){
	var ido = document.getElementById(n+"InfoDiv");
    ido.className = "TrueBox";
	ido.innerHTML = s;
}

function successCheck(v){
	if(v == '用户名已经被注册'){
	   showInfo("usr","用户名已存在，请重新输入！");return false;
	}else if(v == '用户名（4-20位）'){
	   showInfo("usr","用户名（4-20位）！");return false;
	}else if(v == '用户名含有非法字符，不能注册'){
	   showInfo("usr","用户名含有非法字符，不能注册！");return false;
	}else if(v == '用户名不能含有“代练吧”字样'){
	   showInfo("usr","用户名不能含有“代练吧”字样！");return false;
	}else if(v == '用户名只允许英文字母、数字、下划线、中文字符'){
	   showInfo("usr","用户名只允许英文字母、数字、下划线、中文字符！");return false;
	}else{
	   showInfo("usr");return true;
    }
}

function checkUsr(s,n){
	var pwd=jstrim(document.getElementById("pass").value);
	if(s==""){showInfo("usr","用户名（4-20位）！");return false;}
	if(s.replace(/[^\x00-\xff]/g,"**").length<4||s.replace(/[^\x00-\xff]/g,"**").length>20){showInfo("usr","用户名（4-20位）！");return false;}
	//if(s.length<4||s.length>20){showInfo("usr","用户名（4-20位）！");return false;}
	if(!checkenchnumdl(s) || s.indexOf(" ")>-1 || s.indexOf("'")>-1){showInfo("usr","用户名含有非法字符，不能注册！");return false;}
	if(getnoregstr(s,document.getElementById("notoreg").value)){showInfo("usr","用户名含有非法字符，不能注册！");return false;}
	if(s.indexOf("代练吧")>-1){showInfo("usr","用户名不能含有“代练吧”字样！");return false;}
	if(pwd!="" && pwd==s){showInfo("usr","用户名不允许与密码相同，请更换用户名！");return false;}
	if(n==1){
	    return successCheck(loadAjaxData("/Data_processing.asp?action=checkuser&username="+document.getElementById("username").value));
	}else{
		return true;
	}
}

function UserPWdCheck(n){
	var pwd=jstrim(document.getElementById("pass").value);
	var repwd=jstrim(document.getElementById("repass").value);
	var username=document.getElementById("username").value;
	if(n==0){
		if(pwd==""){showInfo("pwd","密码长度为6-20位，字母请区分大小写！");return false;}
		if(pwd.length<6 || pwd.length>20){showInfo("pwd","密码长度为6-20位，字母请区分大小写！");return false;}
		if(username!="" && pwd==username){showInfo("pwd","密码不允许与用户名相同，请更换密码！");return false;}
		if(repwd!="" && pwd!=repwd){showInfo("pwd","两次密码输入不一致，请重新输入！");return false;}
		if(repwd!=""){showInfo("repwd");}
		pwdshowInfo("pwd",pwStrength(pwd));return true;
	}else{
		if(pwd==""){
	       if(repwd.length<6 || repwd.length>20){showInfo("repwd","密码长度为6-20位，字母请区分大小写！");return false;}
		   if(username!="" && repwd==username){showInfo("repwd","密码不允许与用户名相同，请更换密码！");return false;}
		}else{
		   if(pwd!=repwd){showInfo("repwd","两次密码输入不一致，请重新输入！");return false;}
		}
		if(pwd!=""){showInfo("pwd");pwdshowInfo("pwd",pwStrength(pwd));}
		showInfo("repwd");return true;
	}
}

function setfirst(){
	var theform=document.myform;
	if(!theform.followme[0].checked){
		alert("阅读并同意爱客宝服务协议,才能进行下一步！");
	}else if(checkUsr(document.getElementById("username").value,0) && UserPWdCheck(0) && UserPWdCheck(1)){
		document.getElementById("ubase").style.display="none";
		document.getElementById("usafe").style.display="block";
	}
}

function question_change(){
   var theform=document.myform;
   if(theform.question.value=="0"){
   	  document.getElementById("getquestionself").style.display="block";
   }else{
   	  document.getElementById("getquestionself").style.display="none";
   	  theform.questionself.value="";
   }
}

function UserQuestionCheck(s){
	var theform=document.myform;
	if(theform.question.value != "0"){
       question_change();return true;
	}else{
	   s=jstrim(s);
	   if(s==""){showInfo("questionself","请输入你的自定义问题！");return false;}
	   if(s.indexOf("'")>-1){showInfo("questionself","输入的自定义问题含有非法字符“'”");return false;}
	   showInfo("questionself");return true;
	}
}

function UserAnswerCheck(s){
	var theform=document.myform;
	s=jstrim(s);
	if(s==""){showInfo("answer","请输入你的答案！");return false;}
	if(s.indexOf("'")>-1){showInfo("answer","输入的答案含有非法字符“'”");return false;}
	if(s.replace(/[^\x00-\xff]/g,"**").length<4){showInfo("answer","输入的答案长度不能少于4个字符(一个中文为两个字符)");return false;}
	showInfo("answer");return true;
}

function UserEmailCheck(s){
	var theform=document.myform;
	s=jstrim(s);
	if(s==""){showInfo("email","请输入你的电子邮件地址！");return false;}
	if(s.indexOf("'")>-1){showInfo("email","输入的电子邮件地址含有非法字符“'”");return false;}
	if(!checkemail(s)){showInfo("email","邮箱格式不正确,请输入正确的安全邮箱地址。正确的例子如：myname@163.com。");return false;}
	if(s.split("@")[1].indexOf(".")<=-1){showInfo("email","邮箱格式不正确,请输入正确的安全邮箱地址。正确的例子如：myname@163.com。");return false;}
	showInfo("email");return true;
}

function UserQQCheck(s){
	var theform=document.myform;
	s=jstrim(s);
	if(s==""){showInfo("qq","请输入你的个人QQ号码！");return false;}
    if(s.length<4 || s.length>20){showInfo("qq","QQ必须由5-20位数字组成！");return false;}
    if(!checkisnumber(s,0)){showInfo("qq","QQ必须由数字组成！");return false;}
    if(s.substring(0,1)=="0"){showInfo("qq","QQ开始字符不能为0！");return false;}
	showInfo("qq");return true;
}

function CodeCheck(s){
	var theform=document.myform;
	s=jstrim(s);
	if(s==""){showInfo("code","请输入验证码！");return false;}
	if(s.indexOf("'")>-1 || s.length!=4){showInfo("code","请正确输入验证码");return false;}
	showInfo("code");return true;
}

function setsubmit(){
	var theform=document.myform;
	if(UserQuestionCheck(theform.questionself.value) && UserAnswerCheck(theform.answer.value) && UserQQCheck(theform.qq.value) && UserEmailCheck(theform.email.value) && CodeCheck(theform.code.value)){
		//theform.backto.disabled=true;
		theform.submits.disabled=true;
		document.getElementById("esave").style.display="block";
		theform.submit();
	}
}

function backtofirst(){
    document.getElementById("ubase").style.display="block";
	document.getElementById("usafe").style.display="none";
}

function usshowerr(n,m,str){
	var theform=document.myform;
	document.getElementById("esave").style.display="none";
    alert(str);	
	if(n==0){
	   //theform.backto.disabled=false;
	   theform.submits.disabled=false;
	   if(m==0){
          document.getElementById("ubase").style.display="block";
	      document.getElementById("usafe").style.display="none";
	   }
	}else if(n==-1){
	   window.location.href="/";
	}else if(n==1){
	   window.location.href="/user/";
	}
}

function pwStrength(pwd){
   var S_level=checkStrong(pwd);
   document.getElementById("pastrength").value=S_level;
   switch(S_level){
      case 0:
        return "检测提示：密码安全性差。"; 
        break;
      case 1:
        return "检测提示：密码安全性差。"; 
        break;
      case 2:
        return "检测提示：密码安全性良好。";
        break;
      default:
        return "检测提示：密码安全性高。";
   }
}

function checkStrong(sPW){
if (sPW.length<=7)
return 0; //密码太短
Modes=0;
for (i=0;i<sPW.length;i++){
Modes|=CharMode(sPW.charCodeAt(i));
}
return bitTotal(Modes);
} 

function bitTotal(num){
  modes=0;
  for (i=0;i<4;i++){
    if (num & 1) modes++;
    num>>>=1;
  }
  return modes;
}

function CharMode(iN){
  if (iN>=48 && iN <=57){ //数字
    return 1; 
  }else if (iN>=65 && iN <=90){ //大写字母
    return 2;
  }else if (iN>=97 && iN <=122){ //小写
    return 4;
  }else{
   return 8; //特殊字符
  }
}