var vDescs = {};
var vResult = 0;

function outStatus(type,info)
{
	if(type==1)
		return '<img src="http://f1.hxfx.cn/hxfx/images/mark.gif" alt="正确" width="19" height="16" />';
	else
		return '<span><p><img src="http://f1.hxfx.cn/hxfx/images/icon_error.gif" alt="错误" width="16" height="16" />'+info+'</p></span>';
}

function checkValue(vInput)
{
	if (vDescs[vInput.name] == undefined && $(vInput.name + '_notice'))
	{
		vDescs[vInput.name] = $(vInput.name + '_notice').innerHTML;
	}
	
	if (vInput.name == 'username')
	{
		if (vInput.value.isUserName() || vInput.value.isEmail())
		{
			var vAjax = new Ajax();
			var url = './register.php?act=passport&username=' + encodeURIComponent(vInput.value);
			
			vAjax.callback = function(result)
			{
				$(vInput.name + '_notice').innerHTML = (result == 0) ? 	(outStatus(1)) : ((result == 2)?outStatus(0,'您填写的通行证含有禁用词'):outStatus(0,'您填写的通行证已被使用'));
				vResult = result;
			};
			vAjax.runAJAX(url, '', 'get', false);
		} else {
			document.getElementById(vInput.name + '_notice').innerHTML = outStatus(0,'您填写的通行证名不符合规则');
			//$(vInput.name + '_notice').innerHTML = outStatus(0,'您填写的通行证名不符合规则');
			vResult = 1;
		}
	}
	
	if (vInput.name == 'nickname')
	{
		if (vInput.value.isNickName())
		{
			var vAjax = new Ajax();
			var url = './register.php?act=passport&nickname=' + encodeURIComponent(vInput.value);
			
			vAjax.callback = function(result)
			{
				$(vInput.name + '_notice').innerHTML = (result == 0) ? (outStatus(1)) : ((result == 2)?outStatus(0,'您填写的昵称含有禁用词'):outStatus(0,'您填写的昵称已被使用'));
				vResult = result;
			};
			vAjax.runAJAX(url, '', 'get', false);
		} else {
			document.getElementById(vInput.name + '_notice').innerHTML = outStatus(0,'您填写的昵称不符合规则');
			//$(vInput.name + '_notice').innerHTML = outStatus(0,'您填写的昵称不符合规则');
			vResult = 1;
		}
	}
	
	if (vInput.name == 'password')
	{
		if (vInput.value.isPassword())
		{
			$(vInput.name + '_notice').innerHTML = outStatus(1);
		} else {
			$(vInput.name + '_notice').innerHTML = outStatus(0,'您填写的密码不符合规则');
			vResult = 1;
		}
	}
	
	if (vInput.name == 'repassword')
	{
		if (vInput.value.isPassword() && vInput.value == vInput.form.elements['password'].value)
		{
			$(vInput.name + '_notice').innerHTML = outStatus(1);
		} else {
			$(vInput.name + '_notice').innerHTML = outStatus(0,'两次填写的密码不一致');
			vResult = 1;
		}
	}
	
	if (vInput.name == 'email')
	{
		if (vInput.value.isEmail())
		{
			$(vInput.name + '_notice').innerHTML = outStatus(1);
		} else {
			$(vInput.name + '_notice').innerHTML = outStatus(0,'您填写的邮箱不符合规则');
			vResult = 1;
		}
	}
	
	if (vInput.name == 'verify')
	{
		if (vInput.value.length != 5 || !vInput.value.isInt())
		{
			$(vInput.name + '_notice').innerHTML = outStatus(0,'您填写的验证码不符合规则');
			vResult = 1;
		}
		else
		{
			$(vInput.name + '_notice').innerHTML = outStatus(1);
		}
	}

	if (vInput.name == 'idcard')
	{
		if (!vInput.value.isIDCard())
		{
			$(vInput.name + '_notice').innerHTML = outStatus(0,'您填写的身份证不符合规则');
			vResult = 1;
		}
		else
		{
			$(vInput.name + '_notice').innerHTML = outStatus(1);
		}
	}
	/*
	if (vInput.name == 'active')
	{
		if (vInput.value.length != 16)
		{
			$(vInput.name + '_notice').innerHTML = outStatus(0,'您填写的激活码不符合规则');
			vResult = 1;
		}
		else
		{
			$(vInput.name + '_notice').innerHTML = '';
		}
	}*/
	
	if (vInput.name == 'agree')
	{
		if (!vInput.checked)
		{
			alert('请阅读并同意用户协议！');
			vResult = 1;
		}
	}
}

function resetValue(vInput)
{
	try
	{
		if (vDescs[vInput.name]) $(vInput.name + '_notice').innerHTML = vDescs[vInput.name];
	} catch (e) {}
}

function checkStrong(vInput)
{
	var vLevel = 0;
	var vString = vInput.value;
	
	if (vString.isPassword())
	{
		if (vString.match(/[a-z]/ig)) vLevel++;
		if (vString.match(/[0-9]/ig)) vLevel ++;
		if (vString.match(/(.[^a-z0-9])/ig)) vLevel ++;
		if (vString.length >= 10) vLevel ++;
		
		var vFirstChar = vString.substr(0, 1);
		var vRepeat = 1;
		var vSeq = '01234567890abcdefghijklmnopqrstuvwxyz';
		
		for (var i = 1 ; i < vString.length ; i ++)
		{
			if (vString.substr (i, 1) == vFirstChar)
			{
				vRepeat ++;
			} 
			else 
			{
				break;
			}
		}
		
		if (vString.length == vRepeat) vLevel --;
		if (vSeq.indexOf(vString.toLowerCase()) >= 0) vLevel --;
		vLevel = Math.min(Math.max(vLevel, 1), 3);
	}
	if(vLevel==0) $('passlevel').src = 'http://f1.hxfx.cn/hxfx/images/icon_none.gif';
	if(vLevel==1) $('passlevel').src = 'http://f1.hxfx.cn/hxfx/images/icon_weak.gif';
	if(vLevel==2) $('passlevel').src = 'http://f1.hxfx.cn/hxfx/images/icon_middle.gif';
	if(vLevel==3) $('passlevel').src = 'http://f1.hxfx.cn/hxfx/images/icon_strong.gif';
}

function checkForm(vForm, vInputs)
{
	vResult = 0;
	
	for (vKey in vInputs)
	{
		vResult = 0;
		if (vForm.elements[vInputs[vKey]] != undefined)
		{
			checkValue(vForm.elements[vInputs[vKey]]);
			if(vResult!='0')
            {
                vForm.elements[vInputs[vKey]].focus();
                return false;
            }
		}
	}
	return (vResult > 0) ? false : true;
}

function checkReg(vForm)
{
	var vInputs = ['username', 'nickname', 'password', 'repassword', 'email', 'verify', 'idcard', 'agree'];
	//return checkForm(vForm, vInputs);
	if(!checkForm(vForm, vInputs))
    {
        alert("注册资料输入不完整，请检查！");
        return false;
    }
    else return true;
}
