
function URLLoader (target, callback) {
	
	this.target = (typeof target == "object") ? target : document.getElementById(target);
	
	this.callback = new Object();
	for (var items in callback) this.callback[items] = callback[items];
	if (this.callback.args == undefined) this.callback.args = new Array();
	if (this.callback.scope == undefined) this.callback.scope = null;
	
	this.arguments = Array.prototype.slice.call(arguments);
	if (this.arguments.length > 2) this.callback.args = this.arguments.splice(2, this.arguments.length);
	
	//	Test if page is loaded from local file system
	this.local = new RegExp(/[file:]|[C:]/i).test(document.location.toString());
	
	//	Try to create XMLHttpRequest object
	try {
		this.XHR = new XMLHttpRequest();
		this.XHR.target = this.target;
		this.XHR.callback = this.callback;
	} catch (error) {
		try {
			this.XHR = new ActiveXObject("Msxml2.XMLHTTP.6.0");
		} catch (error) {
			try {
				this.XHR = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (error) {
				//alert("XMLHttpRequest not supported");
			}
		}
	}
	
}

Class(URLLoader);

URLLoader.prototype.arguments = new Array();
URLLoader.prototype.append = false; 
URLLoader.prototype.callback = {args: new Array(), name: undefined, scope: null};
URLLoader.prototype.request = {method: "GET", url: undefined};

URLLoader.prototype.load = function (URLRequest) {
	
	// Class instance
	var classPointer = this;
	
	if (URLRequest instanceof String) this.request.url = URLRequest;
	else this.request = URLRequest;
	
	if (this.request.url != undefined) {
		// File to load
		this.XHR.open(this.request.method, this.request.url);
		
		// Listen for state change
		this.XHR.onreadystatechange = function () {
			classPointer.httpStatus(classPointer.XHR);
		}
		
		this.setAllRequestHeaders();
		
		this.XHR.send("");
	}
	
}

URLLoader.prototype.setAllRequestHeaders = function () {
	for (var items = 0; items < this.request.requestHeaders.length; items++) {
		this.XHR.setRequestHeader(this.request.requestHeaders[items].name, this.request.requestHeaders[items].value);
	}
}

URLLoader.prototype.httpStatus = function (XMLRef) {
	
	if (XMLRef.readyState == 4) {
		
		if (XMLRef.status == 200 || this.local) {
			
			// XMLRef.target: This will be used by FireFox, Mozilla, etc.
			// this.target: This will be used by IE5 and above
			var container = (XMLRef.target != undefined) ? XMLRef.target : this.target;
			var callback = (XMLRef.callback != undefined) ? XMLRef.callback : this.callback;
			
			if (container != undefined) {
				
				if (container.innerHTML == undefined) {
					container.text = XMLRef.responseText;
					
					try {
						var xml = new ActiveXObject("Microsoft.XMLDOM");
						//var xml = new ActiveXObject("Msxml2.DOMDocument.3.0");
						xml.loadXML(XMLRef.responseText.toString());
					} catch (error) {
						try {
							var xml = XMLRef.responseXML;
						} catch (error) {
							//alert("XML not supported");
						}
					}
					
					container.xml = xml;
					
				} else {
					container.innerHTML = (container.innerHTML == "" || !this.append) ? XMLRef.responseText : container.innerHTML + XMLRef.responseText;
				}
				
			}
			
			if (callback.name != undefined) {
				callback.name.apply(callback.scope, callback.args);
			}
			
		}
		
	}
	
}

