String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}


var FALSE = 0;
var TRUE  = 1;

var theform = "";

var Text    = "";
var start   = "";
var middle  = "";
var end     = "";

var AddTxt  = "";

var start_selection = 0;
var end_selection   = 0;

function Form_init()
{
	theform = document.forms.mytext;
}

function setfocus()
{
	//theform.Text.focus();
}


function getActiveText()
{
	setfocus();
	getSelection()
	Text = theform.Text.value;
}

function AddText()
{
	if ((start_selection == end_selection) &&  (end_selection == theform.Text.textLength))
	{
		theform.Text.value = Text + AddTxt;
	}
	else
	{
		theform.Text.value = start + AddTxt + end;
	}
}

function getSelection()
{
	start_selection = theform.Text.selectionStart;
	end_selection   = theform.Text.selectionEnd;

	//alert (">" + start_selection + "<>" + end_selection +"<");

	start  = (theform.Text.value).substring(0, start_selection);
	middle = (theform.Text.value).substring(start_selection, end_selection);
	end    = (theform.Text.value).substring(end_selection, theform.Text.textLength);

	if ((start_selection - end_selection) > 0)
		return middle;
	else
		return FALSE;
}

function InsertSmiley(Smiley)
{
	getActiveText();
	AddTxt = Smiley;
	AddText();
}

function InsertImg()
{
	getActiveText();

	if (middle != "")
	{
		if (middle.substring(0,7) == "http://")
			var src = middle;
		else
			var src = prompt("Gib eine URL für ein Bild ein","http://");
	}
	else
	{
		var src = prompt("Gib eine URL für ein Bild ein","http://");
	}

	if ((src != "") && (src != null))
	{
		AddTxt = "[IMG=" + src.trim() + "] ";
		AddText();
	}
}

function InsertSingleTag(tag)
{
	getActiveText();

	if (middle != "")
	{
		var src = middle;
	}
	else
	{
		var src = prompt("Gib deinen Wert ein:","");
	}

	if ((src != "") && (src != null))
	{
		AddTxt = "[" + tag + "=" + src.trim() + "] ";
		AddText();
	}
}

function InsertStyleCode(Type,Param)
{
	getActiveText();

	if (Param != "")
		Param = "=" + Param;

	if (middle != "")
	{
		var FormatText = 1;
		AddTxt = "[" + Type + "" + Param + "]" + middle.trim() + "[/" + Type + "] ";
	}
	else
	{
		var FormatText = prompt("Gib den zu formatierenden Text ein:","");
		AddTxt = "[" + Type + Param +"]" + FormatText.trim() + "[/" + Type + "] ";
	}

	if(FormatText)
		AddText();
}

function InsertLink(Type)
{
	getActiveText();


	if (Type == "URL")
	{
		if (middle != "")
		{
			if (middle.substring(0,7) == "http://")
			{
				var linktext = prompt("Gib einen Beschreibungstext für die URL ein (optional)","");
				var linkurl  = middle;
			}
			else
			{
				var linkurl  = prompt("Gib die URL ein","http://");
				var linktext = middle;
			}
		}
		else
		{
			var linktext = prompt("Gib einen Beschreibungstext für die URL ein (optional)","");
			var linkurl  = prompt("Gib die URL ein","http://");
		}

		if ((linkurl != null) && (linkurl != ""))
		{
			if (linktext != "")
				AddTxt = "[URL=" + linkurl.trim() + "]" + linktext.trim() + "[/URL] ";
			else
				AddTxt = "[URL=" + linkurl.trim() + "]" + linkurl.trim() + "[/URL] ";
		}
	}
	else
	{
		if (middle != "")
		{
			var mailurl = prompt("Gib eine eMail Adresse ein");
			var mailname = middle;
		}
		else
		{
			var mailname = prompt("Gib einen Beschreibungstext für die Mail ein (optional)","");
			var mailurl = prompt("Gib eine eMail Adresse ein");
		}

		if ((mailurl != null) && (mailurl != ""))
		{
			if (mailurl != "")
				AddTxt = "[MAIL=" + mailurl.trim() + "]" + mailname.trim() + "[/MAIL] ";
			else
				AddTxt = "[MAIL=" + mailurl.trim() + "]" + mailurl.trim() + "[/MAIL] ";
		}
	}
	AddText();
}

