function testBrowserType() {
	alert(navigator.userAgent.indexOf("Opera"));
	if (document.htmledit) {
		alert("123");
	if (navigator.userAgent.indexOf("Opera") >= 0) {
		Switch2Text();		
	}
	}
}

function RefreshBody() {
    if (document.Compose.plainmode.value == 0) {        
        document.Compose.CONTENT.value = editor.GetHTML().replace(/\\n/g,"")
    }        
}
    
function Switch2Text() {
   
    if (document.Compose.plainmode.value == 0) { 
        //document.Compose.CONTENT.value = editor.GetText();
        text = editor.GetHTML();
        //text = text.replace(/<br\s*\/?>\n/ig, "\n");
        //text = text.replace(/<br\s*\/?>/ig, "\n");

        //text = text.replace(/\2/, "3");
        document.Compose.CONTENT.value = text;
        
        //select plain tab
        document.getElementById('editmode_plain').style.display = 'block';
        document.getElementById('editmode_advanced').style.display = 'none';
        
        //textarea
        var texarea = document.Compose.CONTENT;
        var par = texarea.parentNode;
        texarea.style.visibility = "visible";
        texarea.style.position = "relative";
        
        frame = document.getElementById('htmledit');
        frame.style.visibility = "hidden";
        frame.setAttribute("width","0");
        frame.setAttribute("height","0");
        
        frame.previousSibling.style.visibility = "hidden";
        frame.previousSibling.setAttribute("width","0");
        frame.previousSibling.style.position = "absolute";
        frame.previousSibling.style.top = "0px";
        frame.previousSibling.style.left = "0px";
        
        //поменять урл в русской клавиатуре
        //var keyb = document.getElementById('keyboard');
        //keyb.href = "keyboard.html";

        // IE bug:
        // visible text in some cases does not match value
        // this action sloves this problem
        document.Compose.CONTENT.focus();
    }
    document.Compose.plainmode.value = '1'; 
}

