/***********************************************************************\
* File:     MoJoForm.js                                                 *
* Date:     Febuary 22, 2009                                            *
* Author:   James Carpenter                                             *
* Modified: Rory Dueck - added form to validate function                *
* 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     *
*               validate: function(e) { return true; }    // default    *
*			});                                                         *
*                                                                       *
* To Use Jquery Validator: do NOT pass it to the validate param above   *
*           $("#mojoForm").validate();                                  *
\***********************************************************************/

(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) 
        {
			// prevent the default action of the clicked element from firing (ex. a tag won't navigate to page)
			e.preventDefault();
			
            // if the form passes validation
            if (opts.validate(form))
            {
                // 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',
        validate: function() { return true; }
    };
 
})(jQuery);

