var commentsNum = 0;
var commentsIndex = 0;
var form_id = 0;
var comments;
var divNum = 0;
var iface = 'comments';

function t(txt) {
    txt = txt.toString();
    txt = txt.replace(/</g,"&lt;");
    txt = txt.replace(/>/g,"&gt;");
    txt = txt.replace(/"/g,'\'');
    txt = txt.replace(/\n/g,"<br />");
    return txt;
}

function ProcessComments(abuse_email, abuse_title, abuse_url, story_urn, filters, i)
 {
    if (comments.comments[i])
    {
        if (comments.comments[i].state != 'UNMODERATED') {
            comments.comments[i].text = FilterProfanity(comments.comments[i].text, filters);
            comments.comments[i].author_name = FilterProfanity(comments.comments[i].author_name, filters);
            comments.comments[i].text = unescape(comments.comments[i].text);
            //abuse_title = escape(abuse_title);
        
            document.getElementById('comments_dump').innerHTML += '<div class="commentContainer"><div class="name">'+ t(comments.comments[i].author_name) + '</div><div class="time">' + t(humane_date(comments.comments[i].time)) + '</div><form action="http://dv1.gatehousemedia.com/submit/abuse.php" method="post" style="display: none;" id="report_abuse_' + t(form_id) + '"><input type="hidden" name="email_address" value="' + t(abuse_email) + '" /><input type="hidden" name="story_url" value="' + t(abuse_url) + '" /><input type="hidden" name="story_comments_url" value="http://commenting.ghm.zope.net/comments/' + t(story_urn) + '" /><input type="hidden" name="story_title" value="' + t(abuse_title) + '" /><input type="hidden" name="comment_text" value="' + t(comments.comments[i].text) + '" /><input type="hidden" name="comment_author" value="' + t(comments.comments[i].author_name) + '" /></form><a class="report_abuse" href="#" onclick="document.getElementById(\'report_abuse_'+t(form_id)+'\').submit(); return false;" title="Report Abuse">Report Abuse</a><div class="comment"><div class="comment">' + t(comments.comments[i].text) + '</div></div></div>'; 
        }    
        form_id++;
        i++;
        setTimeout('ProcessComments("' + abuse_email + '","' + abuse_title + '","' + abuse_url + '","' + story_urn + '","' + filters + '",' + i + ')', 40);
    }
    else
    {
            document.getElementById('comments_dump').style.display = 'block';
            document.getElementById('comments_loading').style.display = 'none';
    }
}

function Logout(anon, site_id) {
    if (iface == 'comments') {
        $.post('/!/commenting/users/logout',{});
        $('#loginRegisterMessage').html('<span class="success">Logged out successfully.</span>');
        AuthForm(anon, site_id);
    }
    else {
        if (logged_in == true) {
            $('#ugcg-content').html('<div class="loading container"><img src="http://global.static.ghm.zope.net/resources/ghns_2/images/loading-child.gif" alt="Logging out..." title="Logging out..." />Logging out...</div>');
            $.post('/!/commenting/users/logout', function() {logged_in = false; UGCPage('home');});
        }
    }
    return false;
}

function LoginSuccess(user, anon, site_id, com_page) {
    if (iface == 'profile') {
        logged_in = true;
        userObj = user;
        UGCPage('home');
    }
    else {
        $('#loginRegisterContainer').html('');
        $('#loginRegisterMessage').html('<span class="success">Logged in successfully as ' + t(user['username']) + '.</span>');
        AddCommentForm(user, anon, site_id, com_page);
    }
    return false;
}

function LoginError() {
    $('#submit_login').attr('disabled',false);
    $('#submit_login').attr('value','Login');
    $('#loginRegisterMessage').html('<span class="error">Login unsuccessful. Please check your username and password and try again.</span>');
    return false;
}

function SubmitResetSuccess(email) {
    $('#loginRegisterMessage').html('<span class="success">We\'ve sent an email to ' + t(email) + ' containing a URL you\'ll need to visit to reset your password. You should receive the e-mail within the next few minutes. <a href="#" onclick="$(\'#loginRegisterMessage\').html(\'\'); return false;">OK</a></span>');
    return false;
}

function SubmitResetError(email, site_id) {
    $('#loginRegisterMessage').html('<span class="error">We can\'t seem to find an account associated with the email ' + t(email) + '. <a href="#" onclick="ResetPasswordForm(\'' + t(site_id) + '\'); return false;">Try again</a> or <a href="#" onclick="$(\'#loginRegisterMessage\').html(\'\'); return false;">cancel</a></span>');
    return false;
}

function SubmitResetPassword(site_id) {
    if ($('#reset_email').attr('value') != '') {
        $('#submit_reset_password').attr('disabled',true);
        $('#submit_reset_password').attr('value','Please wait...');
        var reset_email = $('#reset_email').attr('value');
        $.ajax({
            type: 'POST',
            url: '/!/commenting/users/reset_password',
            data: {
                'site_id': site_id,
                'email': reset_email
            },
            success: function() {SubmitResetSuccess(reset_email)},
            error: function() {SubmitResetError(reset_email, site_id)}
        });
        return false;
    }
}

function ResetPasswordForm(site_id) {
    var resetPasswordForm = '';
    resetPasswordForm += '<div id="reset-password" class="clearfix">';
    resetPasswordForm += '    <form id="resetPasswordForm" method="post">';
    resetPasswordForm += '        <label for="reset_email">Email address:</label>';
    resetPasswordForm += '        <input type="text" id="reset_email" />';
    resetPasswordForm += '        <input type="submit" id="submit_reset_password" value="Reset password" onclick="SubmitResetPassword(\'' + t(site_id) + '\'); return false;" />';
    resetPasswordForm += '        <a href="#" onclick="$(\'#loginRegisterMessage\').html(\'\'); return false;">Cancel</a>';
    resetPasswordForm += '    </form>';
    resetPasswordForm += '</div>';
    $('#loginRegisterMessage').html(resetPasswordForm);
    return false;
}

function SubmitLogin(anon, site_id, com_page) {
    $('#submit_login').attr('disabled',true);
    $('#submit_login').attr('value','Please wait...');
    if ($('#login_username').attr('value') != '' &&
            $('#login_password').attr('value') != '') {

        var data = {
            'login.login': $('#login_username').attr('value'),
            'login.password': $('#login_password').attr('value'),
            'login.domain': 'UGC'
        };
        
        $.ajax({
            type: 'POST',
            url: $('#commentsLogin').attr('action'),
            dataType: 'json',
            data: data,
            success: function(resp) {LoginSuccess(resp, anon, site_id, com_page)},
            error: function() {LoginError()}
        });
        return false;
    }
    else {
        $('#loginRegisterMessage').html('<span class="error">Please enter both your username and password.</span>');
        $('#submit_login').attr('disabled',false);
        $('#submit_login').attr('value','Login');
        return false;
    }
}

function RegistrationError() {
    $('#loginRegisterMessage').html('<span class="error">That email address is already associated with an account.</span>');
    $('#submit_registration').attr('disabled',false);
    $('#submit_registration').attr('value','Register');
    return false;
}

function RegistrationSuccess(reg_email) {
    $('#loginRegisterContainer').html('');
    $('#loginRegisterMessage').html('<span class="success">Thanks for registering. We\'ve sent an email to ' + t(reg_email) + ' containing a URL you\'ll need to visit to verify your account. You should receive the e-mail within the next few minutes.</span>');
    return false;
}

function SubmitRegistration() {
    $('#submit_registration').attr('disabled',true);
    $('#submit_registration').attr('value','Please wait...');
    if ($('#reg_tos_flag').attr('checked') &&
            $('#reg_age_flag').attr('checked') &&
            $('#reg_email').attr('value') != '' &&
            $('#reg_first_name').attr('value') != '' &&
            $('#reg_last_name').attr('value') != '') {
        var reg_email = $('#reg_email').attr('value');
        var reg_first_name = $('#reg_first_name').attr('value');
        var reg_last_name = $('#reg_last_name').attr('value');
        var reg_site_id = $('#reg_site_id').attr('value');
        var data = {
            'email': reg_email,
            'first_name': reg_first_name,
            'last_name': reg_last_name,
            'site_id': reg_site_id,
            'tos_flag': '1',
            'age_flag': '1'
        };

        $.ajax({
            type: 'POST',
            url: '/!/commenting/users/register',
            data: data,
            success: function() {RegistrationSuccess(reg_email)},
            error: function() {RegistrationError()}
        });
        return false;
    }
    else {
        $('#submit_registration').attr('disabled',false);
        $('#submit_registration').attr('value','Register');
        $('#loginRegisterMessage').html('<span class="error">You\'ll need to complete all of the registration fields in order to continue.</span>');
        return false;
    }
}

function AddCommentForm(user, anon, site_id, com_page) {
    var content = '';
    content += '<div class="addCommentContainer">';
	content += '	<a name="addComment"></a>';
	content += '	<form id="addCommentForm" method="post" tal:attributes="action string:/!/commenting/comments/${comID}/add">';
	content += '		<div class="addCommentBlock">';
	content += '			<div class="commentTitle">Leave a Comment:</div>';
	content += '			<div class="poolRulesDisclaimer">';
	content += '				Before diving in to post a comment, be sure to read and follow the <a href="#pool-rules" title="pool rules">pool rules</a>.';
	content += '			</div>';
	content += '            <div class="loggedInAs">Logged in as: ' + t(user['username']) + ' <a href="#" onclick="Logout(' + anon + ', \'' + t(site_id) + '\'); return false;">Logout</a></div>';
	content += '			<textarea id="comment" name="comment" class="commentEmpty" cols="50" rows="10" style="margin: 0;" onfocus="SwapStyle(\'comment\',\'commentFocused\')" onblur="if (this.value == \'\') {SwapStyle(\'comment\',\'commentEmpty\')} else {SwapStyle(\'comment\',\'comment\')}"></textarea>';
	content += '			<div class="poolRules">';
	content += '				<a name="pool-rules"></a>';
	content += '				<div class="poolRulesTitle">Pool Rules</div>';
	content += '				<ol>';
	content += '					<li>Keep it clean.</li>';
	content += '					<li>Stay on topic.</li>';
	content += '					<li>Be honest and accurate.</li>';
	content += '					<li>No personal attacks. Don\'t bash anyone based on their race, creed, heritage, or orientation.</li>';
	content += '					<li>Don\'t say anything here you wouldn\'t say in front of your mother at the dinner table.</li>';
	content += '					<li>Use the \'Report Abuse\' button when you spot a rule violation. (Don\'t report comments just because you disagree.)</li>';
	content += '				</ol>';
	content += '			</div>';
	content += '			<div class="termsDisclaimer">';
	content += '				Participation in online discussion is subject to our <a href="http://www.gatehousemedia.com/terms_of_use" title="Terms of Use">Terms of Use</a>. By commenting, you agree to be bound by these terms.';
	content += '			</div>';
	content += '			<input id="submit" type="button" name="submit" class="submit" onClick="AddComment(\'' + t(user['username']) + '\', \'' + t(site_id) + '\', \'' + t(com_page) + '\')" value="Add Comment" />';
	content += '		</div>';
	content += '	</form>';
	content += '</div>';
	$('#comments_ui').html(content);
	return false;
}

function AuthForm(anon, site_id) {
    var login_url = '/!/commenting/users/login';
    if (anon == true) {
        AddCommentForm('anonymous', anon, site_id);
    }
    else {
        var content = '';
        content += '<div id="loginRegisterContainer" class="clearfix">';
        if (iface == 'comments') {
                content += '    <div class="commentTitle">Login or register to post a comment:</div>';
        }
        content += '    <form id="commentsLogin" method="post" action="' + login_url + '">';
        content += '        <div class="field-login">';
        content += '            <h2>Login</h2>';
        content += '            <div class="formHolder clearfix">';
        content += '                <label for="login_username">Username:</label>';
        content += '                <input type="text" id="login_username" />';
        content += '            </div>';
        content += '            <div class="formHolder clearfix">';
        content += '                <label for="login_password">Password:</label>';
        content += '                <input type="password" id="login_password" />';
        content += '            </div>';
        content += '            <div class="formHolder clearfix">';
        content += '                <div class="reset-password"><a href="#" onclick="ResetPasswordForm(\'' + t(site_id) + '\'); return false;">Forgot password</a></div>';
        content += '                <input type="submit" id="submit_login" value="Login" onclick="SubmitLogin(' + anon + ', \'' + t(site_id) + '\', \'' + t(com_page) + '\'); return false;" />';
        content += '            </div>';
        content += '        </div>';
        content += '    </form>';
        content += '    <form id="commentsRegister" method="post" action="/!/commenting/users/register">';
        content += '        <div class="field-register">';
        content += '            <h2>Register</h2>';
        content += '            <div class="formHolder clearfix">';
        content += '                <label for="reg_email">Email:</label>';
        content += '                <input type="text" id="reg_email" />';
        content += '            </div>';
        content += '            <div class="formHolder clearfix">';
        content += '                <label for="reg_first_name">First Name:</label>';
        content += '                <input type="text" id="reg_first_name" />';
        content += '            </div>';
        content += '            <div class="formHolder clearfix">';
        content += '                <label for="reg_last_name">Last Name:</label>';
        content += '                <input type="text" id="reg_last_name" />';
        content += '            </div>';
        content += '            <div class="formHolder clearfix">';
        content += '                <input type="checkbox" id="reg_tos_flag" />';
        content += '                <label for="reg_tos_flag">I agree to the <a target="_blank" href="http://www.gatehousemedia.com/terms_of_use">terms of use</a></label>';
        content += '            </div>';
        content += '            <div class="formHolder clearfix">';
        content += '                <input type="checkbox" id="reg_age_flag" />';
        content += '                <label for="reg_age_flag">I am over 13 years of age</label>';
        content += '            </div>';
        content += '            <div style="font: 11px Arial,Sans-Serif; color: #8A8A8A; margin-top: 10px;">NOTE: Your inbox must accept emails from "no-reply@gatehousemedia.com"</div>';
        content += '            <div class="formHolder clearfix">';
        content += '                <input type="hidden" id="reg_site_id" value="' + t(site_id) + '" />';
        content += '                <input type="submit" id="submit_registration" value="Register" onclick="SubmitRegistration(); return false;" />';
        content += '            </div>';
        content += '        </div>';
        content += '    </form>';
    	content += '</div>';
    	if (iface == 'comments') {
    	        $('#comments_ui').html(content);
    	}
    	else {
    	        return content;
        }
    }
	return false;
}

function VerifyRegistrationForm(data, verify_id) {
    $('#verification_message').html('');
    var content = '';
    content += '<div class="verifyRegistration">';
    content += '    <form id="verifyRegistrationForm" method="post">';
    content += '        <fieldset id="account_settings">';
    content += '            <div class="formHolder clearfix">';
    content += '                <label for="vfy_username">Username <span class="required">*</span>:</label>';
    content += '                <input type="text" id="vfy_username" />';
    content += '            </div>';
    content += '            <div class="formHolder clearfix">';
    content += '                <label for="vfy_password">Password <span class="required">*</span>:</label>';
    content += '                <input type="password" id="vfy_password" />';
    content += '            </div>';
    content += '            <div class="formHolder clearfix">';
    content += '                <label for="vfy_confirm_password">Confirm Password <span class="required">*</span>:</label>';
    content += '                <input type="password" id="vfy_confirm_password" />';
    content += '            </div>';
    content += '            <div class="formHolder clearfix">';
    content += '                <label for="vfy_first_name">First Name <span class="required">*</span>:</label>';
    content += '                <input type="text" id="vfy_first_name" value="' + t(data['first_name']) + '" />';
    content += '            </div>';
    content += '            <div class="formHolder clearfix">';
    content += '                <label for="vfy_last_name">Last Name <span class="required">*</span>:</label>';
    content += '                <input type="text" id="vfy_last_name" value="' + t(data['last_name']) + '" />';
    content += '            </div>';
    content += '            <div class="formHolder clearfix">';
    content += '                <label for="vfy_email">Email <span class="required">*</span>:</label>';
    content += '                <input type="text" disabled="disabled" id="vfy_email" value="' + t(data['email']) + '" />';
    content += '            </div>';
    content += '        </fieldset>';
    content += '        </fieldset>';
    content += '        <div class="submit">';
    content += '            <input type="hidden" value="' + t(verify_id) + '" id="verify_id" />';
    content += '            <input type="submit" id="verification_submit" value="Submit" onclick="SubmitVerification(); return false;" />';
    content += '        </div>';
    content += '    </form>';
    content += '</div>';
    
    $('#verification_ui').html(content);
    return false;
}

function VerificationSuccess() {
    $('#verification_message').html('<span class="success">Thank you for verifying your registration. Login using your new username and password anytime to add your comments to a story.</span>');
    $('#verification_ui').html('');
    return false;
}

function VerificationError(resp) {
    $('#verification_message').html('<span class="error">' + t(resp) + '</span>');
    $('#verification_submit').attr('disabled',false);
    $('#verification_submit').attr('value','Submit');
    return false;
}

function SubmitVerification() {
    if ($('#verify_id').attr('value') != '' &&
            $('#vfy_username').attr('value') != '' &&
            $('#vfy_password').attr('value') != '' &&
            $('#vfy_confirm_password').attr('value') != '' &&
            $('#vfy_first_name').attr('value') != '' &&
            $('#vfy_last_name').attr('value') != '' &&
            $('#vfy_email').attr('value') != '') {
        if ($('#vfy_username').attr('value').match(/[^A-Za-z0-9-_ ]+/) == null && $('#vfy_password').attr('value').match(/[^A-Za-z0-9-_ ]+/) == null) {
            $('#verification_submit').attr('disabled',true);
            $('#verification_submit').attr('value','Please wait...');
            var data = {
                'id':                 $('#verify_id').attr('value'),
                'username':     $('#vfy_username').attr('value'),
                'password':     $('#vfy_password').attr('value'),
                'confirm':        $('#vfy_confirm_password').attr('value'),
                'first_name': $('#vfy_first_name').attr('value'),
                'last_name':    $('#vfy_last_name').attr('value'),
                'email':            $('#vfy_email').attr('value')
            };
            $.ajax({
                type: 'POST',
                url: '/!/commenting/users/verify',
                data: data,
                success: function() {VerificationSuccess()},
                error: function(resp) {VerificationError(resp.responseText)}
            });
            return false;
        }
        else {
            $('#verification_message').html('<span class="error">You have invalid characters in either your username or password.</span>');
            return false;
        }
    }
    else {
        $('#verification_message').html('<span class="error">You\'ll need to complete all of the required verification fields in order to continue.</span>');
        return false;
    }
}

function VerifyError() {
    $('#verification_message').html('<span class="error">Sorry, but that\'s an invalid verification URL. Please go back to the e-mail message about registration and make sure that you pick up the equal sign (=) at the end of the url. If the url still does not work, contact your local site manager.</div>');
    return false;
}

function VerificationUI(verify_id) {
    $('#verification_message').html('<span class="loading">Loading verification interface...</span>');
    $.ajax({
        type: 'GET',
        dataType: 'json',
        url: '/!/commenting/users/get_data',
        data: {'id': verify_id},
        success: function(data) {VerifyRegistrationForm(data, verify_id)},
        error: function() {VerifyError()}
    });
    return false;
}

function ResetSuccess() {
    $('#reset_ui').html('');
    $('#reset_message').html('<span class="success">You\'ve successfully changed your password.</span>');
    return false;
}

function ResetError(resp) {
    $('#reset_submit').attr('disabled',false);
    $('#reset_submit').attr('value','Submit');
    $('#reset_message').html('<span class="error">' + t(resp.responseText) + '</span>');
    return false;
}

function SubmitReset(reset_id) {
    if ($('#reset_password').attr('value') != '' &&
            $('#reset_confirm_password').attr('value') != '') {
        $('#reset_submit').attr('disabled',true);
        $('#reset_submit').attr('value','Please wait...');
        var data = {
            'id': reset_id,
            'password': $('#reset_password').attr('value'),
            'confirm': $('#reset_confirm_password').attr('value')
        };
        $.ajax({
            type: 'POST',
            url: '/!/commenting/users/change_password',
            data: data,
            success: function() {ResetSuccess()},
            error: function(resp) {ResetError(resp)}
        });
        return false;
    }
    else {
        $('#reset_message').html('<span class="error">Please fill out both the \'password\' and \'confirm password\' fields.</span>');
        return false;
    }
}

function ResetUI(reset_id) {
    var content = '';
    content += '<div class="resetEmail">';
    content += '    <form id="resetEmailForm" method="post">';
    content += '        <fieldset id="account_settings">';
    content += '            <div class="formHolder clearfix">';
    content += '                <label for="reset_password">Password <span class="required">*</span>:</label>';
    content += '                <input type="password" id="reset_password" />';
    content += '            </div>';
    content += '            <div class="formHolder clearfix">';
    content += '                <label for="reset_confirm_password">Confirm Password <span class="required">*</span>:</label>';
    content += '                <input type="password" id="reset_confirm_password" />';
    content += '            </div>';
    content += '        </fieldset>';
    content += '        <div class="submit">';
    content += '            <input type="submit" id="reset_submit" value="Submit" onclick="SubmitReset(\'' + t(reset_id) + '\'); return false;" />';
    content += '        </div>';
    content += '    </form>';
    content += '</div>';
    $('#reset_ui').html(content);
    return false;
}

function CommentsUI(com_page, abuse_email, abuse_title, abuse_url, story_urn, profanity_filter, anon, site_id) {
    $.getJSON (
	    com_page + '/as_json',
	    function(data) {GetCommentsSuccess(data, t(abuse_email), t(abuse_title), t(abuse_url), t(story_urn), t(profanity_filter));}
	);
	
	$('#comments_ui').html('<span class="loading">Loading commenting interface...</span>');
	
    $.ajax({
        type: 'GET',
        url: '/!/commenting/users/check_status',
        data: {},
        dataType: 'json',
        success: function(resp) {AddCommentForm(resp, anon, site_id, com_page);},
        error: function() {AuthForm(anon, site_id, com_page)}
    });
    return false;
}

function GetCommentsSuccess(data, abuse_email, abuse_title, abuse_url, story_urn, filters)
 {
        document.getElementById('comments_loading_outside').style.display = 'none';
        comments = data;
        
        $('#tracker-beacon').replaceWith('<img src="' + $('#tracker-beacon').attr('href').replace('__comment_count__', comments.count) + '" alt="" title="" />');
        
        if (comments.commenting_status == true) {
                document.getElementById('comments').style.display = 'block';
        }

        if (comments.count > 0)
        {
                document.getElementById('commentsCount').innerHTML = 'Comments (' + t(comments.count) + ')';
                commentsNum = comments.count;
                document.getElementById('comments_dump').style.display = 'none';
                document.getElementById('comments_loading').style.display = 'block'; 
                ProcessComments(abuse_email, abuse_title, abuse_url, story_urn, filters, commentsIndex);
        }
        else
        {
                document.getElementById('commentsCount').innerHTML = 'Comments (' + t(comments.count) + ')';
                document.getElementById('comments_dump').innerHTML = '';
        }
}

function AddComment(user, site_id, com_page) {
    if ($('#comment').attr('value') != '') {
        $('#submit').attr('disabled',true);
        $('#submit').attr('value','Please wait...');
        //var author_name_string = escape(user);
        //var comment_string = escape($('#comment').attr('value'));
        var author_name_string = user;
        var scope_string = site_id;
        var comment_string = $('#comment').attr('value').replace(/[^\x0d\x0a\x20-\x7e\t]/g,'');

        $.ajax({
            type: 'POST',
            url: com_page + '/submit',
            data: {
                'author_name': author_name_string,
                'text': comment_string,
                'scope': scope_string
            },
            success: function() {
                AddCommentSuccess(author_name_string, comment_string);
            },
            error: function() {
                AddCommentError();
            }
        });
        return false;
    }
    else {
        return false;
    }
} 

function AddCommentError() {
    $('#submit').attr('disabled',false);
    $('#submit').attr('value','Add Comment');
    $('#loginRegisterMessage').html('<span class="error">Sorry, but there was a problem while trying to add your comment. Please try again.</span>');
    return false;
}

function AddCommentSuccess(author_name, comment) {
    $('#loginRegisterMessage').html('');
    $('#submit').attr('disabled',false);
    $('#submit').attr('value','Add Comment');
    //comment = unescape(comment);

    document.getElementById('comment').value = '';
    commentsNum = commentsNum + 1;
    document.getElementById('commentsCount').innerHTML = 'Comments (' + t(commentsNum) + ')';
    SwapStyle('comment', 'commentEmpty');
    var divID = 'commentDiv_' + divNum;
    document.getElementById('comments_dump').innerHTML += '<div class="commentContainer" id="' + t(divID) + '" style="display: none;"><div class="name">' + t(author_name) + '</div><div class="time"></div><div class="comment"><div class="comment">' + t(comment) + '</div><div class="commentDisclaimer">Please note: It may take up to 15 minutes for your comment to appear on this page.</div></div></div>';
    document.getElementById(divID).style.display = 'block';
    divNum++;
    return false;
}

function FilterProfanity(t, filters)
 {
        filters = filters.split(',');
        for (var i = 0; i < filters.length; i++)
        {
                var repl = "";

                for (var o = 0; o < filters[i].length; o++) {
                        repl += "*";
                }

                if (filters[i] == 'ass') {
                        var backRef = true;
                        var filtersRegEx = '([^A-Za-z]|^)ass([^A-Za-z]|$)';
                }
                else {
                        var backRef = false;
                        var filtersRegEx = filters[i];
                }

                var regex = new RegExp(filtersRegEx, 'gi');

                if (backRef == true) {
                        while (regex.test(t) == true) {
                                t = t.replace(regex, '$1' + repl + '$2');
                        }
                }
                else {
                        t = t.replace(regex, repl);
                }
        }

        return t;
}

