/**
* Xajax wrapper for all Ajax calls, so you don't have to register PHP-Ajax Functions
*
* @author Jorrit
* @author Ralf
* @author Juul Hobert <j.hobert@tsi-solutions.nl>
* @update removed old stuff, build in ajax history more efficient
* @version 2008-12-5
*
* @param string module the name of the module to call
* @param string functionName the name of the function to call
* @param array arguments Array with argument for the module's function
* @return boolean true when the ajax call is done otherwsie false
*
* @require module != undefined && functionName != undefined && arguments != undefined
* @ensure result == true
*/

// Raise the max object size from the default of 2000
// Camelot bookstep 2 can generate huge amounts of data (transport & extra selection for up to 24 travelers)
xajax.config.maxObjectSize = 20000;

function ytw_call(module, functionName, arguments, options) {
        if(options == undefined) { options = {}; }
        if(options != undefined && (options == true || options == false)) {
            //makes old code compatible
            options = {'history': options};
        }
        if(module != undefined && functionName != undefined) {
            var requestObject = {};
            var frameworkSettings = {'module': module, 'function': functionName}
			var callback = xajax.callback.create();
			requestObject.callback = callback;

			if (options.show_loader == undefined || options.show_loader) {
				// Register loader callbacks first
				callback.onRequest = function() {
					loader_get().show();
				}
				callback.onComplete = function() {
					loader_get().hide();
				}
			}

			//add user-defined callbacks
			if (options.callback != undefined) {

				// utility function that combines multiple argumentless functions into one
				var compose = function () {
					var args = $A(arguments);
					return args.invoke.bind(args, 'call');
				}

				// copy event handlers from options.callback to the xajax callback object
				for (handler in options.callback) {
					if (callback[handler]) {
						callback[handler] = compose( callback[handler], options.callback[handler] );
					} else {
						callback[handler] = options.callback[handler];
					}
				}
			}
			
            //process history
            if(typeof(options['history']) != 'undefined' && typeof(window['addHistory']) != 'undefined') {
                var timestamp = new Date().getTime();
                frameworkSettings.historyTimestamp = timestamp;
                addHistory(timestamp, functionName);
            }

            //wrap arguments
            requestObject.parameters = [frameworkSettings];
            if(arguments != undefined) {
                requestObject.parameters = requestObject.parameters.concat(arguments);
            }
			
            //do the request
            var requestMethod = (options.requestMethod != undefined) ? options.requestMethod : 'POST';
            xajax.config.setDefault('defaultMethod', requestMethod);
			
			// IE8 bugfix: IE somehow doesn't send the POST data when an anchor is in the url?
			requestObject.URI = document.URL.replace(/#.+$/, '');
			
            xajax.call(functionName, requestObject);

            return true;
        }

        return false;
}
