function RefreshBody() {
  document.Compose.Body.value = editor.GetHTML();
}
function CheckFormTo(form) {
  RefreshBody()
}

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 = "/guest_img/"+image;
	this.Instantiate = ButtonInstantiate;
	this.Instantiate();
}

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 = true;
	this.GetHTML = EditorGetHTML;
	this.SetHTML = EditorSetHTML;
}

function  EditorGetHTML()	{
	return htmledit.document.body.innerHTML;
}

function  EditorSetHTML(html)	{
	htmledit.document.body.innerHTML = html;
}

function EditorOnBold()	{
	EditorFormat("bold");
}
function EditorOnItalic()	{
	EditorFormat("italic");
//	EditorFormat("","removeFormat");
}
function EditorOnUnderline() {
	EditorFormat("underline");
}
function EditorOnNormal()	{
	EditorFormat("","removeFormat");
}
function AddSmile()	{
	var smile = showModalDialog("/rus/AddSmile.php", " ", "font-family:Verdana;font-size:12;dialogWidth:26em;dialogHeight:18em");//16 12
	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("/rus/AddHand.php", " ", "font-family:Verdana;font-size:8;dialogWidth:20em;dialogHeight:8em");// 15 4
	htmledit.focus();
  if (smile && smile.length) {
    var range = htmledit.document.selection.createRange();
    range.pasteHTML('<img src="' + smile + '">');
    range.select();
  } else {
      htmledit.focus();
	  }
}
function AddImage()	{
	var image = showModalDialog("/rus/AddImage.html", " ", "font-family:Verdana;font-size:12;dialogWidth:26em;dialogHeight:18em");//16 12
	htmledit.focus();
	if (image && image.length) {
		var range = htmledit.document.selection.createRange();
    range.pasteHTML('<img src="' + image + '">');
    range.select();
	} else {
   		htmledit.focus();
	  }
}
function EditorFormat(what, opt) {
	htmledit.focus();
//  if (htmledit.document.selection.createRange().text=="") return;
	if (opt == "removeFormat") {
		what = opt;
		opt = null;
	}
	if (opt == null) {
		htmledit.document.execCommand(what);
//		htmledit.document.selection.empty();
//	  htmledit.document.execCommand(what);
// 		htmledit.focus();
	}	else {
		  htmledit.document.execCommand(what, "", opt);
 		}
}

function EditorGetElement(tagName, start)	{
	while (start && start.tagName != tagName) {
		start = start.parentElement;
	}
	return start;
}

