var frame_load_counter = 0;
var preview_mode = 0;

function initToolbar() 
{
		function hide_all_popups(){
			$('.toolbar .select-color .popup-select-color').hide();
			$('.toolbar .select-fontsize .popup-select-fontsize').hide();
			$('.toolbar .select-image .popup-select-image').hide();
			$('.toolbar .select-video .popup-select-video').hide();
			$('.toolbar .select-smile .popup-select-smile').hide();
		}
		
		$('.toolbar input[class!="icon color"][class!="icon fontsize"][class!="icon smile"][class!="icon image"][class!="icon video"][class!="file"][class!="btn"]').mouseover( function(e){	
		$('.toolbar input').removeClass('hover');
		
		$(this).addClass('hover');
	});
	
	$('.toolbar input[class="icon color"]').mouseover( function(e){		
		$('.toolbar input').removeClass('hover');
		
		$(this).addClass('hover');
	});
	
	$('.toolbar input[class="icon fontsize"]').mouseover( function(e){		
		$('.toolbar input').removeClass('hover');
		
		$(this).addClass('hover');
	});
	
	$('.toolbar input[class="icon smile"]').mouseover( function(e){		
		$('.toolbar input').removeClass('hover');
		
		$(this).addClass('hover');
	});
	
	$('.toolbar input[class="icon image"]').mouseover( function(e){		
		$('.toolbar input').removeClass('hover');
		
		$(this).addClass('hover');
	});
	
	$('.toolbar input[class="icon video"]').mouseover( function(e){		
		$('.toolbar input').removeClass('hover');
		
		$(this).addClass('hover');
	});
	
	/*fontsize*/
	$('.toolbar .select-fontsize input.fontsize').click( function(e){		
			$('.toolbar .select-fontsize .popup-select-fontsize').toggle();
			$('.toolbar .select-color .popup-select-color').hide();			
			$('.toolbar .select-image .popup-select-image').hide();
			$('.toolbar .select-video .popup-select-video').hide();
			$('.toolbar .select-smile .popup-select-smile').hide();
		$(this).blur();
	});
	
	$('.toolbar .select-fontsize .popup-select-fontsize a').click( function(e){		
		$('.toolbar .select-fontsize .popup-select-fontsize').hide();	
		return false;
	});
	
	/*color*/
	var color_html = colorPalette('v', 2, 2);
	$('.toolbar .select-color .popup-select-color').html(color_html);
	
	$('.toolbar .select-color input.color').click( function(e){		
		$('.toolbar .select-color .popup-select-color').toggle();
		
			$('.toolbar .select-fontsize .popup-select-fontsize').hide();
			$('.toolbar .select-image .popup-select-image').hide();
			$('.toolbar .select-video .popup-select-video').hide();
			$('.toolbar .select-smile .popup-select-smile').hide();
			
		$(this).blur();
	});
	
	$('.toolbar .select-color .popup-select-color a').click( function(e){		
		$('.toolbar .select-color .popup-select-color').hide();	
		return false;
	});
	
	/*smile*/
	$('.toolbar .select-smile input.smile').click( function(e){		
		$('.toolbar .select-smile .popup-select-smile').toggle();
		
			$('.toolbar .select-color .popup-select-color').hide();
			$('.toolbar .select-fontsize .popup-select-fontsize').hide();
			$('.toolbar .select-image .popup-select-image').hide();
			$('.toolbar .select-video .popup-select-video').hide();
			
		$(this).blur();
	});
	
	$('.toolbar .select-smile .popup-select-smile img').click( function(e){
		var smile_code = $(this).attr('alt');
		insert_text(smile_code);
		$('.toolbar .select-smile .popup-select-smile').hide();
	});
	
	/*image*/
		$('.toolbar .select-image input.image').click( function(e){	
		$('.toolbar .select-image .popup-select-image').toggle();
			$('.toolbar .select-color .popup-select-color').hide();
			$('.toolbar .select-fontsize .popup-select-fontsize').hide();			
			$('.toolbar .select-video .popup-select-video').hide();
			$('.toolbar .select-smile .popup-select-smile').hide();		
		$(this).blur();
	});
	
	/*video*/
	$('.toolbar .select-video input.video').click( function(e){		
		$('.toolbar .select-video .popup-select-video').toggle();
		$('.toolbar .select-color .popup-select-color').hide();
			$('.toolbar .select-fontsize .popup-select-fontsize').hide();
			$('.toolbar .select-image .popup-select-image').hide();			
			$('.toolbar .select-smile .popup-select-smile').hide();
		$(this).blur();
	});
	
	/*preview*/
	$('.toolbar input.preview').click( function(e){	
		
		preview_mode = !preview_mode;
		
		if($('#preview').css('display')=='none')
		{
			var text = $('form[name="'+form_name+'"] textarea[name="'+text_name+'"]').val();
			
			text = text.replace(/\[b\](.*?)\[\/b\]/gi, "<strong>$1</strong>");
      text = text.replace(/\[i\](.*?)\[\/i\]/gi, "<em>$1</em>");
      text = text.replace(/\[u\](.*?)\[\/u\]/gi, "<u>$1</u>");
      text = text.replace(/\[img](.*?)\[\/img\]/gi, "<img src='$1'>");
      text = text.replace(/\[url\](.*?)\[\/url\]/gi, "<a href='$1'>$1</a>");
      text = text.replace(/\[url=(.*?)\](.*?)\[\/url\]/gi, "<a href='$1'>$2</a>");
      text = text.replace(/\[size=(.*?)\](.*?)\[\/size\]/gi, "<span style='font-size:$1px'>$2</span>");
      text = text.replace(/\[color=(.*?)\](.*?)\[\/color\]/gi, "<span style='color:$1'>$2</span>");
    
    	
  var patterns = new Array(
		'O:-)',
		':-)',
		':-(',
		';-)',
		':-P',
		'8-)',
		':-D',
		':-[',
		'=-O',
		':-*',
		":'(",
		':-X',
		'>:o',
		':-|',
		':-\\',
		'*JOKINGLY*',
		']:->',
		'[:-}',
		'*KISSED*',
		':-!',
		'*TIRED*',
		'*STOP*',
		'*KISSING*',
		'@}->--',
		'*THUMBS UP*',
		'*DRINK*',
		'*IN LOVE*',
		'@=',
		'*HELP*',
		'\m/',
		'%)',
		'*OK*',
		'*WASSUP*',
		'*SORRY*',
		'*BRAVO*',
		'*ROFL*',
		'*PARDON*',
		'*NO*',
		'*CRAZY*',
		'*DONT_KNOW*',
		'*DANCE*',
		'*YAHOO*',
		'*HI*',
		'*BYE*',
		'*YES*',
		';D',
		'*WALL*',
		'*WRITE*',
		'*SCRATCH*'
		);
		
		var replacements = new Array(
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/aa.gif" alt="O:-)" title="O:-)" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/ab.gif" alt=":-)" title=":-)" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/ac.gif" alt=":-(" title=":-(" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/ad.gif" alt=";-)" title=";-)" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/ae.gif" alt=":-P" title=":-P" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/af.gif" alt="8-)" title="8-)" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/ag.gif" alt=":-D" title=":-D" />',	
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/ah.gif" alt=":-[" title=":-[" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/ai.gif" alt="=-O" title="=-O" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/aj.gif" alt=":-*" title=":-*" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/ak.gif" alt=":\'(" title=":\'(" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/al.gif" alt=":-X" title=":-X" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/am.gif" alt=">:o" title=">:o" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/an.gif" alt=":-|" title=":-|" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/ao.gif" alt=":-\" title=":-\" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/ap.gif" alt="*JOKINGLY*" title="*JOKINGLY*" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/aq.gif" alt="]:->" title="]:->" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/ar.gif" alt="[:-}" title="[:-}" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/as.gif" alt="*KISSED*" title="*KISSED*" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/at.gif" alt=":-!" title=":-!" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/au.gif" alt="*TIRED*" title="*TIRED*" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/av.gif" alt="*STOP*" title="*STOP*" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/aw.gif" alt="*KISSING*" title="*KISSING*" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/ax.gif" alt="@}->--" title="@}->--" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/ay.gif" alt="*THUMBS UP*" title="*THUMBS UP*" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/az.gif" alt="*DRINK*" title="*DRINK*" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/ba.gif" alt="*IN LOVE*" title="*IN LOVE*" />',			
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/bb.gif" alt="@=" title="@=" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/bc.gif" alt="*HELP*" title="*HELP*" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/bd.gif" alt="\m/" title="\m/" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/be.gif" alt="%)" title="%)" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/bf.gif" alt="*OK*" title="*OK*" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/bg.gif" alt="*WASSUP*" title="*WASSUP*" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/bh.gif" alt="*SORRY*" title="*SORRY*" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/bi.gif" alt="*BRAVO*" title="*BRAVO*" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/bj.gif" alt="*ROFL*" title="*ROFL*" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/bk.gif" alt="*PARDON*" title="*PARDON*" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/bl.gif" alt="*NO*" title="*NO*" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/bm.gif" alt="*CRAZY*" title="*CRAZY*" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/bn.gif" alt="*DONT_KNOW*" title="*DONT_KNOW*" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/bo.gif" alt="*DANCE*" title="*DANCE*" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/bp.gif" alt="*YAHOO*" title="*YAHOO*" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/bq.gif" alt="*HI*" title="*HI*" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/br.gif" alt="*BYE*" title="*BYE*" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/bs.gif" alt="*YES*" title="*YES*" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/bt.gif" alt=";D" title=";D" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/bu.gif" alt="*WALL*" title="*WALL**" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/bv.gif" alt="*WRITE*" title="*WRITE*" />',
			'<img class="smile" src="/themes/posh/wysiwyg/smiles/bw.gif" alt="*SCRATCH*" title="*SCRATCH*" />'	
		);
    
			for(i=0;i<patterns.length;i++)
			{
				text = text.replace(patterns[i], replacements[i]);
			}		
    
			$('#preview').html(text);
			$('form[name="'+form_name+'"] textarea[name="'+text_name+'"]').hide();
			$('#preview').show();
		}
		else
		{
			$('form[name="'+form_name+'"] textarea[name="'+text_name+'"]').show();
			$('#preview').hide();
		}
		$(this).blur();
	});
	
	
	
}

