<!--

var Init = false;
var EventSource;
var SaveRng;
var PicToFormat;

function IsMSIE()
{
	if (navigator.appName == "Microsoft Internet Explorer")
	{
		if (navigator.userAgent.indexOf("Opera") == -1)    // this is not Opera
			return true;
	}
	
	return false;
}

function InitEditor()
{
	if (Init)
		return;
	Init = true;
	
	var oRTE = document.getElementById("rtefrm").contentWindow.document;

	// upon back from another location explicitly realod	
	var body = oRTE.getElementById("rte");
	if (body != null)
	{
		var savepath = document.location.href;
		document.location.href = "_blank";
		document.location.href = savepath;
		return;
	}

	oRTE.designMode = "on";

	var frameHtml = "<html>\n";
	frameHtml += "<head>\n";
	frameHtml += "</head>\n";
	frameHtml += "<body id=\"rte\" BGCOLOR=\"#ffffff\" style=\"SCROLLBAR-FACE-COLOR:#dde0a9;SCROLLBAR-3DLIGHT-COLOR:#f1f2db;SCROLLBAR-ARROW-COLOR:#974b00\"";
	frameHtml += "</body>\n";
	frameHtml += "</html>";

	oRTE.open();
	oRTE.write(frameHtml);
	oRTE.close();
	
	if (!IsMSIE())
	{
		document.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT | Event.MOUSEDOWN | Event.MOUSEUP);
		document.onmouseover = SaveEventSrc;
		document.onmouseout  = SaveEventSrc;
		document.onmousedown = SaveEventSrc;
		document.onmouseup   = SaveEventSrc;
	}

	if (IsMSIE())	
	{
		oRTE.attachEvent('onkeypress', OnKeyPress);
	}

	for (i = 0; i < 13; i++)
	{
		tbbtn = document.getElementById("tbbtn" + i);
		tbbtn.onmouseover = over;
		tbbtn.onmouseout = out;
		tbbtn.onmousedown = down;
		tbbtn.onmouseup = up;
	}
}

function SaveEventSrc(e)
{
	EventSource = e.target;
}

function over() 
{
	this.style.border='2px outset white';
}
	
function out() 
{
	this.style.border='2px solid #dde0a9';
}

function down() 
{
	this.style.border='2px inset #d4d0c8';
}

function up() 
{
	this.style.border='2px outset #d4d0c8';
}
	
function click() 
{
	SetFormat(this.id);
}

function GetEditResult()
{
	var oRTE = document.getElementById("rtefrm").contentWindow.document;
	return oRTE.getElementById("rte").innerHTML;
}

function SetEditContents(Contents)
{
	var oRTE = document.getElementById("rtefrm").contentWindow.document;
	
	oRTEBody = oRTE.getElementById("rte");
	oRTEBody.innerHTML = Contents;
}

function SetBold()
{
	var oRTEWnd = document.getElementById("rtefrm").contentWindow;
	var oRTE = document.getElementById("rtefrm").contentWindow.document;

	var rng;
	if (IsMSIE())
	{
		rng = oRTE.selection.createRange();
		if (rng != null)
			rng.execCommand("Bold");
		oRTEWnd.focus();
	}
	else
	{
		oRTEWnd.focus();
	  	oRTE.execCommand("bold", false, "");
		oRTEWnd.focus();
	}
}

function SetItalic()
{
	var oRTEWnd = document.getElementById("rtefrm").contentWindow;
	var oRTE = document.getElementById("rtefrm").contentWindow.document;

	var rng;
	if (IsMSIE())
	{
		rng = oRTE.selection.createRange();
		if (rng != null)
			rng.execCommand("Italic");
		oRTEWnd.focus();
	}
	else
	{
		oRTEWnd.focus();
	  	oRTE.execCommand("italic", false, "");
		oRTEWnd.focus();
	}
}

function SetUnderline()
{
	var oRTEWnd = document.getElementById("rtefrm").contentWindow;
	var oRTE = document.getElementById("rtefrm").contentWindow.document;

	var rng;
	if (IsMSIE())
	{
		rng = oRTE.selection.createRange();
		if (rng != null)
			rng.execCommand("Underline");
		oRTEWnd.focus();
	}
	else
	{
		oRTEWnd.focus();
	  	oRTE.execCommand("underline", false, "");
		oRTEWnd.focus();
	}
}

