//<script>
/*
* This script was created by Erik Arvidsson (erik@eae.net)
* for http://webfx.eae.net/license.html * * Created:		2000-??-??
* Updates: 2001-12-02	Added getXHTML, supportsXHTML, usebr, oneditinit,
* fix for no selection, bUI flag to execCommand method
* 2001-12-03 Fixed case of HTML node names to lowercase */
function switch_button() {
}

function printPreview(form, fieldname) {
    
    var text = fieldname.value;
    newtext = stripStuff (text);
    win = window.open('', 'poapup', 'resizable=yes, width=450, height=500, scrollbars=yes, toolbar = no, status = no');
    win.document.write("<html>\n<head>\n<title>Vorschau</title>\n<link rel=stylesheet type='text/css' href='templates/style.css'>\n<style>\n<!--\n\t.demotable,td.demo {border:1px solid #c0c0c0}//-->\n</style>\n</head>\n")
    win.document.write("<body>\n");
    win.document.write("" + newtext + "\n</body>\n</html>");
}

// funktion aus iw pro 11/01
function show_change() {
	var s;
	s = edit.QueryStatus(DECMD_BOLD);

	if (s == DECMDF_DISABLED || s == DECMDF_NOTSUPPORTED) {
		disable(document.all["bold"], "gray");
	} else if (s == DECMDF_ENABLED  || s == DECMDF_NINCHED) {
		enable(document.all["bold"], "unchecked");
	} else { // DECMDF_LATCHED
		disable(document.all["bold"], "checked");
	}
}
function RichEditSave(form,hiddenfeldname) {
    if(isHTMLMode) {
        // uncheck html quelltext checkbox
        form.quelltext.checked = false;
        setMode(false);
    }
    var edit = document.all.edit;
    var htmlSource = (edit.supportsXHTML ? edit.getXHTML() : edit.getHTML()) ;
    hiddenfeldname.value = htmlSource;
   //  alert (hiddenfeldname.value);
   
}

function ieVersion() {
    var ua = window.navigator.userAgent;
    var msieIndex = ua.indexOf("MSIE") ;
    if (msieIndex == -1) return 0;
    return ua.substring(msieIndex+5,msieIndex+6);
}
var isHTMLMode=false;
msg_alert_notHTML="Funktion z.Zt. nicht möglich. Sie befinden sich im HTML-Modus.\nEntfernen sie das Häkchen vor >HTML Quelltext<.";

function setMode(bMode)	{
    var toggleButtons = new Array (bold,italic,underline,rightalign,leftalign,centeralign,indent,outdent,ol,ul,remformat,bild,table,link);
	var sTmp;
  	isHTMLMode = bMode;

	if(isHTMLMode){
        for (i=0; i < toggleButtons.length; i++) {
	        disable(toggleButtons[i]);
	    }

		if (ieVersion() >= 5){
			sTmp = "" + edit.document.documentElement.outerHTML + "";
		}else{
			sTmp = "" + edit.document.body.outerHTML + "";
		}

		edit.document.body.innerText= sTmp;
	}else{
	    for (i=0; i < toggleButtons.length; i++) {
	        enable(toggleButtons[i]);
	    }
		sTmp=edit.document.body.innerText;
		if (ieVersion() >= 5){
			edit.document.body.innerHTML=sTmp;
		}else{
			edit.document.body.innerHTML=sTmp;
		}
	}
	edit.focus();
}

function UserDialog(what)
{
	if (isHTMLMode){alert(msg_alert_notHTML);return;}
	this.focus();
	this.document.execCommand(what, true);
	this.focus();
}
function doSelectClick(str, el) {
	var Index = el.selectedIndex;
	if (Index != 0){
		el.selectedIndex = 0;
		if (el.id == "specialtype")
			specialtype(el.options[Index].value);
		else
			execCommand(str,el.options[Index].value);
	}
}
var myedit;
var RangeType;

