var joinByMatchData = null;

function joinByMatchSetup() {
  	var f = document.forms['parameterForm'];
  	clearForm(f);
   	
  	var div = document.createElement('DIV');
  	f.appendChild(div);
   	   	
	var tb = createTable(div, 5);   	
	   	
	tb.rows[0].appendChild(buildCell(1, buildLabel('Join line with:')));
	tb.rows[0].appendChild(buildCell(1, buildRadio('joinByMatchDirection', '1', true),
													buildLabel('previous line   '),
													buildRadio('joinByMatchDirection', '0', false),
													buildLabel('next line')));
	tb.rows[1].appendChild(buildCell(1, buildLabel('if it:')));
	tb.rows[1].appendChild(buildCell(1, buildRadio('joinByMatchMatch', '1', true),
													buildLabel('matches   '),
													buildRadio('joinByMatchMatch', '0', false),
													buildLabel("doesn't match")));
	   	
  	tb.rows[2].appendChild(buildCell(1, buildLabel('the pattern:')));
  	tb.rows[2].appendChild(buildCell(1, buildText('joinByMatchPattern')));
  	tb.rows[3].appendChild(buildCell(1, buildLabel('')));
  	tb.rows[3].appendChild(buildCell(1, buildCheckbox('joinByMatchRecursive', 'Recursive'),
  													buildLabel('Recursive')));
  	tb.rows[4].appendChild(buildCell(1, buildRunButton(joinByMatchRun)));  	
  	tb.rows[4].appendChild(buildCell(-1, buildButton('Help', joinByMatchHelp),
  	                                     buildButton('Example 1', joinByMatchExample1),
  	                                     buildButton('Example 2', joinByMatchExample2)));
	if (joinByMatchData) {  		                                     
		joinByMatchSet(joinByMatchData);
	}
	
	return joinByMatchTearDown;
}
   
function joinByMatchTearDown() {
	joinByMatchData = joinByMatchGet();
}

function joinByMatchGet() {
  	var o = new Object();
  	o.direction = getRadioChoice(document.forms['parameterForm'].joinByMatchDirection);
  	o.match = getRadioChoice(document.forms['parameterForm'].joinByMatchMatch);
  	o.pattern = document.forms['parameterForm'].joinByMatchPattern.value;
  	o.recursive = document.forms['parameterForm'].joinByMatchRecursive.checked;
  	return o;
}

function joinByMatchSet(o) {
  	setRadioChoice(document.forms['parameterForm'].joinByMatchDirection, o.direction);
  	setRadioChoice(document.forms['parameterForm'].joinByMatchMatch, o.match);
  	document.forms['parameterForm'].joinByMatchPattern.value = o.pattern;
  	document.forms['parameterForm'].joinByMatchRecursive.checked = o.recursive;
}
   
function joinByMatchRun() {
  	makeEditorRequest('joinByMatch', joinByMatchGet());
}
   
function joinByMatchHelp() {
	window.frames['docFrame'].document.location = "html/joinByMatch.html";
}
   
function joinByMatchExample1() {
	document.forms['parameterForm'].joinByMatchDirection[0].checked = true;
	document.forms['parameterForm'].joinByMatchMatch[1].checked = true;
	document.forms['parameterForm'].joinByMatchPattern.value = 'TRACE';
	document.forms['parameterForm'].joinByMatchRecursive.checked = false;
	window.frames['docFrame'].document.location = "html/joinByMatchExample1.html";
	document.forms['inputForm'].inputText.value = 
			'TRACE (07:19) woke up\n' +
			'TRACE (07:23) no milk foun\n' + 
			'd in fridge\n' +
			'TRACE (07:28) got dressed \n' +
			'and went to store, but sto\n' +
			're was closed\n' +
			'TRACE (07:38) decided to g\n' +
			'o back to bed\n' +
			'TRACE (13:12) woke up\n';
	document.forms['outputForm'].outputText.value = '';	
}   

function joinByMatchExample2() {
	document.forms['parameterForm'].joinByMatchDirection[1].checked = true;
	document.forms['parameterForm'].joinByMatchMatch[0].checked = true;
	document.forms['parameterForm'].joinByMatchPattern.value = '</td>$';
	document.forms['parameterForm'].joinByMatchRecursive.checked = true;
	window.frames['docFrame'].document.location = "html/joinByMatchExample2.html";
	document.forms['inputForm'].inputText.value = 
			'<tr>\n' +
			'<td>red</td>\n' +
			'<td>#f00</td>\n' +
			'<td>255,0,0</td>\n' +
			'</tr>\n' +
			'<tr>\n' +
			'<td>green</td>\n' +
			'<td>#0f0</td>\n' +
			'<td>0,255,0</td>\n' +
			'</tr>\n' +
			'<tr>\n' +
			'<td>blue</td>\n' +
			'<td>#00f</td>\n' +
			'<td>0,0,255</td>\n' +
			'</tr>\n';
	document.forms['outputForm'].outputText.value = '';	
}   

   
