// BBCode
function namedLocation() {
 var locationName = prompt("What do you want to name this location?\n\n(location names must be unique,\nand must NOT contain spaces, or\nany characters other than a-z, 0-9)");
	if (locationName==null) {return;}
	var insertedText = '[name="' + locationName + '"]';
	window.replaceText(insertedText);
}

function insertGmap() {
		var mapURI   = prompt("What is the URL (link) of the Google Map?");
  var mapName  = prompt("What is the name of the route?");
  if (mapURI==null||mapName==null) {
			return false;
		}
  var message = "";
								
		message = message + "[googlemap url="+mapURI+" mapname="+mapName+"]\n";

  window.replaceText(message);
}

function insertYouTube() {
		var videoCode       = prompt("Please paste in the <Embed> code\n(click the '<Embed>' button beneath the YouTube video)");
  var message         = "";
  var videoURIexpr    = new RegExp('http[a-zA-Z0-9\.\=\/\&\_\:]*');
		var videoWidthExpr  = new RegExp('width="[0-9]*"');
		var videoHeightExpr = new RegExp('height="[0-9]*"');
		var videoURI        = videoURIexpr.exec(videoCode);
		var videoWidth      = videoWidthExpr.exec(videoCode);
		var videoHeight     = videoHeightExpr.exec(videoCode);
  if (videoCode==null) {
			return false;
		}

  message = message + "[youtube url=\"" + videoURI + "\" " + videoWidth + " " + videoHeight +"]\n";
  message = message.replace(/\"/g,'');
  window.replaceText(message);
}

	// Remember the current position.function storeCaret()
function storeCaret()
{

	// Only bother if it will be useful.
	if (typeof(document.Form1.txtEdit.createTextRange) != 'undefined')
		document.Form1.txtEdit.caretPos = document.selection.createRange().duplicate();
}

// Replaces the currently selected text with the passed text.
function replaceText(text)
{

// Attempt to create a text range (IE).
	if (typeof(document.Form1.txtEdit.caretPos) != "undefined" && document.Form1.txtEdit.createTextRange)
	{
		var caretPos = document.Form1.txtEdit.caretPos;

		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
		caretPos.select();
	}
	// Mozilla text range replace.
	else if (typeof(document.Form1.txtEdit.selectionStart) != "undefined")
	{
		var begin = document.Form1.txtEdit.value.substr(0, document.Form1.txtEdit.selectionStart);
		var end = document.Form1.txtEdit.value.substr(document.Form1.txtEdit.selectionEnd);
		var scrollPos = document.Form1.txtEdit.scrollTop;

		document.Form1.txtEdit.value = begin + text + end;

		if (document.Form1.txtEdit.setSelectionRange)
		{
			document.Form1.txtEdit.focus();
			document.Form1.txtEdit.setSelectionRange(begin.length + text.length, begin.length + text.length);
		}
		document.Form1.txtEdit.scrollTop = scrollPos;
	}
	// Just put it on the end.
	else
	{
		document.Form1.txtEdit.value += text;
		document.Form1.txtEdit.focus(document.Form1.txtEdit.value.length - 1);
	}

}

// Surrounds the selected text with text1 and text2.
function surroundText(text1, text2)
{
	// Can a text range be created?
	if (typeof(document.Form1.txtEdit.caretPos) != "undefined" && document.Form1.txtEdit.createTextRange)
	{
		var caretPos = document.Form1.txtEdit.caretPos;

		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2;
		caretPos.select();
	}
	// Mozilla text range wrap.
	else if (typeof(document.Form1.txtEdit.selectionStart) != "undefined")
	{
		var begin = document.Form1.txtEdit.value.substr(0, document.Form1.txtEdit.selectionStart);
		var selection = document.Form1.txtEdit.value.substr(document.Form1.txtEdit.selectionStart, document.Form1.txtEdit.selectionEnd - document.Form1.txtEdit.selectionStart);
		var end = document.Form1.txtEdit.value.substr(document.Form1.txtEdit.selectionEnd);
		var newCursorPos = document.Form1.txtEdit.selectionStart;
		var scrollPos = document.Form1.txtEdit.scrollTop;

		document.Form1.txtEdit.value = begin + text1 + selection + text2 + end;

		if (document.Form1.txtEdit.setSelectionRange)
		{
			if (selection.length == 0)
				document.Form1.txtEdit.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
			else
				document.Form1.txtEdit.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
			document.Form1.txtEdit.focus();
		}
		document.Form1.txtEdit.scrollTop = scrollPos;
	}
	// Just put them on the end, then.
	else
	{

		document.Form1.txtEdit.value += text1 + text2;
		document.Form1.txtEdit.focus(document.Form1.txtEdit.value.length - 1);
	}
}
