/***********************************************************************\
* File:     MoJoForm.js                                                 *
* Date:     September 29, 2009                                          *
* Author:   James Carpenter                                             *
* Purpose:  To create a form system that prevents bot spam submission   *
*           Based on MoJoForm.js (2006)                                 *
*                                                                       *
* Usage:    $('#mojoForm').mojoForm( {                                  *
*			    action  : '/inc/MoJoMailer06.asp',                      *
*               method  : 'POST',                        // default     *
*               button  : '#btnSubmit',                  // default     *
*               mojoValidate: function() { return true; }    // default     *
*			});                                                         *
\***********************************************************************/

(function($) 
{    
    // begin plugin
    jQuery.fn.mojoForm = function(options) 
    {    
        // merge the options passed w/ the defaults
        var opts = jQuery.extend(jQuery.fn.mojoForm.defaults, options);
        var form = $(this);
        
        // define the submit action
        var submit = function(e) 
        {
            // if the form passes validation
            if (opts.mojoValidate())
            {
                // change the attributes and submit
                form.attr("action", opts.action);
                form.attr("method", opts.method);
                form.submit();
            }
        };
        
        
        // bind the click event of the supplied button selector
        $(opts.button).click( submit );
        
        // make the input fields submit the form on enter
        $('input', form).keypress( function(e) {
            if (e.which == 13) submit(e);
        });
    };
    
    // declare the default values for the plugin (make public to allow for override)
    jQuery.fn.mojoForm.defaults = {
        action  : '?',
        method  : 'POST',
        button  : '#btnSubmit',
        mojoValidate: function() { return true; }
    };
 
})(jQuery);

