﻿/******************************************************
 * Name: Josh Sorem's AJAX Library
 * Version: 0.3
 * Date: December 21th, 2006
 * Author: Josh Sorem
 * Notice: For free use and redistribution with this
 *		   notice attached.
 * Copyright 2006-2007.  All rights reserved.
 ******************************************************/


/******   USAGE   ******
There are two ways to use this library - synchronously and asynchronously.  

Synchronously:
==============================================	
// option 1
alert(Ajax.get("http://theDomain/thefile.ext").text);

// option 2
var response = Ajax.get("http://theDomain/thefile.ext");
if(response.status == 200)
{
	alert(response.text);
}
==============================================

Asynchronously:
==============================================
function myCallbackHandler(response)
{
	if(response.status == 200)
	{
		alert(response.text);
	}
	else
	{
		alert("Error: " + response.status + " " + response.statusText);
	}
}
	
Ajax.get("http://theDomain/thefile.ext", myCallbackHandler);
==============================================

To POST data, make sure that the data is in the following format: key1=value1&key2=value2
==============================================
var data = "key1=value1&key2=value2";
Ajax.post("http://theDomain/thefile.ext", data, myCallback);
==============================================

********************/



// The main Ajax Object. 
var Ajax = new AjaxRequest();

function AjaxRequest()
{
	// Private - This is the XMLHttpRequest object.
	var _httpRequest = GetHttpRequestObject();

	// Public - This method sends a GET request to a specified URL.
	// If you specify a callback function, the request will go
	// out asynchronously and your function will be called
	// when the request has completed.  Otherwise, the script
	// will wait for the response and return the Response object.
	this.get = function(url, callback)
	{
		_httpRequest = GetHttpRequestObject();
		if(_httpRequest)
		{
			if(callback)
			{
				_httpRequest.onreadystatechange = function() { HandleAjaxRequest(callback); };
				_httpRequest.open("GET", url, true);
				_httpRequest.send("");
			}
			else
			{
				_httpRequest.open("GET", url, false);
				_httpRequest.send("");
				return new AjaxResponse();
			}
		}
	}

	// Public - This method sends a POST request to a specified URL with
	// the specified data.  If you specify a callback function, the
	// request will go out asynchronously and your function will be
	// called when the request has completed.  Otherwise, the script
	// will wait for the response and return the Response object.
	this.post = function(url, data, callback)
	{
		_httpRequest = GetHttpRequestObject();
		if(_httpRequest)
		{
			if(callback)
			{
				_httpRequest.onreadystatechange = function() { HandleAjaxRequest(callback); };
				_httpRequest.open("POST", url, true);
				_httpRequest.setRequestHeader("Content-Type", "application/x-www-urlencoded");
				_httpRequest.setRequestHeader("Content-Length", data.length);
				_httpRequest.setRequestHeader("Connection", "close");
				_httpRequest.send(data);
			}
			else
			{
				_httpRequest.open("POST", url, false);
				_httpRequest.setRequestHeader("Content-Type", "application/x-www-urlencoded");
				_httpRequest.setRequestHeader("Content-Length", data.length);
				_httpRequest.setRequestHeader("Connection", "close");
				_httpRequest.send(data);
				return new AjaxResponse();
			}
		}
	}

	// Public - This method sends a HEAD request to a specified
	// URL.  If you specify a callback function, the request 
	// will go out asynchronously and your function will be 
	// called when the request has compelted. Otherwise the script 
	// will wait for the response and return the Response object.
	this.head = function(url, callback)
	{
		_httpRequest = GetHttpRequestObject();
		if(_httpRequest)
		{
			if(callback)
			{
				_httpRequest.onreadystatechange = function() { HandleAjaxRequest(callback); };
				_httpRequest.open("HEAD", url, true);
				_httpRequest.send("");
			}
			else
			{
				_httpRequest.open("HEAD", url, false);
				_httpRequest.send("");
				return new AjaxResponse();
			}
		}
	}

	// Private - This is the Response object.  It's properties, as listed below, 
	// are "text" and "XML" which are the response data from the
	// server (in text and xml formats respectively) and "status" and
	// "statusText" which are the HTTP Status Code and HTTP Status Message
	// from the server's response and, finally, the "headers" which are the
	// Response headers as a hashtable.
	function AjaxResponse()
	{
		this.text		= _httpRequest.responseText;
		this.XML		= _httpRequest.responseXML;
		this.status		= _httpRequest.status;
		this.statusText	= _httpRequest.statusText;			
		this.headers	= GetHeaders(_httpRequest);
	}

	// Private - This function parses the response headers as they come from
	// the server and returns a Hashtable object.
	function GetHeaders(HttpRequest)
	{
		var headers = HttpRequest.getAllResponseHeaders();
		if(headers == null)
		{
			return "";
		}
		headers = headers.split("\n");
		var count = headers.length;
		var header;

		var table = new Object();
    
		for(var i = 0; i < count; i++)
		{
			header = headers[i].split(": ");
			if(header && header.length == 2)
			{
				table[header[0]] = header[1];
			}
		}
		return table;
	}

	// Private - This is the handler which listens for the readystatechange event
	// of the XMLHttpRequest object.  This handler waits for the request 
	// to end and then sends the AjaxResponse object to your callback function.
	function HandleAjaxRequest(callback)
	{
		if(_httpRequest.readyState == 4)
		{
			callback(new AjaxResponse());
		}
	}

	// Private - This function determines which method to use to set up the 
	// XMLHttpRequest.  Should be cross-browser compatible.
	function GetHttpRequestObject()
	{	
		var object = false;

		if(window.XMLHttpRequest)
		{
			object = new XMLHttpRequest();
		}
		else if(window.ActiveXObject)
		{
			try
			{
				object = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e)
			{
				try
				{
					object = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch(e)
				{
				}
			}
		}
		return object;
	}
}
