/**
* class HTTPRequest
* Wrapper for http requests
*
* @author Flash
*/

//global variable to hold HTTPRequest objects as they need to access themselves globally
HTTPRequest._self = {};

/**
* Constructor
*
* @return HTTPRequest
*/
function HTTPRequest() {
	//add mylelf to array
	this.id = HTTPRequest._self.length;
	HTTPRequest._self[this.id] = this;
	
	this.timeout = 0; //in milliseconds
	this._timeout;
	this.onupdate;
	this._headers = {};
	
	//the request object
	this._request;
}

/**
* Sends an http request to the given url
*
* @param string url			Url to call
* @param string content		Content to pass to the url
*/
HTTPRequest.prototype.send = function(url, content) {
	//we aren't allowed to override a current request
	if (this.inProgress()) {
		return false;
	}
	
	this._success = false;
	
	//need to create a new object
	if (! this._generateRequest()) {
		return false;
	}
	
	this._request.open('GET', url, true);

	//add any headers that were set earlier this morning...
	for (name in this._headers) {
		this._request.setRequestheader(name, this.headers[name]);
	}
	
	//add timeout call if one it set
	if (this.timeout > 0) {
		this._timeout = setTimeout("HTTPRequest._self["+this.id+"]._ontimeout()", this.timeout);
	}

	this._request.send(content);

	return true;
}

/**
* Set a header to pass, will overwrite any previous header of same name
* Note: haven't really tested this, looks good though ;)
*
* @param string name		
* @param string value
*/
HTTPRequest.prototype.setHeader = function(name, value) {
	this._headers.push[name] = value;
}

/**
* Get a response header, must be done after successful reponse
* is retrieved
* Note: This is also experimental
*
* @param string name		Name of the header to retrieve
* @return string			The value of the header, or false is can't be retrieved
*/
HTTPRequest.prototype.getHeader = function(name) {
	if (this._success) {
		return this._request.getResponseHeader(name);
	}
	
	return false;
}

/**
* Kill the current request
*
* @param boolean fromCallback		Whether called from the callback function
*/
HTTPRequest.prototype.abort = function(fromCallback) {
	if (this._request) {
		//don't call abort from the callback function as this
		//start an infinite loop
		if (! fromCallback) {
			this._request.abort();
		}

		this._request = null;
		this._success = false;
	}
}

/**
* Gets the response as parsed XML objects
*
* @return object	The xml parsed into objects
*/
HTTPRequest.prototype.getResponseXML = function() {
	if (this._success) {
		return this._request.responseXML;
	}
}

/**
* Gets the response as text
*
* @return string	The raw response
*/
HTTPRequest.prototype.getResponseText = function() {
	if (this._success) {
		return this._request.responseText;
	} 
}

/**
* Returns whether there is already a current request in progress
*
* @return boolean		Whether request is in progress
*/
HTTPRequest.prototype.inProgress = function() {
	return this._request ? true : false;
}

/**
* Tries to create the HTTPRequest object 
*
* @return boolean	Whether the object was created successfully
* @access private
*/
HTTPRequest.prototype._generateRequest = function() {
	var self = this;
	var request;

	//this good way of doing things
	if (window.XMLHttpRequest) {
		request = new XMLHttpRequest();
	}
	//the m$ way of doing things...
	else if (window.ActiveXObject) {
		try	{
			request = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch (e) {
			try {
				request = new ActiveXObject("Msxml2.XMLHTTP");
			} 
			catch (E) {}
		}
	}
	
	if (! request) {
		this._request = null;
		this._success = false;
	}
	else
	{
		this._request = request;
		this._request.onreadystatechange = function()	{
			self._onreadystatechange();
		}
	}
	
	return this._request ? true : false;
}

/**
* Handle request timeouts
* 
* @access private
*/
HTTPRequest.prototype._ontimeout = function() {
	clearTimeout(this._timeout);
	
	if (! this._request) {
		return false;
	}
	
	this.abort();
	
	//notify caller error has occured
	if (this.onupdate) {
		this.onupdate("Request has timed out");
	}
}

/**
* Handle the retrieving of the information
* 
* @access private
*/
HTTPRequest.prototype._onreadystatechange = function() {
	var error = null;

    //check if loaded
    if (this._request.readyState == 4) {
    	clearTimeout(this._timeout);

    	//sometimes get a wierd error in FF, debug later
    	try {
	        //check if everything went ok
	        if (this._request.status == 200) {
	        	this._success = true;
	        }
	        else {
	            error = this._request.statusText;
	        }
	        
			if (this.onupdate) {
				this.onupdate(error);
			}
    	}
    	catch(e){}
    }
}
