function getIFrameDocument(id)
{
	if (document.getElementById(id).contentDocument)
		return document.getElementById(id).contentDocument;
	return document.frames[id].document;
}

function editorInit()
{
	var doc = getIFrameDocument("editor");
	doc.designMode = "On";
	if (doc.selection)
	{
		doc.attachEvent("onkeydown", function()
		{
			var evt = getIFrameDocument("editor").parentWindow.event;
			switch (evt.keyCode)
			{
				case 13:
					if (!evt.shiftKey)
					{
						evt.cancelBubble = true;
						return false;
					}
					break;
				case 45:
				case 67:
					if (evt.ctrlKey)
						editorCopy("editor", true);
					break;
			}
		});
		doc.attachEvent("onkeyup", function()
		{
			var doc = getIFrameDocument("editor");
			var evt = doc.parentWindow.event;
			if (evt.keyCode == 13 && !evt.shiftKey)
			{
				var sel = doc.selection;
				var rg = sel.createRange();
				rg.pasteHTML("<br />");
				rg.select();
				rg.moveEnd("character", 1);
				rg.moveStart("character", 1);
				rg.collapse(false);
			}
		});
	}
	setTimeout("editorFocus();", 0);
}
window.attachEvent("onload", editorInit);

function editorFocus()
{
	getIFrameDocument("editor").body.focus();
}

function editorCopy(id, prepareOnly)
{
	var doc = getIFrameDocument(id);
	var body = doc.body;
	if (body.firstChild && !body.firstChild.tagName)
	{
		var html = body.innerHTML;
		while (body.firstChild)
			body.removeChild(body.firstChild);
		var div = doc.createElement("DIV");
		div.innerHTML = html;
		body.appendChild(div);
	}
	for (var i = 0; i < body.childNodes.length; i++)
	{
		var e = body.childNodes[i];
		if (/P|DIV|H1|H2|H3|H4|H5|H6|TABLE/.test(e.tagName))
			e.style.direction = "rtl";
	}
	if (prepareOnly)
		return;
	doc.execCommand("SelectAll", false, null);
	try
	{
		doc.execCommand("Copy", false, null);
	}
	catch (e)
	{
		alert(e.message);
	}
}
