// -------------------------------------------------------------------------------
// ---  найти родительский объект с заданным tagName
	function rGetParent(elmChild,tagName){
		var elmParent = elmChild;
		while(elmParent.tagName!=tagName && elmParent.tagName!="BODY"){
			elmParent = elmParent.parentNode;
		}
		return (elmParent.tagName==tagName)?elmParent:null;
	}
// -------------------------------------------------------------------------------
// ---  вызвать ColorPicker
	function rGetColorer(color){
		return window.showModalDialog(orPath.base+"richedit.color.html",color,"dialogWidth:350px; dialogHeight:370px; resizable: no; help: no; status: no; scroll: no;");
	}
// -------------------------------------------------------------------------------
// ---  очистка стилей objecta
	function rClearObjectFormat(oObject,oDocument){
		var newTag,orTags;
		if(orTags=oObject.childNodes){
			for(var i=orTags.length-1;i>=0;i--){ 
				rClearObjectFormat(orTags[i],oDocument);
			}
		}
		if(oObject.tagName){
			if(	oObject.tagName == "I" ||
					oObject.tagName == "B" ||
					oObject.tagName == "U" ||
					oObject.tagName == "A" ||
					oObject.tagName == "P" ||
					oObject.tagName == "OL" ||
					oObject.tagName == "UL" ||
					oObject.tagName == "LI" ||
					oObject.tagName == "TR" ||
					oObject.tagName == "TD" ||
					oObject.tagName == "TH" ||
					oObject.tagName == "HR" ||
					oObject.tagName == "BR" ||
					oObject.tagName == "SUP" ||
					oObject.tagName == "SUB" ||
					oObject.tagName=="IMG"	||
					oObject.tagName=="MAP"	||
					oObject.tagName=="AREA"	||
					oObject.tagName == "TBODY" ||
					oObject.tagName=="TABLE"
			){
				oObject.removeAttribute("className");
				oObject.removeAttribute("style");
				oObject.removeAttribute("type");
				oObject.removeAttribute("dir");
				oObject.removeAttribute("lang");
				if(oObject.tagName!="IMG"){
					oObject.removeAttribute("width");
					oObject.removeAttribute("height");
				}
				if(oObject.tagName=="TABLE"){ // для всех тегов TABLE
					oObject.setAttribute("cellSpacing","0");
					oObject.setAttribute("cellPadding","0");
					oObject.setAttribute("border","0");
					oObject.setAttribute("className","reTable");
				}else if(oObject.tagName=="IMG"){ // для всех тегов IMG
					oObject.removeAttribute("vSpace");
					oObject.removeAttribute("hSpace");
					oObject.removeAttribute("align");
					oObject.setAttribute("border","0");
				}
			}else if(oObject.tagName == "BODY"){
			}else if(oObject.tagName == "SCRIPT"){
				 oObject.parentNode.removeChild(oObject);
			}else if(oObject.tagName == "STRONG"){
				var fChar = oObject.innerText.substr(0,1);
				newTag = oDocument.createElement("B");
				newTag.innerHTML = oObject.innerHTML;
				oObject.parentNode.replaceChild(newTag,oObject);
				if(fChar==" ") newTag.parentNode.insertBefore(oDocument.createTextNode(" "),newTag);
			}else if(oObject.tagName == "EM"){
				var fChar = oObject.innerText.substr(0,1);
				newTag = oDocument.createElement("I");
				newTag.innerHTML = oObject.innerHTML;
				oObject.parentNode.replaceChild(newTag,oObject);
				if(fChar==" ") newTag.parentNode.insertBefore(oDocument.createTextNode(" "),newTag);
			}else if(oObject.canHaveHTML){
				var fChar = oObject.innerText.substr(0,1);
				newTag = oDocument.createElement("");
				newTag.innerHTML = oObject.innerHTML;
				oObject.parentNode.replaceChild(newTag,oObject);
				if(fChar==" ") newTag.parentNode.insertBefore(oDocument.createTextNode(" "),newTag);
			}
			if(oObject.canHaveHTML && oObject.tagName!="TABLE" && oObject.tagName!="TR" && oObject.tagName!="TBODY"){
				oObject.innerHTML = oObject.innerHTML.replace(/(&nbsp;)+/g," ");
			}
		}
	}
// -------------------------------------------------------------------------------
// ---  открыть окно с изображением
	var wrImageWindow;
	function rOpenImageWindow(src,width,height){ 
		wWidth = (width)?width+4:470;
		wHeight = (height)?height+4:470;
		wrImageWindow=window.open("","blankImageWindow", 'status=no,scrollbars=no,resizable=yes,width='+(wWidth)+',height='+(wHeight)+'');
			wrImageWindow.document.write("<html><head><title>Изображение</title>");			
			wrImageWindow.document.write("<style>.visible{display:block;} .hide{display:none;} div,a{font:10px verdana,tahoma,sans-serif;text-decoration:none;}</style>");


			wrImageWindow.document.write("<script>function printshow(over){document.getElementById('menu').setAttribute(classFix,(over)?'visible':'hide');}</script>");


			wrImageWindow.document.write("<body class=popupWnd topmargin=0 marginheight=0 leftmargin=0 marginwidth=0 bgcolor=white text=black link=black alink=black vlink=black>");

			wrImageWindow.document.write("<script>var classFix = (document.body && document.body.getAttribute('className'))?'className':'class';</script>");

			wrImageWindow.document.write("<table border=0 cellspacing=0 cellpadding=0 width=100% height=100%><tr><td align=center onmouseover='printshow(true);' onmouseout='printshow(false);'>");
			wrImageWindow.document.write("<table border=0 cellspacing=1 cellpadding=1 bgcolor=#333333><tr><td bgcolor=white>");
			wrImageWindow.document.write("<div id='menu' class='hide' style='position:absolute;z-index:1;background:white;top:2px;left:2px;padding:5px;border:#333333 1px solid;'>");
				wrImageWindow.document.write("[<a href='javascript:;' onclick='printshow(false);self.print();' title='распечатать'>распечатать</a>] &nbsp;");
				wrImageWindow.document.write("[<a href='javascript:;' onclick='self.close();' title='закрыть окно'>закрыть</a>]");
			wrImageWindow.document.write("</div>");
			wrImageWindow.document.write("<img src='"+src+"' width='"+width+"' height='"+height+"' border=0>");
			wrImageWindow.document.write("</td></tr></table>");
			wrImageWindow.document.write("</td></tr></table>");
			wrImageWindow.document.write("</body></html>");
			wrImageWindow.document.close();
		wrImageWindow.resizeTo(wWidth+30,wHeight+40);
		wrImageWindow.focus();
	}
