function FormatTable(TableToFormat, MarkRows, MarkCells, FirstRowSpecial, LastRowSpecial, FirstColSpecial, LastColSpecial, RowAlternationCount, ColAlternationCount)
{
	// Catch invalid tags passed instead of the table
	if(TableToFormat.tagName.toLowerCase() != "table") {
		alert("Please select a table first.\n\nBitte zuerst eine Tabelle klar anwählen.");
		return;
	}
  
	// Loop through the rows, set all classes according to specs
	for(RowCount = 0; RowCount < TableToFormat.rows.length; RowCount++)
	{
		var RowCurrent = TableToFormat.rows.item(RowCount);
		// Set row class attribute rows if it should
		if(MarkRows)
			RowCurrent.className = (RowCount == 0 && FirstRowSpecial) ? "DRowFirst"
				: (RowCount == TableToFormat.rows - 1 && LastRowSpecial) ? "DRowLast"
				: "DRow" + ((RowCount - FirstRowSpecial) % RowAlternationCount + 1);
		
		// Handle the cells if it should do so
		if(MarkCells)
			for(ColCount = 0; ColCount < RowCurrent.childNodes.length; ColCount++) 
				RowCurrent.childNodes.item(ColCount).className = (ColCount == 0 && FirstColSpecial) ? "DColFirst"
					: (ColCount == TableToFormat.rows - 1 && LastColSpecial) ? "DColLast"
					: "DCol" + ((ColCount - FirstColSpecial) % ColAlternationCount + 1);
	}
}	

// Get the table around the cursor
function GetCursorsParentTable() {

    if (document.selection != null)
        var MyParent = document.selection.createRange().parentElement();
    else
        var MyParent = window.frames[0].getSelection().anchorNode.parentNode;
	
	while(MyParent.tagName != "TABLE")
	{
		// Check if body (which means the cursor was not in a table)
		if(MyParent.tagName == "BODY")
		{
			alert("Please put the cursor inside a table cell for this function to work\n\n"
				+ "Bitte setzen Sie den Cursor in eine Tabellenzelle damit dieses Makro funtioniert");
			return null;
		}
		// keep parent element for further processing
		if (document.selection != null)
		    MyParent = MyParent.parentElement;
		else
		    MyParent = MyParent.parentNode;
	}
	return MyParent
}




function DumpObject(obj) 
{
	var DebugObject = window.open('about:blank','DebugObject','scrollbars=yes,width=450,height=300'); 	
	var strOut = "<table border=\"1\" width=\"400\">"; 
	for(var x in obj) 
		strOut += "<tr><td>" + x + "</td><td>" + obj[x] + "</td></tr>"; 
	strOut += "</table>"; 
	DebugObject.document.write(strOut); 
	alert('Dumped ' + obj.id); 
	DebugObject.close(); 
} 


// ----------------------------------------
// Attach all commands to the RAD editor - use this matrix to create all commands, even if we only need a part of it
// This way we can add buttons without changing the script, and they will always work. 
// ----------------------------------------
var RowFirstOptions = new Array("", "RF");	var RowLastOptions = new Array("", "RL");
var ColFirstOptions = new Array("", "CF");	var ColLastOptions = new Array("", "CL");
var MaxRows = 4;							var MaxCols = 4;
for(i = 0; i < RowFirstOptions.length; i++)
	for(j = 0; j < RowLastOptions.length; j++)
		for(k = 0; k < ColFirstOptions.length; k++)
			for(l = 0; l < ColLastOptions.length; l++)
				for(m = 1; m <= MaxRows; m++)
					for(n = 1; n <= MaxCols; n++)
						RadEditorCommandList['FormatTable' 
							+ RowFirstOptions[i] 
							+ RowLastOptions[j]
							+ ColFirstOptions[k]
							+ ColLastOptions[l]
							+ m + "x" + n] = FormatTableByCommandName; 

// Does a format table based on the "command" comming in. For example, FormatTableRF2x1 means RowFirst, 2 row colors, 1 column color
function FormatTableByCommandName(commandName, editor, oTool)
{
	var Command = commandName.substr(11, 100);
	var RowFirst = (Command.indexOf("RF") > -1);
	var RowLast = (Command.indexOf("RL") > -1);
	var ColFirst = (Command.indexOf("CF") > -1);
	var ColLast = (Command.indexOf("CL") > -1);
	var RowCount = Command.substr(Command.search(/[0-9]/), 1);
	var ColCount = Command.substr(Command.search(/x[0-9]/) + 1, 1);
	var MyTable = GetCursorsParentTable();	// Get current table 
	if(MyTable == null) return false;		// Exit if not found
	FormatTable(MyTable, true, true, RowFirst, RowLast, ColFirst, ColLast, RowCount, ColCount);
}
