// JavaScript Document
// BY: Cameron McGregor
// CREATED: October 05, 2006
// ABSTRACT: Contains javascript functions used with the comment forms in EIS Platform

//Comment action being committed
var commentAction = "";

//comment ID acted upon for alerts, etc.
var actCommentID = -1;

//timer object for auto-switching menus
var tmr;

//tcm uri for currently submitted request (comments)
var currentTcmUri = "";

//Whether or not the textarea for post comments has been focused
var focused = false;

//A generic error message in case the submission did not work but we don't know why
var GENERIC_COMMENT_ERROR_MSG = "Failed to submit comment, error...";

//Update controls classes based on the base control set name.
//  Each form has a corresponding tab set (or span) that must be updated
function updateCtrlsClass(ctrlName, newClass)
{
	var ctrl = document.getElementById(ctrlName);
	if (ctrl)
	{
//		alert("ctrl found:" + ctrlName);
		ctrl.className = newClass;
	}
/*	//DEBUG
	else
		alert("ctrl NOT FOUND!!! " + ctrlName);
*/
}

//Show the "showForm" and hide all forms in the "hideForms" array
//  giving the appearance that the forms are overlayed
function showCommentForm(showForm, hideForms)
{
	//show the form
	updateCtrlsClass(showForm, "");

	//Set the selected tag (inactive)
	updateCtrlsClass(showForm + "_tab", "active");


	for (i = 0; i < hideForms.length; i++)
	{
		//Hide the form
		updateCtrlsClass(hideForms[i], "hidden");
		
		//Set the tabs as active
		updateCtrlsClass(hideForms[i] + "_tab", "");
		
	}
		
	return false;
}

//Show the existing comments form
function showExistingComments()
{
	clearTimeout(tmr);

	showCommentForm('view_comment', new Array('post_comment', 'alert_moderator'));

	//Hide the character counter message
	updateCtrlsClass("character_limit_tab", "hidden");

}

//Show the post comment form
function showPostComments()
{
	clearTimeout(tmr);

	updateCtrlsClass("comment_submit", "");
	updateCtrlsClass("comment_thanks", "hidden");
	
	showCommentForm('post_comment', new Array('view_comment', 'alert_moderator'));

	var el = document.getElementById('new_comment');
	if ((el)  && (el.value == ''))
	{
		focused = false;
		el.value = 'Enter comments...';
	}
	
	//Show the character counter message
	updateCtrlsClass("character_limit_tab", "");
}

//Show the confirm alert form
function showConfirmAlert()
{
	clearTimeout(tmr);
	
	updateCtrlsClass("alert_confirm", "");
	updateCtrlsClass("alert_thanks", "hidden");
	
	showCommentForm('alert_moderator', new Array('view_comment', 'post_comment'));
	
	//Hide the character counter message
	updateCtrlsClass("character_limit_tab", "hidden");
}

//Get the comments string
function doCommentString(uri)
{
	var result = "";
	var count = Number(doGetCommentCount(uri));
	if (count > 0)
	{
		
		if (count == 1)
			result = "comment"
		else
			result = "comments"

		result = "(" + String(count) + "&nbsp;" + result + ")";
	}
	

	return result;
}

//Get more comments from the server
function doMoreComments(tcmuri)
{
	commentAction = "more";
	
	return xmlCommentPost("/Comment", false, tcmuri);
}

//Get comment count from the server
function doGetCommentCount(tcmuri)
{
	commentAction = "count";
	
	return xmlCommentPost("/Comment", false, tcmuri);
}

//Post a user's comment
function doPostComment(tcmuri)
{
	//Do webtrends capture of post comment event
//	dcsMultiTrack("DCS.dcsuri","/Post A Comment.html","DCSext.activity","Post a Comment","DCSext.CommentedArticle",
//		getArticleTitle(),"WT.ti","Post A Comment","WT.AJAX","1","DCSqry","","DCSext.articleAuthor","");

//	DCS.dcsuri=DCS.dcsqry=WT.ti=WT.AJAX=DCSext.CommentedArticle=DCSext.activity="";

	commentAction = "post";
	
	xmlCommentPost("/Comment", true, tcmuri);
}

