// Copyright 2001-2003 Interakt Online. All rights reserved.
	
if (Ktml_mozilla) {
	DECMD_BOLD =                      "bold";
	DECMD_COPY =                      "copy";
	DECMD_CUT =                       "cut";
	DECMD_DELETE =                    5004;
	DECMD_DELETECELLS =               5005;
	DECMD_DELETECOLS =                5006;
	DECMD_DELETEROWS =                5007;
	DECMD_FINDTEXT =                  5008;
	DECMD_FONT =                      "fontname";
	DECMD_GETBACKCOLOR =              "backcolor";
	DECMD_GETBLOCKFMT =               5011;
	DECMD_GETBLOCKFMTNAMES =          5012;
	DECMD_GETFONTNAME =               "fontname";
	DECMD_GETFONTSIZE =               "fontsize";
	DECMD_GETFORECOLOR =              "forecolor";
	DECMD_HYPERLINK =                 "createlink";
	DECMD_ANCHORLINK =                "createbookmark";
	DECMD_IMAGE =                     "insertimage";
	DECMD_INDENT =                    "indent";
	DECMD_INSERTCELL =                5019;
	DECMD_INSERTCOL =                 5020;
	DECMD_INSERTROW =                 5021;
	DECMD_INSERTTABLE =               5022;
	DECMD_INSERTHR =               	  "inserthorizontalrule";
	DECMD_ITALIC =                    "italic";
	DECMD_JUSTIFYCENTER =             "justifycenter";
	DECMD_JUSTIFYLEFT =               "justifyleft";
	DECMD_JUSTIFYRIGHT =              "justifyright";
	DECMD_JUSTIFYFULL  = 			  			"justifyfull";
	DECMD_LOCK_ELEMENT =              5027;
	DECMD_MAKE_ABSOLUTE =             5028;
	DECMD_MERGECELLS =                5029;
	DECMD_ORDERLIST =                 "insertorderedlist";
	DECMD_OUTDENT =                   "outdent";
	DECMD_PASTE =                     "paste";
	DECMD_REDO =                      "redo";
	DECMD_REMOVEFORMAT =              "removeformat";
	DECMD_SELECTALL =                 "selectall";
	DECMD_SEND_BACKWARD =             5036;
	DECMD_BRING_FORWARD =             5037;
	DECMD_SEND_BELOW_TEXT =           5038;
	DECMD_BRING_ABOVE_TEXT =          5039;
	DECMD_SEND_TO_BACK =              5040;
	DECMD_BRING_TO_FRONT =            5041;
	DECMD_SETBACKCOLOR =              "backcolor";
	DECMD_SETBLOCKFMT =               "formatblock";
	DECMD_SETFONTNAME =               "fontname";
	DECMD_SETFONTSIZE =               "fontsize";
	DECMD_SETFORECOLOR =              "forecolor";
	DECMD_SPLITCELL =                 5047;
	DECMD_UNDERLINE =                 "underline";
	DECMD_UNDO =                      "undo";
	DECMD_UNLINK =                    "unlink";
	DECMD_UNANCHOR =                  "unBookmark";
	DECMD_UNORDERLIST =               "insertunorderedlist";
	DECMD_PROPERTIES =                "";
	
	
	
	// OLECMDEXECOPT  
	OLECMDEXECOPT_DODEFAULT =         false;
	OLECMDEXECOPT_PROMPTUSER =        true;
	OLECMDEXECOPT_DONTPROMPTUSER =    false;
	
	// DHTMLEDITCMDF
	DECMDF_NOTSUPPORTED =             false;
	DECMDF_DISABLED =                 false;
	DECMDF_ENABLED =                  true;
	DECMDF_LATCHED =                  7;
	DECMDF_NINCHED =                  11;
	
	// DHTMLEDITAPPEARANCE
	DEAPPEARANCE_FLAT =               0;
	DEAPPEARANCE_3D =                 1;
	
	// OLE_TRISTATE
	OLE_TRISTATE_UNCHECKED =          0;
	OLE_TRISTATE_CHECKED =            1;
	OLE_TRISTATE_GRAY =               2;
}
if (Ktml_ie) {
	DECMD_BOLD =                      "Bold";
	DECMD_COPY =                      "Copy";
	DECMD_CUT =                       "Cut";
	DECMD_DELETE =                    5004;
	DECMD_DELETECELLS =               5005;
	DECMD_DELETECOLS =                5006;
	DECMD_DELETEROWS =                5007;
	DECMD_FINDTEXT =                  5008;
	DECMD_FONT =                      "FontName";
	DECMD_GETBACKCOLOR =              "BackColor";
	DECMD_GETBLOCKFMT =               5011;
	DECMD_GETBLOCKFMTNAMES =          5012;
	DECMD_GETFONTNAME =               "FontName";
	DECMD_GETFONTSIZE =               "FontSize";
	DECMD_GETFORECOLOR =              "ForeColor";
	DECMD_HYPERLINK =                 "CreateLink";
	DECMD_ANCHORLINK =                "CreateBookmark";
	DECMD_IMAGE =                     "InsertImage";
	DECMD_INDENT =                    "Indent";
	DECMD_INSERTCELL =                5019;
	DECMD_INSERTCOL =                 5020;
	DECMD_INSERTROW =                 5021;
	DECMD_INSERTTABLE =               5022;
	DECMD_INSERTHR =               	  "InsertHorizontalRule";
	DECMD_ITALIC =                    "Italic";
	DECMD_JUSTIFYCENTER =             "JustifyCenter";
	DECMD_JUSTIFYLEFT =               "JustifyLeft";
	DECMD_JUSTIFYRIGHT =              "JustifyRight";
	DECMD_JUSTIFYFULL =              "JustifyFull";
	DECMD_LOCK_ELEMENT =              5027;
	DECMD_MAKE_ABSOLUTE =             5028;
	DECMD_MERGECELLS =                5029;
	DECMD_ORDERLIST =                 "InsertOrderedList";
	DECMD_OUTDENT =                   "Outdent";
	DECMD_PASTE =                     "Paste";
	DECMD_REDO =                      "Redo";
	DECMD_REMOVEFORMAT =              "RemoveFormat";
	DECMD_SELECTALL =                 "SelectAll";
	DECMD_SEND_BACKWARD =             5036;
	DECMD_BRING_FORWARD =             5037;
	DECMD_SEND_BELOW_TEXT =           5038;
	DECMD_BRING_ABOVE_TEXT =          5039;
	DECMD_SEND_TO_BACK =              5040;
	DECMD_BRING_TO_FRONT =            5041;
	DECMD_SETBACKCOLOR =              "BackColor";
	DECMD_SETBLOCKFMT =               "FormatBlock";
	DECMD_SETFONTNAME =               "FontName";
	DECMD_SETFONTSIZE =               "FontSize";
	DECMD_SETFORECOLOR =              "ForeColor";
	DECMD_SPLITCELL =                 5047;
	DECMD_UNDERLINE =                 "Underline";
	DECMD_UNDO =                      "Undo";
	DECMD_UNLINK =                    "Unlink";
	DECMD_UNANCHOR =                  "UnBookmark";
	DECMD_UNORDERLIST =               "InsertUnorderedList";
	DECMD_PROPERTIES =                "";
	
	
	
	// OLECMDEXECOPT  
	OLECMDEXECOPT_DODEFAULT =         false;
	OLECMDEXECOPT_PROMPTUSER =        true;
	OLECMDEXECOPT_DONTPROMPTUSER =    false;
	
	// DHTMLEDITCMDF
	DECMDF_NOTSUPPORTED =             false;
	DECMDF_DISABLED =                 false;
	DECMDF_ENABLED =                  true;
	DECMDF_LATCHED =                  7;
	DECMDF_NINCHED =                  11;
	
	// DHTMLEDITAPPEARANCE
	DEAPPEARANCE_FLAT =               0;
	DEAPPEARANCE_3D =                 1;
	
	// OLE_TRISTATE
	OLE_TRISTATE_UNCHECKED =          0;
	OLE_TRISTATE_CHECKED =            1;
	OLE_TRISTATE_GRAY =               2;
}

