function SubmitPoll(){
	var lopt=0;
	for(i=0;i<document.poll.poll_option.length;++i){
		if(document.poll.poll_option[i].checked){lopt++;}
	}
	if(lopt==0){
		alert("Bitte markieren Sie eine Option bei der Umfrage!");
	}else{
		document.poll.submit();
	}
}

function AddEntry(txtSrc,lstDest) {
	var MAX_ANSWERS=30;
	var sMSG='';
	var txtAnswer	= txtSrc.value.replace(/\|/gi,"");
	
	if(document.message.poll_title.value.length == 0){
		sMSG=sMSG+'\nSie müssen noch eine Frage eingeben!';
	}
	
	if (lstDest.options.length <(MAX_ANSWERS)){
		if(txtSrc.value.length != 0) {
			var no = new Option();
			no.value		= txtAnswer+'|';
			no.text			= txtAnswer;
			lstDest.options[lstDest.options.length] = no;
			txtSrc.value = "";
			txtSrc.text = "";
			if(sMSG.length != 0){
				alert(sMSG);
			}
		}else{
			alert('Bitte geben Sie eine Auswahlantwort ein!'+sMSG);
		}
	}else{
		alert('Es sind maximal '+MAX_ANSWERS+' Auswahlantworten erlaubt!'+sMSG);
	}
}

function DeleteEntry(lstSrc) {
	if(lstSrc.selectedIndex != -1) {
		lstSrc.options[lstSrc.selectedIndex] = null;
	}else{
		alert('Bitte markieren Sie, welche Auswahlantwort gelöscht werden soll!');
	}
}

function DeleteAllEntrys(lstSrc) {
	var lOptions=lstSrc.options.length;
	for (i=0;i<=lOptions;i++){
		lstSrc.options[i] = null;
	}
	for (i=0;i<=lOptions;i++){
		lstSrc.options[i] = null;
	}
}

function MarkAll(lstSrc) {
	for(var i=0; i<lstSrc.options.length; i++) {
		lstSrc.options[i].selected = true;
	}
}

function Check(Eingabe) {
	var nur_das ="0123456789";
	for (var i = 0; i < Eingabe.length; i++)
		if (nur_das.indexOf(Eingabe.charAt(i))<0 ) return false;
	return true;
}

function CheckTopicPoll() {
	var sDuration=document.message.txtDuration.value;
	var bCheckDuration=Check(sDuration);
	if(document.message.i_title.value.length == 0){
		alert('Sie müssen einen Titel eingeben!');
		document.message.i_title.focus();
	}else if(document.message.i_text.value.length == 0){
		alert('Sie müssen einen Text eingeben!');
		document.message.i_text.focus();
	}else if(document.message.txtDuration.value.length == 0 || document.message.txtDuration.value == 0){
		alert('Bitte geben Sie die Laufzeit der Umfrage in Tagen an!\n\nWertebereich:\n1 bis maximal 365 Tage');
		document.message.txtDuration.focus();
	}else if(! bCheckDuration){
		alert('Bitte geben Sie die Laufzeit der Umfrage in Tagen an!\n\nWertebereich:\n1 bis maximal 365 Tage');
		document.message.txtDuration.focus();
	}else{
		if (document.message.poll_title.value.length != 0 && document.message.lstAnswers.options.length <= 1){
			alert('Umfrage müssen mindestens zwei Auswahlantworten haben!');
			document.message.txtAnswer.focus();
		}else{
			MarkAll(document.message.lstAnswers);
			document.message.submit();
		}
	}
}

function CheckTopic() {
	if(document.message.i_title.value.length == 0){
		alert('Sie müssen einen Titel eingeben!');
		document.message.i_title.focus();
	}else if(document.message.i_text.value.length == 0){
		alert('Sie müssen einen Text eingeben!');
		document.message.i_text.focus();
	}else{
		document.message.submit();
	}
}

// bbCode control by
// subBlue design
// www.subBlue.com

// Startup variables
var imageTag = false;
var theSelection = false;

// Check for Browser & Platform for PC & IE specific bits
// More details from: http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
var clientPC = navigator.userAgent.toLowerCase(); // Get client info
var clientVer = parseInt(navigator.appVersion); // Get browser version

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_win   = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
var is_mac    = (clientPC.indexOf("mac")!=-1);