var wndUploadImage;
var wndUploadVideo;
function CompleteUpload(iframeName)
{
	for(i=0;i<frames.length;i++)
	{
		try{
		if(frames[i].name == iframeName)
		{
			frame_index = i;
		}
		}catch(err){}
	}
	
	var new_src = frames[frame_index].document.getElementById('new_src');
	var fr_text = frames[frame_index].document.body.innerHTML;
	if(!new_src)
	{
		if (fr_text != "")
		{
			if (fr_text != "__FAILED__")
			{	
				if(iframeName == 'ImageUpload')
				{
					frame_load_counter = frame_load_counter+1;
					$('.toolbar .select-image .popup-select-image').hide();					
					
					$('#'+iframeName).attr('src', '/themes/posh/wysiwyg/ImageUpload.html?counter='+frame_load_counter);
					var file_path = "/upload/wysiwyg/" + fr_text;			
					insert_text("[img]"+file_path+"[/img]");
				}
				else if(iframeName == 'VideoUpload')
				{
					frame_load_counter = frame_load_counter+1;
					$('.toolbar .select-video .popup-select-video').hide();
					$('#'+iframeName).attr('src', '/themes/posh/wysiwyg/VideoUpload.html?counter='+frame_load_counter);
					var file_path = "/upload/video/" + fr_text;			
					insert_text("[video]"+file_path+"[/video]");
				}
				
				alert('Upload has been successfully completed');
				
				document.forms[form_name].elements[text_name].focus();			
			}
			else
			{
				frames[frame_index].document.body.innerHTML = 'Loading...';
				$('#ImageUpload').attr('src', '/themes/posh/wysiwyg/ImageUpload.html?counter='+frame_load_counter);
				$('#VideoUpload').attr('src', '/themes/posh/wysiwyg/VideoUpload.html?counter='+frame_load_counter);		
				alert("Upload has been failed.");
			}
		}
	}
}


