// iactEd Copyright (c) 2007 Inter-Act.ro
// version 1.2 / 2007
// Contains code Copyright (c) 2006 openWebWare.com
// Contains code Copyright (c) 2006 wyzz.info
// This copyright notice MUST stay intact for use.


// defines
var editorFontFamily 	= "Tahoma";				// you can leave it empty if you like Times New Roman
var editorFontSize 		= "13px";				// below 13px looks too small
var editorBackground	= "#ffffF0";			// 
var editorFolder		= SITE_LINK+"scripts/editor/";
var editorCSS			= editorFolder + "iactEd.css";		// add full path if needed (ex: ../includes/wyzz.css)
var editorIcons			= editorFolder + "iactEd.gif";		// link to your icons picture. 

// Order of available commands in toolbar
var buttonName 			= new Array("bold","italic","underline","strikethrough","separator","forecolor","separator","insertimage","separator","makelink","separator","justifyleft","justifycenter","justifyright","separator","insertunorderedlist","insertorderedlist","separator","fontname","separator","font-size","separator","undo","separator","removeformat","removewordformat");
// get more button functionality here: http://www.mozilla.org/editor/midas-spec.html 
// all should work, but be aware !!! Some need "popups" for aadditional info, like color, link, etc ..., for those you need extra programming

// Colors
var butonColors 		= new Array("#000000", "#444444", "#666666", "#999999", "#cccccc", "#eeeeee", "#f3f3f3", "#ffffff", "separator",
									"#ff0000", "#ff9900", "#ffff00", "#00ff00", "#00ffff", "#0000ff", "#9900ff", "#ff00ff", "separator",
									"#f4cccc", "#fce5cd", "#fff2cc", "#d9ead3", "#d0e0e3", "#cfe2f3", "#d9d2e9", "#ead1dc",
									"#ea9999", "#f9cb9c", "#ffe599", "#b6d7a8", "#a2c4c9", "#9fc5e8", "#b4a7d6", "#d5a6bd",
									"#e06666", "#f6b26b", "#ffd966", "#93c47d", "#76a5af", "#6fa8dc", "#8e7cc3", "#c27ba0",
									"#cc0000", "#e69138", "#f1c232", "#6aa84f", "#45818e", "#3d85c6", "#674ea7", "#a64d79",
									"#990000", "#b45f06", "#bf9000", "#38761d", "#134f5c", "#0b5394", "#351c75", "#741b47",
									"#660000", "#783f04", "#7f6000", "#274e13", "#0c343d", "#073763", "#20124d", "#4c1130"
									);

var colorsBreak			= 8; 					// how many colors in a row in the color picker



// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //
// DO NOT EDIT BELOW //
// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //
document.write('<link rel="stylesheet" type="text/css" href='+editorCSS+'>\n<style>.iactEd_button{background-image:url('+editorIcons+');}</style>');

var insertimagelive = {};

var browserName = navigator.appName,
	isMSIE= browserName  == "Microsoft Internet Explorer" ? true : false,
	isOpera= browserName  == "Opera" ? true : false,
	isFF= !document.all && document.getElementById && !isOpera;
if(false && isMSIE){
	alert('IE is not supported!');
}


if(!window.$){  window.$ = function(id){ return document.getElementById(id); }}

function x(){/* this does nothing !! it's just here to allow 'a' tags to have the href property*/}