function specialtype(Mark){
	var strHTML;
	if (isHTMLMode){alert(msg_alert_notHTML);return;}
	if (ieVersion() >= 5) selectRange();

	if (RangeType == "Text"){
		strHTML = "<" + Mark + ">" + myedit.text + "</" + Mark + ">";
		myedit.pasteHTML(strHTML);
		edit.focus();
		myedit.select();
	}
}
function replaceStuff() {
    var sel = document.selection;
    if (sel!=null) {
        var rng = sel.createRange();
        if (rng!=null)
            rng.pasteHTML("<P><B>Selection has been replaced.</B></P>");
    }
}
function insertImage(UIN) {
    if (isHTMLMode){alert(msg_alert_notHTML);return;}
    var arr = showModalDialog("config/js/images.php?UIN="+UIN, "", "dialogWidth:40.5em; dialogHeight:380px; status:0;scroll:no");
      if (arr != null){
          string=arr;
          content=edit.document.body.innerHTML;
          content=content+string;
          edit.document.body.innerHTML=content;
      } else edit.focus();
}
function openOptionWindow(mId) {
    // var mId = form.id.value;
    // openBrWindow("config/js/options.php?id="+mId,"winName","")
    var arr = showModalDialog("config/js/options.php?id="+mId, "", "dialogWidth:640px; dialogHeight:540px; status:0;scroll:auto");
      if (arr != null){

          string=arr;
          alert (string);
         // content=edit.document.body.innerHTML;
          // content=content+string;
          // edit.document.body.innerHTML=content;
      } else edit.focus();

}

