function formatText(text){
	text = replaceGTLT(text);
	
	text = allowSimpleTag(text,"<b>","</b>","<b>","</b>");
	text = allowSimpleTag(text,"b[","]","<b>","</b>");
	text = allowSimpleTag(text,"bold[","]","<b>","</b>");
	text = allowSimpleTag(text,"\r\n!b ","\r\n","<b>","</b> ");
	text = allowSimpleTag(text,"\r!b ","\r\n","<b>","</b> "); //mac

	text = allowSimpleTag(text,"i[","]","<i>","</i>");
	text = allowSimpleTag(text,"italic[","]","<i>","</i>");
	text = allowSimpleTag(text,"<i>","</i>","<i>","</i>");
	text = allowSimpleTag(text,"\r\n!i ","\r\n","<i>","</i> ");
	text = allowSimpleTag(text,"\r!i ","\r\n","<i>","</i> "); //mac
	
	text = allowTag(text,"<a ",">","</a>","<a ",">","</a>");
	text = allowTag(text,"w[http://",",","]",'<a href="http://','">','</a>');
	text = allowTag(text,"weblink[http://",",","]",'<a href="http://','">','</a>');
	text = allowTag(text,"w[",",","]",'<a href="http://','">','</a>');
	text = allowTag(text,"weblink[",",","]",'<a href="http://','">','</a>');
	
	text = allowTag(text,"filelink[",",","]",'<a href="##!##filepath##!##','">','</a>');
	text = allowTag(text,"f[",",","]",'<a href="##!##filepath##!##','">','</a>');
	
	text = allowTag(text,"maillink[",",","]",'<a href="mailto:','">','</a>');
	text = allowTag(text,"m[",",","]",'<a href="mailto:','">','</a>');

	text = filterText(text);
	return text;
}

function allowTag(text,tag,bit,ctag,ntag,nbit,nctag){
	tag = replaceGTLT(tag);
	ctag = replaceGTLT(ctag);
	bit = replaceGTLT(bit);

	var ok = true;
	while(ok == true){
		temp = findTag(text,tag,bit,ctag,ntag,nbit,nctag);
		if(text == temp){
			return text;
		}
		text = temp;
	}
	return null;
}

function allowSimpleTag(text,tag,ctag,ntag,nctag){
	tag = replaceGTLT(tag);
	ctag = replaceGTLT(ctag);
	//n = 1;
	var ok = true;
	while(ok == true){
		//temp = findSimpleTag(text,tag,ctag,ntag+n,nctag+n);
		temp = findSimpleTag(text,tag,ctag,ntag,nctag);
		if(text == temp){
			return text;
		}
		text = temp;
		//n += 1;
	}
	return null;
}

function findTag(text,tag,bit,ctag,ntag,nbit,nctag){
	taglength = tag.length;
	ctaglength = ctag.length;
	bitlength = bit.length;
	temp = text.toLowerCase().indexOf(tag.toLowerCase());
	if(temp == -1){
		return text;
	}
	temp2 = text.toLowerCase().indexOf(bit.toLowerCase(),temp + taglength);
	if(temp2 == -1){
		return text;
	}
	if(tag.indexOf("[")!=-1){
		temp3 = findCloseBrace(text,temp + taglength,temp + taglength);
	}else{
		temp3 = text.toLowerCase().indexOf(ctag.toLowerCase(),temp2 + bitlength);
	}
	if(temp3 == -1){
		return text;
	}
	theTag = text.substring(temp + taglength,temp2)
	//alert(theTag);
	return text.substring(0,temp) + ntag + theTag + nbit + text.substring(temp2 + bit.length, temp3) + nctag + text.substring(temp3 + ctaglength,text.length);
}

function findSimpleTag(text,tag,ctag,ntag,nctag){
	taglength = tag.length;
	ctaglength = ctag.length;
	temp = text.toLowerCase().indexOf(tag.toLowerCase());
	if(temp == -1){
		return text;
	}
	if(tag.indexOf("[")!=-1){
		temp3 = findCloseBrace(text,temp + taglength,temp + taglength);
	}else{
		temp3 = text.toLowerCase().indexOf(ctag.toLowerCase(),temp + taglength);
	}
	//alert("here");
	if(temp3 == -1){
		alert("There was an error when trying to format the text.\n\nProbably due to a missing ']'");
		return text;
	}
	theTag = text.substring(temp + taglength,temp)
	return text.substring(0,temp) + ntag + text.substring(temp + taglength, temp3) + nctag + text.substring(temp3 + ctaglength,text.length);
}

function findCloseBrace(text,pos,cpos){
	nextOpenBrace = text.indexOf("[",pos);
	nextCloseBrace = text.indexOf("]",cpos);
	//alert(nextCloseBrace+ ","+nextOpenBrace);
	if(nextCloseBrace<nextOpenBrace){
		return nextCloseBrace;
	}
	if(nextOpenBrace==-1){
		return nextCloseBrace;
	}
	if(nextCloseBrace==-1){
		return -1;
	}
	if(nextOpenBrace<nextCloseBrace){
		return findCloseBrace(text,nextOpenBrace + 1,nextCloseBrace + 1);
	}
}


function countTags(text,tag){
	var i = 0;
	var pos = -1;
	while (true){
		temp = text.toLowerCase().indexOf(tag.toLowerCase(),pos + 1)
		if(temp == -1){
			return i;
		}
		if(temp >= 0){
			pos = temp;
			i += 1;
		}
	}
	return null;
}

function replaceGTLT(text){
	var gt = />/g;
	var lt = /</g;
	text = text.replace(gt, "&gt;");
	text = text.replace(lt, "&lt;");
	return text;
}

function filterText(text){
	var apostrophe = /'/g;
	var lr = /\r\n/g;
	text = text.replace(apostrophe, "&#39;");
	text = text.replace(lr, "<br>");
	var lr = /\r/g;
	text = text.replace(lr, "<br>"); //mac
	return text;
}
