//handles ajax requests
function Ajax(){
	//multidimensional array with all the errors. each error is an array with keys: function and message
	this.errors;

	//last error that occurred
	this.error;

	/**
	 * Sets an error
	 *
	 * @return void
	 */
	this.setError = function(func, err){
		this.errors = this.errors ? this.errors : [];
		var key = this.errors.length;
		this.errors[key] = [];
		this.errors[key]['function'] = func;
		this.errors[key]['message'] = err;
		this.error = 'Function "' + func + '": ' + err;
	}

	/**
	 * Fires an ajax request
	 *
	 * @param string url
	 * @param string data; extra url data
	 * @param string dataType; html/xml, default html
	 * @param string successFunction; callback to be executed on request success
	 * @param string errorFunction; callback to be executed on request error
	 * @param string completeFunction; callback to be executed on request completion
	 * @param string ajaxError; id of object that will contain the standard ajax error
	 * @param string ajaxLoader; id of object that acts as a loading animation
	 * @return void
	 */
	this.request = function(url, data, type, dataType, successFunction, errorFunction, completeFunction, ajaxError, ajaxLoader){
		if(!url){
			this.setError('request', 'No url has been passed');
			return;
		}
		if(ajaxLoader){
			jQuery("#" + ajaxLoader).css("display", "block");
		}
		type = type ? type : "GET";
		dataType = dataType ? dataType : "html";
		jQuery.ajax(
			{
				type: type,
				url: url,
				data: data,
				dataType: dataType,
				success: function(result){
					if(successFunction){
						eval(successFunction);
					}
				},
				error: function(result) {
					if(errorFunction){
						eval(errorFunction);
					} else {
						Ajax.requestError(ajaxError);
					}
				},
				complete: function(result) {
					if(completeFunction){
						eval(completeFunction);
					}
					if(ajaxLoader){
						jQuery("#" + ajaxLoader).css("display", "none");
					}
				}
			}
		);
	};

	/**
	 * Loads html content into an html object
	 *
	 * @param string obj_id; id of the object that will contain the loaded content
	 * @param string url; ajax url
	 * @param string data; extra url data
	 * @param string callBack; callback to be executed on request success
	 * @return void
	 */
	this.load = function(obj_id, url, data, callBack){
		if(!obj_id){
			this.setError('load', 'No object id has been passed');
			return;
		}
		if(!url){
			this.setError('load', 'No url has been passed');
			return;
		}
		jQuery("#" + obj_id).load(url, data, callBack);
	};

	/**
	 * Displays an object to be used as loader
	 *
	 * @param string loader_id; id of the object that will be shown
	 * @param string visibility; whether object uses css property "visibility" instead of "display"
	 * @return void
	 */
	this.loaderShow = function(loader_id, visibility){
		if(!loader_id){
			this.setError('loaderShow', 'No loader id has been passed. Error showing loader');
			return;
		}
		if(visibility){
			jQuery("#" + loader_id).css('visibility', 'visible');
		} else {
			jQuery("#" + loader_id).css('display', 'block');
		}
	};

	/**
	 * Hides an object to be used as loader
	 *
	 * @param string loader_id; id of the object that will be hidden
	 * @param string visibility; whether object uses css property "visibility" instead of "display"
	 * @return void
	 */
	this.loaderHide = function(loader_id, visibility){
		if(!loader_id){
			this.setError('loaderHide', 'No loader id has been passed. Error hiding loader');
			return;
		}
		if(visibility){
			jQuery("#" + loader_id).css('visibility', 'hidden');
		} else {
			jQuery("#" + loader_id).css('display', 'none');
		}
	};

	/**
	 * Handles a standard ajax request error
	 *
	 * @param string ajaxError; id of the object that will contain the error
	 * @return void
	 */
	this.requestError = function(ajaxError){
		jQuery("#" + ajaxError).html("Operatiunea a esuat din cauza unei erori de sistem. Va rugam incercati din nou dupa o reincarcare a paginii si in cazul in care eroarea persista, contactati un administrator.");
		jQuery("#" + ajaxError).css("display", "block");
	};
}

var Ajax = new Ajax();
