/**
 * new Ajax(statusId, recvType)
 * statusId 显示进度的HTML Element, 如果没有填null
 * recvType 返回数据的格式,有html和xml两种,默认是html
 * get(targetUrl, resultHandle)              get方法,targetUrl目标页面,resultHandle 外部回调函数
 * post(targetUrl, sendString, resultHandle) post方法,targetUrl目标页面,sendString 连接后缀参数,resultHandle 外部回调函数
 */
var xml_http_building_link = '请等待，正在建立连接...';
var xml_http_sending = '请等待，正在发送数据...';
var xml_http_loading = '请等待，正在接受数据...';
var xml_http_load_failed = '通信失败，请刷新重新尝试';
//var xml_http_data_in_processed = '通信成功，数据正在处理中...';

function Ajax(statusId, recvType) {
	var aj = new Object();
	if(typeof this.statusId != 'undefined' || this.statusId!=null) {
		aj.statusId = statusId;
	}
	aj.targetUrl = '';
	aj.sendString = '';
	aj.recvType = recvType ? recvType.toUpperCase() : 'HTML';//HTML XML
	aj.resultHandle = null;

	aj.createXMLHttpRequest = function() {
		var request = false;
		if(window.XMLHttpRequest) {
			request = new XMLHttpRequest();
			if(request.overrideMimeType) {
				request.overrideMimeType('text/xml');
			}
		} else if(window.ActiveXObject) {
			var versions = ['Microsoft.XMLHTTP', 'MSXML.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.7.0', 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];
			for(var i=0; i<versions.length; i++) {
				try {
					request = new ActiveXObject(versions[i]);
					if(request) {
						return request;
					}
				} catch(e) {
					//alert(e.message);
				}
			}
		}
		return request;
	}

	aj.XMLHttpRequest = aj.createXMLHttpRequest();

	aj.processHandle = function() {
		if(typeof aj.statusId!='undefined')
		aj.statusId.style.display = '';
		if(aj.XMLHttpRequest.readyState == 1) {
			if(typeof aj.statusId!='undefined')
			aj.statusId.innerHTML = xml_http_building_link;
		} else if(aj.XMLHttpRequest.readyState == 2) {
			if(typeof aj.statusId!='undefined')
			aj.statusId.innerHTML = xml_http_sending;
		} else if(aj.XMLHttpRequest.readyState == 3) {
			if(typeof aj.statusId!='undefined')
			aj.statusId.innerHTML = xml_http_loading;
		} else if(aj.XMLHttpRequest.readyState == 4) {
			if(aj.XMLHttpRequest.status == 200) {
				if(typeof aj.statusId!='undefined')
				aj.statusId.style.display = 'none';
				if(aj.recvType == 'HTML') {
					if(typeof aj.resultHandle != 'undefined')
					aj.resultHandle(aj.XMLHttpRequest.responseText);
				} else if(aj.recvType == 'XML') {
					if(typeof aj.resultHandle != 'undefined')
					aj.resultHandle(aj.XMLHttpRequest.responseXML);
				}
			} else {
				if(typeof aj.statusId!='undefined')
				aj.statusId.innerHTML = xml_http_load_failed;
			}
		}
	}

	aj.get = function(targetUrl, resultHandle, sync) {
		aj.targetUrl = targetUrl;
		aj.XMLHttpRequest.onreadystatechange = aj.processHandle;
		aj.resultHandle = resultHandle;
		if(typeof sync == 'undefined') sync=true;
		if(window.XMLHttpRequest) {
			aj.XMLHttpRequest.open('GET', aj.targetUrl, sync);
			aj.XMLHttpRequest.send(null);
		} else {
	        aj.XMLHttpRequest.open("GET", targetUrl, sync);
	        aj.XMLHttpRequest.send();
		}
	}

	aj.post = function(targetUrl, sendString, resultHandle, sync) {
		aj.targetUrl = targetUrl;
		aj.sendString = sendString;
		aj.XMLHttpRequest.onreadystatechange = aj.processHandle;
		aj.resultHandle = resultHandle;
		if(typeof sync == 'undefined') sync=true;
		aj.XMLHttpRequest.open('POST', targetUrl, sync);
		aj.XMLHttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		aj.XMLHttpRequest.send(aj.sendString);
	}
	return aj;
}
