//~****************************************************
// cleanAttributes.js
//
// Written by Laurie Brown, Webmaster - Office of Policy, SSA,
// and presented at the FedStats 508/Accessibility Workshop, June 24, 2002.
//
// This script was designed to run in ColdFusion Studio 5.
// It removes removes unnecessary attributes from HTML tables, leaving only COLSPANs and ROWSPANs for <td>s.
//
// This script is provided without warranty.
// This script is the second in a series of four scripts that are provided to show logic that can be used to help automate the tagging of tables for Section 508 compliance.
// Individual solutions will vary based on source program generating the HTML, table structure, and CSS coding.
// Please include appropriate attribution in any future work based on these scripts.
//~****************************************************

function Main() 
{
	var app = Application;
	var doc = app.ActiveDocument;
	var strText = new String();
	
	// turn off screen updating
	doc.BeginUpdate();
	
	// select the entire document
	doc.SelectAll();
	
	// assign it to a string
	strText = doc.SelText;

	// replace HTML tag
	strText = strText.replace(/<html.*?>/gi, "<html lang=\"en-US\">");
	
	// replace HEAD tag
	strText = strText.replace(/<head.*?>.*<\/head>/gi,	"<head>\r\n<title><\/title>\r\n<link rel=\"stylesheet\" type=\"text\/css\" href=\"\/opdocstyle2.css\" title=\"Default Document Style\">\r\n<\/head>");

	// replace BODY tag
	strText = strText.replace(/<body.*?>/gi, "<body>");
	
	// replace TABLE start tag
	strText = strText.replace(/<table.*?>/gi, "<div class=\"table\">\r\n<table cellspacing=\"0\" cellpadding=\"3\">");

	// replace TABLE end tag
	strText = strText.replace(/<\/table>/gi, "</table>\r\n</div>");
	
	// create CAPTION from first row of table
	strText = strText.replace(/<tr.*?>\r\n<td.*?>(Table [0-9.]+) (.*)<\/td>\r\n<\/tr>/gi, "<caption>$1<br>$2<\/caption>");

	// replace TR tags to get rid of all attributes
	strText = strText.replace(/<tr.*?>/gi, "<tr>");
	
	// replace TD tags to get rid of all attributes except COLSPAN and ROWSPAN
	strText = strText.replace(/<td( colspan="[0-9]+")?( rowspan="[0-9]+")?.*?>/gi, "<td$1$2>");

	// remove all COLSPAN="1" and ROWSPAN="1"
	strText = strText.replace(/ colspan="1"/gi, "");
	strText = strText.replace(/ rowspan="1"/gi, "");
	
	// replace selected text in current document with revised text
	doc.SelText = strText;

	// positions cursor at start of document
	doc.CursorDocStart(false);

	// turn screen updating back on	
	doc.EndUpdate();

	// sets status bar text
	app.SetStatusText("Done");
}

