// Textarea edit functions

function get_selection(input)
{
	var textarea = document.getElementById(input);
	var selection = '';
	
	if (document.selection) // IE
	{
		selection = document.selection.createRange().text;
		
		return selection;
	}
	else if(textarea.selectionStart || textarea.selectionStart == '0') // Mozilla
	{
		var startPos = textarea.selectionStart;
		var endPos = textarea.selectionEnd;
		
		var selection = (textarea.value).substring(startPos, endPos);
		
		return selection;
	}
}


function insert_text_overwrite(input, text) // Replaces the selected text with the inserted text
{
	var textarea = document.getElementById(input);
	
	if (document.selection) // IE, Opera
	{
		var t1 = document.selection.createRange();
		if (t1.text) // If we have something selected
		{
			textarea.focus();
			var textrange = textarea.createTextRange(); // Textrange contains the whole textarea.value
			textrange = t1;
			t1.text = text; // Set the selection to the inserted text
			document.selection.createRange().select(); // Select the text again
			textarea.focus();
		}
		else
		{
			textarea.focus();
		}
	}
	else if (textarea.selectionStart || textarea.selectionStart == '0') // Mozilla
	{
		if (textarea.selectionStart != textarea.selectionEnd)
		{
			var startPos = textarea.selectionStart;
			var endPos = textarea.selectionEnd;
			var subst;
			
			textarea.focus();
			
			subst = text;
				
			textarea.value = textarea.value.substring(0, startPos) + subst + textarea.value.substring(endPos, textarea.value.length);
			textarea.focus();
			
			textarea.selectionStart = startPos;
			textarea.selectionEnd = startPos + text.length;
		}
		else
		{
			textarea.focus();
		}
	}
}


function insert_text(input, text) // Inserts the selected text in behind the inserted text
{
	var textarea = document.getElementById(input);
	
	if (document.selection) // IE, Opera
	{
		var newtext = document.selection.createRange().text;
		
		var left_space = '';
		var right_space = '';
		if (newtext.charAt(0) == " ") { left_space = ' '; }
		if (newtext.charAt(newtext.length - 1) == " ") { right_space = ' '; }
		
		newtext = newtext + text;
		
		textarea.focus();
		document.selection.createRange().text = left_space + trimAll(newtext) + right_space;
		document.selection.createRange().select();
		textarea.focus();
	}
	else if (textarea.selectionStart || textarea.selectionStart == '0') // Mozilla
	{
		var startPos = textarea.selectionEnd;
		var endPos = textarea.selectionEnd;
		var subst;
		
		var newtext = textarea.value.substring(textarea.selectionStart, textarea.selectionEnd);
		
		var left_space = '';
		var right_space = '';
		if (newtext.charAt(0) == " ") { left_space = ' '; }
		if (newtext.charAt(newtext.length - 1) == " ") { right_space = ' '; }
		
		textarea.focus();
		
		subst = trimAll(text);
			
		textarea.value = textarea.value.substring(0, startPos) + left_space + subst + right_space + textarea.value.substring(endPos, textarea.value.length);
		textarea.focus();
		
		textarea.selectionStart = startPos + subst.length;	//Reposition the cursor in FF
		textarea.selectionEnd = startPos + subst.length;
	}
}


// Misc helpers

function trimAll(str)
{
	return str.replace(/(^\s*)|(\s*$)/g, '');
}


// Edit text

function editText(eT, input)
{
	if (eT != '') {
		var eTe = '';
		if (eT.substr(0,5) == 'align') {
			eTe = 'align';
		} else {
			eTe = eT;
		}
		
		var text = get_selection(input);
		
		var str = '[' + eT + ']' + trimAll(text) + '[/' + eTe + ']';
		
		
		var left_space = '';
		var right_space = '';
		if (text.charAt(0) == " ") { left_space = ' '; }
		if (text.charAt(text.length - 1) == " ") { right_space = ' '; }
		
		insert_text_overwrite(input, left_space + str + right_space);
	}
}


function doAddSmiley(input, smiley)
{
	insert_text(input, smiley);
}


function insertLink(input)
{
	var textarea = document.getElementById(input);

	// our link
	var url = prompt('Ange adress att länka till', 'http://');
  url = trimAll(url);

	if (url.length <= 0)
		return;

	var text = get_selection(input);
	if (!text)
	{
		title = prompt('Ange länktext', '');
	  title = trimAll(title);

		if (title.length <= 0)
			return;
		
		var link = '[url=' + url + ']' + title + '[/url]';

		// update the text field
		insert_text(input, link);
		return;
	}
	
	var left_space = '';
	var right_space = '';
	if (text.charAt(0) == " ") { left_space = ' '; }
	if (text.charAt(text.length - 1) == " ") { right_space = ' '; }
	
	var link = left_space + '[url=' + url + ']' + trimAll(text) + '[/url]' + right_space;

	// update the text field
	insert_text_overwrite(input, link);
}


function insertAlign(align)
{
	var textarea = document.getElementById('message');
  textarea.focus();

	// our link
	var text = get_selection('message');

	if (text.length <= 0)
		return;

	var str = '[align=' + align + ']' + text + '[/align]';

	// update the text field
  insert_text_overwrite('message', str);
}


function insertImage(input)
{
	var textarea = document.getElementById(input);

	// our link
	var url = prompt('Ange adress att hämta bild ifrån', 'http://');
  url = trimAll(url);

	if (url.length <= 0)
		return;

	var link = '[img]' + url + '[/img]';

	// update the text field
  insert_text(input, link);
}
