/**
 * Autosubmit
 * 
 * Usage:
 *	1) Class your element as class="autosubmit"
 *	
 *  Parameters:
 *    Pre-conditions:
 *       > autosubmit-noblank: autosubmit action is no performed if the field is empty
 *       
 *    Post-conditions:
 *       > autosubmit-reset: reset the form after submitting (useful if onsubmit listener is an ajax request, etc)
 *       > autosubmit-blur: blur from the element after submitting (useful for coupling with magiclabel)
 *
 *	General notes:
 *		-Currently only setup to work with the onchange event
 *
 * @author Justin Johnson <justin@booleangate.org>, justin@fryewiles.com
 * @version 0.0.2a Beta 20070619
 * 
 * @requires Behaviour <http://www.bennolan.com/behaviour/>
 * 
 * TODO:
 *   Add support for event listener options via flags in the class string.
 *   Example: class="autosubmit autosumbit-onchange autosumbit-onclick autosumbit-onkeyup" etc
 */

var action_autosubmit = {
	'.autosubmit' : function(element) {
		element.onchange = function(event) {
			var t = this.parentNode;
			
			var params = {
				noblank: this.className.indexOf('autosubmit-noblank') != -1,
				reset:   this.className.indexOf('autosubmit-reset') != -1,
				blur:    this.className.indexOf('autosubmit-blur') != -1
			};
			
			if ( params.noblank && this.value == '' ) {
				return;
			}
			
			// Find a parent node with a submit attribute
			while ( t ) {
				if ( typeof t.submit == 'function' ) {
					t.submit();
					
					if ( params.reset ) {
						t.reset();
					}
					
					if ( params.blur ) {
						t.blur();
					}
					
					// All done
					break;
				}
				
				t = t.parentNode;
			}
		}
	}
};


Behaviour.register(action_autosubmit);