zaehler=0;
automatik_bei=50;
autobreite_merker='true';
max_beitrag=65535;
var ie=document.all?true:false;
var ns=document.layers?true:false;
var offset=50;
var lfd_hilfe_height=210;
var lfd_hilfe_height_offset=20;
var neue_pos;

function smilies_umwandeln(text) {
	text=text.replace(/(\s:\-?\))/g,"<img src='smile1.gif' width=13 height=13 border=0 hspace=3 alt='$1'>");
	text=text.replace(/(\s:\-?\()/g,"<img src='smile2.gif' width=13 height=13 border=0 hspace=3 alt='$1'>");
	text=text.replace(/(\s;\-?\))/g,"<img src='smile3.gif' width=13 height=13 border=0 hspace=3 alt='$1'>");
	text=text.replace(/(\s:\-?D)/g,"<img src='smile4.gif' width=13 height=13 border=0 hspace=3 alt='$1'>");
	text=text.replace(/(\s:\-?\|)/g,"<img src='smile5.gif' width=13 height=13 border=0 hspace=3 alt='$1'>");
	text=text.replace(/(\sX\-?\|)/g,"<img src='smile6.gif' width=13 height=13 border=0 hspace=3 alt='$1'>");
	text=text.replace(/(\sX\-?\()/g,"<img src='smile7.gif' width=13 height=13 border=0 hspace=3 alt='$1'>");
	text=text.replace(/(\s&gt;\-?\|)/g,"<img src='smile8.gif' width=13 height=13 border=0 hspace=3 alt='$1'>");
	text=text.replace(/(\s&gt;\-?\()/g,"<img src='smile9.gif' width=13 height=13 border=0 hspace=3 alt='$1'>");
	return text;
}

function vorschau_formatieren(oQuelle,oZiel,bBedingung1,bBedingung2) {
	if(oQuelle.value.length>max_beitrag)oQuelle.value=oQuelle.value.substring(0,max_beitrag);
	speichereCursor(oQuelle);
	if(!bBedingung1)return;
	if(oQuelle.parentNode.scrollHeight>lfd_hilfe_height+lfd_hilfe_height_offset) {
		if(document.getElementsByName("lfd_hilfe")[0].style.visibility=='hidden') {
			document.getElementsByName("lfd_hilfe")[0].style.visibility='visible';
			gleiten();
		}
	}else{
		if(document.getElementsByName("lfd_hilfe")[0].style.visibility=='visible')
			document.getElementsByName("lfd_hilfe")[0].style.visibility='hidden';
	}
	text=oQuelle.value
	text=text.replace(/<(.*?)>/g,"&lt;$1&gt;");
	text=text.replace(/\r/g,"");
	text=text.replace(/^\n+/,"");
	text=text.replace(/\n+$/,"");
	text=text.replace(/(^|\n)[ \t]*(&lt;.+&gt;)[ \t]*((>[ \t]*)+)/gi,"$1$3 $2");
	text=text.replace(/</g,"&lt;");
	text=text.replace(/>/g,"&gt;");
	for(i=0;i<=1;i++){
		text=text.replace(/(^|\n)(([ \t]*&gt;){4,}.+)(\n|$)/gi,"$1<font color=#fadad5>$2</font>$4");
		text=text.replace(/(^|\n)(([ \t]*&gt;){3}.+)(\n|$)/gi,"$1<font color=#e3a194>$2</font>$4");
		text=text.replace(/(^|\n)(([ \t]*&gt;){2}.+)(\n|$)/gi,"$1<font color=#ac4d38>$2</font>$4");
		text=text.replace(/(^|\n)([ \t]*&gt;.+)(\n|$)/gi,"$1<font color=#661809>$2</font>$3");
	}
	text=text.replace(/([ \t]*\n){3,}/g,"\n\n");
	text=text.replace(/\n/g,"<br>\n");
	text=text.replace(/  /g,"&nbsp; ");
	text=text.replace(/\t/g,"&nbsp; &nbsp; &nbsp; ");
	text=smilies_umwandeln(text);
	text=text.replace(/&lt;(\/?b)&gt;/gi,"<$1>");
	text=text.replace(/&lt;(\/?i)&gt;/gi,"<$1>");
	text=text.replace(/&lt;(\/?u)&gt;/gi,"<$1>");
	text=text.replace(/&lt;(\/?s)&gt;/gi,"<$1>");
	text=text.replace(/&lt;(\/?big)&gt;/gi,"<$1>");
	text=text.replace(/&lt;(\/?small)&gt;/gi,"<$1>");
	text=text.replace(/&lt;(\/?tt)&gt;/gi,"<$1>");
	text=text.replace(/&lt;(\/?center)&gt;/gi,"<$1>");	
	text=text.replace(/http:\/\/([\/a-zA-Z0-9_\-\?@#,\.:;%\!\+=&~\^\{\}\[\]]{6,})[^\(\n]{1,3}\(([^\(\)\n]{2,240})\)/gi,"<a href='http://$1' target='_blank' onMouseOver='window.status=\"Adresse  $1  in neuem Fenster &ouml;ffnen\";return true;' onMouseOut='window.status=\"\";'>$2</a>");
	text=text.replace(/http:\/\/(?![\/a-zA-Z0-9_\-\?@#,\.:;%\!\+=&~\^\{\}\[\]]{6,}'[^<>\n]+>)([\/a-zA-Z0-9_\-\?@#,\.:;%\!\+=&~\^\{\}\[\]]{6,})/gi,"<a href='http://$1' target='_blank' onMouseOver='window.status=\"Adresse  $1  in neuem Fenster &ouml;ffnen\";return true;' onMouseOut='window.status=\"\";'>http://$1</a>");
	text=text.replace(/mailto:([a-zA-Z0-9_\-\.%]+@[a-zA-Z0-9_\-\.]{6,})[^\(\n]{1,3}\(([^\(\)\n]{2,70})\)/gi,"<a href=\"mailto:%20$1\" onMouseOver=\"window.status='eMail an  $1  verfassen';return true;\" onMouseOut=\"window.status='';\">$2</a>");
	text=text.replace(/mailto:(?![a-zA-Z0-9_\-\.%]+@[a-zA-Z0-9_\-\.]{6,}"[^<>\n]+>)([a-zA-Z0-9_\-\.%]+@[a-zA-Z0-9_\-\.]{6,})/gi,"<a href=\"mailto:%20$1\" onMouseOver=\"window.status='eMail an  $1  verfassen';return true;\" onMouseOut=\"window.status='';\">$1</a>");
	oZiel.innerHTML=text;
	zaehler++;
	if(zaehler>=automatik_bei) {
		zaehler=0;
		automatische_breite(oQuelle,oZiel,bBedingung2);
	}
}

function spaltenbreite(spalte_kleiner,spalte_groesser,veraenderung_prozent,minimum) {
	oSpalten=document.getElementsByName("spalte");
	spklw=parseInt(oSpalten[spalte_kleiner].width);
	diff=(spklw-veraenderung_prozent>=minimum)?veraenderung_prozent:spklw-minimum;
	diff=(diff<0)?0:diff;
	if(diff>0) {
		spklwn=spklw-diff;
		spgrw=parseInt(oSpalten[spalte_groesser].width);
		spgrwn=spgrw+diff;
		oSpalten[spalte_kleiner].width=spklwn+"%";
		oSpalten[spalte_groesser].width=spgrwn+"%";
		return true;
	}else{
		return false;
	}
}

function automatische_breite(oQuelle,oZiel,bBedingung) {
	if(!bBedingung)return;
	h1=oQuelle.scrollHeight;
	h2=oZiel.scrollHeight;
	ok=true;
	rt=(h1<h2)?1:-1;
	while((h1-20)*rt<h2*rt&&ok) {
		ok=spaltenbreite((rt>0)?1:2,(rt<0)?1:2,1,25);
		h1=oQuelle.scrollHeight;
		h2=oZiel.scrollHeight;
	}
}

function positionierung() {
	document.verfassen.message.focus();
	document.anchors["absenden"].scrollIntoView(false);
	window.scrollBy(-100,0);
}

function zitat_formatieren(text,istHTML) {
	if(istHTML) {
		text=text.replace(/\r/g,"");
		text=text.replace(/\n/g,"");
		text=text.replace(/&gt;([\w\s])/gi,">$1");
		text=text.replace(/<br>/gi,"\n");
		text=text.replace(/<\/p>/gi,"\n");
		text=text.replace(/<\/?font[^>]{0,20}?>/gi,"");
		text=text.replace(/<img [^>]* alt=['"]?(.{2,3})["']?[^>]*>/gi,"$1");
		text=text.replace(/<(\/?b)>/gi,"&lt;$1&gt;");
		text=text.replace(/<(\/?i)>/gi,"&lt;$1&gt;");
		text=text.replace(/<(\/?u)>/gi,"&lt;$1&gt;");
		text=text.replace(/<(\/?s)>/gi,"&lt;$1&gt;");
		text=text.replace(/<(\/?big)>/gi,"&lt;$1&gt;");
		text=text.replace(/<(\/?small)>/gi,"&lt;$1&gt;");
		text=text.replace(/<(\/?tt)>/gi,"&lt;$1&gt;");
		text=text.replace(/<(\/?center)>/gi,"&lt;$1&gt;");	
		text=text.replace(/<(.*?)>/g,"");
		text=text.replace(/&lt;(\/?b)&gt;/gi,"<$1>");
		text=text.replace(/&lt;(\/?i)&gt;/gi,"<$1>");
		text=text.replace(/&lt;(\/?u)&gt;/gi,"<$1>");
		text=text.replace(/&lt;(\/?s)&gt;/gi,"<$1>");
		text=text.replace(/&lt;(\/?big)&gt;/gi,"<$1>");
		text=text.replace(/&lt;(\/?small)&gt;/gi,"<$1>");
		text=text.replace(/&lt;(\/?tt)&gt;/gi,"<$1>");
		text=text.replace(/&lt;(\/?center)&gt;/gi,"<$1>");	
		text=text.replace(/\n*$/g,"");
	}
	text=text.replace(/(\r?\n)+/g,"\n> ");
	text="\n> "+text+"\n";
	return text;
}

function zitat_holen() {
	if(document.selection) {
		oAuswahl=document.selection.createRange()
		zitat=oAuswahl.htmlText;
		if(zitat) {
			zitattext=oAuswahl.text;
			oNachricht=document.getElementById("nachricht").innerText;
			if(oNachricht.indexOf(zitattext)>=0) {
				infotext="<div align=center>markierter Beitragstext: "+zitattext.length+" Zeichen &#150; ";
				zAnteil=Math.floor(150000/zitattext.length);
				if(zitattext.length>1500)
					infotext+="hier nur "+zAnteil+"% dargestellt";
				else
					infotext+="wird automatisch auskommentiert";
				overlib("<div align=justify>"+zitat.substring(0,1500)+"</div>",WIDTH,500,CAPTION,infotext+"</div>",STATUS,"");
			}else{
				overlib("Textmarkierung ung&uuml;ltig",FGCOLOR,"#ffcccc",WIDTH,165,STATUS,"");
				zitat=null;
			}
		}else
			overlib("Kein Text markiert",FGCOLOR,"#ffcccc",WIDTH,130,STATUS,"");
		return true;
	}else{
		zitat=document.getSelection();
		if(zitat){
			infotext="<div align=center>markierter Text: "+zitat.length+" Zeichen &#150; ";
			zAnteil=Math.floor(150000/zitat.length);
			if(zitat.length>1500)
				infotext+="hier nur "+zAnteil+"% dargestellt";
			else
				infotext+="wird automatisch auskommentiert";
			overlib("<div align=justify>"+zitat.substring(0,1500).replace(/\n/g,"<br>")+"</div>",WIDTH,500,CAPTION,infotext+"</div>",STATUS,"");
		}else
			overlib("Kein Text markiert",FGCOLOR,"#ffcccc",WIDTH,130,STATUS,"");
		return false;
	}
}

function speichereCursor(oTextfeld) {
	if(oTextfeld.createTextRange) {
		oTextfeld.cursor=document.selection.createRange().duplicate();
	}
}

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;
}

function tagClick(oTag,vTag,vFarbe1,vFarbe2) {
	if(oBeitrag1.createTextRange&&oBeitrag1.cursor) {
		if(oBeitrag1.cursor.text.length>0) {
			oBeitrag1.cursor.text="<"+vTag+">"+oBeitrag1.cursor.text+"</"+vTag+">";
		}else{
			if(oTag.style.backgroundColor==vFarbe2) {
				vTag="</"+vTag+">";
				oTag.style.backgroundColor=vFarbe1;
				oTag.style.borderStyle="outset";
			}else{
				vTag="<"+vTag+">";
				oTag.style.backgroundColor=vFarbe2;
				oTag.style.borderStyle="inset";
			}
			oBeitrag1.cursor.text=vTag;
		}
	}else if(oBeitrag1.selectionEnd&&(oBeitrag1.selectionEnd-oBeitrag1.selectionStart>0)) {
		mozWrap(oBeitrag1,"<"+vTag+">","</"+vTag+">");
	}else{
		if(oTag.style.backgroundColor==vFarbe2) {
			vTag="</"+vTag+">";
			oTag.style.backgroundColor=vFarbe1;
			oTag.style.borderStyle="outset";
		}else{
			vTag="<"+vTag+">";
			oTag.style.backgroundColor=vFarbe2;
			oTag.style.borderStyle="inset";
		}
		if(oBeitrag1.selectionEnd)
			mozWrap(oBeitrag1,vTag,"");
		else
			oBeitrag1.value+=vTag;
	}
	speichereCursor(oBeitrag1);
	vorschau_formatieren(oBeitrag1,oBeitrag2,document.verfassen.update.checked,document.verfassen.autobreite.checked);
	if(oBeitrag1.cursor) {
		oBeitrag1.cursor.select();
		if(navigator.userAgent.toLowerCase().indexOf('msie')>-1&&navigator.userAgent.toLowerCase().indexOf('opera')==-1)
			oBeitrag1.setActive();
		automatische_breite(oBeitrag1,oBeitrag2,document.verfassen.autobreite.checked);
	}else
		oBeitrag1.focus();
}

function tagOut(oTag) {
	oTag.style.borderStyle="solid";
	return nd();
}

function tagOver(oTag,vFarbe1,vTip) {
	if(oTag.style.backgroundColor==vFarbe1) {
		oTag.style.borderStyle="outset";
	}else{
		oTag.style.borderStyle="inset";
	}
	return overlib(vTip,WIDTH,30,STATUS,"");
}

function smilyClick(vSmily) {
	if(oBeitrag1.createTextRange&&oBeitrag1.cursor) {
		oBeitrag1.cursor.text=oBeitrag1.cursor.text.charAt(oBeitrag1.cursor.text.length-1)==' '?vSmily+' ':vSmily;
	}else{
		oBeitrag1.value+=vSmily;
	}
	vorschau_formatieren(oBeitrag1,oBeitrag2,document.verfassen.update.checked,document.verfassen.autobreite.checked);
	if(oBeitrag1.cursor&&navigator.userAgent.toLowerCase().indexOf('msie')>-1&&navigator.userAgent.toLowerCase().indexOf('opera')==-1)
		oBeitrag1.setActive();
	else
		oBeitrag1.focus();
}

function smilyOut(oTag) {
	oTag.style.borderStyle="solid";
	return nd();
}

function smilyOver(oTag,vTip) {
	oTag.style.borderStyle="outset";
	return overlib(vTip,WIDTH,30,STATUS,"");
}

function imageClick() {
	if(oBeitrag1.createTextRange&&oBeitrag1.cursor) {
		oBeitrag1.cursor.text='<img align=center>';
	}else{
		oBeitrag1.value+='<img align=center>';
	}
	vorschau_formatieren(oBeitrag1,oBeitrag2,document.verfassen.update.checked,document.verfassen.autobreite.checked);
	if(oBeitrag1.cursor&&navigator.userAgent.toLowerCase().indexOf('msie')>-1&&navigator.userAgent.toLowerCase().indexOf('opera')==-1)
		oBeitrag1.setActive();
	else
		oBeitrag1.focus();
}

function gleiten(){
	if(ns){
  		var pos=document.anchors['pos_lfd_hilfe'];
  		var posi=pos.y+lfd_hilfe_height_offset;
		var posix=pos.x;
  		var pos_ende=document.anchors['pos_lfd_hilfe_ende'];
  		var posi_ende=pos_ende.y;
  		document.layers['lfd_hilfe'].pageX=posix;
  		if(window.pageYOffset+offset>posi)		
			neue_pos=window.pageYOffset+offset;
		else  	
			neue_pos=posi;  
		if(window.pageYOffset+offset+lfd_hilfe_height>posi_ende)
			neue_pos=posi_ende-lfd_hilfe_height;
		document.layers['lfd_hilfe'].pageY=neue_pos;  
		if(document.layers['lfd_hilfe'].visibility=='show')
			setTimeout('gleiten()',50);
	}
	if(ie){
		var pos=document.all.pos_lfd_hilfe;
		posi=getTop(pos)+lfd_hilfe_height_offset;
		posx=getLeft(pos);
		var pos_ende=document.all.pos_lfd_hilfe_ende;
		posi_ende=getTop(pos_ende);
		document.all['lfd_hilfe'].style.posLeft=posx;
		if(document.body.scrollTop+offset<posi)
			neue_pos=posi;
		else
			neue_pos=document.body.scrollTop+offset;
		if(document.body.scrollTop+offset+lfd_hilfe_height>posi_ende)
			neue_pos=posi_ende-lfd_hilfe_height;
		document.all['lfd_hilfe'].style.posTop=neue_pos;
		if(document.all['lfd_hilfe'].style.visibility=='visible')
			setTimeout('gleiten()',50);
	}
}
function getLeft(l){
  if(l.offsetParent)return(l.offsetLeft+getLeft(l.offsetParent));
  else return(l.offsetLeft);
}
function getTop(l){
	if(l.offsetParent)return(l.offsetTop+getTop(l.offsetParent));
	else return(l.offsetTop);
}


