/*
	[EasyJForum] (C)2008 Hongshee Soft.
	$file: ajax.js $
	$Date: 2008-04-08 $
*/

function Ajax() 
{
	var aj = new Object();
	aj.resultHandle = null;
	aj.targetUrl = '';
	aj.XMLHttpRequest = createXMLHttpRequest();
	
	aj.processHandle = function() {
		if(aj.XMLHttpRequest.readyState == 4 && aj.XMLHttpRequest.status == 200) {
			if (aj.resultHandle != null){
				if (aj.XMLHttpRequest.responseXML)
					aj.resultHandle(aj.XMLHttpRequest.responseXML.lastChild.firstChild.nodeValue, aj);
				else{
					var tmpStr = aj.XMLHttpRequest.responseText;
					var p = tmpStr.indexOf("?>");
					if (p > 0){
						tmpStr = tmpStr.substring(p+2);
						var p1 = tmpStr.indexOf("<");
						var p2 = tmpStr.indexOf(">");
						var nodeName = tmpStr.substring(p1+1, p2);
						tmpStr = tmpStr.substring(p2+1);
						p2 = tmpStr.indexOf("</" + nodeName + ">");
						tmpStr = trim(tmpStr.substring(0, p2));
						p1 = tmpStr.indexOf("<![CDATA[");
						if (p1 == 0){
							p2 = tmpStr.indexOf("]]>");
							if (p2 > 0)
								tmpStr = tmpStr.substring(p1+9, p2);
						}
					}
					aj.resultHandle(tmpStr, aj);
				}
			}
		}
	}

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

	}
	return aj;
}

function createXMLHttpRequest()
{
	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) {}
		}
	}
	return request;
}