function show_preview()
{
	var hWndPreviewWindow = window.open("","HTML Preview","width=520,height=300,scrollbars=yes,resizable=yes, status=0");

	hWndPreviewWindow.document.open();
	hWndPreviewWindow.document.writeln('<HTML><HEAD><TITLE>HTML Preview</TITLE></HEAD>');
	hWndPreviewWindow.document.writeln('<BODY BGCOLOR="white">');
	var box = document.getElementById ("entrycontent");
	hWndPreviewWindow.document.write(box.value);
	hWndPreviewWindow.document.writeln('</BODY></HTML>');
	hWndPreviewWindow.document.close();
}

function flash_embed_click ()
{
	var box = document.getElementById ("entrycontent");
	owidth = prompt ("Object width");
	if (owidth == null)
		return;
	oheight = prompt ("Object height");
	if (oheight == null)
		return;
	osource = prompt ("Object URL (source)");
	if (osource == null)
		return;
	
	tag = "<object type=\"application/x-shockwave-flash\" width=\"" + owidth + "\" height=\"" + oheight + "\" data=\"" + osource + "\">";
	tag = tag + "\r\n<param name=\"movie\" value=\"" + osource + "\" />";
	tag = tag + "\r\n<param name=\"wmode\" value=\"transparent\" />";
	tag = tag + "\r\n</object>";
	box.value = box.value.substring (0, box.selectionStart) + tag + box.value.substring (box.selectionEnd, box.value.length);

	box.focus ();
}

function smilie_click (smilie)
{
	var box = document.getElementById ("entrycontent");
	box.value = box.value.substring (0, box.selectionStart) + smilie + box.value.substring (box.selectionEnd, box.value.length);

	box.focus ();
}

function tag_click(opentag, closetag)
{
	var box = document.getElementById ("entrycontent");

	opentag = '<' + opentag + '>';
	closetag = '</' + closetag + '>';

	box.value = box.value.substring (0, box.selectionStart) + opentag + box.value.substring (box.selectionStart, box.selectionEnd) + closetag + box.value.substring (box.selectionEnd, box.value.length);

	box.focus ();
}

function bbcode_click (opencode, closecode)
{
	var box = document.getElementById ("entrycontent");
	opencode = "[" + opencode + "]";
	closecode = "[/" + closecode + "]";
	
	seltxt = box.value.substring (box.selectionStart, box.selectionEnd);
		
	box.value = box.value.substring (0, box.selectionStart) + opencode + seltxt + closecode + box.value.substring (box.selectionEnd, box.value.length);
	box.focus ();
}

function bbquote_click ()
{
	var box = document.getElementById ("entrycontent");
	opencode = "[QUOTE]";
	closecode = "[/QUOTE]";

	if (box.value.substring (box.selectionStart, box.selectionEnd).length != 0)
	{	
		seltxt = box.value.substring (box.selectionStart, box.selectionEnd);
	}
	else
	{
		seltxt = document.getSelection();
	}
		
	box.value = box.value.substring (0, box.selectionStart) + opencode + seltxt + closecode + box.value.substring (box.selectionEnd, box.value.length);
	box.focus ();
}


function shorttag_click(tag)
{
	var box = document.getElementById ("entrycontent");

	shorttag = '<' + tag + ' />';

	box.value = box.value.substring (0, box.selectionStart) + shorttag + box.value.substring (box.selectionStart, box.value.length);

	box.focus ();
}

function list_tag (listopen, listclose)
{
	var box = document.getElementById ("entrycontent");

	opentag = '<' + listopen + '>';
	closetag = '</' + listclose + '>';
	numitems = prompt ('Number of list items');
	if (numitems == null)
		return;
	num = parseInt (numitems);
	tagtext = opentag + "\r\n";
	for (i = 1; i <= num; i = i + 1)
	{
		tagtext = tagtext + "  <li></li>\r\n";
	}
	tagtext = tagtext + closetag + "\r\n";

	box.value = box.value.substring (0, box.selectionStart) + tagtext + box.value.substring (box.selectionEnd, box.value.length);
	
	box.focus ();
}

function table_tag ()
{
	var box = document.getElementById ("entrycontent");
	numrows = prompt ("Number of rows");
	numcols = prompt ("Number of columns");
	header = confirm ("Do you want a header row?")
	
	if (numrows == null || numcols == null)
		return;
		
	nr = parseInt (numrows);
	nc = parseInt (numcols);
	tagtext = "<table>\r\n";
	if (header)
	{
		tagtext += "<thead>\r\n";
		tagtext += "  <tr>\r\n";
		for (i = 1; i <=	nc; i = i + 1)
		{
			tagtext += "   <td></td>\r\n";
		}
		tagtext +="  </tr>\r\n</thead>\r\n";
	}
		
	tagtext += "<tbody>\r\n";
	for (i = 1; i <= nr; i = i + 1)
	{
		tagtext += "  <tr>\r\n";
		for (j = 1; j <= nc; j = j + 1)
			tagtext += "    <td></td>\r\n";
		tagtext += "  </tr>\r\n";
	}
	tagtext += "</tbody>\r\n</table>\r\n";
	
	box.value = box.value.substring (0, box.selectionStart) + tagtext + box.value.substring (box.selectionEnd, box.value.length);
	
	box.focus ();	
}

function image_tag()
{
	imgsrc = prompt ('Image URL', 'http://');
	if (imgsrc == null)
		return;
	alttxt = prompt ('Title/alternate text');
	if (alttxt == null)
		return;

	tag = 'img src="' + imgsrc + '" alt="' + alttxt + '" title="' + alttxt + '"';
	shorttag_click (tag);
}

function link_tag()
{
	linkurl = prompt ('URL', 'http://');
	if (linkurl == null)
		return;
	
	nofollow = confirm ("No follow?");

	otag = 'a href="' + linkurl + '"';
	if (nofollow)
		otag += ' rel="nofollow"'
		
	ctag = 'a';
	tag_click (otag, ctag);
}
