// This function should be removed once render_user_generated_content is
// fully deployed since a back-end profanity filter is included.
//
// Searching for inappropriate words and replacing them with
// nonsense, e.g. "%#&@$!". This function should not be called until
// the document has finished loading.
// To further improve efficiency two versions are implemented,
// one for firefox, where built in support for xpath engine improves speed, and
// another, for other browsers, where the DOM tree is directly traversed.
// The later implementation skipps certain node names, ids and classes to save
// visits to nodes who certainly do not contain user generated content.

function filter_profanity(node)
{
    var isFF = (navigator.appName == 'Netscape');
    if(!isFF)
    {
        return filter_profanity_ie(node);
    }
    else
    {
        return filter_profanity_firefox(node);
    }
}

function filter_profanity_ie(node)
{
    var regexps = [new RegExp("\\b(ass|twat|cum|cock)\\b"),
                   new RegExp("asshole|bitch|cocksucker|cunt|faggot|fuck|nigger|penis|prick|pussy|shit|slut|vagina|whore", "ig")];

    var node_names_to_skip = [ "img",
                               "head",
                               "input",
                               "textarea",
                               "script",
                               "select"];
    if(get_object("pagebox")!= null)
    {
        node = document.getElementById("pagebox");
    }
    function run_regexps(s)
    {
        if((s =="")||(s== null)||(s=="undefined"))
        {
            return;
        }
        var i;
        for (var i = 0; i < regexps.length; i++)
        {
            filtered_word = s.replace(regexps[i], "%#&@$!");
        }
        return filtered_word;
    }

    function filter_node(node)
    {
        var node_name = node.nodeName.toLowerCase();
        var i;
        for (i = 0; i < node_names_to_skip.length; i++)
        {
            if (node_name == node_names_to_skip[i])
            {
                return;
            }
        }

        var num_children = node.childNodes.length;

        if (num_children == 0)
        {
            if (node.nodeValue)
            {
                node.nodeValue = run_regexps(node.nodeValue);
            }
        }
        else
        {
            for (i = 0; i < num_children; i++)
            {
                filter_node(node.childNodes[i]);
            }
        }
    }
    filter_node(node);
}


function filter_profanity_firefox(node)
{
    var regexps = [new RegExp("\\b(ass|twat|cum|cock)\\b"),
                   new RegExp("asshole|bitch|cocksucker|cunt|faggot|fuck|nigger|penis|prick|pussy|shit|slut|vagina|whore", "ig")];

    var node_names_to_skip = [ "img",
                               "head",
                               "textarea",
                               "input",
                               "script",
                               "select"];

    function run_regexps(s)
    {
            for (var i = 0; i < regexps.length; i++)
            {
                s = s.replace(regexps[i], "%#&@$!");
            }
            return s;
    }

    function make_xpath_query()
    {
        var query_string = "//*[not(self::head)"
        for (i = 0; i < node_names_to_skip.length; i++)
        {
            if(node_names_to_skip.length > i)
            {
                query_string+=" and not(self::"+node_names_to_skip[i]+") ";
            }
        }
        query_string+="]/text()";
        return query_string;
    }
    var nodes = document.evaluate(make_xpath_query(),
                                  document.body,
                                  null,
                                  7,
                                  null);

    for ( var i=0 ; i < nodes.snapshotLength; i++ )
    {
        if(nodes.snapshotItem(i).textContent!= 'undefined')
        {
           nodes.snapshotItem(i).textContent =
                run_regexps(nodes.snapshotItem(i).textContent);
        }
    }
}
