// version from http://www.abclinuxu.cz
// http://www.alexking.org - LGPL
// enhanced

function insertAtCursor(prefix, postfix)
{
	var re = new RegExp("^(.*\\S)(\\s*)$");
	document.forms[0].content.focus();
	pos = document.forms[0].content.scrollTop;
	
	//IE support
	if (document.selection)
	{
		sel = document.selection.createRange();
		var selection = sel.text;
		var wasEmpty = (selection == "");
		var space = "";
		if (!wasEmpty)
		{
			var matches = selection.match(re);
			if (matches)
			{
				selection = RegExp.$1;
				space = RegExp.$2;
			}
		}
		sel.text = prefix+selection+postfix+space;
		sel.collapse(false);
		if (wasEmpty)
		{
			sel.moveEnd('character',-(prefix.length+1))
		}
		sel.select();
	}
	
	//MOZILLA/NETSCAPE support
	else
	{
		if (document.forms[0].content.selectionStart || document.forms[0].content.selectionStart == '0')
		{
			var startPos = document.forms[0].content.selectionStart;
			var endPos = document.forms[0].content.selectionEnd;
			var selection = document.forms[0].content.value.substring(startPos, endPos);
			var wasEmpty = (startPos == endPos);
			var space = "";
			
			if (!wasEmpty)
			{
				var matches = selection.match(re);
				if (matches)
				{
					selection = RegExp.$1;
					space = RegExp.$2;
				}
			}
			document.forms[0].content.value = document.forms[0].content.value.substring(0, startPos) + prefix+selection+postfix+space + document.forms[0].content.value.substring(endPos, document.forms[0].content.value.length);
			var newPosition;
			if (wasEmpty)
			{
				newPosition = startPos+prefix.length;
			}
			else
			{
				newPosition = startPos+prefix.length+selection.length+postfix.length+space.length;
			}
			document.forms[0].content.setSelectionRange(newPosition, newPosition);
		}
		else
		{
			document.forms[0].content.value += prefix+postfix;
			document.forms[0].content.setSelectionRange(startPos+prefix.length, startPos+prefix.length);
		}
	}
	document.forms[0].content.scrollTop = pos;
}

function insertAtCursor2(prefix, postfix)
{
	var re = new RegExp("^(.*\\S)(\\s*)$");
	document.forms[0].content.focus();
	pos = document.forms[0].content.scrollTop;
	
	//IE support
	if (self.opener.document.selection)
	{
		sel = self.opener.document.selection.createRange();
		var selection = sel.text;
		var wasEmpty = (selection == "");
		var space = "";
		if (!wasEmpty)
		{
			var matches = selection.match(re);
			if (matches)
			{
				selection = RegExp.$1;
				space = RegExp.$2;
			}
		}
		sel.text = prefix+selection+postfix+space;
		sel.collapse(false);
		if (wasEmpty)
		{
			sel.moveEnd('character',-(prefix.length+1))
		}
		sel.select();
	}
	
	//MOZILLA/NETSCAPE support
	else
	{
		if (document.forms[0].content.selectionStart || document.forms[0].content.selectionStart == 0)
		{
			var startPos = document.forms[0].content.selectionStart;
			var endPos = document.forms[0].content.selectionEnd;
			var selection = document.forms[0].content.value.substring(startPos, endPos);
			var wasEmpty = (startPos == endPos);
			var space = "";
			
			if (!wasEmpty)
			{
				var matches = selection.match(re);
				if (matches)
				{
					selection = RegExp.$1;
					space = RegExp.$2;
				}
			}
			document.forms[0].content.value = document.forms[0].content.value.substring(0, startPos) + prefix+selection+postfix+space + document.forms[0].content.value.substring(endPos, document.forms[0].content.value.length);
			var newPosition;
			if (wasEmpty)
			{
				newPosition = startPos+prefix.length;
			}
			else
			{
				newPosition = startPos+prefix.length+selection.length+postfix.length+space.length;
			}
			document.forms[0].content.setSelectionRange(newPosition, newPosition);
		}
		else
		{
			document.forms[0].content.value += prefix+postfix;
			document.forms[0].content.setSelectionRange(startPos+prefix.length, startPos+prefix.length);
		}
	}
	document.forms[0].content.scrollTop = pos;
}

var wnd;

