var wordsFilterList = {};

new Ajax.Request("/app/application",{	
	method:'POST',
	parameters: "action=getWordsFilter",
	encoding: 'ISO-8859-1',
	requestHeaders: {Accept: 'application/json'},
	onSuccess : function(resp){
		wordsFilterList = resp.responseText.evalJSON(true);
	}
});
	
if ($('email') != null){
	$('email').hide();
}

if ($('charCount') != null){
	$('charCount').hide();
}

function charCount(textarea, counterZone){
	var writed = 0;
	if (!textarea) {
		textarea = document.getElementById(textarea);
	}
	if (textarea.value.length != null){
		writed = textarea.value.length;
	}
	var max = 500;
	var count = max - writed;
	if (writed > max){
		textarea.value = textarea.value.substring(0,max);
	}
	$(counterZone).innerHTML = count;
}

function cleanValueText (elemId, text) {
	if ($(elemId).value == text){
		$(elemId).value = "";
	}
}

function setValueText (elemId, text) {
	if ($(elemId).value == ""){
		$(elemId).value = ""+text;
	}
}

function showSubForm (checkbox, idSubForm){
	if (checkbox.checked){
		$(idSubForm).show();
	} else {
		$(idSubForm).hide();
	}
}

function cleanDemoText(elemList){
	for (i = 0 ; i < elemList.length ; i++){
		if ($(elemList[i][0]).value == elemList[i][1]){
			$(elemList[i][0]).value = "";
		}
	}
}

function showHideItem (elemId){
	if ($(elemId).style.display == "none"){
		$(elemId).show();
	} else {
		$(elemId).hide();
	}
}

function validaEmail(campo){
	var RegExPattern = /[\w-\.]{1,}@([\w-]{1,}\.)*([\w-]{1,}\.)[\w-]{2,4}/;
	var errorMessage = 'Verifique su email';

    if (!(campo.value.match(RegExPattern))) {
         alert(errorMessage);
         campo.value='';
         campo.focus();
         return false;
    }

	return true;

}

function showHideItemAndReplaceTxt (elemId, replaceId, contentHide, contentShow){
	var elemToReplace = $(''+replaceId);
	if ($(elemId).style.display == "none"){
		$(''+replaceId).firstChild.nodeValue = contentShow;
		$(elemId).show();
	} else {
		$(''+replaceId).firstChild.nodeValue = contentHide;
		$(elemId).hide();
	}
}

function replaceWords(textAreaId){
	var text = $F(textAreaId);
	var textLowerCase = text.toLowerCase();
	var re = /[^a-zA-ZñÑáéíóúÁÉÍÓÚâêîôûÂÊÎÔÛàèìòùÀÈÌÒÙäëïöüÄËÏÖÜ]/i;
	var badWordsNumber = 0;
	if ( text.charAt(text.length - 1).match(re) != null || (TAHasFocus != undefined && !TAHasFocus) ) {
		
		for (i = 0 ; i < wordsFilterList.size() ; i++) {
			
			var word = wordsFilterList[i];
			var j = 0;
			while (j < textLowerCase.length) {
				var pos = textLowerCase.indexOf(word, j);
				if (pos == -1) {
					j = textLowerCase.length;
				} else {
					var endPos = pos + word.length;
					var reLetter = /[a-zA-ZñÑáéíóúÁÉÍÓÚâêîôûÂÊÎÔÛàèìòùÀÈÌÒÙäëïöüÄËÏÖÜ]/i;
					if (((pos > 0) && (textLowerCase.charAt(pos - 1).match(reLetter) == null) && (textLowerCase.charAt(endPos).match(reLetter) == null)) 
						|| (pos == 0 && (textLowerCase.charAt(endPos).match(reLetter) == null)) 
						|| (pos > 0 && (textLowerCase.charAt(pos - 1).match(reLetter) == null) && endPos == (textLowerCase.length - 1) && (TAHasFocus != undefined && !TAHasFocus))) {
						temp = textLowerCase.charAt(pos) + "***";
						textLowerCase = textLowerCase.substring(0, pos) + temp + textLowerCase.substring(endPos);
						temp = text.charAt(pos) + "***";
						text = text.substring(0, pos) + temp + text.substring(endPos);
						badWordsNumber++;
						j = pos + 1;
					} else {
						j = endPos;
					}
				}
			}
		}
	}
	//$(textAreaId).value = text;
	if (badWordsNumber > 0){
		answer = confirm("Ha escrito palabras que el sistema censurará. \n¿deseas volver a escribir correctamente tu comentario?"); 
		
		if (answer){
			$(textAreaId).focus;
		}
		return !answer;
		if ($('warning') != undefined && $('warning') != null) {
			$('warning').show();
		}
	} else {
		if ($('warning') != undefined && $('warning') != null) {
			$('warning').hide();
		}
	}
	return true;
}

function mostrar(elemId){
	$(elemId).show();
}
function ocultar(elemId){
	$(elemId).hide();
}
