/**
 *
 * Base class for AJAX XmlHttpRequest
 * @class Base class for an XmlHttpRequest
 * <p>Usage: new W.Request('GET', 'xhr/template.inc.php').call(obj.template.bind(obj));</p>
 * <p>Usage: new W.Request('GET', 'xhr/template.inc.php').load('content');</p>
 * @requires W
 * @requires W.Dom
 * @constructor
 * @param {String}		method		post or get
 * @param {String} 		url 		URL to use for the request
 * @param {Function}	callback 	Reference to the function to call when the request is complete
 * @param {String}		post		post vars
 */

W.Request = function(method, url, post)
{
	this.init(method, url, post);
};

W.Request.prototype = {


	request		: null,
	callback	: null,
	post		: null,
	
	
	init : function(method, url, post)
	{
		var obj			= this;
		
		this.request	= this.xmlhttp();
		
		if (!this.request) { return false; }
		
		this.post		= post || this.post;
		
		//var t = new Date().getTime();
		//url += '&t=' + t;
				
		this.request.open(method, url, true);
		
		if (method.toUpperCase() == 'POST') { this.request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); }
		
		this.request.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT');
		this.request.onreadystatechange = function() { obj.update(this.request); };
	},
	
	
	
	send : function()
	{
		this.request.send(this.post);
	},


	xmlhttp : function()
	{
		var xmlreq = false;

  		if (window.XMLHttpRequest) {
			xmlreq = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			try {
				xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e1) {
				try {
					xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e2) {
        			// Unable to create an XMLHttpRequest with ActiveX
        			return false;
      			}
    		}
 		 }

		return xmlreq;
	},
	
	
	update : function()
	{
		if (this.request.readyState == 4) { 
			if (this.callback) { this.callback(); }
		}
	},
	
	
	call : function(fn)
	{
		var obj	= this;
		
		this.callback = function() { fn(obj.request); };
		
		this.send();
	},
	
	
	load : function(id)
	{
		var obj = this;
		
		this.callback = function() { W.$(id).innerHTML = obj.request.responseText; };
		
		this.send();
	}
};

