// Written by Chris Umphress <chris@epicvoyage.org>
//
// Remember how many sections we are already showing...
var cur_display_id = 0;

// Usage:
//   <?php
//   $out = '<table>...</table>';
//   $out = str_replace("\r", '', $out);
//   $out = str_replace("\n", '', $out);
//   $out = str_replace("'", '&apos;', $out);
//   ?>
//   <div id="tableout"></div>
//   <tag onclick="add_to_tabe('tableout', '<?php print htmlentities($out); ?>');">
// Parameters:
//   tableid     - ID of Div to insert into
//   text        - table to isnert into tableid
//   parent_elem - Optional, prepended to cur_display_id
// Other notes:
//   %X% in "text" is replaced with cur_display_id++
//   %Y% in "text" is shifted to "%X%"
function add_to_table(control_id, text, parent_elem) {
	var fields = new Array();
	var cur_id, f;

	control_id = control_id.replace(/:/g, '\\:');

	cur_id = cur_display_id++;
	if (parent_elem !== undefined) {
		cur_id = parent_elem+'_'+cur_id;
	}

	// Unconvert from minor PHP htmlentities() items (escaped because JavaScript
	// doesn't handle them in regular form very well).
	text = text.replace(/&lt;/g,"<");
	text = text.replace(/&gt;/g,">");
	text = text.replace(/&quot;/g, '"');
	text = text.replace(/&apos;/g, "'");
	text = text.replace(/&amp;/g, "&");

	// Insert a unique ID -- old format
	text = text.replace(/%X%/g, cur_id);
	text = text.replace(/%Y%/g, '%X%');

	// New format -- shift B-W down one
	text = text.replace(/%A%/g, cur_id);
	for (f = 66; f < 88; f++) {
		text = text.replace(new RegExp('%'+String.fromCharCode(f)+'%', 'g'), '%'+String.fromCharCode(f-1)+'%');
	}

	// Update the form
	$('#'+control_id).append(text+"\n");

	return;
}

// New and improved; beats DreamWeaver's function hands-down (even if we do
// use jQuery).
function domTab(i, blockid) {
	var blk, f = 0;

	if (blockid == null) {
		blockid = 'contentblock';
	}

	do {
		blk = $('#'+blockid+(++f));

		if (f == i) {
			blk.css('display', '');
		} else {
			blk.css('display', 'none');
		}
	} while (blk.length);

	return;
}