function SetLeft()
{
	var oRTEWnd = document.getElementById("rtefrm").contentWindow;
	var oRTE = document.getElementById("rtefrm").contentWindow.document;

	oRTEWnd.focus();
	oRTE.execCommand("JustifyLeft", false, "");
	oRTEWnd.focus();
}

function SetRight()
{
	var oRTEWnd = document.getElementById("rtefrm").contentWindow;
	var oRTE = document.getElementById("rtefrm").contentWindow.document;

	oRTEWnd.focus();
	oRTE.execCommand("JustifyRight", false, "");
	oRTEWnd.focus();
}

function SetCenter()
{
	var oRTEWnd = document.getElementById("rtefrm").contentWindow;
	var oRTE = document.getElementById("rtefrm").contentWindow.document;

	oRTEWnd.focus();
	oRTE.execCommand("JustifyCenter", false, "");
	oRTEWnd.focus();
}


function SetColor()
{
	if (IsMSIE())
	{
		var oRTE = document.getElementById("rtefrm").contentWindow.document;
		SaveRng = oRTE.selection.createRange();
	}

	var LeftOffs = 0;
	var TopOffs = 0;
	var ParentElem = document.getElementById("tbbtn3").parentNode;
	do
	{
		if ((ParentElem.tagName.toUpperCase() != "TR") && 
			(ParentElem.tagName.toUpperCase() != "TBODY") && (ParentElem.tagName.toUpperCase() != "CENTER"))
		{
			LeftOffs += ParentElem.offsetLeft;
			TopOffs += ParentElem.offsetTop;
		}
		ParentElem = ParentElem.parentNode;

	} while ((ParentElem != null) && (ParentElem.tagName != "undefined") && (ParentElem.tagName.toUpperCase() != "FORM"));

	document.getElementById("colorsdlg").style.left = LeftOffs + document.getElementById("tbbtn3").offsetLeft;
	document.getElementById("colorsdlg").style.top = TopOffs + document.getElementById("tbbtn3").offsetTop +
		document.getElementById("tbbtn3").offsetHeight;

	document.getElementById("colorsdlg").style.display = "inline";
}

function ApplyColor(color)
{
	var oRTEWnd = document.getElementById("rtefrm").contentWindow;
	var oRTE = document.getElementById("rtefrm").contentWindow.document;

	if (IsMSIE())
	{
		if (SaveRng != null)
		{
			SaveRng.select();
			SaveRng.execCommand("ForeColor", false, color);		
		}
		oRTEWnd.focus();
	}
	else
	{
		oRTEWnd.focus();
	  	oRTE.execCommand("ForeColor", false, color);
		oRTEWnd.focus();
	}

	document.getElementById("colorsdlg").style.display = "none";
}

function font_nameChange()
{
	var oRTEWnd = document.getElementById("rtefrm").contentWindow;
	var oRTE = document.getElementById("rtefrm").contentWindow.document;

	var fontBox = document.getElementById("fontBox");

	if (fontBox[fontBox.selectedIndex].value != "none")
	{
		oRTEWnd.focus();
		oRTE.execCommand("FontName", false, fontBox[fontBox.selectedIndex].value);
		oRTEWnd.focus();
		fontBox.selectedIndex = 0;
	}
}

function font_sizeChange()
{
	var oRTEWnd = document.getElementById("rtefrm").contentWindow;
	var oRTE = document.getElementById("rtefrm").contentWindow.document;

	var sizeBox = document.getElementById("sizeBox");

	if (sizeBox[sizeBox.selectedIndex].value != "none")
	{
		oRTEWnd.focus();
		oRTE.execCommand("FontSize", false, sizeBox[sizeBox.selectedIndex].value);
		oRTEWnd.focus();
		sizeBox.selectedIndex = 0;
	}
}

