// wrapper class to access ajax request functions
// use to support multiple async requests simultaneously

/* EXAMPLE:
		var ajax = new AJAX();
		
		ajax.url = '../ajax/some_url.php';
		ajax.AddParam('name', 'val');
		ajax.ReadyState4 = function(request) {
			// something with request.responseText etc...
			// request parameter is the standard JS ajax response
		}
		
		ajax.Go();
*/



function AJAX() {
	
	var request ;

	// returns a non-used (open) ajax request object 
	function Fetch() {
		return Create();
	}
	
	// returns a new XMLHttpRequest
	function Create() {		

		try {
			request = new XMLHttpRequest();
		} catch (trymicrosoft) {
			try {
				request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (othermicrosoft) {
				try {
					request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (failed) {
					request = false;
				}  
			}
		}

		if (! request) {
			alert("Error initializing XMLHttpRequest!");
		}
			
		return request;
	}
	
		
	// destructively add the parameter n (name) with v (value)
	// POST or GET Flavors
	
	this.AddPOST = function(n,v) {
		pparams[n] = v;	
	}
	
	this.AddGET = function(n,v) {
		params[n] = v;		
	}	
	
	
	// return the current parameter string (GET/POST)
	function ParamString(m) {	
		
		var ap = (m == 'POST') ? pparams : params;
	
		// temp array to store 'n=v' strings and then join
		var ps = new Array();
		for(var n in ap)
			ps.push(n + '=' + encodeURI(ap[n]));
		
		return ps.join('&');
	}
	
	
	// a context maintenance variable to hold the place
	function Callback(_this)  {

		switch(request.readyState) {
			case 1:
				_this.ReadyState1(request);
				break;			
			case 2:
				_this.ReadyState2(request);
				break;			
			case 3:
				_this.ReadyState3(request);
				break;			
			case 4:
				_this.ReadyState4(request);
				break;			
			default:
				alert('Unknown ready state');
				break;
		}
	}
	
	
	this.Go = function(m) {	

		request = Fetch();

		if(this.url == '') {
			e.Add('Blank url for Ajax request');
			return;
		}
		  			

  		// anonymous function to call the prototype function		
		// set the var so we can scope the callback
		var _this = this;
		request.onreadystatechange = function() {
			Callback(_this);

		}

		// Open a connection to the server
		var url = this.url + '?' + ParamString('GET');	


		if(m == 'POST') {

  			request.open("POST", url, true);
  			
			var ps = ParamString('POST');

			request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

			request.setRequestHeader("Content-length", ps.length);
  		// Send the request
  		request.send(ps);

		} else {
			//var url = this.url + '?' + ParamString('GET');				
			var url = this.url

  			request.open("GET", url, true);
			var ps = null; 
  			request.send(null);
			
  		}
  
  		// Send the request
  		

	}
	// these need to be set
	this.url = '';
	
	// an associative array for the url parameters
	// name = value
	var params = new Array();
	// post version of above
	var pparams = new Array();
	
	return this;
}


// OVERLOAD THESE METHODS TO CONTROL THE STATE ACTION
AJAX.prototype.ReadyState1 = function(request) {
}


AJAX.prototype.ReadyState2 = function(request) {
}


AJAX.prototype.ReadyState3 = function(request) {
}


AJAX.prototype.ReadyState4 = function(request) {
}