/**
* bbCode control by subBlue design [ www.subBlue.com ]
* Includes unixsafe colour palette selector by SHS`
*/

// Startup variables
var imageTag = false;
var theSelection = false;

// Check for Browser & Platform for PC & IE specific bits
// More details from: http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
var clientPC = navigator.userAgent.toLowerCase(); // Get client info
var clientVer = parseInt(navigator.appVersion); // Get browser version

var is_ie = ((clientPC.indexOf('msie') != -1) && (clientPC.indexOf('opera') == -1));
var is_win = ((clientPC.indexOf('win') != -1) || (clientPC.indexOf('16bit') != -1));

var baseHeight;
onload_functions.push('initInsertions()');

/**
* Shows the help messages in the helpline window
*/
function helpline(help)
{
	document.forms[form_name].helpbox.value = help_line[help];
}

/**
* Fix a bug involving the TextRange object. From
* http://www.frostjedi.com/terra/scripts/demo/caretBug.html
*/
function initInsertions()
{
	var doc;

	if (document.forms[form_name])
	{
		doc = document;
	}
	else
	{
		doc = opener.document;
	}

	var textarea = doc.forms[form_name].elements[text_name];

	if (is_ie && typeof(baseHeight) != 'number')
	{
		textarea.focus();
		baseHeight = doc.selection.createRange().duplicate().boundingHeight;

		if (!document.forms[form_name])
		{
			document.body.focus();
		}
	}
}

/**
* bbstyle
*/
function bbstyle(bbnumber)
{
	if (bbnumber != -1)
	{
		bbfontstyle(bbtags[bbnumber], bbtags[bbnumber+1]);
	}
	else
	{
		insert_text('[*]');
		document.forms[form_name].elements[text_name].focus();
	}
}

