var Request = new Object();
var requesttype=true;
Request.send = function(url, method, callback, data, urlencoded, name) {
//alert(url+"\n"+method+"\n"+data);
	var req, durl, tries = 0;
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (typeof name == "undefined") {
		name = "";
	} else {
		name += "\n";
	}
	var readychange = function() {
		if (req.readyState == 4) {// only if req shows "loaded"
			if (req.status < 400) {// only if "OK"
				var lm = null;
				try {
					lm = req.getResponseHeader("Last-Modified");
					lm = new Date(lm);
				} catch (e) { // workaround for Firefox 1.0.7
					lm = new Date();
					if (Math.random() > 0.8) {
						lm -= 2400001;
					}
				}
				if (data && String(data).substr(0,4) == "feed") {
					if ((new Date()) - lm > 2400000) { // 40 * 60 * 1000
						// stale cache
						data = "";
						do_request();
					} else {
						callback(req);
					}
				} else {
					(method=="POST") ? callback(req) : callback(req,data);
				}
			} else if (typeof req == "undefined" || typeof req.status == "undefined") {
				// don't do anything. user has navigated away
			} else if (req.status == 401) { // unauthorized
				callback(req);
			} else if (req.status == 404) {
				if (data && String(data).substr(0,4) == "feed") { 
					// cache miss
					data = "";
					do_request();
				}
			} else {
				switch(req.status) {
					// windows error codes
					case 12002: // server timeout
					case 12029: case 12030: case 12031: // dropped connection
					case 12152: // connection closed by server
					case 13030:
//						alert(name + "error_Network");
						break;
					case 500: case 503:
//						alert(name + "error_Internal");
						break;
					default:
//						alert("status: " + req.status+ "/" + req.statusText);
				}
			}
		}
	};
	function do_request() {
		if (++tries > 2) { // retry only twice
			return false;
		}
		if (method=="POST") {
			req.open("POST", url, true);
			//if (urlencoded) req.setRequestHeader('content', 'application/x-www-form-urlencoded');
			if (urlencoded) req.setRequestHeader('Content-Type', 'text/html; charset=UTF-8');
			req.onreadystatechange = readychange;
			req.send(data);
		} else {
			durl = url;
			if (data && String(data).substr(0,4) == "feed") {
				durl = hex_md5(String(data).substr(4));
				durl = NV_PATH + "cache/" + durl.substr(0, 1) + "/" + durl.substr(1, 1) + "/" + durl.substr(2, 1) + "/" + durl + ".xml?" + Math.random();
			}
			req.open("GET", durl, requesttype);
			req.onreadystatechange = readychange;
			req.send(null);
		}
	};
	do_request();
	return req;
}

//异步调用
Request.sendRawPOST = function(url, data, callback, name, refresh) {
    requesttype=true;
	var t = (url.indexOf('?')>0)?"&t=":"?t=";
	if(refresh==1)
		url+t+Math.random();
	Request.send(url, "POST", callback, data, false, name);
}
Request.sendPOST = function(url, data, callback, name, refresh) {
    requesttype=true;
	var t = (url.indexOf('?')>0)?"&t=":"?t=";
	if(refresh==1)
		url+t+Math.random();
	Request.send(url, "POST", callback, data, true, name);
}
Request.sendGET = function(url, callback, args, name, refresh) {
    requesttype=true;
	var t = (url.indexOf('?')>0)?"&t=":"?t=";
	if(refresh==1)
		url+t+Math.random();
	return Request.send(url, "GET", callback, args, name);
}
//直到执行完成才继续执行
Request.sendGETsync = function(url, callback, args, name, refresh) {
    requesttype=false;
	var t = (url.indexOf('?')>0)?"&t=":"?t=";
	if(refresh==1)
		url+t+Math.random();
	return Request.send(url, "GET", callback, args, name);
}
//同步调用
Request.syncsendGET = function(url) {
    requesttype=true;
	var req = null;
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	req.open('GET',url,false);
	req.send();
	return req;
}