
// Basic AJAX data pass class (per SitePoint book)
function MPBasicAjax()
	{
	this.req = null;
	this.url = null;
	this.method = 'GET';
	this.async = true;
	this.status = null;
	this.statusText = '';
	this.postData = null;
	this.readyState = null; // 0:uninitialized, 1:loading, 2:loaded, 3:interactive, 4:complete
	this.handleResp = null;
	this.responseFormat = 'text'; // text, xml or object
	this.forwardData = new Object();
	this.init = function()
		{
		if (!this.req)
			{
			try {
				// Firefox, Safari, IE7
				this.req = new XMLHttpRequest();
				} catch(e) {
				try {
					// IE 6
					this.req = new ActiveXObject('MSXML2.XMLHTTP');
					} catch(e) {
					try {
						// old IE
						this.req = new ActiveXObject('Microsoft.XMLHTTP');
						} catch(e) {
						// no XMLHttpRequest object
						return false;
						}
					}
				}
			}
		return this.req;
		}
	this.doReq = function()
		{
		if (!this.init())
			{
			alert('Could not create XMLHttpRequest object.');
			return;
			}
		this.req.open(this.method, this.url, this.async);
		if (this.method == 'POST')
			{
			this.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			}
		var self = this; // fix loss of scope in inner function
		this.req.onreadystatechange = function()
			{
			if (self.req.readyState == 4)
				{
				// handle response
				switch (self.responseFormat)
					{
					case 'text':
						resp = self.req.responseText;
						break;
					case 'xml':
						resp = self.req.responseXML;
						break;
					case 'object':
						resp = req;
						break;
					}
				if (self.req.status >= 200 && self.req.status <= 299)
					{
					self.handleResp(resp, self.forwardData);
					} else {
					self.handleErr(resp);
					}
				}
			}
		this.req.send(this.postData);
		}
	this.handleErr = function(resp)
		{
		alert("AJAX ERROR");
		// alert("AJAX ERROR:\n\n"+resp);
		}
	this.abort = function()
		{
		if (this.req)
			{
			this.req.onreadystatechange = function() { };
			this.req.abort();
			this.req = null;
			}
		}
	this.doGet = function (url, hand, format)
		{
		this.url = url;
		this.handleResp = hand;
		this.responseFormat = format || 'text';
		this.doReq();
		}
	this.doPost = function (url, postData, hand, format)
		{
		this.url = url;
		this.handleResp = hand;
		this.responseFormat = format || 'text';
		this.method = 'POST';
		var queryData = new Array();
		for (x in postData)
			{
			if (typeof postData[x] != "function")
				queryData.push(x+"="+escape(postData[x]));
			}
		this.postData = queryData.join('&');
		this.doReq();
		}
	}