/**
* Xajax wrapper for all Ajax calls, so you don't have to register PHP-Ajax Functions
*
* @author Jorrit
* @author Ralf
* @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
*/

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}

            //process history
            if(options.history != 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);
            }

			//add callbacks
			if (options.callback != undefined) {
				
				// create callback object
				requestObject.callback = xajax.callback.create();

				// copy event handlers from options.callback to the xajax callback object
				for (handler in options.callback) {
					requestObject.callback[handler] = options.callback[handler];
				}
			}

            //do the request
            var requestMethod = (options.requestMethod != undefined) ? options.requestMethod : 'POST';
            xajax.config.setDefault('defaultMethod', requestMethod);
            xajax.call(functionName, requestObject);

            return true;
        }

        return false;
}

