aTable_matrix=new Array();

function cTable_cell()
{
	this.refi=-2;
	this.refr=-1;
	this.refc=-1;
}
cTable_cell.prototype.refi=-2;
cTable_cell.prototype.refr=-1;
cTable_cell.prototype.refc=-1;

function table_prepmatrix(oTable)
{
	aTable=new Array();
	
	cols=0;
	for (c=0;c<oTable.rows(0).cells.length;c++)
	{
		oCell=oTable.rows(0).cells(c);
		cols+=oCell.colSpan;
	}
	
	for (r=0;r<oTable.rows.length;r++)
	{
		aTable[r]=new Array();
		for (c=0;c<cols;c++) aTable[r][c]=new cTable_cell;
	}

	for (r=0;r<oTable.rows.length;r++)
	{
		for (c=0,rc=0;c<oTable.rows(r).cells.length;c++)
		{
			while (aTable[r][rc].refi==-1) rc++;
			oCell=oTable.rows(r).cells(c);
			//alert(oCell.rowSpan+':'+oCell.colSpan);
			for (tr=r;tr<(r+oCell.rowSpan);tr++) for (tc=rc;tc<(rc+oCell.colSpan);tc++)
			{
				//alert(tr+":"+tc);
				aTable[tr][tc].refi=-1;
				aTable[tr][tc].refr=r;
				aTable[tr][tc].refc=rc;
			}
			aTable[r][rc++].refi=oCell.cellIndex;
		}		
	}
	
	return aTable;
}

function table_set_bg(oTable,sr,sc,er,ec,color)
{
	if (aTable_matrix.length==0) aTable_matrix=table_prepmatrix(oTable);

	for(r=sr;r<=er;r++)
	{
		for (c=sc;c<=ec;c++)
		{
			cind=aTable_matrix[r][c].refi;

			if (cind>=0)
			{
				csr=r;
				csc=c;
				oCell=oTable.rows(r).cells(cind);
			} else
			{
				csr=aTable_matrix[r][c].refr;
				csc=aTable_matrix[r][c].refc;
				oCell=oTable.rows(csr).cells(aTable_matrix[csr][csc].refi);
			}

			cer=csr+oCell.rowSpan-1;
			cec=csc+oCell.colSpan-1;
			common=(oCell && csc>=sc && cec<=ec && csr>=sr && cer<=er);

			if (common) oCell.runtimeStyle.backgroundColor=color;
		}
	}
}

function table_set_border(oTable,sr,sc,er,ec,color,width,mode)
{
	if (aTable_matrix.length==0) aTable_matrix=table_prepmatrix(oTable);

	for(r=sr;r<=er;r++)
	{
		for (c=sc;c<=ec;c++)
		{
			cind=aTable_matrix[r][c].refi;

			set_left=false;
			set_right=false;
			set_top=false;
			set_bottom=false;

			if (cind>=0)
			{
				csr=r;
				csc=c;
				oCell=oTable.rows(r).cells(cind);
			} else
			{
				csr=aTable_matrix[r][c].refr;
				csc=aTable_matrix[r][c].refc;
				oCell=oTable.rows(csr).cells(aTable_matrix[csr][csc].refi);
			}

			cer=csr+oCell.rowSpan-1;
			cec=csc+oCell.colSpan-1;
			common=oCell;
			switch(mode)
			{
				case 'o':
					common=common && csc>=sc && cec<=ec && csr>=sr && cer<=er;
					set_left=(csc==sc);
					set_right=(cec==ec);
					set_top=(csr==sr);
					set_bottom=(cer==er);
					break;
				case 'i':
					common=common && csc>=sc && cec<=ec && csr>=sr && cer<=er;
					set_right=(cec!=ec && cec<=ec);
					set_bottom=(cer!=er && cer<=er);
					break;
				case 'b':
					common=common && csc>=sc && cec<=ec;
					set_bottom=(r==cer);
					break;
				case 't':
					common=common && csc>=sc && cec<=ec;
					set_top=(r==csr);
					break;
				case 'l':
					common=common && csr>=sr && cer<=er;
					set_left=(c==csc);
					break;
				case 'r':
					common=common && csr>=sr && cer<=er;
					set_right=(c==cec);
					break;
			}

			if (set_left && common)
			{
				oCell.runtimeStyle.borderLeftColor=color;
				oCell.runtimeStyle.borderLeftStyle='solid';
				oCell.runtimeStyle.borderLeftWidth=width;
			}
			
			if (set_right && common)
			{
				oCell.runtimeStyle.borderRightColor=color;
				oCell.runtimeStyle.borderRightStyle='solid';
				oCell.runtimeStyle.borderRightWidth=width;
			}
			
			if (set_top && common)
			{
				oCell.runtimeStyle.borderTopColor=color;
				oCell.runtimeStyle.borderTopStyle='solid';
				oCell.runtimeStyle.borderTopWidth=width;
			}
			
			if (set_bottom && common)
			{
				oCell.runtimeStyle.borderBottomColor=color;
				oCell.runtimeStyle.borderBottomStyle='solid';
				oCell.runtimeStyle.borderBottomWidth=width;
			}
			
		}
	}
}

function set_rows_visibility(name, show)
{
	id = 0;
	while(SetVisibility(name+'_'+id,show))
		id++;
}

function set_rows_display(name, show)
{
	id = 0;
	while(SetDisplay(name+'_'+id,show))
		id++;
}


