// 
// Global handling scripts
//
var loc_pf_scr = 'http://dev.kickapps.com/badwords/itv/pf_itv.php';

// 
// Handle pf Filter
//
function fixPFText(strText) {

	var strFinalText;
	
	// Strip out the HTML
	strFinalText = strText.replace(/&(lt|gt);/g, function (strMatch, p1){
		return (p1 == "lt")? "<" : ">";
	});
	strFinalText = strFinalText.replace(/<\/?[^>]+(>|$)/g, " ");
	
	// Double spaces to single spaces
	strFinalText = strFinalText.replace(/\s+/g," ");

	// Replace spaces with underscores
	strFinalText = strFinalText.replace(/\s/g, "_");
	
	// Encode the text to handle internationalization
	strFinalText = encodeURIComponent(strFinalText);

	return strFinalText;
}

function checkInChunks(strText) {
	
	var strFullText = fixPFText(strText);
	var strToCheck = "";
	var maxLength = 150;
	
	// Setup
	response = 'true';

	// Parse into a word array
	var tempArray = strFullText.split('_');
	
	// Test in chunks with whole words
	for (var i = 0; i < tempArray.length; i++) {
		
		strToCheck = strToCheck + tempArray[i] + "_";
		if ((strToCheck.length > maxLength) || (i == (tempArray.length - 1))) {
			var response = $j.ajax({
				type: "GET",
				url: location.protocol+'//'+location.hostname+'/service/getHttpUrl.kickAction?urlLink='+loc_pf_scr+'?texttocheck='+strToCheck,
				async: false
				}).responseText;
		
			if (response!='true') {
				return response;
			}
			
			// Start over
			strToCheck = "";
		}
	}

	return response;
}

// 
// Handle tinyMCE handler
//
function setTinyMCEValues() {

	// Edit Profile checks
	if (Ka.Info.PAGE == 'pages/manageProfileQuestions.jsp'){
		tinymce.EditorManager.editors['aboutMe'].save();
	}
	
	// Edit Blog checks
	if (Ka.Info.PAGE == 'pages/editBlog.jsp'){
		tinymce.EditorManager.editors['description'].save();
	}
	
	// Add Blog checks
	if (Ka.Info.PAGE == 'pages/addBlog.jsp'){
		tinymce.EditorManager.editors['mediaDescriptionRequired'].save();
	}
	
	// Media Upload checks
	if (Ka.Info.PAGE == 'pages/newVideoUpload.jsp' || Ka.Info.PAGE == 'pages/newAudioUpload.jsp' || Ka.Info.PAGE == 'pages/newPhotoUpload.jsp') {
		tinymce.EditorManager.editors['mediaDescription'].save();
	}
	
	// Media Edit checks
	if (Ka.Info.PAGE == 'pages/editVideo.jsp' || Ka.Info.PAGE == 'pages/editAudio.jsp' || Ka.Info.PAGE == 'pages/editPhoto.jsp') {
		tinymce.EditorManager.editors['mediaDescription'].save();
	}
	
	// Message Board checks
	if (Ka.Info.PAGE == 'pages/newDiscussion.jsp' || Ka.Info.PAGE == 'pages/newDiscussionThread.jsp'){
		tinymce.EditorManager.editors['ka_discussionSubject'].save();
	}
}

// Handle comments
$j(document).ready(function() {

	if (typeof window.sendMessage != 'undefined') {
		var p_sendMessage = sendMessage;
		sendMessage = function(){
			var strComments = DWRUtil.getValue("text");
			var response;
				
			response = checkInChunks(strComments);
			if ((response=='true') || (response=='')) {
				p_sendMessage();
			}
			else {
				alert("Your message contains inappropriate language and cannot be posted.");
			};
		}
	}

});

// Handle email to a friend
$j(document).ready(function() {

	if (typeof window.emailToFriends != 'undefined') {
		var p_emailToFriends = emailToFriends;
		emailToFriends = function(){
			var strEmailMsg = DWRUtil.getValue("emailMsg");
			var response;
				
			response = checkInChunks(strEmailMsg);
			if ((response=='true') || (response=='')) {
				p_emailToFriends();
			}
			else {
				alert("Your message contains inappropriate language and cannot be sent.");
			};
		}
	}

});

// Handle the tag fields add button
$j(document).ready(function() {

	if (typeof window.setTags != 'undefined') {
		var p_setTags = setTags;
		setTags = function(){
			var response;
			var strTags = DWRUtil.getValue("ka_playerAddTagsInput");
				
			response = checkInChunks(strTags);
			if ((response=='true') || (response=='')) {
				// Handle the commas in the tag
				DWRUtil.setValue("ka_playerAddTagsInput", strTags);
				
				// handle the tag sets
				p_setTags();
			}
			else {
				alert("Your message contains inappropriate language and cannot be posted.");
			};
		}
	}

});

