// 
// Global handling scripts
//
var loc_profanity_scr = 'http://dev.kickapps.com/badwords/mccain/profanity_secure.php';

// 
// Handle Profanity Filter
//
function fixBadWordText(strText) {

	var strFinalText;

	// Strip out the HTML
	strFinalText = strText.replace(/<(?:.|\s)*?>/g, "");

	// Replace spaces with underscores
	return strFinalText.replace(/\s/g, "_");
}

function checkInChunks(strText) {
	
	var strFullText = fixBadWordText(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_profanity_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("The text you entered contains inappropriate content (word: " + response + ") and is not allowed on the site.");
			};
		}
	}

});

// 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("The text you entered contains inappropriate content (word: " + response + ") and is not allowed on the site.");
			};
		}
	}

});

if (Ka.formValidation){
	Ka.formValidation.rules['badWordCheck'] = {
		 message: '',
		 test: function(input){
			var response;

			response = checkInChunks(input);
			if ((response=='true') || (response=='')) {return true};
			this.message = 'The information you entered on this page contains inappropriate content (word: ' + response + ') and cannot be posted.';
			return false;
		 }
	}

	// Registration checks
	if (Ka.Info.PAGE == 'login/registerUser.jsp'){
		$j('#email').attr('badWordCheck','true');
		$j('#username').attr('badWordCheck','true');
	}
	
	// Edit Profile checks
	if (Ka.Info.PAGE == 'pages/manageProfileQuestions.jsp'){
		var profileTags = document.getElementsByName('ka_inputTags');
	
		$j('#aboutMe').attr('badWordCheck','true');
		$j('#answer1').attr('badWordCheck','true');
		$j('#answer2').attr('badWordCheck','true');
		$j('#answer3').attr('badWordCheck','true');
		if (profileTags) {	
			$j('#ka_inputTags').attr('badWordCheck','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('badWordCheck','true');
		$j('#firstName').attr('badWordCheck','true');
		$j('#lastName').attr('badWordCheck','true');
		$j('#postalCity').attr('badWordCheck','true');
		$j('#postalProvince').attr('badWordCheck','true');
		$j('#postalCode').attr('badWordCheck','true');
	}
	
	// Edit Blog checks
	if (Ka.Info.PAGE == 'pages/editBlog.jsp'){
		$j('#name').attr('badWordCheck','true');
		$j('#city').attr('badWordCheck','true');
		$j('#postalProvince').attr('badWordCheck','true');
		$j('#postalCode').attr('badWordCheck','true');
		$j('#description').attr('badWordCheck','true');
		$j('#tags').attr('badWordCheck','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('badWordCheck','true');
		$j('#city').attr('badWordCheck','true');
		$j('#postalProvince').attr('badWordCheck','true');
		$j('#postalCode').attr('badWordCheck','true');
		$j('#mediaDescriptionRequired').attr('badWordCheck','true');
		$j('#tags').attr('badWordCheck','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('badWordCheck','true');
		$j('#mediaName1').attr('badWordCheck','true');
		$j('#mediaName2').attr('badWordCheck','true');
		$j('#mediaName3').attr('badWordCheck','true');
		$j('#mediaName4').attr('badWordCheck','true');
		$j('#mediaName5').attr('badWordCheck','true');
		$j('#mediaDescription').attr('badWordCheck','true');
		$j('#postalCity').attr('badWordCheck','true');
		$j('#postalProvince').attr('badWordCheck','true');
		$j('#postalCode').attr('badWordCheck','true');
		$j('#mediaTags').attr('badWordCheck','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('badWordCheck','true');
		$j('#mediaDescription').attr('badWordCheck','true');
		$j('#postalCity').attr('badWordCheck','true');
		$j('#postalProvince').attr('badWordCheck','true');
		$j('#postalCode').attr('badWordCheck','true');
		$j('#mediaTags').attr('badWordCheck','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('badWordCheck','true');
		$j('#ka_DiscussionName').attr('badWordCheck','true');
	}

	if (Ka.Info.PAGE == 'pages/newForum.jsp'){
		$j('#forumTitle').attr('badWordCheck','true');
		$j('#forumName').attr('badWordCheck','true');
	}
}