function make_iactEd(textareaID) {

	$(textareaID).style.display = "none" ;		// Hide the textarea 
	var n = textareaID;	  												// get textareaID
	
	// Generate iactEd toolbar
	var toolbar;
	toolbar = '<div class="iactEd_toolbar">';						// the toolbar line
	toolbar += '<div class="iactEd_visumode" style="display:none" id="iactEd_' + n + '_vis_mode">Visual mode: <b>OFF</b></div><div style="float:right;"><span class="iactEd_button_separator">&nbsp;</span><a class="iactEd_button htmlcode" id="iactEd_' + n +'htmlcode_btn" href="javascript: x();" onClick="formatText(\'htmlcode\',\'' + n + '\');" onmouseover="status=\'Show HTML code\';return true;">&nbsp;</a></div>';
	// MAXIMIZE BUTTON
	//toolbar += '<div style="float:right;"><span class="iactEd_button_separator">&nbsp;</span><a class="iactEd_button maximize" onmouseover="status=\'Maximise / minimse editor window.\';return true;" id="iactEd_' + n +'maximize_btn" href="javascript: ;" onClick="formatText(\'maximize\',\'' + n + '\');" >&nbsp;</a></div>';

	// MAIN BUTTONS
	toolbar += '<div style="float:left;" id="iactEd_' + n + '_toolbar">';
	for (i=0; i<buttonName.length;i++){
		if(buttonName[i]=="separator"){
			toolbar += '<span class="iactEd_button_separator">&nbsp;</span>';
		} else if(buttonName[i]=="font-heading"){
			toolbar +='<span style="width:90px; display:block; float:left; padding-top:1px;"><select name="no_fontheading" id="iactEd_font-heading_'+n+'" style="width:90px; padding-right:0; font-size:10px;" onchange="javascript: formatSize(\'iactEd_font-heading_'+n+'\',\'' + n + '\', \'formatblock\');"><option value="">Formating</option> <option value="<p>">Paragraph</option><option value="&lt;h1&gt;">Heading 1</option><option value="<h2>">Heading 2</option><option value="<h3>">Heading 3</option><option value="<h4>">Heading 4</option><option value="<h5>">Heading 5</option><option value="<h6>">Heading 6</option> </select></span>';
		} else if(buttonName[i]=="font-size"){
			
			
			toolbar +='<span style="width:50px; display:block; float:left; padding-top:1px;"><select name="no_fontsize" id="iactEd_font-size_'+n+'" style="width:50px; padding-right:0; font-size:10px;" onchange="javascript: formatSize(\'iactEd_font-size_'+n+'\',\'' + n + '\', \'fontsize\');"><option value="">Size</option> <option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option> </select></span>';
		
		
		
		
		} else if(buttonName[i]=="fontname"){
			
			
			toolbar +='<span style="width:50px; display:block; float:left; padding-top:1px;"><select name="no_fontname" id="iactEd_fontname_'+n+'" style="width:50px; padding-right:0; font-size:10px;" onchange="javascript: formatSize(\'iactEd_fontname_'+n+'\',\'' + n + '\', \'fontname\');"><option value="">Font</option> <option value="Arial">Arial</option><option value="Verdana">Verdana</option><option value="Times New Roman">Times</option><option value="sans">sans serif</option><option value="serif">serif</option> </select></span>';
		
		
		
		
		} else if(buttonName[i]=="removewordformat"){
			toolbar += '<a class="iactEd_button '+buttonName[i]+ '" id="iactEd_' + n +''+buttonName[i]+ '_btn"  href="javascript: window.formatWT ? window.formatWT(\''+ n +'\') : formatText(\''+buttonName[i]+'\',\'' + n + '\');" onmouseover="status=\'Set ' +buttonName[i]+ '\';return true;">&nbsp;</a>';
		} else {
			toolbar += '<a class="iactEd_button '+buttonName[i]+ '" id="iactEd_' + n +''+buttonName[i]+ '_btn"  href="javascript: formatText(\''+buttonName[i]+'\',\'' + n + '\');" onmouseover="status=\'Set ' +buttonName[i]+ '\';return true;">&nbsp;</a>';			
		}		
	}
	toolbar += '<span class="iactEd_button_separator">&nbsp;</span>';
	toolbar += '</div></div>';  										// close toolbar line
	
	// COLOR PICKER
	toolbar += '<div id="colorpicker_' + n + '" class="colorpicker" style="display:none; width:160px; ">';	var counter=0;
	toolbar += '<div style="float:left;">';
	for (j=0; j<butonColors.length; j++) {
		if(butonColors[j]!="separator"){
			toolbar += '<a href="javascript: formatTextColor(\'' + butonColors[j] + '\',\'' + n + '\');" class="colorButton" style="background:' + butonColors[j] + '">&nbsp;</a>';
		} else {
			toolbar +='</div><div style="clear:both; height:5px;"></div><div style="float:left;">';
			counter=-1;
		}
		
		counter ++;
		if(counter==colorsBreak){counter=0; toolbar +='<br>';}
	}
	toolbar += '</div >';
	//toolbar += '<div style="float:left;"><a href="javascript: x()" class="iactEd_button close" onClick="formatText(\'forecolor\',\'' + n + '\');" >&nbsp;</a></div>';
	toolbar += '</div>\n'; 											// close the color div
	
	// INSERT LINK
	toolbar += '<div id="makelink_' + n + '"  class="colorpicker makelink"  style="display:none; "><div>';
		toolbar += '<div class="iactEd_dl"><dl>';

		toolbar += '<dt>Name:</dt><dd><input id="makelink_'+n+'_name" type="text" class="large"/></dd>';
		toolbar += '<dt>Link:</dt><dd><input id="makelink_'+n+'_link" type="text" class="xtralarge"/><br>Please add "http://" in front of the link!</dd>';

		toolbar += '<dt>Target:</dt><dd><select id="makelink_'+n+'_target" class="small"><option value="_blank">New Window</option><option value="_self">Same Window</option></select></dd></dl>';
		toolbar += '<dl><dt></dt><dd>';
		toolbar += '<input type="button" onclick="javascript:putLink(\''+n+'\');"  class="button small" value="INSERT" />';
		toolbar += '<input type="button" onclick="formatText(\'makelink\',\''+n+'\')"  class="button" value="cancel" />';
		toolbar += '</dl></div ></div>';
	toolbar += '</div>\n'; 	
	
	// INSERT IMAGE
	toolbar += '<div id="insertimage_' + n + '"  class="colorpicker colorpicker colorpicker_image"  style="display:none; "></div>';
	
	// Create iframe for editor
	var iframe = '<div class="iactEd_frame"><iframe frameborder="0"  id="iactEd_' + n + '" class="iactEd_iframe " style="margin:0; padding:0; visibility:visible;"></iframe></div>\n';
	var overlay = '<div class="iactEd_overlay" id="iactEd_overlay_'+n+'" style="display:none">&nbsp;</div>\n';
	
	// Insert toolbar after the textArea
	var currentEditor = '<div class="iactEditor"  id="iactEd_All_'+n+'"><div >' + toolbar + overlay + iframe + '<textarea id="iactEd_' + n + '_textarea" style="visibility:hidden;" class="iactEd_txtframe"></textarea></div></div>';
	
	
	$(n).parentNode.innerHTML += (currentEditor);
		
	
	
	
	$('iactEd_' + n + '_textarea').style.visibility= "hidden"; 	// might not be hidden automatically
	
	// resize the editable area
	$("iactEd_" + n).style.width = "100%";
	$("iactEd_" + n + "_textarea").style.width = $("iactEd_" + n).style.width;
	$("iactEd_" + n + "_textarea").style.height = $("iactEd_" + n).style.height;
	
	var content = $(n).value;
	var doc = $("iactEd_" + n).contentWindow.document;

	// Write the textarea's content into the iframe
	doc.open();
	doc.write(content);	
	doc.close();
	
	if(doc.body.contentEditable){									// IE, Opera
		doc.body.contentEditable = true;  
	} else {														// other browsers, i presume
		doc.designMode = "on";
	}
	
	doc.body.style.fontFamily 	= editorFontFamily;
	doc.body.style.fontSize 	= editorFontSize;
	doc.body.style.background 	= editorBackground;
	
	
	// Update the textarea with content in iactEd when user submits form
	if (browserName == "Microsoft Internet Explorer" || browserName == "Opera") {
		for (var idx=0; idx < document.forms.length; idx++) {
			document.forms[idx].attachEvent('onsubmit', function() { updateTextArea(n); });
		}
	} else {
		for (var idx=0; idx < document.forms.length; idx++) {
			document.forms[idx].addEventListener('submit',function OnSumbmit() { updateTextArea(n); }, true);
		}
	}
}


