//用户注册
var rclick = null;
function notnull(val){
	return val.match(/^\S+$/) ? true : false;
}

function nextspan(obj){
	while(true){
		if(obj.nextSibling.tagName == 'SPAN'){
			return obj.nextSibling;
		}else{
			obj = obj.nextSibling;
		}
	}
}

function setStats(name, mess, check){
	var obj = document.getElementsByName(name)[0];
	obj.cnfocus = function(){
		nextspan(obj).className = "stats1";
		nextspan(obj).innerHTML = mess;
	}

	obj.onblur = function(){
		info = check(this.value, mess);
		if(info[0]){
			nextspan(obj).className = "stats2";
			nextspan(obj).innerHTML = "输入正确！";
		}else{
			nextspan(obj).className = "stats3";
			nextspan(obj).innerHTML = info[1];
		}
	}

	if(rclick == 'click')
		obj.onblur();
}

window.onload = validate;

function validate(click){
	if(click)
		rclick = click;

	var fg = true;

	setStats("username", '用户名应该为3-10位之间', function(val, mess){
		var info = [false, mess];
		if(notnull(val) && val.length >=3 && val.length <=10){
			Ajax('html', false).get(url+"/checkuser/username/"+val, function(data){
				info[0] = eval(data);
				if(!info[0]){
					info[1] = "用户 "+val+" 已经存在，请重新输入";
					fg = false;
				}
			})
		}else{
			fg = false;
		}

		return info;
	});

	setStats("password", '密码不能少于6位，或者大于16位',  function(val, mess){
		var info = [false, mess];
		if(notnull(val) && val.length >=6 && val.length <=16){
			info[0] = true;
		}else{
			fg = false;
		}

		return info;
	});

	setStats("password1", '确认密码和上面的密码必须一致', function(val, mess){
		var info = [false, mess];
		var passwd = document.getElementsByName("password")[0];
		if(val == passwd.value && val.length >=6 && val.length <= 16){
			info[0] = true;
		}else{
			fg = false;
		}

		return info;
	});

	setStats("email", '请正解输入EMAIL格式', function(val, mess){
		var info=[false, mess];		

		if(val.match(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/)){
			info[0]=true;	
		}else{
			fg=false;
		}
		return info;
	});
}