function ajax(expression, params, success, failure) {
	params['e'] = '#{' + expression + '}';
	sendAjaxRequest(params, success, failure);
}
function invoke(id, action) {
	var params = $A(arguments);
	params.shift();
	params.shift();
	var paramString = params.join("','");
	if (paramString.length > 0) {
		paramString = "'" + paramString + "'";
	}
	var expression = '#{' + action + '(' + paramString + ')' + '}';
	sendAjaxRequest(
		{
			id:id,
			action:expression
		},
		function(transport) {
			var redirect = transport.getHeader('X-Redirect-To');
			if (redirect) {
				window.location.href = redirect;
			}
		}
	);
}
function sendAjaxRequest(params, success, failure) {
	var viewState = $('javax.faces.ViewState');
	if (!viewState) {
		alert('No ViewState found.');
		return;
	}
	params['javax.faces.ViewState'] = viewState.value;
	params['convid'] = viewState.form.action.match(/convid=([^&]+)/)[1];
	new Ajax.Request(window.location.pathname.replace(/.jsf/, '.ajax'), { 
		method:'post', 
		parameters : params,
		onSuccess: function(transport) {
			var messages;
			var result;
			if (transport.responseJSON) {
				messages = transport.responseJSON.messages;
				result = transport.responseJSON.result;
			}
			if (messages != undefined && result != undefined) {
				handleMessages(messages);
				success(result);
			} else {
				success(transport);
			}
		},
		onFailure: failure
	});
}

function handleMessages(messages) {
	if (messages.size() > 0 && addFacesMessage != undefined && prepareMessageContainer != undefined) {
		prepareMessageContainer();
		messages.each(function(message) {
			addFacesMessage(message.detail, message.severity);
		});
	} 
}

function preloadBusy() {
	if (!window.imgBack || !window.imgBusy) {
		var matches = window.location.href.match(/(.*)\/asp\/.*/);
		if (!matches) {
			matches = window.location.href.match(/(.*)\/faces\/.*/);
		}
		if (!matches) {
			matches = window.location.href.match(/(.*)\/flow\/.*/);
		}
		var imgBase = matches[1] + '/resources/gfx/';
		window.imgBack = new Image();
		imgBack.src = imgBase + 'busy_background.png';
		window.imgBusy = new Image();
		imgBusy.src = imgBase + 'busy.gif';
		window.imgBusySmall = new Image();
		imgBusySmall.src = imgBase + 'busy_small.gif';
	}
}

function showBusy(msg) {
	document.body.style.cursor='wait';
	if (!msg) {
		msg = 'Please wait for a moment';
	}
	preloadBusy();
	Insertion.Before(document.getElementsByTagName('body')[0].firstChild, 
		'<div id="busyMessage" style="position:absolute;left:' 
			+ (document.viewport.getWidth() - 250) / 2 
			+ 'px;top:150px;width:250px;height:250px;z-index:1000;background-image:url(\'' + window.imgBack.src + '\')">'
		+ '<img style="position:absolute;left:62px;top:30px;" src="' + window.imgBusy.src + '" border="0" />'
		+ '<div style="position:absolute;left:20px;bottom:30px;width:210px;text-align:center;color:white;font-weight:bold;">'
		+ msg
		+ '</div>' 
		+ '</div>');
	return true;
}

function hideBusy() {
	var el = $('busyMessage');
	if (el) {
		el.remove();
	}
	document.body.style.cursor='auto';
}

if (typeof(A4J) != 'undefined') {
	A4J.AJAX.onExpired = function(loc, expiredMessage) {
		// TODO: Complete this
		alert(expiredMessage + ': ' + loc);
		return "/faces/session-expired.jsf";
	};
}

