// BBCode
var startPos, endPos, help, input, selection, default_url, default_text;

function insertBBCode(include_start, include_end, feld) {
	if (document.selection)
	{
		document.getElementById(feld).focus();
		document.selection.createRange().text = include_start + document.selection.createRange().text + include_end;
	}
	else if (document.getElementById(feld).selectionStart || document.getElementById(feld).selectionStart == '0')
	{
		input = document.getElementById(feld);
		
		startPos = input.selectionStart;
		endPos = input.selectionEnd;
		help = input.value;
		
		selection = help.substr(startPos, endPos-startPos);
		
		document.getElementById(feld).value = help.substring(0, startPos) + include_start + selection + include_end +
			help.substring(endPos, help.length);
		
		if(selection == "")
		{
			input.selectionStart = startPos+include_start.length;
			input.selectionEnd = startPos+include_start.length;
		}
		else
		{
			input.selectionStart = startPos;
			input.selectionEnd = startPos + include_end.length + include_start.length + selection.length;
		}
	}
	else
	{
		document.getElementById(feld).value += include_start + include_end;
	}
	
	document.getElementById(feld).focus();
};

function insertBBCodeSpecial(type, feld)
{
	if(type == "url")
	{
		if (document.selection)
		{
			document.getElementById(feld).focus();
			selection = document.selection.createRange().text;
		}
		else if (document.getElementById(feld).selectionStart || document.getElementById(feld).selectionStart == '0')
		{
			input = document.getElementById(feld);
			
			startPos = input.selectionStart;
			endPos = input.selectionEnd;
			help = input.value;
			
			selection = help.substr(startPos, endPos-startPos);
		}
		
		if(selection.substr(0, 7).toLowerCase() == "http://")
		{
			default_url = selection;
		}
		else if(selection.substr(0, 4).toLowerCase() == "www.")
		{
			default_url = "http://" + selection;
		}
		else
		{
			default_url = "";
		}
		
		set_url = window.prompt("Bitte gib eine Link-Adresse an!", default_url);
		set_text = window.prompt("Bitte gib einen Link-Titel an!", selection);
		
		if(set_url == null)
			set_url = "";
		
		if(set_url.substr(0, 7).toLowerCase() != "http://")
			set_url = "http://" + set_url;
		
		if(set_text == null)
			set_text = "";
		
		if ( set_url != "" && set_text != "" )
		{
			if(set_url == set_text)
			{
				set_selection = "[url]" + set_url + "[/url]";
			}
			else
			{
				set_selection = "[url=" + set_url + "]" + set_text + "[/url]";
			}
			
			if (document.selection)
			{
				document.selection.createRange().text = set_selection;
			}
			else if (document.getElementById(feld).selectionStart || document.getElementById(feld).selectionStart == '0')
			{
				document.getElementById(feld).value = help.substring(0, startPos) + set_selection + help.substring(endPos, help.length);
				
				if(selection != "")
				{
					input.selectionStart = startPos;
					input.selectionEnd = startPos + set_selection.length;
				}
				else
				{
					input.selectionStart =  startPos + set_selection.length;
					input.selectionEnd = input.selectionStart;
				}
			}
			else
			{
				document.getElementById(feld).value += set_selection;
			}
		}
	}
	else if(type == "img")
	{
		if (document.selection)
		{
			document.getElementById(feld).focus();
			selection = document.selection.createRange().text;
		}
		else if (document.getElementById(feld).selectionStart || document.getElementById(feld).selectionStart == '0')
		{
			input = document.getElementById(feld);
			
			startPos = input.selectionStart;
			endPos = input.selectionEnd;
			help = input.value;
			
			selection = help.substr(startPos, endPos - startPos)
		}
		
		if (selection == "" )
			set_img = window.prompt("Bitte gib den Link zu einem Bild an!\n\nBeachte:\nDas Bild muss irgendwo im Internet zu finden sein, du kannst kein Bild von " +
				"deiner Festplatte verwenden!", selection);
		else
			set_img = selection;
		
		if(set_img == null)
			set_img = "";
		
		if ( set_img != "" )
		{
			if (document.selection)
			{
				document.selection.createRange().text = "[img]" + set_img + "[/img]";
			}
			else if (document.getElementById(feld).selectionStart || document.getElementById(feld).selectionStart == '0')
			{
				document.getElementById(feld).value = help.substring(0, startPos) + "[img]" + set_img + "[/img]" + help.substring(endPos, help.length);
				
				input.selectionStart = startPos + set_img.length + 11;
				input.selectionEnd = input.selectionStart;
			}
			else
			{
				document.getElementById(feld).value += "[img]" + set_img + "[/img]";
			}
		}
	}
	else if(type == "color")
	{
		set_color = window.prompt("Bitte gib einen Farb-Code oder -Namen an!\n\nBeispiele:\n  red, blue, white,...\n  oder\n  #FF0000, #0000FF, #FFFFFF,...");
		
		if(set_color == null)
			set_color = "";
		
		if ( set_color != "" )
			insertBBCode("[color=" + set_color + "]", "[/color]", feld);
	}
	else if(type.substr(0, 4) == "list")
	{
		if (document.selection)
		{
			document.getElementById(feld).focus();
			selection = document.selection.createRange().text;
		}
		else if (document.getElementById(feld).selectionStart || document.getElementById(feld).selectionStart == '0')
		{
			input = document.getElementById(feld);
			
			startPos = input.selectionStart;
			endPos = input.selectionEnd;
			help = input.value;
			
			selection = help.substr(startPos, endPos - startPos);
		}
		
		var ipos = 1;
		
		while(selection.indexOf("\n", ipos) > 0)
		{
			ipos = selection.indexOf("\n", ipos);
			
			selection = selection.substr(0, ipos) + "\n  [*]" + selection.substr(ipos + 1);
			
			ipos++;
		}
		
		set_selection = "[" + type + "]\n  [*]" + selection + "\n[/list]";
		
		if (document.selection)
		{
			document.selection.createRange().text = set_selection;
		}
		else if (document.getElementById(feld).selectionStart || document.getElementById(feld).selectionStart == '0')
		{
			document.getElementById(feld).value = help.substring(0, startPos) + set_selection + help.substring(endPos, help.length);
			
			input.selectionStart = startPos + set_selection.length;
			input.selectionEnd = input.selectionStart;
		}
		else
		{
			document.getElementById(feld).value += set_selection;
		}
	}
	
	document.getElementById(feld).focus();
};
