/**
 * Makes an AJAX call and puts the result in any container
 * 
 * @param link the link to the webpage
 * @param target -optional- the container where to put the result
 * @param completionFunction -optional- a function that does postprocessing after the DOM is created
 */
function ajaxCall_generic(link, target, completionFunction, asynchronousCall) {
	var isAsync = true;
	if (asynchronousCall)
		isAsync = async;
	$('body').css('cursor','progress');
	if (target)
		$(target).html("<img src='images/ajax-loader.gif'/>");
	
	$.ajax({
		type : 'POST',
		url : link,
		cache : false,
		async: isAsync,
		success : function(response) {
			$('body').css('cursor','default');	
			if (target)
				$(target).html(response);
			if (completionFunction)
				completionFunction();
		},
		complete : function() {
			return true;
		}
	});
}

/**
 * Makes an AJAX call
 * 
 * @param link the link to the webpage
 * @param targetId -optional- the container if where to put the result
 * @param completionFunction -optional- a function that does postprocessing after the DOM is created
 * @return
 */
function ajaxCall(link, targetId,completionFunction) {
	var jquery_cont = $('#'+targetId);
	ajaxCall_generic(link, jquery_cont, completionFunction);
}



/**
* Makes an AJAX POST call
* 
* @param link the link to the webpage
* @param data key-value pairs to be sent
* @param targetId -optional- the container if where to put the result
* @param completionFunction -optional- a function that does postprocessing after the DOM is created
* @return
*/
function ajaxPost(link, data, targetId,completionFunction) {
	$('body').css('cursor','progress');
	if (targetId)
		$(targetId).html("<img src='images/ajax-loader.gif'/>");
	$.post(link,data,
				function(response) {
				$('body').css('cursor','default');
				
				var targetElem = "";
				if (targetId) {
					if ((targetId.charAt(0) == '.' )|| 
						(targetId.charAt(0) == '#'))
						targetElem = targetId;
					else
						targetElem = '#' + targetId;
				}
				var jquery_cont = $(targetElem);
				if (!jquery_cont) ;
				else $(targetElem).html(response);
				if (!completionFunction);
				else completionFunction();
			},
			"text");
}

function ajaxPostToDOMObject(link, data, targetObject,completionFunction) {
	$('body').css('cursor','progress');
	$.post(link,data,
				function(response) {
				$('body').css('cursor','default');

				var jquery_cont = $(targetObject);
				if (!jquery_cont) ;
				else $(targetObject).html(response);
				if (!completionFunction);
				else completionFunction();
			},
			"text");
}
