








function SelectRange(input, selectionStart, selectionEnd)
{
	if (input.setSelectionRange)
		input.setSelectionRange(selectionStart, selectionEnd);
	
	else if (input.createTextRange)
	{
		var range = input.createTextRange();
		range.collapse(true);
		range.moveEnd('character', selectionEnd);
		range.moveStart('character', selectionStart);
		range.select();
	}
}

function bbCode (input, openingTag, closingTag)
{
	input.focus();
	if (input.setSelectionRange)
	{
		var selectionStart = input.selectionStart;
		var selectionEnd = input.selectionEnd;
		var selectedText = input.value.substring(selectionStart, selectionEnd);
		var textPrefix = input.value.substring(0, selectionStart);
		var textSuffix = input.value.substring(selectionEnd);
		
		input.value = textPrefix + openingTag + selectedText + closingTag + textSuffix;

		if (selectionStart != selectionEnd)
		{	// has there been a selection
			SelectRange(input, selectionStart + openingTag.length, selectionStart + openingTag.length + selectedText.length);
		}
		else
		{	// set caret
			SelectRange(input, selectionStart + openingTag.length, selectionStart + openingTag.length);
		}
	}
	else if (document.selection)
	{
		var range = document.selection.createRange();
		if (range.parentElement() == input)
		{
			var length = range.text.length;
			range.text = openingTag + range.text + closingTag;
			range.moveStart('character', -(length + closingTag.length));
			range.moveEnd('character', -closingTag.length);
			range.select();
		}
	}
}
function bbCodeAsk (input, tag, question, type)
{
	input.focus();
	var userInputText;
	userInputText = prompt(question);
	if (type == '0')
	{
		bbCode(input, "[" + tag + "=\"" + userInputText + "\"]", "[/" + tag + "]");
	}
	else
	{
		bbCode(input, "[" + tag + "]" + userInputText + "[/" + tag + "]", '');
	}
}
function bbUrl (input)
{
	input.focus();
	var userInputURL;
	var userInputText;
	
	if (input.setSelectionRange)
	{	// Figure out if anything was selected in FF/Opera
		var selectionStart = input.selectionStart;
		var selectionEnd = input.selectionEnd;
		var length = input.value.substring(selectionStart, selectionEnd).length;
	}
	else if (document.selection)
	{	// Figure out if anything was selected in IExplore
		var range = document.selection.createRange();
		if (range.parentElement() == input)
		{
			var length = range.text.length;
		}
	}
	if (length == 0)
	{	// No text was selected, so we'll prompt it
		userInputURL = prompt("Enter the URL that you want to link to.");
		if(userInputURL.length != 0)
		{	// Something was entered
			userInputText = prompt("Enter the text you want to display on your link.");
			if(userInputText.length != 0)
			{	// Text was entered, thus we use that as display text
				bbCode(input, "[url=\"" + userInputURL + "\"]" + userInputText, "[/url]");
			}
			else
			{	// No text was entered, so we'll only use the URL provided
				bbCode(input, "[url]" + userInputURL, "[/url]");
			}
			
			// Right now our pointer is right in front of our closing tag, we want it behind it
			if (input.setSelectionRange)
			{
				var selectionStart = input.selectionStart;
				SelectRange(input, selectionStart + 6, selectionStart + 6);
			}
			else if (document.selection)
			{
				var movePointer = 0;
				if(userInputText.length == 0)
				{
					movePointer = 11 + userInputURL.length;
				}
				else
				{
					movePointer = 14 + userInputText.length + userInputURL.length;
				}
				range.moveStart('character', movePointer);
				range.moveEnd('character', movePointer);
				range.select();
			}
		}
	}
	else
	{	// Use the selected text as the link text.
		userInputURL = prompt("Enter the URL that you want to link to.");
		if(userInputURL.length != 0)
		{
			bbCode(input, "[url=\"" + userInputURL + "\"]", "[/url]");
		}
	}
}
function bbImg (input)
{
	input.focus();
	var userInputURL;
	
	if (input.setSelectionRange)
	{	// Figure out if anything was selected in FF/Opera
		var selectionStart = input.selectionStart;
		var selectionEnd = input.selectionEnd;
		var length = input.value.substring(selectionStart, selectionEnd).length;
	}
	else if (document.selection)
	{	// Figure out if anything was selected in IExplore
		var range = document.selection.createRange();
		if (range.parentElement() == input)
		{
			var length = range.text.length;
		}
	}
	if (length == 0)
	{	// Nothing was selected, we need to ask for the URL to the image
		userInputURL = prompt("Enter a link to the image you wish to display.");
		if(userInputURL.length != 0)
		{
			bbCode(input, "[img]" + userInputURL, "[/img]");
		}
	}
	else
	{	// Something was selected, we'll use this as the URL for the image
		bbCode(input, "[img]", "[/img]");
	}
}
function bbAImg (input)
{
	input.focus();
	var userInputURL;
	var userInputAlign;
	
	if (input.setSelectionRange)
	{	// Figure out if anything was selected in FF/Opera
		var selectionStart = input.selectionStart;
		var selectionEnd = input.selectionEnd;
		var length = input.value.substring(selectionStart, selectionEnd).length;
	}
	else if (document.selection)
	{	// Figure out if anything was selected in IExplore
		var range = document.selection.createRange();
		if (range.parentElement() == input)
		{
			var length = range.text.length;
		}
	}
	if (length == 0)
	{	// Nothing was selected, we need to ask for the URL to the image
		userInputURL = prompt("Enter a link to the image you wish to display.");
		if(userInputURL.length != 0)
		{
			userInputAlign = prompt("Please enter the alignment you wish to use (Left, Right, Center).");
			if(userInputAlign.length != 0)
			{
				bbCode(input, "[img=\"" + userInputAlign + "\"]" + userInputURL, "[/img]");
			}
			else
			{
				bbCode(input, "[img]" + userInputURL, "[/img]");
			}
		}
	}
	else
	{	// Something was selected, we'll use this as the URL for the image
		userInputAlign = prompt("Please enter the alignment you wish to use (Left, Right, Center).");
		if(userInputAlign.length != 0)
		{
			bbCode(input, "[img=\"" + userInputAlign + "\"]", "[/img]");
		}
		else
		{
			bbCode(input, "[img]", "[/img]");
		}
	}
}

function AddSmiley(Trigger, input)
{
	bbCode(input, Trigger, "");
}

function toggleSwatches(TargetHTML, TargetInput)
{
	if(document.getElementById(TargetHTML).style.display != 'block' && TargetInput != '')
	{
		var curSwatches = swatches;
		curSwatches = curSwatches.replace(/::TARGETINPUT::/g, TargetInput);
		curSwatches = curSwatches.replace(/::TARGETHTML::/g, TargetHTML);
		document.getElementById(TargetHTML).innerHTML = curSwatches;
		document.getElementById(TargetHTML).style.display = 'block';
	}
	else
	{
		document.getElementById(TargetHTML).innerHTML = "";
		document.getElementById(TargetHTML).style.display = 'none';
	}
}