function fortable() {
  if (isHTMLMode){alert(msg_alert_notHTML);return;}
  var arr = showModalDialog("config/js/table.html", "", "dialogWidth:26.5em; dialogHeight:240px; status:0;scroll:no");

  if (arr != null){
  string=arr;
  content=edit.document.body.innerHTML;
  content=content+string;
  edit.document.body.innerHTML=content;
  }
  else edit.focus();
}
function forfield() {
  if (isHTMLMode){alert(msg_alert_notHTML);return;}
  edit.focus();
  string="<fieldset><legend>Fieldset</legend>Der Inhalt vom Fieldset</fieldset>";
  content=edit.document.body.innerHTML;
  content=content+string;
  edit.document.body.innerHTML=content;
  edit.focus();
}
function initRichEdit(el) {
	if (el.id) {	// needs an id to be accessible in the frames collection
		el.frameWindow = document.frames[el.id];
		if (el.value == null)
			el.value = el.innerHTML;

		if ( el.value.replace(/\s/g, "") == "" )
			el.value = "<?xml version=\"1.0\"?>\n" +

				"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" " +

				"\"DTD/xhtml1-transitional.dtd\">\n" +

				"<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n" +
				"<head>\n<title></title>\n</head>\n<body>\n</body>\n</html>";

		var d = el.frameWindow.document;
		d.designMode = "On";
		d.open();
		d.write(el.value);
		d.close();

		el.supportsXHTML = el.frameWindow.document.documentElement && el.frameWindow.document.childNodes != null;

		// set up the expandomethods

		// first some basic

		el.setHTML = function (sHTML) {
			el.value = sHTML;
			initRichEdit(el);
		}

		el.getHTML = function () {
			// notice that IE4 cannot get the document.documentElement so we'll use the body
			return el.frameWindow.document.body.innerHTML;
			// for IE5 the following is much better. If you don't want IE4 compatibilty modify this
			//  return el.frameWindow.document.documentElement.outerHTML;
		}

		el.getXHTML = function () {
			if (!el.supportsXHTML) {
				alert("Document root node cannot be accessed in IE4.x");
				return;
			}
			else if (typeof window.StringBuilder != "function") {
				alert("StringBuilder is not defined. Make sure to include stringbuilder.js");
				return;
			}


			var sb = new StringBuilder;

			// IE5 and IE55 has trouble with the document node

			var cs = el.frameWindow.document.childNodes;
			var l = cs.length;
			for (var i = 0; i < l; i++)
				_appendNodeXHTML(cs[i], sb);

			return sb.toString();
		};

		el.setText = function (sText) {
			el.value = sText.replace(/\&/g, "&amp;").replace(/\</g, "&lt;").replace(/\>/g, "&gt;").replace(/\n/g, "<br>");
			initRichEdit(el);
		}

		el.getText = function () {
			// notice that IE4 cannot get the document.documentElement so we'll use the body
			// not hat it matters when it comes to innerText :-)
			return el.frameWindow.document.body.innerText;
		}

		// and now some text manipulations

		el.execCommand = function (execProp, execVal, bUI) {
			return execCommand(this, execProp, execVal, bUI);
		}
		el.setBold = function () {
		    if (isHTMLMode){alert(msg_alert_notHTML);return;}
            return this.execCommand("bold");

		}
		el.setItalic = function () {
		    if (isHTMLMode){alert(msg_alert_notHTML);return;}
			return this.execCommand("italic");
		}
		el.setUnderline = function () {
		    if (isHTMLMode){alert(msg_alert_notHTML);return;}
			return this.execCommand("underline");
		}
		el.setBackgroundColor = function(sColor) {
		    if (isHTMLMode){alert(msg_alert_notHTML);return;}
			return this.execCommand("backcolor", sColor);
		}
		el.setColor = function(sColor) {
		    if (isHTMLMode){alert(msg_alert_notHTML);return;}
			return this.execCommand("forecolor", sColor);
		}

        el.setBackColor = function(sColor) {
		    if (isHTMLMode){alert(msg_alert_notHTML);return;}
			return this.execCommand("BackColor", sColor);
		}
        el.setCenterAlign = function () {
            if (isHTMLMode){alert(msg_alert_notHTML);return;}
		    return this.execCommand("justifycenter");
		}
		el.setRightAlign = function () {
		    if (isHTMLMode){alert(msg_alert_notHTML);return;}
		    return this.execCommand("justifyright");
		}
		el.setLeftAlign = function () {
		    if (isHTMLMode){alert(msg_alert_notHTML);return;}
		    return this.execCommand("justifyleft");
		}
        el.Cut = function () {
		    return this.execCommand("cut");
		}
		el.Copy = function () {
		    return this.execCommand("copy");
		}
		el.Paste = function () {
		    return this.execCommand("paste");
		}
		el.Undo = function () {
		    return this.execCommand("undo");
		}
		el.Redo = function () {
		    return this.execCommand("redo");
		}

		el.setOutdent = function () {
		    if (isHTMLMode){alert(msg_alert_notHTML);return;}
		    return this.execCommand("outdent");
		}
        el.setIndent = function () {
            if (isHTMLMode){alert(msg_alert_notHTML);return;}
		    return this.execCommand("indent");
		}
		el.setHyperlink = function () {
		    if (isHTMLMode){alert(msg_alert_notHTML);return;}
		    return this.execCommand("CreateLink");
		}
		el.setOrderList = function () {
		    if (isHTMLMode){alert(msg_alert_notHTML);return;}
		    return this.execCommand("insertorderedlist");
		}
		el.setUnorderlist = function () {
		    if (isHTMLMode){alert(msg_alert_notHTML);return;}
		    return this.execCommand("insertunorderedlist");
		}

		el.RemoveFormat = function () {
		    if (isHTMLMode){alert(msg_alert_notHTML);return;}
		    return this.execCommand("RemoveFormat");
		}
        el.setFormatBlock = function (sFormat) {
		    if (isHTMLMode){alert(msg_alert_notHTML);return;}
		    return this.execCommand("FormatBlock",sFormat,true);
		}


        el.setSize = function (Size) {
		    if (isHTMLMode){alert(msg_alert_notHTML);return;}
		    return this.execCommand("FontSize",Size,false);
		}
		el.setFont = function (Face) {
		    if (isHTMLMode){alert(msg_alert_notHTML);return;}
		    return this.execCommand("FontName",Face,false);
		}

		/* modifies the enter keyup event to generate BRs. */

		/* Enabled by default */

		if (el.getAttribute("usebr")) {

			el.frameWindow.document.onkeydown = function () {
				if (el.frameWindow.event.keyCode == 13) {	// ENTER
					var sel = el.frameWindow.document.selection;
					if (sel.type == "Control")
						return;

					var r = sel.createRange();
					r.pasteHTML("<br />");
					el.frameWindow.event.cancelBubble = true;
					el.frameWindow.event.returnValue = false;

					r.select();
					r.moveEnd("character", 1);
					r.moveStart("character", 1);
					r.collapse(false);

					return false;
				}
			};
			el.frameWindow.document.onkeypress =
			el.frameWindow.document.onkeyup = function () {
				if (el.frameWindow.event.keyCode == 13) {	// ENTER
					el.frameWindow.event.cancelBubble = true;
					el.frameWindow.event.returnValue = false;
					return false;
				}
			};
		}

		// Add your own or use the execCommand method.
		// See msdn.microsoft.com for commands

		// call oneditinit if defined
		if (typeof el.oneditinit == "string")
			el.oneditinit = new Function(el.oneditinit);
		if (typeof el.oneditinit == "function")
			el.oneditinit();
	}

	function execCommand(el, execProp, execVal, bUI) {
		var doc = el.frameWindow.document;
		var type = doc.selection.type;
		var oTarget = type == "None" ? doc : doc.selection.createRange();
		var r = oTarget.execCommand(execProp, bUI, execVal);
		if (type == "Text")
			oTarget.select();
		return r;
	}
}

