function UnsetTable(){
if(tagTABLE)editor.blur();
tagTD=null;
tagTR=null;
tagTABLE=null;
trIndex=0;
allWidth=0;
allCol=0;
fullTR=0;
HideArrows();
window.status="";
}
function MapTable(){
if (iscode||!tagTABLE||!tagTD||!tagTR)return;
UnMapTable();
var el=tagTR.parentNode;
for (i=0;i<el.childNodes.length;i++){
			var realIndex=0;
			for(a=0;a<el.childNodes[i].childNodes.length;a++){
				var td=el.childNodes[i].childNodes[a];
				if(el.childNodes[i].childNodes.length+GetColRowSpan(el.childNodes[i],"col")==allCol){	//Если полный ряд
					td.id=realIndex;
					if(td.colSpan>1){
						realIndex+=td.colSpan-1;
					}
					realIndex++;
					continue;
				}else{
				var arraySpans=new Array();
				for(h=0;h<i;h++){
						for(s=0;s<el.childNodes[h].childNodes.length;s++){
							if(el.childNodes[h].childNodes[s].rowSpan+h>i){
							arraySpans[el.childNodes[h].childNodes[s].id]=el.childNodes[h].childNodes[s].id;
						}
					}
				}
				for(z=0;z<arraySpans.length;z++){
					if(realIndex==arraySpans[z])realIndex++;
				}
				if(td.colSpan>1){
					td.id=realIndex;
					realIndex+=td.colSpan;
				}else{
					td.id=realIndex;
					realIndex++;
				}
			}
			}
		}
}
function UnMapTable(){
if (iscode||!tagTABLE)return;
var el=null;
(tagTABLE.firstChild.tagName=="TBODY")?el=tagTABLE.firstChild:el=tagABLE;
for (i=0;i<el.childNodes.length;i++){
	for(a=0;a<el.childNodes[i].childNodes.length;a++){//Не забыть сменить!!!!
		el.childNodes[i].childNodes[a].removeAttribute("id");
	}
}
}

function Border(){
if (iscode||!tagTABLE)return;
if (tagTABLE.border==0 || !tagTABLE.border)tagTABLE.border=1;
else tagTABLE.border=0;
HideArrows();
}
function GetColRowSpan(el,what,stop){
if (iscode||!el)return;
var i=0;
var result=0;
if(what=="col")for (i=0;i<el.childNodes.length;i++)if (el.childNodes[i].colSpan>1)result+=el.childNodes[i].colSpan-1;
if(what=="row"){
	for (i=0;i<el.childNodes.length;i++){
	if (i==stop)break;
	if (el.childNodes[i].rowSpan>1)result+=el.childNodes[i].rowSpan;
	}
}
return result;
}
function ChangeRows(action){
if (iscode||!tagTD||!tagTR||!tagTABLE)return;
if (action=='del') {
alert("Эта опция в демоверсии отключена")
UnMapTable();
UnsetTable();
RemoveStatusBar();
return;
};
MapTable();
var el=tagTR.parentNode;
var rowSpans=0;
for (i=0;i<el.childNodes.length;i++){
	if(trIndex==i)break;
	for(a=0;a<el.childNodes[i].childNodes.length;a++){
		if(el.childNodes[i].childNodes[a].rowSpan>1&&el.childNodes[i].childNodes[a].rowSpan+i>trIndex){
			(action=='add')?el.childNodes[i].childNodes[a].rowSpan+=1:el.childNodes[i].childNodes[a].rowSpan-=1;
			rowSpans++;
		}
	}
}
if(action=='add'){
	var newTR=document.createElement('TR');
	for (i = 0;i<allCol-rowSpans;i++){
		var newTD=document.createElement('TD');
		newTD.setAttribute('vAlign','top');
		newTR.appendChild(newTD);
	}
	el.insertBefore(newTR,tagTR);
}
UnMapTable();
UnsetTable();
RemoveStatusBar();
}
function ChangeCols(action){
if (iscode||!tagTD||!tagTR||!tagTABLE)return;
MapTable();
var el=tagTR.parentNode;
for (i=0;i<el.childNodes.length;i++){
	var newTD=document.createElement('TD');
	newTD.setAttribute('vAlign','top');
	var index=0;
	var before=false;
	for (a=el.childNodes[i].childNodes.length-1;a>=0;a--){
		if(parseInt(el.childNodes[i].childNodes[a].id)==parseInt(tagTD.id)){
			index=a;
			break;
		}
		if(parseInt(el.childNodes[i].childNodes[a].id)<parseInt(tagTD.id)){
			index=a;
			before=true;
			break;
		}
	}
	if (action=="add"){
		if(!before){
			el.childNodes[i].insertBefore(newTD,el.childNodes[i].childNodes[index]);
		}else{
			if(el.childNodes[i].childNodes[index].colSpan>1){
				el.childNodes[i].childNodes[index].colSpan++;
			}else{
			el.childNodes[i].insertBefore(newTD,el.childNodes[i].childNodes[index]);
			el.childNodes[i].childNodes[index].swapNode(el.childNodes[i].childNodes[index].nextSibling);
			}
		}
	}else{
		if (el.childNodes.length==1&&el.firstChild.childNodes.length==1){
			UnsetTable();
			UnMapTable();
			return;
		}
		if(!before){
			if(el.childNodes[i].childNodes[index].colSpan==1){
				var a=el.childNodes[i].childNodes[index].rowSpan;
				el.childNodes[i].removeChild(el.childNodes[i].childNodes[index]);
			i+=a-1;
			}else{
				el.childNodes[i].childNodes[index].colSpan--;
			}

		}else{
			el.childNodes[i].childNodes[index].colSpan--;
		}
	}
}
UnMapTable();
UnsetTable();
RemoveStatusBar();
}

