/**
* @Date 2009-05-29
* @author wps2000(zhangxinling@kingsoft.com)
* @see http://www.bluebirdsky.cn/article.asp?id=271
* 出于不和 checkinput.js 命名向冲突的考虑，这里将一些工具函数封到 wps 命名空间下
*/
var wps	= {
	char : {
		/**
		* 判断一个字符是不是大写字母
		*/
		isUpper : function (code)
		{
			if(code >= 65 && code <= 90) return true;
			return false;
		},
		/**
		* 判断是不小写字母
		*/
		isLower : function (chr)
		{
			if(chr >= 97 && chr <= 122) return true;
			return false;
		},
		/**
		* 判断是不是数字
		*/
		isNumber : function (chr)
		{
			if(chr >= 48 && chr <= 57)	return true;
			return false;
		},
		/**
		* 判断一个字符是不是符号
		*/
		isOther : function (chr)
		{
			if(!this.isUpper(chr) && !this.isLower(chr) && !this.isNumber(chr)) return true;
			return false;
		}
	},
	string : {
		/**
		* 字符串中有没有小写字符
		*/
		haveUpper : function (str)
		{
			var i = 0;
			for(; i < str.length; ++ i)
			{
				if(wps.char.isUpper(str.charCodeAt(i)))	return true;
			}
			return false;
		},
		/**
		* 字符串中有没有大写字符
		*/
		haveLower : function(str)
		{
			var i = 0;
			for(; i < str.length; ++ i)
			{
				if(wps.char.isLower(str.charCodeAt(i))) return true;
			}
			return false;
		},
		/**
		* 计算字符串中数字的个数
		*/
		countNumber : function (str)
		{
			var rte = 0;
			var i = 0;
			for(; i < str.length; ++ i)
			{
				if(wps.char.isNumber(str.charCodeAt(i)))	++ rte;
			}
			return rte;
		},
		/**
		* 计算字符串中符号的个数
		*/
		countOther : function(str)
		{
			var rte	= 0;
			var i = 0;
			for(; i < str.length; ++ i)
			{
				if(wps.char.isOther(str.charCodeAt(i)))	++ rte;
			}
			return rte;
		}
	},
	/**
	* 检查一个密码的强度
	*/
	checkLevel : function(str)
	{
		var scode = 0;
		if(str.length <= 4) scode += 5;
		else
		{
			if(str.length >= 5 && str.length <= 7) scode += 10;
			else scode += 25;
		}
		if(wps.string.haveUpper(str)) scode += 10;
		if(wps.string.haveLower(str)) scode += 10;
		var number_count = wps.string.countNumber(str);
		if(number_count > 0 && number_count <= 2)	scode += 10;
		if(number_count >= 3) scode += 20;
		var other_count	= wps.string.countOther(str);
		if(other_count == 1) scode += 10;
		if(other_count > 1) scode += 25;
		if((wps.string.haveLower(str) || wps.string.haveUpper(str)) && number_count > 0){
			scode += 2;
			if(other_count > 0){
				scode += 3;
				if(wps.string.haveLower(str) && wps.string.haveUpper(str)) scode += 5;
			}
		}
		return scode;
	}
};
/**
* 适配器,出于向下兼容的考虑
*/
function checkLevel(str)
{
	var c	= wps.checkLevel(str);
	if(c >= 90)	return 7;
	if(c >= 80)	return 6;
	if(c >= 70)	return 5;
	if(c >= 60)	return 4;
	if(c >= 50)	return 3;
	if(c >= 25)	return 2;
	return 0;
}

//垃圾代码开始
function testPasswordLevel(o, p)
{
	var n = wps.checkLevel(p);
	if(n >= 90){
		$(o).html('非常安全');
		return;
	}
	if(n >= 80)
	{
		$(o).html('安全');
		return;
	}
	if(n >= 70)
	{
		$(o).html('非常强');
		return;
	}
	if(n >= 60)
	{
		$(o).html('强');
		return;
	}
	if(n >= 50)
	{
		$(o).html('一般');
		return;
	}
	if(n >= 25)
	{
		$(o).html('弱');
		return;
	}
	$(o).html('非常弱');
	return;
}

/**
* 本函数本来不应该在这里，但出于兼容性考虑，放在这里
*/
function isRightPassword(str)
{
	var num_rule = /^\d{0,8}$/;
	return !num_rule.test(str);
}