<?xjs
page_start(undefined,'<script src="'+http_request.virtual_path+'toggle_button.js"></script>',"onLoad="+'"'+"document.getElementById('ToggleAll').style.visibility = 'visible';"+'"');
var expand_all_source='';
var collapse_all_source='';

function not_found_error(reason)
{
	http_reply.status="404 Not Found";
	write("<html><head><title>Not found</title></head><body>The requested URL cannot be found. Reason: "+reason+"</body></hmtl>");
	exit(0);
}

var hdr=msgbase.get_msg_header(msgid);
if(hdr==null)
	not_found_error("no header");
if(hdr.attr & (MSG_PRIVATE|MSG_DELETE))
	not_found_error("not viewable");
if(hdr.from.toUpperCase() != poster.toUpperCase())
	not_found_error("poster");
//if(hdr.from_ext != pnum)
//	not_found_error("pnum");

if(hdr.thread_back)
	not_found_error("top");

var date=new Date(hdr.date);
this_year=date.getFullYear();
this_month=date.getMonth()+1;
this_day=date.getDate();

if(year != this_year)
	not_found_error("year");

if(month != this_month)
	not_found_error("month "+month+"!="+this_month);

if(day != this_day)
	not_found_error("day");

if(subject != clean_subject(hdr.subject))
	not_found_error("subject");

var body=msgbase.get_msg_body(true, hdr.offset, true, hdr);
body=html_encode(body,true,true,false,false);
//body=body.split("&#13;&#10;&#13;&#10;").join("</p><p>");
//body=body.split("&#13;&#10;").join(" ");
body=body.split("&#13;&#10;").join("<br>");
?>

<b><?xjs write(hdr.subject); ?></b> (<?xjs write(date); ?>)<br>
<p><?xjs write(body); ?></p>

<hr>
<p><div id="ToggleAll" style="visibility: hidden"><input type="button" name="Collapse All" value="Collapse All" alt="Collpase All" onClick="collapse_all()"><input type="button" name="Expand All" value="Expand All" alt="Expand All" onClick="expand_all()"></div></p>

<?xjs
// "Comments"
var skip=0;
var path=new Array();

/* Depth-first traversal of the thread */
while(1) {
	if(!skip && hdr.thread_first) {
		path.push(hdr);
		msgid=hdr.thread_first;
		skip=0;
	}
	else {
		if(hdr.thread_next) {
			msgid=hdr.thread_next;
			skip=0;
		}
		else {
			if(hdr.thread_back) {
				msgid=hdr.thread_back;
				hdr=path.pop();
				skip=1;
				continue;
			}
			else
				break;
		}
	}
	hdr=msgbase.get_msg_header(msgid);
	if(hdr==null)
		continue;
	if(msg_area.sub[msg_code].is_moderated && (hdr.attr & MSG_VALIDATED) == 0)
		continue;
	if(hdr.attr & (MSG_PRIVATE|MSG_DELETE))
		continue;
	var body=msgbase.get_msg_body(true, hdr.offset, true, hdr);
	body=html_encode(body,true,true,false,false);
	//body=body.split("&#13;&#10;&#13;&#10;").join("</p><p>");
	//body=body.split("&#13;&#10;").join(" ");
	body=body.split("&#13;&#10;").join("<br>");

	expand_all_source += "expand_item('msg-"+msgid+"','button-"+msgid+"');";
	collapse_all_source += "collapse_item('msg-"+msgid+"','button-"+msgid+"');";
?>

<div style="padding-left: <?xjs write(path.length*indent_pixels) ?>px">
<a onClick="toggle_display('msg-<?xjs write(msgid)?>','button-<?xjs write(msgid) ?>');"><img border="0"  src="<?xjs write(http_request.virtual_path) ?>plus.gif" id="button-<?xjs write(msgid); ?>"></a> <b><?xjs write(hdr.subject); ?></b> (<?xjs write(date); ?>)<br>
By: <i><?xjs write(hdr.from); ?></i><br>
<div id="msg-<?xjs write(msgid) ?>" style="display: none"><?xjs write(body); ?>
<hr>
</div>
</div>

<?xjs
}
?>

<script>
var image_path='<?xjs write(http_request.virtual_path) ?>';
function collapse_all()
{
<?xjs write(collapse_all_source)?>
}

function expand_all()
{
<?xjs write(expand_all_source)?>
}
</script>
