		/*
	The code in this file is based upon wonder.js. Which is used in Project Wonder's Ajax Framework.
	http://wiki.objectstyle.org/confluence/display/WO/Project+WONDER-Overview
	MoWonder.js ports the functionality found in wonder.js to MooTools.  wonder.js is based upon Prototype/Scriptaculous
	@author Jonathan Miller
 */


/*
	Mootools has $exec function - same functionality as wonder.js Object.extend exec
*/

String.implement({
	
	addQueryParameters : function(additionalParameters) {
		if (additionalParameters) {
			return this + (this.match(/\?/) ? '&' : '?') + additionalParameters;
		} else {
			return this;
		}
	},
	
	blank : function() {
		return this.trim().length === 0;
	}

});

// In wonder.js the Event object is extended to recognize key board input.  This functionality already exists in MooTools.
// Functions in Wonder's Form object pre-exist in mooTools.

var AjaxOnDemand = {
	
	loadScript : function(script) {
		new Request({
			url: script,
			method : 'get',
			async : false,
			onSuccess: function(responseText, responseXML) {
				Document.exec(responseText);
			}
		});
	},
	loadedScript: function() {},
	loadedCSS : function() {},
	loadCSS : function() {}
	
};

var AjaxInPlace = {
	saveFunctionName : function() {},
	cancelFunctionName : function() {},
	editFunctionName : function() {},
	cleanupEdit : function() {},
	cleanupView : function() {}
};

var AjaxOptions = {
	defaultOptions: function(additionalOptions) {
		var options = { 
			method : 'get',
			async : true,
			evalScripts : true 
		};
		options = $H(options);
		options.extend(additionalOptions);
		return options;
	}
	
};

var AjaxUpdateContainer = {
	
	registerPeriodic : function(id, canStop, stopped, options) {
		
		var url = $(id).get('updateUrl');
		var updater;
		if(!canStop) {
			updater = new Request.HTML($merge(AjaxOptions.defaultOptions(options), {
				url: url,
				initialDelay: options.delay || options.frequency * 1000,
				delay: options.frequency * 1000,
				update: $(id)
			})).startTimer();
		}
	
	},
	insertionFunc : function() {},
	register : function(id, options) {
		if(!options) options = {};
		eval(id + "Update = function() { AjaxUpdateContainer.update(id, options) }");
	},
	update : function(id, options) {
		var actionUrl = $(id).get('updateUrl');
		actionUrl = actionUrl.addQueryParameters('__updateID=' + id);
//		new Ajax.Updater(id, actionUrl, AjaxOptions.defaultOptions(options));
		new Request.HTML($merge(AjaxOptions.defaultOptions(options), {
			update : $(id),
			url : actionUrl
		})).send();
	}
	
};

var AjaxUpdateLink = {
	updateFunc: function() {},
	update: function(id, options, elementID, queryParams) {
		eval(options.onRequest);
		var updateElement = $(id);
		if(updateElement == null) {
			alert('There is no element on this page with the id "' + id + '".');
		}
		var pattern = /[^/]+$/;
		var actionUrl = updateElement.get('updateUrl').replace(pattern, elementID);
		actionUrl = actionUrl.addQueryParameters(queryParams);
		actionUrl = actionUrl.addQueryParameters('__updateID=' + id);
		new Request.HTML($merge(AjaxOptions.defaultOptions(options), {
			update : $(id),
			url : actionUrl,
			onRequest : options.onRequest || $empty,
			onSuccess : options.onSuccess || $empty
		})).send();
	},
	request: function() {}
};

var AjaxSubmitButton = {

	PartialFormSenderIDKey : '_partialSenderID',
	AjaxSubmitButtonNameKey : 'AJAX_SUBMIT_BUTTON_NAME',

	defaultOptions : function(additionalOptions) {
		var options = AjaxOptions.defaultOptions(additionalOptions);
		options['method'] = 'post';
		return options;
	},
	
	generateActionUrl : function(id, form, queryParams) {
		
		var actionUrl = form.action;
		
		if(queryParams != null) {
			actionUrl = actionUrl.addQueryParameters(queryParams);
		}
		
		actionUrl = actionUrl.replace('/wo/', '/ajax/');
		
		if(id != null) {
			actionUrl = actionUrl.addQueryParameters('__updateID=' + id);
		}
		
		return actionUrl;
	
	},
	
	processOptions : function(form, options) {
		
		var processedOptions = null;
		if(options != null) {
			processedOptions = $H(options);
			var ajaxSubmitButtonName = processedOptions.get('_asbn');
			if(ajaxSubmitButtonName != null) {
				processedOptions.set('_asbn', null);
				var parameters = processedOptions.get('parameters');
				if(parameters === undefined || parameters == null) {
					var formSerializer = processedOptions.get('_fs');
					var serializedForm = $(form).toQueryString();
					processedOptions.set('parameters', serializedForm + 
							'&' + AjaxSubmitButton.AjaxSubmitButtonNameKey + '=' + ajaxSubmitButtonName);
					
				} else {
					processedOptions.set('parameters', parameters + 
							'&' + AjaxSubmitButton.AjaxSubmitButtonNameKey + '=' + ajaxSubmitButtonName);
				}
			}
		}

		processedOptions = AjaxSubmitButton.defaultOptions(processedOptions);
		return processedOptions;

	},
	
	partial : function(updateContainerID, formFieldID, options) {},
	
	preventEnterKeySubmit : function() {
		$$('.m-a-s-b').each(function(el) {
			el.form.addEvent('keydown', function(e) {
				if(e.key == 'enter' && e.target.tagName != 'SELECT') {
					e.preventDefault();
				}
			});			
		});	
	},
	
	update : function(id, form, queryParams, options) {
		var updateElement = $(id);
		if(updateElement == null) {
			alert('There is no element on this page with the id "' + id + '".');
		}
		var finalUrl = AjaxSubmitButton.generateActionUrl(id, form, queryParams);
		var finalOptions = AjaxSubmitButton.processOptions(form, options);
		new Request.HTML({
				async : finalOptions.async || true,
				evalScripts : finalOptions.evalScripts || true,
				method : finalOptions.method || 'post',
				onSuccess : finalOptions.onSuccess || $empty,
				update : id,
				url : finalUrl
		}).send(finalOptions.parameters);
		
	},
	
	observeDescendentFields : function(updateContainerID, containerID, observeFieldFrequency, partial, observeDelay, options) {},
	observeField : function(updateContainerID, formFieldID, observeFieldFrequency, partial, observeDelay, options) {}
	
};

var AjaxObserveDelayer = new Class({
	delay : null,
	waiting : null,
	lastValueChange : null,
	submitFunction: null,
	element : null,
	value : null
});

var AjaxPeriodicUpdater = new Class({
	initialize : function(id) {
		this.id = id;
	}
});

AjaxPeriodicUpdater.implement({
	
	start: function() {
		var actionUrl = $(this.id).get('updateUrl');
		actionUrl = actionUrl.addQueryParameters('__updateID=' + id);
		this.updater = new Request({
			url : actionUrl,
			frequency : 2.0
		});
	}
	
});

var AOD = AjaxOnDemand;
var AIP = AjaxInPlace;
var AUC = AjaxUpdateContainer;
var AUL = AjaxUpdateLink;
var ASB = AjaxSubmitButton;