// Helpline messages
b_help = "Text in fett: [b]Text[/b]  (alt+b)";
i_help = "Text in kursiv: [i]Text[/i]  (alt+i)";
u_help = "Unterstrichener Text: [u]Text[/u]  (alt+u)";
q_help = "Zitat: [quote]Text[/quote]  (alt+q)";
c_help = "Spoiler anzeigen: [spoiler]Spoiler[/spoiler]  (alt+s)";
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]  Tip: Du kannst ebenfalls color=#FF0000 benutzen";
f_help = "Schriftgröße: [size=x-small]Kleiner Text[/size]";

// Define the bbCode tags
bbcode = new Array();
bbtags = new Array('[b]','[/b]','[i]','[/i]','[u]','[/u]','[q]','[/q]','[spoiler]','[/spoiler]','[list]','[/list]','[list=]','[/list]','[img]','[/img]','[url]','[/url]');
imageTag = false;

// Shows the help messages in the helpline window
function helpline(help) {
	document.message.helpbox.value = eval(help + "_help");
}


// Replacement for arrayname.length property
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 checkForm() {

	formErrors = false;

	if (document.message.i_text.value.length < 2) {
		formErrors = "Zu einem Beitrag muss ein Text eingeben werden";
	}

	if (formErrors) {
		alert(formErrors);
		return false;
	} else {
		bbstyle(-1);
		//formObj.preview.disabled = true;
		//formObj.submit.disabled = true;
		return true;
	}
}

function emoticon(text) {
	text = ' ' + text + ' ';
	if (document.message.i_text.createTextRange && document.message.i_text.caretPos) {
		var caretPos = document.message.i_text.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
		document.message.i_text.focus();
	} else {
	document.message.i_text.value  += text;
	document.message.i_text.focus();
	}
}

function bbfontstyle(bbopen, bbclose) {
	if ((clientVer >= 4) && is_ie && is_win) {
		theSelection = document.selection.createRange().text;
		if (!theSelection) {
			document.message.i_text.value += bbopen + bbclose;
			document.message.i_text.focus();
			return;
		}
		document.selection.createRange().text = bbopen + theSelection + bbclose;
		document.message.i_text.focus();
		return;
	} else {
		document.message.i_text.value += bbopen + bbclose;
		document.message.i_text.focus();
		return;
	}
	storeCaret(document.message.i_text);
}


function bbstyle(bbnumber) {

	donotinsert = false;
	theSelection = false;
	bblast = 0;

	if (bbnumber == -1) { // Close all open tags & default button names
		while (bbcode[0]) {
			butnumber = arraypop(bbcode) - 1;
			document.message.i_text.value += bbtags[butnumber + 1];
			buttext = eval('document.message.addbbcode' + butnumber + '.value');
			eval('document.message.addbbcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
		}
		imageTag = false; // All tags are closed including image tags :D
		document.message.i_text.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 = bbtags[bbnumber] + theSelection + bbtags[bbnumber+1];
		document.message.i_text.focus();
		theSelection = '';
		return;
	}

	// Find last occurance of an open tag the same as the one just clicked
	for (i = 0; i < bbcode.length; i++) {
		if (bbcode[i] == bbnumber+1) {
			bblast = i;
			donotinsert = true;
		}
	}

	if (donotinsert) {		// Close all open tags up to the one just clicked & default button names
		while (bbcode[bblast]) {
				butnumber = arraypop(bbcode) - 1;
				document.message.i_text.value += bbtags[butnumber + 1];
				buttext = eval('document.message.addbbcode' + butnumber + '.value');
				eval('document.message.addbbcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
				imageTag = false;
			}
			document.message.i_text.focus();
			return;
	} else { // Open tags

		if (imageTag && (bbnumber != 14)) {		// Close image tag before adding another
			document.message.i_text.value += bbtags[15];
			lastValue = arraypop(bbcode) - 1;	// Remove the close image tag from the list
			document.message.addbbcode14.value = "Img";	// Return button back to normal state
			imageTag = false;
		}

		// Open tag
		document.message.i_text.value += bbtags[bbnumber];
		if ((bbnumber == 14) && (imageTag == false)) imageTag = 1; // Check to stop additional tags after an unclosed image tag
		arraypush(bbcode,bbnumber+1);
		eval('document.message.addbbcode'+bbnumber+'.value += "*"');
		document.message.i_text.focus();
		return;
	}
	storeCaret(document.message.i_text);
}

// Insert at Claret position. Code from
// http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
function storeCaret(textEl) {
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}