/**
* Apply bbcodes
*/
function bbfontstyle(bbopen, bbclose)
{
	theSelection = false;

	var textarea = document.forms[form_name].elements[text_name];

	textarea.focus();

	if ((clientVer >= 4) && is_ie && is_win)
	{
		// Get text selection
		theSelection = document.selection.createRange().text;

		if (theSelection)
		{
			// Add tags around selection
			document.selection.createRange().text = bbopen + theSelection + bbclose;
			document.forms[form_name].elements[text_name].focus();
			theSelection = '';
			return;
		}
	}
	else if (document.forms[form_name].elements[text_name].selectionEnd && (document.forms[form_name].elements[text_name].selectionEnd - document.forms[form_name].elements[text_name].selectionStart > 0))
	{
		mozWrap(document.forms[form_name].elements[text_name], bbopen, bbclose);
		document.forms[form_name].elements[text_name].focus();
		theSelection = '';
		return;
	}

	//The new position for the cursor after adding the bbcode
	var caret_pos = getCaretPosition(textarea).start;
	var new_pos = caret_pos + bbopen.length;

	// Open tag


// >>>BEGIN>>>SECTION>>>1

// >>>IMG_L>>>1
   if (bbopen == '[img_l]')
	{
	    var Error = '';
		var IMGL = prompt('Image URL' , 'http://');
		if (!IMGL) {
        Error = "You have no picture URL";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		if (IMGL) {
			insert_text(bbopen + IMGL + bbclose);
		}
		else {
	insert_text(bbopen + bbclose);
		}
	}

// >>>IMG_R>>>1
   else if (bbopen == '[img_r]')
	{
	    var Error = '';
		var IMGR = prompt('Image URL' , 'http://');
		if (!IMGR) {
        Error = "You have no picture URL";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		if (IMGR) {
			insert_text(bbopen + IMGR + bbclose);
		}
		else {
			insert_text(bbopen + bbclose);
		}
	}

// >>>FLV>>>1
   else if (bbopen == '[flv]')
	{
	    var Error = '';
		var FLV = prompt('FLV Link eingeben' , 'http://');
		if (!FLV) {
        Error = "You have not entered FLV Link";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		if (FLV) {
			insert_text(bbopen + FLV + bbclose);
		}
		else {
			insert_text(bbopen + bbclose);
		}
	}

// >>>STREAM>>>1
   else if (bbopen == '[stream]')
	{
	    var Error = '';
		var STREAM = prompt('MP3 Link eingeben' , 'http://');
		if (!STREAM) {
        Error = "You have no MP3 link entered";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		if (STREAM) {
			insert_text(bbopen + STREAM + bbclose);
		}
		else {
			insert_text(bbopen + bbclose);
		}
	}

// >>>FLASH_I>>>1
   else if (bbopen == '[flash_i]')
	{
	    var Error = '';
		var FLASHI = prompt('Flash Link eingeben' , 'http://');
		if (!FLASHI) {
        Error = "	You have no Flash link entered";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		if (FLASHI) {
			insert_text(bbopen + FLASHI + bbclose);
		}
		else {
			insert_text(bbopen + bbclose);
		}
	}

// >>>MOV>>>1
   else if (bbopen == '[mov]')
	{
	    var Error = '';
		var MOV = prompt('Quicktime Link eingeben' , 'http://');
		if (!MOV) {
        Error = "You have not entered Quicktime link";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		if (MOV) {
			insert_text(bbopen + MOV + bbclose);
		}
		else {
			insert_text(bbopen + bbclose);
		}
	}

// >>>RAM>>>1
   else if (bbopen == '[rm]')
	{
	    var Error = '';
		var RM = prompt('RealTime Link eingeben' , 'http://');
		if (!RM) {
        Error = "You have not entered RealTimeProof Link";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		if (RM) {
			insert_text(bbopen + RM + bbclose);
		}
		else {
			insert_text(bbopen + bbclose);
		}
	}

// >>>WMV>>>1
   else if (bbopen == '[wmv]')
	{
	    var Error = '';
		var WMV = prompt('Windows\251 Media Link eingeben' , 'http://');
		if (!WMV) {
        Error = "You have no Windows \ Media Link entered 251eingegeben";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		if (WMV) {
			insert_text(bbopen + WMV + bbclose);
		}
		else {
			insert_text(bbopen + bbclose);
		}
	}

// >>>DIVX>>>1
   else if (bbopen == '[divx]')
	{
	    var Error = '';
		var DIVX = prompt('DIVX Link eingeben' , 'http://');
		if (!DIVX) {
        Error = "You have no link entered DIVX";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		if (DIVX) {
			insert_text(bbopen + DIVX + bbclose);
		}
		else {
			insert_text(bbopen + bbclose);
		}
	}
	

	
	
	
	
	
		
	// >>>SPOIL_TAG=>>>1
	/*
   else if (bbopen == '[spoil_tag=]')
	{
	    var Error = '';
		var SPTAG = prompt('Spoil-Tag Text eingegeben' , '');
		if (!SPTAG) {
        Error = "You have no Spoil tag text";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		if (SPTAG) {
		
		var RAND = Math.floor(Math.random() * 100000 + 50000);

		// var RAND = Math.random();
		insert_text('[spoil_tag=' + RAND + ']' + SPTAG + bbclose)
		}
		else {
		insert_text(bbopen + bbclose);
		}
	}
	*/
	
	else if (bbopen == '[spoil_tag=]')
	{
	var RAND = Math.floor(Math.random() * 1000000 + 500000);
	insert_text('[spoil_tag=' + RAND + ']' + '[/spoil_tag]')
	}
	
	
	// >>>IMG_LYTEBOX=>>>1

    else if (bbopen == '[img_lytebox=]')
	{
		var Error = '';
		var IMGLYTEBOXTITLE = prompt('Image title' , '');
		if (!IMGLYTEBOXTITLE) {
        Error = "You have no image titles";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		if (IMGLYTEBOXTITLE && IMGLYTEBOXTITLE != '') {
		var IMGLYTEBOX = prompt('Image URL' , 'http://');
	    if (!IMGLYTEBOX) {
        Error = "You have no picture URL";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
			if (IMGLYTEBOX) {
				insert_text("[img_lytebox=" + IMGLYTEBOXTITLE + "]" + IMGLYTEBOX + bbclose);
			}
		}
	}	

	
	
	
	// >>>COPYRIGHT>>>1
   else if (bbopen == '[copyright=]')
	{
	    var Error = '';
		var COPYRIGHTOWNER = prompt('Behalf of the copyright holder to enter' , '');
		if (!COPYRIGHTOWNER) {
        Error = "You have entered no copyright holder";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		var COPYRIGHTLEFT = prompt('Position of links: % as a number (eg 25)\r\Hinweis: % inclusive of justice should be no more than 50%' , '');
		if (!COPYRIGHTLEFT) {
        Error = "You have no domain name";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		var COPYRIGHTRIGHT = prompt('	Position of justice:% as a number (eg 25)\r\nHinweis: 	Inclusive% of links should be no more than 50%.' , '');
		if (!COPYRIGHTRIGHT) {
        Error = "You have no top-level domain";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
        var COPYRIGHTDOMAIN = prompt('Copyright holder URL' , '');
	    if (!COPYRIGHTDOMAIN) {
        Error = "You do not have copyright holder URL";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		if (COPYRIGHTOWNER && COPYRIGHTLEFT && COPYRIGHTRIGHT && COPYRIGHTDOMAIN) {
			insert_text('[copyright=' + COPYRIGHTOWNER + '|' + COPYRIGHTLEFT + '|' + COPYRIGHTRIGHT + ']' + COPYRIGHTDOMAIN + bbclose);
		}
		else {
			insert_text(bbopen + bbclose);
		}
	}
	

	
	// >>>COPYRIGHT>>>1
   else if (bbopen == '[websnap=]')
	{
	    var Error = '';
		var WEBSNAPURL = prompt('Websnap URL' , 'http://');
		if (!WEBSNAPURL) {
        Error = "You have no Websnap URL";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		var WEBSNAPIMG = prompt('Websnap image URL' , 'http://');
		if (!WEBSNAPIMG) {
        Error = "You have no Websnap Image URL";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		var WEBSNAPTITLE = prompt('Description or title' , '');
		if (!WEBSNAPTITLE) {
        Error = "You have no description or title entered";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		if (WEBSNAPURL && WEBSNAPIMG && WEBSNAPTITLE) {
			insert_text('[websnap=' + WEBSNAPURL + ' pic=' + WEBSNAPIMG + ']' + WEBSNAPTITLE + bbclose);
		}
		else {
			insert_text(bbopen + bbclose);
		}
	}	
	
	
	

	
	
	
	
	
	
	
	
	
	

// >>>MAIL>>>1
   else if (bbopen == '[mail=]')
	{
	    var Error = '';
		var TESTMAILNAME = prompt('E-mail name\r\nOnly that which is before the @' , '');
		if (!TESTMAILNAME) {
        Error = "You have no e-mail name";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		var TESTMAILDOMAIN = prompt('Domain name\r\nSo everything after the @ and before. Xx' , '');
		if (!TESTMAILDOMAIN) {
        Error = "You have no domain name";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		var TESTMAILTLD = prompt('Top-Level Domain (TLD) eingeben\r\nWithout the point before: de, com, net, etc.' , '');
		if (!TESTMAILTLD) {
        Error = "You have no top-level domain";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
        var TESTMAILDESCRIPT = prompt('Mail enter name\r\ne.g.: Martins E-mail Address\r\nIn no case the e-mail address ;)' , '');
	    if (!TESTMAILDESCRIPT) {
        Error = "You have no mail name entered";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		if (TESTMAILNAME && TESTMAILDOMAIN && TESTMAILTLD && TESTMAILDESCRIPT) {
			insert_text('[mail=' + TESTMAILNAME + '|' + TESTMAILTLD + '|' + TESTMAILDOMAIN + ']' + TESTMAILDESCRIPT + bbclose);
		}
		else {
			insert_text(bbopen + bbclose);
		}
	}

// >>>URL>>>1
else if (bbopen == '[url]')
	{
	    var Error = '';
		var URL = prompt('Enter the URL' , 'http://');
		if (!URL) {
        Error = "You have no URL";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		var URLTITEL = prompt('Description or title URL' , '');
		if (!URLTITEL) {
        Error = "You have no description or title URL";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		if (URL && URLTITEL) {
			insert_text('[url=' + URL + ']' + URLTITEL + bbclose);
		}
		else {
			insert_text(bbopen + bbclose);
		}
	}

// >>>QUOTE>>>1
else if (bbopen == '[quote]')
	{
	    var Error = '';
		var QUOTENAME = prompt('Enter the nick name of the quoted' , '');
		if (!QUOTENAME) {
        Error = "You have no nick name";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		if (QUOTENAME) {
			insert_text('[quote=' + '"' + QUOTENAME + '"' + ']' + bbclose);
		}
		else {
			insert_text(bbopen + bbclose);
		}
	}

// >>>YOUTUBE>>>1
   else if (bbopen == '[youtube]')
	{
	    var Error = '';
		var YOUTUBE = prompt('Youtube link to enter\r\nhttp://youtube.com/watch?v=xxxxxxxxxxx' , 'http://');
		if (!YOUTUBE) {
        Error = "You have not entered Youtube link";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		if (YOUTUBE) {
			insert_text(bbopen + YOUTUBE + bbclose);
		}
		else {
			insert_text(bbopen + bbclose);
		}
	}

// >>>GVIDEO>>>1
   else if (bbopen == '[gvideo]')
	{
	    var Error = '';
		var GVIDEO = prompt('Google Video Link\r\nhttp://video.google.com/videoplay?docid=123456789' , 'http://');
		if (!GVIDEO) {
        Error = "You have not entered the Google Video Link";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		if (GVIDEO) {
			insert_text(bbopen + GVIDEO + bbclose);
		}
		else {
			insert_text(bbopen + bbclose);
		}
	}

// >>>MYSPACE>>>1
   else if (bbopen == '[myspace]')
	{
	    var Error = '';
		var MYSPACE = prompt('Enter Myspace Link\r\nhttp://vids.myspace.com/index.cfm?fuseaction=vids.individual&videoid=12345' , 'http://');
		if (!MYSPACE) {
        Error = "You have not entered Myspace Link";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		if (MYSPACE) {
			insert_text(bbopen + MYSPACE + bbclose);
		}
		else {
			insert_text(bbopen + bbclose);
		}
	}

// >>>MYVIDEO>>>1
   else if (bbopen == '[myvideo]')
	{
	    var Error = '';
		var MYVIDEO = prompt('MyVideo link to enter\r\nhttp://www.myvideo.de/watch/1234567' , 'http://');
		if (!MYVIDEO) {
        Error = "You have no link entered MyVideo";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		if (MYVIDEO) {
			insert_text(bbopen + MYVIDEO + bbclose);
		}
		else {
			insert_text(bbopen + bbclose);
		}
	}

// >>>CLIPFISH>>>1
   else if (bbopen == '[clipfish]')
	{
	    var Error = '';
		var CLIPFISH = prompt('Clipfish link to enter\r\nhttp://www.clipfish.de/player.php?videoid=xxxxxxxxxxxx' , 'http://');
		if (!CLIPFISH) {
        Error = "You have not entered Clipfish Link";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		if (CLIPFISH) {
			insert_text(bbopen + CLIPFISH + bbclose);
		}
		else {
			insert_text(bbopen + bbclose);
		}
	}

// >>>AMAZON>>>1
   else if (bbopen == '[amazon]')
	{
	    var Error = '';
		var AMAZON = prompt('ISBN-number\r\nexample: 3897214210' , '');
		if (!AMAZON) {
        Error = "You have no ISBN number entered";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		if (AMAZON) {
			insert_text(bbopen + AMAZON + bbclose);
		}
		else {
			insert_text(bbopen + bbclose);
		}
	}

// >>>WIKI>>>1

    else if (bbopen == '[wiki=]')
	{
		var Error = '';
		var WIKIWORD = prompt('Wiki term' , '');
		if (!WIKIWORD) {
        Error = "You have no concept entered Wiki";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		if (WIKIWORD && WIKIWORD != '') {
		var WIKITITLE = prompt('Wiki title' , '');
	    if (!WIKITITLE) {
        Error = "You have not entered Wiki Title";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
			if (WIKITITLE) {
				insert_text("[wiki=" + WIKIWORD + "]" + WIKITITLE + bbclose);
			}
		}
	}

// >>>GMAP2>>>1

    else if (bbopen == '[gmap2]')
	{
		var Error = '';
		var LOCATION2 = prompt('Enter Location\r\nexample: Berlin, Unter den Linden' , '');
		if (!LOCATION2) {
        Error = "You have no location entered";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
			if (LOCATION2) {
				insert_text("[gmap2]" + LOCATION2 + bbclose);
			}
	}

// >>>GMAP>>>1	
else if (bbopen == '[gmap=]')
	{
		var Error = '';
		var LOCATION = prompt('Location or description\r\n' , '');
		if (!LOCATION) {
        Error = "You have no location entered";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		var UTF8LINK = prompt('	Google Map Link UTF8 input\r\nSo only the part of UTF8 to z = xx:\r\nUTF8&ll=50.796535,7.206337&spn=0.000815,0.001824&z=19' , '');
	    if (!UTF8LINK) {
        Error = "You have no link to enter UTF8";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		if (LOCATION && UTF8LINK) {
			insert_text("[gmap=" + LOCATION + "]" + "http://maps.google.de/maps?t=k&ie=" + UTF8LINK  + bbclose);
		}
		else {
			insert_text(bbopen + bbclose);
		}
	}


// >>>IMG>>>1
   else if (bbopen == '[img]')
	{
	    var Error = '';
		var IMG = prompt('Enter the URL of the image' , 'http://');
		if (!IMG) {
        Error = "You have no picture URL";
		}
		if (Error) {
        alert("Error: "+Error);
        return;
        }
		if (IMG) {
			insert_text(bbopen + IMG + bbclose);
		}
		else {
			insert_text(bbopen + bbclose);
		}
	}

// >>>ALL_OTHER_ONE_CLICK_TAGS>>>
	else {
	insert_text(bbopen + bbclose);
	}
// >>>END>>>SECTION>>>1


	// Center the cursor when we don't have a selection
	// Gecko and proper browsers
	if (!isNaN(textarea.selectionStart))
	{
		textarea.selectionStart = new_pos;
		textarea.selectionEnd = new_pos;
	}
	// IE
	else if (document.selection)
	{
		var range = textarea.createTextRange();
		range.move("character", new_pos);
		range.select();
		storeCaret(textarea);
	}

	textarea.focus();
	return;
}

/**
* Insert text at position
*/
function insert_text(text, spaces, popup)
{
	var textarea;

	if (!popup)
	{
		textarea = document.forms[form_name].elements[text_name];
	}
	else
	{
		textarea = opener.document.forms[form_name].elements[text_name];
	}
	if (spaces)
	{
		text = ' ' + text + ' ';
	}

	if (!isNaN(textarea.selectionStart))
	{
		var sel_start = textarea.selectionStart;
		var sel_end = textarea.selectionEnd;

		mozWrap(textarea, text, '')
		textarea.selectionStart = sel_start + text.length;
		textarea.selectionEnd = sel_end + text.length;
	}
	else if (textarea.createTextRange && textarea.caretPos)
	{
		if (baseHeight != textarea.caretPos.boundingHeight)
		{
			textarea.focus();
			storeCaret(textarea);
		}

		var caret_pos = textarea.caretPos;
		caret_pos.text = caret_pos.text.charAt(caret_pos.text.length - 1) == ' ' ? caret_pos.text + text + ' ' : caret_pos.text + text;
	}
	else
	{
		textarea.value = textarea.value + text;
	}
	if (!popup)
	{
		textarea.focus();
	}
}

/**
* Add inline attachment at position
*/
function attach_inline(index, filename)
{
	insert_text('[attachment=' + index + ']' + filename + '[/attachment]');
	document.forms[form_name].elements[text_name].focus();
}

/**
* Add quote text to message
*/
function addquote(post_id, username)
{
	var message_name = 'message_' + post_id;
	var theSelection = '';
	var divarea = false;

	if (document.all)
	{
		divarea = document.all[message_name];
	}
	else
	{
		divarea = document.getElementById(message_name);
	}

	// Get text selection - not only the post content :(
	if (window.getSelection)
	{
		theSelection = window.getSelection().toString();
	}
	else if (document.getSelection)
	{
		theSelection = document.getSelection();
	}
	else if (document.selection)
	{
		theSelection = document.selection.createRange().text;
	}

	if (theSelection == '' || typeof theSelection == 'undefined' || theSelection == null)
	{
		if (divarea.innerHTML)
		{
			theSelection = divarea.innerHTML.replace(/<br>/ig, '\n');
			theSelection = theSelection.replace(/<br\/>/ig, '\n');
			theSelection = theSelection.replace(/&lt\;/ig, '<');
			theSelection = theSelection.replace(/&gt\;/ig, '>');
			theSelection = theSelection.replace(/&amp\;/ig, '&');
			theSelection = theSelection.replace(/&nbsp\;/ig, ' ');
		}
		else if (document.all)
		{
			theSelection = divarea.innerText;
		}
		else if (divarea.textContent)
		{
			theSelection = divarea.textContent;
		}
		else if (divarea.firstChild.nodeValue)
		{
			theSelection = divarea.firstChild.nodeValue;
		}
	}

	if (theSelection)
	{
		insert_text('[quote="' + username + '"]' + theSelection + '[/quote]');
	}

	return;
}

/**
* From http://www.massless.org/mozedit/
*/

function mozWrap(txtarea, open, close)
{
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	var scrollTop = txtarea.scrollTop;

	if (selEnd == 1 || selEnd == 2)
	{
		selEnd = selLength;
	}

	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);

	txtarea.value = s1 + open + s2 + close + s3;
	// with special thanks to Ash Hi Fi Zone
	txtarea.selectionStart = selStart + open.length;
    txtarea.selectionEnd = selEnd + open.length;
	// txtarea.selectionStart = selEnd + open.length + close.length;
	// txtarea.selectionEnd = txtarea.selectionStart;
	txtarea.focus();
	txtarea.scrollTop = scrollTop;

	return;
}


/**
* Insert at Caret position. Code from
* http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
*/
function storeCaret(textEl)
{
	if (textEl.createTextRange)
	{
		textEl.caretPos = document.selection.createRange().duplicate();
	}
}

/**
* Color pallette
*/
function colorPalette(dir, width, height)
{
	var r = 0, g = 0, b = 0;
	var numberList = new Array(6);
	var color = '';

	numberList[0] = '00';
	numberList[1] = '40';
	numberList[2] = '80';
	numberList[3] = 'BF';
	numberList[4] = 'FF';

	document.writeln('<table cellspacing="1" cellpadding="0" border="0">');

	for (r = 0; r < 5; r++)
	{
		if (dir == 'h')
		{
			document.writeln('<tr>');
		}

		for (g = 0; g < 5; g++)
		{
			if (dir == 'v')
			{
				document.writeln('<tr>');
			}

			for (b = 0; b < 5; b++)
			{
				color = String(numberList[r]) + String(numberList[g]) + String(numberList[b]);
				document.write('<td bgcolor="#' + color + '" style="width: ' + width + 'px; height: ' + height + 'px;">');
				document.write('<a href="#" onclick="bbfontstyle(\'[color=#' + color + ']\', \'[/color]\'); return false;"><img src="images/spacer.gif" width="' + width + '" height="' + height + '" alt="#' + color + '" title="#' + color + '" /></a>');
				document.writeln('</td>');
			}

			if (dir == 'v')
			{
				document.writeln('</tr>');
			}
		}

		if (dir == 'h')
		{
			document.writeln('</tr>');
		}
	}
	document.writeln('</table>');
}


/**
* Caret Position object
*/
function caretPosition()
{
	var start = null;
	var end = null;
}


/**
* Get the caret position in an textarea
*/
function getCaretPosition(txtarea)
{
	var caretPos = new caretPosition();

	// simple Gecko/Opera way
	if(txtarea.selectionStart || txtarea.selectionStart == 0)
	{
		caretPos.start = txtarea.selectionStart;
		caretPos.end = txtarea.selectionEnd;
	}
	// dirty and slow IE way
	else if(document.selection)
	{

		// get current selection
		var range = document.selection.createRange();

		// a new selection of the whole textarea
		var range_all = document.body.createTextRange();
		range_all.moveToElementText(txtarea);

		// calculate selection start point by moving beginning of range_all to beginning of range
		var sel_start;
		for (sel_start = 0; range_all.compareEndPoints('StartToStart', range) < 0; sel_start++)
		{
			range_all.moveStart('character', 1);
		}

		txtarea.sel_start = sel_start;

		// we ignore the end value for IE, this is already dirty enough and we don't need it
		caretPos.start = txtarea.sel_start;
		caretPos.end = txtarea.sel_start;
	}

	return caretPos;
}
