/* 
   Script name: PowerPaste
   author: sam@sam-i-am.com
   date: Wednesday, August 09, 2000
   
   purpose: 
   PowerPaste gives the ability to paste a block of text any (given) number of times.
   One variable can be marked to be replaced by either:
   . an incrementing number (currently 0 to n, though I'm almost there with a range eg [3-12]
   . or a value from a comma delimited list
      this in incredibly useful for example when beginning to mark up a dhtml page, where you need lots of 
   DIV tags, IDs, classes, etc.. that differ only slightly, and to a pattern.
   
   Useage (by example):
   Type and select
	   <DIV ID="layer[*]">this layer has the ID: [*]</DIV>
   Then hit your PowerPaste button.. you will be prompted for "the text you want to paste".
   The selection will appear as the default value. Hit OK
   You will be prompted for "repeat how many times?"
   Power Paste accepts a few alternatives at this point:
   Incremental Mode (zero-based):
	   Enter a number. Entering 3 gives me:
		   <DIV ID="layer0">this layer has the ID: 0</DIV>
		   <DIV ID="layer1">this layer has the ID: 1</DIV>
		   <DIV ID="layer2">this layer has the ID: 2</DIV>
		   <DIV ID="layer3">this layer has the ID: 3</DIV>
   Range Mode:
	   Enter a numberic range. Entering 6-9 gives me:
		   <DIV ID="layer6">this layer has the ID: 6</DIV>
		   <DIV ID="layer7">this layer has the ID: 7</DIV>
		   <DIV ID="layer8">this layer has the ID: </DIV>
		   <DIV ID="layer9">this layer has the ID: 3</DIV>
   List Mode:
	   Enter a comma delimited list of values. Entering a,bee,sea,dee,8000 gives me:
		   <DIV ID="layera">this layer has the ID: a</DIV>
		   <DIV ID="layerbee">this layer has the ID: bee</DIV>
		   <DIV ID="layersea">this layer has the ID: sea</DIV>
		   <DIV ID="layerdee">this layer has the ID: dee</DIV>
		   <DIV ID="layer8000">this layer has the ID: 8000</DIV>
      
*/

// object references, constants
  var app = Application;
  var hsOKInfo = 64; // the info icon
  
// declare, initialise variables
  var ourClipBoard = app.ActiveDocument.SelText;
  var outputStr = "";
  var isOK = false;
  var intPaste;
  var strText;

function Main () {
  isOK = getUserInput(); if(!isOK) return;

  isOK = processInput(); if(!isOK) return;
  printOutput()
}

function getUserInput() {
  strText = app.InputBox("PowerPaste","the text to paste (with insertion markers [*]):", ourClipBoard);
  if(!strText) { alert("no text entered. Aborting..."); return false; }
  intPaste = app.InputBox("PowerPaste","repeat how many times? (see source for help)", 0);
  if(intPaste == 0 || intPaste == ' ' || intPaste.length <=0) { alert("Need a positive integer, integer range, or comma delimited list.\nAborting... see script source for more help with PowerPaste useage"); return false; }
  return true;
}  

function processInput() {
  // determine whether there is an insertion marker (indicating one or more variable to increment)
  // syntax will look like [*]
  var re = /\[\*\]/gi;
  if(re.test(strText))
  {
  	//yes, so use the incrementing subroutine
	outputStr = incrementingPaste(strText,intPaste,re);
  }
  else 
  { 
    // no, so use regular 'replicatePaste'
	if(parseInt(intPaste)) 
	{
	  outputStr = replicatePaste(strText,intPaste);
	}
	else return false;
	// return (error) if "repeat how many times" was not an integer
  }
  return true;
} // end processInput()


function printOutput() {
  // print output to page
  app.ActiveDocument.SelText = outputStr;

} // printOutput()


function replicatePaste(strText,intPaste)
{
  outputStr = "";
  
  for(i=0;i<intPaste;i++)
  {
	outputStr += strText + "\n";
  }
  return outputStr;
}


function incrementingPaste(strText,intPaste,re)
{
	outputStr = "";

	// build the array of variables or integers
	// assess whether we are using a variable list
	if(intPaste.length > 1 &&  intPaste.indexOf(",") != -1)
	{
		// then it looks like we use as the variable list
		arrIdentifiers = intPaste.split(",");
	}
	else if(intPaste.length > 1 &&  intPaste.indexOf("-") != -1)
	{
		// then we'll assume a range has been specified
		quantityStart = parseInt(intPaste.substring(0,intPaste.indexOf("-")));
		quantityEnd = parseInt(intPaste.substring(intPaste.indexOf("-")+1,intPaste.length));
		noReplications = quantityEnd - quantityStart;
		var arrIdentifiers = new Array();
		for(i=0;i<=noReplications;i++) { arrIdentifiers[i] = eval(quantityStart + i) }
	}
	else if(parseInt(intPaste))
	{
		// a single integer has been supplied
		quantityStart = 0;
		quantityEnd = parseInt(intPaste);
		noReplications = quantityEnd - quantityStart;
		var arrIdentifiers = new Array();
		for(i=0;i<=noReplications;i++) { arrIdentifiers[i] = eval(quantityStart + i) }
	}
	else {
		// error 
		return false;
	}

	// populate the array that will build the output string
	for(r=0;r<arrIdentifiers.length;r++) 
	{
		// build output str
		outputStr += strText.replace(re,arrIdentifiers[r]) + "\n";
	}
	return outputStr

} // end incrementing subroutine

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