function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function

	function confirmar(accao, texto) {
	 	if( confirm(texto) ) {
		 	document.location.href = accao;
		}
	}

	function CountWords (this_field, show_word_count, show_char_count) {
		if (show_word_count == null) {
			show_word_count = true;
		}
		if (show_char_count == null) {
			show_char_count = false;
		}
		var char_count = this_field.value.length;
		var fullStr = this_field.value;
		fullStr = replaceSubstring(fullStr, "\n", " ");
		var acentuados = /[áÁàÀãâÃÂäÄéÉèÈêÊëËíÍìÌĩĨîÎïÏóÓòÒôÔõÕöÖúÚùÙûÛũŨüÜçÇ]/gi;
		fullStr = fullStr.replace(acentuados, "x");
		var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9 ]+/gi;
		fullStr = fullStr.replace(non_alphanumerics_rExp, "");
		var espacosMultiplos = /[ ]+/gi;
		fullStr = fullStr.replace(espacosMultiplos, " ");
		var splitString = fullStr.split(" ");
		var word_count = splitString.length;
		if (fullStr.length <2) {
			word_count = 0;
		}
		if (word_count == 1) {
			wordOrWords = " palavra";
		}
		else {
			wordOrWords = " palavras";
		}
		if (char_count == 1) {
			charOrChars = " caracter";
		} else {
			charOrChars = " caracteres";
}
		if (show_word_count & show_char_count) {
			alert ("Palavras:\n" + "    " + word_count + wordOrWords + "\n" + "    " + char_count + charOrChars);
		}
		else {
			if (show_word_count) {
				alert (word_count + wordOrWords);
			}
			else {
				if (show_char_count) {
					alert ("Caracteres:  " + char_count + charOrChars);
				}
			}
		}
		return word_count;
	}