function formatTextColor(color, n, selected) {
	overlay_off(n);
	$('iactEd_' + n +colorElement+'_btn').className = "iactEd_button "+colorElement+"";
	$("iactEd_" + n).contentWindow.focus();
	$('colorpicker_' + n).style.display = "none";
	if ((browserName != "Microsoft Internet Explorer" || browserName == "Opera") && colorElement=='backcolor'){highligtAction='hilitecolor';}else{highligtAction=colorElement;}
	$("iactEd_" + n).contentWindow.document.execCommand(highligtAction, false, color);
}

function formatSize(id, n, action){
	var button = id;
	var soption = $(button).selectedIndex;
	if (soption != 0) {
		var selected = $(button).options[soption].value;
		$("iactEd_" + n).contentWindow.document.execCommand(action, false, selected);
		$(button).selectedIndex = 0;
	}
}

var Selection;
function formatText(id, n, selected) {
	
	// When user clicks button make sure it always targets correct textarea
	var contentWindow = $("iactEd_" + n).contentWindow;
	$("iactEd_" + n).contentWindow.focus();
	overlay_off(n);
	// Particular cases
	if(id=="forecolor" || id=="backcolor" ){
/* ---- COLORS --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
		colorElement = id;
		if($('colorpicker_' + n).style.display == ""){
			overlay_off(n);
			$('iactEd_' + n +id+'_btn').className = "iactEd_button "+id+"";
			$('colorpicker_' + n).style.display = "none";
		} else {
			overlay_on(n);
			$('colorpicker_' + n).className  = "colorpicker colorpicker_color";
			$('iactEd_' + n +id+'_btn').className = "iactEd_button "+id+" iactEd_button_active";
			$('colorpicker_' + n).style.display = "";	
		}
		
		
		
		
	}else if(id == 'maximize'){
/* ---- MAXIMIZE --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
		
	}else if(id == 'fontsize'){
/* ---- FONT-SIZE --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */

	} else if(id=="htmlcode"){
/* ---- HTML CODE --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
		overlay_off(n);
		if($("iactEd_" + n).style.visibility =="" || $("iactEd_" + n).style.visibility =="visible"){
			updateTextArea(n);
			
			$("iactEd_" + n + '_vis_mode').style.display = "";
			$('iactEd_' + n + '_toolbar').style.display = "none";
			$("iactEd_" + n).style.visibility = "hidden";
			$('iactEd_' + n + '_textarea').style.visibility = "visible";

			$('iactEd_' + n +'htmlcode_btn').className = "iactEd_button iactEd_button_active";
			$('iactEd_' + n + '_textarea').value = $("iactEd_" + n).contentWindow.document.body.innerHTML;
		} else {
			$("iactEd_" + n + '_vis_mode').style.display = "none";
			$('iactEd_' + n + '_toolbar').style.display = "";
			$("iactEd_" + n).style.visibility = "visible";
			$('iactEd_' + n + '_textarea').style.visibility = "hidden";
			
			$("iactEd_" + n).contentWindow.document.body.innerHTML = $('iactEd_' + n + '_textarea').value;
			$('iactEd_' + n +'htmlcode_btn').className = "iactEd_button";
			
		}
		
		
		
	} else if(id=="makelink"){
/* ---- MAKE LINK --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
		if($('makelink_' + n).style.display == ""){
			overlay_off(n);
			$('iactEd_' + n +'makelink_btn').className = "iactEd_button "+id+"";
			$('makelink_' + n).style.display = "none";
		} else {
			overlay_on(n);
			//clear the items
			$("makelink_"+n+"_name").value="";
			$("makelink_"+n+"_link").value="";
			
			Selection = returnSelection(contentWindow);
			TheText = typeof Selection.text != 'undefined'? Selection.text : Selection;
			var hasLink = findParent("a", Selection);
			
			if(hasLink){										// link =1, select =0/1
				selectThisNode(hasLink, contentWindow);
				$("makelink_"+n+"_name").value=hasLink.innerHTML;
				$("makelink_"+n+"_link").value=hasLink.href;
			} else if(!hasLink && TheText!=""){					// link =0, select =1
				$("makelink_"+n+"_name").value = TheText;
				$("makelink_"+n+"_link").value = "http://";
			} else {											// link =0, select =0
				$("makelink_"+n+"_link").value = "http://";
			}
	
			$('makelink_' + n).className  = "colorpicker colorpicker_link";
			$('iactEd_' + n +'makelink_btn').className = "iactEd_button "+id+" iactEd_button_active";
			$('makelink_' + n).style.display = "";
			
		}





	} else if(id=="insertimage"){
/* ---- INSERT IMAGE --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
		if($('insertimage_' + n).style.display == ""){
			overlay_off(n);
			$('iactEd_' + n +'insertimage_btn').className = "iactEd_button "+id+"";
			$('insertimage_' + n).style.display = "none";
			if(insertimagelive[n]){
				overlay_off(n);
				document.getElementById('iactEd_' + n +'insertimage_btn').className = "iactEd_button insertimage";
				$('insertimage_'+n).removeChild($('insertimage_'+n).firstChild);
				$('insertimage_'+n).style.display = "none";
				insertimagelive[n] = false;
				//return;
			}
		
		} else {
			overlay_on(n);
			$('iactEd_' + n +'insertimage_btn').className = "iactEd_button "+id+" iactEd_button_active";
			$('insertimage_' + n).style.display = "";
			//alert(Selection.text);
			includeImageLibrary(n);
		}
	





	} else {
/* ---- all other --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
		$("iactEd_" + n).contentWindow.document.execCommand(id, false, null);
	}
};





/* -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
	function updateTextArea(n) {
		//if($("iactEd_" + n).style.display ==""){
		if($("iactEd_" + n).style.visibility =="visible"){
			var currentInput = $("iactEd_" + n).contentWindow.document.body.innerHTML;
		} else {
			var currentInput = $("iactEd_" + n + "_textarea").value;		
		}
		$(n).value = currentInput;
	};


	function overlay_on(n){
		$('iactEd_overlay_'+n).style.display="";
	}
	
	function overlay_off(n){
		$('iactEd_overlay_'+n).style.display="none";
	}


	function includeImageLibrary(n){
		var d = $('insertimage_'+n),
			i = document.createElement('iframe');
		document.getElementById('iactEd_' + n +'insertimage_btn').className = "iactEd_button insertimage iactEd_button_active";
		//i.src=editorFolder+"insert_image.php?n="+n+"&cw="+$("iactEd_" + n).contentWindow;
		i.src=editorFolder+"insert_image.php?n="+n+"&path="+SITE_ROOT+ADM_PATH;
		i.style.width="560px";
		if(isMSIE) i.style.height="280px";
		else i.style.height="240px";
		i.style.border = "0";
		i.style.margin = "0";
		i.style.padding = "0";
		i.setAttribute ('frameborder', '0');
		d.style.display = "";
		d.appendChild(i);
		d.style.position = 'absolute';
		d.style.left = '0';
		insertimagelive[n] = true;
	}
	
	
/* --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	it gets hardcore 
/* -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */



	function putLink(n){
		overlay_off(n);
		var Name 	= $("makelink_"+n+"_name").value;
		var Link 	= $("makelink_"+n+"_link").value;
		var Target 	= $("makelink_"+n+"_target").value; 
		var Ceva = '<a href="'+Link+'" target="'+Target+'">'+Name+'</a> ';

		var a = findParent('a', Selection);
		a && a.parentNode.removeChild(a);

		if(isMSIE){
			Selection.pasteHTML(Ceva);
		} else {
			$("iactEd_" + n).contentWindow.document.execCommand("inserthtml", false, Ceva);
		}
		
		// hide box
		$('iactEd_' + n +'makelink_btn').className = "iactEd_button makelink";
		$('makelink_' + n).style.display = "none";
	}




/* -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
	function selectThisNode(node, contentWindow){
			
		if(Selection.rangeCount) {
			try{Selection.removeAllRanges();} catch(e) {if(Selection.empty)Selection.empty()}
		}
		var r = contentWindow.document.createRange? contentWindow.document.createRange() : contentWindow.document.selection.createRange();
		try{
			
			r.selectNode(node);
			Selection.addRange(r);
			//alert(node.innerHTML)
		} catch (e) {
			try{
			r.moveToElementText(node);
			r.select();
			}catch(e){
				alert(e.message)
			}
		}
		return node;
	}

/* -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
	function findParentNode(tagName, node) {
		while (node.tagName != "HTML") {
			if (node.tagName == tagName){
				return node;
			} 
			node = node.parentNode;
		}
		return null;
	}
	
/* -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
	function findParent(parentTagName, range){
			parentTagName = parentTagName.toUpperCase();
			var rangeWorking;
			var elmWorking = null;
			try {
				
				if(!isMSIE) {
					range.getRangeAt && (range = range.getRangeAt(0));
					var node = range.startContainer||{};	
					var pos = range.startOffset;
					if(node.nodeType != 3) { node = node.childNodes[pos]; }
					return findParentNode(parentTagName, node);
				}
				else {
					elmWorking = (range.length > 0) ? range.item(0): range.parentElement();					
					elmWorking = findParentNode(parentTagName, elmWorking);
					if(elmWorking != null) return elmWorking;
					rangeWorking = range.duplicate();
					rangeWorking.collapse(true);
					rangeWorking.moveEnd("character", 1);
					if (rangeWorking.text.length>0) {
						while (rangeWorking.compareEndPoints("EndToEnd", range) < 0){
							rangeWorking.move("Character");
							if (null != this.findParentTag(parentTagName, rangeWorking)){
								return this.findParentTag(parentTagName, rangeWorking);
							}
						}
					}
					return null;
				}
			}
			catch(e) {
				//alert("DEVELOPER ERROR:\n" + e);
				return null;
			}
		}

/* -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
	function returnSelection(parent){
		if(!parent) parent=window;
		var u;
		
		if (parent.getSelection) {
			u = parent.getSelection();
		} else if(!parent && window.getSelection){
			u = window.getSelection();
		}	else if (parent.document && parent.document.selection) {
			u = parent.document.selection.createRange();
		} else if(document.selection){
			u =document.selection.createRange();
		}
		Selection = u;
		return u;
	}
	
/* -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
	function insertImage(src, width, height, align, border, alt, hspace, vspace, n, LImg){
		var cw = $("iactEd_" + n).contentWindow;
		var doc = cw.document;
		var range = returnSelection(cw);
		var img = LImg || findParent("img", range);
		var update = img != null;
		
		if(!update) {
			img = doc.createElement("img");
		}
		img.src= src;
		img.style.width= width;
		img.style.height= height;
		img.setAttribute('width', width);
		img.setAttribute('height', height);
		if(align != "") { img.align=align; } else { img.removeAttribute("align"); }
		img.border= border;
		img.alt= alt;
		img.hspace= hspace;
		img.vspace=vspace;
		img.removeAttribute("width");
		img.removeAttribute("height");
		if(update) { return; }   	
		if (isMSIE) {
			try {
					returnSelection(cw).pasteHTML(img.outerHTML);
			} catch (e){
				var imag = new Image(img.width||100, img.height||100);
				imag.src = img.src
				try{
					undefine.undefined.call();
				} catch (ee){
					doc.body.innerHTML += img.outerHTML;
				}
			}  
		}
		else {
			insertNodeAtSelection(img, n);
			//$("iactEd_" + n).contentWindow.document.execCommand("inserthtml", false, Ceva);
		}
	}
	
	function insertNodeAtSelection(insertNode, n) {
		var cw = $("iactEd_" + n).contentWindow;
		var doc = cw.document;
		var sel = returnSelection(cw);
		var range = sel.getRangeAt(0);
		sel.removeAllRanges();
		range.deleteContents();
		var container = range.startContainer;
		var pos = range.startOffset;
		range = doc.createRange();
		if (container.nodeType==3 && insertNode.nodeType==3) {					
			container.insertData(pos, insertNode.data);
			range.setEnd(container, pos+insertNode.length);
			range.setStart(container, pos+insertNode.length);		
		} 	
		else {
			var afterNode;	
			var beforeNode;
			if (container.nodeType==3) {
				var textNode = container;
				container = textNode.parentNode;
				var text = textNode.nodeValue;
				var textBefore = text.substr(0,pos);
				var textAfter = text.substr(pos);
				beforeNode = document.createTextNode(textBefore);
				afterNode = document.createTextNode(textAfter);
				container.insertBefore(afterNode, textNode);
				container.insertBefore(insertNode, afterNode);
				container.insertBefore(beforeNode, insertNode);
				container.removeChild(textNode);
			} 
			else {
				afterNode = container.childNodes[pos];
				container.insertBefore(insertNode, afterNode);
			}
			try {
				range.setEnd(afterNode, 0);
				range.setStart(afterNode, 0);
			}
			catch(e) {
			}
		}
		sel.addRange(range);
	}		


/* -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */

function present(element, only, not){
	if(!element) return 'null!';
	if(typeof(element)== 'string') return element;
	var m = '', i=null;
	for(i in element){
		if(only && typeof(element[i]) != only) continue;
		else if(not && typeof(element[i]) == not) continue;
		m+="("+typeof(element[i])+") "+i+": "+element[i]+"\n";
	}
	return m;
}


function formatWT(token){
	var cu 		= $("iactEd_" + token).contentWindow.document.body.innerHTML,
		http 	= (function (){
					var request_o;
					var browser = navigator.appName;
					if( browser == "Microsoft Internet Explorer"){
						request_o = new ActiveXObject("Microsoft.XMLHTTP");
					} else {
						request_o = new XMLHttpRequest();
					}
					return request_o;
				})()
		
	http.open('post', '../admin/_actions.php', true);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		
	http.onreadystatechange = function(){
		if(http.readyState!=4) return;
		$("iactEd_" + token).contentWindow.document.body.innerHTML = http.responseText;
	}
	http.send('no_action=stripword&content='+encodeURIComponent(cu));
	
}
