
var agt = navigator.userAgent.toLowerCase();
is_ie     = ((agt.indexOf('msie') != -1) && (agt.indexOf('opera') == -1));

var MicroAJAX = Class.create();

MicroAJAX.prototype = {
	httpObj: null,
	sMethod: "GET",
	iTimeOut: 5000, // 1000 = 1 second
	timerID: null,
	aCookies: new Array(),

	initialize: function() {
	},

	/*

	Property get / set functions

	*/

	setMethod: function (sMethod) {
		if(sMethod != "POST" && sMethod != "GET" && sMethod != "PUT" && sMethod != "HEAD") {
			return 0;
		}

		this.sMethod = sMethod;

		return 1;
	},

	setTimeOut: function (iTimeOut) {
		if(iTimeOut < 1) {
			iTimeOut = 1;
		}

		this.iTimeOut = iTimeOut;
	},

	initHTTPRequestObject: function () {
		if (window.XMLHttpRequest) { // Mozilla, Safari,...
			this.httpObj = new XMLHttpRequest();
		} else if (window.ActiveXObject) { // IE
			try {
				this.httpObj = new ActiveXObject('Msxml2.XMLHTTP');
			} catch (e) {
				try {
					this.httpObj = new ActiveXObject('Microsoft.XMLHTTP');
				} catch (e) {}
			}
		}
	},

	makeGetRequest: function(sURL, callBackFunction) {
		this.setMethod("GET");

		this._sendRequest(sURL, callBackFunction, null);
	},

	submitForm: function (sFormID, callBackFunction, sCallBackParameters) {
		var postVars = "";

		var frm = document.getElementById(sFormID);

		if(frm) {
			var ajaxURL = frm.elements['action_ajax'].value;

			if(ajaxURL.length < 3) {
				ajaxURL = frm.action;
			}

			for (i = 0; i != frm.elements.length; i++) {
				if(frm.elements[i].name == null || frm.elements[i].name == undefined)
					continue;

				postVars = postVars + '&' + escape(frm.elements[i].name) + '=' + escape(frm.elements[i].value);
			}

			if(postVars.length > 0) {
				postVars.substring(1);
			}

			return this.makePostRequest(ajaxURL, postVars, callBackFunction, sCallBackParameters);
		} else {
			return 0;
		}
	},

	makePostRequest: function(sURL, aPostVars, callBackFunction, sCallBackParameters) {
		this.setMethod("POST");

		this._sendRequest(sURL, callBackFunction, aPostVars, sCallBackParameters);
	},

	timeoutHandler: function (callBackFunction,sCallBackParameters) {
		try {
			this.httpObj.abort();
		} catch (e) {}

		clearTimeout(this.timerID);

		if(callBackFunction != null && callBackFunction != undefined) {
			callBackFunction(0,sCallBackParameters);
		}

		return 0;
	},

	_sendRequest: function (sURL, callBackFunction, toSend, sCallBackParameters) {
		if(this.httpObj == null || this.httpObj == undefined /* || is_ie */) {
			this.initHTTPRequestObject();
		}

		if(this.httpObj == null || this.httpObj == undefined || sURL.length == 0) {
			return false;
		}

		try {
			this.httpObj.abort();
		} catch (e) {}


		this.timerID = setTimeout("myMicroAjax.timeoutHandler(" + callBackFunction + ",'" + sCallBackParameters + "')", this.iTimeOut);

		this.httpObj.onreadystatechange = function() { myMicroAjax._readyStateHandler(callBackFunction,sCallBackParameters); };
		//this.httpObj.open(this.sMethod, sURL + '&ajax=1', true);
		this.httpObj.open(this.sMethod, sURL, true);

		if(this.sMethod == "POST") {
			this.httpObj.setRequestHeader('Content-Type',  'application/x-www-form-urlencoded; charset=UTF-8');
		} else {
			//this.httpObj.setRequestHeader('Content-Type',  'text/plain');
		}

		this.httpObj.send(toSend);
	},

	_readyStateHandler: function (callBackFunction, sCallBackParameters) {
		try {
			if(this.httpObj.readyState == 4) {
				if (this.httpObj.status == 200) {
					// success, call the callbackfunction
					this.httpObj.onreadystatechange = function() {};

					// Clear the timer
					clearTimeout(this.timerID);

					if(callBackFunction != null && callBackFunction != undefined) {
						callBackFunction(this.httpObj.responseText, sCallBackParameters);
					}
				}
			}
		}
		catch (e) { this.timeoutHandler(callBackFunction); }
	}
}

function initMicroAJAX() { myMicroAjax = new MicroAJAX(); }
initMicroAJAX();