var gWin;

function parseCodes(txt)
{
	txt = txt.replace(/</gi,"&lt;");	
	txt = txt.replace(/>/gi,"&gt;");	
	txt = txt.replace(/\[B\]/gi,"<B>");
	txt = txt.replace(/\[\/B\]/gi,"</B>");

	txt = txt.replace(/\[I\]/gi,"<I>");
	txt = txt.replace(/\[\/I\]/gi,"</I>");

	txt = txt.replace(/\[U\]/gi,"<U>");
	txt = txt.replace(/\[\/U\]/gi,"</U>");	

	txt = txt.replace(/\[LIST\]/gi,"<UL><LI>");
	txt = txt.replace(/\[\/LIST\]/gi,"</UL>");	
	txt = txt.replace(/\[\*\]/gi,"<LI>");

    // ... make sure HTTP'd links end up with http:// in the href and link text
    txt = txt.replace( /\[link\]http:\/\/(.*?(?=\[\/link\]))\[\/link\]/gi, "<a target=\"_blank\" href=\"http:\/\/$1\">http:\/\/$1</a>" );
    
    // ... handle links without http://
	txt = txt.replace(/\[link\](.*?(?=\[\/link\]))\[\/link\]/gi,"<a target=\"_blank\" href=\"http:\/\/$1\">$1</a>");


    // ... make sure HTTP'd images end up with http:// in the href and link text
    txt = txt.replace( /\[image\]http:\/\/(.*?(?=\[\/image\]))\[\/image\]/gi, "<img src=\"http:\/\/$1\" />" );
    
    // ... handle images without http://
	txt = txt.replace(/\[image\](.*?(?=\[\/image\]))\[\/image\]/gi,"<img src=\"http:\/\/$1\" />");

	txt = txt.replace(/\n/gi,"<BR>");
	return txt;	
}

function stripCodes(txt)
{
	txt = txt.replace(/</gi,"&lt;");	
	txt = txt.replace(/>/gi,"&gt;");	
	txt = txt.replace(/\[B\]/gi,"");
	txt = txt.replace(/\[\/B\]/gi,"");

	txt = txt.replace(/\[I\]/gi,"");
	txt = txt.replace(/\[\/I\]/gi,"");

	txt = txt.replace(/\[U\]/gi,"");
	txt = txt.replace(/\[\/U\]/gi,"");	

	txt = txt.replace(/\[LIST\]/gi,"");
	txt = txt.replace(/\[\/LIST\]/gi,"");	
	txt = txt.replace(/\[\*\]/gi,"");

	txt = txt.replace(/\[link\](.*?(?=\[\/link\]))\[\/link\]/gi,"$1");
	txt = txt.replace(/\[image\](.*?(?=\[\/image\]))\[\/image\]/gi,"$1");

	return txt;
}

function genPreview(txt,desc,endDesc)
{
	var parseTxt;
	var strippedTxt;
	gWin = window.open("", "Preview", 
		"width=350,height=300,toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,top=200,left=200");
	gWin.opener = self;
	
	this.gWin.document.writeln("<HTML>");
	this.gWin.document.writeln("<HEAD><TITLE>Preview</TITLE>");
	this.gWin.document.writeln("<BODY>");

	if ( desc != null && desc != "" )
        {
        this.gWin.document.writeln( "<b>" + desc + "</b>" );
        this.gWin.document.writeln("<BR><BR>");
        }
	parseTxt=parseCodes(txt);
	this.gWin.document.writeln(parseTxt);
	this.gWin.document.writeln("<BR><BR>");
	
//	strippedTxt=stripCodes(txt);
//	this.gWin.document.writeln(strippedTxt);
//	this.gWin.document.writeln("<BR><BR>");
	
	this.gWin.document.writeln( "<hr>" );
    this.gWin.document.writeln( endDesc );
	this.gWin.document.writeln("</BODY>");
	this.gWin.document.writeln("</HTML>");
	
	gWin.document.close();
	
	return false;
}