    $(function(){
    
        var enterHandler = function(e){
            if (e.keyCode == 13) 
                $('#contact-form').submit();
        };
        
        var serverError = function(container){
           
            container.html("Sorry! An error has occurred. Feel free to try again. I'll work on it in the meantime. ");
            container.slideDown();
        };
        
        $('#contact-form')        
                // Clear fields to prevent Google Toolbar conflicts
                .find("input[type=text]")
                
                // Add enter-key submission to text fields
                .keydown(enterHandler).attr("value", "").end()
                .find("textarea").html("").end()        
                
                // Add validation
                .validate({
                    rules: {
                        "contact[first_name]": {
                            required: true,
                            remote: "/validate_contact_field"
                        },
                        "contact[last_name]": {
                            required: true,
                            remote: "/validate_contact_field"
                        },
                        "contact[email]": {
                            required: true,
                            remote: "/validate_contact_field"
                        },
                        "contact[message]": {
                            required: true,
                            remote: "/validate_contact_field"
                        }
                    },
                    submitHandler: function(){
                        var f = $('#contact-form');
                        var msgs = $('#contact-messages');
                        var errors = $('#contact-errors');
                        
                        f.ajaxSubmit({
                            success: function(){
                                if (arguments[0] === "true") {
                                    errors.slideUp();
                                    
                                    msgs.html('Your message has been sent. Thanks for your interest!');
                                    msgs.slideDown();
                                    
                                    f.slideUp();
                                }
                                else {
                                    serverError(errors);
                                }
                            },
                            error: function(){
                                serverError(errors);
                            },
                            complete: function() {
                               
                            }
                        });
                    }
            
                });
        
        
        $('#contact-form button[type=submit]').click(function(e){

            try {
                $('#contact-form').submit();
            }
            catch (exception)
            {
        
            }
            
            return false;
        });
        
    });