if (Ka.formValidation){
	Ka.formValidation.rules.pfWordCheck = {
		 message: '',
		 test: function(input) {
			 var response, $element = $j(arguments[2]);
			 response = checkInChunks(input);
			 if ((response == 'true') || (response == '')) { // if the element has an oldErrorKey move it back
				 if ( !! $element.attr('olderrorkey')) {
					 $element.attr('errorkey', $element.attr('olderrorkey')).removeAttr('olderrorkey');
				 }
				 return true;
			 } // reference to current dom element
			 // if the element has an error key move off
			 if ( !! $element.attr('errorkey')) {
				 $element.attr('olderrorkey', $element.attr('errorkey')).removeAttr('errorkey');
			 }
			 this.message = "Your message contains inappropriate language and cannot be posted.";
			 return false;
		 }
	}

	// Registration checks
	if (Ka.Info.PAGE == 'login/registerUser.jsp'){
		$j('#email').attr('pfWordCheck','true');
		$j('#username').attr('pfWordCheck','true');
	}
	
	// Edit Profile checks
	if (Ka.Info.PAGE == 'pages/manageProfileQuestions.jsp'){
		var profileTags = document.getElementsByName('ka_inputTags');
		var Personal_motto = document.getElementsByName('Personal_motto');
		var answer4 = document.getElementsByName('cq_4');
		var answer5 = document.getElementsByName('cq_5');
		var answer6 = document.getElementsByName('cq_6');
		var answer7 = document.getElementsByName('cq_7');
		var answer8 = document.getElementsByName('cq_8');
		var answer9 = document.getElementsByName('cq_9');
		var answer10 = document.getElementsByName('cq_10');
		var Occupation = document.getElementsByName('Occupation');
		var Company = document.getElementsByName('Company');
		var College = document.getElementsByName('College');
		var High_school = document.getElementsByName('High_school');
		var Website_URL = document.getElementsByName('Website_URL');
		var AIM_username = document.getElementsByName('AIM_username');
		var MSN_username = document.getElementsByName('MSN_username');
		var Yahoo_username = document.getElementsByName('Yahoo!_username');
		var Skype_username = document.getElementsByName('Skype_username');
		var Google_talk_username = document.getElementsByName('Google_talk_username');
		var ICQ_number = document.getElementsByName('ICQ_number');
		var Interest_and_hobbies = document.getElementsByName('Interest_and_hobbies');
		var I_would_like_to_meet = document.getElementsByName('I_would_like_to_meet');
		var Best_attribute = document.getElementsByName('Best_attribute');
		var Worst_attribute = document.getElementsByName('Worst_attribute');
		var Favorite_bands_and_musicians = document.getElementsByName('Favorite_bands_and_musicians');
		var Favorite_movies = document.getElementsByName('Favorite_movies');
		var Favorite_TV_shows = document.getElementsByName('Favorite_TV_shows');
		var Favorite_books_magazines_and_writers = document.getElementsByName('Favorite_books-_magazines_and_writers');
		var Favorite_blogs_and_websites = document.getElementsByName('Favorite_blogs_and_websites');
		var Favorite_games_sports_and_teams = document.getElementsByName('Favorite_games-_sports_and_teams');
		var Favorite_places = document.getElementsByName('Favorite_places');
		var Favorite_artists = document.getElementsByName('Favorite_artists');
		var Favorite_stuff = document.getElementsByName('Favorite_stuff');
		var Scary_stuff = document.getElementsByName('Scary_stuff');
		var My_pet_name = document.getElementsByName('My_pet_name');


		$j('#aboutMe').attr('pfWordCheck','true');
		$j('#answer1').attr('pfWordCheck','true');
		$j('#answer2').attr('pfWordCheck','true');
		$j('#answer3').attr('pfWordCheck','true');
		if (profileTags) {	
			$j('#ka_inputTags').attr('pfWordCheck','true'); 
		};
		if (Personal_motto) {	
			$j('#Personal_motto').attr('pfWordCheck','true'); 
		};
		if (answer4) {	
			$j('#cq_4').attr('pfWordCheck','true'); 
		};
		if (answer5) {	
			$j('#cq_5').attr('pfWordCheck','true'); 
		};
		if (answer6) {	
			$j('#cq_6').attr('pfWordCheck','true'); 
		};
		if (answer7) {	
			$j('#cq_7').attr('pfWordCheck','true'); 
		};
		if (answer8) {	
			$j('#cq_8').attr('pfWordCheck','true'); 
		};
		if (answer9) {	
			$j('#cq_9').attr('pfWordCheck','true'); 
		};
		if (answer10) {	
			$j('#cq_10').attr('pfWordCheck','true'); 
		};
		if (Occupation) {	
			$j('#Occupation').attr('pfWordCheck','true'); 
		};
		if (Company) {	
			$j('#Company').attr('pfWordCheck','true'); 
		};
		if (College) {	
			$j('#College').attr('pfWordCheck','true'); 
		};
		if (High_school) {	
			$j('#High_school').attr('pfWordCheck','true'); 
		};
		if (Website_URL) {	
			$j('#Website_URL').attr('pfWordCheck','true'); 
		};
		if (AIM_username) {	
			$j('#AIM_username').attr('pfWordCheck','true'); 
		};
		if (MSN_username) {	
			$j('#MSN_username').attr('pfWordCheck','true'); 
		};
		if (Yahoo_username) {	
			$j('#Yahoo!_username').attr('pfWordCheck','true'); 
		};
		if (Skype_username) {	
			$j('#Skype_username').attr('pfWordCheck','true'); 
		};
		if (Google_talk_username) {	
			$j('#Google_talk_username').attr('pfWordCheck','true'); 
		};
		if (ICQ_number) {	
			$j('#ICQ_number').attr('pfWordCheck','true'); 
		};
		if (Interest_and_hobbies) {	
			$j('#Interest_and_hobbies').attr('pfWordCheck','true'); 
		};
		if (I_would_like_to_meet) {	
			$j('#I_would_like_to_meet').attr('pfWordCheck','true'); 
		};
		if (Best_attribute) {	
			$j('#Best_attribute').attr('pfWordCheck','true'); 
		};
		if (Worst_attribute) {	
			$j('#Worst_attribute').attr('pfWordCheck','true'); 
		};
		if (Favorite_bands_and_musicians) {	
			$j('#Favorite_bands_and_musicians').attr('pfWordCheck','true'); 
		};
		if (Favorite_movies) {	
			$j('#Favorite_movies').attr('pfWordCheck','true'); 
		};
		if (Favorite_TV_shows) {	
			$j('#Favorite_TV_shows').attr('pfWordCheck','true'); 
		};
		if (Favorite_books_magazines_and_writers) {	
			$j('#Favorite_books-_magazines_and_writers').attr('pfWordCheck','true'); 
		};
		if (Favorite_blogs_and_websites) {	
			$j('#Favorite_blogs_and_websites').attr('pfWordCheck','true'); 
		};
		if (Favorite_games_sports_and_teams) {	
			$j('#Favorite_games-_sports_and_teams').attr('pfWordCheck','true'); 
		};
		if (Favorite_places) {	
			$j('#Favorite_places').attr('pfWordCheck','true'); 
		};
		if (Favorite_artists) {	
			$j('#Favorite_artists').attr('pfWordCheck','true'); 
		};
		if (Favorite_stuff) {	
			$j('#Favorite_stuff').attr('pfWordCheck','true'); 
		};
		if (Scary_stuff) {	
			$j('#Scary_stuff').attr('pfWordCheck','true'); 
		};
		if (My_pet_name) {	
			$j('#My_pet_name').attr('pfWordCheck','true'); 
		};
		
		// Handle the tinyMCE map to the related field
		if (typeof window.validateProfileFields != 'undefined') {
			var p_validateProfileFields = validateProfileFields;
			validateProfileFields = function(this_obj){
				setTinyMCEValues();
				return p_validateProfileFields(this_obj);
			}
		}

	}
	
	// Edit Account checks
	if (Ka.Info.PAGE == 'pages/manageAccountSettings.jsp'){
		$j('#email').attr('pfWordCheck','true');
		$j('#firstName').attr('pfWordCheck','true');
		$j('#lastName').attr('pfWordCheck','true');
		$j('#postalCity').attr('pfWordCheck','true');
		$j('#postalProvince').attr('pfWordCheck','true');
		$j('#postalCode').attr('pfWordCheck','true');
	}
	
	// Edit Blog checks
	if (Ka.Info.PAGE == 'pages/editBlog.jsp'){
		$j('#name').attr('pfWordCheck','true');
		$j('#city').attr('pfWordCheck','true');
		$j('#postalProvince').attr('pfWordCheck','true');
		$j('#postalCode').attr('pfWordCheck','true');
		$j('#description').attr('pfWordCheck','true');
		$j('#tags').attr('pfWordCheck','true');
		
		// Handle the tinyMCE map to the related field
		if (typeof window.validateBeforeSubmit != 'undefined') {
			var p_validateBeforeSubmit = validateBeforeSubmit;
			validateBeforeSubmit = function(this_obj){
				setTinyMCEValues();
				return p_validateBeforeSubmit(this_obj);
			}
		}
		
	}
	
	// Add Blog checks
	if (Ka.Info.PAGE == 'pages/addBlog.jsp'){
		$j('#name').attr('pfWordCheck','true');
		$j('#city').attr('pfWordCheck','true');
		$j('#postalProvince').attr('pfWordCheck','true');
		$j('#postalCode').attr('pfWordCheck','true');
		$j('#mediaDescriptionRequired').attr('pfWordCheck','true');
		$j('#tags').attr('pfWordCheck','true');

		// Handle the tinyMCE map to the related field
		if (typeof window.validateAddBlog != 'undefined') {
			var p_validateAddBlog = validateAddBlog;
			validateAddBlog = function(this_obj){
				setTinyMCEValues();
				return p_validateAddBlog(this_obj);
			}
		}
	}
	
	// Media Upload checks
	if (Ka.Info.PAGE == 'pages/newVideoUpload.jsp' || Ka.Info.PAGE == 'pages/newAudioUpload.jsp' || Ka.Info.PAGE == 'pages/newPhotoUpload.jsp') {
		$j('#mediaName').attr('pfWordCheck','true');
		$j('#mediaName1').attr('pfWordCheck','true');
		$j('#mediaName2').attr('pfWordCheck','true');
		$j('#mediaName3').attr('pfWordCheck','true');
		$j('#mediaName4').attr('pfWordCheck','true');
		$j('#mediaName5').attr('pfWordCheck','true');
		$j('#mediaDescription').attr('pfWordCheck','true');
		$j('#postalCity').attr('pfWordCheck','true');
		$j('#postalProvince').attr('pfWordCheck','true');
		$j('#postalCode').attr('pfWordCheck','true');
		$j('#mediaTags').attr('pfWordCheck','true');

		// Handle the tinyMCE map to the related field
		if (typeof window.validateAddVideo != 'undefined') {
			var p_validateAddVideo = validateAddVideo;
			validateAddVideo = function(this_obj){
				setTinyMCEValues();
				return p_validateAddVideo(this_obj);
			}
		}
		
		if (typeof window.validateAddAudio != 'undefined') {
			var p_validateAddAudio = validateAddAudio;
			validateAddAudio = function(this_obj){
				setTinyMCEValues();
				return p_validateAddAudio(this_obj);
			}
		}

		if (typeof window.validateAddPhoto != 'undefined') {
			var p_validateAddPhoto = validateAddPhoto;
			validateAddPhoto = function(this_obj){
				setTinyMCEValues();
				return p_validateAddPhoto(this_obj);
			}
		}

	}
	
	// Media Edit checks
	if (Ka.Info.PAGE == 'pages/editVideo.jsp' || Ka.Info.PAGE == 'pages/editAudio.jsp' || Ka.Info.PAGE == 'pages/editPhoto.jsp') {
		$j('#mediaName').attr('pfWordCheck','true');
		$j('#mediaDescription').attr('pfWordCheck','true');
		$j('#postalCity').attr('pfWordCheck','true');
		$j('#postalProvince').attr('pfWordCheck','true');
		$j('#postalCode').attr('pfWordCheck','true');
		$j('#mediaTags').attr('pfWordCheck','true');

		// Handle the tinyMCE map to the related field
		if (typeof window.validateBeforeSubmit != 'undefined') {
			var p_validateBeforeSubmit = validateBeforeSubmit;
			validateBeforeSubmit = function(this_obj){
				setTinyMCEValues();
				return p_validateBeforeSubmit(this_obj);
			}
		}

	}
	
	// Message Board checks
	if (Ka.Info.PAGE == 'pages/newDiscussion.jsp' || Ka.Info.PAGE == 'pages/newDiscussionThread.jsp'){
		$j('#ka_discussionSubject').attr('pfWordCheck','true');
		$j('#ka_DiscussionName').attr('pfWordCheck','true');
	}

	if (Ka.Info.PAGE == 'pages/newForum.jsp'){
		$j('#forumTitle').attr('pfWordCheck','true');
		$j('#forumName').attr('pfWordCheck','true');
	}
	
	// Profile Comment checks
	if (Ka.Info.PAGE == 'pages/kickPlaceServerSide.jsp'){
		$j('#ka_profileNewCommentTextarea').attr('pfWordCheck','true');
	}
	
	// Profile Comment checks
	if (Ka.Info.PAGE == 'pages/newMessage.jsp'){
		$j('#subject').attr('pfWordCheck','true');
		$j('#message').attr('pfWordCheck','true');
	}

}