	/**
	 * Javascript AJAX Functions
	 * 
	 * @name ajax
	 * @author cjoudrey
	 * @package reptiletech
	 */
	 
	var AJAX = {
		
		XMLHttpRequest: function()
		{
			var req = false;
			
			if(window.XMLHttpRequest && !(window.ActiveXObject))
			{
				try
				{
					req = new XMLHttpRequest();
				}
				catch(e)
				{
					req = false;
				}
	
			}
			else if(window.ActiveXObject)
			{
				try
				{
					req = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(e)
				{
					try
					{
						req = new ActiveXObject("Microsoft.XMLHTTP");
					}
					catch(e)
					{
						req = false;
					}
				}
			}
			
			return(req);
		},
		
		FormParams: function(form)
		{
			var params = [];
			
			for(elId = 0; elId < form.length; elId++)
			{
				switch (form[elId].type)
				{
					case 'radio':
					
						if (form[elId].checked)
						{
							params.push(form[elId].name + '=' + form[elId].value);
						}
						break;
						
					case 'button':
						break;
						
					default:
						params.push(form[elId].name + '=' + form[elId].value);
						break;
				}
			}
			
			return params;
		},
		
		Response: function(xmlobject)
		{
			this.status = xmlobject.status;
			this.statusText = xmlobject.statusText;
			this.responseText = xmlobject.responseText;
			this.responseXML = xmlobject.responseXML;

			if (xmlobject.getResponseHeader('content-type').indexOf('application/json') != -1)
			{
				this.responseJSON = JSON.parse(xmlobject.responseText);
			}
			
			this.nodeValue = function(nodeName, nodeRoot)
			{
				node = this.node(nodeName, nodeRoot);
			
				if (node.length == 0)
				{
					return "";
				}
			
				if (typeof(node) != "undefined")
				{
					return(node[0].firstChild.nodeValue);
				}
				else
				{
					return(this.responseXML.getElementsByTagName(nodeName).item(0).textContent);
				}
			};
			
			this.nodeValues = function(nodeNames, nodeRoot)
			{
				returnObj = {};
				
				for(nodeNameId in nodeNames)
				{
					returnObj[nodeNames[nodeNameId]] = this.nodeValue(nodeNames[nodeNameId], nodeRoot);
				}
				
				return returnObj;
			};
			
			this.nodeAllValues = function(nodeRoot)
			{
				var elChildNodes = nodeRoot.childNodes;
				
				var elObj = {};
				
				for(elChildId = 0; elChildId < elChildNodes.length; elChildId++)
				{
					if (typeof(elChildNodes[elChildId].childNodes[0]) != "undefined" && typeof(elChildNodes[elChildId].childNodes[0].tagName) != "undefined")
					{
						elObj[elChildNodes[elChildId].tagName] = '<' + elChildNodes[elChildId].childNodes[0].tagName + '>' + elChildNodes[elChildId].textContent + '</' + elChildNodes[elChildId].childNodes[0].tagName + '>';
					}
					else
					{
						elObj[elChildNodes[elChildId].tagName] = elChildNodes[elChildId].textContent;
					}
				}
				
				return elObj;
			};
			
			this.node = function(nodeName, nodeRoot)
			{
				if (typeof(nodeRoot) == "undefined")
				{
					return this.responseXML.getElementsByTagName(nodeName);
				}
				else
				{
					return nodeRoot.getElementsByTagName(nodeName);
				}
			};
		},
		
		Request: function(url, options)
		{
			if (!isset(url))
			{
				return(false);
			}
			
			this.url = url;
			this.method = (isset(options.method)) ? options.method : 'GET';
			this.asynchronous = (isset(options.asynchronous) && typeof(options.asynchronous) == "boolean") ? options.asynchronous : true;
			this.contentType = (isset(options.contentType)) ? options.contentType : 'application/x-www-form-urlencoded';
			this.parameters = (isset(options.parameters)) ? options.parameters : '';

			if (typeof(this.parameters.join) != "undefined")
			{
				this.parameters = this.parameters.join("&");
			}
			
			this.encoding = (isset(options.encoding)) ? options.encoding : 'utf-8';
			
			this.callbacks = {};
			
			this.dispatch = function(event, parameter)
			{
				if (isset(this.callbacks[event]))
				{
					if (isset(parameter))
					{
						this.callbacks[event](parameter);
					}
					else
					{
						this.callbacks[event]();
					}
				}
			};
			
			this.handler = function()
			{
				switch(this.object.readyState)
				{
					case 3:
						this.dispatch('onLoading');
						break;
					
					case 4:
						this.dispatch('onComplete', new AJAX.Response(this.object));
						break;
				}
			};
			
			if (isset(options.onComplete))
			{
				this.callbacks['onComplete'] = options.onComplete;
			}
			
			if (isset(options.onFailure))
			{
				this.callbacks['onFailure'] = options.onFailure;
			}
						
			this.object = new AJAX.XMLHttpRequest();
			
			if (!this.object)
			{
				this.dispatch('onFailure');
				return false;
			}
			
			if (this.asynchronous)
			{
				this.object.onreadystatechange = this.handler.bindAsEventListener(this);
			}
			
			if (this.method == "GET")
			{
				if (this.parameters  != '')
				{
					this.object.open(this.method, this.url + "?" + this.parameters, this.asynchronous);
				}
				else
				{
					this.object.open(this.method, this.url, this.asynchronous);					
				}
			}
			else
			{
				this.object.open(this.method, this.url, this.asynchronous);
				
			}
			
			if (isset(options.json))
			{
				this.object.setRequestHeader("Output", "text/json");
			}
			else if (isset(options.xml))
			{
				this.object.setRequestHeader("Output", "text/xml");
			}

			this.object.setRequestHeader("Content-Encoding", this.encoding);
			this.object.setRequestHeader("Content-Type", this.contentType);
			
			if (this.method == "GET")
			{
				this.object.send("");
			}
			else
			{
				this.object.send(this.parameters);
			}
			
			if (!this.asynchronous)
			{
				this.response = new AJAX.Response(this.object);
			}
		}	
	};
	
	function isset(check)
	{
		return(typeof(check) != "undefined");
	};