var wd_wyschanged_mark=false;
var wd_visedit_b_HtmlMode=0;

function wd_EditorSelected(sFldName) {
	var sFraName=sFldName+'_editor';
	var theFrame=document.frames[sFraName]; // to work with iframe 'document' object
	var theFraDoc=theFrame.document;
}

function wd_ShowDialog(sDlgType, sFldName, sButtName, iUpDir, iLtDir) {
	eval("tmr_"+sDlgType+"=window.setTimeout('document.getElementById(\\\""+sDlgType+"\\\").style.visibility=\\\"hidden\\\";document.getElementById(\\\""+sFldName+"_editor\\\").style.visibility=\\\"visible\\\";', 1000);");
	if (!(document.createAttribute)) {
		var theField=document.getElementById(sFldName+"_editor");
		if (theField) theField.style.visibility="hidden";
	}
	var theDialog=document.getElementById(sDlgType);
	var theButt=document.images[sFldName+"_"+sButtName];
	var arrButtPos=wd_AbsPos(theButt);
	var theEditorF=document.getElementById(sDlgType+'_editor');
	if (theEditorF) theEditorF.value=sFldName;
	var theActorF=document.getElementById(sDlgType+'_actor');
	if (theActorF) theActorF.value=sButtName;
	var theValueF=document.getElementById(sDlgType+'_value');
	if (theValueF) theValueF.value="";
	var theCurrF=document.getElementById(sDlgType+'_current');
	if (theCurrF) theCurrF.innerText="";
	//
	var lDlgLt=0;
	if (iLtDir) {
		lDlgLt=arrButtPos[0]+theButt.offsetWidth-theDialog.offsetWidth;
	} else {
		lDlgLt=arrButtPos[0];
	}
	if (lDlgLt<2) lDlgLt=2;
	theDialog.style.left=lDlgLt+"px";
	//
	var lDlgTp=0;
	if (iUpDir) {
		lDlgTp=arrButtPos[1]-theDialog.offsetHeight;
	} else {
		lDlgTp=arrButtPos[1];//+theButt.offsetHeight;
	}
	if (lDlgTp<2) lDlgTp=2;
	theDialog.style.top=lDlgTp+"px";
	//
	theDialog.style.visibility="visible";
}

function wdFindRangeTextNode(theCreator, theNeedleRange, theElToWalk, sMark) {
	var theTextNode=null;
	//
	var theHaystackRange=theCreator.createTextRange();
	theHaystackRange.moveToElementText(theElToWalk);
	theHaystackRange.collapse(true);
	//
	if (theElToWalk.childNodes.length) {
		var theNextNode=theElToWalk.childNodes(0);
		while (theNextNode && !theTextNode) {
			if (theNextNode.nodeType===3) { // text node
				theHaystackRange.collapse(false);
				var sToSearch=theNextNode.nodeValue;
				if (sToSearch.length) {
					var bFound=theHaystackRange.findText(sToSearch, 1000000000, 131072);//6);
					if (bFound && theHaystackRange.inRange(theNeedleRange)) {
						theTextNode=theNextNode;
					}
				}
			} else if (theNextNode.nodeType===1) {
				if (theNextNode.innerText.length) theHaystackRange.moveToElementText(theNextNode);
			}
			theNextNode=theNextNode.nextSibling;
		}
	}
	//
	theHaystackRange=null;
	theNeedleRange=null;
	//
	return theTextNode;
}
function wdEditorRangeContext(sSelType, theRange, theBody, sMark) {
	var arrAnswer=new Array();
		arrAnswer['PAREL']=null;
		arrAnswer['PARELTAG']="";
		arrAnswer['EXACTEL']=null;
		arrAnswer['EXACTELTAG']="";
		arrAnswer['PARBLOCKEL']=null;
		arrAnswer['PARBLOCKTAG']="";
		arrAnswer['EXACTBLOCK']=null;
		arrAnswer['EXACTBLOCKTAG']="";
		arrAnswer['PARTNODE']=null;
		arrAnswer['EXACTTNODE']=null;
		arrAnswer['TNODEEL']=null;
	//
	sSelType=sSelType.toUpperCase();
	//
	if (sSelType==='CONTROL') {
		arrAnswer['PAREL']=theRange.item(0);
	} else {
		arrAnswer['PAREL']=theRange.parentElement();
	}
	if (arrAnswer['PAREL'] && arrAnswer['PAREL'].tagName) arrAnswer['PARELTAG']=arrAnswer['PAREL'].tagName.toUpperCase();
	if (sSelType==='CONTROL') {
		arrAnswer['EXACTEL']=arrAnswer['PAREL'];
	} else {
		var sParObjText=arrAnswer['PAREL'].innerText;
		if (sParObjText) sParObjText=wd_TrimStr(sParObjText);
		var sRangeText=theRange.text;
		if (sRangeText) sRangeText=wd_TrimStr(sRangeText);
		if (sParObjText===sRangeText) arrAnswer['EXACTEL']=arrAnswer['PAREL'];
	}
	if (arrAnswer['EXACTEL'] && arrAnswer['EXACTEL'].tagName) arrAnswer['EXACTELTAG']=arrAnswer['EXACTEL'].tagName.toUpperCase();
	arrAnswer['PARBLOCKEL']=wd_ClimbToBlock(arrAnswer['PAREL']);
	if (arrAnswer['PARBLOCKEL'] && arrAnswer['PARBLOCKEL'].tagName) arrAnswer['PARBLOCKTAG']=arrAnswer['PARBLOCKEL'].tagName.toUpperCase();
	if (sSelType==='CONTROL') {
		arrAnswer['EXACTBLOCK']=arrAnswer['PARBLOCKEL'];
	} else {
		var sParBlockObjText=arrAnswer['PARBLOCKEL'].innerText;
		if (sParBlockObjText) sParBlockObjText=wd_TrimStr(sParBlockObjText);
		var sRangeText=theRange.text;
		if (sRangeText) sRangeText=wd_TrimStr(sRangeText);
		if (sParBlockObjText===sRangeText) arrAnswer['EXACTBLOCK']=arrAnswer['PARBLOCKEL'];
	}
	if (arrAnswer['EXACTBLOCK'] && arrAnswer['EXACTBLOCK'].tagName) arrAnswer['EXACTBLOCKTAG']=arrAnswer['EXACTBLOCK'].tagName.toUpperCase();
	//
	if (theBody && sSelType!=='CONTROL' && arrAnswer['PAREL']) {
		// find range parent TEXT node
		arrAnswer['PARTNODE']=wdFindRangeTextNode(theBody, theRange, arrAnswer['PAREL'], sMark);
		if (arrAnswer['PARTNODE']) {
			var sTextNodeText=arrAnswer['PARTNODE'].nodeValue;
			if (sTextNodeText) sTextNodeText=wd_TrimStr(sTextNodeText);
			var sRangeText=theRange.text;
			if (sRangeText) sRangeText=wd_TrimStr(sRangeText);
			if (sTextNodeText===sRangeText) arrAnswer['EXACTTNODE']=arrAnswer['PARTNODE'];
			//
			var sParObjText=arrAnswer['PAREL'].innerText;
			if (sParObjText) sParObjText=wd_TrimStr(sParObjText);
			if (sTextNodeText===sParObjText) arrAnswer['TNODEEL']=arrAnswer['PAREL'];
		}
	}
	//
	return arrAnswer;
}
var theInfoPane=null;
function wdEditorDetActions(sFldName, bFasterMode, sEventName) {
	if (!sEventName) var sEventName="";
	if (!bFasterMode) var bFasterMode=false;
	var arrContexts=new Array();
	/* detect available actions and marks buttons */
	// frame with edit doc:
	var sFraName=sFldName+'_editor';
	var theFrame=document.frames[sFraName]; // to work with iframe 'document' object
	// info panel
	if (!theInfoPane) theInfoPane=document.getElementById('wd_vsdtr_infopane');
	// selection and textrange
	if (theFrame) {
		var theFraDoc=theFrame.document;
		var theFraBody=theFraDoc.body;
		var theSel=theFraDoc.selection;
		var sSelType=theSel.type.toUpperCase();
		//
		var theRange=theSel.createRange();
		if (!bFasterMode || (sSelType==='NONE' || (sSelType==='TEXT' && !theRange.htmlText.length))) {
			theInfoPane.innerHTML="";
			theInfoPane.innerHTML+="EVENT: ["+sEventName+"]<BR/>";
			theInfoPane.innerHTML+="SELTYPE: ["+sSelType+"]<BR/><BR/>";
			//
			if (sSelType==='TEXT') {
				var iMoved=1;
				while (iMoved && (theRange.htmlText.length>=4 && (theRange.htmlText.substr(theRange.htmlText.length-4).toUpperCase()==="<BR>")) || (theRange.htmlText.length>=5 && (theRange.htmlText.substr(theRange.htmlText.length-5).toUpperCase()==="<BR/>"))) {
					iMoved=theRange.moveEnd('character', -1);
				}
				var iMoved=1;
				while (iMoved && (theRange.htmlText.length>=4 && (theRange.htmlText.substr(0, 4).toUpperCase()==="<BR>")) || (theRange.htmlText.length>=5 && (theRange.htmlText.substr(0, 5).toUpperCase()==="<BR/>"))) {
					iMoved=theRange.moveStart('character', 1);
				}
			}
			//
			var arrRangeContext=wdEditorRangeContext(sSelType, theRange, theFraBody, "+");
			if (sSelType==='TEXT') {
				var theStartRange=theRange.duplicate();
				theStartRange.collapse(true);
				var arrStartRangeContext=wdEditorRangeContext(sSelType, theStartRange, theFraBody, "^");
				//
				var theFinRange=theRange.duplicate();
				theFinRange.collapse(false);
				var arrFinRangeContext=wdEditorRangeContext(sSelType, theFinRange, theFraBody, "$");
			} else {
				var theStartRange=theRange;
				var theFinRange=theRange;
				var arrStartRangeContext=arrRangeContext;
				var arrFinRangeContext=arrRangeContext;
			}
			//
			theInfoPane.innerHTML+="<BR/>==== debug: ====<BR/>";
			//
			theInfoPane.innerHTML+="<BR/>==== selection: ====<BR/>";
			theInfoPane.innerHTML+="PAREL: "+arrRangeContext['PAREL']+"["+arrRangeContext['PARELTAG']+"]<BR/>";
			theInfoPane.innerHTML+="EXACTEL: "+arrRangeContext['EXACTEL']+"["+arrRangeContext['EXACTELTAG']+"]<BR/>";
			theInfoPane.innerHTML+="PARBLOCKEL: "+arrRangeContext['PARBLOCKEL']+"["+arrRangeContext['PARBLOCKTAG']+"]<BR/>";
			theInfoPane.innerHTML+="EXACTBLOCK: "+arrRangeContext['EXACTBLOCK']+"["+arrRangeContext['EXACTBLOCKTAG']+"]<BR/>";
			theInfoPane.innerHTML+="PARTNODE: ["+arrRangeContext['PARTNODE']+"]<BR/>";
			theInfoPane.innerHTML+="EXACTTNODE: ["+arrRangeContext['EXACTTNODE']+"]<BR/>";
			//
			theInfoPane.innerHTML+="<BR/>==== startpoint: ====<BR/>";
			theInfoPane.innerHTML+="PAREL: "+arrStartRangeContext['PAREL']+"["+arrStartRangeContext['PARELTAG']+"]<BR/>";
			theInfoPane.innerHTML+="EXACTEL: "+arrStartRangeContext['EXACTEL']+"["+arrStartRangeContext['EXACTELTAG']+"]<BR/>";
			theInfoPane.innerHTML+="PARELBLOCK: "+arrStartRangeContext['PARBLOCKEL']+"["+arrStartRangeContext['PARBLOCKTAG']+"]<BR/>";
			theInfoPane.innerHTML+="EXACTBLOCK: "+arrStartRangeContext['EXACTBLOCK']+"["+arrStartRangeContext['EXACTBLOCKTAG']+"]<BR/>";
			theInfoPane.innerHTML+="PARTNODE: ["+arrStartRangeContext['PARTNODE']+"]<BR/>";
			theInfoPane.innerHTML+="EXACTTNODE: ["+arrStartRangeContext['EXACTTNODE']+"]<BR/>";
			//
			theInfoPane.innerHTML+="<BR/>==== endpoint: ====<BR/>";
			theInfoPane.innerHTML+="PAREL: "+arrFinRangeContext['PAREL']+"["+arrFinRangeContext['PARELTAG']+"]<BR/>";
			theInfoPane.innerHTML+="EXACTEL: "+arrFinRangeContext['EXACTEL']+"["+arrFinRangeContext['EXACTELTAG']+"]<BR/>";
			theInfoPane.innerHTML+="PARBLOCKEL: "+arrFinRangeContext['PARBLOCKEL']+"["+arrFinRangeContext['PARBLOCKTAG']+"]<BR/>";
			theInfoPane.innerHTML+="EXACTBLOCK: "+arrFinRangeContext['EXACTBLOCK']+"["+arrFinRangeContext['EXACTBLOCKTAG']+"]<BR/>";
			theInfoPane.innerHTML+="PARTNODE: ["+arrFinRangeContext['PARTNODE']+"]<BR/>";
			theInfoPane.innerHTML+="EXACTTNODE: ["+arrFinRangeContext['EXACTTNODE']+"]<BR/>";
			//
			theInfoPane.innerHTML+="<BR/>==== analitics: ====<BR/>";
			theInfoPane.innerHTML+="MULTINODES: ["+(arrStartRangeContext['PAREL']!==arrFinRangeContext['PAREL'])+"]<BR/>";
			theInfoPane.innerHTML+="MULTIBLOCKS: ["+(arrStartRangeContext['PARBLOCKEL']!==arrFinRangeContext['PARBLOCKEL'])+"]<BR/>";
			theInfoPane.innerHTML+="MULTITEXTS: ["+(arrStartRangeContext['PARTNODE']!==arrFinRangeContext['PARTNODE'])+"]<BR/>";
			//
			arrContexts['tailrngs']=new Array();
			arrContexts['tailrngs']['START']=null;
			arrContexts['tailrngs']['EXACTSTARTTNODE']=null;
			arrContexts['tailrngs']['EXACTSTARTEL']=null;
			arrContexts['tailrngs']['EXACTSTARTBLOCK']=null;
			arrContexts['tailrngs']['FINISH']=null;
			arrContexts['tailrngs']['EXACTFINISHTNODE']=null;
			arrContexts['tailrngs']['EXACTFINISHEL']=null;
			arrContexts['tailrngs']['EXACTFINISHBLOCK']=null;
			if (sSelType==='TEXT') {
				if (arrStartRangeContext['PARTNODE']) {
					// make start tail
						// expand to text node finish
						var iMoved=1;
						while (iMoved && arrStartRangeContext['PARTNODE'] && wdFindRangeTextNode(theFraBody, theStartRange, theStartRange.parentElement(), '')===arrStartRangeContext['PARTNODE']) {
							iMoved=theStartRange.moveEnd('character', 1);
						}
						if (arrStartRangeContext['PARTNODE'] && theStartRange.text.length>0) theStartRange.moveEnd('character', -1);
						// limit by finish tail
						var iMoved=1;
						while (iMoved && theStartRange.text.length && theStartRange.compareEndPoints('EndToStart', theFinRange)===1) {
							iMoved=theStartRange.moveEnd('character', -1);
						}
						arrContexts['tailrngs']['START']=theStartRange;
					if (arrStartRangeContext['PARTNODE'] && arrStartRangeContext['PARTNODE'].nodeValue===theStartRange.text) {
						arrContexts['tailrngs']['EXACTSTARTTNODE']=arrStartRangeContext['PARTNODE'];
					}
					if (arrStartRangeContext['PAREL'] && arrStartRangeContext['PAREL'].innerText===theStartRange.text) {
						arrContexts['tailrngs']['EXACTSTARTEL']=arrStartRangeContext['PAREL'];
					}
					if (arrStartRangeContext['PARBLOCKEL'] && arrStartRangeContext['PARBLOCKEL'].innerText===theStartRange.text) {
						arrContexts['tailrngs']['EXACTSTARTBLOCK']=arrStartRangeContext['PARBLOCKEL'];
					}
				} else if (arrStartRangeContext['PAREL']) {
					//var iMoved=1;
					//while (iMoved && !) {
					//	
					//}
				}
				if (arrFinRangeContext['PARTNODE']) {
					// make finish tail
						// expand to text node start
						var iMoved=1;
						while (iMoved && arrFinRangeContext['PARTNODE'] && wdFindRangeTextNode(theFraBody, theFinRange, theFinRange.parentElement(), '')===arrFinRangeContext['PARTNODE']) {
							iMoved=theFinRange.moveStart('character', -1);
						}
						if (arrFinRangeContext['PARTNODE'] && theFinRange.text.length>0) theFinRange.moveStart('character', 1);
						// limit by start tail
						var iMoved=1;
						while (iMoved && theFinRange.text.length && theFinRange.compareEndPoints('StartToEnd', theStartRange)===-1) {
							iMoved=theFinRange.moveStart('character', 1);
						}
						arrContexts['tailrngs']['FINISH']=theFinRange;
					if (arrFinRangeContext['PARTNODE'] && arrFinRangeContext['PARTNODE'].nodeValue===theFinRange.text) {
						arrContexts['tailrngs']['EXACTFINISHTNODE']=arrFinRangeContext['PARTNODE'];
					}
					if (arrFinRangeContext['PAREL'] && arrFinRangeContext['PAREL'].innerText===theFinRange.text) {
						arrContexts['tailrngs']['EXACTFINISHEL']=arrFinRangeContext['PAREL'];
					}
					if (arrFinRangeContext['PARBLOCKEL'] && arrFinRangeContext['PARBLOCKEL'].innerText===theFinRange.text) {
						arrContexts['tailrngs']['EXACTFINISHBLOCK']=arrFinRangeContext['PARBLOCKEL'];
					}
				}
			}
			//
			arrContexts['startpoint']=arrStartRangeContext;
			arrContexts['endpoint']=arrFinRangeContext;
			arrContexts['selection']=arrRangeContext;
			arrContexts['analitics']=new Array();
			arrContexts['analitics']['MULTINODES'] = (arrStartRangeContext['PAREL'] !== arrFinRangeContext['PAREL']);
			arrContexts['analitics']['MULTIBLOCKS'] = (arrStartRangeContext['PARBLOCKEL'] !== arrFinRangeContext['PARBLOCKEL']);
			arrContexts['analitics']['MULTITEXTS'] = (arrStartRangeContext['PARTNODE'] !== arrFinRangeContext['PARTNODE']);
			//
			if (sSelType==='TEXT') {
				//
			} else if (sSelType==='NONE') {
				//
			} else if (sSelType==='CONTROL') {
				//
			}
		}
	} else {
		alert("can't detect editing frame");
	}
	//
	return arrContexts;
}

