/*
    Filename: HttpClient.js
	Purpose: A standard XMLHttpRequest Wrapper used for AJAX
	Author: Brenley Dueck
	Email: brenelz@redtigergroup.com
	Last Modifid: July 2, 2008
*/

function HttpClient() {}
HttpClient.prototype = {
	
	// type of request (GET or POST)
	requestType: 'GET',
	
	// type of a response you expect (TEXT, XML, or JSON)
	responseType: 'TEXT',
	
	// if the call is asynchronous or not
	isAsync: false,
	
	// where the actual XMLHttpRequest instance is stored
	xmlhttp: false,
	
	// what is called when a successful request is made
	callback: false,
	
	// called when data loading is in progress
	onSend:function()
	{
		var e = document.getElementById('HttpClientStatus');
		if( e != null)
			e.style.display = 'block';
	},
	
	// called when data is completely loaded
	onLoad:function()
	{
		var e = document.getElementById('HttpClientStatus');
		if( e != null)
			e.style.display = 'none';
	},
	
	// called when an http error occurs
	onError:function(error)
	{
		alert(error);
	},
	
	// method to initialize an xmlhttpclient
	init:function()
	{
		try
		{
			// mozilla, safari, opera, and IE7
			this.xmlhttp = new XMLHttpRequest();
		}
		catch(e)
		{
			// IE ActiveXObject array
			var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.5.0',
			                            'MSXML2.XMLHTTP.4.0',
										'MSXML2.XMLHTTP.3.0',
										'MSXML2.XMLHTTP',
										'Microsoft.XMLHTTP'
										);
			
			// loop through each ActiveXObject to initialize in IE
			var success = false;
			for (var i=0; i < XMLHTTP_IDS.length && !success; i++)
			{
				try
				{
					this.xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]);
					success = true;
				}
				catch(e){}
			}
			
			// report an error if nothing is initialized
			if(!success)
			{
				this.onError('Unable to create XMLHttpRequest.');
			}
		}
	},
	
	// method to make a page request
	makeRequest:function(url, payload)
	{
		// if xmlhttp is not initialized yet do so
		if(!this.xmlhttp)
		{
			this.init();
		}
		
		// setup what we want to get from the server
		this.xmlhttp.open(this.requestType, url, this.isAsync);
		
		// set onreadystate change here since it will be
		// reset after a completed call in mozilla
		var self = this;
		this.xmlhttp.onreadystatechange = function()
		{
			self._readyStateChangeCallback();
		};
		
		// mimic an actual POST request by setting the appropriate header
		if( this.requestType == 'POST' )
			this.xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded;charset=UTF-8;');
		
		// make connection to url specified with open
		// and send data to the server
		this.xmlhttp.send(payload);
		
		// synchronous call is made
		if(!this.isAsync)
		{
			// detection for TEXT, XML, and JSON types
			if(this.responseType == 'TEXT')
			{
				return this.xmlhttp.responseText;
			}
			else if(this.responseType == 'XML')
			{
				return this.xmlhttp.responseXML;
			}
			else if(this.responseType == 'JSON')
			{
				return this.xmlhttp.responseText;
			}
			else
			{
				this.onError('Invalid response type');
			}
		}
	},
	
	// internal method used to handle ready state changes
	_readyStateChangeCallback:function()
	{
		switch(this.xmlhttp.readyState)
		{
			// send method called but data isn't available
			case 2:
			  this.onSend();
			  break;
			
			// all data has been received
			case 4:
			  this.onLoad();
			  
			  // if xmlhttp status is OK
			  if(this.xmlhttp.status == 200)
			  {
				// detection for TEXT, XML, and JSON types
				if(this.responseType == 'TEXT')
				{
					this.callback(this.xmlhttp.responseText);
				}
				else if(this.responseType == 'XML')
				{
					this.callback(this.xmlhttp.responseXML);
				}
				else if(this.responseType == 'JSON')
				{
					var result = this.xmlhttp.responseText;
					this.callback(result.parseJSON());
				}
				else
				{
					this.onError('Invalid response type');
				}
			  }
			  // report error if xmlhttp status is not OK
			  else
			  {
			  	this.onError('HTTP Error Making Request: ' +
				'['+this.xmlhttp.status+'] '+this.xmlhttp.statusText);
			  }
			  break;
		}	
	}
};