function dlgTable()
{
	wnd = window.open("", "dlgTable", "width=240,height=240,status=no,resizable=yes,top=200,left=200");
	wnd.document.writeln('<html><head><meta http-equiv="Content-type" content="text/html; charset=iso-8859-2"><title>Vložit tabulku</title><link rel=stylesheet href="/style/popup.css" type="text/css"><script language="javascript1.2" src="/scripts/insert_at_cursor.js"></script></head><body><form name=myform><table>');
	wnd.document.writeln('<tr><th>Sloupců:<td><input type=text name=cols>');
	wnd.document.writeln('<tr><th>Řádků:<td><input type=text name=rows>');
	wnd.document.writeln('<tr><th>Styl:<td><select name=style><option value=1>prvni</option><option value=2>druha</option></select>');
	wnd.document.writeln('<tr class=buttons><th><td><input type=button value="OK" onclick="var tbl; tbl = \'\'; for(i = 0; i < document.myform.rows.value; i++) { for(j = 0; j < document.myform.cols.value; j++) tbl = tbl + \'|\'; tbl = tbl + \'\\n\'; } insertAtCursor2(self.opener.document.forms[0].content, tbl, \'\'); window.close();"><input type=button value="Zrusit" onclick="window.close();"></table></form></body></html>');
	wnd.document.close();
}

function dlgLink()
{
	wnd = window.open("", "dlgLink", "width=240,height=240,status=no,resizable=yes,top=200,left=200");
	wnd.document.writeln('<html><head><meta http-equiv="Content-type" content="text/html; charset=iso-8859-2"><title>Vložit tabulku</title><link rel=stylesheet href="/style/popup.css" type="text/css"><script language="javascript1.2" src="/scripts/insert_at_cursor.js"></script></head><body><form name=myform><table>');
	wnd.document.writeln('<tr><td colspan=2><input type=radio name=type value=1 id=type1><label for=type1>Existující stránka</label>');
	wnd.document.writeln('<tr><th>Název:<td><select name=oldname size=6></select>');
	wnd.document.writeln('<tr><td colspan=2><input type=radio name=type value=2 id=type2><label for=type2>Nová stránka</label>');
	wnd.document.writeln('<tr><th>Název:<td><input type=text name=newname>');
	wnd.document.writeln('<tr><td colspan=2><input type=radio name=type value=3 id=type3><label for=type3>Externí odkaz</label>');
	wnd.document.writeln('<tr><th>URL:<td><input type=text name=url>');
	wnd.document.writeln('<tr><td colspan=2><hr>');
	wnd.document.writeln('<tr><th>Styl:<td><select name=style><option value=1>prvni</option><option value=2>druha</option></select>');
	wnd.document.writeln('<tr class=buttons><th><td><input type=button value="OK" onclick="var tbl; tbl = \'\'; for(i = 0; i < document.myform.rows.value; i++) { for(j = 0; j < document.myform.cols.value; j++) tbl = tbl + \'|\'; tbl = tbl + \'\\n\'; } insertAtCursor2(self.opener.document.forms[0].content, tbl, \'\'); window.close();"><input type=button value="Zrusit" onclick="window.close();"></table></form></body></html>');
	wnd.document.close();
}

function dlgImage()
{
	wnd = window.open("", "dlgImage", "width=240,height=240,status=no,resizable=yes,top=200,left=200");
	wnd.document.writeln('<html><head><meta http-equiv="Content-type" content="text/html; charset=iso-8859-2"><title>Vložit tabulku</title><link rel=stylesheet href="/style/popup.css" type="text/css"><script language="javascript1.2" src="/scripts/insert_at_cursor.js"></script></head><body><form name=myform><table>');
	wnd.document.writeln('<tr><td colspan=2><input type=radio name=type value=\"qwe\">');
	wnd.document.writeln('<tr><th>Sloupců:<td><input type=text name=cols>');
	wnd.document.writeln('<tr><th>Řádků:<td><input type=text name=rows>');
	wnd.document.writeln('<tr><th>Styl:<td><select name=style><option value=1>prvni</option><option value=2>druha</option></select>');
	wnd.document.writeln('<tr class=buttons><th><td><input type=button value="OK" onclick="var tbl; tbl = \'\'; for(i = 0; i < document.myform.rows.value; i++) { for(j = 0; j < document.myform.cols.value; j++) tbl = tbl + \'|\'; tbl = tbl + \'\\n\'; } insertAtCursor2(self.opener.document.forms[0].content, tbl, \'\'); window.close();"><input type=button value="Zrusit" onclick="window.close();"></table></form></body></html>');
	wnd.document.close();
}