var wd_ArrTxtRngContnrs = new Array();
wd_ArrTxtRngContnrs['TD']=1; wd_ArrTxtRngContnrs['LI']=1;
wd_ArrTxtRngContnrs['OL']=1; wd_ArrTxtRngContnrs['UL']=1;
wd_ArrTxtRngContnrs['DIV']=1; wd_ArrTxtRngContnrs['A']=1;
wd_ArrTxtRngContnrs['BODY']=1; wd_ArrTxtRngContnrs['TH']=1;
wd_ArrTxtRngContnrs['FORM']=1; wd_ArrTxtRngContnrs['INPUT']=1;
wd_ArrTxtRngContnrs['P']=1; wd_ArrTxtRngContnrs['PRE']=1;
wd_ArrTxtRngContnrs['SPAN']=1;

var WD_SEL_EXACT=1;
var WD_SEL_CHAIN=2;
var WD_SEL_CNTRL=3;
var WD_SEL_NONE=4;
function wd_EditCommand(sFldName, sCommand, sParam1, sParam2) {
	//alert(sCommand+"\n"+sParam1+"\n"+sParam2);
	var bReturnAnswer=true;
	//
	var sButtName=sCommand;
	sCommand=sCommand.toUpperCase();
	//
	var sFraName=sFldName+'_editor';
	var theFrame=document.frames[sFraName]; // to work with iframe 'document' object
	if (theFrame) {
		theFrame.focus();
		var theFraDoc=theFrame.document;
		var theFraBody=theFraDoc.body;
		var theSel=theFraDoc.selection;
		var sSelType=theSel.type.toUpperCase();
		var theRange=theSel.createRange();
		var sDupBookmark = null;
		if (sSelType==="TEXT") sDupBookmark = theRange.getBookmark();
		//
		if (sSelType==="CONTROL") {
			var theObj=theRange.item(0);
		} else {
			if (sSelType==="TEXT") {
				var iMoved=1;
				while (iMoved && (theRange.htmlText.length>=4 && (theRange.htmlText.substr(theRange.htmlText.length-4).toUpperCase()==="<BR>")) || (theRange.htmlText.length>=5 && (theRange.htmlText.substr(theRange.htmlText.length-5).toUpperCase()==="<BR/>"))) {
					iMoved=theRange.moveEnd('character', -1);
				}
				var iMoved=1;
				while (iMoved && (theRange.htmlText.length>=4 && (theRange.htmlText.substr(0, 4).toUpperCase()==="<BR>")) || (theRange.htmlText.length>=5 && (theRange.htmlText.substr(0, 5).toUpperCase()==="<BR/>"))) {
					iMoved=theRange.moveStart('character', 1);
				}
				theRange.select();
			}
			var theObj=theRange.parentElement();
			var sOTag=theObj.tagName.toUpperCase();
			while (theObj!==theFraBody && theObj.parentElement && (!wd_ArrTxtRngContnrs[sOTag])) {
				theObj=theObj.parentElement;
				sOTag=theObj.tagName.toUpperCase();
			}
			// ==== to detect multiple cells selection:
			var theStartCell=null; var theFinCell=null;
			try {
				var theStartTRange=theRange.duplicate();
				theStartTRange.collapse(true);
				var theStartObj=theStartTRange.parentElement();
				theStartObj=wd_ClimbToTag(theStartObj, "TD");
				if (theStartObj && theStartObj.tagName && theStartObj.tagName.toUpperCase()==="TD") {
					theStartCell=theStartObj;
				}
				var theFinTRange=theRange.duplicate();
				theFinTRange.collapse(false);
				var theFinObj=theFinTRange.parentElement();
				theFinObj=wd_ClimbToTag(theFinObj, "TD");
				if (theFinObj && theFinObj.tagName && theFinObj.tagName.toUpperCase()==="TD") {
					theFinCell=theFinObj;
				}
			} catch (theError) {
				//alert(theError.description);
			}
		}
		var sTag=theObj.tagName.toUpperCase();
		var sClass=theObj.className;
		//
		var sTabTag=sTag;
		var theTabObj=theObj;
		if (!(sTabTag=="TABLE"||sTabTag=="TBODY"||sTabTag=="TR"||sTabTag=="TD"||sTabTag=="TH")) {
			while (!(sTabTag=="TABLE"||sTabTag=="TBODY"||sTabTag=="TR"||sTabTag=="TD"||sTabTag=="TH")&&theTabObj.offsetParent) {
				theTabObj=theTabObj.offsetParent;
				sTabTag=theTabObj.tagName.toUpperCase();
			}
		}
		var sTabClass=theTabObj.className;
		//
		var sDivTag=sTag;
		var theDivObj=theObj;
		if (sDivTag!="DIV") {
			while (sDivTag!="DIV" && theDivObj.parentElement) {
				theDivObj=theDivObj.parentElement;
				sDivTag=theDivObj.tagName.toUpperCase();
			}
		}
		var sDivClass=theDivObj.className;
		//
		var sAnchorTag=sTag;
		var theAnchorObj=theObj;
		if (sAnchorTag!="A") {
			while (sAnchorTag!="A" && theAnchorObj.parentElement) {
				theAnchorObj=theAnchorObj.parentElement;
				sAnchorTag=theAnchorObj.tagName.toUpperCase();
			}
		}
		var sAnchorClass=theAnchorObj.className;
		//
		var bInGlyph=(sDivTag=="DIV" && sDivClass.substr(0, 5)=="glyph");
		if (bInGlyph && !(sCommand=="ONKEYCODE" && sParam1==13)) {
			wd_wyschanged_mark=true;
			return true;
		}
		//
		//var theVisButt=document.getElementById(sFldName+"_visbutt");
		//var bCodeMode=true; if (theVisButt.disabled) bCodeMode=false;
		//
	}
	// show dialogs if need and return:
	var bNeedReturn=false;
	if (sParam1==='-') {
		switch (sCommand) {
			case "FCOLOR":
			case "FFAM":
			case "FSIZE":
			case "BCOLOR":
			case "FILL":
			case "USTYLES":
				// sParam2 - fall dialog name
				wd_ShowDialog(sParam2, sFldName, sButtName);
				bNeedReturn=true;
				break;
			default:
				break;
		}
	}
	if (bNeedReturn) return bNeedReturn;
	//
	var iSelMode=0;
	if (sSelType==="TEXT" && theRange.htmlText.length>0) {
		//var arrContexts=wdEditorDetActions(sFldName, false, 'user');
		var arrRgCntxt=wdEditorRangeContext(sSelType, theRange, theFraBody, "+");
		if ((arrRgCntxt['EXACTEL'] && arrRgCntxt['EXACTEL']!==theFraBody) || (arrRgCntxt['EXACTBLOCK'] && arrRgCntxt['EXACTBLOCK']!==theFraBody) || arrRgCntxt['EXACTTNODE']) {
			iSelMode=WD_SEL_EXACT;
			var RG_EXACTEL=arrRgCntxt['EXACTEL'];
			var RG_EXACTBLOCK=arrRgCntxt['EXACTBLOCK'];
			var RG_EXACTTNODE=arrRgCntxt['EXACTTNODE'];
		} else {
			iSelMode=WD_SEL_CHAIN;
			//
			var theStartSpan=theFraDoc.getElementById("wd_576879_Start");
			if (theStartSpan) theStartSpan.removeNode(true);
			var theFinSpan=theFraDoc.getElementById("wd_576879_Fin");
			if (theFinSpan) theFinSpan.removeNode(true);
			//
			var theStartRg=theRange.duplicate();
			theStartRg.collapse(true);
			theStartRg.pasteHTML("<SPAN ID=\"wd_576879_Start\"></SPAN>");
			var theFinRg=theRange.duplicate();
			theFinRg.collapse(false);
			theFinRg.pasteHTML("<SPAN ID=\"wd_576879_Fin\"></SPAN>");
			var theStartSpan=theFraDoc.getElementById("wd_576879_Start");
			var theFinSpan=theFraDoc.getElementById("wd_576879_Fin");
			var theCeilNode=wd_CommonAncestor(theStartSpan, theFinSpan);
			var arrStartChain=wd_PickupChain(theStartSpan, theCeilNode, 1);
			var arrFinChain=wd_PickupChain(theFinSpan, theCeilNode, 2);
			//
			// validate tails:
			var theStartEl=null;
			var theStartBlock=null;
			var theStartTNode=null;
			if (arrStartChain.length) {
				var theStartTail=arrStartChain[0];
				if (theStartTail.nodeType!==1) {
					theStartEl=theStartTail.parentNode;
					while (theStartEl.nodeType!==1) theStartEl=theStartEl.parentNode;
					if (wd_TrimStr(theStartEl.innerText)===wd_TrimStr(theStartTail.nodeValue)) {
						theStartTail=theStartEl;
					} else {
						var theStartTNode=wdFindRangeTextNode(theFraBody, theStartTRange, theStartEl.parentNode, '');
						if (theStartTNode && (wd_TrimStr(theStartTNode.nodeValue)===wd_TrimStr(theStartTail.nodeValue))) {
							theStartTail=theStartTNode;
						}
					}
				}
				arrStartChain[0]=theStartTail;
				if (theStartEl) theStartBlock=wd_ClimbToBlock(theStartEl);
			}
			//
			var theFinEl=null;
			var theFinBlock=null;
			var theFinTNode=null;
			if (arrFinChain.length) {
				var theFinTail=arrFinChain[0];
				if (theFinTail.nodeType!==1) {
					theFinEl=theFinTail.parentNode;
					while (theFinEl.nodeType!==1) theFinEl=theFinEl.parentNode;
					if (wd_TrimStr(theFinEl.innerText)===wd_TrimStr(theFinTail.nodeValue)) {
						theFinTail=theFinEl;
					} else {
						theFinTNode=wdFindRangeTextNode(theFraBody, theFinTRange, theFinTail.parentNode, '');
						if (theFinTNode && (wd_TrimStr(theFinTNode.nodeValue)===wd_TrimStr(theFinTail.nodeValue))) {
							theFinTail=theFinTNode;
						}
					}
				}
				arrFinChain[0]=theFinTail;
				if (theFinEl) theFinBlock=wd_ClimbToBlock(theFinEl);
			}
			//
			var arrMediumChain=new Array();
			if (arrStartChain.length) {
				var theSNode=arrStartChain[arrStartChain.length-1];
			} else {
				var theSNode=theStartSpan;
			}
			while (theSNode.parentNode && theSNode.parentNode!==theCeilNode && theSNode.parentNode!==theFraBody) {
				theSNode=theSNode.parentNode;
			}
			if (arrFinChain.length) {
				var theFNode=arrFinChain[arrFinChain.length-1];
			} else {
				var theFNode=theFinSpan;
			}
			while (theFNode.parentNode && theFNode.parentNode!==theCeilNode && theFNode.parentNode!==theFraBody) {
				theFNode=theFNode.parentNode;
			}
			//
			var theMSNode=theSNode;
			if (!(theMSNode.nodeType===1 && wd_ClimbToTag(theFNode, theMSNode.tagName)===theMSNode)) {
				while(theMSNode && theMSNode.nextSibling && theMSNode!==theFNode && theMSNode.nextSibling!==theFNode) {
					theMSNode=theMSNode.nextSibling;
					arrMediumChain[arrMediumChain.length]=theMSNode;
				}
			}
			//
			var arrEditChains=new Array(); var iJ=0;
			for (iJ=0; iJ<arrStartChain.length; iJ++) arrEditChains[arrEditChains.length]=arrStartChain[iJ];
			for (iJ=0; iJ<arrMediumChain.length; iJ++) arrEditChains[arrEditChains.length]=arrMediumChain[iJ];
			for (iJ=arrFinChain.length; iJ>0; iJ--) arrEditChains[arrEditChains.length]=arrFinChain[iJ-1];
			//
		}
	} else if (sSelType==="CONTROL") {
		iSelMode=WD_SEL_CNTRL;
		var theObj=theRange.item(0);
	} else {
		iSelMode=WD_SEL_NONE;
		var arrRgCntxt=wdEditorRangeContext(sSelType, theRange, theFraBody, "+");
		var RG_PARBLOCKEL=arrRgCntxt['PARBLOCKEL'];
		var RG_PAREL=arrRgCntxt['PAREL'];
		var RG_TNODEEL=arrRgCntxt['TNODEEL'];
		var RG_PARTNODE=arrRgCntxt['PARTNODE'];
	}
	//
	if (sCommand==='USTYLES') sCommand=sParam1.toUpperCase();
	switch (sCommand) {
		case "TEST2":
			if (iSelMode===1) {
				if (arrRgCntxt['EXACTBLOCK'] && arrRgCntxt['EXACTBLOCK']!==theFraBody) {
					var theNextNode=arrRgCntxt['EXACTBLOCK'];
					if (theNextNode.tagName==="TR") {
						var iNextCell=0;
						for (iNextCell=0; iNextCell<theNextNode.cells.length; iNextCell++) {
							theNextNode.cells(iNextCell).style.backgroundColor="blue";
						}
					} else if (theNextNode.tagName==="TABLE" || theNextNode.tagName==="THEAD" || theNextNode.tagName==="TBODY" || theNextNode.tagName==="TFOOT") {
						var iNextRow=0;
						for (iNextRow=0; iNextRow<theNextNode.rows.length; iNextRow++) {
							var iNextCell=0;
							for (iNextCell=0; iNextCell<theNextNode.rows(iNextRow).cells.length; iNextCell++) {
								theNextNode.rows(iNextRow).cells(iNextCell).style.backgroundColor="blue";
							}
						}
					} else {
						theNextNode.style.backgroundColor="blue";
					}
				} else if (arrRgCntxt['EXACTEL'] && arrRgCntxt['EXACTEL']!==theFraBody) {
					arrRgCntxt['EXACTEL'].style.backgroundColor="blue";
				} else if (arrRgCntxt['EXACTTNODE']) {
					var theNewSpan=theFraDoc.createElement("SPAN");
					theNewSpan.style.backgroundColor="blue";
					theNewSpan.innerText=arrRgCntxt['EXACTTNODE'].nodeValue;
					arrRgCntxt['EXACTTNODE'].replaceNode(theNewSpan);
				}
			} else if (iSelMode===2) {
				for (iNext=0; iNext<arrStartChain.length; iNext++) {
					var theNextNode=arrStartChain[iNext];
					if (theNextNode.nodeType===1) {
						if (theNextNode.tagName==="TR") {
							var iNextCell=0;
							for (iNextCell=0; iNextCell<theNextNode.cells.length; iNextCell++) {
								theNextNode.cells(iNextCell).style.backgroundColor="green";
							}
						} else if (theNextNode.tagName==="TABLE" || theNextNode.tagName==="THEAD" || theNextNode.tagName==="TBODY" || theNextNode.tagName==="TFOOT") {
							var iNextRow=0;
							for (iNextRow=0; iNextRow<theNextNode.rows.length; iNextRow++) {
								var iNextCell=0;
								for (iNextCell=0; iNextCell<theNextNode.rows(iNextRow).cells.length; iNextCell++) {
									theNextNode.rows(iNextRow).cells(iNextCell).style.backgroundColor="green";
								}
							}
						} else {
							theNextNode.style.backgroundColor="green";
						}
					} else {
						var theNewSpan=theFraDoc.createElement("SPAN");
						theNewSpan.style.backgroundColor="green";
						theNewSpan.innerText=theNextNode.nodeValue;
						theNextNode.replaceNode(theNewSpan);
					}
				}
				for (iNext=0; iNext<arrFinChain.length; iNext++) {
					var theNextNode=arrFinChain[iNext];
					if (theNextNode.nodeType===1) {
						if (theNextNode.tagName==="TR") {
							var iNextCell=0;
							for (iNextCell=0; iNextCell<theNextNode.cells.length; iNextCell++) {
								theNextNode.cells(iNextCell).style.backgroundColor="yellow";
							}
						} else if (theNextNode.tagName==="TABLE" || theNextNode.tagName==="THEAD" || theNextNode.tagName==="TBODY" || theNextNode.tagName==="TFOOT") {
							var iNextRow=0;
							for (iNextRow=0; iNextRow<theNextNode.rows.length; iNextRow++) {
								var iNextCell=0;
								for (iNextCell=0; iNextCell<theNextNode.rows(iNextRow).cells.length; iNextCell++) {
									theNextNode.rows(iNextRow).cells(iNextCell).style.backgroundColor="yellow";
								}
							}
						} else {
							theNextNode.style.backgroundColor="yellow";
						}
					} else {
						var theNewSpan=theFraDoc.createElement("SPAN");
						theNewSpan.style.backgroundColor="yellow";
						theNewSpan.innerText=theNextNode.nodeValue;
						theNextNode.replaceNode(theNewSpan);
					}
				}
				for (iNext=0; iNext<arrMediumChain.length; iNext++) {
					var theNextNode=arrMediumChain[iNext];
					if (theNextNode.nodeType===1) {
						if (theNextNode.tagName==="TR") {
							var iNextCell=0;
							for (iNextCell=0; iNextCell<theNextNode.cells.length; iNextCell++) {
								theNextNode.cells(iNextCell).style.backgroundColor="red";
							}
						} else if (theNextNode.tagName==="TABLE" || theNextNode.tagName==="THEAD" || theNextNode.tagName==="TBODY" || theNextNode.tagName==="TFOOT") {
							var iNextRow=0;
							for (iNextRow=0; iNextRow<theNextNode.rows.length; iNextRow++) {
								var iNextCell=0;
								for (iNextCell=0; iNextCell<theNextNode.rows(iNextRow).cells.length; iNextCell++) {
									theNextNode.rows(iNextRow).cells(iNextCell).style.backgroundColor="red";
								}
							}
						} else {
							theNextNode.style.backgroundColor="red";
						}
					} else {
						var theNewSpan=theFraDoc.createElement("SPAN");
						theNewSpan.style.backgroundColor="red";
						theNewSpan.innerText=theNextNode.nodeValue;
						theNextNode.replaceNode(theNewSpan);
					}
				}
			} else if (iSelMode===3) {
				theObj.style.backgroundColor="orange";
			} else if(iSelMode===4) {
				if (arrRgCntxt['PARBLOCKEL'] && arrRgCntxt['PARBLOCKEL']!==theFraBody) {
					var theNextNode=arrRgCntxt['PARBLOCKEL'];
					if (theNextNode.tagName==="TR") {
						var iNextCell=0;
						for (iNextCell=0; iNextCell<theNextNode.cells.length; iNextCell++) {
							theNextNode.cells(iNextCell).style.backgroundColor="olive";
						}
					} else if (theNextNode.tagName==="TABLE" || theNextNode.tagName==="THEAD" || theNextNode.tagName==="TBODY" || theNextNode.tagName==="TFOOT") {
						var iNextRow=0;
						for (iNextRow=0; iNextRow<theNextNode.rows.length; iNextRow++) {
							var iNextCell=0;
							for (iNextCell=0; iNextCell<theNextNode.rows(iNextRow).cells.length; iNextCell++) {
								theNextNode.rows(iNextRow).cells(iNextCell).style.backgroundColor="olive";
							}
						}
					} else {
						theNextNode.style.backgroundColor="olive";
					}
				} else if (arrRgCntxt['PAREL'] && arrRgCntxt['PAREL']!==theFraBody) {
					arrRgCntxt['PAREL'].style.backgroundColor="olive";
				} else if (arrRgCntxt['PARTNODE']) {
					var theNewSpan=theFraDoc.createElement("SPAN");
					theNewSpan.style.backgroundColor="olive";
					theNewSpan.innerText=arrRgCntxt['PARTNODE'].nodeValue;
					arrRgCntxt['PARTNODE'].replaceNode(theNewSpan);
				}
			}
			break;
		case "TEST":
			var arrContexts=wdEditorDetActions(sFldName, false, 'user');
			var theStartNode=null;
			if (arrContexts['startpoint']['PARTNODE']) {
				theStartNode=arrContexts['startpoint']['PARTNODE'];
			} else if (arrContexts['startpoint']['PAREL']) {
				theStartNode=arrContexts['startpoint']['PAREL'];
			} else if (arrContexts['startpoint']['PARELBLOCK']) {
				theStartNode=arrContexts['startpoint']['PARELBLOCK'];
			}
			var theFinNode=null;
			if (arrContexts['endpoint']['PARTNODE']) {
				theFinNode=arrContexts['endpoint']['PARTNODE'];
			} else if (arrContexts['endpoint']['PAREL']) {
				theFinNode=arrContexts['endpoint']['PAREL'];
			} else if (arrContexts['endpoint']['PARELBLOCK']) {
				theFinNode=arrContexts['endpoint']['PARELBLOCK'];
			}
			var theLimitNode=null;
			if (arrContexts['selection']['PARTNODE']) {
				theLimitNode=arrContexts['selection']['PARTNODE'];
			} else if (arrContexts['selection']['PAREL']) {
				theLimitNode=arrContexts['selection']['PAREL'];
			} else if (arrContexts['selection']['PARELBLOCK']) {
				theLimitNode=arrContexts['selection']['PARELBLOCK'];
			}
			var arrChain=new Array();
			var arrStartChain=wd_PickupChain(theStartNode, theLimitNode, 1);
			var arrFinChain=wd_PickupChain(theFinNode, theLimitNode, 2);
			//
			var arrMediumChain=new Array();
			if (arrStartChain.length) {
				var theSNode=arrStartChain[arrStartChain.length-1];
			} else {
				var theSNode=theStartNode;
			}
			while (theSNode.parentNode && theSNode.parentNode!==theLimitNode && theSNode.parentNode!==theFraBody) {
				theSNode=theSNode.parentNode;
			}
			if (arrFinChain.length) {
				var theFNode=arrFinChain[arrFinChain.length-1];
			} else {
				var theFNode=theFinNode;
			}
			while (theFNode.parentNode && theFNode.parentNode!==theLimitNode && theFNode.parentNode!==theFraBody) {
				theFNode=theFNode.parentNode;
			}
			//var theMSNode=theSNode.nextSibling;
			var theMSNode=theSNode;
			if (!(theMSNode.nodeType===1 && wd_ClimbToTag(theFNode, theMSNode.tagName)===theMSNode)) {
				while(theMSNode && theMSNode.nextSibling && theMSNode!==theFNode && theMSNode.nextSibling!==theFNode) {
					theMSNode=theMSNode.nextSibling;
					arrMediumChain[arrMediumChain.length]=theMSNode;
				}
			}
			//
			var iNext=0;
			for (iNext=0; iNext<arrStartChain.length; iNext++) {
				var theNextNode=arrStartChain[iNext];
				if (theNextNode.nodeType===1) {
					if (theNextNode.tagName==="TR") {
						var iNextCell=0;
						for (iNextCell=0; iNextCell<theNextNode.cells.length; iNextCell++) {
							theNextNode.cells(iNextCell).style.backgroundColor="green";
						}
					} else if (theNextNode.tagName==="TABLE" || theNextNode.tagName==="THEAD" || theNextNode.tagName==="TBODY" || theNextNode.tagName==="TFOOT") {
						var iNextRow=0;
						for (iNextRow=0; iNextRow<theNextNode.rows.length; iNextRow++) {
							var iNextCell=0;
							for (iNextCell=0; iNextCell<theNextNode.rows(iNextRow).cells.length; iNextCell++) {
								theNextNode.rows(iNextRow).cells(iNextCell).style.backgroundColor="green";
							}
						}
					} else {
						theNextNode.style.backgroundColor="green";
					}
				} else {
					var theNewSpan=theFraDoc.createElement("SPAN");
					theNewSpan.style.backgroundColor="green";
					theNewSpan.innerText=theNextNode.nodeValue;
					theNextNode.replaceNode(theNewSpan);
				}
			}
			for (iNext=0; iNext<arrFinChain.length; iNext++) {
				var theNextNode=arrFinChain[iNext];
				if (theNextNode.nodeType===1) {
					if (theNextNode.tagName==="TR") {
						var iNextCell=0;
						for (iNextCell=0; iNextCell<theNextNode.cells.length; iNextCell++) {
							theNextNode.cells(iNextCell).style.backgroundColor="yellow";
						}
					} else if (theNextNode.tagName==="TABLE" || theNextNode.tagName==="THEAD" || theNextNode.tagName==="TBODY" || theNextNode.tagName==="TFOOT") {
						var iNextRow=0;
						for (iNextRow=0; iNextRow<theNextNode.rows.length; iNextRow++) {
							var iNextCell=0;
							for (iNextCell=0; iNextCell<theNextNode.rows(iNextRow).cells.length; iNextCell++) {
								theNextNode.rows(iNextRow).cells(iNextCell).style.backgroundColor="yellow";
							}
						}
					} else {
						theNextNode.style.backgroundColor="yellow";
					}
				} else {
					var theNewSpan=theFraDoc.createElement("SPAN");
					theNewSpan.style.backgroundColor="yellow";
					theNewSpan.innerText=theNextNode.nodeValue;
					theNextNode.replaceNode(theNewSpan);
				}
			}
			for (iNext=0; iNext<arrMediumChain.length; iNext++) {
				var theNextNode=arrMediumChain[iNext];
				if (theNextNode.nodeType===1) {
					if (theNextNode.tagName==="TR") {
						var iNextCell=0;
						for (iNextCell=0; iNextCell<theNextNode.cells.length; iNextCell++) {
							theNextNode.cells(iNextCell).style.backgroundColor="red";
						}
					} else if (theNextNode.tagName==="TABLE" || theNextNode.tagName==="THEAD" || theNextNode.tagName==="TBODY" || theNextNode.tagName==="TFOOT") {
						var iNextRow=0;
						for (iNextRow=0; iNextRow<theNextNode.rows.length; iNextRow++) {
							var iNextCell=0;
							for (iNextCell=0; iNextCell<theNextNode.rows(iNextRow).cells.length; iNextCell++) {
								theNextNode.rows(iNextRow).cells(iNextCell).style.backgroundColor="red";
							}
						}
					} else {
						theNextNode.style.backgroundColor="red";
					}
				} else {
					var theNewSpan=theFraDoc.createElement("SPAN");
					theNewSpan.style.backgroundColor="red";
					theNewSpan.innerText=theNextNode.nodeValue;
					theNextNode.replaceNode(theNewSpan);
				}
			}
			// теперь осталось два хвоста - сначала и в конце
			var theStartTailRange=arrContexts['tailrngs']['START'];
			var theFinTailRange=arrContexts['tailrngs']['FINISH'];
			if (theStartTailRange && theFinTailRange && theStartTailRange.isEqual(theFinTailRange)) {
				if (arrContexts['tailrngs']['EXACTSTARTBLOCK']) {
					arrContexts['tailrngs']['EXACTSTARTBLOCK'].style.backgroundColor="blue";
				} else if (arrContexts['tailrngs']['EXACTSTARTEL']) {
					arrContexts['tailrngs']['EXACTSTARTEL'].style.backgroundColor="blue";
				} else if (arrContexts['tailrngs']['EXACTSTARTTNODE']) {
					var theNewSpan=theFraDoc.createElement("SPAN");
					theNewSpan.style.backgroundColor="blue";
					theNewSpan.innerText=arrContexts['tailrngs']['EXACTSTARTTNODE'].nodeValue;
					arrContexts['tailrngs']['EXACTSTARTTNODE'].replaceNode(theNewSpan);
				} else if (theStartTailRange && theStartTailRange.text.length) {
					theStartTailRange.pasteHTML("<SPAN STYLE=\"background-color:blue;\">"+theStartTailRange.htmlText+"</SPAN>");
				}
			} else {
				// first fin then start !!!
				if (arrContexts['tailrngs']['EXACTFINISHBLOCK']) {
					arrContexts['tailrngs']['EXACTFINISHBLOCK'].style.backgroundColor="olive";
				} else if (arrContexts['tailrngs']['EXACTFINISHEL']) {
					arrContexts['tailrngs']['EXACTFINISHEL'].style.backgroundColor="olive";
				} else if (arrContexts['tailrngs']['EXACTFINISHTNODE']) {
					var theNewSpan=theFraDoc.createElement("SPAN");
					theNewSpan.style.backgroundColor="olive";
					theNewSpan.innerText=arrContexts['tailrngs']['EXACTFINISHTNODE'].nodeValue;
					arrContexts['tailrngs']['EXACTFINISHTNODE'].replaceNode(theNewSpan);
				} else if (theFinTailRange && theFinTailRange.text.length) {
					theFinTailRange.pasteHTML("<SPAN STYLE=\"background-color:olive;\">"+theFinTailRange.htmlText+"</SPAN>");
				}
				if (arrContexts['tailrngs']['EXACTSTARTBLOCK']) {
					arrContexts['tailrngs']['EXACTSTARTBLOCK'].style.backgroundColor="orange";
				} else if (arrContexts['tailrngs']['EXACTSTARTEL']) {
					arrContexts['tailrngs']['EXACTSTARTEL'].style.backgroundColor="orange";
				} else if (arrContexts['tailrngs']['EXACTSTARTTNODE']) {
					var theNewSpan=theFraDoc.createElement("SPAN");
					theNewSpan.style.backgroundColor="orange";
					theNewSpan.innerText=arrContexts['tailrngs']['EXACTSTARTTNODE'].nodeValue;
					arrContexts['tailrngs']['EXACTSTARTTNODE'].replaceNode(theNewSpan);
				} else if (theStartTailRange && theStartTailRange.text.length) {
					theStartTailRange.pasteHTML("<SPAN STYLE=\"background-color:orange;\">"+theStartTailRange.htmlText+"</SPAN>");
				}
			}
			//arrContexts['startpoint']
			//arrContexts['endpoint']
			//arrContexts['selection']
			//arrContexts['analitics']
			break;
		case "ONPASTE":
			//http://www.kruglov.ru/useful/cleanhtml/
			//http://xpoint.ru/forums/programming/javascript/misc/thread/23753.xhtml
			//http://xpoint.ru/forums/programming/javascript/misc/thread/31509.xhtml
			try {
				var theIFra=window.parent.document.frames['wd_visedit_paster'];
				if (theIFra) {
					var theDoc=theIFra.document;
					if (theDoc) {
						var theBody=theDoc.body;
						if (theBody) {
							theBody.innerHTML="";
							theBody.focus();
							theDoc.execCommand("PASTE");
							if (wd_visedit_b_HtmlMode) {
								var sCleaned=theBody.innerText;
								theRange.text=sCleaned;
							} else {
								var sWasted=theBody.innerHTML;
								theBody.innerHTML="";
								var theDiv=document.createElement("DIV");
								theDiv.innerHTML=sWasted;
								cleanTree(theDiv, false);
								var sCleaned=theDiv.innerHTML;
								theRange.pasteHTML(sCleaned);
								theDiv.removeNode(true);
							}
						}
					}
					// для баги с прорисовкой после вставки: //
					window.parent.opener.focus();
					window.setTimeout("document.frames['"+sFraName+"'].focus();", 200);
				}
			} catch (theError) {
				//alert("error:\n"+theError.description);
			}
			wd_wyschanged_mark=true;
			bReturnAnswer=false;
			break;
		case "ONKEYCODE":
			/*
			для обработки нажатия клавиш (см. wd_MkViseditorsPrepareing())
			sParam1 - keyCode, sParam2 - frame event Object
			*/
			switch (sParam1) { // sParam2=event
				case 13: // ENTER
					if (sParam2.ctrlKey) {
						if (sDivTag=="DIV") {
							theRange.moveToElementText(theDivObj);
							theRange.collapse(false);
							theRange.pasteHTML("<BR>");
							theRange.collapse(false);
							theRange.select();
						}
						bReturnAnswer=false;
					} else if (sTag!="LI") {
						if (sSelType != "CONTROL") {
							theRange.collapse(true);
							if (sOTag=="PRE") {
								theRange.text="\n ";
							} else {
								theRange.pasteHTML("<BR>");
							}
							theRange.select();
						}
						bReturnAnswer=false;
					}
					break;
				case 16: // Shift
				case 17: // Ctrl
				case 18: // Alt
					bReturnAnswer=true;
					break;
				case 49: // 1 = STYLE1 - block style 1
				case 50: // 2 = STYLE2 - block style 2
				case 51: // 3 = STYLE3 - block style 3
				case 52: // 4 = STYLE4 - inline style 1
				case 53: // 5 = STYLE5 - inline style 2
				case 54: // 6 = STYLE6 - inline style 3
					if (sParam2.ctrlKey) {
						wd_EditCommand(sFldName, "STYLE"+(sParam1-48), "", "");
						bReturnAnswer=false;
					}
					break;
				case 66: // B = Bold
				case 73: // I = Italic
				case 84: // T = Table
					if (sParam2.ctrlKey) {
						wd_EditCommand(sFldName, String.fromCharCode(sParam1), "", "");
						bReturnAnswer=false;
					}
					break;
				default:
					break;
			}
			wd_wyschanged_mark=true;
			break;
		case 'STYLE7':
		case 'STYLE8':
		case 'STYLE9':
		case 'STYLE0':
			switch (sParam1) {
				case 'style0':
					var sNeedVal="H4";
					break;
				case 'style9':
					var sNeedVal="H3";
					break;
				case 'style8':
					var sNeedVal="H2";
					break;
				case 'style7':
				default:
					var sNeedVal="H1";
					break;
			}
			//
			var theGoal=null;
			if (iSelMode===WD_SEL_EXACT) {
				if (RG_EXACTBLOCK && RG_EXACTBLOCK!==theFraBody) {
					theGoal=RG_EXACTBLOCK;
				} else if (RG_EXACTEL && RG_EXACTEL!==theFraBody) {
					theGoal=RG_EXACTEL;
				} else if (RG_EXACTTNODE) {
					theGoal=RG_EXACTTNODE;
				}
			} else if (iSelMode===WD_SEL_CHAIN) {
				if (arrEditChains.length) {
					var iNext=0;
					for (iNext=0; iNext<arrEditChains.length; iNext++) {
						wd_Edit_MkNodeHn(theFraDoc, arrEditChains[iNext], sNeedVal, 1);
					}
					arr_Wd_Edit_Removed=new Array();
					var iNext=0; for (iNext=0; iNext<arrEditChains.length; iNext++) {
						var bMayBeTouched=true; var iJ=0;
						for (iJ=0; iJ<arr_Wd_Edit_Removed.length; iJ++) {
							if (arr_Wd_Edit_Removed[iJ]===arrEditChains[iNext]) {
								bMayBeTouched=false;
								break;
							}
						}
						if (bMayBeTouched) {
							wd_Edit_MkNodeHn(theFraDoc, arrEditChains[iNext], sNeedVal, 1);
						}
					}
					arr_Wd_Edit_Removed=new Array();
				}
			} else if (iSelMode===WD_SEL_CNTRL) {
				if (wd_IsEl(theObj, 'TABLE')) theGoal=theObj;
			} else if (iSelMode===WD_SEL_NONE) {
				if (RG_TNODEEL && RG_TNODEEL!==theFraBody) {
					theGoal=RG_TNODEEL;
				} else if (RG_PARTNODE) {
					theGoal=RG_PARTNODE;
				} else if (RG_PAREL && RG_PAREL!==theFraBody) {
					theGoal=RG_PAREL;
				}
			}
			if (theGoal) {
				wd_Edit_MkNodeHn(theFraDoc, theGoal, sNeedVal, 1);
			}
			break;
		case 'STYLE4':
		case 'STYLE5':
		case 'STYLE6':
		case 'BOLD':
		case 'B':
		case 'ITALIC':
		case 'I':
		case 'FCOLOR':
		case 'FFAM':
		case 'FSIZE':
		case 'BCOLOR':
		case 'FILL':
//alert("iSelMode="+iSelMode);
			var sNeedVal=sParam1;
			if ((sCommand==="BCOLOR" || sCommand==="FILL" || sCommand==="FCOLOR") && typeof(arrPaletteCodes[sNeedVal])!=='undefined') {
				sNeedVal=arrPaletteCodes[sNeedVal];
			} else if (sCommand.substr(0, 5)==='STYLE') {
				sNeedVal="tpl_"+sCommand.toLowerCase();
			}
			//
			var theGoal=null;
			if (iSelMode===WD_SEL_EXACT) {
				if (RG_EXACTBLOCK && RG_EXACTBLOCK!==theFraBody) {
					theGoal=RG_EXACTBLOCK;
				} else if (RG_EXACTEL && RG_EXACTEL!==theFraBody) {
					theGoal=RG_EXACTEL;
				} else if (RG_EXACTTNODE) {
					theGoal=RG_EXACTTNODE;
				}
			} else if (iSelMode===WD_SEL_CHAIN) {
				if (arrEditChains.length) {
					if (sCommand==="ITALIC" || sCommand==="I") {
						var theTestEl=arrEditChains[0];
						if (theTestEl.nodeType && theTestEl.nodeType===3) theTestEl=theTestEl.parentNode;
						sNeedVal=wd_Edit_TestNeedFStyle(theTestEl);
					} else if (sCommand==="BOLD" || sCommand==="B") {
						var theTestEl=arrEditChains[0];
						if (theTestEl.nodeType && theTestEl.nodeType===3) theTestEl=theTestEl.parentNode;
						sNeedVal=wd_Edit_TestNeedFWeight(theTestEl);
					}
					var iNext=0; for (iNext=0; iNext<arrEditChains.length; iNext++) {
						if (sCommand==="FFAM") {
							wd_Edit_MkNodeFFamily(theFraDoc, arrEditChains[iNext], sNeedVal);
						} else if (sCommand==="FSIZE") {
							wd_Edit_MkNodeFSize(theFraDoc, arrEditChains[iNext], sNeedVal);
						} else if (sCommand==="BCOLOR") {
							wd_Edit_MkNodeBgColor(theFraDoc, arrEditChains[iNext], sNeedVal);
						} else if (sCommand==="FILL") {
							wd_Edit_MkNodeFill(theFraDoc, arrEditChains[iNext], sNeedVal);
						} else if (sCommand==="FCOLOR") {
							wd_Edit_MkNodeFColor(theFraDoc, arrEditChains[iNext], sNeedVal);
						} else if (sCommand==="ITALIC" || sCommand==="I") {
							wd_Edit_MkNodeItalic(theFraDoc, arrEditChains[iNext], sNeedVal);
						} else if (sCommand==="BOLD" || sCommand==="B") {
							wd_Edit_MkNodeBold(theFraDoc, arrEditChains[iNext], sNeedVal);
						} else if (sCommand.substr(0, 5)==='STYLE') {
							wd_Edit_MkNodeClass(theFraDoc, arrEditChains[iNext], sNeedVal, 1);
						}
						
					}
				}
			} else if (iSelMode===WD_SEL_CNTRL) {
//alert('WD_SEL_CNTRL'+"\n"+"TABLE="+wd_IsEl(theObj, 'TABLE'));
				if (wd_IsEl(theObj, 'TABLE') || (wd_IsEl(theObj, 'IMG') && (sCommand==="BCOLOR" || sCommand==="FILL"))) {
					theGoal=theObj;
				}
			} else if (iSelMode===WD_SEL_NONE) {
				if (sCommand==="FILL") {
					if (RG_PARBLOCKEL && RG_PARBLOCKEL!==theFraBody) {
						theGoal=RG_PARBLOCKEL;
					} else if (RG_PAREL && RG_PAREL!==theFraBody) {
						theGoal=RG_PAREL;
					} else if (RG_TNODEEL && RG_TNODEEL!==theFraBody) {
						theGoal=RG_TNODEEL;
					} else if (RG_PARTNODE) {
						theGoal=RG_PARTNODE;
					}
				} else {
					if (RG_TNODEEL && RG_TNODEEL!==theFraBody) {
						theGoal=RG_TNODEEL;
					} else if (RG_PARTNODE) {
						theGoal=RG_PARTNODE;
					} else if (RG_PAREL && RG_PAREL!==theFraBody) {
						theGoal=RG_PAREL;
					}
				}
			}
			if (theGoal) {
				if (sCommand==="FFAM") {
					wd_Edit_MkNodeFFamily(theFraDoc, theGoal, sNeedVal);
				} else if (sCommand==="FSIZE") {
					wd_Edit_MkNodeFSize(theFraDoc, theGoal, sNeedVal);
				} else if (sCommand==="BCOLOR") {
					wd_Edit_MkNodeBgColor(theFraDoc, theGoal, sNeedVal);
				} else if (sCommand==="FILL") {
					wd_Edit_MkNodeFill(theFraDoc, theGoal, sNeedVal);
				} else if (sCommand==="FCOLOR") {
					wd_Edit_MkNodeFColor(theFraDoc, theGoal, sNeedVal);
				} else if (sCommand==="ITALIC" || sCommand==="I") {
					wd_Edit_MkNodeItalic(theFraDoc, theGoal, false);
				} else if (sCommand==="BOLD" || sCommand==="B") {
					wd_Edit_MkNodeBold(theFraDoc, theGoal, false);
				} else if (sCommand.substr(0, 5)==='STYLE') {
					wd_Edit_MkNodeClass(theFraDoc, theGoal, sNeedVal, 1);
				}
			}
			break;
		case 'STYLE1':
		case 'STYLE2':
		case 'STYLE3':
		case 'ALLEFT':
		case 'ALRIGHT':
		case 'ALCENTER':
		case 'ALJUSTIFY':
		case 'AL':
			if (sCommand.substr(0, 5)==='STYLE') {
				var sNeedVal="tpl_"+sCommand.toLowerCase();
			} else {
				var sNeedVal=sCommand.substr(2).toLowerCase();
			}
			var theGoal=null;
			if (typeof(arrPaletteCodes[sColor])!=='undefined') sColor=arrPaletteCodes[sColor];
			if (iSelMode===WD_SEL_EXACT) {
				if (RG_EXACTBLOCK && RG_EXACTBLOCK!==theFraBody) {
					theGoal=RG_EXACTBLOCK;
				} else if (RG_EXACTEL && RG_EXACTEL!==theFraBody) {
					theGoal=RG_EXACTEL;
				} else if (RG_EXACTTNODE) {
					theGoal=RG_EXACTTNODE;
				}
			} else if (iSelMode===WD_SEL_CHAIN) {
				if (arrEditChains.length) {
					arr_Wd_Edit_Removed=new Array();
					var iNext=0; for (iNext=0; iNext<arrEditChains.length; iNext++) {
						var bMayBeTouched=true; var iJ=0;
						for (iJ=0; iJ<arr_Wd_Edit_Removed.length; iJ++) {
							if (arr_Wd_Edit_Removed[iJ]===arrEditChains[iNext]) {
								bMayBeTouched=false;
								break;
							}
						}
						if (bMayBeTouched) {
							if (sCommand.substr(0, 5)==='STYLE') {
								wd_Edit_MkNodePStyle(theFraDoc, arrEditChains[iNext], sNeedVal, 1);
							} else {
								wd_Edit_MkNodeTAlign(theFraDoc, arrEditChains[iNext], sNeedVal);
							}
						}
					}
					arr_Wd_Edit_Removed=new Array();
				}
			} else if (iSelMode===WD_SEL_CNTRL) {
				if (wd_IsEl(theObj, 'TABLE') || wd_IsEl(theObj, 'IMG')) theGoal=theObj;
			} else if (iSelMode===WD_SEL_NONE) {
				if (RG_PARBLOCKEL && RG_PARBLOCKEL!==theFraBody) {
					theGoal=RG_PARBLOCKEL;
				} else if (RG_PAREL && RG_PAREL!==theFraBody) {
					theGoal=RG_PAREL;
				} else if (RG_TNODEEL && RG_TNODEEL!==theFraBody) {
					theGoal=RG_TNODEEL;
				} else if (RG_PARTNODE) {
					theGoal=RG_PARTNODE;
				}
			}
			if (theGoal) {
				if (sCommand.substr(0, 5)==='STYLE') {
					wd_Edit_MkNodePStyle(theFraDoc, theGoal, sNeedVal, 1);
				} else {
					wd_Edit_MkNodeTAlign(theFraDoc, theGoal, sNeedVal);
				}
			}
			break;
		case "OL":
		case "UL":
			if (sSelType!=='CONTROL') {
				var theCurrText=theRange.htmlText;
				theCurrText=theCurrText.replace(/<BR>/gi, "</LI><LI>");
				theCurrText="<"+sCommand+"><LI>"+theCurrText+"</LI></"+sCommand+">";
				theRange.pasteHTML(theCurrText);
			} else {
				if (sSelType=="NONE") {
					theRange.text="\r\n<"+sCommand+">\r\n<LI>\r\n</"+sCommand+">\r\n";
				} else if (sSelType=="TEXT") {
					theRange.text="\r\n<"+sCommand+">\r\n<LI>"+theRange.text+"\r\n</"+sCommand+">\r\n";
				}
			}
			break;
		case "TABLE":
		case "T":
			if (sSelType!="CONTROL") {
				theRange.collapse(false);
				theRange.pasteHTML("<TABLE WIDTH=\"100%\" CELLSPACING=\""+(typeof(iWDTblCellspacing)=="undefined"?"0":iWDTblCellspacing)+"\" BORDER=\""+(typeof(iWDTblOpts3D)=="undefined"?"0":iWDTblOpts3D)+"\" CLASS=\"WDTBL\"><THEAD><TR><TH CLASS=\"WDTH\">A</TH><TH CLASS=\"WDTH\">B</TH><TH CLASS=\"WDTH\">C</TH><TH CLASS=\"WDTH\">D</TH></TR></THEAD><TBODY><TR><TD CLASS=\"WDTD\">A1</TD><TD CLASS=\"WDTD\">B1</TD><TD CLASS=\"WDTD\">C1</TD><TD CLASS=\"WDTD\">D1</TD></TR><TR><TD CLASS=\"WDTD\">A2</TD><TD CLASS=\"WDTD\">B2</TD><TD CLASS=\"WDTD\">C2</TD><TD CLASS=\"WDTD\">D2</TD></TR><TR><TD CLASS=\"WDTD\">A3</TD><TD CLASS=\"WDTD\">B3</TD><TD CLASS=\"WDTD\">C3</TD><TD CLASS=\"WDTD\">D3</TD></TR></TBODY></TABLE>");
			}
			break;
		case "COL":
			if ((sTabTag=="TABLE"||sTabTag=="TBODY"||sTabTag=="TR"||sTabTag=="TD"||sTabTag=="TH")) {
				var iIndex=-1;
				var theTable=theTabObj;
				var iI=0;
				var theTable=theTabObj;
				if (sTabTag=="TD"||sTabTag=="TH") {
					iIndex=theTabObj.cellIndex;
					var theTR=theTabObj.parentElement;
					var theTmp=theTR.parentElement;
					while (theTmp&&theTmp.tagName&&theTmp.tagName.toUpperCase()!="TABLE") {
						theTmp=theTmp.parentElement;
					}
					theTable=theTmp;
				} else if (sTabTag=="TR") {
					var theTmp=theTR.parentElement;
					while (theTmp&&theTmp.tagName&&theTmp.tagName.toUpperCase()!="TABLE") {
						theTmp=theTmp.parentElement;
					}
					theTable=theTmp;
				}
				for (iI=0; iI<theTable.rows.length; iI++) {
					var theNextTD=theTable.rows[iI].insertCell(iIndex);
					var iSampleIndex=0; if (iIndex==0) iSampleIndex=1;
					theNextTD.className=theTable.rows[iI].cells[iSampleIndex].className;
				}
			}
			break;
		case "DELCOL":
			if ((sTabTag=="TABLE"||sTabTag=="TBODY"||sTabTag=="TR"||sTabTag=="TD"||sTabTag=="TH")) {
				var iIndex=-1;
				var theTable=theTabObj;
				var iI=0;
				var theTable=theTabObj;
				if (sTabTag=="TD"||sTabTag=="TH") {
					iIndex=theTabObj.cellIndex;
					var theTR=theTabObj.parentElement;
					var theTmp=theTR.parentElement;
					while (theTmp&&theTmp.tagName&&theTmp.tagName.toUpperCase()!="TABLE") {
						theTmp=theTmp.parentElement;
					}
					theTable=theTmp;
				}
				for (iI=0; iI<theTable.rows.length; iI++) theTable.rows[iI].deleteCell(iIndex);
			}
			break;
		case "ROW":
			if ((sTabTag=="TABLE"||sTabTag=="TBODY"||sTabTag=="TR"||sTabTag=="TD"||sTabTag=="TH")) {
				var iIndex=-1;
				var theTable=theTabObj;
				var iI=0;
				if (sTabTag=="TD"||sTabTag=="TH"||sTabTag=="TR") {
					if (sTabTag=="TD"||sTabTag=="TH") {
						var theTR=theTabObj.parentElement;
					} else {
						var theTR=theTabObj;
					}
					iIndex=theTR.rowIndex;
					var theTmp=theTR.parentElement;
					while (theTmp&&theTmp.tagName&&theTmp.tagName.toUpperCase()!="TABLE") {
						theTmp=theTmp.parentElement;
					}
					theTable=theTmp;
				} else {
					var theTR=theTable.rows[theTable.rows.length-1];
				}
				var iCells=theTable.rows[0].cells.length;
				var theNewTR=theTable.insertRow(iIndex);
				var iSampleIndex=0; if (iIndex==0) iSampleIndex=1;
				var sClass=theTR.cells[0].className;
				for (iI=0; iI<iCells; iI++) {
					var theNextTD=theNewTR.insertCell();
					theNextTD.className=sClass;
				}
			}
			break;
		case "DELROW":
			if ((sTabTag=="TABLE"||sTabTag=="TBODY"||sTabTag=="TR"||sTabTag=="TD"||sTabTag=="TH")) {
				var iIndex=-1;
				var theTable=theTabObj;
				if (sTabTag=="TD"||sTabTag=="TH"||sTabTag=="TR") {
					if (sTabTag=="TD"||sTabTag=="TH") {
						var theTR=theTabObj.parentElement;
					} else {
						var theTR=theTabObj;
					}
					iIndex=theTR.rowIndex;
					var theTmp=theTR.parentElement;
					while (theTmp&&theTmp.tagName&&theTmp.tagName.toUpperCase()!="TABLE") {
						theTmp=theTmp.parentElement;
					}
					theTable=theTmp;
				}
				theTable.deleteRow(iIndex);
			}
			break;
		//
		case "LINK":
			try {
			if (sSelType!="CONTROL" || sTag=="IMG") {
				if (sParam1==="-") {
					var bFirstParam=true; if (sParam2.indexOf("?")!=-1) bFirstParam=false;
					var sStartMark="?"; if (!bFirstParam) sStartMark="&";
					var sLText=""; if (sSelType=="TEXT") sLText=theRange.text;
					var arrPreParams=new Array();
					arrPreParams[0]=''; // text
					arrPreParams[1]=''; // href
					arrPreParams[2]=''; // target
					arrPreParams[3]=''; // onclick
					if (sAnchorTag=="A") {
						sLText=theAnchorObj.innerText;
						var sOnclick=theAnchorObj.onclick;
						var sPreHref=theAnchorObj.href;
						if (sPreHref.indexOf("#")!=-1) {
							sPreHref="";
							if (sOnclick.length) {
								var iLocStart1=sOnclick.indexOf(".open(");
								var iLocStart2=sOnclick.indexOf(".href=");
								if (iLocStart1!=-1) {
									sPreHref=sOnclick.substr(iLocStart1+".open(".length);
								} else if (iLocStart2!=-1) {
									sPreHref=sOnclick.substr(iLocStart2+".href=".length);
								}
								if (sPreHref.substr(0,1)=="\"" || sPreHref.substr(0,1)=="'") {
									sPreHref=sPreHref.substr(1);
								}
								sPreHref=sPreHref.replace(/\'|\"/ig, "'");
								var arrPreHref=sPreHref.split("'");
								sPreHref=arrPreHref[0];
							}
						}
						arrPreParams[1]=sPreHref;
						arrPreParams[2]=theAnchorObj.target;
						arrPreParams[3]=sOnclick;
					}
					arrPreParams[0]=sLText;
					var sUserInput=amg_ModalDialog(sParam2+sStartMark+"field="+sFldName+"&actor="+sButtName, 360, 580, arrPreParams, 0);
					//
					if (sUserInput) {
						var arrParams=sUserInput;//.split("+++");
						sParam1=arrParams[0];
						sParam2=arrParams[1];
						sParam3=arrParams[2];
						//
						if (sParam1.length) {
							var sLinkURL=sParam1;
							var sLinkText=sParam3;
							if (!sLinkText.length) sLinkText=sLinkURL;
							//if (sSelType=="TEXT") sLinkText=theRange.text;
							sLinkText=sLinkText.replace(/[\(\)\[\]]/gi, "/");
							var iParam2=parseInt(sParam2);
							var bJSLink=(iParam2 & 1);
							var bNewWin=(iParam2 & 2);
							//
							var sHref=""; var sTarget=""; var sOnClick=""; var sText="";
							//
							sText=sLinkText;
							if (bJSLink) {
								sHref="#"+sText.replace(/\"/ig, "&quot;");
								if (bNewWin) {
									sOnClick="window.open('"+sLinkURL+"'); return false;";
									sTarget="";
								} else {
									sOnClick="document.location.href='"+sLinkURL+"'; return false;";
									sTarget="";
								}
							} else {
								sHref=sLinkURL;
								if (bNewWin) {
									sOnClick="";
									sTarget="_blank";
								} else {
									sOnClick="";
									sTarget="";
								}
							}
							if (sAnchorTag=="A") {
								theAnchorObj.href=sHref
								theAnchorObj.target=sTarget
								theAnchorObj.onclick=sOnClick
								theAnchorObj.title=sText.replace(/\"/ig, "&quot;");
								if (sTag!="IMG") theAnchorObj.innerText=sText;
							} else {
								var sToPaste="<A HREF=\""+sHref+"\" TARGET=\""+sTarget+"\" ONCLICK=\""+sOnClick+"\" TITLE=\""+sText.replace(/\"/ig, "&quot;")+"\">"+sText+"</A>";
								//
								if (sTag=="IMG") {
									var theAnchor=theFraDoc.createElement("A");
									theAnchor.href=sHref;
									theAnchor.target=sTarget;
									theAnchor.onclick=sOnClick;
									theAnchor.title=sText.replace(/\"/ig, "&quot;");
									theAnchor.innerHTML=theObj.outerHTML;
									theFraDoc.body.replaceChild(theAnchor, theObj);
								} else {
									theRange.pasteHTML(sToPaste);
								}
							}
						}
					}
				} else {
					if (sParam1.length) {
						var sLinkURL=sParam1;
						var sLinkText=sLinkURL;
						if (sSelType=="TEXT") sLinkText=theRange.text;
						var iParam2=parseInt(sParam2);
						var bJSLink=(iParam2&1);
						var bNewWin=(iParam2&2);
						var sToPaste=sLinkURL;
						if (sLinkURL!=sLinkText) sToPaste+=(" "+sLinkText);
						var bExtLink=(sLinkURL.substr(0,7).toUpperCase()=="HTTP://"||sLinkURL.substr(0,4).toUpperCase()=="WWW.");
						if ((bNewWin&&(!bExtLink))||(bExtLink&&(!bNewWin))) sToPaste+=" 1";
						if (bJSLink) {
							sToPaste="["+sToPaste+"]";
						} else {
							sToPaste="("+sToPaste+")";
						}
						theRange.pasteHTML(sToPaste);
					}
				}
			}
			} catch (theError) {
				//alert("theError: "+theError.description);
			}
			break;
		case "MDLMACROS":
			var sToPaste='['+sParam2.toUpperCase()+']';
			sToPaste="<DIV CLASS=\"glyph_mcrs\" CONTENTEDITABLE=\"false\">"+sToPaste+"</DIV><BR>&nbsp;";
			theRange.pasteHTML(sToPaste);
			break;
		case "MDLSELITEM":
			if (sParam1=="-") {
				var arrParam2=sParam2.split("::");
				if (arrParam2.length>1) {
					var arrPreParams=new Array();
					arrPreParams[0]=arrParam2[1];
					var sUserInput=amg_ModalDialog("/webdirector/shell/setmainitem.php"+"?mdl="+arrParam2[0]+"&field="+sFldName+"&actor="+sButtName, 360, 520, arrPreParams, 0);
					if (sUserInput) {
						if (sSelType=="TEXT") theRange.collapse(true);
						var sToPaste="["+arrParam2[1].toUpperCase()+" "+sUserInput+"]";
						sToPaste="<DIV CLASS=\"glyph_mcrs\" STYLE=\"background-image:url(http://"+WD_HOST+"/webdirector/"+arrParam2[0]+"/images/mcrs_"+arrParam2[1]+".gif);\">"+sToPaste+"</DIV><BR>&nbsp;";
						theRange.pasteHTML(sToPaste);
					}
				}
			} else {
				void(0);
			}
			break;
		case "CHILDREN":
			if (sSelType!="CONTROL") {
				if (sParam1=="-") {
					var bFirstParam=true; if (sParam2.indexOf("?")!=-1) bFirstParam=false;
					var sStartMark="?"; if (!bFirstParam) sStartMark="&";
					var sUserInput=amg_ModalDialog(sParam2+sStartMark+"field="+sFldName+"&actor="+sButtName, 360, 560, "wd_cataloger", -96);
					//
					if (sUserInput) {
						var arrParams=sUserInput.split("+++");
						sParam1=arrParams[0];
						sParam2=arrParams[1];
						//
						if (sParam1.length) {
							var arrParam1=sParam1.split("%%%");
							var arrParam2=sParam2.split("/");
							var sColumnSID=arrParam1[0];
							var sTplSID=arrParam1[1];
							var sCurrSID=arrParam1[2];
							var iNeedReverse=parseInt(arrParam2[0]);
							var iNeedLimit=parseInt(arrParam2[1]);
							var iQuantLimit=parseInt(arrParam2[2]);
							if (!iQuantLimit) iQuantLimit=0;
							var sToPaste="[CATALOG";
							if (sColumnSID.length) {
								if (sColumnSID!=sCurrSID||sTplSID.length||(iNeedLimit&&iQuantLimit)||iNeedReverse) {
									sToPaste+=(" "+sColumnSID);
									if (sTplSID.length&&(sTplSID!=sCurrSID||(iNeedLimit&&iQuantLimit)||iNeedReverse)) {
										sToPaste+=(" "+sTplSID);
										if ((iNeedLimit&&iQuantLimit)||iNeedReverse) sToPaste+=(" "+iQuantLimit);
										if (iNeedReverse) sToPaste+=" reverse";
									}
								}
							}
							sToPaste+="]";
							if (sToPaste=="[CATALOG "+sCurrSID+"]") sToPaste="[CATALOG]";
							//
							if (sSelType=="TEXT") theRange.collapse(true);
							sToPaste="<DIV CLASS=\"glyph_ctlg\" CONTENTEDITABLE=\"false\">"+sToPaste+"</DIV><BR>&nbsp;";
							theRange.pasteHTML(sToPaste);
						}
					}
				} else {
					if (sParam1.length) {
						var arrParam1=sParam1.split("%%%");
						var arrParam2=sParam2.split("/");
						var sColumnSID=arrParam1[0];
						var sTplSID=arrParam1[1];
						var sCurrSID=arrParam1[2];
						var iNeedReverse=parseInt(arrParam2[0]);
						var iNeedLimit=parseInt(arrParam2[1]);
						var iQuantLimit=parseInt(arrParam2[2]);
						if (!iQuantLimit) iQuantLimit=0;
						var sToPaste="[CATALOG";
						if (sColumnSID.length) {
							if (sColumnSID!=sCurrSID||sTplSID.length||iNeedLimit||iNeedReverse) {
								sToPaste+=(" "+sColumnSID);
								if (sTplSID.length&&(sTplSID!=sCurrSID||iNeedLimit||iNeedReverse)) {
									sToPaste+=(" "+sTplSID);
									if (iNeedLimit||iNeedReverse) sToPaste+=(" "+iQuantLimit);
									if (iNeedReverse) sToPaste+=" reverse";
								}
							}
						}
						sToPaste+="]";
						//
						if (sSelType=="TEXT") theRange.collapse(true);
						sToPaste="<DIV CLASS=\"glyph_ctlg\" CONTENTEDITABLE=\"false\">"+sToPaste+"</DIV><BR>&nbsp;";
						theRange.pasteHTML(sToPaste);
					}
				}
			}
			break;
		case "FORMA":
			if (sSelType=="NONE"||sSelType=="TEXT") {
				var sCaption="Form Caption";
				if (sSelType=="TEXT") sCaption=theRange.text;
				var sText="[FORM\n";
				sText+="email=email@example.com\n";
				sText+="subject="+sCaption+"\n";
				sText+="caption="+sCaption+"\n";
				sText+="submit=Submit form\n";
				sText+="====\n";
				sText+="separator=Section 1\n";
				sText+="string=String Field\n";
				sText+="text=Text Field\n";
				sText+="separator=Section 2\n";
				sText+="select=Select One=a+b+c+d\n";
				sText+="]\n";
				sText="<DIV CLASS=\"glyph_form\" CONTENTEDITABLE=\"false\"><PRE>"+sText+"</PRE></DIV><BR>&nbsp;";
				theRange.pasteHTML(sText);
				
			}
			break;
		case "CODE":
			if (sSelType=="NONE"||sSelType=="TEXT") {
				theRange.pasteHTML("<BR>&nbsp;<DIV CLASS=\"glyph_code\" CONTENTEDITABLE=\"false\"><PRE>&lt;?\n"+theRange.htmlText+"\n?&gt;</PRE></DIV><BR>&nbsp;");
			}
			break;
		case "REDIRECT":
			if (sSelType!="CONTROL") {
				if (sParam1=="-") {
					var bFirstParam=true; if (sParam2.indexOf("?")!=-1) bFirstParam=false;
					var sStartMark="?"; if (!bFirstParam) sStartMark="&";
					var sUserInput=amg_ModalDialog(sParam2+sStartMark+"field="+sFldName+"&actor="+sButtName, 320, 530, "wd_redirector");
					//
					if (sUserInput) {
						sParam1=sUserInput;
						if (sParam1.length) {
							if (sSelType=="TEXT") theRange.collapse(true);
							var sToPaste="[REDIRECT "+sParam1+"]";
							sToPaste="<DIV CLASS=\"glyph_rdrt\" CONTENTEDITABLE=\"false\">"+sToPaste+"</DIV><BR>&nbsp;";
							theRange.pasteHTML(sToPaste);
						}
					}
				} else {
					if (sParam1.length) {
						if (sSelType=="TEXT") theRange.collapse(true);
						var sToPaste="[REDIRECT "+sParam1+"]";
						sToPaste="<DIV CLASS=\"glyph_rdrt\" CONTENTEDITABLE=\"false\">"+sToPaste+"</DIV><BR>&nbsp;";
						theRange.pasteHTML(sToPaste);
					}
				}
			}
			break;
		case "IMG":
			if (sSelType!="CONTROL" || sTag=="IMG") {
				if (sParam1=="-") {
					var bFirstParam=true; if (sParam2.indexOf("?")!=-1) bFirstParam=false;
					var sStartMark="?"; if (!bFirstParam) sStartMark="&";
					amg_ModalWin(sParam2+sStartMark+"field="+sFldName+"&actor="+sButtName+"&bmode="+0, 480, 600, "wd_imgsettor", 0, 1);
				} else {
					if (sParam1.length) {
						var arrParam1=sParam1.split("%%%");
						var arrParam2=sParam2.split("/");
						var sImgName=arrParam1[0];
						var sImgAlt=arrParam1[1];
						var sImgAlign=arrParam2[0];
						var sImgHSpace=arrParam2[1];
						var sImgVSpace=arrParam2[2];
						if (sSelType=="TEXT") theRange.collapse(true);
						if (sImgName.length) {
							var sToPaste="";
							//if (sImgAlt.length) sImgAlt=sImgAlt.replace(/,|\(|\)\[\]/ig, "_");
							switch (sImgAlign) {
								case "1":
									var sAlignment=" ALIGN=\"left\"";
									break;
								case "2":
									var sAlignment=" ALIGN=\"right\"";
									break;
								default:
									var sAlignment="";
									break;
							}
							var sHSpacer="";
							if (sImgHSpace!="-1") sHSpacer=" HSPACE=\""+sImgHSpace+"\"";
							var sVSpacer="";
							if (sImgVSpace!="-1") sVSpacer=" VSPACE=\""+sImgVSpace+"\"";
							//
							sToPaste="<IMG SRC=\""+sImgName+"\" ALT=\""+sImgAlt+"\" BORDER=\"0\""+sAlignment+""+sHSpacer+""+sVSpacer+">";
							//
							if (sTag=="IMG") {
								theObj.src=sImgName;
								theObj.alt=sImgAlt;
								switch (sImgAlign) {
									case "1":
										theObj.align="left";
										break;
									case "2":
										theObj.align="right";
										break;
									default:
										theObj.align="";
										break;
								}
								if (sImgHSpace!="-1") {
									theObj.hspace=sImgHSpace;
								} else {
									theObj.hspace=0;
								}
								if (sImgVSpace!="-1") {
									theObj.vspace=sImgVSpace;
								} else {
									theObj.vspace=0;
								}
							} else {
								theRange.pasteHTML(sToPaste);
							}
						}
					}
				}
			}
			break;
		case "BGIMG":
			if (true || sSelType=="NONE" || sTag=="TABLE") {
				if (sParam1=="-") {
					var bFirstParam=true; if (sParam2.indexOf("?")!=-1) bFirstParam=false;
					var sStartMark="?"; if (!bFirstParam) sStartMark="&";
					amg_ModalWin(sParam2+sStartMark+"field="+sFldName+"&actor="+sButtName+"&bmode="+0, 480, 600, "wd_imgsettor", 0, 1);
				} else {
					if (sParam1.length) {
						var arrParam1=sParam1.split("%%%");
						var arrParam2=sParam2.split("/");
						var sImgName=arrParam1[0];
						var sImgAlt=arrParam1[1];
						var sBgPositionX=arrParam2[0];
						switch (sBgPositionX) {
							case '2':
								sBgPositionX="right";
								break;
							case '1':
								sBgPositionX="center";
								break;
							case '0':
							default:
								sBgPositionX="left";
								break;
						}
						var sBgPositionY=arrParam2[1];
						switch (sBgPositionY) {
							case '2':
								sBgPositionY="bottom";
								break;
							case '1':
								sBgPositionY="center";
								break;
							case '0':
							default:
								sBgPositionY="top";
								break;
						}
						var sBgRepeat=arrParam2[2];
						switch (sBgRepeat) {
							case '3':
								sBgRepeat="repeat-y";
								break;
							case '2':
								sBgRepeat="repeat-x";
								break;
							case '1':
								sBgRepeat="no-repeat";
								break;
							case '0':
							default:
								sBgRepeat="repeat";
								break;
						}
						if (sSelType=="TEXT") theRange.collapse(true);
						if (sImgName.length) {
							theObj.style.backgroundImage="url("+sImgName+")";
							theObj.style.backgroundRepeat=sBgRepeat;
							theObj.style.backgroundPositionY=sBgPositionY;
							theObj.style.backgroundPositionX=sBgPositionX;
						} else {
							theObj.style.backgroundImage="none";
						}
					}
				}
			}
			break;
		case "DOC":
			if (sSelType!="CONTROL") {
				if (sParam1=="-") {
					var sText=""; if (sSelType=="TEXT") sText=theRange.text;
					sText=sText.replace(/,|\(|\)\[\]/ig, "_");
					sText=sText.replace(/\r\n/ig, "");
					sText=sText.replace(/\"/ig, "&quot;");
					var arrText=sText.split("<");
					sText=arrText[0];
					//sText=escape(sText);
					//sText="";
					var bFirstParam=true; if (sParam2.indexOf("?")!=-1) bFirstParam=false;
					var sStartMark="?"; if (!bFirstParam) sStartMark="&";
					amg_ModalWin(sParam2+sStartMark+"field="+sFldName+"&actor="+sButtName+"&lnktext="+sText, 480, 600, "wd_docsettor", 0, 1);
				} else {
					if (sParam1.length) {
						var arrParam1=sParam1.split("%%%");
						var sImgName=arrParam1[0];
						var sImgAlt=arrParam1[1];
						if (sSelType=="TEXT") theRange.collapse(true);
						if (sImgName.length) {
							//sImgAlt=sImgAlt.replace(/,|\(|\)\[\]/ig, "_");
							sImgAlt=sImgAlt.replace(/\r\n/ig, "");
							sImgAlt=sImgAlt.replace(/\"/ig, "&quot;");
							sToPaste="<A HREF=\""+sImgName+"\" ALT=\""+sImgAlt+"\">"+sImgAlt+"</A>";
							//
							theRange.pasteHTML(sToPaste);
						}
					}
				}
			}
			break;
		case "COLOR":
			if (sSelType!="CONTROL") {
				if (sParam1=="-") {
					wd_ShowDialog('wd_dlg_Xpalette', sFldName, sButtName, 1, 1);
				} else {
					var sColor=sParam1;
					if (typeof(arrPaletteCodes[sColor])!=='undefined') sColor=arrPaletteCodes[sColor];
					theRange.pasteHTML(sColor);
				}
			}
			break;
		//
		default:
			break;
	}
//alert('1');
	//
	var theStartSpan=theFraDoc.getElementById("wd_576879_Start");
	if (theStartSpan) {
		if (!theStartSpan.innerHTML.length) {
			theStartSpan.removeNode(true);
		} else {
			theStartSpan.removeAttribute('id', 0);
		}
	}
	var theFinSpan=theFraDoc.getElementById("wd_576879_Fin");
	if (theFinSpan) {
		if (!theFinSpan.innerHTML.length) {
			theFinSpan.removeNode(true);
		} else {
			theFinSpan.removeAttribute('id', 0);
		}
	}
	//
	//wd_ValidateSpanAdjacents(theObj);
	//
//alert('2');
	if (sCommand!=='ONPASTE' && sCommand!=='ONKEYCODE' && sDupBookmark) {
		//var theDupRange=theFraBody.createTextRange();
		//theDupRange.moveToBookmark(sDupBookmark);
		//theDupRange.select();
	}
//alert('3');
	// == <P>&nbsp;</P> :
	if (theFrame && theFrame.document && theFrame.document.body && !theFrame.document.body.innerText.length) {
		theFrame.document.body.innerHTML="";
	}
//alert('4');
	if (sCommand==="ONKEYCODE" && (sParam1===8 || sParam1===46) && theFrame && theFrame.document && theFrame.document.body && theFrame.document.body.innerText.length===1) {
		theFrame.document.body.innerHTML="";
	}
//alert('5');
	// ==
	if (typeof(wd_wyschanged_mark)!="undefined" && !wd_wyschanged_mark) {
		wd_wyschanged_mark=bReturnAnswer;
	}
//alert('6');
	return bReturnAnswer;
}
//==============================================================================
clean_a=true;
clean_img=true;
clean_colors=false;
clean_aligns=false;

function cleanTree(theDomObj, bNeedClean) {
	var theChildren=theDomObj.children;
	var i=0;
	if (theChildren) {
		for(i=theChildren.length-1; i>=0; i--) cleanTree(theChildren[i], true);
	}
	if(bNeedClean) cleanObject(theDomObj);
}
function cleanObject(o) {
	var s="";
	var myAttributes=[];
	var i;
	
	if (o.outerHTML.substr(0,2)=="<?") {
		o.removeNode(false);
		//o.parentNode.replaceChild(o.firstChild, o)
		return;
	}
	var tag=o.tagName.toLowerCase();

	// удаляем плохие, заказные и пустые теги //
	if(!goodTags[tag] || (clean_a && tag=="a") || (clean_img && tag=="img") || (o.canHaveHTML && o.innerHTML=="" && !canEmptyTags[tag])){
		try{
			o.removeNode(false);
		//	o.parentNode.replaceChild(o.firstChild, o)
		}catch(e){}

		return;
	}

	var a=o.attributes;
	if(!a) return;

	for(i in a){ // хм...
		if(""+a[i]!="null"){ // странно, но просто if(a[i]) не прокатывает //
			myAttributes[i]=a[i];
		}
	}
	// киляем плохие атрибуты и плохие классы //
	for(i in myAttributes){
		//i=i.toLowerCase()
		if( 
			(
				!goodAttributes[""][i] &&
				(
					!goodAttributes[tag] || !goodAttributes[tag][i] || i=="class" || i=="className"
				)
			)
			||
			(
				clean_colors &&
				(
					i=="bgColor" || i=="color"
				)
			)
			||
			(
				clean_aligns &&
				(
					i=="align" || i=="vAlign"
				)
			)
		){
			
			if(i=="class"){
				i="className";
			}
			if(i!="className" || !goodClasses[o.className]){
				o.removeAttribute(i);
			}
		}	
	}
	// в 6-ом MSIE не киляем непонятных атрибутов у таблиц. см. выше. //
	if(o.tagName.toLowerCase()!="table"){
		for(i in mustDieAttributes){
			o.removeAttribute(mustDieAttributes[i]);
		}
	}
	o.style.cssText="";
	try {
		var sOTag=o.tagName.toUpperCase();
		switch (sOTag) {
			case "TABLE":
				o.width="100%";
				o.cellSpacing=(typeof(iWDTblCellspacing)=="undefined"?"0":iWDTblCellspacing);
				o.border=(typeof(iWDTblOpts3D)=="undefined"?"0":iWDTblOpts3D);
				o.className="WDTBL";
				break;
			case "TH":
				o.className="WDTH";
				break;
			case "TD":
				o.className="WDTD";
				break;
			default:
				break;
		}
	} catch (theError) {
		//alert("err1:\n"+sOTag+"\n"+theError.description);
	}
	try {
//
		if ((sOTag=="A" || sOTag=="ACRONYM" || sOTag=="ADDRESS" || sOTag=="B" || sOTag=="BDO" || sOTag=="BIG" || sOTag=="BLOCKQUOTE" || sOTag=="BODY" || sOTag=="BUTTON" || sOTag=="CAPTION" || sOTag=="CENTER" || sOTag=="CITE" || sOTag=="CODE" || sOTag=="COL" || sOTag=="COLGROUP" || sOTag=="CUSTOM" || sOTag=="DD" || sOTag=="DEL" || sOTag=="DFN" || sOTag=="DIR" || sOTag=="DIV" || sOTag=="DL" || sOTag=="DT" || sOTag=="EM" || sOTag=="FIELDSET" || sOTag=="FONT" || sOTag=="FORM" || sOTag=="FRAMESET" || sOTag=="HN" || sOTag=="HTML" || sOTag=="I" || sOTag=="INS" || sOTag=="KBD" || sOTag=="LABEL" || sOTag=="LEGEND" || sOTag=="LI" || sOTag=="LISTING" || sOTag=="MAP" || sOTag=="MARQUEE" || sOTag=="MENU" || sOTag=="NOBR" || sOTag=="OL" || sOTag=="OPTION" || sOTag=="P" || sOTag=="PRE" || sOTag=="Q" || sOTag=="RT" || sOTag=="RUBY" || sOTag=="S" || sOTag=="SAMP" || sOTag=="SCRIPT" || sOTag=="SELECT" || sOTag=="SMALL" || sOTag=="SPAN" || sOTag=="STRIKE" || sOTag=="STRONG" || sOTag=="STYLE" || sOTag=="SUB" || sOTag=="SUP" || sOTag=="TABLE" || sOTag=="TBODY" || sOTag=="TD" || sOTag=="TFOOT" || sOTag=="TH" || sOTag=="THEAD" || sOTag=="TITLE" || sOTag=="TR" || sOTag=="TT" || sOTag=="U" || sOTag=="UL" || sOTag=="VAR")&&(sOTag!="COL" && sOTag!="COLGROUP" && sOTag!="FRAMESET" && sOTag!="HTML" && sOTag!="STYLE" && sOTag!="TABLE" && sOTag!="TBODY" && sOTag!="TFOOT" && sOTag!="THEAD" && sOTag!="TITLE" && sOTag!="TR")) {
			var sHtml=o.innerHTML;
			sHtml=sHtml.replace(/<P\/?>/ig, "<BR><BR>");
			sHtml=sHtml.replace(/<\/P>/ig, "");
			o.innerHTML=sHtml;
		}
	} catch (theError) {
		//alert("err2:\n"+sOTag+"\n"+theError.description);
	}
}

// хорошие теги
goodTags0=["a","p","br","strong","b","em","i","tt","code","pre","ul","ol","li","img","table","tbody","thead","tfoot","caption","tr","td","th","col","colgroup","h1","h2","h3","h4","h5","h6","small","big","sub","sup","div"]

// теги, которые canHaveHTML() и могут быть пустыми. //
canEmptyTags0=["td","th"]

goodAttributes0=[]
// хорошие атрибуты (допустимы у всех тегов) //
goodAttributes0[""] = ["href", "target", "name", "title", "alt", "src", "id", "bgColor", "color"]

// дополнительные допустимые атрибуты для отдельных тегов //
goodAttributes0["img"] = ["width", "height", "border", "align"]
goodAttributes0["table"] = ["cellSpacing", "cellPadding", "border", "width %"] // width % - на будущее - сделаю, чтоб только процентные меры принимать, остальные посылать... //
goodAttributes0["td"] = ["colSpan", "rowSpan"]
goodAttributes0["tr td col colgroup"] = ["noWrap", "align", "vAlign", "width %"]
goodAttributes0["a area"] = ["href", "name"]
goodAttributes0["br"] = ["clear"]

// однозначно убиваемые псевдо-атрибуты (отсутствуют в коллекции attributes)
mustDieAttributes=["x:str","x:num","x:fmla"] // внимание - в 6-ом MSIE убийство несуществующих атрибутов у тега TABLE ведет к краху браузера //

// хорошие классы (не убиваем)
goodClasses0=["important","noindent","note","h1","h2","h3","h4","h5","h6"]

goodTags=[]
for(i in goodTags0){
	goodTags[goodTags0[i]]=true
}
canEmptyTags=[]
for(i in canEmptyTags0){
	canEmptyTags[canEmptyTags0[i]]=true
}

goodAttributes=[]
for(i in goodAttributes0){
	var splitted=i.split(" ")
	for(ii in splitted){
		if(!goodAttributes[splitted[ii]]) goodAttributes[splitted[ii]]=[]		
		for(j in goodAttributes0[i]){
			goodAttributes[splitted[ii]][goodAttributes0[i][j]]=true
		}
	}
}

goodClasses=[]
for(i in goodClasses0){
	goodClasses[goodClasses0[i]]=true
}