function SetQuot()
{
	var oRTEWnd = document.getElementById("rtefrm").contentWindow;
	var oRTE = document.getElementById("rtefrm").contentWindow.document;

	if (IsMSIE())
	{
		rng = oRTE.selection.createRange();
		if ((rng != null) && (rng.htmlText.length != 0))
		{
			var sText = "<BLOCKQUOTE style=\"background-color:#FFFFFF;border-style:inset;border-width:2px\">" + 
				rng.htmlText + "</BLOCKQUOTE>";
			rng.pasteHTML(sText);
		}
	}
	else
	{
		var sel = oRTEWnd.getSelection();
		var sText = "<BLOCKQUOTE style=\"background-color:#FFFFFF;border-style:inset;border-width:2px\">" + 
			sel + "</BLOCKQUOTE>";

		if (PasteDataThroughtClipboard(oRTEWnd, oRTE, "[#@#@#@#]"))
		{
			oRTEBody = oRTE.getElementById("rte");
			oRTEBody.innerHTML = oRTEBody.innerHTML.replace("[#@#@#@#]", sText);
		}
 	}

	oRTEWnd.focus();
}

function InsertQuot(srctext)
{
	var oRTEWnd = document.getElementById("rtefrm").contentWindow;
	var oRTE = document.getElementById("rtefrm").contentWindow.document;

	oRTEWnd.focus();

	oRTEBody = oRTE.getElementById("rte");
	var sText = "<BLOCKQUOTE style=\"background-color:#FFFFFF;border-style:inset;border-width:2px\">" + 
		srctext + "</BLOCKQUOTE>";

	// try to insert on cursor through clipboard
	if (PasteDataThroughtClipboard(oRTEWnd, oRTE, "[#@#@#@#]"))
	{
		oRTEBody.innerHTML = oRTEBody.innerHTML.replace("[#@#@#@#]", sText);
	}
	else // insert to end
	{
		oRTEBody.innerHTML = oRTEBody.innerHTML + sText;
	}
	
	oRTEWnd.focus();
}

function SetLink()
{
	var sTitle = window.prompt("Введите имя линка:");

	if (sTitle == undefined)
		return;

	if (sTitle.length == 0)
		return;

	var sAddress = window.prompt("Введите адрес линка:");
	if (sAddress.length == 0)
		return;

	var oRTEWnd = document.getElementById("rtefrm").contentWindow;
	var oRTE = document.getElementById("rtefrm").contentWindow.document;

	oRTEWnd.focus();

	oRTEBody = oRTE.getElementById("rte");

	// try to insert on cursor through clipboard
	if (PasteDataThroughtClipboard(oRTEWnd, oRTE, "[#@#@#@#]"))
	{
		oRTEBody.innerHTML = oRTEBody.innerHTML.replace("[#@#@#@#]", "<a href=\"" + sAddress + "\" target=\"_blank\">" + sTitle + "</a> ");
	}
	else // insert to end
	{
		oRTEBody.innerHTML = oRTEBody.innerHTML + "<a href=\"" + sAddress + "\" target=\"_blank\">" + sTitle + "</a> ";
	}

	oRTEWnd.focus();
	var fontBox = document.getElementById("fontBox");
	oRTE.execCommand("FontName", false, fontBox[fontBox.selectedIndex].value);
	var sizeBox = document.getElementById("sizeBox");
	oRTE.execCommand("FontSize", false, sizeBox[sizeBox.selectedIndex].value);
	oRTE.execCommand("ForeColor", false, "#000000");
	oRTEWnd.focus();
}

function SetPic()
{
	if (IsMSIE())
	{
		var ret = window.showModalDialog("PrivateImgCollectionFrmst.htm", "Вставить картинку", 
			"dialogWidth:600px;dialogHeight:600px;resizable:no;status:no");
		if (ret != undefined)
			DoSetPic(ret);
	}
	else
	{
		window.open("PrivateImgCollectionFrmst.htm", "Вставить картинку", "left=210,top=100,width=600,height=600,dependent");
	}
}

function DoSetPic(Name)
{
	var oRTEWnd = document.getElementById("rtefrm").contentWindow;
	var oRTE = document.getElementById("rtefrm").contentWindow.document;

	oRTEWnd.focus();
	
	if (Name.search("/t_") == -1) // fullsize picture
	{
		oRTE.execCommand("InsertImage", false, Name);
	}
	else // thumbnail
	{
		FullName = Name.replace("t_", "");
		oRTEBody = oRTE.getElementById("rte");
		
		// try to insert on cursor through clipboard
		if (PasteDataThroughtClipboard(oRTEWnd, oRTE, "[#@#@#@#]"))
		{
			oRTEBody.innerHTML = oRTEBody.innerHTML.replace("[#@#@#@#]", 
				"<a href=\"" + FullName + "\" target=\"_blank\"><img src='" + Name + "'></a> ");
		}
		else // insert to end
		{
			oRTEBody.innerHTML = oRTEBody.innerHTML + "<a href=\"" + 
				FullName + "\" target=\"_blank\"><img src='" + Name + "'></a> ";
		}
	}
	
	oRTEWnd.focus();
}

