// execCommand
function ec(sCommand, vValue)
	{
	if (document.composeform.switchMode.checked)
		{
		alert("Чтобы продолжить работу с помощью панели редактирования снимите флажок \"Просмотр HTML кода\"");
		Composition.focus();
		}
	else
		{
		if (vValue == null)
			Composition.document.execCommand(sCommand);
		else
			Composition.document.execCommand(sCommand, false, vValue);
		Composition.focus();
		}
	}

// Change image onclick
function change_img_src(name)
	{
	document.images[name].src = "/pic/" + name + "1.gif"
	}

// Switch between text and html mode.
function setMode(newMode)
	{
	var content;
	if (newMode)
		{
		content = Composition.document.body.innerHTML;
		Composition.document.body.innerText = content;
		}
	else
		{
		content = Composition.document.body.innerText;
    	Composition.document.body.innerHTML = content;
		}
  	Composition.focus();
	}

// Find and return an element.
function getEl(sTag,start)
	{
	while ((start != null) && (start.tagName != sTag))
		start = start.parentElement;
	return start;
	}

// Create hyperlink
function createLink()
	{
	if (document.composeform.switchMode.checked)
		{
		alert("Чтобы продолжить работу с помощью панели редактирования снимите флажок \"Просмотр HTML кода\"");
		Composition.focus();
		}
	else
		{  
		var isA = getEl("A", Composition.document.selection.createRange().parentElement());
		var url = prompt("Введите адрес:", isA ? isA.href : "http:\/\/");
		var link_text = prompt("Введите текст для обозначения ссылки:", "");
	  
		if ((url != null) && (url != "http://"))
			{
			if (Composition.document.selection.type == "None")
				{
				Composition.focus();
				var selectedItem = Composition.document.selection.createRange();
				selectedItem.pasteHTML("<a href=\"" + url + "\">" + link_text + "</a> ");
				selectedItem.select();
				}
			else
				ec("CreateLink", url);
			}
		else Composition.focus();
		}
	}

// Create tab
function createTab()
	{
	if (document.composeform.switchMode.checked)
		{
		alert("Чтобы продолжить работу с помощью панели редактирования снимите флажок \"Просмотр HTML кода\"");
		Composition.focus();
		}
	else
		{
		var isA = getEl("A", Composition.document.selection.createRange().parentElement());
		Composition.focus();
		var selectedItem = Composition.document.selection.createRange();
		selectedItem.pasteHTML("&nbsp;&nbsp;&nbsp;&nbsp;");
		selectedItem.select();
		}
	}

// Resize editor window
function TableResize(new_width)
	{
	tresize.width = (new_width / 10) * 720;
	}

// check input & iframe values & submit form
function Post()
	{
   if (document.composeform.title.value == "")
		{
		alert("Ошибка: не заполнено поле \"Заголовок страницы\"");
		return;
		}
		
	if (document.composeform.switchMode.checked)
		document.composeform.content.value = Composition.document.body.innerText
	else
		document.composeform.content.value = Composition.document.body.innerHTML
	
	document.composeform.submit()
	}

// Uploading & handling uploaded images
function handle_images()
	{
	if (document.composeform.switchMode.checked)
		{
		alert("Чтобы продолжить работу с помощью панели редактирования снимите флажок \"Просмотр HTML кода\"");
		Composition.focus();
		}
	else
		window.open("handle_images.php?action=showuploader","","height=600,width=800,top=40,left=40,directories=no,location=no,menubar=no,resizeable=yes,scrollbars=yes,status=yes,toolbar=no");
	}

// Show images in editor
function show_image(img_src)
	{
	document.images['uploaded_image'].src = "/pic/uploaded/" + img_src;
	}

// Insert image
function insert_image()
	{
	if (document.myform.hspace.value / 2)
		var hspace = document.myform.hspace.value;
	else
		var hspace = "0";
		
	if (document.myform.vspace.value / 2)
		var vspace = document.myform.vspace.value;
	else
		var vspace = "0";
	
	var align = "";
	if (document.myform.align.value == "left")
		align = "left";
	else if (document.myform.align.value == "right")
		align = "left";
	else
		align = "";
	
	var alt = document.myform.alt.value;

	var filename = document.myform.img_list.options[document.myform.img_list.options.selectedIndex].value;
		
	if (window.opener.document.composeform.switchMode.checked)
		{
		alert("Чтобы вставить рисунок снимите флажок \"Просмотр HTML кода\"");
//		window.opener.Composition.focus();
		}
	else if (document.myform.img_list.options[document.myform.img_list.options.selectedIndex].value == "choose_image.gif")
		{
		alert("Выберите изображение в колонке слева");
//		window.opener.Composition.focus();
		}
	else
		{		
		window.opener.Composition.focus();
		var selectedItem = window.opener.Composition.document.selection.createRange();
		selectedItem.pasteHTML('<img alt="' + alt + '" align="' + align + '" hspace="' + hspace + '" vspace="' + vspace + '" src="http://www.zveroforma.ru/pic/uploaded/' + filename + '">');
//		selectedItem.select();
		window.close();
		}
	}