//Alert the moderator
function doAlertModerator(commentID)
{
	actCommentID = commentID;
	showConfirmAlert();
}

//Commit the moderator alert
function commitAlert(tcmuri)
{
	commentAction = "alert";
	xmlCommentPost("/Comment", true, tcmuri);
}

//Update the comment counter div contents
function updateCommentCounter(tcmuri)
{
	var countCtrl = document.getElementById("counter_" + tcmuri);
	if (countCtrl)
	{
		var newCount = document.getElementById("newcount_" + tcmuri);
		if (newCount)
			countCtrl.innerHTML = newCount.innerHTML;
	}
}

//Setup the Comment query to be sent to the server
function setupCommentQuery(tcmuri)
{
	//We will always have the action to be performed
	var result = "commentAction=" + commentAction + "&uri=" + tcmuri;
	
	if (commentAction == "post")
	{
		ctrl = document.getElementById("new_comment");
	
		if (ctrl)
			result = result + "&comment=" + escape(FilterExpletives(ctrl.value));
	}
	else if (commentAction == "alert")
	{
		result = result + "&commentID=" + actCommentID;
	}
	
	return result;
}

//Post an XML HTTP Request for AJAX
// This code was taken from common.js originally written by Will Price
function xmlCommentPost(strURL, doAsynchronous, tcmuri) 
{
    var xmlHttpReq = false;
    var query = setupCommentQuery(tcmuri);
    
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlHttpReq.open('GET', strURL + '?' + query , doAsynchronous);
    xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    //Do asynchronous request
    if (doAsynchronous)
    {
        xmlHttpReq.onreadystatechange = function() {
            if (xmlHttpReq.readyState == 4) 
			{
				if (xmlHttpReq.status == 200)
					updatePage(xmlHttpReq.responseText);
				else
					showCommentError(GENERIC_COMMENT_ERROR_MSG + xmlHttpReq.status);
            }
        }

        //Set the current uri for setting comments when returned
        currentTcmUri = tcmuri;

        //Compose and send the request string
        xmlHttpReq.send(query);

    }
    //Return results on synchronous requests
    else
    {
        //Compose and send the request string
        xmlHttpReq.send(query);

        return xmlHttpReq.responseText;
    }	
    
}

//Update the page according to results returned from the AJAX request
// This code was taken from common.css originally written by Will Price
function updatePage(strResult)
{
    var comments = document.getElementById("existing_comments");
	
	if (comments)
		comments.innerHTML = strResult;
	
	//Update the comment counter
	updateCommentCounter(currentTcmUri);
	currentTcmUri = "";
	
	//Show appropriate success message depending on action
	if (commentAction == "post")
	{
		updateCtrlsClass("comment_submit", "hidden");
		updateCtrlsClass("comment_thanks", "");
		tmr = setTimeout("showExistingComments();", 5000);
		ctrl = document.getElementById("new_comment");
	
		if (ctrl)
		{
			ctrl.value = "Enter comments...";
			focused = false;
		}
	}
	else if (commentAction == "alert")
	{
		updateCtrlsClass("alert_confirm", "hidden");
		updateCtrlsClass("alert_thanks", "");
		tmr = setTimeout("showExistingComments();", 5000);
	} 
}

//====================PROFANITY FILTER====================
// ==UserScript==
// @name          Profanity Filter
// @namespace     http://www.someurlthingherethatsprettyunique.com/blah/ProfanityFilter
// @description    Version 1.0: Filters profanity from website.  Edit the array profanity to control 
//                               which words are removed from pages and replaced with ***.
// @include         *
// ==/UserScript==

