	/**
	 * 
	 * Javascript AJAX Functions
	 * 
	 * @name ajax
	 * @author C. Joudrey
	 * @since 2006-07-06
	 * @version 2.1.1
	 * @package reptileframework
	 * 
	 * 
	 */
	 
	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);
		},
		
		Response: function(xmlobject)
		{
			this.status = xmlobject.status;
			this.statusText = xmlobject.statusText;
			this.responseText = xmlobject.responseText;
			this.responseXML = xmlobject.responseXML;
		},
		
		Request: function(url, options)
		{
			if (!isset(url))
			{
				return(false);
			}
			
			this.url = url;
			this.method = (isset(options.method)) ? options.method : 'GET';
			this.form = (isset(options.form)) ? options.form : null;
			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("&");
			}
			
			if (this.method == "FILE")
			{
				if (this.form)
				{
					this.form.action = this.url + "?" + this.parameters;
					this.form.method = "POST";
					Ajax_File.submit(this.form, {'onComplete' : options.onComplete});
				}
				
				return;
			}
			
			this.encoding = (isset(options.encoding)) ? options.encoding : 'iso-8859-1';
			
			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.onLoading))
			{
				this.callbacks['onLoading'] = options.onLoading;
			}
			
			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;
			}
	
			this.object.onreadystatechange = this.handler.bind(this);
			
			if (this.method == "GET")
			{
				this.object.open(this.method, this.url + "?" + this.parameters, this.asynchronous);
			}
			else
			{
				this.object.open(this.method, this.url, this.asynchronous);
				
			}
			
			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);
			}
		}
		
	}
	
	var Ajax_File = {

	    frame : function(c) {
	
	        var n = 'f' + Math.floor(Math.random() * 99999);
	        var d = document.createElement('DIV');
	        d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="Ajax_File.loaded(\''+n+'\')"></iframe>';
	        document.body.appendChild(d);
	        var i = document.getElementById(n);
	        
	        if (c && typeof(c.onComplete) == 'function')
	        {
	            i.onComplete = c.onComplete;
	        }
	
	        return n;
	    },
	
	    form : function(f, name) {
	        f.setAttribute('target', name);
	    },
	
	    submit : function(f, c) {
	        Ajax_File.form(f, Ajax_File.frame(c));
	        f.submit();
	    },
	
	    loaded : function(id) {
	        var i = document.getElementById(id);
	        
	        if (i.contentDocument)
	        {
	            var d = i.contentDocument;
	        }
	        else if (i.contentWindow)
	        {
	            var d = i.contentWindow.document;
	        }
	        else
	        {
	            var d = window.frames[id].document;
	        }
	        
	        if (d.location.href == "about:blank")
	        {
	            return;
	        }
			
	        if (typeof(i.onComplete) == 'function')
	        {
	            i.onComplete(d.body.innerHTML);
	        }
	    }
	}
	
	function isset(check)
	{
		return(typeof(check) != "undefined");
	}
	
	Function.prototype.bind = function()
	{
		var __method = this;
		args = $A(arguments);
		object = args.shift();
		
		return function()
		{
			return __method.apply(object, args.concat($A(arguments)));
		}
	}
	
	var $A = Array.from = function(iterable)
	{
		if (!iterable) return [];
		if (iterable.toArray)
		{
			return iterable.toArray();
		}
		else
		{
			var results = [];
			for (var i = 0, length = iterable.length; i < length; i++)
			{
				results.push(iterable[i]);
			}
			return results;
		}
	}