function ChangeColSpan(action){
if (iscode||!tagTD||!tagTR)return;
if(action=="add"){
alert("В демоверсии эта опция отключена");
UnMapTable();
UnsetTable();
RemoveStatusBar();
}
MapTable();
var el=tagTR.parentNode;
if(action=="del"){
	if (tagTD.colSpan>1){
		var newTD=document.createElement('TD');
		newTD.setAttribute('vAlign','top');
		tagTD.colSpan-=1;
		(tagTD.nextSibling)?tagTR.insertBefore(newTD,tagTD.nextSibling):tagTR.appendChild(newTD);
	}
}
UnMapTable();
UnsetTable();
RemoveStatusBar();
}
function ChangeRowSpan(action){
if (iscode||!tagTD||!tagTR||!tagTABLE)return;
MapTable();
var el=tagTR.parentNode;
if(action=="add"){
	trIndex+=tagTD.rowSpan;
	if(el.childNodes.length<2||tagTD.colSpan>1||el.childNodes.length<=trIndex){
		UnsetTable();
		UnMapTable();
		return;
	}
	for(a=0;a<el.childNodes[trIndex].childNodes.length;a++){
		if(el.childNodes[trIndex].childNodes[a].colSpan>1){
		UnsetTable();
		UnMapTable();
		return;
		}
		if(parseInt(tagTD.id)==parseInt(el.childNodes[trIndex].childNodes[a].id)){
			el.childNodes[trIndex].childNodes[a].rowSpan>1?tagTD.rowSpan+=el.childNodes[trIndex].childNodes[a].rowSpan:tagTD.rowSpan+=1;
			el.childNodes[trIndex].removeChild(el.childNodes[trIndex].childNodes[a]);
		}
	}
	if(el.childNodes[trIndex].childNodes.length==0)el.removeChild(el.childNodes[trIndex]);
	if(el.childNodes.length==1)for(i=0;i<el.firstChild.childNodes.length;i++)el.firstChild.childNodes[i].rowSpan=1;
}
if(action=="del"){
	if(tagTD.rowSpan>1){
		var newTD=document.createElement('TD');
		newTD.setAttribute('vAlign','top');
		trIndex+=tagTD.rowSpan-1;
		tagTD.rowSpan-=1;
		var index=0;
		for(a=el.childNodes[trIndex].childNodes.length-1;a>=0;a--){
			if(parseInt(tagTD.id)>parseInt(el.childNodes[trIndex].childNodes[a].id)){
				index=a;
				break;
			}
		}
		(el.childNodes[trIndex].childNodes[index].nextSibling)?el.childNodes[trIndex].insertBefore(newTD,el.childNodes[trIndex].childNodes[index].nextSibling):el.childNodes[trIndex].appendChild(newTD);
	}
}
UnMapTable();
UnsetTable();
RemoveStatusBar();
}
function SetTable(){
if(!selectedObj)return;
UnsetTable();
var horiz=false;
var vertic=true;
var tmp=selectedObj;
while(tmp.id!="editor"){
	if(tmp.tagName=="TD"){
		tagTD=tmp;
		}
	if(tmp.tagName=="TR"){
		tagTR=tmp;
	}
	if(tmp.tagName=="TABLE"&&tagTR&&tagTD){
		tagTABLE=tmp;
		tmp=tagTR.parentNode;

		a=GetColRowSpan(tmp.firstChild,"col");
		(a>0)?allCol=tmp.firstChild.childNodes.length+a:allCol=tmp.firstChild.childNodes.length;
		var tmp_Width=0;
		for (i=0;i<tmp.childNodes.length;i++){
			if (tmp.childNodes[i]==tagTR)trIndex=i;
			if (tmp.childNodes[i].childNodes.length>tmp_Width){
			tmp_Width=tmp.childNodes[i].childNodes.length;
			fullTR=i;
			}
			for(a=0;a<tmp.childNodes[i].childNodes.length;a++){
				if(tmp.childNodes[i].childNodes[a].width){
					horiz=false;
					vertic=false;
					return;
				}
			}
		}
		var widthTable=0;
		for (i=0;i<tmp.childNodes.length;i++){
			for(a=0;a<tmp.childNodes[i].childNodes.length;a++){
				if (i==fullTR){
				if(!tmp.childNodes[i].childNodes[a].style.width) tmp.childNodes[i].childNodes[a].style.width=tmp.childNodes[i].childNodes[a].offsetWidth;// В полном TR тэгам TD без style.width присваиваем занчение. Во всех тэгах TD(кроме TD[fullTR]) убираем td.style.width
				widthTable+=parseInt(tmp.childNodes[i].childNodes[a].style.width);
				}else{
					tmp.childNodes[i].childNodes[a].style.width=null;
				}
			}
		}
		tagTABLE.height=null;
		tagTABLE.style.height=null;
		tagTABLE.style.width=null;
		tagTABLE.width=widthTable;
		window.status="Ширина таблицы:"+tagTABLE.width+"px";
		break;
	}
tmp=tmp.parentNode;
}
if(trIndex==fullTR)horiz=true;
if(tagTABLE)ShowArrows(vertic,horiz);
document.getElementById('editor').focus();
}
function ShowArrows(vert,horiz) {
if (!tagTD)  return;
cellWidth =  tagTD.offsetWidth;
cellHeight = tagTD.offsetHeight
var cell_hgt = tagTD.offsetTop;
var cell_wdt = tagTD.offsetLeft;
var par = tagTD.offsetParent;
while (par) {
	cell_hgt = cell_hgt + par.offsetTop;
	cell_wdt = cell_wdt + par.offsetLeft;
    current_obj = par;
    par = current_obj.offsetParent;
}
cellX = cell_wdt + tagTD.offsetWidth;
cellY = cell_hgt + tagTD.offsetHeight;
var scrollTop = document.getElementById('editor').scrollTop;
var scrollLeft = document.getElementById('editor').scrollLeft;
document.getElementById("rArrow").style.posLeft = cell_wdt + tagTD.offsetWidth + 2 - scrollLeft;
document.getElementById("rArrow").style.posTop = cell_hgt + (tagTD.offsetHeight / 2) - 2 - scrollTop;
document.getElementById("dArrow").style.posLeft = cell_wdt + (tagTD.offsetWidth / 2) - 2 - scrollLeft;
document.getElementById("dArrow").style.posTop = cell_hgt + tagTD.offsetHeight + 2 - scrollTop;
if (horiz) document.getElementById("rArrow").style.visibility = 'visible';
if (vert)document.getElementById("dArrow").style.visibility = 'visible';
}
function HideArrows() {
document.getElementById("rArrow").style.visibility = 'hidden';
document.getElementById("dArrow").style.visibility = 'hidden';
move=null;
}
function ResizeRow(){
if(!tagTABLE||!tagTR)return;
var newHeigth=2;
move.style.pixelTop = event.clientY-4;
newHeigth=cellHeight+event.clientY-differY;
if(newHeigth>2){
	for(i=0;i<tagTR.childNodes.length;i++)	{
	if(tagTR.childNodes[i].tagName=="TD")tagTR.childNodes[i].style.height=newHeigth;
	window.status="Высота ячейки:"+newHeigth+"px";
	}
}
}
function ResizeCol(){
if(!tagTABLE||!tagTD||!tagTR)return;
var newWidth=2;
var el=tagTR.parentNode;
if(allWidth==0){
	for(i=0;i<el.childNodes[fullTR].childNodes.length;i++)	{
	if(el.childNodes[fullTR].childNodes[i]!=tagTD)allWidth+=parseInt(el.childNodes[fullTR].childNodes[i].style.width);
}
}
newWidth=cellWidth+event.clientX-differX-6;
if(newWidth>=2){
	tagTD.style.width=newWidth;
	tagTABLE.width=allWidth+newWidth;
	window.status="Ширина ячейки:"+newWidth+"px  ширина таблицы:"+tagTABLE.width+"px";
}
}