var help_line = {
			b: 'Bold text: [b]text[/b]',
			i: 'Italic text: [i]text[/i]',
			u: 'Underline text: [u]text[/u]',
			s: 'Font colour: [color=red]text[/color]  Tip: you can also use color=#FF0000',
			f: 'Font size: [size=9]small text[/size]',
			w: 'Insert URL: [url]http://url[/url] or [url=http://url]URL text[/url]',
			smile: 'Insert smiles',
			v: 'Insert video: [video]http://video_url[/video]',
			p: 'Insert image: [img]http://image_url[/img]',
			pr: 'Preview',
			tip: 'Tip: Styles can be applied quickly to selected text.'
		}
		
var bbtags = new Array('[b]','[/b]','[i]','[/i]','[u]','[/u]','[quote]','[/quote]','[code]','[/code]','[list]','[/list]','[list=]','[/list]','[img]','[/img]','[url]','[/url]','[flash=]', '[/flash]','[size=]','[/size]');


function tag_url()
{
  var FoundErrors = '';
	var thesel ='';
	if ( (clientVer >= 4) && is_ie && is_win)
	{
	thesel = document.selection.createRange().text;
	} else thesel ='My Webpage';

    if (!thesel) {
        thesel ='My Webpage';
    }

    var enterURL   = prompt('Please enter URL', "http://");
    var enterTITLE = prompt('Please enter URL name', thesel);

    if (!enterURL) {
        FoundErrors += " No URL.";
    }
    if (!enterTITLE) {
        FoundErrors += " No URL name.";
    }

    if (FoundErrors) {
        alert("Error!"+FoundErrors);
        return;
    }
	
	insert_text("[url="+enterURL+"]"+enterTITLE+"[/url]");
	document.forms[form_name].elements[text_name].focus();
};

