/**
 * @requires jquery.js
 * @param {String} url: url to send the call to
 * @param {Object} params: parameters passed to the url
 * @param {Function} completed: function called once ajax returns
 */
function ajax(url, params, completed) {
	if (url.substring(0, BASE_URL.length) != BASE_URL) {
		url = BASE_URL + url;
	}
	$.ajax({
		type: "POST",
		url:  url + '/format/json',
		data: params,
		dataType: "json",
		success: function(response){
			if (response.redirect) {
				window.location = BASE_URL + response.redirect;
			}
			if (typeof(completed) == 'function') {
				completed(response);
			}
		}
	});
}
/**
 * @requires jquery.js
 * @param {String} url: url to send the call to
 * @param {Object} params: parameters passed to the url
 * @param {Function} completed: function called once ajax returns
 */
function ajaxHTML(url, params, completed) {
	if (url.substring(0, BASE_URL.length) != BASE_URL) {
		url = BASE_URL + url;
	}
	$.ajax({
		type: "GET",
		url: url + '/format/html',
		data: params,
		dataType: "html",
		success: function(response){
			if (response[0] == '{' && response[response.length - 1] == '}') {
				var json = window["eval"]("(" + response + ")");
				if (json.redirect) {
					window.location = BASE_URL + json.redirect;
					return;
				}
			}
			if (typeof(completed) == 'function') {
				completed(response);
			}
		}
	});
}

function cancelButton(closeContainer, onclick) {
	// only add new button if it doesn't exist
	if ($(closeContainer + " a.closex").length == 0) {
		var cancelButton = document.createElement('a');
		cancelButton.href = '#';
		cancelButton.className = 'closex';
		cancelButton.html = 'Close';
		cancelButton.onclick = function() {
			$(closeContainer).hide();
			if (typeof(onclick) == 'function') {
				onclick();
			}
			return false;
		}
		$(cancelButton).appendTo(closeContainer);
	}
}