jQuery(document).ready(function() {

    var div_error = "<div   id='mobracer_comment_firewall_error' \n\
                            style='color:#b50707 !important; font-size:90%; font-weight:normal;'>\n\
                    </div>";    
    jQuery('#comment').after(div_error);
    
    var go = false;
    jQuery('#commentform').submit(function() {
        
        if(go == true) {
            return true;
        }

        jQuery('#comment_error_message').css('display', 'none');//reset
        var comment = jQuery("#comment").val(); //comment text
        
        if(comment == "") { //display error if comment text is empty
            jQuery('#comment').css('border','1px solid #b50707');
            jQuery('#mobracer_comment_firewall_error').text('Hey, write something!');
        } else {
            jQuery.post(MR_API + 'detect_bad_words', {text: comment}, function(hasBadWords) {
                    if(hasBadWords == '1') {
                        jQuery("#comment_error_message").text("No bad words, please!");
                        jQuery('#comment_error_message').css('display', 'block');
                        return false;
                    }
                    go = true;                    
                    jQuery('#commentform').submit();
            });            
        }
        return false;
    });

    jQuery('#comment').blur(function() {
        if(jQuery('#comment').val().length > 0) {
            jQuery('#comment').css('border', '');
            jQuery('#mobracer_comment_firewall_error').text('');
        }
    });
});