function checkRegistEmail() {
	var _e = $("#emailInput").val();
	$("#emailValid").removeClass("checkfalse").removeClass("checkok");
	if(!isEmail(_e)) {
		$("#emailValid").addClass("checkfalse");
		$("#emailError").show();
		return false;
	}
	$("#emailValid").addClass("checkok");
	return _e;
};

function checkRegistAccount() {
	var _a = $("#accountInput").val();
	$("#accountValid").removeClass("checking").removeClass("checkfalse").removeClass("checkok");
	var regex = /^[a-zA-Z0-9-]{4,20}$/g;
	var re = new RegExp(regex);
	if(!re.test(_a)) {
		$("#accountValid").addClass("checkfalse");
		$("#accountErrorMsg").text('帐号不能设置为空! 长度在 4- 20 字符内. 谢谢合作!');
		$("#accountError").show();
		return false;
	}

	$("#accountValid").addClass("checking");
	var checkUrl = '/checkRegistAccount.html';
	$.post(checkUrl, {account:_a}, function(dt){
		if(!dt.result) {
			$("#accountValid").removeClass("checking").addClass("checkfalse");
			$("#accountErrorMsg").text(dt.msg);
			$("#accountError").show();
			return false;
		} else {
			$("#accountValid").removeClass("checking").addClass("checkok");
			return _a;
		}
	}, 'json');
	return _a;
};

function checkRegistPasswd() {
	var _p = $("#passwdInput").val();
	$("#passwdValid").removeClass("checkfalse").removeClass("checkok");
	if(_p.length < 4 || _p.length > 20) {
		$("#passwdValid").addClass("checkfalse");
			$("#passwdError").show();
		return false;
	}
	$("#passwdValid").addClass("checkok");
	return _p;
};

$(document).ready(function(){
	$(".registInput").addClass("inputFocusOff");
	$(".registInput").focus(function(){
		$(".registInput").removeClass("inputFocusOn").addClass("inputFocusOff");
		$(this).removeClass("inputFocusOff").addClass("inputFocusOn");
		$("#" + $(this).attr("name") + "Valid").removeClass("checkok").removeClass("checkfalse").removeClass("checking");
		$("#" + $(this).attr("name") + "Error").hide();
	});
	$(".registInput").blur(function(){
		$(this).removeClass("inputFocusOn").addClass("inputFocusOff");
	});

	$("#emailInput").blur(function(){
		checkRegistEmail();
	});
	$("#accountInput").blur(function(){
		checkRegistAccount();
	});
	$("#passwdInput").blur(function(){
		checkRegistPasswd();
	});

	$("#registedForm").submit(function(){
		var e = checkRegistEmail();
		var a = checkRegistAccount();
		var p = checkRegistPasswd();
		if(e && a && p) {
			addMmLightBoxMsg('正在向服务器申请注册. 请稍后...', 0, 0, 1);
			var postUrl = $(this).attr("action");
			$("#submitButton").attr("disabled", true);
			$.post(postUrl, {account:a, passwd:p, email:e}, function(dt){
				if(!dt.result) {
					$("#submitButton").attr("disabled", false);
					addMmLightBoxMsg(dt.msg, 1, 1, 0, function(){
						window.setTimeout(function(){
							removeMmLightBoxMsg();
						}, 2000);
					});
				} else {
					addMmLightBoxMsg(dt.msg, 0, 0, 0, function(){
						window.setTimeout(function(){
							window.location.href = '/';
						}, 2000);
					});
				};
			}, 'json');
		}
		return false;
	});
});