/*	romke Ajax Interface - Class stuff
	2006(c) romke Roman Barczy�ski (romke/na/estrefa.pl)
	$Id: Ajax.js 114 2006-10-18 14:42:51Z romke $
	vim: set ts=4 : */

Ajax = function (aBaseURI, aReferer) {
	var ajax = {
		BaseURI : aBaseURI,
		GETParams : new Array(),
		POSTParams : new Array(),
		Referer : aReferer,

		AddGETParam : function (name, value) {
			this.GETParams.push(name + '=' + encodeURIComponent(value));
		},

		AddPOSTParam : function (name, value) {
			this.POSTParams.push(name + '=' + encodeURIComponent(value));
		},

		TimeParam : function () {
			return '_pch=' + (new Date().getTime()); /* pch - Prevent Cache Hack */
		},

		_Open : function (method) {
			var URIParams = this.GETParams.join('&');

			if (window.XMLHttpRequest) { /* mozilla */
				this.HTTP = new XMLHttpRequest();
			} else if (window.ActiveXObject) { /* ie */
				try { this.HTTP = new ActiveXObject("Msxml2.XMLHTTP"); }
				catch (e) {
					try { this.HTTP = new ActiveXObject("Microsoft.XMLHTTP"); }
					catch (E) { this.HTTP = false; }
				}
			}

			var _self = this;

			this.HTTP.onreadystatechange = function () {
				if (_self.HTTP.readyState == 4) {
					if (_self.HTTP.status != 200) {
						if (_self.onError != null) _self.onError();
						delete _self.HTTP;
					} else {
						if (_self.onSuccess != null) _self.onSuccess();
					}
				}
			}

			if (!this.HTTP) return false;

		  //this.HTTP.open(method, this.BaseURI + this.TimeParam() + ((URIParams != '') ? '&' + URIParams : ''), true);
	         this.HTTP.open(method, this.BaseURI + ((this.BaseURI.indexOf("?")==-1) ? '?' : '&') + this.TimeParam() + ((URIParams != '') ? '&' + URIParams : ''), true);
			if (this.Referer != '') this.HTTP.setRequestHeader('Referer', this.Referer);

		},

		GET : function () {
			this._Open('GET');
			this.HTTP.send(null);
		},

		POST : function () {
			var POSTParams = this.POSTParams.join('&');
			this._Open('POST');
			this.HTTP.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			this.HTTP.setRequestHeader('Content-length', POSTParams.length);
			this.HTTP.setRequestHeader('Connection', 'close');
			this.HTTP.send(POSTParams);
		}

	};

	return ajax;
}

/* setInner replaces innerHTML of element specified by "id" with "html" */
function setInner(html, id) {
	document.getElementById(id).innerHTML = html;
}


function setInnerByUrl(url, id, method, parameters, enable_object) 
{
	if(document.getElementById(id)) {
		setInner('<center><br/><img alt="" src="http://img.nocowanie.pl/gfx/main/ajax-loader.gif" style="vertical-align: middle;"/><br/><br/></center>', id);
		if (enable_object && document.getElementById(enable_object))
			document.getElementById(enable_object).disabled = true;
		var aj2 = new Ajax(url, document.location);
		aj2.onSuccess = function () { 
			setInner(this.HTTP.responseText, id);  
			if (enable_object && document.getElementById(enable_object))
				document.getElementById(enable_object).disabled = false;
		}

		if (parameters)
		{
			for (var param in parameters)
			{
				if (!method || method=='get')
					aj2.AddGETParam(param,parameters[param]);
				else
					aj2.AddPOSTParam(param,parameters[param]);
			}
		}
		
		if (!method || method=='get')
			aj2.GET();
		else
			aj2.POST();
	}
	return false;
}