// -------------------------------------------------------------------------------
// ---   преобразовать RGB то Hex
	function color(r,g,b){
		return toHex((r<<16)+(g<<8)+(b<<0));
	}
// ---  преобразовать число (0 - 16) к шеснадцатеричному виду 
	function enHex(aDigit){ 
		return("0123456789ABCDEF".substring(aDigit, aDigit+1)); 
	}
// --- преобразовать число к шеснадцатеричному виду
	function toHex(n){
		return (enHex((0xf00000 & n) >> 20) +
						enHex((0x0f0000 & n) >> 16) +
						enHex((0x00f000 & n) >> 12) +
						enHex((0x000f00 & n) >>  8) +
						enHex((0x0000f0 & n) >>  4) +
						enHex((0x00000f & n) >>  0));
	}
// ---
	function colorToRGB(color){
		return ((color & 0x0000ff) << 16)+
		  			(color & 0x00ff00)+
					 ((color & 0xff0000) >> 16);
	} 
// -------------------------------------------------------------------------------
// --- класс позиция
	function rPosition(){
		this.row = -1;
		this.col = -1;
		this.copy = rPositionCopy;
	}
// ---
	function rPositionCopy(mPos){
		this.row = mPos.row;
		this.col = mPos.col;
	}
// -------------------------------------------------------------------------------
// --- класс преобразования таблицы в матрицу
	function rMatrix(elmTABLE){
		this.matrix = new Array();
		this.table	= elmTABLE;
	// --- иницализация масива
		var yCell=0;
		var cellIndex=0;
		for(var i=0; i<this.table.rows.length; i++) this.matrix[i]=new Array();
		for(var i=0; i<this.table.rows.length; i++){
			var xCell=0;
			for(var j=0; j<this.table.rows[i].cells.length; j++){
				while(this.matrix[yCell][xCell]>=0) xCell++;
				for(var k=0;k<this.table.rows[i].cells[j].colSpan;k++){
					for(var l=0;l<this.table.rows[i].cells[j].rowSpan;l++){
						this.matrix[yCell+l][xCell]=cellIndex;
					} xCell++;
				} cellIndex++;
			} yCell++;
		}
	// --- 
		this.mrows	= this.matrix.length;
		this.mcols	= this.matrix[0].length;
	// --- 
		this.index = rMatrixGetCellIndex;
		this.position = rMatrixGetPosition;
		this.nearest	= rMatrixGetNearest;
	}
// -------------------------------------------------------------------------------
// --- найти ближаший елемент в матрице
	function rMatrixGetNearest(mPos,direction){	
		var mNearPos = new rPosition(); mNearPos.copy(mPos);
			 	 if(direction == 'left')	while(mNearPos.col>=0					&& this.index(mNearPos)==this.index(mPos)) mNearPos.col--;
		else if(direction == 'right')	while(mNearPos.col<this.mcols && this.index(mNearPos)==this.index(mPos)) mNearPos.col++;
		else if(direction == 'top')		while(mNearPos.row>=0					&& this.index(mNearPos)==this.index(mPos)) mNearPos.row--;
		else													while(mNearPos.row<this.mrows	&& this.index(mNearPos)==this.index(mPos)) mNearPos.row++;
		var nearIndex = this.index(mNearPos);
		if(nearIndex!=-1 && nearIndex!=this.index(mPos)){
			mNearPos = this.position(this.table.cells[nearIndex]);
			//alert(this.index(mPos)+" > "+mPos.row+","+mPos.col+"\n"+this.index(mNearPos)+" > "+mNearPos.row+","+ mNearPos.col);
			return mNearPos;
		}else{
			return false;
		}
	}
// -------------------------------------------------------------------------------
// --- значение елемента в матрице по его позиции
	function rMatrixGetCellIndex(mPos){ 
		return (mPos.row<this.mrows && mPos.col<this.mcols)?this.matrix[mPos.row][mPos.col]:-1;	
	}
// -------------------------------------------------------------------------------
// --- позиция елемента в матрице
	function rMatrixGetPosition(elmTD){			
		var mPos = new rPosition();
		for(var i=0;i<this.matrix.length;i++)
			for(var j=0;j<this.matrix[i].length;j++){
				if(this.table.cells[this.matrix[i][j]]==elmTD){
					mPos.row =i;
					mPos.col =j;
			    return mPos;
				}
			}
    return mPos;
	}