/************************/
/* Autor: Felipe Jaekel */
/**********************************/
/* Funções para manipular strings */
/**********************************/

/*****************************************************************************/
/* Remove espaços no início e final da string */
/**********************************************/
function trim(str)
{
	return str.replace(/(^\s+)|(\s+$)/g, '');
}

/******************************************************************************
	Converte caracteres HTML para seu código ASCII, permitindo assim sua visualização na tela como se fossem  caracteres quaisquer
******************************************************************************/
function html2entities(conteudo)
{
   var regex =	/[<>"'&]/g;

   //Tags <br /> são as únicas que podem ser consideradas
   var exclude	= 	/<br \/>/;

	if(conteudo.match(regex) && !conteudo.match(exclude))
	{
   	conteudo = conteudo.replace(regex, function(m){return replaceChar(m)});
   }

	return conteudo;
}
function replaceChar(match)
{
	switch(match)
	{
		case '<'	: 	return '&lt;';
		case '>'	: 	return '&gt;';
		case '"'	: 	return '&quot;';
		case "'"	: 	return '&#039;';
		case '&'	:	return '&amp;';
	}
}
/*****************************************************************************/
/* Converte ASCII para texto novamente */
/***************************************/
function html2text(conteudo)
{
   var regex =	/(&lt;)|(&gt;)|(&quot;)|(&#039;)|(&amp;)/g;

	if(conteudo.match(regex))
	{
   	conteudo = conteudo.replace(regex, function(m){return replaceChar2(m)});
   }

	return conteudo;
}
function replaceChar2(match)
{
	switch(match)
	{
		case '&lt;'	  : return '<';
		case '&gt;'	  : return '>';
		case '&quot;' : return '"';
		case '&#039;' : return "'";
		case '&amp;'  : return '&';
	}
}

/****************************************************************************************/
/* Converte \n para <br />, permitindo assim a reprodução da formatação de uma textarea */
/****************************************************************************************/
function nl2br(conteudo)
{
   if(conteudo.match(/\n/))
   {
   	conteudo = conteudo.replace(/\n/g, '<br />');
   }

	return conteudo;
}

/**
 * stringFunctions.js
 *
 * This file contains a collection of string functions for javascript.
 * Most of them are inspired by their PHP equivalent.
 *
 * This source file is subject to version 2.1 of the GNU Lesser
 * General Public License (LPGL), found in the file LICENSE that is
 * included with this package, and is also available at
 * http://www.gnu.org/copyleft/lesser.html.
 * @package     Javascript
 *
 * @author      Dieter Raber <dieter@dieterraber.net>
 * @copyright   2004-12-27
 * @version     1.0
 * @license     http://www.gnu.org/copyleft/lesser.html
 *
 *
 *
 * htmlEntities
 *
 * Convert all applicable characters to HTML entities
 *
 * object string
 * return string
 *
 * example:
 *   test = 'äöü'
 *   test.htmlEntities() //returns '&auml;&ouml;&uuml;'
 */

String.prototype.htmlEntities = function()
{
  var chars = new Array ('&','à','á','â','ã','ä','å','æ','ç','è','é',
                         'ê','ë','ì','í','î','ï','ð','ñ','ò','ó','ô',
                         'õ','ö','ø','ù','ú','û','ü','ý','þ','ÿ','À',
                         'Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë',
                         'Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö',
                         'Ø','Ù','Ú','Û','Ü','Ý','Þ','€','\"','ß','<',
                         '>','¢','£','¤','¥','¦','§','¨','©','ª','«',
                         '¬','­','®','¯','°','±','²','³','´','µ','¶',
                         '·','¸','¹','º','»','¼','½','¾');

  var entities = new Array ('amp','agrave','aacute','acirc','atilde','auml','aring',
                            'aelig','ccedil','egrave','eacute','ecirc','euml','igrave',
                            'iacute','icirc','iuml','eth','ntilde','ograve','oacute',
                            'ocirc','otilde','ouml','oslash','ugrave','uacute','ucirc',
                            'uuml','yacute','thorn','yuml','Agrave','Aacute','Acirc',
                            'Atilde','Auml','Aring','AElig','Ccedil','Egrave','Eacute',
                            'Ecirc','Euml','Igrave','Iacute','Icirc','Iuml','ETH','Ntilde',
                            'Ograve','Oacute','Ocirc','Otilde','Ouml','Oslash','Ugrave',
                            'Uacute','Ucirc','Uuml','Yacute','THORN','euro','quot','szlig',
                            'lt','gt','cent','pound','curren','yen','brvbar','sect','uml',
                            'copy','ordf','laquo','not','shy','reg','macr','deg','plusmn',
                            'sup2','sup3','acute','micro','para','middot','cedil','sup1',
                            'ordm','raquo','frac14','frac12','frac34');

  newString = this;
  for (var i = 0; i < chars.length; i++)
  {
    myRegExp = new RegExp();
    myRegExp.compile(chars[i],'g')
    newString = newString.replace (myRegExp, '&' + entities[i] + ';');
  }
  return newString;
}