function setCookie(name, value) {
	document.cookie = escape(name) + "=" + escape(value);
}

function getCookie(name) {
	var allCookies = document.cookie.split("; ");
	for (var i = 0; i < allCookies.length; i ++) {
		var theCookie = allCookies[i].split("=");
		if (theCookie[0] == escape(name))
			return unescape(theCookie[1]);
	}
	return null;
}

function deleteCookie(name) {
	var gone = new Date();
	gone.setTime(gone.getTime() - 1);
	document.cookie = escape(name) + "=; expires=" + gone.toGMTString();
}

function createXMLHttpRequest(){
	xmlHttp = false;
	try {
		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
  		try {
    		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e2) {
			xmlHttp = false;
		}
	}
	if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
  		xmlHttp = new XMLHttpRequest();
	}
	return xmlHttp;
}
// arg: the argument, except the xmlHttpRequest, of the call-back function.
// It can be null if not required.
function sendRequest(url, params, show, callBack, arg) {
	show.innerHTML = "操作进行中……";
	var xmlHttp = createXMLHttpRequest();
	try{
		xmlHttp.onreadystatechange = function(){commonBack(xmlHttp, show, callBack, arg)};
		xmlHttp.open("POST", url, true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(params);
	}catch(exception){
		alert(exception);
		// Network error or server unavailable
		show.innerHTML = "无法连接服务器，请稍后再试";
	}
}
// The call-back of the sendRequest. It first gets the xml response. When the response has status of 200 or 0,
// it calls the user-specified callBack function.
function commonBack(xmlHttp, show, callBack, arg) {
	if(xmlHttp.readyState == 4){
		if (xmlHttp.status == 200 || xmlHttp.status == 0){
			callBack(xmlHttp, arg);
		} else {
			// Got a 404 or something else.
			show.innerHTML = "服务器无法连接或返回异常（返回代码为'" + xmlHttp.status + "'）";
		}
	}
}
// Get (the first line) of the content in an xml tag.
function getContent(node) {
	return node.firstChild == null ? "" : node.firstChild.data;
}
// Check the radio with the given value.
function checkRadio(radios, value) {
	var radio;
	for (var i = 0; i < radios.length; i ++) {
		radio = radios[i];
		if (radio.value == value)
			radio.checked = true;
	}
}
// Get the value of the checked.
// If no radio is checked, an empty string will be returned.
function radioValue(radios) {
	var radio;
	for (var i = 0; i < radios.length; i ++) {
		radio = radios[i];
		if (radio.checked)
			return radio.value;
	}
	return "";
}
// Uncheck all the radios
function uncheckAll(radios) {
	for (var i = 0; i < radios.length; i ++) {
		radios[i].checked = false;
	}
}
// If str is "true", then check the checkBox; otherwise, uncheck it.
function setCheck(checkBox, str) {
	checkBox.checked = str == "true";
}
// Return true if str has nothing but space
function isEmpty(str) {
	for (var i = 0; i < str.length; i++) {
		if (str.charAt(i) != ' ')
			return false;
	}
	return true;
}

function showPrivilege(privilege) {
	if (privilege == 0)
		return "普通用户";
	else if (privilege == 1)
		return "编辑";
	else if (privilege == 2)
		return "主编";
	else return "未知";
}

var badFormatShow = "很抱歉！页面程序执行出现异常，请报告系统管理员！";
