	
	function SiuleAjax () {
		this.con = siuleAjax_getXMLHttpRequest ();
		this.request_ok = true;				
	
		this.timeoutMillis = 5000;
		
		this.timeoutData = null;
		
		this.timer = null;
		
		this.aborted = false;
		
	}
	
	SiuleAjax.prototype.doPostRequest = siuleAjax_doPostRequest;
	SiuleAjax.prototype.doGetRequest = siuleAjax_doGetRequest;
	SiuleAjax.prototype.doFormRequest = siuleAjax_doFormRequest;
	
	SiuleAjax.prototype.getTextResponse = siuleAjax_getTextResponse;
	SiuleAjax.prototype.getXmlResponse = siuleAjax_getXmlResponse;
	SiuleAjax.prototype.getStatus = siuleAjax_getStatus;
	
	SiuleAjax.prototype._timeOutHandler = siuleAjax_timeOutHandler;
	
	SiuleAjax.prototype.onCreated = siuleAjax_empty;
	SiuleAjax.prototype.onReadyToSend = siuleAjax_empty;
	SiuleAjax.prototype.onStablished = siuleAjax_empty;
	SiuleAjax.prototype.onReceiving = siuleAjax_empty;
	SiuleAjax.prototype.onComplete = siuleAjax_empty;
	
	SiuleAjax.prototype.onFailed = siuleAjax_empty;
	SiuleAjax.prototype.onTimeOut = siuleAjax_empty;
	
	function siuleAjax_getXMLHttpRequest () {
		try { return new ActiveXObject("MSXML2.XMLHTTP"); } catch (e) {};
		try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {};
		try { return new XMLHttpRequest(); } catch (e) {};

		return null;
	}	

	function siuleAjax_doPostRequest (page, parameters, data) {
		if (this.con==null) {
			return;
		}

		var con = this.con;
		var _this = this;
		
		
		con.onreadystatechange = function () {
			var l;
			
			if (con.readyState == 0) {
				_this.onCreated (_this, data);
				return;
			}
			
			if (con.readyState == 1) {
				// cargando...	
				if (_this.aborted) {
					return;
				}
				
				_this.onReadyToSend (_this, data);
				
				// startup a time out	
				if (_this.timer == null) {
					_this.timeoutData = data;
					_this.timer = setTimeout (function () { _this._timeOutHandler();}, _this.timeoutMillis);
				}
				
				return;
			}
			
			if (con.readyState == 2) {			
				
				if (_this.aborted) {
					return;
				}
				
				// end time out
				if (_this.timer != null) {
					clearTimeout(_this.timer);
					_this.timer = null;
				}
				
				_this.onStablished (_this, data);
				
				return;
			}
			
			if (con.readyState == 3) {
				
				if (_this.aborted) {
					return;
				}
				
				_this.onReceiving (_this, data);				
				return;
			}
			
			if (con.readyState == 4) {
			
				if (_this.aborted) {
					return;
				}
				
				if (con.status != 200) {
					_this.onFailed(_this, data);//, con.status);
					return;
				}

				_this.onComplete (_this, data);
				
			}
		};
		
		con.open ("POST", page, true);
		con.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		con.setRequestHeader("Content-length", parameters.length);
		con.setRequestHeader("Connection", "close");
		con.send(parameters);

	}

	function siuleAjax_doGetRequest (page, data) {
		if (this.con==null) {			
			return;			
		}
		
		var con = this.con;
				
		var _this = this;
		
		con.onreadystatechange = function () {
			var l;
			
			if (con.readyState == 0) {
				_this.onCreated (_this, data);
				return;
			}
			
			if (con.readyState == 1) {
				// cargando...	
				if (_this.aborted) {
					return;
				}
				
				_this.onReadyToSend (_this, data);
				
				// startup a time out	
				if (_this.timer == null) {
					_this.timeoutData = data;
					_this.timer = setTimeout (function () { _this._timeOutHandler();}, _this.timeoutMillis);
				}
				
				return;
			}
			
			if (con.readyState == 2) {			
				
				if (_this.aborted) {
					return;
				}
				
				// end time out
				if (_this.timer != null) {
					clearTimeout(_this.timer);
					_this.timer = null;
				}
				
				_this.onStablished (_this, data);
				
				return;
			}
			
			if (con.readyState == 3) {
				
				if (_this.aborted) {
					return;
				}
				
				_this.onReceiving (_this, data);				
				return;
			}
			
			if (con.readyState == 4) {
			
				if (_this.aborted) {
					return;
				}
				
				if (con.status != 200) {
					_this.onFailed(_this, data);//, con.status);
					return;
				}

				_this.onComplete (_this, data);
				
			}
		};
		
		con.open ("POST", page, true);
		con.send(null);
	}

	function siuleAjax_doFormRequest (f, data) {
		var i;
		var params = "";

		for (i=0; i < f.length; i++) {
			var obj = f.elements[i];

			if (obj.type==null) {
				continue;
			}

			if (obj.name=='') {
				continue;
			}

			if (obj.type=='checkbox') {
				if (obj.checked==false) {
					continue;
				}				
			}
			
			if (params!="") {
				params += "&";
			}

			//params += obj.name + "=" + encodeURI (obj.value);

			params += obj.name + "=" + encodeURIComponent (obj.value);			

		}				

		this.doPostRequest (f.action, params, data);
	}
	
	function siuleAjax_timeOutHandler () {
		this.aborted = true;
		this.con.abort();
		this.timer=null;
		this.onTimeOut(this, this.timeoutData);
	}
	
	function siuleAjax_getTextResponse () {
		return this.con.responseText;
	}
	
	function siuleAjax_getXmlResponse () {
		return this.con.responseXML;
	}
	
	function siuleAjax_getStatus () {
		return this.con.status;
	}
	
	function siuleAjax_empty () {
		//
	}
	

	
	

