﻿function XMLHttpAjax()
{
	this._callbacks = new Array();

	this._init = function()
	{
		var x = null;
		var M = new Array(
			'Msxml2.XMLHTTP.5.0',
			'Msxml2.XMLHTTP.4.0',
			'Msxml2.XMLHTTP.3.0',
			'Msxml2.XMLHTTP',
			'Microsoft.XMLHTTP'
		);
		
		for (var i = 0; i < M.length; i++) {
			try { x = new ActiveXObject(M[i]); }
			catch (e) { x = null; }
			if (x != null) break;
		}

		if(!x && typeof XMLHttpRequest != 'undefined') x = new XMLHttpRequest();
		if (!x) return null;
		return x;
	}
	
	this._addCallback = function(x,oncallback,context)
	{
		for(var i=0;i<this._callbacks.length;i++)
			if (this._callbacks[i]._x == x || this._callbacks[i]._x == null)
			{
				this._callbacks[i]._x = x;
				this._callbacks[i]._oncallback = oncallback;
				this._callbacks[i]._context = context;
				return;
			}
			
		this._callbacks[this._callbacks.length] = {
			_x			: x,
			_oncallback	: oncallback,
			_context	: context
		};
	}
	
	this._processCallback = function(x,rvalue)
	{
		for(var i=0;i<this._callbacks.length;i++)
			if (this._callbacks[i]._x == x)
			{
				this._callbacks[i]._x = null;
				if (this._callbacks[i]._oncallback != null)
				{
					this._callbacks[i]._oncallback(rvalue, this._callbacks[i].context);
					this._callbacks[i]._oncallback = null;
					return;
				}
			}
	}
  
	this._exec = function(uri,oncallback,context,data)
	{
		var x = this._init();
		if (x == null) return false;	
		
		this._addCallback(x,oncallback,context);
  
		x.onreadystatechange = function()
		{
			if (x.readyState != 4)
				return;
			var txt = x.responseText.replace(/^\s+|\s+$/g, '');
			x.onreadystatechange = function() { };
			Ajax._processCallback(x, txt);
		};
    
		x.open('POST', uri, true);
		x.send(data);
    
		return true;
	}
  
  this.execute = function(url,oncallback,context) { return this._exec(url.replace(/&amp;/g, '&'),oncallback,context,''); }
  
	this._escape = function(s)
	{
		if (typeof(encodeURIComponent) == 'function')
			return encodeURIComponent(s);
		else
			return escape(s);
	}
	
	this._dataAdd = function(name,value)
	{
		if (name == '') return;
		if (this._data != '') this._data += '&';
		this._data += name + '=' + this._escape(value);
	}
  
  this.executeForm = function(form,oncallback,context)
  {
		this._data = '';
		
		var url = '' + form.action;
		if (url == '') url = '' + url;
		for(var i=0;i<form.elements.length;i++)
		{
			var e = form.elements[i];
			if (e.tagName.toUpperCase() == 'INPUT')
			{
				switch (e.type.toUpperCase())
				{
					case 'TEXT','PASSWORD','SUBMIT','HIDDEN':
						this._dataAdd(e.name, e.value);
						break;
					case 'RADIO','CHECKBOX':
						var v = e.value;
						if (v == '') v = 'On';
						if (e.checked) this._dataAdd(e.name, v);					
						break;
				}
			}
			else if (e.tagName.toUpperCase() == 'TEXTAREA' || (e.tagName.toUpperCase() == 'BUTTON' && e.type.toUpperCase() == 'SUBMIT'))
				this._dataAdd(e.name, e.value);
		}
		
		return this._exec(url.replace(/&amp;/g, '&'),oncallback,context,this._data);
	}
  
}

var Ajax = new XMLHttpAjax();