/*
 * HIT Cyclone Platform, PERL Power Web Platform.
 * Copyright (C) 2006-2008, HIT IT Consulting & Services, individual authors
 * as indicated by the @author tags.
 *
 * This is CLOSED SOURCE SOFTWARE. You may neither read nor copy
 * and / or distribute any portion of this software without prior
 * permission by HIT IT Consulting & Services.
 *
 * @author Ben Hildebrand (ben.hildebrand [at] hit-consult.net
 */

function srvSide( functionName, args, successCallback ) {
	ajaxRequest( 
		"/run/" + functionName, 
		true, 
		unescape( $H( args ).toQueryString() ), 
		function( t ) {
			try {
				res = deflateJSON( t.responseText );
			
				if( res.actionResult < 0 ) {
				    if( res.actionResult == -99 ) {
					_showError( "platformError" );
				    } else {
					_showError( "comError" );
				    }
				} else {
					successCallback( res );
				}
			} catch( e ) {
				_showError( "invalidResponse" );
			}
		} 
	);
}

function ajaxRequest( target, asyncMode, postTxt, onSuccessCallback ) {
	var opt = {
	// Use get
	method: 'post',
	postBody: postTxt,
	asynchronous: asyncMode,
	encoding: 'ISO-8859-1',
	// Handle successful response
	onSuccess: function( t ) {
		setWaiting( false );
		onSuccessCallback( t );
	},
	// Handle 404
	on404: function( t ) {
		setWaiting( false );
		_showError( 'ajax404' );
	},
	// Handle other errors
	onFailure: function(t) {
		setWaiting( false );
		_showError( 'communicationError', { special: 1, errorStatus: t.status, errorText: t.statusText } );
	},
	onException: function( requester, exception ) {
		setWaiting( false );
		throw( exception );
	}
}

	setWaiting( true );
	new Ajax.Request( target, opt );
}

function setWaiting( value ) {
	waiting = value;
	updateDynamics();
}

function updateDynamics() {
	if( waiting ) {
		$$( ".dynamics_waiter" ).each( function( s ) { s.show(); } );
	}
	else {
		$$( ".dynamics_waiter" ).each( function( s ) { s.hide(); } );
	}
}

