$(document).ready(function() {
	$("#username").focus(function() {
		hiddenTip("name");
	});
	$("#username").blur(function() {
		ckUsername();
	});
	$("#email").focus(function() {
		hiddenTip("email");
	});
	$("#email").blur(function() {
		ckEmail();
    });
    
    $("#password").focus(function() {
    	hiddenTip("pwd");
    });
    $("#password").blur(function() {
		ckPassword();
    });
    
    $("#checkbox").click(function() {
    	if (document.getElementById("checkbox").checked == false) {
    		$("#err_ckbox").css("display", "");
			$("#err_ckbox").html("您必须接受本网站协议才能注册!!");
    	} else {
    		$("#err_ckbox").css("display", "none");
		}
    });
});

function submitForm() {
   if($("#err_name").html() != "true" ){
		ckUsername();
	}
	if($("#err_email").html()!= "true" ){
		ckEmail();
	}
	if($("#err_pwd").html()!= "true" ){
		ckPassword();
	}
	if(document.getElementById("checkbox").checked == false) {
		$("#err_ckbox").css("display", "");
		$("#err_ckbox").html("您必须接受本网站协议才能注册!!");
		return false;
	} else if ($("#err_name").html() == "true" && $("#err_email").html() == "true" && $("#err_pwd").html() == "true") {
		return true;
	}
	return false;
}

function ckUsername() {
		showWaitTip("name");
		var name = trim($("#username").val());
		if (isNull(name)) {
			showWrongTip("name", "用户名不能为空");
		} else if (!isNumberOr_Letter(name)) {
			showWrongTip("name", "用户名只能由字母、半角数字和下划线组成");
		} else if (name.length > 0 && name.length < 3) {
			showWrongTip("name", "用户名不能少于3个字符");
		} else {
			/*
	    	$.post("/ApiServlet", {method: "isnamereg", name: name}, function(xml) {
		    	if (xml == "0") {
		    		showWrongTip("name", name + "已经被注册");
		    	} else {
		    		showRightTip("name");
		    	}
	    	});
			*/
	    }
}

function ckEmail() {
		showWaitTip("email");
		var email = trim($("#email").val());
		if (isNull(email)) {
			showWrongTip("email", "邮箱地址不能为空");
		} else if (!isEmail(email)) {
			showWrongTip("email", "请输入正确的邮箱地址");
		} else {
			/*
			$.post("/ApiServlet", {method: "isemailreg", email: email}, function(xml) {
		    	if (xml == "0") {
		    		showWrongTip("email", "邮箱" + email + "已经注册过");
		    	} else {
		    		showRightTip("email");
		    	}
		    });
			*/
		}
}

function ckPassword() {
		showWaitTip("password");
		var pwd = trim($("#password").val());
		var username = trim($("#username").val());
		if (isNull(pwd)) {
			showWrongTip("pwd", "密码不能为空");
		} else if (pwd.length > 0 && pwd.length < 6) {
	    	showWrongTip("pwd", "密码不能少于6个字符");
	    } else if (duplicate(pwd)) {
	    	showWrongTip("pwd", "密码(" + pwd + ")不能是重复的字符");
	    } else if (pwd == "123456" || pwd == "1234567" || pwd == "12345678" || pwd == "123456789" || pwd == "1234567890") {
	    	showWrongTip("pwd", "密码(" + pwd + ")过于简单");
	    } else if (username.length > 0 && pwd.length > 0 && (pwd.indexOf(username) >= 0 || username.indexOf(pwd) >= 0)) {
	    	showWrongTip("pwd", "密码不能与用户名相似");
	    } else {
	    	showRightTip("pwd");
	    } 
}
function showWaitTip(type) {
	$("#img_" + type).html('<img src="/images/ico/ico_waiting2.gif" align="bottom">');
}

function showWrongTip(type, msg) {
	$("#img_" + type).html("&nbsp;");
	$("#img_" + type).removeClass();
	$("#img_" + type).addClass("r_wrong");
	$("#err_" + type).css("display", "");
	$("#err_" + type).html(msg);
}

function showRightTip(type) {
	$("#img_" + type).html("&nbsp;");
	$("#img_" + type).removeClass();
	$("#img_" + type).addClass("r_right");
	$("#err_" + type).html("true");
	$("#err_" + type).css("display", "none");
}

function hiddenTip(type) {
	$("#img_" + type).html("&nbsp;");
	$("#img_" + type).removeClass();
	$("#img_" + type).addClass("r");
	$("#err_" + type).css("display", "none");
}
