function isIE()
{
	return document.all ? true : false;
}


function $(ID)
{
	return document.getElementById(ID);
}


/** 返回一个合法的location.search值
	* 将location.search中的参数param的值替换为value。如不存在则追加
	* @param string param			变量名
	* @param string value			变量值
	* return string						不带“?”的字符串
	*/
function modifyQueryString(param, value)
{	   
	var tmp = new Array();

	QUERY_STRING = location.search;
	if(/^\?/.test(QUERY_STRING)) QUERY_STRING=QUERY_STRING.substr(1);

	if(QUERY_STRING=="") QUERY_STRING=param+"="+value;
	else
	{	
		var tmp = new Array();

		T = QUERY_STRING.split("&");
		exist = false;
		for(V in T)
		{
			t = T[V].split("=");
			if(t[0] == param)
			{
				t[1] = value;
				exist = true;
			}
			tmp.push(t[0]+"="+t[1]);
		}
		if(!exist) tmp.push(param+"="+value);
	}

	return tmp.join("&");
}


/*通用表单验证函数

	formObj		表单对象
	field			要验证的字段数组。每个元素的索引是它的name，值是他的中文名称。
						例如：field = array("amount"=>"金额", "password"=>"密码"))
*/
function commonCheckForm(formObj, field)
{
	for(i in field)
	{
		if(formObj.elements[i].value == "")
		{
			alert("请填写"+field[i]);
			formObj.elements[i].focus();
			return false;
		}
	}
	return true;
}


/*将tbody内的行重新排序的函数

	string TbodyID			Tbody的ID
	int CellIndex				排序参照列的index
*/
function sortTable(TbodyID, CellIndex)
{
	table = document.getElementById(TbodyID);
	backupTable = table.cloneNode(true);			//复制一个table供以后操作

	content = new Array();										//td的内容数组
	for(tr=0; tr<table.rows.length; tr++)
	{
		v = table.rows[tr].cells[CellIndex].innerHTML;
		content[tr] = parseFloat(v)==v ? parseFloat(v) : v;
	}

	//计算排序方法
	if(typeof(method)=="undefined" || method=="DESC")	method="ASC";
	else method="DESC";

	function t(a, b)
	{
		if(a==b) return 0;
		else
		{
			if(method == "ASC") return a>b?1:-1;
			else return a<b?1:-1;
		}
	}
	content.sort(t);

	/*
	在table副本中搜索原值的index，同时删除它
	可能会出现重复值情况，所以将已经搜索出来的index做记录，下次搜索时自动跳过
	*/
	deletedIndex = new Array();
	function searchIndex(v)
	{
		for(tr=0; tr<table.rows.length; tr++)
		{
			exists = false;
			if(table.rows[tr].cells[CellIndex].innerHTML == v)
			{
				for(c=0; c<deletedIndex.length; c++)
				{
					if(deletedIndex[c] == tr)
					{
						exists = true;
						continue;
					}
				}
				if(exists) continue;
				deletedIndex[deletedIndex.length] = tr;
				return tr;
			}
		}
	}

	//计算content数组中每个值的原index
	indexs = new Array();
	for(c=0; c<content.length; c++)
	{
		indexs[c] = searchIndex(content[c]);
	}

	//清空原table
	while(table.rows.length > 0) table.deleteRow(0);

	//重建table
	tmp = isIE() ? backupTable.childNodes : backupTable.rows;
	for(c=0; c<indexs.length; c++)
	{
		a = tmp[indexs[c]].cloneNode(true);
		table.appendChild(a);
	}

	backupTable = null;
}
