var replaceData = null;

function replaceSetup() {
  	var f = document.forms['parameterForm'];
  	clearForm(f);
   	
  	var div = document.createElement('DIV');
  	f.appendChild(div);
   	   	
	var tb = createTable(div, 4);   	
	   	
  	tb.rows[0].appendChild(buildCell(1, buildLabel('Target:')));
  	tb.rows[0].appendChild(buildCell(1, buildText('replaceTarget')));
  	tb.rows[1].appendChild(buildCell(1, buildLabel('Replacement:')));
  	tb.rows[1].appendChild(buildCell(1, buildText('replaceReplacement')));
	tb.rows[2].appendChild(buildCell(2, buildRadio('replaceFirst', '1', true),
													buildLabel('First   '),
													buildRadio('replaceFirst', '0', false),
													buildLabel('All occurrence(s) in each line')));
  	tb.rows[3].appendChild(buildCell(1, buildRunButton(replaceRun)));  	
  	tb.rows[3].appendChild(buildCell(-1, buildButton('Help', replaceHelp),
  	                                     buildButton('Example 1', replaceExample1),
  	                                     buildButton('Example 2', replaceExample2)));
	if (replaceData) {  		                                     
		replaceSet(replaceData);
	}
	
	return replaceTearDown;
}
   
function replaceTearDown() {
	replaceData = replaceGet();
}

function replaceGet() {
  	var o = new Object();
  	o.target = document.forms['parameterForm'].replaceTarget.value;
  	o.replacement = document.forms['parameterForm'].replaceReplacement.value;
  	o.first = getRadioChoice(document.forms['parameterForm'].replaceFirst);
  	return o;
}

function replaceSet(o) {
  	document.forms['parameterForm'].replaceTarget.value = o.target; 
  	document.forms['parameterForm'].replaceReplacement.value = o.replacement;
  	setRadioChoice(document.forms['parameterForm'].replaceFirst, o.first);
}
   
function replaceRun() {
  	makeEditorRequest('replace', replaceGet());
}
   
function replaceHelp() {
	window.frames['docFrame'].document.location = "html/replace.html";
}
   
function replaceExample1() {
	document.forms['parameterForm'].replaceTarget.value = '\\s+';
	document.forms['parameterForm'].replaceReplacement.value = '_';
	document.forms['parameterForm'].replaceFirst[1].checked = true;
	window.frames['docFrame'].document.location = "html/replaceExample1.html";
	document.forms['inputForm'].inputText.value = 'See the    child.    \nHe is pale	and  thin, he wears a    thin  and ragged  linen shirt.';
	document.forms['outputForm'].outputText.value = '';	
}   
   
function replaceExample2() {
	document.forms['parameterForm'].replaceTarget.value = '([^,]+), ([^,]+)';
	document.forms['parameterForm'].replaceReplacement.value = '$2 $1';
	document.forms['parameterForm'].replaceFirst[0].checked = true;
	window.frames['docFrame'].document.location = "html/replaceExample2.html";
	document.forms['inputForm'].inputText.value = 'Smith, Mary Jane\nMacdonald, John\nSt. Michael, Stephen\nArmstrong-Jones, Sarah';
	document.forms['outputForm'].outputText.value = '';	
}   
   
   