var m_code = '';
var openform = null;
var selstyle = '';

// ----- \\ Crispys Code from http://www.webreference.com/js/column8/functions.html // -----
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}
// ----- \\ End of Crispys Code // -----

function inserttags(begin, end, formname)
{
	var tArea = document.getElementsByName(formname)[0];
	
	if ((tArea) && tArea.createTextRange)
	{
		tArea.focus();
		var range = document.selection.createRange();
		var insText = range.text;
		range.text = begin + insText + end;
		range = document.selection.createRange();
		if (insText.length == 0) {
			range.move('character', -end.length);
		} else {
			range.moveStart('character', begin.length + insText.length + end.length);			
		}
		range.select();
	} else {
		if ((typeof tArea.selectionStart) != 'undefined')
		{
			var selLength = tArea.textLength;
			var selStart = tArea.selectionStart;
			var selEnd = tArea.selectionEnd;
			var scrollTop = tArea.scrollTop;
			var s1 = (tArea.value).substring(0,selStart);			// Vor der Markierung
			var s2 = (tArea.value).substring(selStart, selEnd)		// Markierung
			var s3 = (tArea.value).substring(selEnd, selLength);		// Nach der Markierung
			tArea.value = s1 + begin + s2 + end + s3;			// Text neu erstellen
			
			//	Markierung erhalten
			//txtarea.selectionStart = s1.length;						
			//txtarea.selectionEnd = s1.length + s2.length + begin.length + end.length;
			
			if(s2 != '') { 
				// Wenn was Markiert war Cursor hinter 
				// den eingefügten end-Tag setzen
				tArea.selectionStart = s1.length + s2.length + begin.length + end.length;
			} else {
				// sonst zwischen die Tags
				tArea.selectionStart = s1.length + begin.length
			}
			tArea.selectionEnd = tArea.selectionStart;

			tArea.scrollTop = scrollTop;
			tArea.focus();
		} else {
			inserttext(begin + ' ' + end, formname);
		}
	}
}


function inserttext(str, formname)
{
	var tArea = document.getElementsByName(formname)[0];

	if (tArea.createTextRange)
	{
		tArea.focus();
		var range = document.selection.createRange();
		insText = range.text;
		range.text = str;
		range = document.selection.createRange();
		if(insText.length != 0) range.moveStart('character', str.length);			
		range.select();
	} else if ((typeof tArea.selectionStart) != 'undefined') {
		var selStart = tArea.selectionStart;
		var selEnd = tArea.selectionEnd;
		var scrollTop = tArea.scrollTop;
		var txtLen = tArea.value.length;
		var txtBefore = tArea.value.substring(0, selStart);
		var txtAfter =	tArea.value.substring(selEnd, txtLen);
		tArea.value = txtBefore + str + txtAfter;

		tArea.selectionStart = txtBefore.length + str.length;
		tArea.selectionEnd = tArea.selectionStart; 

		tArea.scrollTop = scrollTop;
		tArea.focus();
	} else {
		if(tArea) {
			tArea.value += str;
			tArea.focus();
		}
	}
}

function striptags(formname) {
	var tArea = document.getElementsByName(formname)[0];
	if (tArea.createTextRange) {
		var str = document.selection.createRange().text;
		tArea.focus();
		var sel = document.selection.createRange();
		sel.text = dostrip(str);
		tArea.focus();
	} else if ((typeof tArea.selectionStart) != 'undefined') {
		var selStart = tArea.selectionStart;
		var selEnd = tArea.selectionEnd;
		var txtLen = tArea.value.length;
		var txtBefore = tArea.value.substring(0, selStart);
		var txtBetween = tArea.value.substring(selStart, selEnd);
		var txtAfter =	tArea.value.substring(selEnd, txtLen);
		tArea.value = txtBefore + dostrip(txtBetween) + txtAfter;
		
	} else {
		if(confirm("Ihr Browser ist nicht in der Lage das entfernen der Formatierung auf die Auswahl zu beschränken. Solllen die Formatirungen aus dem kompletten Text entfernt werden?")) {
			if(tArea) {
				tArea.value = dostrip(tArea.value);
				tArea.focus();
			}
		}
	}
}

function dostrip(s) {
	var RegEx = /\[\/?(b|i|u|s|size|font|class|sub|sup|forecol|backcol|img|abbr|acronym|hr|br|email|anchor|link|filelink|list|\*|footnote|code|source|table|row|tr|cell|td|head|th)(=.*?| .*?)?\]/;
	while(RegEx.test(s)) {
		s = s.replace(RegEx, "");
	}
	return s;
}

function resetselstyle() {
	selstyle = '';
	var o 
	o = document.getElementById('classes');
	if(o) o.selectedIndex = 0;
	
	o = document.getElementById('stylebox');
	if(o) o.checked = false;
}

