/* 去除字符串两边的空白字符
 */
String.prototype.Trim = function()
{
	return this.replace(/(^\s*)|(\s*$)/g, "");
}

/* 生成重复指定次数的字符串
 */
String.prototype.Repeat = function(num)
{
	if (arguments.length == 0)
	{
		alert("必须给 Repeat 函数指定一个参数");
		return;
	}
	if (typeof(num) != "number" || num < 0)
	{
		alert("必须给 Repeat 函数指定一个正整数参数");
		return;
	}

	num = parseInt(num);
	var result = "";
	for (var i = 0; i < num; i++)
		result += this;
	return result;
}

/* 随机取出 v1 到 v2 之间(包括 v1 和 v2)的一个整数
   v1 -> 正整数
   v2 -> 正整数, 可省略, 默认为 0
*/
Random = function(v1, v2)
{
	var ok = true;
	if (arguments.length == 1)
		if (typeof(v1) != "number" || v1 < 0) ok = false;
	else if (arguments.length == 2)
		if (typeof(v1) != "number" || v1 < 0 || typeof(v2) != "number" || v2 < 0) ok = false;
	else
		ok = false
	if (!ok)
	{
		alert("Random 函数需要一个或两个正整数参数");
		return false;
	}
	if (arguments.length == 1)
	{
		var v2 = v1;
		v1 = 0;
	}
	else
	{
		var temp = Math.max(v1, v2);
		v1 = Math.min(v1, v2);
		v2 = temp;
	}
	return Math.floor(Math.random() * (Math.floor(v2 - v1) + 1)) + v1;
}

/* 根据传递的正则表达式验证值
   name -> 被验证的页面元素的名称(应该保证这个名称和所有的id不一样)
   re -> 验证元素值的正则表达式
   description -> 验证失败显示提示信息时对这个元素的称呼
   required -> 是否是必填项
   常用正则表达式
   email ^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$
   汉字 [\u4E00-\u9FA5]
   全角字母和数字 [\u0030-\u0039]|[\u0041-\u007A]|[\uFF10-\u0019]|[\uFF21-\uFF5A]
 */
function CheckFormat(name, re, description, required)
{
	if (typeof(re) == "string")
	{
		switch (re)
		{
			case "email" : re = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/; break;
		}
	}
	if (typeof(name) == "string") var e = document.getElementsByName(name)[0];
	if (!e) return true;
	if (description == "") description = "这个地方";
	var value = e.value;
	if (required)
	{
		if (value == "")
		{
			alert("必须填写 " + description);
			e.focus();
			return false;
		}
	}
	else
		if (value == "") return true;

	if (!re.test(e.value))
	{
		alert(description + " 不正确");
		e.focus();
		return false;
	}
	else
		return true;
}

/* 按比例调整图象的大小
   eImg -> IMG 元素
   width -> 调整后的宽度
   height -> 调整后的高度
 */
function AdjustImage(eImg, imgWidth, imgHeight, imgBorder)
{
	var args = arguments.length;
	if (args < 3)
	{
		alert("AdjustImage 的参数不正确, 至少三个参数");
		return;
	}
	if (args >= 4) eImg.style.borderWidth = imgBorder;
	var image = new Image();
	image.onload = function()
	{
		eImg.removeAttribute("width");
		eImg.removeAttribute("height");
		eImg.style.width = "";
		eImg.style.height = "";
		if (this.height / this.width > imgHeight / imgWidth)
			eImg.style.height = Math.min(this.height, imgHeight);
		else
			eImg.style.width = Math.min(this.width, imgWidth);
	}
	image.src = eImg.src;
}


// 转到下拉列表选择的页
function GoSelectedPage(page)
{
	var uri = location.href;
	uri = uri.replace(/page=\d*&?/, "");

	if (uri.indexOf("?") == -1)
		uri += "?page=";
	else
	{
		var lastChar = uri.substr(uri.length - 1, 1);
		if (lastChar == "?" || lastChar == "&")
			uri += "page=";
		else
			uri += "&page=";
	}
	location.href = uri + page;
}
