/**
 	* fieldId - 校验的字段ID
 	* msgAreaId - 信息提示区域ID
 	* tips - 一个数组，分别是正常提示、正在校验的时候的提示
 	* param - 可以是一个校验用的URL，返回JSON格式字符串；也可以是一个function，返回一个对象
 	* 返回的JSON数据字符串或者function返回的对象都按下面的格式：
 	* {"ok":true,"messages": "anymessage"}或者{"ok":false,"errors": "anyerror"}
 	*
 	**/
function validateField(fieldId, msgAreaId, tips, param) {
	$("#" + fieldId).focus(function () {
		$("#" + msgAreaId).removeClass();
		$("#" + msgAreaId).html(tips[0]);
	});
	$("#" + fieldId).blur(function () {
		changeClass(msgAreaId, "checking");
		$("#" + msgAreaId).html(tips[1]);
		if (typeof param == "string") {
			var p = "";
			if (param.indexOf("?") > 0) {
				p = param + "&" + fieldId + "=" + $("#" + fieldId).val() + "&stamp=" + Math.random();
			} else {
				p = param + "?" + fieldId + "=" + $("#" + fieldId).val() + "&stamp=" + Math.random();
			}
			$.post(p, {}, function (json) {
				if (json.ok) {
					changeClass(msgAreaId, "ok");
					$("#" + msgAreaId).html(json.messages);
				} else {
					changeClass(msgAreaId, "error");
					$("#" + msgAreaId).html(json.errors);
				}
			}, "json");
		} else {
			if (typeof param == "function") {
				json = param();
				if (json.ok) {
					changeClass(msgAreaId, "ok");
					$("#" + msgAreaId).html(json.messages);
				} else {
					changeClass(msgAreaId, "error");
					$("#" + msgAreaId).html(json.errors);
				}
			}
		}
	});
}
function toggle(src, id) {
	$("#" + id).toggle();
	$(src).parent().toggleClass("collapse");
}
/**
 	隐藏表格的列，id是表格TBODY的ID，colIndex是表格的列序号
 	*/
function hideCol(id, colIndex) {
	$("#" + id).children("tr").each(function (row) {
		$(this).children(/td|th/g).each(function (col) {
			if (colIndex.length) {
				for (index = 0; index < colIndex.length; index++) {
					if (col == colIndex[index]) {
						$(this).hide();
						break;
					}
				}
			} else {
				if (col == colIndex) {
					$(this).hide();
				}
			}
		});
	});
}
function _tableMouseAction(tableid) {
	$("#" + tableid + ">tbody").children("tr").each(function () {
		$(this).mouseover(function () {
			$(this).addClass("alt_mouseover");
		});
	});
	$("#" + tableid + ">tbody").children("tr").each(function () {
		$(this).mouseout(function () {
			$(this).removeClass("alt_mouseover");
		});
	});
}
function tableMouseAction(tableid) {
	if (typeof (tableid) != "string") {
		for (index = 0; index < tableid.length; index++) {
			_tableMouseAction(tableid[index]);
		}
	} else {
		_tableMouseAction(tableid);
	}
}
function _tabRowColor(tbid, c1, c2) {
	$("#" + tbid + ">tbody>tr").each(function (i) {
		$(this).addClass([c1, c2][i % 2]);
	});
}
function tabRowColor(tbid, c1, c2) {
	if (typeof (tbid) != "string") {
		for (index = 0; index < tbid.length; index++) {
			_tabRowColor(tbid[index], c1, c2);
		}
	} else {
		_tabRowColor(tbid, c1, c2);
	}
}
/**
JS版的URLEncode和URLDecode
*/
function URLEncode(text) {
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
	"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
	"abcdefghijklmnopqrstuvwxyz" + "-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";
	var plaintext = text;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++) {
		var ch = plaintext.charAt(i);
		if (ch == " ") {
			encoded += "+";				// x-www-urlencoded, rather than %20
		} else {
			if (SAFECHARS.indexOf(ch) != -1) {
				encoded += ch;
			} else {
				var charCode = ch.charCodeAt(0);
				if (charCode > 255) {
					alert("Unicode Character '" + ch + "' cannot be encoded using standard URL encoding.\n" + "(URL encoding only supports 8-bit characters.)\n" + "A space (+) will be substituted.");
					encoded += "+";
				} else {
					encoded += "%";
					encoded += HEX.charAt((charCode >> 4) & 15);
					encoded += HEX.charAt(charCode & 15);
				}
			}
		}
	} // for
	return encoded;
}
function URLDecode(text) {
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
	var HEXCHARS = "0123456789ABCDEFabcdef";
	var encoded = text;
	var plaintext = "";
	var i = 0;
	while (i < encoded.length) {
		var ch = encoded.charAt(i);
		if (ch == "+") {
			plaintext += " ";
			i++;
		} else {
			if (ch == "%") {
				if (i < (encoded.length - 2) && HEXCHARS.indexOf(encoded.charAt(i + 1)) != -1 && HEXCHARS.indexOf(encoded.charAt(i + 2)) != -1) {
					plaintext += unescape(encoded.substr(i, 3));
					i += 3;
				} else {
					alert("Bad escape combination near ..." + encoded.substr(i));
					plaintext += "%[ERROR]";
					i++;
				}
			} else {
				plaintext += ch;
				i++;
			}
		}
	} // while
	return plaintext;
}


