function doAjaxAction(theAction){
	switch(theAction.d){
		case '_prompt' :
			// CALL PROMPT PROCEDURE
			prompt(theAction.c);
			break;
		case '_messageBox' :
			// CALL MESSAGE BOX PROCEDURE
			alert(theAction.c);
			break;
		case '_javascript' :
			// EXECUTE JAVASCRIPT CODE
			eval(theAction.c);
			break;
		case '_window' :
			// OPEN A WINDOW MODAL
			openWindow(theAction.c);
			break;
		case '_redirect' :
			// OPEN A WINDOW MODAL
			window.location.replace(theAction.c);
			break;
		default:
			$('#'+theAction.d).html(theAction.c);
			break;
	}
}

function ajaxAction(actions, returns, options){
	if(typeof(returns)=='undefined' || returns==''){
		returns = 'null:null';
	}
	options = $.extend(options, {
		'_a':actions,
		'_r':returns,
		'_d':(new Date()).getTime()
	});
	$.post( '/',
	
			options,
	
			function(data){
				$.each(data, function(i,item){
					doAjaxAction(item);
				});
			},
	
			'json'
	);
}

var _windowStack = new Array();
function openWindow(content){
	_windowStack.unshift(content);
	_displayWindow();
}


function _displayWindow(){
	if($('#_window').length!=0){
		$('#_window').remove();
	}
	$("<div id=\"_window\" title=\"\"></div>").appendTo("body")
	$('#_window').html(_windowStack[0]);
	$('#_window').dialog({
		modal:true,
		close: function(ev, ui) {
			_windowStack.shift();
			$('#_window').remove();
			if(_windowStack.length!=0){
				_displayWindow();
			}
		}
	});
}
