/************************************************************************************************************
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.
************************************************************************************************************/	
var ajaxObjectArray = new Array();
	
function ajax(){ //komunikacija sa serverom samo za kategorije
	this.request = null;
	
	this.resetData = function() {
		this.method = "POST";
		this.asinch = true; //Whether to send the request asynchronously or not
		this.queryString = "";
		this.requestFile = "";
		this.responseStatus = new Array(2);
  		this.queryStringSeparator = "?";
  	};

	this.resetFunctions = function() {
  		this.onLoading = function() { };
  		this.onLoaded = function() { };
  		this.onInteractive = function() { };
  		this.onCompletion = function() { };
  		this.onError = function() { };
		this.onFail = function() { };
	};

	this.reset = function() {
		this.resetFunctions();
		this.resetData();
	};

	this.httpRequest = function() {
		 //Mozilla-based browsers
		 if(window.XMLHttpRequest){
			this.request = new XMLHttpRequest();
		 } 
		 else if (window.ActiveXObject){ //IE-based
				this.request=new ActiveXObject("Msxml2.XMLHTTP");
				if (! this.request){
					this.request=new ActiveXObject("Microsoft.XMLHTTP");
				}
			 } else {
				this.request = null;
			 }
		if (! this.request){
			this.failed = true
		}
	};

	this.initRequest = function() {
		if (this.failed) {
			this.onFail();
		} else {
			if (this.request) {
			  try{
				var self = this;
				var totalUrlString;
				this.request.onreadystatechange = function() {
					switch (self.request.readyState) {
						case 1:
							self.onLoading();
							break;
						case 2:
							self.onLoaded();
							break;
						case 3:
							self.onInteractive();
							break;
						case 4:
							self.response = self.request.responseText;
							self.responseXML = self.request.responseXML;
							self.responseStatus[0] = self.request.status;
							self.responseStatus[1] = self.request.statusText;
							if (self.responseStatus[0] == "200") {
								self.onCompletion();
							} else {
								self.onError();
							}
							break;
					}
				};
				
				if (this.method.toUpperCase() == "GET") { //treba da proverim da li postoji querystring
					totalUrlString = this.requestFile + this.queryStringSeparator + this.queryString;
					this.request.open(this.method, totalUrlString, this.asinch);
					this.request.send(null);
				} else {
					this.request.open(this.method, this.requestFile, this.asinch);
					this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
					this.request.send(this.queryString);
				}
			  } catch (err) {
				self.onError(); 
			  }
			}
		}
	};

	this.reset();
	this.httpRequest();
}

function handleError(ajaxIndex){
	var errorNum, errorDesc;
	errorNum = ajaxObjectArray[ajaxIndex].responseStatus[0];
	errorDesc = ajaxObjectArray[ajaxIndex].responseStatus[1];
	alert("There is an error!\n" + errorNum + " - " + errorDesc + ".");
	//The application cannot contact the server at the moment. Please try again in a few seconds.
}

function handleFail(){
	alert("Your browser does not permit the use of all of this application's features!");
}