function SetPicformat()
{
	var oRTEWnd = document.getElementById("rtefrm").contentWindow;
	var oRTE = document.getElementById("rtefrm").contentWindow.document;

	if (IsMSIE())
	{
		if (oRTE.selection.type != "Control")
		{
			alert("Выберите картинку.");
			oRTEWnd.focus();
			return;
		}

		var rng = oRTE.selection.createRange();
		PicToFormat = rng.item(0);
	}
	else
	{
		var sel = oRTEWnd.getSelection();
		var rng = sel.getRangeAt(sel.rangeCount - 1).cloneRange();

		var ancestor = rng.commonAncestorContainer;
		var pic = getFirstElement(ancestor);
		if (pic == null)
		{
			alert("Выберите картинку.");
			oRTEWnd.focus();
			return;
		}
		if (pic.nodeName.toUpperCase() != "IMG")
		{
			alert("Выберите картинку.");
			oRTEWnd.focus();
			return;
		}

		PicToFormat = pic;
	}

	if (PicToFormat.getAttribute("OrgWidth") == null)
	{
		PicToFormat.setAttribute("OrgWidth", PicToFormat.width);
		PicToFormat.setAttribute("CurrZoom", 100);

		CurrZoom = PicToFormat.getAttribute("CurrZoom");
	}

	var LeftOffs = 0;
	var TopOffs = 0;
	var ParentElem = document.getElementById("tbbtn3").parentNode;
	do
	{
		if ((ParentElem.tagName.toUpperCase() != "TR") && 
			(ParentElem.tagName.toUpperCase() != "TBODY") && (ParentElem.tagName.toUpperCase() != "CENTER"))
		{
			LeftOffs += ParentElem.offsetLeft;
			TopOffs += ParentElem.offsetTop;
		}
		ParentElem = ParentElem.parentNode;

	} while ((ParentElem != null) && (ParentElem.tagName != "undefined") && (ParentElem.tagName.toUpperCase() != "FORM"));

	document.getElementById("picfmtdlg").style.left = LeftOffs  + document.getElementById("tbbtn10").offsetLeft;
	document.getElementById("picfmtdlg").style.top = TopOffs  + document.getElementById("tbbtn10").offsetTop + 
		document.getElementById("tbbtn10").offsetHeight;

	if (IsMSIE())
	{
		document.getElementById("picfmtdlg").width = "84px";
		document.getElementById("picfmtdlg").height = "86px";
	}

	document.getElementById("picfmtdlg").style.display = "inline";
}

function ApplyPicFormat(format)
{
	switch(format)
	{
	case "left":
		PicToFormat.align = "left";
		PicToFormat.style.marginLeft = "0px";
		PicToFormat.style.marginRight = "10px";
		break;

	case "center":
		PicToFormat.align = "";
		break;

	case "right":
		PicToFormat.align = "right";
		PicToFormat.style.marginLeft = "10px";
		PicToFormat.style.marginRight = "0px";
		break;

	case "zoomin":
		CurrZoom = PicToFormat.getAttribute("CurrZoom");
		if (CurrZoom > 20)
			CurrZoom -= 20;
		PicToFormat.width = (PicToFormat.getAttribute("OrgWidth") * CurrZoom) / 100;
		PicToFormat.setAttribute("CurrZoom", CurrZoom);
		break;

	case "nozoom":
		PicToFormat.width = PicToFormat.getAttribute("OrgWidth");
		break;

	case "zoomout":
		CurrZoom = PicToFormat.getAttribute("CurrZoom");
		for (i=0; i < 20; i++)
			CurrZoom++;
		PicToFormat.width = (PicToFormat.getAttribute("OrgWidth") * CurrZoom) / 100;
		PicToFormat.setAttribute("CurrZoom", CurrZoom);
		break;

	case "close":
		document.getElementById("picfmtdlg").style.display = "none";
		break;
	}

}

