$(document).ready(function(){

/* ---------- Functions ---------- */

    /**
     * Toggle a form field's "focus" style.
     *
     * @param   object  ev  The triggering event.
     * @author  Dianne Castillo
     *
     */
    var toggleFormFocusStyle = function(ev) {

        // Get form field
        var formField = ev.target;
        var formFieldContainer = $(formField).parent('span');

        // No wrapper: bail out
        if($(formFieldContainer).length === 0) {
            return;
        }
        else {
            // Detect event type
            switch(ev.type) {
                case 'focus':
                    formFieldContainer.attr('class', 'text-focus');
                    break;
                default:
                    formFieldContainer.attr('class', 'text');
                    break;
            }
        }

    }; // END: toggleFormFocusStyle()

/* ---------- Events ---------- */

    // Toggle "focus" styles on form fields
    $('span[class^="text"] input').focus(toggleFormFocusStyle);
    $('span.text input').blur(toggleFormFocusStyle);
    $('span[class^="text"] textarea').focus(toggleFormFocusStyle);
    $('span.text textarea').blur(toggleFormFocusStyle);

    // Detect form wrapper
    var formWrapper = 'table.form-wrapper';
    if($(formWrapper).length > 0) {    
        // Add red star to mandatory fields
        $('.required').parents('td').siblings('th').append('<abbr title="required">*</abbr>');
    }

    // Specific form: Donations
    if($('form#cart').length > 0) {
        // Form validation
        $('#cart').validate();
        // Additional rules for form validation:
        // "Email Confirmation" must match "Email"
        $('#shipemailconfirm').rules('add', {
            'equalTo': '#shipemail',
            'messages': {
                'equalTo': 'Email does not match.'
            }
        });
    }

});