/****************************************/

/**
* 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;
//window.onload = 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() 
{
	
	$('<input type="text" readonly="readonly" name="helpbox" class="helpline" value="" />').insertAfter('textarea[name="'+text_name+'"]');
	$('<div id="toolbar"></div>').insertBefore('textarea[name="'+text_name+'"]');
	$('<div id="preview"></div>').insertAfter('textarea[name="'+text_name+'"]');	
	$("#toolbar").load("/themes/posh/wysiwyg/toolbar.tpl","", initToolbar);
	
	var doc;

	if (document.forms[form_name])
	{
		doc = document;
	}
	else 
	{
		doc = opener.document;
	}
	
	var form = doc.forms[form_name];
	if(form)
	{
		var textarea = form.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(preview_mode==1) return false;
	
	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)
{
	if(preview_mode==1) return false;
	
	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
	insert_text(bbopen + bbclose);

	// 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)
{
	if(preview_mode==1) return false;
	
	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, '&');
		}
		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;
	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 = '';

	var colors = new Array('#FFFFFF', '#FFCCCC', '#FFCC99', '#FFFF99', '#FFFFCC', '#99FF99', '#99FFFF', '#CCFFFF', '#CCCCFF', '#FFCCFF',
										 		 '#CCCCCC', '#FF6666', '#FF9966', '#FFFF66', '#FFFF33', '#66FF99', '#33FFFF', '#66FFFF', '#9999FF', '#FF99FF',
										 		 '#C0C0C0', '#FF0000', '#FF9900', '#FFCC66', '#FFFF00', '#33FF33', '#66CCCC', '#33CCFF', '#6666CC', '#CC66CC',
										 		 '#999999', '#CC0000', '#FF6600', '#FFCC33', '#FFCC33', '#33CC00', '#00CCCC', '#3366FF', '#6633FF', '#CC33CC',
										 		 '#666666', '#990000', '#CC6600', '#CC9933', '#999900', '#009900', '#339999', '#3333FF', '#6600CC', '#993399',
										 		 '#333333', '#660000', '#993300', '#996633', '#666600', '#006600', '#336666', '#000099', '#333399', '#663366',
										 		 '#000000', '#330000', '#663300', '#663333', '#333300', '#003300', '#003333', '#000066', '#330099', '#330033'
										 		 
										);

	numberList[0] = '00';
	numberList[1] = '33';
	numberList[2] = '66';
	numberList[3] = '99';
	numberList[4] = 'FF';
	
	var count_colors = colors.length;
	
	var counter = 0;
	var stop = 0;
	
	var color_html = '';
	
  color_html = '<table cellspacing="1" cellpadding="0" border="1">';

	for (r = 0; r < 7; r++)
	{
		color_html = color_html+'<tr>';	
		for (g = 0; g < 10; g++)
		{	
			var color = String(colors[counter]);
			//alert(color); 
			
			color_html = color_html+'<td bgcolor="' + color + '" width="10" height="10">';
			color_html = color_html+'<a href="#" onclick="bbfontstyle(\'[color=' + color + ']\', \'[/color]\'); return false;" onmouseover="helpline(\'s\');"  onmouseout="helpline(\'tip\');">' + color + '</a>';
			color_html = color_html+'</td>';
			
			counter = counter+1;
		
			if(counter == count_colors) 
			{
				stop = 1;
				break;
			}
			
		}
		
		color_html = color_html+'</tr>';
		
		if(stop == 1)
		{
			break;
		}
			
	}
	
	color_html = color_html+'</table>';
	
	return color_html;
}


/**
* 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;
}