function ui_openAboutBox() {
	util_openwnd("_about",NEXT_ROOT+"includes/ktedit/popups/about.html", 350, 200);
}

function util_openwnd(u,n,w,h, x) {
	var top, left, top, features;
	left = (screen.width - w) / 2;
	top = (screen.height - h) / 2;
	features = ",left="+left+",top="+top;
  winargs="width="+w+",height="+h+",resizable=no,scrollbars=no,status=0";
	dialogargs = "scrollbars=no,center=yes;dialogHeight="+h+"px;dialogWidth="+w+"px;help=no;status=no;resizable=no";
	if (n.indexOf("?") != -1) {
		n += "&";
	} else {
		n += "?";
	}
	n +="rand="+Math.random().toString().substring(3);
	//alert(n)
	if (window.showModalDialog && !x) {
		remote=window.showModelessDialog(n, window, dialogargs + features);
		if (n.match(/dirbrowser/)) {
			window._dlg_ = remote;
		}
	} else {
		remote=window.open(n, u, winargs + features);
		remote.focus();
	}
  
  if (remote != null) {
    if (remote.opener == null)
      remote.opener = self;
  }
  return remote;
}

function logic_submitKTMLS() {
	for ( var i = 0; i < ktmls.length ; i++) {
		if (ktmls[i].viewinvisibles == true) {
			ktmls[i].logic_toggleInvisibles();
		}
		if(ktmls[i].displayMode == 'RICH') {
			document.getElementById(ktmls[i].name).value = hndlr_save(ktmls[i].edit.body.innerHTML);
		} else {
			document.getElementById(ktmls[i].name).value = ktmls[i].textarea.value;
		}
	}
	return true;
}

function get_docXHtml(doc) {
	var toReturn = innerXHTML(doc, doc.body, new RegExp("contenteditable"));
	return toReturn;
}

