Ajax = {

	/**
	 * Submit a form with an ajax request.
	 * @author leo
	 */
	request: function(settings) {
		var base = this;
		
		if (settings.url == undefined) {
			return false;
		}
		
		settings = $.extend({
            type: 'POST',
			data: {},
            dataType: 'json',
			success: function(data, textStatus, jqXHR) {
                switch (data.status) {
                    case 'success':
                        if (data.action != undefined) {
                            // Execute action
                            try {
                                eval(data.action);
                            } catch (e) {
                                window.console.log(e);
                                alert('Ajax failure!');
                            }
                        } else if (data.html != undefined) {
                            if (settings.target != undefined) {
                                var $target = settings.target;
                                if (!($target instanceof $)) {
                                    $target = $($target);
                                }

                                // Update target element
                                $target.replaceWith(data.html);
                            }
                        }
                        break;
                }
			},
			error: function(jqXHR, textStatus, errorThrown) {
				alert('There was an error while processing your request. Please contact the administrator.');
			}
		}, settings);
		
		$.ajax(settings);
		
		return false;
	}

};

