function insertTag(tag, parovy, text, prependText, appendText)
{
	//inicializace promÄ›nĂ˝ch
	var textarea = document.forms["user"].elements["obsah"];

	var startTag = '<' + tag + '>';
	var endTag = '</' + tag + '>';

	if (typeof text == 'undefined') {
		var tagText = getTextareaSelection();
	} else {
		var tagText = text;
	}
	
	if (typeof parovy == 'undefined') {
		var parovy = true;
	}

	if (typeof prependText == 'undefined') {
		var prependText = '';
	}
	
	if (typeof appendText == 'undefined') {
		var appendText = '';
	}
	
	
	//pĹ™epsĂˇnĂ­ vĂ˝chozĂ­ch hodnot pro specifickĂ© tagy
	if (tag == 'url') {
		if (tagText == '') {
			tagText = 'Odkaz';
		}
		var regexp = new RegExp("^((http|https|ftp)\\://)([a-zA-Z0-9\\.\\-]+(\\:[a-zA-Z0-9\\.&%\\$\\-]+)*@)?((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9\\-]+\\.)*[a-zA-Z0-9\\-]+\\.[a-zA-Z]{2,4})(\\:[0-9]+)?(/[^/][a-zA-Z0-9\\.\\,\\?\\'\\\\/\\+&%\\$#\\=~_\\-@]*)*$");
		var regexp2 = new RegExp("^([a-zA-Z0-9\\.\\-]+(\\:[a-zA-Z0-9\\.&%\\$\\-]+)*@)?((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|www\\.([a-zA-Z0-9\\-]+\\.)*[a-zA-Z0-9\\-]+\\.[a-zA-Z]{2,4})(\\:[0-9]+)?(/[^/][a-zA-Z0-9\\.\\,\\?\\'\\\\/\\+&%\\$#\\=~_\\-@]*)*$");
		
		if (regexp.test(tagText)) {
			startTag = '<a href=' + tagText + '" target="_blank">';
		} else if (regexp2.test(tagText)) {
			startTag = '<a href=http://' + tagText + '" target="_blank">';
		} else {
			startTag = '<a href="' + tagText + '" target="_blank">';
			selectionStart = prependText.length + 5;
			selectionEnd = prependText.length + 23;
		}
		endTag = '</a>';
	}
		
	//vytvoĹ™enĂ­ textu pro vloĹľenĂ­
	if (!parovy) {
		endTag = '';
	}
		
	var text2insert = prependText + startTag + tagText + endTag + appendText;
	
	//vloĹľenĂ­:
	textarea.focus();
	
	if (typeof textarea.selectionStart != 'undefined') {
		var start = textarea.selectionStart;
		var end = textarea.selectionEnd;
		
		textarea.value = textarea.value.substr(0, start) + text2insert + textarea.value.substr(end);
		
		//nastaveni kurzoru na spravnĂ© misto / vybrani pozadovaneho textu
		if (typeof selectionStart != 'undefined' && typeof selectionEnd != 'undefined') {
			textarea.selectionStart = start + selectionStart;
			textarea.selectionEnd = start + selectionEnd;
		} else {
			if (tagText == '') {
				//skoceni mezi tagy
				textarea.selectionStart = start + prependText.length + startTag.length;
				textarea.selectionEnd = start + prependText.length + startTag.length;
			} else {
				//skoceni za posledni tag
				textarea.selectionStart = start + text2insert.length;
				textarea.selectionEnd = start + text2insert.length;
			}
		}
	}
	else if (typeof document.selection != 'undefined') {
		var range = document.selection.createRange();
		range.text = text2insert;
		
		//nastaveni kurzoru na spravnĂ© misto / vybrani pozadovaneho textu
		if (typeof selectionStart != 'undefined' && typeof selectionEnd != 'undefined') {
			range.moveStart('character', -text2insert.length + selectionStart);
			range.moveEnd('character', -text2insert.length + selectionEnd);
		} else {
			if (tagText == '') {
				//skoceni mezi tagy
				range.move('character', -(endTag.length + appendText.length));
			} else {
				//skoceni za posledni tag
				//protoze prohlizec v tomto pripade skoci za posledni tag automaticky, neni treba nic posouvat	
			}
		}
		range.select();
	}

	//unset promenych
	delete selectionStart;
	delete selectionEnd;
}
function getTextareaSelection()
{
	var textarea = document.forms["user"].elements["obsah"];
	
	if (typeof textarea.selectionStart != 'undefined') {
		var selection = textarea.value.substring(textarea.selectionStart, textarea.selectionEnd);
	} else if (typeof document.selection != 'undefined') {
		var selection = document.selection.createRange().text;
	} else {
		//alert('Bug!');
	}
	return selection;
}
function insertTagk(tag, parovy, text, prependText, appendText)
{
	//inicializace promÄ›nĂ˝ch
	var textarea = document.forms["user"].elements["obsah_kratky"];

	var startTag = '<' + tag + '>';
	var endTag = '</' + tag + '>';

	if (typeof text == 'undefined') {
		var tagText = getTextareaSelection2();
	} else {
		var tagText = text;
	}
	
	if (typeof parovy == 'undefined') {
		var parovy = true;
	}

	if (typeof prependText == 'undefined') {
		var prependText = '';
	}
	
	if (typeof appendText == 'undefined') {
		var appendText = '';
	}
	
	
	//pĹ™epsĂˇnĂ­ vĂ˝chozĂ­ch hodnot pro specifickĂ© tagy
	if (tag == 'url') {
		if (tagText == '') {
			tagText = 'Odkaz';
		}
		var regexp = new RegExp("^((http|https|ftp)\\://)([a-zA-Z0-9\\.\\-]+(\\:[a-zA-Z0-9\\.&%\\$\\-]+)*@)?((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9\\-]+\\.)*[a-zA-Z0-9\\-]+\\.[a-zA-Z]{2,4})(\\:[0-9]+)?(/[^/][a-zA-Z0-9\\.\\,\\?\\'\\\\/\\+&%\\$#\\=~_\\-@]*)*$");
		var regexp2 = new RegExp("^([a-zA-Z0-9\\.\\-]+(\\:[a-zA-Z0-9\\.&%\\$\\-]+)*@)?((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|www\\.([a-zA-Z0-9\\-]+\\.)*[a-zA-Z0-9\\-]+\\.[a-zA-Z]{2,4})(\\:[0-9]+)?(/[^/][a-zA-Z0-9\\.\\,\\?\\'\\\\/\\+&%\\$#\\=~_\\-@]*)*$");
		
		if (regexp.test(tagText)) {
			startTag = '<a href=' + tagText + '" target="_blank">';
		} else if (regexp2.test(tagText)) {
			startTag = '<a href=http://' + tagText + '" target="_blank">';
		} else {
			startTag = '<a href="' + tagText + '" target="_blank">';
			selectionStart = prependText.length + 5;
			selectionEnd = prependText.length + 23;
		}
		endTag = '</a>';
	}
		
	//vytvoĹ™enĂ­ textu pro vloĹľenĂ­
	if (!parovy) {
		endTag = '';
	}
		
	var text2insert = prependText + startTag + tagText + endTag + appendText;
	
	//vloĹľenĂ­:
	textarea.focus();
	
	if (typeof textarea.selectionStart != 'undefined') {
		var start = textarea.selectionStart;
		var end = textarea.selectionEnd;
		
		textarea.value = textarea.value.substr(0, start) + text2insert + textarea.value.substr(end);
		
		//nastaveni kurzoru na spravnĂ© misto / vybrani pozadovaneho textu
		if (typeof selectionStart != 'undefined' && typeof selectionEnd != 'undefined') {
			textarea.selectionStart = start + selectionStart;
			textarea.selectionEnd = start + selectionEnd;
		} else {
			if (tagText == '') {
				//skoceni mezi tagy
				textarea.selectionStart = start + prependText.length + startTag.length;
				textarea.selectionEnd = start + prependText.length + startTag.length;
			} else {
				//skoceni za posledni tag
				textarea.selectionStart = start + text2insert.length;
				textarea.selectionEnd = start + text2insert.length;
			}
		}
	}
	else if (typeof document.selection != 'undefined') {
		var range = document.selection.createRange();
		range.text = text2insert;
		
		//nastaveni kurzoru na spravnĂ© misto / vybrani pozadovaneho textu
		if (typeof selectionStart != 'undefined' && typeof selectionEnd != 'undefined') {
			range.moveStart('character', -text2insert.length + selectionStart);
			range.moveEnd('character', -text2insert.length + selectionEnd);
		} else {
			if (tagText == '') {
				//skoceni mezi tagy
				range.move('character', -(endTag.length + appendText.length));
			} else {
				//skoceni za posledni tag
				//protoze prohlizec v tomto pripade skoci za posledni tag automaticky, neni treba nic posouvat	
			}
		}
		range.select();
	}

	//unset promenych
	delete selectionStart;
	delete selectionEnd;
}
function getTextareaSelection2()
{
	var textarea = document.forms["user"].elements["obsah_kratky"];
	
	if (typeof textarea.selectionStart != 'undefined') {
		var selection = textarea.value.substring(textarea.selectionStart, textarea.selectionEnd);
	} else if (typeof document.selection != 'undefined') {
		var selection = document.selection.createRange().text;
	} else {
		//alert('Bug!');
	}
	return selection;
}