function innerXHTMLAttributes(el, ignore) {
	var str = '';
	for (var i = 0; i < el.attributes.length; i++) {
		var attr = el.attributes[i];
		if (attr.nodeValue && typeof(attr.nodeValue) == "string") {
			if (!ignore || attr.nodeName.toLowerCase().search(ignore) == -1) {
				if (str.length) str += ' ';
				str += attr.nodeName.toLowerCase(); //lower
				str += '="' + (attr.nodeValue).replace(/[\"]/g, "&quot;") + '"'; //put between quotes, escape quote
			}
		}
	}
	return str;
}

function innerXHTML(doc, el, ignore) {
	var str = '';
	var r2;
	var r = doc.body.createTextRange(); //create range 
	r.moveToElementText(el); //contain the element in the range 
	for (var i = 0; i < el.children.length; i++) { //for all the children 
		r2 = doc.body.createTextRange();

		// set the end of our range to the start of this child node.
		// so that r.text contains all the text up to this element.
		r2.moveToElementText(el.children[i]);
		r.setEndPoint("EndToStart", r2);
		str += r.text.replace(/[&]/g, "&amp;").replace(/[<]/g, "&lt;").replace(/[>]/g, "&gt;").replace(/["]/g, "&quot;");
		// emit the child node
		str += outerXHTML(doc, el.children[i], ignore);
		// now, reset the text range for the main element and then move
		// the start point of our range to the end of the element just
		// output in preparation for the next chunk of text (or last chunk
		// if this was the last child node)
		r.moveToElementText(el);
		r.setEndPoint("StartToEnd", r2);
	}
	return str + r.text.replace(/[&]/g, "&amp;").replace(/[<]/g, "&lt;").replace(/[>]/g, "&gt;").replace(/["]/g, "&quot;");
}

function outerXHTML(doc, el, ignore) {
	var attrs = innerXHTMLAttributes(el, ignore);
	var inner = innerXHTML(doc, el, ignore);
	//return '<' + el.nodeName.toLowerCase() + (attrs.length ? ' ' + attrs : '') + (inner.length ? '>' + inner + '</' + el.nodeName.toLowerCase() + '>':' />');
	//return '\n<' + el.nodeName.toLowerCase() + (attrs.length ? ' ' + attrs : '') + (inner.length ? '>\n' + inner + '\n</' + el.nodeName.toLowerCase() + '>\n':' />\n');
	return ' <' + el.nodeName.toLowerCase() + (attrs.length ? ' ' + attrs : '') + (inner.length ? '> ' + inner + ' </' + el.nodeName.toLowerCase() + '> ':' /> ');
}

function util_Transform(html) {
	//var html = doc.innerHTML;
	//if (html) html = replaceCharacters(html);
	//if (html) html = util_replaceAbsoluteUrls(html);
	//if (html) html = util_remEmptyTags(html)
	//if (html) html = util_replaceTags([["strong","B"],["em","I"]],html);
	return html;
}

function util_remEmptyTags() {
	var html = activeActiveX.contentWindow.document.body.innerHTML;
	var re = /<[^(>|\/|tr|td)]+>[ |	]*<\/[^>]+>/gi;
	while(re.test(html)) {
		html = html.replace(re,"");
		while(re.test(html)) {
			html = html.replace(re,"");
		}
	}
	return html;
}

function util_replaceAbsoluteUrls(html) {
	var docLoc = document.location.toString();
	docLoc = docLoc.substring(0,docLoc.lastIndexOf("/")+1);
	docLoc = docLoc.replace(/\//gi,"\\\/");
	var re = eval("/"+docLoc+"/gi");
	return html.replace(re, "");
}
// set= [["strong", "b"], ["em", "i"]]
function util_replaceTags(set, html) {
	var re;
	for(var i = 0; i < set.length; i++) {
		re = eval("/(<[\/]{0,1})"+set[i][0]+">/gi");
		html=html.replace(re,"$1"+set[i][1]+">");
	}
	return html;
}

function util_ccParser(html) {
//	html = html.replace(/@/gi,"_AT_");
//	html = html.replace(/#/gi,"_DZ_");
//
	var htmltag = /(&lt;[\w\/]+[ ]*[\w\=\"\'\.\/\;\: \)\(-]*&gt;)/gi;
//	html = html.replace(htmltag,"<span class=ccp_tag>$1</span>");
//	html = html.replace(htmltag,"<span style='color:#0000CC'>$1</span>");
	return html;
}
function sg(a,a2,a3) {
	var r='';
	while (a.indexOf(a2)!=-1) {
		var v1=a.indexOf(a2);
		r+=a.substring(0,v1)+a3;
		a=a.substring(v1+a2.length);
	}
	return r+a;
}

function toSafeString(a) {
//	a = sg(a,'\\','\\\\'); // Backslash backslashes.
//	a = sg(a,'\n','\\n');  // Backslash newlines (Unix, Windows, DOS).
//	a = sg(a,'\r','\\r');  // Backslash newlines (Macintosh).
//	a = sg(a,'\t','\\t');  // Backslash tabs.
//	a = sg(a,'"','\\"');   // Backslash double quotes.
	a = sg(a,'\'','\\\'');
	//return '"' + a + '"';  // Return quoted string.
	return a; //Return nonquoted String
}

function util_srchString(htmlEl, attrMask) {
	var toRet = new String("|");
	for ( var j = 0; j < htmlEl.attributes.length; j++) {
		var attr = htmlEl.attributes[j];
		if (attr.nodeValue && typeof(attr.nodeValue) == "string") {
			if (attr.nodeName.toLowerCase().search(attrMask) >= 0) {
				toRet+= attr.nodeValue+"|";
			}
		}
	}
	for (var i = 0; i< htmlEl.children.length; i++) {
		toRet+=util_srchString(htmlEl.children[i], attrMask);
	}
	return toRet;
}

function util_modAttr(htmlEl, tagNames, attrMask, attrValue) {
	var toRet = new String("|");
	if (htmlEl.nodeType == 1) { //htmlElement
		if (tagNames.join().indexOf(htmlEl.tagName.toLowerCase()+",") > 0) {
			htmlEl.setAttribute(attrMask, attrValue);
		}
	} else if (htmlEl.nodeType == 3) { //Textnode
		return;
	}
	if (htmlEl.nodeType == 1) {
		if (tagNames.join().indexOf(htmlEl.tagName.toLowerCase()+",") > 0) { //is a div or a p , operate only on the children
			if (htmlEl.children.length > 0) {
				for (var i = 0; i< htmlEl.children.length; i++) {
					util_modAttr(htmlEl.children[i], tagNames, attrMask, attrValue);
				}
			} else {
				return;
			}
		} else { //is not a div, p operate on the TextNodes also
			if (htmlEl.childNodes.length > 0) {
				var txtNodes = new Array();
				var htmlNodes = new Array();
				for (var i = 0; i< htmlEl.childNodes.length; i++) {
					if (htmlEl.childNodes[i].nodeType == 3) {//TextNode
						txtNodes[txtNodes.length] = htmlEl.childNodes[i];
					} else {
						htmlNodes[htmlNodes.length] = htmlEl.childNodes[i];
					}
				}
				for (var j = 0; j<txtNodes.length; j++) {
					var tmpstr = txtNodes[j].data;
					if (tmpstr != "" && tmpstr != "&nbsp;") {
						var dv = activeActiveX.document.createElement("DIV");
						dv.align = "justify";
						dv.innerHTML = tmpstr;
						htmlEl.insertBefore(dv, txtNodes[j]);
					}
				}
				for ( var j = 0; j< htmlEl.childNodes.length; j++) {
					if (htmlEl.childNodes[j].nodeType == 3) {
						htmlEl.childNodes[j].removeNode();
					}
				}
				for ( var j = 0; j< htmlNodes.length; j++) {
					util_modAttr(htmlNodes[j], tagNames, attrMask, attrValue);
				}
			} else {
				return;
			}
		}
	}
	return;
}

function util_remSpaces(tmpstr) {
	if(tmpstr == "<p>&nbsp;</p>" || tmpstr == "<div>&nbsp;</div>") {
		tmpstr = "";
	}
	return tmpstr;
}

function util_fix4NS(instr){
	var re  = /STYLE=\"WIDTH\s*:\s*(\d+)px;\s*HEIGHT:\s*(\d+)px;*\s*\"/gi;
        var re1 = /<A\s+href=".*?"\s*>\s*<\/A>/gi; // for taking out blank links
        instr=instr.replace(re1, "");
	return(instr.replace(re, "width=$1 height=$2"));
}	

function util_cleanHTMLContent(tmp) {
	tmp = tmp.replace(/<\?xml:.*?>/ig, "");

	tmp = tmp.replace(/<H[0-9]+\s?([^>]*)>/ig, "<P $1>");
	tmp = tmp.replace(/<\/H[0-9]+>/ig, "</P>");

	tmp = tmp.replace(/<TT([^>]*)>/ig, "<P $1>");
	tmp = tmp.replace(/<\/TT>/ig, "</P>");

	tmp = tmp.replace(/(<[^>]+)lang=[a-z]*([^>]*>)/ig, "$1$2");
	tmp = tmp.replace(/("|\s)MARGIN:.*?(;|")/ig, "$1$2");
	tmp = tmp.replace(/("|\s)TEXT-INDENT:.*?(;|")/ig, "$1$2");
	tmp = tmp.replace(/("|\s)FONT-WEIGHT:.*?(;|")/ig, "$1$2");
	tmp = tmp.replace(/("|\s)tab-stops:.*?(;|")/ig, "$1$2");

	tmp = tmp.replace(/("|\s)mso-.*?:.*?(;|")/ig, "$1$2");
	tmp = tmp.replace(/\sclass=Mso.*?\s/ig, " ");
	tmp = tmp.replace(/<\/?o:p>/ig, "");
	tmp = tmp.replace(/(style="[^"]*)TEXT-ALIGN:\s?([a-z]*)([^"]*")/ig, "align=$2 $1$3");
	tmp = tmp.replace(/(style="[^"]*)BACKGROUND:\s?([a-z0-9#]*)([^"]*")/ig, "bgcolor=$2 $1$3");
	tmp = tmp.replace(/<SPAN style="FONT-FAMILY: Symbol">\s*<FONT\s+size=[0-9]*>\ï¿½<\/FONT>/ig, "<li>");
	tmp = tmp.replace(/(<font[^>]*)>\s*<font/ig, "$1");
	tmp = tmp.replace(/<dir>/ig, "<BLOCKQUOTE>");
	tmp = tmp.replace(/<\/dir>/ig, "</BLOCKQUOTE>");

	tmp = tmp.replace(/\sstyle="[^"]*"/ig, " ");
	tmp = tmp.replace(/\sclass=[A-Z0-9_]+/ig, " ");
	tmp = tmp.replace(/\sclass="[^"]*"/ig, " ");
	//tmp = tmp.replace(/(<td[^>]*)(width|height)=["'a-z0-9_%]*([^>]*>)/ig, '$1$3');
	//tmp = tmp.replace(/(<table[^>]*)(width|height)=["'a-z0-9_%]*([^>]*>)/ig, '$1$3');
	tmp = tmp.replace(/<font[^>]*><\/font>/ig, '');

	return tmp;//activeActiveX.document.body.innerHTML = tmp;
}

function util_cleanHTMLContent2(tmp) {
	tmp = tmp.replace(/<H[0-9]+\s?(.*)>/ig, "<P $1>");
	tmp = tmp.replace(/<\/H[0-9]+>/ig, "</P>");

	tmp = tmp.replace(/<TT([^>]*)>/ig, "<P $1>");
	tmp = tmp.replace(/<\/TT>/ig, "</P>");

	tmp = tmp.replace(/style="[^"]*"/ig, "");
	tmp = tmp.replace(/<font[^>]*>/ig, "");
	tmp = tmp.replace(/<\/font>/ig, "");
	tmp = tmp.replace(/\xFE/g, "Å£");
	tmp = tmp.replace(/\xDE/g, "Å¢");

	tmp = tmp.replace(/\xE2/g, "Ã¢");
	tmp = tmp.replace(/\xC2/g, "Ã‚");
	
	tmp = tmp.replace(/\xEE/g, "Ã®");
	tmp = tmp.replace(/\xCE/g, "ÃŽ");
	
	tmp = tmp.replace(/\xBA/g, "ÅŸ");
	tmp = tmp.replace(/\xAA/g, "ï¿½");
	
	tmp = tmp.replace(/\xE3/g, "Äƒ"); 
	tmp = tmp.replace(/\xC3/g, "Ä‚");
	tmp = tmp.replace(/\x92/g, "kk");
	tmp = tmp.replace(/\x96/g, "-");
	tmp = tmp.replace(/â€“/g, "-");
	tmp = tmp.replace(/â€™/g, "-");

	tmp = tmp.replace(/class=[^ >]*/ig, "");
	tmp = tmp.replace(/class="[^"]*"/ig, "");

	//tmp = tmp.replace(/(<td[^>]*)(width|height)=[^ >]*([^>]*>)/ig, '$1$3');
	//tmp = tmp.replace(/(<table[^>]*)(width|height)=[^ >]*([^>]*>)/ig, '$1$3');

	tmp = tmp.replace(/<font[^>]*><\/font>/ig, '');
	//tmp = hndlr_load(tmp);
	return tmp;//activeActiveX.document.body.innerHTML = tmp;
}

/**
* inserts a node in selection
*
* @param
*		win - ktml.edit
*		insertNode - node to be inserted
*/
if (Ktml_mozilla) {
function util_insertNodeSel(win, insertNode) {
	Logging.entering(3, 'mozilla');
	// get current selection
	var sel = win.selection;
	// get the first range of the selection
	// (there's almost always only one range)
	var range = sel.getRangeAt(0);
	// deselect everything
	sel.removeAllRanges();
	// remove content of current selection from document
	range.deleteContents();
	// get location of current selection
	var container = range.startContainer;
	var pos = range.startOffset;
	// make a new range for the new selection
	//range=sel.createRange();
	if (container.nodeType==3 && insertNode.nodeType==3) {
		container.insertData(pos, insertNode.nodeValue);// if we insert text in a textnode, do optimized insertion
		range.setEnd(container, pos+insertNode.length); // put cursor after inserted text
		range.setStart(container, pos+insertNode.length);
	} else {
		var afterNode;
		if (container.nodeType==3) {
			// when inserting into a textnode
			// we create 2 new textnodes
			// and put the insertNode in between
			var textNode = container;
			container = textNode.parentNode;
			var text = textNode.nodeValue;
			// text before the split
			var textBefore = text.substr(0,pos);
			// text after the split
			var textAfter = text.substr(pos);
			var beforeNode = document.createTextNode(textBefore);
			var afterNode = document.createTextNode(textAfter);
			// insert the 3 new nodes before the old one
			container.insertBefore(afterNode, textNode);
			container.insertBefore(insertNode, afterNode);
			container.insertBefore(beforeNode, insertNode);
			// remove the old node
			container.removeChild(textNode);
		} else {
			// else simply insert the node
			afterNode = container.childNodes[pos];
			container.insertBefore(insertNode, afterNode);
		}
		range.selectNode(insertNode);
		range.collapse(false);
		//range.setStart(afterNode, 0);
	}
	sel.addRange(range);
}
}
if (Ktml_ie) {
function util_insertNodeSel(win, insertNode) {
	Logging.entering(3, 'ie');
	if (win.selection.type =='Text' || win.selection.type =="None" ) {
		var tr = win.selection.createRange();
		tr.pasteHTML(insertNode.outerHTML);
		return tr.parentElement();
	} else {
		var ctrlRange = win.selection.createRange(); //the text that is selected
		var tmpel = ctrlRange.commonParentElement();
		tmpel.outerHTML = insertNode.outerHTML;
		return tmpel;
	}
}
}


function getObjectParentTag (tname, startel) {
    found = false; error = false;
    toret = null;
    do {
        if (startel.tagName.toLowerCase() == tname) {
            found = true;
            toret = startel;
        }
        if (startel.tagName.toLowerCase() == "body" || !startel.parentElement || startel == null) {
			error = true;
		}
		if (found || error) {
			break;
		}
		startel = startel.parentElement;
	} while (1);                    
	return toret;
}   
     
function hndlr_buttonMouseDown(src) {
	if(src.state==0) return;
	if(src.getAttribute("kttype")=="btn") {
		src.className="toolbaritem_inset";
	}
	return true;
}

function hndlr_buttonMouseUp(src) {
	if(src.state==0) return;
	if(src.state==2) { src.className="toolbaritem_latched"; return; }
	if(src.getAttribute("kttype")=="btn") {
		src.className="toolbaritem_outset";
	}
	return true;
}

function hndlr_buttonMouseOver(src) {
	if(src.state==0) {
		return;
	}
	//if(src.state==2) return;
	if(src.getAttribute("kttype")=="btn") {
		src.className="toolbaritem_outset";
	}
}

function hndlr_buttonMouseOut(src) {
	if(src.state==0) return;	
	if(src.state==2) { 
		src.className="toolbaritem_latched"; return; 
	}
	if(src.getAttribute("kttype")=="btn") {
		src.className="toolbaritem_flat";
	}
}

var orig_window_location = "";
var orig_short_location = "";

function hndlr_load (str, mode) {
	//str = str.replace(/></gi, "> <");
	//str = str.replace(/><\//gi, "> </");
	//str = str.replace(/\/></gi, "/> <");
	str = str.replace(/<span badword=[^>]*>([^<]+)<\/span>/gi, '$1');
	str = util_removeURLfromHref(str);
	return str;
}


function util_removeURLfromHref(href) {
	if (orig_window_location == "") {
    	orig_window_location = window.location.href;
		orig_short_location  = window.location.href;
    	var ix = orig_window_location.lastIndexOf("/");
    	if(ix != -1) {
	    	orig_window_location = orig_window_location.substr(0, ix);
	    }
		ix = orig_short_location.indexOf("/", 7);
		if(ix != -1) {
			orig_short_location = orig_short_location.substr(0, ix);
		}
    }

    if(orig_window_location != "") {
        var href_re = new RegExp(orig_window_location+".*#", "ig")
        href = href.replace(href_re, '#');
    }
	
	return href;	
}

function hndlr_save(str) {
	//str = str.replace(/> </gi, "><");
	//str = str.replace(/> <\//gi, "></");
	//str = str.replace(/\/> </gi, "/><");

	str = util_removeURLfromHref(str);
	str = str.replace(orig_short_location, "");
    
	
	return str;
	
	str = str.replace(/<param name="_filename_"/gi, '<param name="filename"');
	str = str.replace(/<param name="_src_"/gi, '<param name="src"');
	str = str.replace(/<param name="_movie_"/gi, '<param name="movie"');
	str = str.replace(/<embed _src_="/gi, '<embed src="');
	str = str.replace(/_pluginspage_="[^"]"/gi, 'pluginspage="');
	str = str.replace(/\s*<span badword=[^><]*>([^><]+)<\/span>\s*/gi, ' $1 ');

	return str;
}

function util_setInput(el, vl) {
	if (Ktml_mozilla) {
		el.value = vl;
	} else {
		el.value = vl;
// 	try {
// 		if (el.value != vl) {
// 			var par = el.parentElement;
// 			if (par.tagName == "DIV" || par.tagName == "SPAN") {
// 				par.contentEditable = true;
// 				try {
// 					var tmp = par.innerHTML;
// 				} catch(e) {
// 				}
// 				tmp = tmp.replace(/value=[^ >]*/, "");
// 				tmp = tmp.replace(/>/, ' value="'+vl+'">');
// 				par.innerHTML = tmp;
// 				par.contentEditable = false;
// 			}	
// 		}
// 	} catch(e) {
// 		setTimeout("util_setInput()", 10);
// 	}
	}
}


function util_checkFld(el, max) {
	var cond = true;
	//
	//if (!el.value.match(/^#[0-9a-fA-F]{6}*$/) && (el.value.match(/^[0-9]*%$/))) {
	if (!el.value.match(/^[0-9]*%?$/)) {
		cond = false;
		text = "Invalid value";
	}
	if ( parseInt(el.value) > max) {
		cond = false;
		text = "Value to big! Must be less then " +  max;
	}
	if (el.value == "") {
	 cond = true;
	}
	if (cond) {
		return true;
	} else {
		el.value = '';
		alert(text);
		return false;
	}
}

if (Ktml_mozilla) {
function util_preventEvent(e) {
	var keycode = e.keyCode;
	if (keycode == 0) {
		keycode = e.charCode;
	}
	if (keycode == 13) {
		e.preventDefault();
		e.stopPropagation();
		return false;
	}
}
function util_preventEvent2(o, e) {
	var keycode = e.keyCode;
	if (keycode == 0) {
		keycode = e.charCode;
	}
	if (keycode == 13) {
		e.preventDefault();
		e.stopPropagation();
		o.onchange();
		return false;
	}
}
}

if (Ktml_ie) {
	function util_preventEvent(e) {
		if (e.keyCode == 13) {
			return false;
		}
	}
	function util_preventEvent2(o, e) {
		if (e.keyCode == 13) {
			o.onchange();
			return false;
		}
	}
}


var framesLoaded = false;

function loadIframes(){
	if(framesLoaded) {
		return;
	}

	for(i=0;i<window.frames.length;i++){
		ifr = window.frames[i].name;
		if(ifr.match(/_htmlObject$/)){
			hfi = ifr.replace(/_htmlObject$/, "");
			if(document.getElementById(hfi)){
				if(document.getElementById(hfi).value){
					window.frames[i].document.write(document.getElementById(hfi).value);
				}
			}
		}
	}
	framesLoaded = true;
}

function lay_getAbsolutePos(el) {
	var r = new Object();
	r.x = el.offsetLeft;
	r.y = el.offsetTop;
	if (el.offsetParent) {
		var tmp = lay_getAbsolutePos(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
}

function util_srchString(htmlEl, attrMask) {
	var toRet = new String("|");
	var regexpstr = "/"+attrMask+"/";
	for ( var j = 0; j < htmlEl.attributes.length; j++) {
		var attr = htmlEl.attributes[j];
		if (attr.nodeValue && typeof(attr.nodeValue) == "string") {
			if (attr.nodeName.toLowerCase().search(attrMask) >= 0) {
				toRet+= attr.nodeValue+"|";
			}
		}
	}
	for (var i = 0; i< htmlEl.children.length; i++) {
		toRet+=util_srchString(htmlEl.children[i], attrMask);
	}
	return toRet;
}

// CBA 2003.08.21
// x,y,w,h - wanted position (relative to document 0,0) and box size
// return {x: new x, y: new y} (relative to document0,0)
function util_positionOnScreen(x,y,w,h) {
	var bw, bh, sw, sh, d;
	if (Ktml_mozilla) {
		bw = document.width;
		bh = document.height;
		sw = window.pageXOffset;
		sh = window.pageYOffset;
	}
	else {
		d = document.body;
		bw = d.offsetWidth - 20;
		bh = d.offsetHeight;
		sw = d.scrollLeft;
		sh = d.scrollTop;
	}
	if (x + w > bw + sw) {
		x = bw + sw - w;
	}
	if (y + h > bh + sh) {
		y = bh + sh - h;
	}
	if (x < 0) x = 0;
	if (y < 0) y = 0;
	return {x: x, y:y};
}

function util_preserveSession(numSeconds) {
	if (Ktml_ie) {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (Ktml_mozilla) {
		xmlhttp=new XMLHttpRequest();
	}
	xmlhttp.open("GET", NEXT_ROOT+"/includes/ktedit/popups/session.php",true);
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			setTimeout('util_preserveSession('+numSeconds+')', numSeconds);
		}
	}
	try {
		xmlhttp.send(null);
	} catch (e) {
		//alert('error');
	}
}
 
//xmlhttp_to();


// string extensions

String.prototype.ltrim = function() {
	return this.replace(/^\s+/,'');
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,'');
}
String.prototype.trim = function() {
	return this.replace(/^\s+/,'').replace(/\s+$/,'');
}

// zeroPad adds 0 in frotn till length
function util_zeroPad(str, length) {
	if (!str) str = "";
	str = str.toString();
	while (str.length < length) {
		str = "0" + str;
	}
	return str;
}

// array extensions

// works like String.indexOf
Array.prototype.indexOf = function(searchValue, fromIndex) {
	var i;
	if (!fromIndex) fromIndex = 0;
	for (i=fromIndex; i<this.length; i++) {
		if (this[i] == searchValue) {
			return i;
		}
	}
	return -1; 
}
// works like String.lastIndexOf
Array.prototype.lastIndexOf = function(searchValue, fromIndex) {
	var i;
	if (!fromIndex) fromIndex = this.length;
	for (i=fromIndex-1; i>=this.length; i--) {
		if (this[i] == searchValue) {
			return i;
		}
	}
	return -1; 
}

// decimal to hex - should be redone
function decimalToHex(_decimal)
{
 if (!_decimal) return;
 var inputDecimal = _decimal;
 var inputDecimal1 = parseInt(inputDecimal);
 var outputHex = new String("");
 var outputHex1;
 var decimalLength = inputDecimal.length;
 var decimalDigit;
 var hexDigit;
 var i = 0;
 var j = 0;
 var temp;
 for(j=0 ; Math.pow(16,j)<=inputDecimal ; j++)
     ;
 i = j - 1;
 while(i!=-1) {
     temp = Math.floor(inputDecimal1 / Math.pow(16,i));
     if(temp!=0)
 inputDecimal1 = inputDecimal1 - temp*Math.pow(16,i);
     if(temp<10)
 outputHex1 = temp;
     else if(temp==10)
 outputHex1 = "A";
     else if(temp==11)
 outputHex1 = "B";
     else if(temp==12)
 outputHex1 = "C";
     else if(temp==13)
 outputHex1 = "D";
     else if(temp==14)
 outputHex1 = "E";
     else if(temp==15)
 outputHex1 = "F";
     else
 outputHex1 = "";
     outputHex = outputHex + outputHex1;
     i--;  }
 if(inputDecimal=="0" || inputDecimal=="1")
     outputHex = inputDecimal;
 return outputHex;
	}

// bgr2rgb
function intbgr2hexrgb(a) {
	d = decimalToHex;
	// on mozilla will report rgb(a, b, c) - so the following is not good
	return "#" + util_zeroPad(d(a%256),2) + util_zeroPad(d((a/256)%256),2) + util_zeroPad(d((a/65536)%256),2);
}

function dumpVar(obj, sep) {
	if (sep == undefined) {
		sep = '';
	}
	tm = "";
	if (typeof(obj) == "object") {
		for (i in obj) {
			tm += sep + i + ":{\n" + dumpVar(obj[i], sep + '  ') + "}\n";
		}
		return tm;
	} 
	if (typeof(obj) == "function") return sep + typeof(obj) + "\n";
	return sep + obj + "\n";
}
function alertAttr(obj) {
	alert(dumpVar(obj));
}
function dumpToWindowAttr(obj) {
	var winHndl = window.open('about:blank');
	winHndl.document.write('<pre>');
	winHndl.document.write(dumpVar(obj));
	winHndl.document.write('</pre>');
}

function Log() {
	this.window = null;
	this.enabled = false;
}
Log.prototype.init = Log_init;
Log.prototype.add = Log_add;

function Log_init() {
	if (!log.enabled)return;
	var win;
	win = window.open("about:blank","log");
	win.blur();
	window.focus();
	win.document.open();
	this.window = win;
}
function Log_add(str) {
	if (!log.enabled)return;
	this.window.document.write(str + "\n");
}

log = new Log();
log.enabled = false;
log.init();
log.add("<pre>");




//--------------------------------------------------------------------
// CLASS:
//   Logging
//
// DESCRIPTION:
//	Creates a logger
//
// PUBLIC PROPERTIES:
//
// PUBLIC FUNCTIONS:
//
//   entering(classname, method, params)
//   exiting(classname, method, result)
//   log(message)
//
//   setLogging(logging)
//
//
//--------------------------------------------------------------------

//--------------------------------------------------------------------
// FUNCTION:
//   Logging
//
// DESCRIPTION:
//   Constructor function for the Logging class
//
// ARGUMENTS:
//   param - param
//
// RETURNS:
//   nothing
//--------------------------------------------------------------------
function Logging() {
	alert("This is a static class");
} 

//public methods
Logging.initialize = Logging_initialize;
Logging.entering = Logging_entering;
Logging.exiting = Logging_exiting;
Logging.log = Logging_log;
Logging.setLevel = Logging_setLevel;


//private functions
Logging.write = Logging_write;
Logging.parse = Logging_parse;
Logging.accepted = Logging_accepted;
Logging.alertAttr = Logging_alertAttr;
Logging.dumpVar = Logging_dumpVar;
Logging.htmlSpecialChars = Logging_htmlSpecialChars;

Logging.level = 0;
Logging.MAXLOG = 20;
Logging.FILE = "IAKT.log";
Logging.ws = " ";
Logging.enter = "{  ";
Logging.exit = "}  ";
Logging.profile = true;
Logging.times = Array();
Logging.window = null;



//--------------------------------------------------------------------
// FUNCTION:
//   initialize
//
// DESCRIPTION:
//	Initialize logging system	
//
// ARGUMENTS:
//	none
//
// RETURNS:
//   nothing
//--------------------------------------------------------------------
function Logging_initialize(level) {

	Logging.level = level;
	
	if (Logging.level == 0) return;
	var tmp = "<pre>------ Logging system 0.1 ---------\n";
	tmp += "-----------------------------------\n\n";
	
	
	var win;
	win = window.open('about:blank','Log');
	if (win) {
		win.blur();
		window.focus();
		win.document.open();
		Logging.window = win;
		Logging.write(tmp, false);	
	} else {
		alert("Logging initialisation failed!");
	}
}

//--------------------------------------------------------------------
// FUNCTION:
//   entering
//
// DESCRIPTION:
//	logs a method entering	
//
// ARGUMENTS:
//	level - int - log level
//	className - String - class name
//
// RETURNS:
//   nothing
//--------------------------------------------------------------------
function Logging_entering(level, className) {
	if(!Logging.accepted(level)) {
		return;
	}
	var rest = Logging.parse(Logging.entering.caller);
	var temp = ".L" + level + ".." + Logging.ws + Logging.enter + className + "." + rest;
	Logging.ws += "  ";
	Logging.write(temp + "\n");
	if (Logging.profile) {
		Logging.times.push(new Date());
	}
}

//--------------------------------------------------------------------
// FUNCTION:
//   exiting
//
// DESCRIPTION:
//   logs a method exiting 
//
// ARGUMENTS:
//	level - int - log level
//	className - String - class name
//	result - JS object - method result if any
//
// RETURNS:
//   nothing
//--------------------------------------------------------------------
function Logging_exiting(level, className, result) {
	if(!Logging.accepted(level)) {
		return;
	}
	if (Logging.profile) {
		var now = new Date();
		var start = Logging.times.pop();
	}
	Logging.ws = Logging.ws.substring(0, Logging.ws.length-2);
	var name ='';
	if (Logging_exiting.caller.name) {
		name = Logging_exiting.caller.name;
	} else {
		if (Logging_exiting.caller.toString().match(/\s*function\s*(.*?)\(/)) {
			name = RegExp.$1;
		} else {
			name = 'Unknown';
		}
	}
	
	var temp = ".L" + level + ".." + Logging.ws + Logging.exit + className + "." + name + " with result " + Logging.dumpVar(result);
	if (Logging.profile) {
		temp += ' in ' + (now - start) + ' ms';
	}
	Logging.write(temp + "\n");
}

//--------------------------------------------------------------------
// FUNCTION:
//   log
//
// DESCRIPTION:
//   Logs a message
//
// ARGUMENTS:
//	level - int - log level
//	message - String - the message to be logged
//
// RETURNS:
//   nothing
//--------------------------------------------------------------------
function Logging_log(level, message) {
	if(!Logging.accepted(level)) {
		return;
	}
	Logging.write(".L" + level + "..  " + Logging.ws + Logging.dumpVar(message) + "\n");
}

//--------------------------------------------------------------------
// FUNCTION:
//   setLevel
//
// DESCRIPTION:
//   Set the accepted log level logging 
//
// ARGUMENTS:
//	level - int - log level
//
// RETURNS:
//   nothing
//--------------------------------------------------------------------
function Logging_setLevel(level) {
	Logging.level = level;
}

//--------------------------------------------------------------------
// FUNCTION:
//   accepted
//
// DESCRIPTION:
//   Accepts a log level 
//
// ARGUMENTS:
//	level - int - log level
//
// RETURNS:
//   true - if the level is accepted
//--------------------------------------------------------------------
function Logging_accepted(level) {
	if(level <= 0 || level > Logging.MAXLOG) {
		alert("Logging level must be an integer between 1 and 20.");
		return false;
	}
	if(level <= Logging.level) {
		return true;
	} else {
		return false;
	}
}

//--------------------------------------------------------------------
// FUNCTION:
//   dumpVar
//
// DESCRIPTION:
//   Convert a JS Object to String 
//
// ARGUMENTS:
//	obj - JS Object - 
//
// RETURNS:
//   String representation of an object
//--------------------------------------------------------------------
function Logging_dumpVar(obj, tab, inner) {
	tm = "";
	if(!tab) {
		tab = "";
	}
	if (inner == undefined) {
		inner = true;
	}
	tab += "  ";
	var logMeString = false;
	try {
		if (obj['logMeString'] != undefined) {
			logMeString = true;
		}
	} catch (e) {
	}
	if (logMeString) {
		tm += obj.toString();
	} else if (typeof(obj) == "object") {
		for (i in obj) {
			if (typeof(obj[i]) != "function") {
				var isobj = typeof(obj[i]) == "object";
				tm += Logging.ws + tab + i + ":{ ";
				if(isobj) {
					tm += "\n";
				}
				if (inner) {
					tm += Logging.dumpVar(obj[i], tab, true);
				}
				if(isobj) {
					tm += Logging.ws + tab + " ";
				}
				tm += " }\n";			
			}
		}
	} else if (typeof(obj) == "function") {
		tm += Logging.ws + tab + typeof(obj);
	} else {
		if(!inner) {
			tm += Logging.ws + tab;
		}
		tm += Logging.htmlSpecialChars(obj);
	}
	tab = tab.substring(0, tab.length-2);
	return tm;
}


//--------------------------------------------------------------------
// FUNCTION:
//   htmlSpecialChars
//
// DESCRIPTION:
//   encode html special chars
//
// ARGUMENTS:
//	str - string - to be encoded
//
// RETURNS:
//   encoded string
//--------------------------------------------------------------------
function Logging_htmlSpecialChars(str) {
	var ret = str;
	if (ret && ret.replace) {
		ret = ret.replace(/</g,"&lt;");
		//~ ret = ret.replace(/>/g,"&gt;");
		//~ ret = ret.replace(/"/g,"&quot;");
		//~ ret = ret.replace(/\&/g,"&amp;");
	}
	return ret;
}


//--------------------------------------------------------------------
// FUNCTION:
//   alertAttr
//
// DESCRIPTION:
//   Same as JS alert for a JS Object 
//
// ARGUMENTS:
//	obj - JS Object - 
//
// RETURNS:
//   String representation of an object
//--------------------------------------------------------------------
function Logging_alertAttr(obj) {
	alert(Logging.dumpVar(obj));
}

//--------------------------------------------------------------------
// FUNCTION:
//   writeAttr
//
// DESCRIPTION:
//    
//
// ARGUMENTS:
//	message - String
//
// RETURNS:
//   String representation of an object
//--------------------------------------------------------------------
function Logging_write(message, append) {
	if(Logging.window) {
		Logging.window.document.write(message);
	}
	/*
	if ((append != undefined)  && (append == false)){
		DWfile.write(dw.getConfigurationPath() + "/" + Logging.FILE, message);
	} else {
		DWfile.write(dw.getConfigurationPath() + "/" + Logging.FILE, message,"append");
	}
	*/
}

//--------------------------------------------------------------------
// FUNCTION:
//   parse
//
// DESCRIPTION:
//	Parses a method definition and retrieves it's name and arguments	
//
// ARGUMENTS:
//	method - String - method definition
//
// RETURNS:
//   string 
//--------------------------------------------------------------------
function Logging_parse(method) {
	var all = "";
	if (method.name) {
		name = method.name;
	} else {
		if (method.toString().match(/\s*function\s*(.*?)\(/)) {
			name = RegExp.$1;
		} else {
			name = '';
		}
	}
	all += name;
	args = method.arguments;
	if(args.length > 0) {
		all += " with params ";
		for (i=0; i<args.length; i++) {
			all = all + args[i] + " ";
		}
	}
	return all;
}

// dom helpers
function ktml_hideElem(elem) {
	ktml_setCssProperty(elem, "display", "none");
}
function ktml_showElem(elem, inline) {
	var t = "block";
	if (inline) t = "inline";
	ktml_setCssProperty(elem, "display", t);
}
function ktml_toggleElem(elem, inline) {
	var d, t;
	var t = "block";
	if (inline) t = "inline";
	try {
		d = document.getElementById(elem);
		if (d.style.display != t) {
			d.style.display = t;
		} else {
			d.style.display = "none";
		}
	}
	catch(e) {
		// 
	}
}

function ktml_setCssProperty(elem, name, value) {
	var d;
	// sanity
	if (!elem | !name | !value) return;
	d = document.getElementById(elem);
	if (!d) return;
	d.style[name] = value;
}