function _appendNodeXHTML(node, sb) {



	function fixAttribute(s) {
		return String(s).replace(/\&/g, "&amp;").replace(/>/g, "&gt;").replace(/</g, "&lt;").replace(/\"/g, "&quot;");
	}



	function fixText(s) {
		return String(s).replace(/\&/g, "&amp;").replace(/>/g, "&gt;").replace(/</g, "&lt;");
	}



	switch (node.nodeType) {
		case 1:	// ELEMENT

			if (node.nodeName == "!") {	// IE5.0 and IE5.5 are weird
				sb.append(node.text);
				break;
			}

			var name = node.nodeName;
			if (node.scopeName == "HTML")
				name = name.toLowerCase();

			sb.append("<" + name);

			// attributes
			var attrs = node.attributes;
			var l = attrs.length;
			for (var i = 0; i < l; i++) {
				if (attrs[i].specified) {
					if (attrs[i].nodeName != "style")
						sb.append(" " + attrs[i].nodeName + "=\"" + fixAttribute(attrs[i].nodeValue) + "\"");
					else
						sb.append(" style=\"" + fixAttribute(node.style.cssText) + "\"");
				}
			}

			if (node.canHaveChildren || node.hasChildNodes()) {

				sb.append(">");

				// childNodes
				var cs = node.childNodes;
				l = cs.length;
				for (var i = 0; i < l; i++)
					_appendNodeXHTML(cs[i], sb);

				sb.append("</" + name + ">");
			}
			else if (name == "script")
				sb.append(">" + node.text + "</" + name + ">");
			else if (name == "title" || name == "style" || name == "comment")
				sb.append(">" + node.innerHTML + "</" + name + ">");
			else
				sb.append(" />");

			break;

		case 3:	// TEXT
			sb.append( fixText(node.nodeValue) );
			break;

		case 4:
			sb.append("<![CDA" + "TA[\n" + node.nodeValue + "\n]" + "]>");
			break;

		case 8:
			//sb.append("<!--" + node.nodeValue + "-->");
			sb.append(node.text);
			if (/(^<\?xml)|(^<\!DOCTYPE)/.test(node.text) )
				sb.append("\n");
			break;

		case 9:	// DOCUMENT
			// childNodes
			var cs = node.childNodes;
			l = cs.length;
			for (var i = 0; i < l; i++)
				_appendNodeXHTML(cs[i], sb);
			break;

		default:
			sb.append("<!--\nNot Supported:\n\n" + "nodeType: " + node.nodeType + "\nnodeName: " + node.nodeName + "\n-->");
	}

}

function initAllRichEdits() {
	var iframes = document.all.tags("IFRAME");

	for (var i=0; i<iframes.length; i++) {
		if (iframes[i].className == "richEdit")
			initRichEdit(iframes[i]);
	}
}



if (window.attachEvent)	// IE5
	window.attachEvent("onload", initAllRichEdits)
else if (document.all)	// IE4
	window.onload = initAllRichEdits;

