$(document).ready(function(){
	$('#capcha').Capcha();
	$("input[name='username']").focus(function(){
		$("#usernameinfo").attr("class","showme");
	}).blur(function(){
		if($(this).val().length == 0){
			$("input[name='username']").next().html("请输入用户名").attr("class","colorred");
		}else {
			$("#usernameinfo").attr("class","hideme");
		}
	});

	$("input[name='password']").focus(function(){
		$("#passinfo").attr("class","showme");
	}).blur(function(){
		if($(this).val().length >= 6){
		$("#passinfo").attr("class","hideme");
		}
	});

	$("input[name='email']").focus(function(){
		$("#emailinfo").attr("class","showme");
	});

});
function checkname(){
	if($("input[name='username']").val().length != 0){
		$.post('/pp/reg/checkname', {username: $("input[name='username']").val()}, function(data){
			var str = '';
			if(data == '1'){
				$("input[name='username']").next().html('恭喜您,用户名有效').attr("class","colorgreen");
			} else {
				if(data == '-1' ){
					str = '用户名不合法';
				} else if(data == '-2' ){
					str = '用户名包含不允许注册的词汇';
				} else if(data == '-3' ){
					str = '用户名已经被注册';
				}
				$("input[name='username']").next().html(str).attr("class","colorred");
			}
		},'text');
	}else {
		$("input[name='username']").next().html('请输入用户名').attr("class","colorred");
	}
}

function checkemail(){
	$.post('/pp/reg/checkemail', {email: $("input[name='email']").val()}, function(data){
		var str = '';
		if(data == '1'){
			$("input[name='email']").next().html('邮箱地址有效').attr("class","colorgreen");
			$("#emailinfo").attr("class","hideme");
		} else{
			if(data == '-4' ){
				str = '请输入正确的邮箱地址';
			} else if(data == '-5' ){
				str = '该邮箱地址不允许注册';
			} else if(data == '-6' ){
				str = '该邮箱已经被注册';
			}
			$("input[name='email']").next().html(str).attr("class","colorred");
		}

	},'text');
}

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; //特殊字符
}

function bitTotal(num){
	modes=0;
	for (i=0;i<4;i++){
		if (num & 1) modes++;
		num>>>=1;
	}
	return modes;
}

function  pwdstrength(){

	var pwd = $("input[name='password']").val();
	var pwdmodes ='';
	if(pwd.length<6){
		pwdmodes = 1 ;
		if(pwd.length==0){
			$("#pwdstrength > span").removeClass("strength_select");
			return false;
		}
		$("input[name='password1']").attr("disabled","ture").next().html("密码至少6位字符");
	}else{
		$("input[name='password1']").removeAttr("disabled").next().html("");
		Modes=0;
		for (i=0;i<pwd.length;i++){
			Modes|=CharMode(pwd.charCodeAt(i));
		}
		pwdmodes = bitTotal(Modes);
	}
	if(pwdmodes==1){
		$("#pwdstrength > span:eq(0)").addClass("strength_select").siblings().removeClass("strength_select");
		$("#pwdstrength").children("div").attr("class","showme");
	}else if(pwdmodes==2){
		$("#pwdstrength > span:eq(1)").addClass("strength_select").siblings().removeClass("strength_select");
		$("#pwdstrength").children("div").attr("class","showme");
	}else if(pwdmodes==3 || pwdmodes==4){
		$("#pwdstrength > span:eq(2)").addClass("strength_select").siblings().removeClass("strength_select");
		$("#pwdstrength").children("div").attr("class","hideme");
	}

}

function pwdcheck(){
	if($("input[name='password']").val().length > 5){
	if($("input[name='password']").val()==$("input[name='password1']").val()){
		$("input[name='password1']").next().html("密码确认有效").attr("class","colorgreen");
		return true;
	}else{
		$("input[name='password1']").next().html("两次密码不一致,请重新输入").attr("class","colorred");
		return false;
	}
	}else{
		return false;
	}
}

function showcheck(){
    var pwd = $("input[name='password']").val();
	if(pwd.length<6){
		$("input[name='password1']").attr("disabled","ture");
	}else{
		$("input[name='password1']").removeAttr("disabled");
	}
}

function toreg(){
	if($("input[name='capcha']").is(":checked")){
		if(pwdcheck()){
		$.post("/pp/reg/do", {username: $("input[name='username']").val(),password: $("input[name='password']").val(),email: $("input[name='email']").val(),capcha:$("input[name='capcha']:checked").val()},function(data){
			if(data.state > 0){ 
			    $("#step_text").html('1 填写信息 &nbsp;&nbsp; <img id="stepnum" src="/images/step_2.gif" alt="step two" align="bottom"/> <b>注册成功</b>');
			    $("#regforum").slideUp('500',function(){
			    	$(this).remove();
			    	$("#welcome").after('<div id="regjump"><span><img src="/images/success.gif" alt="成功" align="bottom"/></span> <span><b>'+data.username+'</b>，恭喜您已经成功注册为三峡大学校友网会员。</span><br/>本页面将在3秒后返回通行证首页，如果页面没有自动跳转，请 <a href="/pp" target="_self">点击此处</a>。</div>');
			    });
			    $.post("/pp/login/syn", {userid:data.state},function(data){ //同步登陆其他平台
					$("body").prepend(data);
				},"html");
			}else{
				
				if(data.state == -1||data.state == -2||data.state == -3){ 
				      $("input[name='username']").next().html(data.str).attr("class","colorred");	
			    }else if(data.state == -4||data.state == -5||data.state == -6){
				     $("input[name='email']").next().html(data.str).attr("class","colorred");	
			    }
			    
				$.post("/pp/capcha/", {action:'new'},function(data){
					$("#capcha").html(data);
					$('#capcha').Capcha();
					$("#capcha").fadeOut('800').fadeIn('800')
				},"html");
			}
		},"json");
		}
	}else{
		$("#capcha > p").addClass("colorred");
	}
}