// Copyright (c) 2005 by Yanis Prasol (Gero)
var isFirefox = navigator.userAgent.indexOf("Firefox") > -1;

function getSelText(){
	var str;
	if (window.getSelection){
		str = window.getSelection();
		if (str == ''){
				var edit = document.F1.text;
				str = edit.value;
				if (str != '')
					str = str.substring(edit.selectionStart, edit.selectionEnd);
		}
		return str;
	}
	if (document.getSelection){
		if (document.selection){
			str = document.selection.createRange().text;
			if (!str) str = document.getSelection();
		} 
		else
			str = document.getSelection();
		return str;
	}
	if (document.selection)
		return document.selection.createRange().text;
	else
		return null;
}
function setSelText(text){
	var area = document.F1.text;
	if (document.selection){
		document.selection.createRange().text = text;
	}
	else if (window.getSelection){
		area.value = area.value.substring(0, area.selectionStart) + text + area.value.substring(area.selectionEnd, area.value.length);
	}
	else
		area.value += text;
}
function createCite(str){
	str = '' + str; // to fix Firefox bug
	var text = '> ';
	var wrapStr = ' ,.?!-:;';
	var j = 0, symb;
	for(var i = 0; i < str.length; i++){
		symb = str.charAt(i);
		text += symb;
		j++;
		if (symb == '\n'){ 
			text += '> ';
			j = 0;
		}
		if ((j > 55 && j < str.length && wrapStr.indexOf(symb) > -1)){
			text += '\n> ';
			j = 0;
		}
	}
	return text;
}

function formatTag(tag){
	var str = getSelText();
	if (document.selection)
		document.F1.text.focus();
	setSelText('<' + tag + '>' + str + '</' + tag + '>');
	if (!document.selection)
		document.F1.text.focus();
	return false;
}

function formatCite(){
	var str = getSelText();
	if (str == '')
		alert("Не выделен текст!\nДля вставки цитаты сначала выделите на странице нужный текст, а затем нажмите эту кнопку.");
	else {
		var cite = createCite(str);
		if (document.selection)
			document.F1.text.focus();
		setSelText('<I>\n' + cite + '\n</I>\n');
		if (!document.selection)
			document.F1.text.focus();
	}
	return false;
}
function citeMouseDownProc(){
	return isFirefox?formatCite():null;
}
function citeClickProc(){
	return isFirefox?null:formatCite();
}

