/**
 * Copyright (c) 2008, Culnou. All rights reserved.
 * http://www.culnou.com/license.txt
 * version: 0.0.1
 * 
 * XML HTTP request wrapper.
 * 
 * @author tsakura
 */
{

	function XMLRequest(url, params, method, async, username, password) {
		this.url = url;
		this.params = params;
		this.method = method || 'POST';
		this.async = (async != null) ? async : true;
		this.username = username;
		this.password = password;
	}

	/**
	 * Returns true if the response is ready.
	 */
	XMLRequest.prototype.isReady = function() {
		return this.request.readyState == 4;
	}

	/**
	 * Returns the response as an XML document.
	 */
	XMLRequest.prototype.getXML = function() {
		var xml = this.request.responseXML;
			
		// Handles missing response headers in IE
		if (xml == null || xml.documentElement == null) {
			xml = Utils.parseXML(this.request.responseText);
		}
		return xml;
	}

	/**
	 *
	 */
	XMLRequest.prototype.create = function() {
		var req = null;
		if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
		} else if (typeof(ActiveXObject) != "undefined"){
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		return req;
	}
	
	/**
	 *
	 */
	XMLRequest.prototype.send = function(onload, onerror) {
		this.request = this.create();
		if (this.request != null) {
			var self = this; // closure
			this.request.onreadystatechange = function() {
				if (self.isReady()) {
					if (onload != null) {
						onload(self);
					}
				}
			}
			this.request.open(this.method, this.url, this.async, this.username, this.password);
			//this.request.overrideMimeType('text/plain; charset=ISO-8859-1');
			if (this.params != null) {
				this.request.setRequestHeader('Content-Type',
					'application/x-www-form-urlencoded');
			}
			this.request.send(this.params);
		}
	}

}

