/*
Run submitAjaxForm in the submit method for the form:
i.e. 
		$(document).ready(function(){
			$('#MyForm').submitAjaxForm('post.php', {
				'errorElem': 'MyFormError',
				'successElem': 'MyFormSuccess'
			});
		});

You may also include an options array as a paramter to the submitAjaxForm function
	OPTIONS
	 errorElem: id of error container - defaults to MyFormError
	 successElem: id of success container - defaults to MyFormSuccess
	 loadingIcon: path to loading icon gif - defaults to ajax-loader.gif
	 
Expects a JSON response.
ERROR RESPONSE
['error': 'true'; 'content':'error string']
SUCCESS RESPONSE
['content':'success string']
*/ 
	
(function($) {

jQuery.fn.submitAjaxForm = function(url, settings){
	// setup defaults
	var config = {
		'errorElem': 'MyFormError',
		'successElem': 'MyFormSuccess',
		'loadingIcon': 'ajax-loader.gif'
	};
	
	// load settings
	if (settings) $.extend(config, settings);
	
	// create message containers
	this.before('<div id="AjaxFormLoading"><img src="'+config['loadingIcon']+'" alt="Loading Icon" /></div>');
	this.before('<div id="'+config['errorElem']+'"></div>');
	this.before('<div id="'+config['successElem']+'"></div>');
	$('#AjaxFormLoading').hide();
	$('#'+config['errorElem']).hide();
	$('#'+config['successElem']).hide();
	
	$('input[type=submit]').removeAttr('disabled');
	
	this.submit(function(){
		$('#AjaxFormLoading').show();
		var dataString = $(this).serializeArray();
		
		$('input[type=submit]').attr('disabled', 'disabled');
		
		$('#'+config['errorElem']).empty().hide();
		$('#'+config['successElem']).empty().hide();
		
		$.post(url, dataString, function(data){
			$('#AjaxFormLoading').hide();
			if (data.error == 'true')
			{
				$('#'+config['errorElem']).fadeIn('slow');
				$('#'+config['errorElem']).append(data.content);
				$('input[type=submit]').removeAttr('disabled');
			}
			else
			{
				$('#'+config['successElem']).fadeIn('slow');
				$('#'+config['successElem']).append(data.content);
			}
			
		}, 'json');
		return false;
	});
	
	this.each(function() {
		// element-specific code here
	});

	return this;
}

})(jQuery);
