/*======================================================================
 * SimpleXML(body)
 *
 * The class constructor.
 *======================================================================
*/
function SimpleXML(body)
{
	this.body = body;
}

/*======================================================================
 * getNodeGroup(group)
 *
 * Returns a list of elements by name.
 *======================================================================
*/
SimpleXML.prototype.getNodeGroup = function(group)
{
	return this.body.getElementsByTagName(group);
}

/*======================================================================
 * getNodeValue(field,node)
 *
 * Given a field and a node, return the value of that node. This is only
 * intended for nodes that have single depth, single value, no children
 * result sets.
 *======================================================================
*/
SimpleXML.prototype.getNodeValue = function(field,node)
{
	return node.getElementsByTagName(field)[0].childNodes[0].nodeValue;
}

/*======================================================================
 * getAttribut(field,node)
 *
 * returns the attribute value for a field.
 *======================================================================
*/
SimpleXML.prototype.getAttribute = function(field,node)
{
	return node.getAttribute(field);
}

/*======================================================================
 * SimpleAjax()
 *
 * The class constructor. This object assumes you will be sending
 * messages to the server and getting an XML document as a response.
 *======================================================================
*/
function SimpleAJAX()
{
	this.params = Array();
	var me      = this;

	/*==================================================================
	 * handleReadyState()
	 *
	 * This is a callback to handle the responce from the server. Since
	 * it is a callback it needs to be defined differently using 
	 * closures (i.e., it is a "variable" actually attached to the object 
	 * not just a definition for the object).     
	 *==================================================================
	*/
	this.handleReadyState = function()
	{
		if ( me.getHTTP().readyState == 4 ) {
			me.responseXML = new SimpleXML(me.getHTTP().responseXML.documentElement);
			var handler    = me.getHandler();
			handler(me.responseXML);
		}
	}
}

/*======================================================================
 * setAction(action)
 *
 * Set where the object sends its data to.
 *======================================================================
*/
SimpleAJAX.prototype.setAction = function(action)
{
	this.action = action;
}

/*======================================================================
 * setMethod(method)
 *
 * Set how the object sends its data (POST or GET).
 *======================================================================
*/
SimpleAJAX.prototype.setMethod = function(method)
{
	this.method = method;
}

/*======================================================================
 * getResponseXML()
 *
 * Get the server response.
 *======================================================================
*/
SimpleAJAX.prototype.getResponseXML = function()
{
	return this.responseXML;
}

/*======================================================================
 * getAction()
 *
 * Get where the object sends its data to.
 *======================================================================
*/
SimpleAJAX.prototype.getAction = function()
{
	return this.action;
}

/*======================================================================
 * getMethod()
 *
 * Get how the object sends its data (POST or GET).
 *======================================================================
*/
SimpleAJAX.prototype.getMethod = function()
{
	return this.method;
}

/*======================================================================
 * getHTTP()
 *
 * Returns the HTTP object.
 *======================================================================
*/
SimpleAJAX.prototype.getHTTP = function()
{
	if ( ! this.http ) this.setXMLHttpRequestObject();    
	return this.http;
}

/*======================================================================
 * addParameter(key,value)
 *
 * Adds a (key,value) pair to send to its target.
 *======================================================================
*/
SimpleAJAX.prototype.addParameter = function(key,value)
{
	var i               = this.params.length;
	this.params[i]      = new Array();
	this.params[i][key] = value;		
}

/*======================================================================
 * setXMLHttpRequestObject
 *
 * Sets the objects require object to talk to the server with.
 *======================================================================
*/
SimpleAJAX.prototype.setXMLHttpRequestObject = function()
{
	if ( window.XMLHttpRequest ) 
		this.http = new XMLHttpRequest();
	else if (window.ActiveXObject) 
		this.http = new ActiveXObject("Microsoft.XMLHTTP");
}

/*======================================================================
 * setCustomHandler(handler)
 *
 * If you want to manually control what the object does once it sends
 * the request you can control that with this.
 *======================================================================
*/
SimpleAJAX.prototype.setHandler = function(handler)
{
	this.handler = handler;
}

/*======================================================================
 * getCustomHandler()
 *
 * Return any custom handler.
 *======================================================================
*/
SimpleAJAX.prototype.getHandler = function()
{
	return this.handler;
}

/*======================================================================
 * send()
 *
 * Sends the HTTP Request.
 *======================================================================
*/
SimpleAJAX.prototype.send = function()
{
	var url       = this.getAction();
	var query_str = '';
	for ( var i = 0; i < this.params.length; i++ ) {
		for ( key in this.params[i] ) {
			var value = escape(this.params[i][key]);
			var div   = ( query_str != '' ) ? '&' : ''; // What divides this val from the last?
			query_str += div + key + '=' + value;
		}
	}
	this.getHTTP().onreadystatechange = this.handleReadyState;
	if ( this.getMethod() == 'POST' ) {
		this.getHTTP().open(this.getMethod(), url, true);
		this.getHTTP().setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		this.getHTTP().send(query_str);	
	} else {
		this.getHTTP().open(this.getMethod(), url+'?'+query_str, true);
		this.getHTTP().setRequestHeader("Content-Type", "text/xml");
		this.getHTTP().send(null);
	}
}