/*
	sh_object.js
	Global sh object
	copyright Copyright (c) 2007, Sen Haerens
*/

if (!sh)
{
	var sh = new Object();
}

// XML
sh.xml = new Object();

// XML HTTP request
sh.xml.request = function()
{
	this.url = null;
	this.asyncFlag = true;
	this.req = false;
	
	// Initialize object
	this.initialize = function()
	{
		// Branch for native XMLHttpRequest object
		if (window.XMLHttpRequest && !(window.ActiveXObject))
		{
			try
			{
				this.req = new XMLHttpRequest();
			}
			catch(e)
			{
				this.req = false;
			}
		}
		
		// Branch for IE/Windows ActiveX version
		else if(window.ActiveXObject)
		{
			try
			{
				this.req = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e) 
			{
				try
				{
					this.req = new ActiveXObject("Microsoft.XMLHTTP");
				}
				
				catch(e)
				{
					this.req = false;
				}
			}
		}
	}

	// Event handler that process request changes
	this.processReqChange = function()
	{
		this.debug('readyState: ' + this.req.readyState);

		if (this.req.readyState == 4)
		{
			this.debug('status: ' + this.req.status + ' | ' + this.req.statusText);

			if (this.req.status == 200)
			{
				if (this.onComplete)
				{
					this.debug('complete: succes');
					this.onComplete(this.req.responseText, this.req.responseXML);
				}
			}
		}
	}
	
	// Debug logger
	this.debug = function(value)
	{
		return sh.debug('sh.xml.request.' + value);
	}

	// Empty event handler for completed requests
	this.onComplete = function(responseText, responseXML) {};
}

sh.xml.request.prototype.submit = function()
{
	if (this.url != null)
	{
		var self = this;
		
		this.initialize();
		this.req.onreadystatechange = function() { self.processReqChange(); };
		this.req.open('GET', this.url, this.asyncFlag);

		this.debug('submit: ' + this.url);
		this.req.send(null);
	}
}

// Unserialize XML
sh.xml.unserialize = function(string)
{
	var domParser = new DOMParser();
	return domParser.parseFromString(string, 'application/xml');
}

// Remove all children from a node
sh.xml.removeChildren = function(node)
{
	while (node.hasChildNodes())
	{
		node.removeChild(node.firstChild);
	}
}

// Display output
sh.debug = function(value)
{
	if (sh.debug.set == true)
	{
		return alert(value);
	}
}


