//~****************************************************
// cleanTags.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 extra white space characters, removes comments, and runs a customized CodeSweeper on an HTML table.
//
// This script is provided without warranty.
// This script is the first 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 <br>s with spaces
	strText = strText.replace(/<br>/gi, " ");
	
	// replace white space characters with a single space
		// replace hard returns with a single space
		strText = strText.replace(/\r\n/g, " ");
		// replace tabs with a single space
		strText = strText.replace(/\t/g, " ");
		// replace nonbreaking spaces with regular spaces
		strText = strText.replace(/&nbsp;/gi, " ");
		// replace multiple spaces with a single space
		strText = strText.replace(/[ ]+/g, " ");

	// remove comments
	strText = strText.replace(/<!.+?>/g, "");

	// replace selected text in current document with revised text
	doc.SelText = strText;

	// run CodeSweeper
		//Note:  There seems to be a quirk with CFStudio--even with the following line of code, it still wants to use whatever CodeSweeper is set as the default.
		// However, if I run the "Tables" CodeSweeper once (just open a file, run the CodeSweeper, and close the file without saving changes) it will then use the correct CodeSweeper for the rest of the session.
		// select appropriate CodeSweeper
		app.SetActiveCodeSweeper('C:\\Program Files\\Macromedia\\ColdFusion Studio 5\\Extensions\\CodeSweepers\\Tables.vtm');
		// run it
		app.RunCodeSweeper();

	// position cursor at start of document
	doc.CursorDocStart(false);

	// turn screen updating back on		
	doc.EndUpdate();
	
	// sets status bar text
	app.SetStatusText("Done");
}
