92431516f8e0c2560d5c2bf57770706f341db9c79322396cd506ba66edf3d096

Source Code:

/*
 * Form
 *
 * This is an attempt at having a central form "object" to
 * handle common form needs. 
 *
 */

Forms = (function()
{
    var _ERROR_INVALID_FORMID = -104

    var _show_error = function( message, where, focus )
    {
        _hide_message(where);
        _hide_error(where);

        where = where || '';

        if( where === '' ){
            // Show a generic message
            $('#errorMessage').sfAlert({
                'style':'error',
                'hideClose': true,
                'message': message
            });
        } else {
            // Show an error on a specific element
            error_element = "<div class=\"form-error\" " + 
                            " id=\"" + where + "_error\">" + 
                            message + "</div>";
            // Do a special case for the recaptcha
            if( where === "captcha_response_field"){
                where_to_insert = "captcha_response_field";
                // The form group to add the error to is up a few parents
                $("#" + where_to_insert).parents(".captcha_container").addClass("has-error");
                
                // restart recaptcha if its loaded
                if (window.grecaptcha) grecaptcha.reset();

                // else fail the botdetect component
                else failCaptcha();

            } else {
                // Add the error class to the form group
                $("#" + where).parent().removeClass("has-success");
                $("#" + where).parent().addClass("has-error");
                where_to_insert = where;
            }
            $( error_element ).insertAfter("#" + where_to_insert);
            if (focus) {
                $("#" + where).focus();
            };

            // showOrHidePassword hack -----------------------------
            if( $("#" + where + "_show_hide_watcher").length > 0){
                //Before triggering the reset, restore the height of
                // the parent div
                $("#" + where).parent().css({
                    'height': ''
                });
                // Trigger the reset
                $("#" + where + "_show_hide_watcher").click();
                // Move the error message up a little bit
                $("#" + where + "_error").css({
                    'top': '-20px',
                    'position': 'relative'
                });
            }
        }

    }

    var _hide_error = function(where)
    {
        where = where || '';
        if( where !== ''){
            $("#" + where).parent().removeClass("has-error");
            $("#" + where + "_error").remove();
        } else {
            $(".has-error").removeClass("has-error");

            // Special case for recaptcha: remove red border
            var recaptcha_box = $('.g-recaptcha div div iframe');
            if (recaptcha_box) {
                recaptcha_box.css('border', '');
                recaptcha_box.css('border-radius', '');
                $('.recaptcha-checkbox-border').attr("style", "");
            }

            $("[id$=_error]").each( function( index ){
                // Get the id
                var this_id = $(this).attr('id');
                // Remove the element
                $(this).remove();
                // showOrHidePassword hack --------------------
                // Associated input field (slices off "_error")
                var assoc_field = this_id.replace('_error', '');
                // Reset the parent's height
                $("#" + assoc_field).parent().css({
                    'height': ''
                });
                // Trigger the reset
                $("#" + assoc_field + "_show_hide_watcher").click();
            });
            $('#errorMessage').hide();
        }
    }

    var _hide_message = function()
    {
        $('#statusMessage').hide();
    }

    // Function to show or hide the password from the user
    var existing_show_hide_links = [];
    var _showOrHidePassword = function(action, show_in, validator) {
        // Valid actions are "show" and "hide"
        action = action || "hide";

        // The element to add the link to
        show_in = show_in || '';
        if( show_in === ''){
            return;
        } else if( $("#" + show_in).length === 0) { 
            return;
        }

        // Destroy any validator attached to it
        $("#" + show_in).sfValidator("destroy");

        // The id of the link
        var show_hide_link = show_in + "_show_hide_link";

        // Set up the resize watcher
        if( $.inArray(show_hide_link, existing_show_hide_links) == -1){
            $(window).resize( function () {
                _showOrHidePassword("hide", show_in, validator); 
            });
        }
        existing_show_hide_links.push(show_hide_link);

        // Destroy it if it exists
        $("#" + show_hide_link).remove();

        var class_list = "uxicon";

        // Decide which text to display, 
        // and actually show or hide the password.
        if(action === "hide"){
            class_list += " uxicon-eye"
            $("#" + show_in)[0].type='password';
            $("#" + show_in).addClass("has-show-hide-password");
        } else {
            class_list += " uxicon-eye-close"
            $("#" + show_in)[0].type='text';
            $("#" + show_in).addClass("has-show-hide-password");
        }
        
        var style_list = "position: relative; " + 
                            "display: none;" + 
                            "cursor: pointer;";

        var element_output = "<span id=\"" + show_hide_link + "\" " + 
            "class=\"" + class_list + "\" " + 
            "style=\"" + style_list + "\"></span>";

        // First insert the element after the element it goes in
        $(element_output).insertAfter("#" + show_in);

        // Then reposition it so its in that element
        var css_top = -($("#" + show_in).outerHeight()) + 
                        ($("#" + show_in).outerHeight() / 2) - 
                        ($("#" + show_hide_link).outerHeight() / 2) -
                        4;

        var css_left = $("#" + show_in).outerWidth() - 
                        $("#" + show_hide_link).outerWidth() - 
                        14;

        // Save the parent's size
        var parent_height = $("#" + show_hide_link).parent().height();
        
        // Insert the element
        $("#" + show_hide_link).css({
            'left': css_left,
            'top': css_top,
            'display': 'inline-block'
        });

        // Restore the parent's size
        $("#" + show_hide_link).parent().height(parent_height);

        // Bind the click handler
        if(action === "hide"){
            $("#" + show_hide_link).unbind("click");
            $("#" + show_hide_link).click( function () {
                _showOrHidePassword("show", show_in, validator);
                $("#" + show_in).focus();
            });
        } else {
            $("#" + show_hide_link).unbind("click");
            $("#" + show_hide_link).click( function () {
                _showOrHidePassword("hide", show_in, validator); 
                $("#" + show_in).focus();
            });
        }

        // Re-bind the validator
        if(validator){
            validator();
        }

        // Add a watcher to see if there's an error.
        watcher_button = show_in + "_show_hide_watcher";
        watcher_button_html = "<button style=\"display:none;\" " + 
            "id=\"" + watcher_button + "\"></button>";
        $(watcher_button_html).insertAfter("#" + show_hide_link);
        $("#" + watcher_button).click( function() {
            _showOrHidePassword("hide", show_in, validator); 
            return false;
        });

    };

    return {
        'show_error': function(message, where, focus) {
            _show_error(message,where,focus);
        },
        'hide_error': function(where) {
            _hide_error(where);
        },
        'hide_message': function() {
            _hide_message();
        },
        'showOrHidePassword': function(action, show_in, validator) {
            _showOrHidePassword(action, show_in, validator);
        }
    }
} )();

// Chrome dev tools flag for debugging dynamic loaded js files
//# sourceURL=form.js