// Startup variables
var imageTag = false;
var theSelection = false;

// Browserüberprüfung & Betriebssystem & IE spezifische Codes
// More details from: http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
var clientPC = navigator.userAgent.toLowerCase(); // Infos Über Client
var clientVer = parseInt(navigator.appVersion); // Browserversion

var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
var is_nav = ((clientPC.indexOf('mozilla') != -1) && (clientPC.indexOf('spoofer') == -1) && (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera') == -1) && (clientPC.indexOf('webtv') == -1) && (clientPC.indexOf('hotjava') == -1));
var is_moz = 0;
var is_win = ((clientPC.indexOf("win") != -1) || (clientPC.indexOf("16bit") != -1));
var is_mac = (clientPC.indexOf("mac") != -1);

// Texte für die Fehlerabfrage
error1 = "Sie haben keinen Seiteninhalt eingegeben.";

// Texte für die Hilfezeile
standart_help = "Zeigen Sie mit der Maus auf ein Symbol um Hilfe zu erhalten";

alignleft_help = "Linksbündiger Text: [LEFT]Text[/LEFT]";
aligncenter_help = "Zentrierter Text: [CENTER]Text[/CENTER]";
alignright_help = "Rechtsbündiger Text: [RIGHT]Text[/RIGHT]";
alignjustify_help = "Blocksatz Text: [JUSTIFY]Text[/JUSTIFY]";

b_help = "Text in fett: [B]Text[/B]";
i_help = "Text in kursiv: [I]Text[/I]";
u_help = "Unterstrichener Text: [U]Text[/U]";

br_help = "Sichtbarer Zeilenumbruch: [BR]";

link_help = "URL/Link einfügen: [URL=adresse]Link[/URL]";
wiki_help = "DLW-Wiki Verweis: [WIKI=seite]Seite[/WIKI]";
img_help = "Bild einfügen: [IMG=adresse]";
quote_help = "Zitat einfügen: [QUOTE=von]Text[/QUOTE]";

h1_help = "Überschrift 1: [H1]Überschrift[/H1]";
h2_help = "Überschrift 2: [H2]Überschrift[/H2]";
h3_help = "Überschrift 3: [H3]Überschrift[/H3]";
h4_help = "Überschrift 4: [H4]Überschrift[/H4]";

hr_help = "Horizontale Linie: [HR]";

ul_help = "Ungeordnete Liste: [UL]Text[/UL]";
li_help = "Liste: [LI]Text[/LI]";

imgupload_help = "Bild hochladen (powered by my-images.eu)";
closetags_help = "Offenen Tag schließen";

fontsize_help = "Schriftgröße: <font size=\"ZAHL\">Text</font>";
fontcolor_help = "Schriftfarbe: <font color=\"Farbe Bsp.: #000000\">Text</font>";

sonderzeichen_help = "Sondezeichen einfügen: z.B. \", ', &, usw...";

q_help = "Zitat: [quote]Text[/quote] (alt+q)";
c_help = "Code anzeigen: [code]Code[/code] (alt+c)";
l_help = "Liste: [list]Text[/list] (alt+l)";
o_help = "Geordnete Liste: [list=]Text[/list] (alt+o)";
p_help = "Bild einfügen: [img]http://URL_des_Bildes[/img] (alt+p)";
w_help = "URL einfügen: [url]http://URL[/url] oder [url=http://url]URL Text[/url] (alt+w)";
a_help = "Alle offenen BBCodes schließen";
s_help = "Schriftfarbe: [color=red]Text[/color] Tipp: Du kannst ebenfalls color=#FF0000 benutzen";
f_help = "Schriftgröße: [size=x-small]Kleiner Text[/size]";

// Definition der Tags
hmtlcode = new Array();
htmltags = new Array('[LEFT]','[/LEFT]',
					 '[CENTER]','[/CENTER]',
					 '[RIGHT]','[/RIGHT]',
					 '[JUSTIFY]','[/JUSTIFY]',
					 '[B]','[/B]',
					 '[I]','[/I]',
					 '[U]','[/U]',
					 '[BR]','[BR]',
					 '[URL=]','[/URL]',
					 '[IMG=]','[IMG=]',
					 '[H1]','[/H1]',
					 '[H2]','[/H2]',
					 '[H3]','[/H§]',
					 '[H4]','[/H$]',
					 '[HR]','[HR]',
					 '[UL]','[/UL]',
					 '[LI]','[/LI]',
					 '[QUOTE=]','[/QUOTE]',
					 '[WIKI=]','[/WIKI]',
					 '','',
					 '','');
imageTag = false;

// Zeigen der Hilfenachricht
function helpline(help) {
	document.post.hilfe_box.value = eval(help + "_help");
}

// Array-Eigenschaften ersetzen
function getarraysize(thearray) {
	for (i = 0; i < thearray.length; i++) {
		if ((thearray[i] == "undefined") || (thearray[i] == "") || (thearray[i] == null))
			return i;
		}
	return thearray.length;
}

// Replacement for arrayname.push(value) not implemented in IE until version 5.5
// Appends element to the array
function arraypush(thearray,value) {
	thearray[ getarraysize(thearray) ] = value;
}

// Replacement for arrayname.pop() not implemented in IE until version 5.5
// Removes and returns the last element of an array
function arraypop(thearray) {
	thearraysize = getarraysize(thearray);
	retval = thearray[thearraysize - 1];
	delete thearray[thearraysize - 1];
	return retval;
}

function emoticon(text) {
	var txtarea = document.post.text;
	text = ' ' + text + ' ';
	if (txtarea.createTextRange && txtarea.caretPos) {
		var caretPos = txtarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
		txtarea.focus();
	} else {
		txtarea.value  += text;
		txtarea.focus();
	}
}

function checkForm() {
	formErrors = false;    

	if (document.post.text.value.length < 2) {
		formErrors = error1;
	}

	if (formErrors) {
		alert(formErrors);
		return false;
	} else {
		htmlstyle(-1);
		return true;
	}
}

function htmlfontstyle(htmlopentag, htmlclosetag) {
	var txtarea = document.post.text;

	if ((clientVer >= 4) && is_ie && is_win) {
		theSelection = document.selection.createRange().text;
		if (!theSelection) {
			txtarea.value += htmlopentag + htmlclosetag;
			txtarea.focus();
			return;
		}
		document.selection.createRange().text = htmlopentag + theSelection + htmlclosetag;
		txtarea.focus();
		return;
	}
	else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
	{
		mozWrap(txtarea, htmlopentag, htmlclosetag);
		return;
	}
	else
	{
		txtarea.value += htmlopentag + htmlclosetag;
		txtarea.focus();
	}
	storeCaret(txtarea);
}


function htmlstyle(htmlcodenumber) {
	var txtarea = document.post.text;

	donotinsert = false;
	theSelection = false;
	htmllast = 0;

	if (htmlcodenumber == -1) { // Close all open tags & default button names
		while (hmtlcode[0]) {
			buttonnumber = arraypop(hmtlcode) - 1;
			txtarea.value += htmltags[buttonnumber + 1];
			buttext = eval('document.post.addhtmlcode' + buttonnumber + '.value');
			eval('document.post.addhtmlcode' + buttonnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
		}
		imageTag = false; // All tags are closed including image tags :D
		txtarea.focus();
		return;
	}

	if ((clientVer >= 4) && is_ie && is_win)
	{
		theSelection = document.selection.createRange().text; // Get text selection
		if (theSelection) {
			// Add tags around selection
			document.selection.createRange().text = htmltags[htmlcodenumber] + theSelection + htmltags[htmlcodenumber+1];
			txtarea.focus();
			theSelection = '';
			return;
		}
	}
	else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
	{
		mozWrap(txtarea, htmltags[htmlcodenumber], htmltags[htmlcodenumber+1]);
		return;
	}
	
	// Find last occurance of an open tag the same as the one just clicked
	for (i = 0; i < hmtlcode.length; i++) {
		if (hmtlcode[i] == htmlcodenumber+1) {
			htmllast = i;
			donotinsert = true;
		}
	}

	if (donotinsert) {		// Close all open tags up to the one just clicked & default button names
		while (hmtlcode[htmllast]) {
				buttonnumber = arraypop(hmtlcode) - 1;
				txtarea.value += htmltags[buttonnumber + 1];
				buttext = eval('document.post.addhtmlcode' + buttonnumber + '.value');
				eval('document.post.addhtmlcode' + buttonnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
				imageTag = false;
			}
			txtarea.focus();
			return;
	} else { // Open tags
	
		if (imageTag && (htmlcodenumber != 14)) {		// Close image tag before adding another
			txtarea.value += htmltags[15];
			lastValue = arraypop(hmtlcode) - 1;	// Remove the close image tag from the list
			document.post.addhtmlcode14.value = "Img";	// Return button back to normal state
			imageTag = false;
		}
		
		// Open tag
		txtarea.value += htmltags[htmlcodenumber];
		if ((htmlcodenumber == 14) && (imageTag == false)) imageTag = 1; // Check to stop additional tags after an unclosed image tag
		arraypush(hmtlcode,htmlcodenumber+1);
		eval('document.post.addhtmlcode'+htmlcodenumber+'.value += "*"');
		txtarea.focus();
		return;
	}
	storeCaret(txtarea);
}

// From http://www.massless.org/mozedit/
function mozWrap(txtarea, open, close)
{
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd == 1 || selEnd == 2) 
		selEnd = selLength;

	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + open + s2 + close + s3;
	return;
}

// http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
function storeCaret(textEl) {
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}
