
/**
 * Funkcja do obsługi AJAX-a
 */
function ajaxRequestHandler() {
	this.xmlHttp = null;
	
	this.createAjaxObject = function() {
	  try {
	    this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	  } catch (e1) {
	    try {
	      this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	    } catch (e2) {
			this.xmlHttp = null;
       }
	  }

	  if (!this.xmlHttp) {
	    try {
		 	this.xmlHttp = new XMLHttpRequest();
		 } catch (e3) {
		 	this.xmlHttp = null;
			this.ajaxFail = true;
		 }
	  }
		
	};

	this.resetObjectData = function() {
		this.requestURL = '';
		this.response = '';
		this.ajaxFail = false;
  		this.onLoading = function() { };
  		this.onLoaded = function() { };
  		this.onCompletion = function() { };
		this.onError = function() { };
	};
	
	this.runRequest = function() {
		if (this.ajaxFail) {
			alert('Twoja przeglądarka nie obłsuguje protokołu AJAX!');
		} else {
			var self = this;
			this.xmlHttp.open('GET', this.requestURL, true);

			this.xmlHttp.onreadystatechange = function() {
				switch (self.xmlHttp.readyState) {
					case 1:
						self.onLoading();
						break;
					case 2:
						self.onLoaded();
						break;
					case 3:
						break;
					case 4:
						self.response = self.xmlHttp.responseText;

						if (self.xmlHttp.status == "200") {
							self.onCompletion();
						} else {
							self.onError();
						}
						break;
				}
			};
			this.xmlHttp.send(this.URLString);
		}
	};	
	
 	// --------------------
 	this.resetObjectData();
	this.createAjaxObject();
}