function Switch2Html()	{    
    if (document.Compose.plainmode.value == 1) {
        //editor.SetHTML(document.Compose.CONTENT.value); 
        editor.SetText(document.Compose.CONTENT.value); 
        
        //select plain tab
        document.getElementById('editmode_plain').style.display = 'none';
        document.getElementById('editmode_advanced').style.display = 'block';
        
        //textarea
        var texarea = document.Compose.CONTENT;
        var par = texarea.parentNode;
        texarea.style.visibility = "hidden";
        texarea.style.position = "absolute";
        //texarea.style.top = "0px";
        //texarea.style.left = "0px";
        
        frame = document.getElementById('htmledit');
        frame.style.visibility = "visible";
        frame.setAttribute("width","100%");
        frame.setAttribute("height","300");
        
        frame.previousSibling.style.visibility = "visible";    
        frame.previousSibling.style.position = "relative";
        //frame.previousSibling.style.top = "0px";
        //frame.previousSibling.style.left = "0px";
        
        //поменять урл в русской клавиатуре
        //var keyb = document.getElementById('keyboard');
        //keyb.href = "keyboard.html";
    }
    document.Compose.plainmode.value = '0';
}     

	var BUTTON_IMAGE_PREFIX = "buttonImage";
	var BUTTON_DIV_PREFIX = "buttonDiv";
	var BUTTON_PAD1_PREFIX = "buttonPad1";
	var BUTTON_PAD2_PREFIX = "buttonPad2";
	var buttonid = 0;
	var buttonMap = new Object();

	function Button
	(
		caption,
		action,
		image
	)
	{
		this.caption = caption;
		this.action = action;
		this.image = "images"+image;
		this.Instantiate = ButtonInstantiate;
	}

	function ButtonInstantiate()
	{
		this.id = buttonid++;;
		buttonMap[this.id] = this;
		var html = "";
		html += '<div id="';
		html += BUTTON_DIV_PREFIX;
		html += this.id;
		html += '" class="bnn"';
		html += ' onselectstart="ButtonOnSelectStart()"';
		html += ' ondragstart="ButtonOnDragStart()"';
		html += ' onmousedown="ButtonOnMouseDown(this)"';
		html += ' onmouseup="ButtonOnMouseUp(this)"';
		html += ' onmouseout="ButtonOnMouseOut(this)"';
		html += ' onmouseover="ButtonOnMouseOver(this)"';
		html += ' onclick="ButtonOnClick(this)"';
		html += ' ondblclick="ButtonOnDblClick(this)"';
		html += '>';
		html += '<img id="';
		html += BUTTON_PAD1_PREFIX;
		html += this.id;
		html += '" width=2 height=2>';
		html += '<img id="';
		html += BUTTON_IMAGE_PREFIX;
		html += this.id;
		html += '" src="';
		html += this.image;
		html += '" title="';
		html += this.caption;
		html += '" class="Image"';
		html += '>';
		html += '<img id="';
		html += BUTTON_PAD2_PREFIX;
		html += this.id;
		html += '" width=2 height=2>';
		html += '</div>';
		document.write(html);
	}

	function ButtonOnSelectStart()
	{
		window.event.returnValue = false;
	}

	function ButtonOnDragStart()
	{
		window.event.returnValue = false;
	}

	function ButtonOnMouseDown(element)
	{
		if (event.button == 1)
		{
			var id = element.id.substring(BUTTON_DIV_PREFIX.length, element.id.length);
			var button = buttonMap[id];
			ButtonPushButton(id);
		}
	}

	function ButtonOnMouseUp(element)
	{
		if (event.button == 1)
		{
			var id = element.id.substring(BUTTON_DIV_PREFIX.length, element.id.length);
			var button = buttonMap[id];
			ButtonReleaseButton(id);
		}
	}

	function ButtonOnMouseOut(element)
	{
		var id = element.id.substring(BUTTON_DIV_PREFIX.length, element.id.length);
		var button = buttonMap[id];
		ButtonReleaseButton(id);
		document.all[BUTTON_DIV_PREFIX + id].className = "bnn";
		
	}

	function ButtonOnMouseOver(element)
	{
		var id = element.id.substring(BUTTON_DIV_PREFIX.length, element.id.length);
		var button = buttonMap[id];
		ButtonReleaseButton(id);
		document.all[BUTTON_DIV_PREFIX + id].className = "bnmo";
	}

	function ButtonOnClick(element)
	{
		var id = element.id.substring(BUTTON_DIV_PREFIX.length, element.id.length);
		var button = buttonMap[id];
		eval(button.action);
	}

	function ButtonOnDblClick(element)
	{
		ButtonOnClick(element);
	}

	function ButtonPushButton(id)
	{
		document.all[BUTTON_PAD1_PREFIX + id].width = 3;
		document.all[BUTTON_PAD1_PREFIX + id].height = 3;
		document.all[BUTTON_PAD2_PREFIX + id].width = 1;
		document.all[BUTTON_PAD2_PREFIX + id].height = 1;
		document.all[BUTTON_DIV_PREFIX + id].className = "bnp";
	}

	function ButtonReleaseButton(id)
	{
		document.all[BUTTON_PAD1_PREFIX + id].width = 2;
		document.all[BUTTON_PAD1_PREFIX + id].height = 2;
		document.all[BUTTON_PAD2_PREFIX + id].width = 2;
		document.all[BUTTON_PAD2_PREFIX + id].height = 2;
		document.all[BUTTON_DIV_PREFIX + id].className = "bnn";
	}


	function Editor()
	{
		this.instantiated = false;
		this.Instantiate = EditorInstantiate;
		this.GetText = EditorGetText;
		this.SetText = EditorSetText;
		this.GetHTML = EditorGetHTML;
		this.SetHTML = EditorSetHTML;
	}

	function EditorInstantiate()
	{
		var html = "";		
		
		html += "<table class=menu_font cellpadding=0 cellspacing=0 border=0 width='100%' style=''><tr>";
		
		html += "<td><img src=images/0.gif height=1><br/>";
		html += "<table cellspacing=0 class=sort2><tr>";
		html += "<td>";
		html += "<select onchange=\"EditorOnFont(this)\" class=tah70>";
		html += "<option class=\"Heading\">Шрифт</option>";
		html += "<option value=\"Arial\">Arial</option>";
		html += "<option value=\"Arial Black\">Arial Black</option>";
		html += "<option value=\"Arial Narrow\">Arial Narrow</option>";
		html += "<option value=\"Comic Sans MS\">Comic Sans MS</option>";
		html += "<option value=\"Courier New\">Courier New</option>";
		html += "<option value=\"System\">System</option>";
		html += "<option value=\"Times New Roman\">Times New Roman</option>";
		html += "<option value=\"Verdana\">Verdana</option>";
		html += "<option value=\"Wingdings\">Wingdings</option>";
                html += "</select>";
		html += "</td>";
		html += "<td>";
		html += "<select onchange=\"EditorOnSize(this)\" class=tah70>";
		html += "<option class=\"Heading\">Размер</option>";
		html += "<option value=\"1\">1</option>";
		html += "<option value=\"2\">2</option>";
		html += "<option value=\"3\">3</option>";
		html += "<option value=\"4\">4</option>";
		html += "<option value=\"5\">5</option>";
		html += "</select>";
		html += "</td>";
		html += "<td>";
		html += "<scr"+"ipt>";
		html += "var b4 = new Button(";
		html += "\"Цвет текста\",";
		html += "\"EditorOnForegroundColor()\",";
		html += "\"/font_col.gif\"";
		html += ");";
		html += "b4.Instantiate();";
		html += "</scr"+"ipt>";
		html += "</td>";
		html += "<td>";
		html += "<scr"+"ipt>";
		html += "var b1 = new Button(";
		html += "\"Жирный\",";
		html += "\"EditorOnBold()\",";
		html += "\"/bold_kn.gif\"";
		html += ");";
		html += "b1.Instantiate();";
		html += "</scr"+"ipt>";
		html += "</td>";
		html += "<td>";
		html += "<scr"+"ipt>";
		html += "var b2 = new Button(";
		html += "\"Курсив\",";
		html += "\"EditorOnItalic()\",";
		html += "\"/ital_kn.gif\"";
		html += ");";
		html += "b2.Instantiate();";
		html += "</scr"+"ipt>";
		html += "</td>";
		html += "<td>";
		html += "<scr"+"ipt>";
		html += "var b3 = new Button(";
		html += "\"Подчеркнутый\",";
		html += "\"EditorOnUnderline()\",";
		html += "\"/podch_kn.gif\"";
		html += ");";
		html += "b3.Instantiate();";
		html += "</scr"+"ipt>";
		html += "</td>";
		
		html += "<td><img src=/raz_line.gif width=1 height=16></td>";
		html += "<td>";
		html += "<scr"+"ipt>";
		html += "var b6 = new Button(";
		html += "\"Выравнивание влево\",";
		html += "\"EditorOnAlignLeft()\",";
		html += "\"/al_lef.gif\"";
		html += ");";
		html += "b6.Instantiate();";
		html += "</scr"+"ipt>";
		html += "</td>";
		html += "<td>";
		html += "<scr"+"ipt>";
		html += "var b7 = new Button(";
		html += "\"Центрирование\",";
		html += "\"EditorOnCenter()\",";
		html += "\"/al_cent.gif\"";
		html += ");";
		html += "b7.Instantiate();";
		html += "</scr"+"ipt>";
		html += "</td>";
		html += "<td>";
		html += "<scr"+"ipt>";
		html += "var b8 = new Button(";
		html += "\"Выравнивание вправо\",";
		html += "\"EditorOnAlignRight()\",";
		html += "\"/al_rig.gif\"";
		html += ");";
		html += "b8.Instantiate();";
		html += "</scr"+"ipt>";
		html += "</td>";
		html += "<td><img src=/raz_line.gif width=1 height=16></td>";
		html += "<td>";
		html += "<scr"+"ipt>";
		html += "var b9 = new Button(";
		html += "\"Уменьшить отступ\",";
		html += "\"EditorOnDecreaseIndent()\",";
		html += "\"/vistup.gif\"";
		html += ");";
		html += "b9.Instantiate();";
		html += "</scr"+"ipt>";
		html += "</td>";
		html += "<td>";
		html += "<scr"+"ipt>";
		html += "var b9 = new Button(";
		html += "\"Увеличить отступ\",";
		html += "\"EditorOnIncreaseIndent()\",";
		html += "\"/otstup.gif\"";
		html += ");";
		html += "b9.Instantiate();";
		html += "</scr"+"ipt>";
		html += "</td>";
		html += "<td><img src=/raz_line.gif width=1 height=16></td>";
		html += "<td>";
		html += "<scr"+"ipt>";
		html += "var b10 = new Button(";
		html += "\"Гиперссылка\",";
		html += "\"EditorOnCreateHyperlink()\",";
		html += "\"/web.gif\"";
		html += ");";
		html += "b10.Instantiate();";
		html += "</scr"+"ipt>";
		html += "</td>";
		html += "<td>";
		html += "<scr"+"ipt>";
		html += "var b11 = new Button(";
		html += "\"Смайлик\",";
		html += "\"AddSmile()\",";
		html += "\"/smilee.gif\"";
		html += ");";
		html += "b11.Instantiate();";
		html += "</scr"+"ipt>";
		html += "</td>";
		html += "<td>";
		html += "<scr"+"ipt>";
		html += "var b12 = new Button(";
		html += "\"Жест\",";
		html += "\"AddHand()\",";
		html += "\"/up.gif\"";
		html += ");";
		html += "b12.Instantiate();";
		html += "</scr"+"ipt>";
		html += "</td></tr></table>";
		html += "</tr></table>";
		
		html += "<iframe id=\"htmledit\" width=\"100%\" height=\"300px\" class=wr_let style=''>";
		
		html += "</iframe>";
		document.write(html);
		html = '';
		html += '<body class=wr_let>';
		html += document.Compose.CONTENT.value.replace(/\\n/g,"");
		html += '</body>';
		
		htmledit.document.open();
		htmledit.document.write(html);
		htmledit.document.close();
		htmledit.document.designMode = "on";
		htmledit.document.domain="nnov.ru";
		this.instantiated = true;
	}
	

	function  EditorGetText()
	{
		return htmledit.document.body.innerText;
	}

	function  EditorSetText(text)
	{
		//text = text.replace(/\n/g, "<br/>\n");
		text = text.replace(/\2/, "3");
		htmledit.document.body.innerHTML = text;
	}

	function  EditorGetHTML()
	{
		EditorCleanHTML();
		EditorCleanHTML();
		return htmledit.document.body.innerHTML;
	}

	function  EditorSetHTML(html)
	{
		htmledit.document.body.innerHTML = html;
	}


	function EditorOnBold()
	{
		EditorFormat("bold");
	}

	function EditorOnItalic()
	{
		EditorFormat("italic");
	}

	function EditorOnUnderline()
	{
		EditorFormat("underline");
	}

	function EditorOnForegroundColor()
	{
		var color = showModalDialog("color.html", " ", "font-family:Verdana;font-size:12;dialogWidth:28em;dialogHeight:21em");
		if (color) {
			EditorFormat("forecolor", color);
		}
		else {

			htmledit.focus();
		}
	}

	function AddSmile()
	{
		var smile = showModalDialog("smile.html", " ", "font-family:Verdana;font-size:12;dialogWidth:16em;dialogHeight:12em");
		htmledit.focus();
		if (smile && smile.length) {
				var range = htmledit.document.selection.createRange();
                                range.pasteHTML('<img src="' + smile + '">');
                                range.select();
		}
		else {

			htmledit.focus();
		}
	}
 
	function AddHand()
	{
                var smile = showModalDialog("hand.html", " ", "font-family:Verdana;font-size:8;dialogWidth:15em;dialogHeight:4em");
		htmledit.focus();
                if (smile && smile.length) {
                                var range = htmledit.document.selection.createRange();
                                range.pasteHTML('<img src="' + smile + '">');
                                range.select();
                }
		else {

			htmledit.focus();
		}
	}

	function IntImg()
	{
                var IntImg = showModalDialog("intimg.html", " ", "font-family:Verdana;font-size:8;dialogWidth:35em;dialogHeight:20em");
				htmledit.focus();
                if (IntImg && IntImg.length) {
                                var range = htmledit.document.selection.createRange();
                                range.pasteHTML('<img src="' + IntImg + '">');
                                range.select();
                }
		else {

			htmledit.focus();
		}
	}
	
	function EditorOnAlignLeft()
	{
		EditorFormat("justifyleft");
	}

	function EditorOnCenter()
	{
		EditorFormat("justifycenter");
	}

	function EditorOnAlignRight()
	{
		EditorFormat("justifyright");
	}

	function EditorOnDecreaseIndent()
	{
		EditorFormat("outdent");
	}

	function EditorOnIncreaseIndent()
	{
		EditorFormat("indent");
	}

	function EditorOnCreateHyperlink()
	{
		var anchor = EditorGetElement("A", htmledit.document.selection.createRange().parentElement());
		var link = prompt("Введите адрес ссылки (например, http://www.extreme.nnov.ru):", anchor ? anchor.href : "http://");
		if (link && link != "http://") {
			if (htmledit.document.selection.type == "None") {
				var range = htmledit.document.selection.createRange();
				range.pasteHTML('<A HREF="' + link + '"></A>');
				range.select();
			}
			else {
				EditorFormat("CreateLink", link);
			}
		}
	}

	function EditorOnFont(select)
	{
		EditorFormat("fontname", select[select.selectedIndex].value);
		select.selectedIndex = 0;
	}

	function EditorOnSize(select)
	{
		EditorFormat("fontsize", select[select.selectedIndex].value);
		select.selectedIndex = 0;
	}

	function EditorFormat(what, opt)
	{
		htmledit.focus();
		if (opt == "removeFormat") {
			what = opt;
			opt = null;
		}
		if (opt == null) {
			htmledit.document.execCommand(what);
		}
		else {
			htmledit.document.execCommand(what, "", opt);
		}
	}

	function EditorCleanHTML()
	{
		htmledit.document.domain="nnov.ru";
		var fonts = htmledit.document.body.all.tags("FONT");		
		for (var i = fonts.length - 1; i >= 0; i--) {
			var font = fonts[i];
			if (font.style.backgroundColor == "#ffffff") {
				font.outerHTML = font.innerHTML;
			}
		}
	}

	function EditorGetElement(tagName, start)
	{
		while (start && start.tagName != tagName) {
			start = start.parentElement;
		}
		return start;
	}


