// DEPRECATED!!!

/**
 * Get the target element of an event
 * */
function getTarget(event) {
	var targ;
	if (!event) var event = window.event;
	if (event.target) targ = event.target;
	else if (event.srcElement) targ = event.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
	return targ;
}

function preventDefault(event) {
	if(event.preventDefault) {
		event.preventDefault();
	} else {
		event.returnValue = false;
	}
}

/**
 * Invoke an ajax call at the given location and expect a json response.
 * After the call the given delegates are invoked. 
 * 
 * @param tag the tag containing the href of the ajax call.
 * @param data the data to send along with the ajax call.
 * @param atBegin the function to be invoked after the ajax call but before
 *        the others.
 * @param ifTrue the function invoked if the result is true.
 * @param ifFalse the function to be invoked if the result is false.
 * @param atEnd the function to be invoked at the end independently of the result.
 * @return the ajax result encoded in a json message.
 */
function href_invoke_and_do(href, data, atBegin, ifTrue, ifFalse, atEnd) {
	$.ajax({
			url: href,
			data: data, 
			type: "GET",
			cache: false,
			dataType: "json",
			contentType: "application/json;charset=UTF-8",
			beforeSend: function(x) {
		        if(x && x.overrideMimeType) {
		            x.overrideMimeType("application/json;charset=UTF-8");
		        }
		    },
			success: function(json_response) {
			     result = json_response.result;      
		         if (atBegin != null) {
			    	 atBegin(result);
		    	 }
			     try {
				     if (result) {
				    	 if (ifTrue != null) {
				    		 ifTrue(json_response);
				    	 }
				     } else {
				    	 if (ifFalse != null) {
				    		 ifFalse(json_response);
				    	 }
				     }
			     } finally {
			    	 if (atEnd != null) {
			    		 atEnd(json_response);
			    	 }
			     }
			     return result;
			 },
			 error: function (XMLHttpRequest, textStatus, errorThrown) {
				 //alert("XMLHttpRequest="+XMLHttpRequest.responseText+"\ntextStatus="+textStatus+"\nerrorThrown="+errorThrown);
			 }
     });
}

function invoke_and_do(tag, data, atBegin, ifTrue, ifFalse, atEnd) {
	return href_invoke_and_do(tag.href, data, atBegin, ifTrue, ifFalse, atEnd)
}

/**
 * Invokes an ajax call and reload the page if the result is true.
 * 
 * @param tag the tag containing the href of the ajax call.
 * @return the ajax result encoded in a json message.
 */
function invoke_and_reload(tag) {
	$.getJSON(tag.href, function(json_response) {
	     result = json_response.result;    
	     if (result) {
	    	 window.location.reload();
	     }
	     return result;
	 });
}


/**
 * Prevents an event from happening, invokes the target with an ajax call
 * and if the invocation was successful reloads the page. 
 * 
 * @param event the given event.
 * @return the ajax result encoded in a json message.
 */
function prevent_invoke_and_reload(event) {
	preventDefault(event);
    return invoke_and_reload(event.target);
}


/**
 * Invokes an ajax call and removes a tag independently from the
 * result.
 * 
 * @param toInvoke the tag containing the href of the ajax call.
 * @param toDestroy the tag to be removed from the DOM.
 * @return the ajax result encoded in a json message.
 */
function href_invoke_and_destroy(href, toDestroy) {
	return href_invoke_and_do(href, '', null, null, null, 
			function(json_response) {
				toDestroy = $("#".concat(toDestroy));
				toDestroy.remove();
        	});
}

function invoke_and_destroy(toInvoke, toDestroy) {
	return href_invoke_and_destroy(toInvoke.href, toDestroy)
}


/**
 * Prevents an event from happening, invokes the target with an ajax call
 * and removes the element with the given id. 
 * 
 * @param event the given event.
 * @param toDestroy the id of the tag to be removed from the DOM.
 * @return the ajax result encoded in a json message.
 */
function prevent_invoke_and_destroy(event, toDestroy) {
	preventDefault(event);
    return invoke_and_destroy(event.target, toDestroy);
}
