/*
    Written by Jonathan Snook, http://www.snook.ca/jonathan
    Add-ons by Robert Nyman, http://www.robertnyman.com
*/

function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}

if(getElementsByClassName(document, 'table', 'RadEWrapper').length > 0){
	RadEditorCommandList["Save Content"] = function(commandName, editor, oTool) { editor.Submit(); }
	RadEditorCommandList["Cancel Content"] = function(commandName, editor, oTool) { editor.CancelEdit(); }
	
	// removes <p> tags from table cells
	RadEditorCommandList["Strip"] = function(commandName, editor, oTool) {
		
		// mozilla
		if(window.getSelection)
		{
			editorHtml = editor.GetSelectionHtml();
			tableRegExp = /^<(\s?)table/ ;
			if (editorHtml.match(tableRegExp))
			{
				var newHtml = editorHtml.replace(/<(\/{0,1})p(.*?)(\/{0,1})\>/gi, '');
				newHtml = newHtml.replace(/(<(?:\/{0,1})(?:td|TD|th|TH|tr|TR|table|TABLE))(?:.*?)((?:\/{0,1})\>)/gi, '$1$2');
				editor.PasteHtml(newHtml);
			}
			else
			{
				alert('Please select a table using the node inspector');	
			}
		}
		
		//  internet explorer
		else if (editor.Document.selection)
		{
			// if the selection is a control
			if ("Control" == editor.Document.selection.type)
			{
				// get the control
				var cr = editor.Document.selection.createRange();
				// if the control is a table
				if (cr(0).tagName == 'TABLE')
				{

					// get the table attributes
					tableAttributes = cr(0).attributes;
					// for each of the table attributes
					for (var t=0; t < tableAttributes.length; t++)
					{
						// remove it
						cr(0).removeAttribute(tableAttributes[t].name);
					}
					// get each of the table cells
					tableCells = cr(0).cells;
					// for each of the cells
					for(var i=0; i < tableCells.length; i++)	
					{
						// get the cell attributes
						cellAttributes = tableCells[i].attributes;
						// for each of the cells attributes
						for (var j=0; j < cellAttributes.length; j++)
						{
							// remove the attribute
							tableCells[i].removeAttribute(cellAttributes[j].name);
						}
						
					}
					
					// for each of the cells
					for(var k=0; k < cr(0).cells.length; k++)
					{
						// remove the paragraphs
						cr(0).cells[k].innerHTML = cr(0).cells[k].innerHTML.replace(/<(\/{0,1})p(.*?)(\/{0,1})\>/gi, '');
					}
				}
				else
				{
					alert('Please select a table using the node inspector');
				}
			}
			else
			{
				alert('Please select a table using the node inspector');
			}
		}
	}
}