/*	
	This script created by sam.foster AT frogdesign.com
	Tuesday, July 31, 2001
	
 	Accepts: single interger or integer range (e.g 4-12), 
	... which are the characters (columns) to remove
	open ended range will (e.g. 5-) will remove everything to the end of the line after the column supplied.
*/

// global variables, constants
var app = Application;
var MB_OK = 0;
var isOK = false;
var strUserInput = '';
var arColumnRange = new Array();

function Main () {

// abort if no text is selected
	if(!app.ActiveDocument.SelText) 
	{
		app.MessageBox("This will only run on a selection \n\naborting..","Remove Columns",MB_OK);
		return false;
	}

// get user input, and quit if no value is given	
  isOK = getUserInput(); if(!isOK) return;

// process user input, and quit if invalid	
  isOK = processUserInput(strUserInput); if(!isOK) return;

	// this gives us an array of all the selected lines that we can loop over
	// thanks to Joel Muller for this block
	var intSelection = app.ActiveDocument.SelLength; 
	var strText = eval("app.ActiveDocument." + ((intSelection > 0) ? "SelText" : "Text"));
	var CrLf = String.fromCharCode(13) + String.fromCharCode(10);
	var arrLines = strText.split(CrLf);
	var tempVal;    

	// now loop through the lines to assemble the return string 
	retString = "";
	for(i=0;i<arrLines.length;i++)
	{
		// pass each line of the the removeColumns subroutine, 
		// this returns the resulting line (string)
		arrLines[i] = removeColumns(arrLines[i]);
		// alert(arrLines[i]);

		// append the new line to the return string
		retString += arrLines[i];

		// add newline unless this is the last line in the selection
		if(i < arrLines.length-1) 
		{
			retString += "\n";
		}
	}

	// replace old with new in the document
	app.ActiveDocument.SelText = retString;

}


function getUserInput() {
  strUserInput = app.InputBox("Remove Columns","columns to remove (zero based):", '');
  if(!strUserInput) { alert("no value entered. Aborting..."); return false; }
  return true;
}  

function processUserInput(strUserInput)
{
	// manipulate the user input to give us a valid 2 integer range we can work with. 	
	// arColumnRange is global..
	var re = /([0-9]+)([, \-])([0-9]*)/;
	if(re.test(strUserInput))
	{
		arColumnRange[0] = RegExp.$1;
		if(RegExp.$3) {
			arColumnRange[1] = RegExp.$3;
		}
		else arColumnRange[1] = '~';
	}
	else if(parseInt(strUserInput) > 0) {
	// if no range was supplied, assume its zero-based
		arColumnRange[0] = 0;
		arColumnRange[1] = parseInt(strUserInput);
	}

	// quit if range is invalid - i.e. if it represents less than 1 column
	if(arColumnRange[1] != '~' && arColumnRange[1] - arColumnRange[0] < 1)
	{
		app.MessageBox("Range must represent at least 1 column \n\naborting..","Remove Columns",MB_OK);
		return false;
	}

	// everything looks good to go...
	return true;
}

function removeColumns(strLine)
{
	// uses global arColumnRange
	// strLine is a single line string.
	// grab up to the beginning of the range

	var strStart = strLine.substring(0,arColumnRange[0]);

	// grab frm the end of the range to the end of the string
	var strEnd;
	if(arColumnRange[1] == '~') {
		// remove everything to the end of the line
		strEnd 	= '';
	}
	else {
		// re-append everything to the end of the line
	 strEnd = strLine.substring(arColumnRange[1],strLine.length);
	}
	
	// return the assembled new string
	return (strStart + strEnd);
}

// misc functions..
function alert(txt) {
	app.MessageBox(txt, "HomeSite: Total Image Widths",0);
}