function spell_open(button) {
    document.domain="nnov.ru";
    var posX = 50;
    var posY = 50;
    var text = '';
    if (document.forms['Compose'].plainmode.value==0) {
        text = htmledit.document.body.innerHTML;
        document.forms['Compose'].elements['HTMLMessage'].value = '1';
    }
    else {
        text = document.forms['Compose'].elements['CONTENT'].value;
        document.forms['Compose'].elements['HTMLMessage'].value = '';
    }
    
    document.forms['Compose'].elements['text'].value = text;    
    //var test = document.forms['Compose'].elements['test'].value;    
    //var orfo = document.forms['Compose'].elements['orfo'].value; 

    //SpellWin=window.open("http://win.extreme.nnov.ru/cgi-bin/spell?HTMLMessage="+document.forms['Compose'].elements['HTMLMessage'].value, "Orfo",
  // 	"menubar=no,resizable=yes,width=535,height=300,toolbar=no,focus=yes,scrollbars=yes," +
    //    "screenX="+posX+",screenY="+posY+",left="+posX+",top="+posY);  
        
}

function fkeyboard() {
  document.domain="nnov.ru";
	 	window.open("keyboard.html?form=Compose&text=CONTENT&keymode=2&savemode=3","keyb","height=360,width=610,toolbar=no,menubar=no,location=no,scrollbars=no,focus=yes,top=50,left=50");   

  return false;
}