function showform(form, width, height, formname, qstr, resizeable)
{
	var qs = '';
	var c = null;
	qs = '&dedi=' + dedisess + '&sid=' + dedisess;
	if((qstr) && (qstr != '') && (qstr != null)) {
		qs += '&' + qstr;
	}
	//qs += ((qs == '') ? '?' : '&') + 'formname=' + formname;
	qs += '&' + 'formname=' + formname;
	//if(!resizeable) resizeable = false; // mag mir einer erklären wie ich auf sowas sinnloses komme? *gg*
	
	c = getCookie('bbcode_formheight_' + form);
	if(c !== null) height = parseInt(c);
	c = getCookie('bbcode_formwidth_' + form);
	if(c !== null) width = parseInt(c);
	
	var url = frontendfile + '?bbcodepopup=' + form + qs;
	if(openform != null) { openform.close(); }
	openform = window.open(url, 'openform', 'height=' + height + ',width=' + width + 'location=no,menubar=no,resizable=' + (resizeable ? 'yes' : 'no') + ',scrollbars=no,status=no,toolbar=no');
	openform.focus();
}

function bbcode(code, formname, par)
{
	switch(code)
	{
		case 'b':
			inserttags('[b]', '[/b]', formname);
		break;

		case 'i':
			inserttags('[i]', '[/i]', formname);
		break;

		case 'u':
			inserttags('[u]', '[/u]', formname);
		break;

		case 's':
			inserttags('[s]', '[/s]', formname);
		break;
		
		case 'size':
			if((par) && (par.value != null) && (par.value != '')) {
				inserttags('[size="' + par.value + '"]', '[/size]', formname);
			}
			par.selectedIndex = 0;
		break;
		
		case 'font':
			if((par) && (par.value != null) && (par.value != '')) {
				inserttags('[font="' + par.value + '"]', '[/font]', formname);
			}
			par.selectedIndex = 0;
		break;
		
		case 'class':
			var stylebox = document.getElementById('stylebox');
			if(((stylebox) && stylebox.checked) || !allowclasses) {
				if(par.value == '' && (stylebox)) stylebox.checked = false;
				selstyle = par.value;
			} else {
				if(allowclasses) {
					if((par) && (par.value != null) && (par.value != '')) {
						inserttags('[class="' + par.value + '"]', '[/class]', formname);
					}
					par.selectedIndex = 0;
				}
			}
		break;
		
		case 'stylebox':
			var classes = document.getElementById('classes');
			
			if(!par.checked) {
				if(classes) classes.selectedIndex = 0;
			}
		break;

		case 'sub':
			inserttags('[sub]', '[/sub]', formname);
		break;

		case 'sup':
			inserttags('[sup]', '[/sup]', formname);
		break;

		case 'forecol':
			showform('color', 250, 350, formname, 'type=forecol');
		break;

		case 'backcol':
			showform('color', 250, 350, formname, 'type=backcol');
		break;

		case 'email':
			showform('mail', 305, 135, formname, par);
		break;

		case 'anchor':
			showform('anchor', 300, 100, formname, par);
		break;

		case 'link':
			showform('link', 500, 230, formname, par, true);
		break;

		case 'filelink':
			showform('filelink', 400, 240, formname, par, true);
		break;

		case 'list':
			showform('list', 375, 240, formname);
		break;

		case 'code':
			inserttags('[code]', '[/code]', formname);
		break;

		case 'phpcode':
			inserttags('[code=php]', '[/code]', formname);
		break;

		case 'img':
			showform('img', 640, 230, formname, par, true);
		break;

		case 'hr':
			var s = '[hr';
			if(selstyle != '') s += ' style="' + selstyle + '"';
			s += ']';
			
			resetselstyle();
			inserttext(s, formname);
		break;

		case 'br':
			inserttext('[br]', formname);
		break;

		case 'abbr':
			showform('abbr', 280, 90, formname, 'type=abbr');
		break;

		case 'acronym':
			showform('abbr', 280, 90, formname, 'type=acronym');
		break;
		
		case 'footnote':
			inserttags('[footnote]', '[/footnote]', formname);
		break;

		case 'source':
			inserttags('[source]', '[/source]', formname);
		break;

		case 'br':
			inserttext('[br]', formname);
		break;

		case 'striptags':
			if(confirm("Sind sie sicher dass sie ALLE bbCode-Formatierungen im markierten Bereich zu entfernen?")) {
				striptags(formname);
			}
		break;

		case 'charmap':
			showform('charmap', 700, 665, formname);
		break;
		
		case 'table':
			//showform('table', 200, 110, formname);
			showform('table', 300, 150, formname);
		break;

		case 'help':
			window.open(dedipath + 'external/bbcode/help.php?feat=' + par, 'helpform', 'height=480,width=640,location=no,scrollbars=yes,menubar=no,toolbar=no');
		break;
	}
	return false;
}