function getFirstElement(_node) 
{
	var kids = _node.childNodes;
	for (var i = 0; i < kids.length; i++)
	{
		if (kids.item(i).nodeType == 1)
		{
			return kids.item(i);
		}
		
	}

	return null;
}

function CommonSmylies()
{
	var LeftOffs = 0;
	var TopOffs = 0;
	var ParentElem = document.getElementById("tbbtn3").parentNode;
	do
	{
		if ((ParentElem.tagName.toUpperCase() != "TR") && 
			(ParentElem.tagName.toUpperCase() != "TBODY") && (ParentElem.tagName.toUpperCase() != "CENTER"))
		{
			LeftOffs += ParentElem.offsetLeft;
			TopOffs += ParentElem.offsetTop;
		}
		ParentElem = ParentElem.parentNode;

	} while ((ParentElem != null) && (ParentElem.tagName != "undefined") && (ParentElem.tagName.toUpperCase() != "FORM"));

	document.getElementById("commonsmylies").style.left = LeftOffs  + document.getElementById("tbbtn11").offsetLeft + 
		+ document.getElementById("tbbtn11").offsetWidth - document.getElementById("commonsmylies").width;

	document.getElementById("commonsmylies").style.top = TopOffs  + document.getElementById("tbbtn11").offsetTop + 
		document.getElementById("tbbtn11").offsetHeight;

	document.getElementById("commonsmylies").style.display = "inline";
}

function InsertSmilic(path)
{
	var oRTEWnd = document.getElementById("rtefrm").contentWindow;
	var oRTE = document.getElementById("rtefrm").contentWindow.document;

	document.getElementById("commonsmylies").style.display = "none";

	if (path.search("MoreSmylies.gif") == -1)
	{
		oRTEWnd.focus();
		oRTE.execCommand("InsertImage", false, path);
		oRTEWnd.focus();
	}
	else
	{
		MoreSmilys();
	}
}

function MoreSmilys()
{
	var oRTEWnd = document.getElementById("rtefrm").contentWindow;
	var oRTE = document.getElementById("rtefrm").contentWindow.document;

	oRTEWnd.focus();

	if (IsMSIE())
	{
		var ret = window.showModalDialog("MoreSmilyesFrm.htm", "", 
			"dialogWidth:940px;dialogHeight:600px;status:no");
		if (ret != undefined)
			InsertMoreSmilic(ret);
	}
	else
	{
		window.open("MoreSmilyesFrm.htm", "Smilies", "left=50,top=50,width=940,height=600,dependent");
	}
}

function InsertMoreSmilic(pathname)
{
	var oRTEWnd = document.getElementById("rtefrm").contentWindow;
	var oRTE = document.getElementById("rtefrm").contentWindow.document;

	oRTEWnd.focus();
	oRTE.execCommand("InsertImage", false, pathname);
	oRTEWnd.focus();
}

function PasteDataThroughtClipboard(Wnd, Doc, sData)
{
	if (IsMSIE())
	{
		Wnd.clipboardData.setData("Text", sData);
		Doc.execCommand("Paste");
		Wnd.clipboardData.clearData("Text");
	}
	else
	{
		try 
		{ 
			netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		} 
		catch (e) 
		{
			return false;
		}

		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
		if (!clip) 
			return false;

		var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
		if (!trans) 
			return false;

		trans.addDataFlavor('text/unicode');
   
		var str = new Object();
		var len = new Object();
   
		var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
   
		var copytext = sData;
   
		str.data = copytext;
   
		trans.setTransferData("text/unicode",str,copytext.length*2);
   
		var clipid = Components.interfaces.nsIClipboard;
   
		if (!clip) 
			return false;

		clip.setData(trans, null, clipid.kGlobalClipboard);
		Doc.execCommand("Paste", false, "");
	}
	
	return true;
}

function OnKeyPress()
{
	var oRTEWnd = document.getElementById("rtefrm").contentWindow;
	var oRTE = document.getElementById("rtefrm").contentWindow.document;

	if (oRTEWnd.event.keyCode == 13) // enter
	{
		oRTEWnd.event.returnValue = false;	
		oRTEWnd.clipboardData.setData("Text", "\r\n");
		oRTE.execCommand("Paste");
	}
}

function Help()
{
	window.open("cbrehelp.htm");	
}
//-->