/*
Author: MCE

Significantly rewritten other replacement scripts to noticably improve performance and add pseudo-threading to gradually replace words on larger 

pages.

Version: 1.0
1.0 - First Release

Competing scripts and extensions:
* http://www.arantius.com/article/arantius/clean+language/

Improvements Needed:
* Filter HTML attributes (ALT, TITLE, TOOLTIP, etc)
* Add an interface to manage the words by turning this into an extension.
*/
// Licensed for unlimited modification and redistribution as long as
// this notice is kept intact.

//This Profanity script was borrowed and severly modified from
// Author: Cameron McGregor, EIS
// Version: 1.0, JULY-27-2007

//Add words/phrases to be filtered out to this list in reverse alphabetical order
var badwords=['wtf',
'wop',
'whore',
'whoar',
'wetback',
'wank',
'twaty',
'twat',
'titty',
'titties',
'tits',
'teets',
'spunk',
'spook',
'spic',
'son-of-a-bitch',
'sonofabitch',
'son of a bitch',
'sob',
'snatch',
'smut',
'sluts',
'slut',
'sleaze',
'slag',
'shiz',
'shitty',
'shittings',
'shitting',
'shitters',
'shitter',
'shitted',
'shits',
'shitings',
'shiting',
'shit-head',
'shithead',
'shitfull',
'shited',
'shit',
'shemale',
'sheister',
'shat',
'sh!t',
'scum-bum',
'scumbum',
'scum-bucket',
'scumbucket',
'scum-bag',
'scumbag',
'scum',
'screw',
'schlong',
'retard',
'qweef',
'quif',
'queer',
'queef',
'queeb',
'pussys',
'pussy',
'pussies',
'pusse',
'punk',
'pricks',
'prick',
'pr0n',
'pornos',
'porno',
'porn',
'piss-off',
'pissoff',
'pissing',
'pissin',
'pisses',
'pissers',
'pisser',
'pissed-off',
'pissed',
'piss',
'pimp',
'phuq',
'phuks',
'phukking',
'phukked',
'phuking',
'phuked',
'phuk',
'phuck',
'phone-sex',
'phonesex',
'pecker',
'orgasms',
'orgasm',
'orgasims',
'orgasim',
'niggers',
'nigger',
'niggas',
'nigga',
'nerd',
'muthafucks',
'muthafuckings',
'muthafucking',
'muthafuckin',
'muthafuckers',
'muthafucker',
'muthafucked',
'muthafuckaz',
'muthafuckas',
'muthafucka',
'muthafuck',
'mutha',
'muff',
'mound',
'motherfucks',
'motherfuckings',
'motherfucking',
'motherfuckin',
'motherfuckers',
'motherfucker',
'motherfucked',
'motherfuck',
'mothafucks',
'mothafuckings',
'mothafucking',
'mothafuckin',
'mothafuckers',
'mothafucker',
'mothafucked',
'mothafuckaz',
'mothafuckas',
'mothafucka',
'mothafuck',
'motha',
'mook',
'mick',
'merde',
'masturbate',
'lusting',
'lust',
'loser',
'lesbo',
'lesbian',
'kyke',
'kunilingus',
'kums',
'kumming',
'kummer',
'kum',
'kuksuger',
'kuk',
'kraut',
'kondums',
'kondum',
'kock',
'knob',
'kike',
'kawk',
'jizz',
'jizm',
'jiz',
'jism',
'jis',
'jew',
'jesus h christ',
'jesus fucking christ',
'jerk-off',
'jerk',
'jap',
'jack-off',
'jackoff',
'jacking off',
'jack-ass',
'jackass',
'jack off',
'hussy',
'hot-sex',
'hotsex',
'hot sex',
'horny',
'horniest',
'hore',
'hook-nose',
'hooknose',
'hooker',
'honkey',
'homo',
'hoer',
'hell',
'heebie',
'hardcoresex',
'hard on',
'h4x0r',
'h0r',
'guinne',
'gook',
'gonads',
'goddamn',
'gazongers',
'gay-sex',
'gaysex',
'gay-lord',
'gaylord',
'gay',
'gang-bangs',
'gangbangs',
'gang-banged',
'gangbanged',
'gang-bang',
'gangbang',
'fux0r',
'furburger',
'fuks',
'fukc',
'fuk',
'fuck-you',
'fuckyou',
'fuck-up',
'fuckup',
'fucks',
'fuckme',
'fuckings',
'fucking',
'fuckin',
'fuckers',
'fucker',
'fucked',
'fuck',
'fuck you',
'fuck me',
'fuc',
'fu',
'foreskin',
'fock',
'fist-fucks',
'fistfucks',
'fist-fuckings',
'fistfuckings',
'fist-fucking',
'fistfucking',
'fist-fuckers',
'fistfuckers',
'fist-fucker',
'fistfucker',
'fist-fucked',
'fistfucked',
'fist-fuck',
'fistfuck',
'finger-fucks',
'fingerfucks',
'finger-fucking',
'fingerfucking',
'finger-fuckers',
'fingerfuckers',
'finger-fucker',
'fingerfucker',
'finger-fucked',
'fingerfucked',
'finger-fuck',
'fingerfuck',
'fellatio',
'felatio',
'feg',
'fcuk',
'fatso',
'fat-ass',
'fatass',
'farty',
'farts',
'fartings',
'farting',
'farted',
'fart',
'fah-q',
'fags',
'fagots',
'fagot',
'faggs',
'faggot',
'faggit',
'fagging',
'fagget',
'fag',
'dyke',
'dumbass',
'douche bag',
'dong',
'dipshit',
'dinkus',
'dinks',
'dink',
'dildos',
'dildo',
'dike',
'dick-weed',
'dickweed',
'dick-lips',
'dicklips',
'damn',
'cyberfucking',
'cyberfuckers',
'cyberfucker',
'cyberfucked',
'cyberfuck',
'cyberfuc',
'cunts',
'cuntlicking',
'cuntlicker',
'cuntlick',
'cunt',
'cunnilingus',
'cunillingus',
'cunilingus',
'cumshot',
'cums',
'cumming',
'cummer',
'cum',
'crap',
'cooter',
'cocksucks',
'cocksucking',
'cock-sucker',
'cocksucker',
'cocksucked',
'cocksuck',
'cocks',
'cock-moan',
'cockmoan',
'cock-brethe',
'cockbrethe',
'cock',
'cock moan',
'cobia',
'clits',
'clit',
'clam',
'circle jerk',
'chink',
'cawk',
'buttpicker',
'butthole',
'butthead',
'butt-fucker',
'buttfucker',
'butt-fuck',
'buttfuck',
'butt-face',
'buttface',
'butt',
'butt hair',
'butt fucker',
'butt breath',
'butch',
'bung hole',
'bum',
'bullshit',
'bull shit',
'bucket cunt',
'brown-town',
'browntown',
'brown-eye',
'browneye',
'brown eye',
'boobs',
'boobies',
'boob',
'boner',
'bone-head',
'bonehead',
'blow-jobs',
'blowjobs',
'blow-job',
'blowjob',
'bitching',
'bitchin',
'bitches',
'bitchers',
'bitcher',
'bitch',
'bestiality',
'bestial',
'belly whacker',
'beaver',
'beastility',
'beastiality',
'beastial',
'bazoombas',
'bastard',
'balls',
'ass-wipe',
'asswipe',
'ass-master',
'assmaster',
'ass-licker',
'asslicker',
'ass-kisser',
'asskisser',
'assholes',
'asshole',
'asses',
'ass wipe',
'ass master',
'ass licker',
'ass lick',
'ass kisser',
'ass'];

//Build the expletives array into a regular expression
var bw = "\\b(" + badwords.join("|") + ")\\b";
bw = new RegExp(bw, "gi");

//Default replacement text if not provided
var DEFAULT_REPLACE_TEXT = "***";

//Do a find replace on the passed string
function FilterExpletives(checkStr, replaceText)
{
	
	if (typeof(replaceText) == 'undefined')
		replaceText = DEFAULT_REPLACE_TEXT;
	
	return checkStr.replace(bw, replaceText);
}