var b = 0;
var i = 0;
var u = 0;

function insertIntoSelection(object, text)
{
	if(object.selectionStart || object.selectionStart == 0)
	{
		object.value = object.value.substring(0,object.selectionStart) + text + object.value.substring(object.selectionEnd);
		object.selectionStart = object.selectionStart + text.length;
		object.selectionEnd = object.selectionStart;
		object.focus();
	}
	else
		object.value = object.value + text;
}

function bold(object)
{
	if(object.selectionStart == object.selectionEnd)
	{
		if(b == 0)
		{
			insertIntoSelection(object, '<b>');
			b = 1;
		}
		else
		{
			insertIntoSelection(object, '</b>');
			b = 0;
		}
	}
	else if(object.selectionStart != object.selectionEnd)
	{
		object.value = object.value.substring(0,object.selectionStart) + '<b>' + object.value.substring(object.selectionStart,object.selectionEnd) + '</b>' + object.value.substring(object.selectionEnd);
	}
	else
		object.value += '<b></b>';
}

function italic(object)
{
	if(object.selectionStart == object.selectionEnd)
	{
		if(i == 0)
		{
			insertIntoSelection(object, '<i>');
			i = 1;
		}
		else
		{
			insertIntoSelection(object, '</i>');
			i = 0;
		}
	}
	else if(object.selectionStart != object.selectionEnd)
	{
		object.value = object.value.substring(0,object.selectionStart) + '<i>' + object.value.substring(object.selectionStart,object.selectionEnd) + '</i>' + object.value.substring(object.selectionEnd);
	}
	else
		object.value += '<i></i>';
}

function underline(object)
{
	if(object.selectionStart == object.selectionEnd)
	{
		if(u == 0)
		{
			insertIntoSelection(object, '<u>');
			u = 1;
		}
		else
		{
			insertIntoSelection(object, '</u>');
			u = 0;
		}
	}
	else if(object.selectionStart != object.selectionEnd)
	{
		object.value = object.value.substring(0,object.selectionStart) + '<u>' + object.value.substring(object.selectionStart,object.selectionEnd) + '</u>' + object.value.substring(object.selectionEnd);
	}
	else
		object.value += '<u></u>';
}

function quote(object)
{
	if(object.selectionStart == object.selectionEnd)
	{
		if(u == 0)
		{
			insertIntoSelection(object, '<blockquote>');
			u = 1;
		}
		else
		{
			insertIntoSelection(object, '</blockquote>');
			u = 0;
		}
	}
	else if(object.selectionStart != object.selectionEnd)
	{
		object.value = object.value.substring(0,object.selectionStart) + '<blockquote>' + object.value.substring(object.selectionStart,object.selectionEnd) + '</blockquote>' + object.value.substring(object.selectionEnd);
	}
	else
		object.value += '<blockquote</blockquote>';
}

function img(object)
{
	insertIntoSelection(object, '<img src="' + prompt('Adres obrazka:','http://') + '" />');
}

function url(object)
{
	a = prompt('Adres:','http://');
	c = prompt('Nazwa linku:','');

	if(c)
		insertIntoSelection(object, '<a href="' + a + '">' + c + '</a>');
	else
		insertIntoSelection(object, '<a href="' + a + '">' + a + '</a>');
}