function getImage() {
window.open("http://files.extreme.nnov.ru/upload.aspx?","gi","height=360,width=350,toolbar=no,menubar=no,location=no,scrollbars=no,focus=yes,top=50,left=50");   
return false;
             /*   var url = showModalDialog("getimage.html", " ", "dialogWidth:25em;dialogHeight:25em");
				htmledit.focus();
                if (image && image.length) {
                                var range = htmledit.document.selection.createRange();
                                range.pasteHTML(image);
                                range.select();
                }
				else {
					htmledit.focus();
				}
				return url;*/
}

function uploadFileDialog(obj) {
var path="http://files.extreme.nnov.ru/upload.aspx/uploadArtWork?"+obj;
document.domain="nnov.ru";
window.open(path,"gi","height=360,width=350,toolbar=no,menubar=no,location=no,scrollbars=no,focus=yes,top=50,left=50");
}

function uploadImagePrwDialog(obj) {
var path="http://files.extreme.nnov.ru/upload.aspx/uploadImagePrw?"+obj;
document.domain="nnov.ru";
window.open(path,"gi","height=360,width=350,toolbar=no,menubar=no,location=no,scrollbars=no,focus=yes,top=50,left=50");
}

function uploadImageDialog(obj) {
var path="http://files.extreme.nnov.ru/upload.aspx/uploadImage?"+obj;
document.domain="nnov.ru";
window.open(path,"gi","height=360,width=350,toolbar=no,menubar=no,location=no,scrollbars=no,focus=yes,top=50,left=50");
}

function uploadImageWithPrwDialog(obj) {
var path="http://files.extreme.nnov.ru/upload.aspx/uploadImageWithPrw?"+obj;
document.domain="nnov.ru";
window.open(path,"gi","height=360,width=350,toolbar=no,menubar=no,location=no,scrollbars=no,focus=yes,top=50,left=50");
}