function html2bb(text){
	text = text.trim();
	text = text.replace(/<a href=\"([^\"]+)\">\1<\/a>/igm, '[url]$1[/url]');
	text = text.replace(/<a href=\"([^\"]+)\">(.*)?<\/a>/igm, '[url=$1]$2[/url]');
//	text = text.replace(/<span style=\"font-size:([^\"]+);\">(.*)?<\/span>/igm, '[size=$1]$2[/size]');
	text = text.replace(/<font color=\"([^\"]+)\">(.*)?<\/font>/igm, '[color=$1]$2[/color]');
	text = text.replace(/<i>(.*)?<\/i>/igm, '[i]$1[/i]');
	text = text.replace(/<b>(.*)?<\/b>/igm, '[b]$1[/b]');
	
	text = html_decode(text);
	return text;
}

function bb2html(text){
	text = text.trim();
	text = html_encode(text);
	text = text.replace(/\[b\](.*)?\[\/b\]/igm, '<b>$1</b>');
	text = text.replace(/\[i\](.*)?\[\/i\]/igm, '<i>$1</i>');
	text = text.replace(/\[color="?([^\[]+)"?\](.*)?\[\/color\]/igm, '<font color="$1">$2</font>');
//	text = text.replace(/\[size="?([^\[]+)"?\](.*)?\[\/size\]/igm, '<span style="font-size:$1;">$2</span>');
	
	text = text.replace(/\[url\](.*)?\[\/url\]/igm, '<a href="$1">$1</a>');
	text = text.replace(/\[url=([^\[]+)\](.*)?\[\/url\]/ig, '<a href="$1">$2</a>');
	return text;
}

if(typeof(String.prototype.trim) === "undefined"){
    String.prototype.trim = function(){
        return String(this).replace(/^\s+|\s+$/g, '');
    };
}

function html_encode(str) {
	if (typeof(str) == "string") {
		str = str.replace(/&/g, '&amp;'); /* must do &amp; first */
		str = str.replace(/"/g, '&quot;');
		//str = str.replace(/'/g, '&#039;');
		//str = str.replace(/\n/g, '<br>');
		//str = str.replace(/</g, '&lt;');
		//str = str.replace(/>/g, '&gt;');
	}
	return str;
}

function html_decode(str) {
	if (typeof(str) == "string") {
		str = str.replace(/<br>/g, '\n');
		//str = str.replace(/&gt;/ig, '>');
		//str = str.replace(/&lt;/ig, '<');
		//str = str.replace(/&#039;/g, "'");
		//str = str.replace(/&quot;/ig, '"');
		str = str.replace(/&amp;/ig, '&'); /* must do &amp; last */
	}
 return str;
}
