###############################################################################
# Post.pl                                                                     #
###############################################################################
# YaBB: Yet another Bulletin Board                                            #
# Open-Source Community Software for Webmasters                               #
# Version:        YaBB 2 RC 1                                                 #
# Released:       December 27, 2004                                           #
# Distributed by: http://www.yabbforum.com                                    #
# =========================================================================== #
# Copyright (c) 2000-2005 YaBB (www.yabbforum.com) - All Rights Reserved.     #
# Software by: The YaBB Development Team                                      #
#              with assistance from the YaBB community.                       #
# Sponsored by: Xnull Internet Media, Inc. - http://www.ximinc.com            #
#               Your source for web hosting, web design, and domains.         #
###############################################################################

$postplver = 'YaBB 2 RC 1 $Revision: 1.112 $';
if($action eq 'detailedversion') { return 1; }

if (!$post_txt_loaded){
	LoadLanguage("Post");
	$post_txt_loaded = 1;
}
LoadLanguage("Display");
LoadLanguage("FA");

sub Post {
	if($iamguest && $enable_guestposting == 0) { &fatal_error($post_txt{'165'}); }
	if( $currentboard eq '' ) { &fatal_error($post_txt{'1'}); }
	my($filetype_info, $filesize_info);
	my( $subtitle, $x, $mnum, $msub, $mname, $memail, $mdate, $mreplies, $musername, $micon, $mstate, $msubject, $mattach, $mip, $mmessage, $mns, $quotestart, $notify);
	my $quotemsg = $INFO{'quote'};
	$threadid = $INFO{'num'};

	($mnum, $msub, $mname, $memail, $mdate, $mreplies, $musername, $micon, $mstate) = split(/\|/,$yyThreadLine);
	if( $mstate =~ /l/i ) { &fatal_error($post_txt{'90'}); }

	#Determine category
	$curcat = ${$uid.$currentboard}{'cat'};
	&BoardTotals("load",$currentboard);

	# Figure out the name of the category
	unless ($mloaded == 1){require "$boardsdir/forum.master";}
	($cat, $catperms) = split(/\|/, $catinfo{"$curcat"});

	$pollthread = 0;
	$post_polltxt{'1a'} =~ tr/+/ /;
	$post_polltxt{'2a'} =~ tr/+/ /;
	if ($INFO{'title'} eq $post_polltxt{'1a'}) { $pollthread = 1; }
	elsif ($INFO{'title'} eq $post_polltxt{'2a'}) { $pollthread = 2; }
	if ($pollthread == 2 && $useraddpoll == 0) { &fatal_error($post_txt{'1'}); }

	# check if you are already being notified and if so we check the checkbox. easy =-)
	# if the mail file exists then we have to check it otherwise we continue on
	if(-e "$datadir/$threadid.mail"){
		fopen(CHECKNOTIFY, "$datadir/$threadid.mail");
		my @entries = <CHECKNOTIFY>;
		fclose(CHECKNOTIFY);
		foreach $entry (@entries) {
			($curuser,$curmail,$curlang,$isboardnotify,$hasviewed) = split (/\|/, $entry);
			if ($curuser eq $username) {
				$notify = " CHECKED ";
				last;
			}
		}
	} else {
		#no notification file exists
	}

	$notification = ! $enable_notification || $iamguest ? '' : <<"~;";
    <tr>
      <td class="windowbg"><b>$post_txt{'131'}:</b></td>
      <td class="windowbg"><input type="checkbox" name="notify" value="x"$notify /> <span  class="small">$post_txt{'750'}</span></td>
    </tr>
~;

	$name_field = $realname eq '' ? qq~      <tr>
    <td class="windowbg" align="left" width="23%"><b>$post_txt{'68'}:</b></td>
    <td class="windowbg" align="left" width="77%"><input type="text" name="name" size="25" value="$FORM{'name'}" maxlength="25" tabindex="2" /></td>
      </tr>~ : qq~~;

	$email_field = $realemail eq '' ? qq~      <tr>
    <td class="windowbg" width="23%"><b>$post_txt{'69'}:</b></td>
    <td class="windowbg" width="77%"><input type="text" name="email" size="25" value="$FORM{'email'}" maxlength="40" tabindex="3" /></td>
      </tr>~ : qq~~;

	$sub = "";
	$settofield="subject";
	if( $threadid ne '' ) {
		fopen(FILE, "$datadir/$threadid.txt") || &fatal_error("201 $post_txt{'106'}: $post_txt{'23'} $threadid.txt", 1);
		@messages = <FILE>;
		fclose(FILE);
		$message =~ s/([\000-\x09\x0b\x0c\x0e-\x1f\x7f])/\x0d/g;
		if($quotemsg ne '') {
			($msubject, $mname, $memail, $mdate, $musername, $micon, $mattach, $mip, $mmessage, $mns) = split(/\|/,$messages[$quotemsg]);
			$message=$mmessage;
			$message =~ s~<br />~\n~g;
			$message =~ s/ \&nbsp; \&nbsp; \&nbsp;/\t/ig;
			if (!$nestquotes){
				$message =~ s~\n{0,1}\[quote([^\]]*)\](.*?)\[/quote\]\n{0,1}~\n~isg;
				$message =~ s~\n*\[/*quote([^\]]*)\]\n*~~ig;
			}
			$mname ||= $musername || $post_txt{'470'};
			$quotestart = int( $quotemsg / $maxmessagedisplay ) * $maxmessagedisplay;
			$message = qq~[quote author=$mname link=$threadid/$quotestart#$quotemsg date=$mdate\]$message\[/quote\]\n~;
			$msubject =~ s/\bre:\s+//ig;
			if ($mns eq "NS") {$nscheck="checked";}
		}
		else {
			($msubject, $mname, $memail, $mdate, $musername, $micon, $mattach, $mip, $mmessage, $mns) = split(/\|/,$messages[0]);
			$msubject =~ s/\bre:\s+//ig;
		}
		$sub = "Re: $msubject";
		$settofield="message";
	}
	$submittxt = "$post_txt{'105'}";
	$destination = "post2";
	$icon = "xx";
	$is_preview = 0;
	$post = "post";
	$preview = "preview";
	$yytitle = "$INFO{'title'}";
	&Postpage;
	if ($pollthread != 2) { &doshowthread; }
	&template;
	exit;
}

sub Postpage {
	my $extra;
	my($filetype_info, $filesize_info, $extensions);
	$extensions = join(" ", @ext);
	$filetype_info = $checkext == 1 ? qq~$fatxt{'2'} $extensions~ : qq~$fatxt{'2'} $fatxt{'4'}~;
	$filesize_info = $limit != 0 ? qq~$fatxt{'3'} $limit KB~ : qq~$fatxt{'3'} $fatxt{'5'}~;
	if ($is_preview) {$post_txt{'507'}=$post_txt{'771'};}
	$normalquot = $post_txt{'599'};
	$simpelquot = $post_txt{'601'};
	$simpelcode = $post_txt{'602'};
	$edittext = $post_txt{'603'};
	if(!$fontsizemax){$fontsizemax = 72;}
	if(!$fontsizemin){$fontsizemin = 6;}
	$message =~ s~<\/~\&lt\;/~isg;
	$message =~ s/([\000-\x09\x0b\x0c\x0e-\x1f\x7f])/\x0d/g;
	&ToChars($message);
	&ToChars($sub);
	# this defines what the top area of the post box will look like: option 1 ) IM area
	# option 2) all other post areas
	if($post eq "imsend") {
		if(!$INFO{'to'}) { $INFO{'to'} = $FORM{'to'};}
		if($INFO{'to'}) {$settofield="message";} else {$settofield="to";}
		$idinfo = "$INFO{'id'}";
		$extra = qq~
      <tr>
        <td class="windowbg" width="23%"><b>$post_txt{'150'}</b></td>
        <td class="windowbg">
	<input type="text" name="to" value="$INFO{'to'}" size="20" maxlength="50" tabindex="2" />
	<span class="small">$post_txt{'748'}</span></td>
      </tr>
		~;

	} else {
		$extra = qq~
      <tr>
        <td class="windowbg" width="23%"><b>$post_txt{'71'}:</b></td>
        <td class="windowbg">
        <select name="icon" onchange="showimage(); updatTopic();">
         <option value="xx"$ic1>$post_txt{'281'}</option>
         <option value="thumbup"$ic2>$post_txt{'282'}</option>
         <option value="thumbdown"$ic3>$post_txt{'283'}</option>
         <option value="exclamation"$ic4>$post_txt{'284'}</option>
         <option value="question"$ic5>$post_txt{'285'}</option>
         <option value="lamp"$ic6>$post_txt{'286'}</option>
         <option value="smiley"$ic7>$post_txt{'287'}</option>
         <option value="angry"$ic8>$post_txt{'288'}</option>
         <option value="cheesy"$ic9>$post_txt{'289'}</option>
         <option value="laugh"$ic10>$post_txt{'290'}</option>
         <option value="sad"$ic11>$post_txt{'291'}</option>
         <option value="wink"$ic12>$post_txt{'292'}</option>
        </select>
        <img src="$imagesdir/$icon.gif" name="icons" border="0" hspace="15" alt="" /></td>
      </tr>
	 	~;
		if ($realname eq '' && $threadid ne '') {$settofield="name";}
	}

	#this shows on every post page. regardless of where it is called from
	$yymain .= qq~

	
~;
	if ($pollthread && $iamguest) {$guest_vote = 1;}
	if ($pollthread == 2) {
		$settofield="question";
		$sub = "$msubject";
	} else {
		#this defines if the notify on reply is shown or not.
		if (!$enable_notification || $iamguest) {
			$notification = ""
		} else {		
			# check if you are already being notified and if so we check the checkbox. easy =-)
			# if the mail file exists then we have to check it otherwise we continue on
			if(-e "$datadir/$threadid.mail"){
				fopen(CHECKNOTIFY, "$datadir/$threadid.mail");
				my @entries = <CHECKNOTIFY>;
				fclose(CHECKNOTIFY);
				foreach $entry (@entries) {
					($curuser,$curmail,$curlang,$isboardnotify,$hasviewed) = split (/\|/, $entry);
					if ($curuser eq $username) {
						$notify = " CHECKED ";
						last;
					}
				}
			} else {
				#no notification file exists
			}

if ($post ne "imsend") {
			$notification = qq~
	      <tr>
                <td><b>$post_txt{'131'}:</b></td>
                <td><input type="checkbox" name="notify" value="x"$notify /> <span class="small">$post_txt{'750'}</span></td>
              </tr>~;}
		}
}

	if(!$sub) { $subtitle = "<i>$post_txt{'33'}</i>"; }
	else { $subtitle = "<i>$sub</i>"; }
	# this is shown every post page except the IM area.
	unless ($post eq "imsend"){

		if ($threadid) {
			$threadlink = qq~<a href="$scripturl?num=$threadid" class="nav">$subtitle</a>~;
		} else {
			$threadlink = "$subtitle";
		}
		&ToChars($boardname);
		$yymain .= qq~
	<div style="width: 100%; margin-left: auto; margin-right: auto;">
    <span class="small"><b>		   
	    <a href="$scripturl" class="nav">$mbname</a> &rsaquo; 
          <a href="$scripturl?catselect=$catid" class="nav">$cat</a> &rsaquo; 
		     <a href="$scripturl?board=$currentboard" class="nav">$boardname</a> &rsaquo;
				 $INFO{'title'} ( $threadlink )</b>
       </span>
	</div>
		~;
	}	
#this is the end of the upper area of the post page.
$yymain .= qq~
<script language="JavaScript1.2" src="$html_root/post.js" type="text/javascript"></script>
<script language="JavaScript1.2" type="text/javascript">
<!--
var postas = '$post';
var namefield = '$realname';
var mailfield = '$realemail';
function checkForm(theForm) {
if (navigator.appName == "Microsoft Internet Explorer") { theForm.message.createTextRange().execCommand("Copy"); }
if (theForm.subject.value == "") { alert("$post_txt{'77'}"); theForm.subject.focus(); return false }
if (postas == "imsend") { if (theForm.to.value == "") { alert("$post_txt{'752'}"); theForm.to.focus(); return false } }
else {
	if (namefield == "") {
		if (theForm.name.value == "" || theForm.name.value == "_" || theForm.name.value == " ") { alert("$post_txt{'75'}"); theForm.name.focus(); return false }
		if (theForm.name.value.length > 25)  { alert("$post_txt{'568'}"); theForm.name.focus(); return false }
	}
	if (mailfield == "") {
		if (theForm.email.value == "") { alert("$post_txt{'76'}"); theForm.email.focus(); return false }
		if (! checkMailaddr(theForm.email.value)) { alert("$post_txt{'500'}"); theForm.email.focus(); return false }
	}
}
if (theForm.message.value == "") { alert("$post_txt{'78'}"); theForm.message.focus(); return false }
return true
}
//-->
</script>

~;
# if this is an IM from the admin or to groups declare where it goes.
if($INFO{'adminim'} || $INFO{'action'} eq "imgroups") {
	$yymain .= qq~<form action="$scripturl?action=imgroups" method="post" name="postmodify" onsubmit="return submitproc()">~;
} else {
	if($allowattach && ${$uid.$currentboard}{'attperms'} == 1){
		$yymain .= qq~<form action="$scripturl?board=$currentboard;action=$destination" method="post" name="postmodify" enctype="multipart/form-data" onsubmit="return checkForm(this); return submitproc()">~;
	} else {
		$yymain .= qq~<form action="$scripturl?board=$currentboard;action=$destination" method="post" name="postmodify" onsubmit="return checkForm(this); return submitproc()">~;
	}
}
#this declares the beginning of the UBBC section
$yymain .= qq~
<div class="bordercolor" style="padding: 1px; width: 100%; margin-left: auto; margin-right: auto;">
<script language="JavaScript1.2" src="$ubbcjspath" type="text/javascript"></script>
<script language="JavaScript1.2" type="text/javascript">
<!--
~;

	$moresmilieslist = "";
	$more_smilie_array = "";
	$i=0;
	if ($showadded eq 1){
		while($SmilieURL[$i]) {
			if ($SmilieURL[$i]=~ /\//i) {$tmpurl = $SmilieURL[$i];} else {$tmpurl = qq~$imagesdir/$SmilieURL[$i]~;}
			$moresmilieslist .= qq~      document.write("<img src='$tmpurl' align='bottom' alt='$SmilieDescription[$i]' border='0' onclick='javascript:MoreSmilies($i)' style='cursor:hand' alt=''>$SmilieLinebreak[$i] ");\n~;
			$tmpcode = $SmilieCode[$i];
			$tmpcode =~ s/\&quot;/"+'"'+"/g; #" Adding that because if not it'll screw up my syntax view'
			&FromHTML($tmpcode);
			$tmpcode =~ s/&#36;/\$/g;
			$tmpcode =~ s/&#64;/\@/g;
			$more_smilie_array .= qq~" $tmpcode", ~;
			$i++;
		}
	}

	if ($showsmdir eq 1){
		opendir(DIR, "$smiliesdir");
		@contents = readdir(DIR);
		closedir(DIR);

		foreach $line (sort {uc($a) cmp uc($b)} @contents) {
			($name, $extension) = split (/\./, $line);
			if ($extension =~ /gif/i || $extension =~ /jpg/i || $extension =~ /jpeg/i || $extension =~ /png/i ){
				if ($line !~ /banner/i) {
					$moresmilieslist .= qq~      document.write("<img src='$smiliesurl/$line' align='bottom' alt='$name' border='0' onclick='javascript:MoreSmilies($i)' style='cursor:hand'>$SmilieLinebreak[$i] ");\n~;
					$more_smilie_array .= qq~" [smiley=$line]", ~;
					$i++
				}
			}
		}
	}

 	$more_smilie_array .= qq~""~;

	$yymain .= qq~
moresmiliecode = new Array($more_smilie_array)

 function MoreSmilies(i) {
  AddTxt=moresmiliecode[i];
  AddText(AddTxt);
 }

~;
#this is more stuff for the ubbc but it is specifically for smilies and the top area of the page. including the title
	if ($smiliestyle eq 1) { $smiliewinlink = "$scripturl?action=smilieput"; }
	else { $smiliewinlink = "$scripturl?action=smilieindex"; }

	$yymain .= qq~
function smiliewin() {
  window.open("$smiliewinlink", 'list', 'width=$winwidth,height=$winheight, scrollbars=yes');
}

function showimage() {
   document.images.icons.src="$imagesdir/"+document.postmodify.icon.options[document.postmodify.icon.selectedIndex].value+".gif";
}

//-->
</script>
    <input type="hidden" name="threadid" value="$threadid" />
    <input type="hidden" name="postid" value="$postid" />
    <input type="hidden" name="info" value="$idinfo" />
    
    <table border="0" width="100%" cellpadding="3" cellspacing="0" class="windowbg">
      <tr>
        <td class="titlebg" height="18" colspan="2"><span class="text1"><b>$yytitle</b></span></td>
      </tr>
	~;

## [Poll Mod Options] ##
	if ($pollthread) {
		$maxpq ||= 60;
		$maxpo ||= 50;
		$maxpc ||= 0;
		$numpolloptions ||= 8;

		if ($guest_vote) {$gvchecked = " checked";}
		if ($hide_results) {$hrchecked = " checked";}
		if ($multi_choice) {$mcchecked = " checked";}
		$yymain .= qq~
        <tr>
            <input type="hidden" name="pollthread" value="$pollthread" />
                <td width="23%" align="left" class="windowbg2"><b>$post_polltxt{'6'}:</b></td>
                <td align="left"  class="windowbg2"><input type="text" size="40" name="question" value="$poll_question" maxlength="$maxpq" /></td>
              </tr>~;

		for(my $i = 1; $i <= $numpolloptions; $i++) {
			$yymain .= qq~
                    <tr>
                      <td align="right" class="windowbg2"> &nbsp; $post_polltxt{'7'} $i: &nbsp;</td>
                      <td align="left" class="windowbg2"><input type="text" size="25" maxlength="$maxpo" name="option$i" value="$options[$i]" /></td>
                    </tr>~;
		}
		$yymain .= qq~
                
                </td>~;

		if ($maxpc > 0) {
			$yymain .= qq~
             </tr><tr>
                <td valign=top class="windowbg2" width="23%"><b>$post_polltxt{'59'}:</b></td>
                <td class="windowbg2"><textarea name="poll_comment" rows="3" cols="60" wrap="soft" onkeyup="if (document.postmodify.poll_comment.value.length > $maxpc) {document.postmodify.poll_comment.value = document.postmodify.poll_comment.value.substring(0,$maxpc)}">$poll_comment</textarea></td>~;
		}

		$yymain .= qq~
              </tr><tr>
                <td width="23%" align="left"><b>$post_polltxt{'32'}</b></td>
                <td align="left"><input type="checkbox" name="guest_vote" value="1"$gvchecked /> <span  class="small">$post_polltxt{'54'}</span></td>
              </tr><tr>
                <td width="23%" align="left"><b>$post_polltxt{'26'}</b></td>
                <td align="left"><input type="checkbox" name="hide_results" value="1"$hrchecked /> <span  class="small">$post_polltxt{'55'}</span></td>
              </tr><tr>
                <td width="23%" align="left"><b>$post_polltxt{'58'}</b></td>
                <td align="left"><input type="checkbox" name="multi_choice" value="1"$mcchecked /> <span  class="small">$post_polltxt{'56'}</span></td>
              </tr>
~;
	}

	if($pollthread != 2) {
	$yymain .= qq~
      <tr>
        <td class="windowbg2" valign="top">
	  <div id="SaveInfo" style="height: 16px;">
        <img name="prevwin" id="prevwin" src="$imagesdir/cat_expand.gif" alt="$npf_txt{'01'}" border="0" style="cursor: pointer; cursor: hand;" onclick="enabPrev();" /> <b>$npf_txt{'04'}</b>
        </div>
        </td>
        <td class="windowbg2">
        <div id="SaveTopic" style="float: left; height: 0px; font-weight: bold; visibility: hidden; overflow: auto;">&nbsp;</div>
        <div id="SaveFrame" name="SaveFrame" class="message" style="float: left; height: 0px; border-top: 1px black solid; padding-top: 4px; vertical-align: top; visibility: hidden; overflow: auto;">&nbsp;</div>
        </td>
      </tr>
	~;
	}
	if ($pollthread != 2 || ($pollthread == 2 && $iamguest)) {
		if ($pollthread == 2 ) {$extra = "";}
		else {
			$yymain .= qq~
		      <tr>
		        <td align="left" class="windowbg" width="23%"><b>$post_txt{'70'}:</b></td>
		        <td align="left" class="windowbg" width="77%"><input type="text" name="subject" value="$sub" size="40" maxlength="50" tabindex="1" onchange="updatTopic()" /></td>
		      </tr>
			~;
		}
		$yymain .= qq~
		$name_field
		$email_field
		$extra
	  	~;
	}

	if ($pollthread != 2) { # if not adding a poll to an existing thread, display standard post page inputs

## [/Poll Mod Options] ##

if($enable_ubbc && $showyabbcbutt) {
	# this is for the ubbc buttons
	$yymain .= qq~
  <tr>
      <td class="windowbg2" width="23%"><b>$post_txt{'252'}:</b></td>
        <td valign="middle" class="windowbg2">
		<div style="width: 438px; clear: both;">
		<div style="width: 345px; float:left;">
        <script language="JavaScript1.2" type="text/javascript">
        <!--
        if((navigator.appName == "Netscape" && navigator.appVersion.charAt(0) >= 4) || (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.charAt(0) >= 4) || (navigator.appName == "Opera" && navigator.appVersion.charAt(0) >= 4) || (navigator.appName == "WebTV Plus Receiver" && navigator.appVersion.charAt(0) >= 3) || (navigator.appName == "Konqueror" && navigator.appVersion.charAt(0) >= 2)) {
          HAND = "onmouseover=this.style.cursor='hand'";
          document.write("<img src='$imagesdir/url.gif' onclick='hyperlink();' "+HAND+" align=top width=23 height=22 alt='$post_txt{'257'}' title='$post_txt{'257'}' border='0'>");
          document.write("<img src='$imagesdir/ftp.gif' onclick='ftp();' "+HAND+" align=top width=23 height=22 alt='$post_txt{'434'}' title='$post_txt{'434'}' border='0'>");
          document.write("<img src='$imagesdir/img.gif' onclick='image();' "+HAND+" align=top width=23 height=22 alt='$post_txt{'435'}' title='$post_txt{'435'}' border='0'>");
          document.write("<img src='$imagesdir/email2.gif' onclick='emai1();' "+HAND+" align=top width=23 height=22 alt='$post_txt{'258'}' title='$post_txt{'258'}' border='0'>");
          document.write("<img src='$imagesdir/flash.gif' onclick='flash();' "+HAND+" align=top width=23 height=22 alt='$post_txt{'433'}' title='$post_txt{'433'}' border='0'>");
		  document.write("<img src='$imagesdir/table.gif' onclick='table();' "+HAND+" align=top width=23 height=22 alt='$post_txt{'436'}' title='$post_txt{'436'}' border='0'>");
          document.write("<img src='$imagesdir/tr.gif' onclick='trow();' "+HAND+" align=top width=23 height=22 alt='$post_txt{'449'}' title='$post_txt{'449'}' border='0'>");
          document.write("<img src='$imagesdir/td.gif' onclick='tcol();' "+HAND+" align=top width=23 height=22 alt='$post_txt{'437'}' title='$post_txt{'437'}' border='0'>");
          document.write("<img src='$imagesdir/hr.gif' onclick='hr();' "+HAND+" align=top width=23 height=22 alt='$post_txt{'531'}' title='$post_txt{'531'}' border='0'>");
          document.write("<img src='$imagesdir/tele.gif' onclick='teletype();' "+HAND+" align=top width=23 height=22 alt='$post_txt{'440'}' title='$post_txt{'440'}' border='0'>");
          document.write("<img src='$imagesdir/code.gif' onclick='showcode();' "+HAND+" align=top width=23 height=22 alt='$post_txt{'259'}' title='$post_txt{'259'}' border='0'>");
          document.write("<img src='$imagesdir/quote2.gif' onclick='quote();' "+HAND+" align=top width=23 height=22 alt='$post_txt{'260'}' title='$post_txt{'260'}' border='0'>");
		  document.write("<img src='$imagesdir/sup.gif' onclick='superscript();' "+HAND+" align=top width=23 height=22 alt='$post_txt{'447'}' title='$post_txt{'447'}' border='0'>");
          document.write("<img src='$imagesdir/sub.gif' onclick='subscript();' "+HAND+" align=top width=23 height=22 alt='$post_txt{'448'}' title='$post_txt{'448'}' border='0'>");
          document.write("<img src='$imagesdir/move.gif' onclick='move();' "+HAND+" align=top width=23 height=22 alt='$post_txt{'439'}' title='$post_txt{'439'}' border='0'></b><br />");
		  document.write("<img src='$imagesdir/bold.gif' onclick='bold();' "+HAND+" align=top width=23 height=22 alt='$post_txt{'253'}' title='$post_txt{'253'}' border=0>");
          document.write("<img src='$imagesdir/italicize.gif' onclick='italicize();' "+HAND+" align=top width=23 height=22 alt='$post_txt{'254'}' title='$post_txt{'254'}' border='0'>");
          document.write("<img src='$imagesdir/underline.gif' onclick='underline();' "+HAND+" align=top width=23 height=22 alt='$post_txt{'255'}' title='$post_txt{'255'}' border='0'>");
          document.write("<img src='$imagesdir/strike.gif' onclick='strike();' "+HAND+" align=top width=23 height=22 alt='$post_txt{'441'}' title='$post_txt{'441'}' border='0'>");
		  document.write('<select name="fontface" id="fontface" onchange="if(this.options[this.selectedIndex].value) fontfce(this.options[this.selectedIndex].value);" style="width: 93px; margin-top: 2px; margin-left: 2px; margin-right: 1px; font-size: 9px;">');
          document.write('<option value="">Verdana</option>');
          document.write('<option value="">-----</option>');
		  document.write('<option value="Sans-Serif" style="font-family: Sans-Serif">Sans-Serif</option>');
          document.write('<option value="Verdana" style="font-family: Verdana" selected="selected">Verdana</option>');
		  document.write('<option value="Arial" style="font-family: Arial">Arial</option>');
		  document.write('<option value="Serif" style="font-family: Serif">Serif</option>');
          document.write('<option value="Courier" style="font-family: Courier">Courier</option>');
          document.write('<option value="Courier New" style="font-family: Courier New">Courier New</option>');
		  document.write('<option value="Fantasy" style="font-family: Fantasy">Fantasy</option>');
		  document.write('<option value="monospace" style="font-family: monospace">Monospace</option>');
          document.write('</select>');
          document.write('<select name="fontsize" id="fontsize" onchange="if(this.options[this.selectedIndex].value) fntsize(this.options[this.selectedIndex].value);" style="width: 39px; margin-top: 2px; margin-left: 1px; margin-right: 2px; font-size: 9px;">');
          document.write('<option value="">11</option>');
          document.write('<option value="">--</option>');
          document.write('<option value="6">6</option>');
          document.write('<option value="7">7</option>');
          document.write('<option value="8">8</option>');
          document.write('<option value="9">9</option>');
          document.write('<option value="10">10</option>');
          document.write('<option value="11" selected="selected">11</option>');
          document.write('<option value="12">12</option>');
          document.write('<option value="14">14</option>');
          document.write('<option value="16">16</option>');
          document.write('<option value="18">18</option>');
          document.write('<option value="20">20</option>');
          document.write('<option value="22">22</option>');
          document.write('<option value="24">24</option>');
          document.write('<option value="36">36</option>');
          document.write('<option value="48">48</option>');
          document.write('<option value="56">56</option>');
          document.write('<option value="72">72</option>');
          document.write('</select>');
		  document.write("<img src='$imagesdir/pre.gif' onclick='pre();' "+HAND+" align=top width=23 height=22 alt='$post_txt{'444'}' title='$post_txt{'444'}' border='0'>");
          document.write("<img src='$imagesdir/left.gif' onclick='left();' "+HAND+" align=top width=23 height=22 alt='$post_txt{'445'}' title='$post_txt{'445'}' border='0'>");
          document.write("<img src='$imagesdir/center.gif' onclick='center();' "+HAND+" align=top width=23 height=22 alt='$post_txt{'256'}' title='$post_txt{'256'}' border='0'>");
          document.write("<img src='$imagesdir/right.gif' onclick='right();' "+HAND+" align=top width=23 height=22 alt='$post_txt{'446'}' title='$post_txt{'446'}' border='0'>");
          document.write("<img src='$imagesdir/list.gif' onclick='list();' "+HAND+" align=top width=23 height=22 alt='$post_txt{'261'}' title='$post_txt{'261'}' border='0'>");
          img1 = new Image();
          img1.src = "$imagesdir/palette.gif";

        }
        else { document.write("<span class='small'>$post_txt{'215'}</span>"); }
        //-->
        </script>
        <noscript>
        <span class="small">$post_txt{'215'}</span>
        </noscript></div>
        <div style="height: 42px; width: 86px; overflow: auto; border: 1px; border-style: outset; float: right;">
        ~;
        for($z=0; $z<256; $z+=51) {
          $c3 = sprintf ("%02x", $z);
          for($y=0; $y<256; $y+=51) {
            $c2 = sprintf ("%02x", $y);
            for($x=0; $x<256; $x+=51) {
              $c1 = sprintf ("%02x", $x);
              $selcolor = qq~#$c1$c2$c3~;
if($ENV{'HTTP_USER_AGENT'} =~ /Safari/) {$yymain .= qq~<span style="background-color: $selcolor; width: 11px; height: 11px;"><img src="$imagesdir/palette.gif" border="0" hspace="0" vspace="0" alt="" style="height: 11px; width: 11px; margin: 0px;" onclick="showcolor('$selcolor')" /></span>~;}
else {$yymain .= qq~<span style="float: left; background-color: $selcolor; width: 9px; height: 9px; border: 1px outset; font-size: 5px;" onclick="showcolor('$selcolor')">&nbsp;</span>~;}
            }
if($ENV{'HTTP_USER_AGENT'} =~ /Safari/) {$yymain .= qq~<br />~;}
          }
        }
        $yymain .= qq~
        </div>
		</div>
        </td>
      </tr>
~;
}
else {
	$yymain .= qq~
      <!--<tr>
        <td colspan="2">
        <table width="100%" cellpadding="3" cellspacing="0" class="windowbg">-->
~;
}
	$yymain .= qq~
      <tr>
        <td class="windowbg2" width="23%"><b>$post_txt{'297'}:</b></td>
        <td valign="middle" class="windowbg2">
        <script language="JavaScript1.2" type="text/javascript">
        <!--
        if((navigator.appName == "Netscape" && navigator.appVersion.charAt(0) >= 4) || (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.charAt(0) >= 4) || (navigator.appName == "Opera" && navigator.appVersion.charAt(0) >= 4) || (navigator.appName == "Konqueror" && navigator.appVersion.charAt(0) >= 2)) {
          document.write("<img src='$imagesdir/smiley.gif' onclick='smiley();' "+HAND+" align=bottom alt='$post_txt{'287'}' title='$post_txt{'287'}' border='0'> ");
          document.write("<img src='$imagesdir/wink.gif' onclick='wink();' "+HAND+" align=bottom alt='$post_txt{'292'}' title='$post_txt{'292'}' border='0'> ");
          document.write("<img src='$imagesdir/cheesy.gif' onclick='cheesy();' "+HAND+" align=bottom alt='$post_txt{'289'}' title='$post_txt{'289'}' border='0'> ");
          document.write("<img src='$imagesdir/grin.gif' onclick='grin();' "+HAND+" align=bottom alt='$post_txt{'293'}' title='$post_txt{'293'}' border='0'> ");
          document.write("<img src='$imagesdir/angry.gif' onclick='angry();' "+HAND+" align=bottom alt='$post_txt{'288'}' title='$post_txt{'288'}' border='0'> ");
          document.write("<img src='$imagesdir/sad.gif' onclick='sad();' "+HAND+" align=bottom alt='$post_txt{'291'}' title='$post_txt{'291'}' border='0'> ");
          document.write("<img src='$imagesdir/shocked.gif' onclick='shocked();' "+HAND+" align=bottom alt='$post_txt{'294'}' title='$post_txt{'294'}' border='0'> ");
          document.write("<img src='$imagesdir/cool.gif' onclick='cool();' "+HAND+" align=bottom alt='$post_txt{'295'}' title='$post_txt{'295'}' border='0'> ");
          document.write("<img src='$imagesdir/huh.gif' onclick='huh();' "+HAND+" align=bottom alt='$post_txt{'296'}' title='$post_txt{'296'}' border='0'> ");
          document.write("<img src='$imagesdir/rolleyes.gif' onclick='rolleyes();' "+HAND+" align=bottom alt='$post_txt{'450'}' title='$post_txt{'450'}' border='0'> ");
          document.write("<img src='$imagesdir/tongue.gif' onclick='tongue();' "+HAND+" align=bottom alt='$post_txt{'451'}' title='$post_txt{'451'}' border='0'> ");
          document.write("<img src='$imagesdir/embarassed.gif' onclick='embarassed();' "+HAND+" align=bottom alt='$post_txt{'526'}' title='$post_txt{'526'}' border='0'> ");
          document.write("<img src='$imagesdir/lipsrsealed.gif' onclick='lipsrsealed();' "+HAND+" align=bottom alt='$post_txt{'527'}' title='$post_txt{'527'}' border='0'> ");
          document.write("<img src='$imagesdir/undecided.gif' onclick='undecided();' "+HAND+" align=bottom alt='$post_txt{'528'}' title='$post_txt{'528'}' border='0'> ");
          document.write("<img src='$imagesdir/kiss.gif' onclick='kiss();' "+HAND+" align=bottom alt='$post_txt{'529'}' title='$post_txt{'529'}' border='0'> ");
          document.write("<img src='$imagesdir/cry.gif' onclick='cry();' "+HAND+" align=bottom alt='$post_txt{'530'}' title='$post_txt{'530'}' border='0'> ");$moresmilieslist
        }
        else { document.write("<span  class='small'>$post_txt{'215'}</span>"); }
        //-->
        </script>
~;
if (($showadded == 3 && $showsmdir ne 2) || ($showsmdir eq 3 && $showadded ne 2)){
	$yymain .= qq~
<a href=javascript:smiliewin()>$post_smiltxt{'1'}</a>
~;
}
$yymain .= qq~

        <noscript>
        <span class="small">$post_txt{'215'}</span>
        </noscript>
        </td>
      </tr><tr>
        <td valign="top" class="windowbg2" width="23%"><b>$post_txt{'72'}:</b>
        <br /><br />
	~;
		if ($showadded eq 2 || $showsmdir eq 2){
	$yymain .= qq~
        <script language="JavaScript1.2" type="text/javascript">
        <!--
          function Smiliextra() {
            AddTxt=smiliecode[document.postmodify.smiliextra_list.value];
            AddText(AddTxt);
          }
	~;

	$smilieslist = "";
	$smilie_url_array = "";
	$smilie_code_array = "";
	$i=0;
	if ($showadded eq 2){
	while($SmilieURL[$i]) {
		$smilieslist .= qq~           document.write("<option value='$i'>$SmilieDescription[$i]");\n~;
		if ($SmilieURL[$i]=~ /\//i) {$tmpurl = $SmilieURL[$i];} else {$tmpurl = qq~$imagesdir/$SmilieURL[$i]~;}
		$smilie_url_array .= qq~"$tmpurl", ~;
		$tmpcode = $SmilieCode[$i];
		$tmpcode =~ s/\&quot;/"+'"'+"/g; # "
  		&FromHTML($tmpcode);
  		$tmpcode =~ s/&#36;/\$/g;
  		$tmpcode =~ s/&#64;/\@/g;
		$smilie_code_array .= qq~" $tmpcode", ~;
		$i++;
	}
	}
	if ($showsmdir eq 2){
	opendir(DIR, "$smiliesdir");
		@contents = readdir(DIR);
	closedir(DIR);
	foreach $line (sort {uc($a) cmp uc($b)} @contents){
		($name, $extension) = split (/\./, $line);
		if ($extension =~ /gif/i || $extension =~ /jpg/i || $extension =~ /jpeg/i || $extension =~ /png/i ){
			if ($line !~ /banner/i) {
				$smilieslist .= qq~           document.write("<option value='$i'>$name");\n~;
				$smilie_url_array .= qq~"$smiliesurl/$line", ~;
				$smilie_code_array .= qq~" [smiley=$line]", ~;
				$i++
			}
		}
	}
	}
	$smilie_url_array .= qq~""~;
	$smilie_code_array .= qq~""~;

	$yymain .= qq~

	smilieurl = new Array($smilie_url_array)
	smiliecode = new Array($smilie_code_array)

        if((navigator.appName == "Netscape" && navigator.appVersion.charAt(0) >= 4) || (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.charAt(0) >= 4) || (navigator.appName == "Opera" && navigator.appVersion.charAt(0) >= 4)) {

          document.write('<table class="bordercolor" height="120" width="120" border="0" cellpadding="2" callspacing="1" align="center"><tr>');
          document.write('<td height="15" align="center" valign="middle" class="titlebg"><span class="small"><b>$post_smiltxt{'1'}</b></span></td>');
          document.write('</tr><tr>');
          document.write('<td height="20" align="center" valign="top" class="windowbg2"><select name="smiliextra_list" onchange="document.images.smiliextra_image.src= smilieurl[document.postmodify.smiliextra_list.value]" style="width:114px; font-size:7pt;">');
$smilieslist
          document.write('</select></td>');
          document.write('</tr><tr>');
          document.write('<td height="70" align="center" valign="middle" class="windowbg2"><img name="smiliextra_image" src="'+smilieurl[0]+'" alt="" border="0" onclick="javascript:Smiliextra()" style="cursor:hand"></td>');
          document.write('</tr><tr>');
          document.write('<td height="15" align="center" valign="middle" class="windowbg2"><span class="small"><a href="javascript:smiliewin()">$post_smiltxt{'17'}</a></span></td>');
          document.write('</tr></table>');
	}
        //-->
        </script>
        ~;
}
# this is hte message area.
if(!${$uid.$username}{'postlayout'} || ${$uid.$username}{'postlayout'} >= 100){$pwidth = 90; } else {$pwidth = ${$uid.$username}{'postlayout'};}
$yymain .= qq~
        </td>
        <td class="windowbg2">
        <textarea name="message" id="message" rows="8" cols="60" style="width: $pwidth%; height: 166px; font-family: Verdana; font-size: 11px; padding: 5px; margin: 0px; visibility: visible;" onclick="javascript:storeCaret(this);" onkeyup="javascript:storeCaret(this);" onchange="javascript:storeCaret(this);" tabindex="4">$message</textarea>
        <br /></td>
      </tr>
      <tr>
        <td class="windowbg2"></td>
        <td class="windowbg2">
        <img src="$imagesdir/green1.gif" name="chrwarn" height="8" width="8" border="0" vspace="0" hspace="0" alt="" align="middle" />
        <span class="small">$npf_txt{'03'} <input value="$MaxMessLen" size="3" name="msgCL" class="windowbg2" style="border: 0px; font-size: 11px; width: 40px; padding: 1px" disabled="disabled" /></span>
        </td>
      </tr>
~;
# File Attachment's Browse Box Code
if ($allowattach == 1 && ${$uid.$currentboard}{'attperms'} == 1 && $fa_ok == 1 && ($action eq 'post' || $action eq 'post2' || $action eq 'modify' || $action eq 'modify2') && (($allowguestattach == 0 && !$iamguest) || $allowguestattach == 1)) {
$selnewatt = "";
if ($action eq 'modify' && $isatt) {
	$selnewatt = qq~ onchange="selectNewattach();"~;
}
$yymain .= qq~
<tr>
	<td valign="top"><b>$fatxt{'6'}</b></td>
	<td><input type="file" name="file" size="50"$selnewatt /></td>
</tr>
<tr>
	<td align="right"></td>
	<td><span class="small">$filetype_info<br>$filesize_info</span></td>
</tr>
~;
if ($action eq 'modify' && (-e "$uploaddir/$isatt")) {
	if($isatt ne ""){
		$oldattcheck = qq~selected="selected"~;
		$newattcheck = qq~~;
		$atistxt = qq~$fatxt{'40'}: <a href="$uploadurl/$isatt">$isatt</a>~;
	} else {
		$newattcheck = qq~selected="selected"~;
		$oldattcheck = qq~~;
		$atistxt = "";
	}
$yymain .= qq~
<tr>
	<td valign="top"></td>
	<td><font size="1">
	<select id="w_file" name="w_file" size="1">
	<option value="attachdel">$fatxt{'6c'}</option>
	<option value="attachnew" $newattcheck>$fatxt{'6b'}</option>
	<option value="attachold" $oldattcheck>$fatxt{'6a'}</option>
	</select>&nbsp;$atistxt</font></td>
</tr>~;
}
if( ($is_preview == 1) && $FORM{'file'} ) {
$yymain .= qq~
<tr>
	<td align="right"></td>
	<td><span class="small">$fatxt{'7'}</span></td>
</tr>
~;
}

}
# /File Attachment's Browse Box Code
$yymain .= qq~

$notification
      <tr>
        <td class="windowbg" width="23%"><b>$post_txt{'276'}:</b><br /><br /></td>
        <td class="windowbg"><input type="checkbox" name="ns" value="NS"$nscheck /> <span class="small"> $post_txt{'277'}</span><br /><br /></td>
      </tr>
$lastmod
~;
	}
#these are the buttons to submit
	$yymain .= qq~
      <tr>
        <td align="center" class="titlebg" colspan="2">
        <span class="small">$post_txt{'329'}</span><br />
		<input type="submit" name="$post" value="$submittxt" accesskey="s" tabindex="5" />
~;
unless ($pollthread == 2) {$yymain .= qq~		<input type="submit" name="$preview" value="$post_txt{'507'}" accesskey="p" />~;}
unless ($is_preview){$yymain .= qq~       <input type="reset" value="$post_txt{'278'}" accesskey="r" />~;}
$yymain .= qq~
        </td>
      </tr>
</table>
</div>
</form>
~;

unless ($pollthread == 2) {

require "$templatesdir/$usedisplay.Display.template";

$yymain.=qq~
<script language="JavaScript1.2" src="$html_root/yabbc.js" type="text/javascript"></script>
<script type="text/javascript" language="JavaScript"> <!--
var noalert = true, gralert = false, rdalert = false, clalert = false;
var prevsec = 5
var prevtxt

function tick() {
  calcCharLeft()
  timerID = setTimeout("tick()",1000)
}

var autoprev = false
var topicfirst = true;

function enabPrev() {
	if ( autoprev == false ) {
		autoprev = true
		topicfirst = true
		document.getElementById("SaveFrame").style.visibility = "visible";
		document.getElementById("SaveFrame").style.height = "180px";
		document.getElementById("SaveFrame").style.width = "100%";
		document.getElementById("SaveTopic").style.visibility = "visible";
		document.getElementById("SaveTopic").style.height = "25px";
		document.getElementById("SaveTopic").style.width = "100%";
		document.getElementById("SaveInfo").style.visibility = "visible";
		document.getElementById("SaveInfo").style.height = "205px";
		document.getElementById("SaveInfo").style.width = "100%";
		document.postmodify.message.focus();
		document.images.prevwin.alt = "$npf_txt{'02'}";
		document.images.prevwin.title = "$npf_txt{'02'}";
		document.images.prevwin.src="$imagesdir/cat_collapse.gif";
		autoPreview();
	}
	else {
		autoprev = false;
		ubbstr = '';
		document.getElementById("SaveFrame").style.visibility = "hidden";
		document.getElementById("SaveFrame").style.height = "0px";
		document.getElementById("SaveTopic").style.visibility = "hidden";
		document.getElementById("SaveTopic").style.height = "0px";
		document.getElementById("SaveInfo").style.visibility = "visible";
		document.getElementById("SaveInfo").style.height = "16px";
		document.postmodify.message.focus();
		document.images.prevwin.alt = "$npf_txt{'01'}";
		document.images.prevwin.title = "$npf_txt{'01'}";
		document.images.prevwin.src="$imagesdir/cat_expand.gif";
	}
	tick();
}

function calcCharLeft() {
  clipped = false
  maxLength = $MaxMessLen
  if (document.postmodify.message.value.length > maxLength) {
	document.postmodify.message.value = document.postmodify.message.value.substring(0,maxLength)
	charleft = 0
	clipped = true
  } else {
	charleft = maxLength - document.postmodify.message.value.length
  }
  prevsec++
  if(autoprev && prevsec > 5 && prevtxt != document.postmodify.message.value) {
	autoPreview()
	prevtxt = document.postmodify.message.value
  }
  document.postmodify.msgCL.value = charleft
  if (charleft >= 100 && noalert) { noalert = false; gralert = true; rdalert = true; clalert = true; document.images.chrwarn.src="$imagesdir/green1.gif"; }
  if (charleft < 100 && charleft >= 50 && gralert) { noalert = true; gralert = false; rdalert = true; clalert = true; document.images.chrwarn.src="$imagesdir/green0.gif"; }
  if (charleft < 50 && charleft > 0 && rdalert) { noalert = true; gralert = true; rdalert = false; clalert = true; document.images.chrwarn.src="$imagesdir/red0.gif" }
  if (charleft == 0 && clalert) { noalert = true; gralert = true; rdalert = true; clalert = false; document.images.chrwarn.src="$imagesdir/red1.gif"; }
  return clipped
}

var codestr = '$simpelcode';
var quotstr = '$normalquot';
var squotstr = '$simpelquot';
var fontsizemax = '$fontsizemax';
var fontsizemin = '$fontsizemin';
var edittxt = '$edittext';
var dispname = '${$uid.$username}{"realname"}';
var scrpurl = '$scripturl';
var imgdir = '$imagesdir';
var ubsmilieurl = '$smiliesurl';
var parseflash = '$parseflash';

function autoPreview() {
	if (topicfirst)  { updatTopic(); }
	var scrlto = parseInt(180) + 5;
	vismessage = document.postmodify.message.value;
	while ( c=vismessage.match(/date=(\\d+?)\\]/i) ) {
		var qudate=c[1];
		qudate=qudate * 1000;
		qdate=new Date()
		qdate.setTime(qudate);
		qdate=qdate.toLocaleString();
		vismessage=vismessage.replace(/(date=)\\d+?(\\])/i, "\$1"+qdate+"\$2");
	}
	var ubbstr = jsDoUbbc(vismessage,codestr,quotstr,squotstr,edittxt,dispname,scrpurl,imgdir,ubsmilieurl,parseflash,fontsizemax,fontsizemin);
	document.getElementById("SaveFrame").innerHTML=ubbstr;
	scrlto += parseInt(document.getElementById("SaveFrame").scrollTop) + parseInt(document.getElementById("SaveFrame").offsetHeight);
	document.getElementById("SaveFrame").scrollTop = scrlto;
	prevsec = 0
}

var visikon = '';

function updatTopic() {
	topicfirst = false;
	~;
	if($post ne 'imsend') {
	$yymain .= qq~
	visicon = document.postmodify.icon.value;
	visicon=visicon.replace(/[^A-Za-z]/g, "");
	visicon=visicon.replace(/\\\\/g, "");
	visicon=visicon.replace(/\\//g, "");
	if (visicon != "xx" && visicon != "thumbup" && visicon != "thumbdown" && visicon != "exclamation") {
		if (visicon != "question" && visicon != "lamp" && visicon != "smiley" && visicon != "angry") {
			if (visicon != "cheesy" && visicon != "laugh" && visicon != "sad" && visicon != "wink") {
				visicon = "xx";
			}
		}
	}
	visikon = "<img border='0' src='$imagesdir/"+visicon+".gif' alt='"+visicon+"' \/> ";
	~;
	}
	$yymain .= qq~
	vistopic = document.postmodify.subject.value;
	var htmltopic = jsDoTohtml(vistopic);
	document.getElementById("SaveTopic").innerHTML=visikon+htmltopic;
	document.postmodify.message.focus();
}

document.postmodify.$settofield.focus();
tick();
//--> </script>
~;
}
}

sub Preview {
	&clear_temp;
	my $error = $_[0];
	&ToHTML($e);
	# allows the following HTML-tags in error messages: <BR> <B>
	$error =~ s/&lt;br( \/)&gt;/<br \/>/ig;
	$error =~ s/&lt;(\/?)b&gt;/<$1b>/ig;
	$poll_question = $FORM{'question'};
	$numpolloptions ||= 8;
	for(my $i = 1; $i <= $numpolloptions; $i++) {$options[$i] = $FORM{"option$i"};}
	$guest_vote = $FORM{'guest_vote'};
	$hide_results = $FORM{'hide_results'};
	$multi_choice = $FORM{'multi_choice'};
	$poll_comment = $FORM{'poll_comment'};
	$pollthread =  $FORM{'pollthread'} || 0;

	$name = $FORM{'name'};
	$email = $FORM{'email'};
	$sub = $FORM{'subject'};
	$FORM{'message'} =~ s~\r~~g;
	$mess = $FORM{'message'};
	$message = $FORM{'message'};
 	$icon = $FORM{'icon'};
	$ns = $FORM{'ns'};
	$threadid = $FORM{'threadid'};
	$notify = $FORM{'notify'};
	$postid = $FORM{'postid'};
	$message =~ s/([\000-\x09\x0b\x0c\x0e-\x1f\x7f])/\x0d/g;

	if (!$sub) { $error = $post_txt{'77'}; }
	$sub =~ s/[\r\n]//g;
	my $testsub = $sub;
	$testsub =~ s/\&nbsp;//g;
	$testsub =~ s/ //g;

	$sub =~ s/&amp;/&/g;
	$sub =~ s/&quot;/"/g;
	$sub =~ s/&lt;/</g;
	$sub =~ s/&gt;/>/g;
	&FromChars($sub);
	$convertstr = $sub;
	$convertcut = 50;
	&CountChars;
	$sub = $convertstr;
	$sub =~ s/"/&quot;/g;
	$sub =~ s/</&lt;/g;
	$sub =~ s/>/&gt;/g;

	if ($testsub eq "" && $pollthread != 2){fatal_error($maintxt{'2'});}
	my $testmessage = substr($mess,0,120);;
	$testmessage =~ s/[\r\n\ ]//g;
	$testmessage =~ s/\&nbsp;//g;
	$testmessage =~ s~\[table\].*?\[tr\].*?\[td\]~~g;
	$testmessage =~ s~\[/td\].*?\[/tr\].*?\[/table\]~~g;
	$testmessage =~ s/\[.*?\]//g;
	if ($testmessage eq "" && $mess ne "" && $pollthread != 2){fatal_error("$maintxt{'2'} $testmessage");}
	$message =~ s/\cM//g;
	$message =~ s~\[([^\]]{0,30})\n([^\]]{0,30})\]~\[$1$2\]~g;
	$message =~ s~\[/([^\]]{0,30})\n([^\]]{0,30})\]~\[/$1$2\]~g;
	$message =~ s~(\w+://[^<>\s\n\"\]\[]+)\n([^<>\s\n\"\]\[]+)~$1$2~g;
	&FromChars($message);
	&ToHTML($message);
	$message =~ s/\t/ \&nbsp; \&nbsp; \&nbsp;/g;
	$message =~ s/\n/<br \/>/g;
	&CheckIcon;

	if($icon eq "xx") { $ic1 = " selected=\"selected\" "; }
	elsif($icon eq "thumbup") { $ic2 = " selected=\"selected\" "; }
	elsif($icon eq "thumbdown") { $ic3 = " selected=\"selected\" "; }
	elsif($icon eq "exclamation") { $ic4 = " selected=\"selected\" "; }
	elsif($icon eq "question") { $ic5 = " selected=\"selected\" "; }
	elsif($icon eq "lamp") { $ic6 = " selected=\"selected\" "; }
	elsif($icon eq "smiley") { $ic7 = " selected=\"selected\" "; }
	elsif($icon eq "angry") { $ic8 = " selected=\"selected\" "; }
	elsif($icon eq "cheesy") { $ic9 = " selected=\"selected\" "; }
	elsif($icon eq "laugh") { $ic10 = " selected=\"selected\" "; }
	elsif($icon eq "sad") { $ic11 = " selected=\"selected\" "; }
	elsif($icon eq "wink") { $ic12 = " selected=\"selected\" "; }

	$name_field = $realname eq '' ? qq~      <tr>
    <td class="windowbg" align="left" width="23%"><b>$post_txt{'68'}:</b></td>
    <td class="windowbg" align="left" width="77%"><input type="text" name="name" size="25" value="$FORM{'name'}" maxlength="25" tabindex="2" /></td>
      </tr>~ : qq~~;

	$email_field = $realemail eq '' ? qq~      <tr>
    <td class="windowbg" width="23%"><b>$post_txt{'69'}:</b></td>
    <td class="windowbg" width="77%"><input type="text" name="email" size="25" value="$FORM{'email'}" maxlength="40" tabindex="3" /></td>
      </tr>~ : qq~~;
	if ($FORM{'notify'} eq "x") {$notify = " checked";}
	if ($FORM{'ns'} eq 'NS') {$nscheck = " checked";}

	if ($iamguest) {
		$name .= "($post_txt{'772'})";
	}

	&wrap;
	$displayname = ${$uid.$username}{'realname'};
	if($enable_ubbc) { if(!$yyYaBBCloaded) { require "$sourcedir/YaBBC.pl"; } &DoUBBC; }
	&wrap2;
	if( $FORM{'previewmodify'} ) {
		$submittxt = "$post_txt{'10'}";
		$is_preview = 1;
		$post = "postmodify";
		$preview = "previewmodify";
		$destination = 'modify2';
		$submittxt = $post_txt{'10'};
	}
	elsif( $FORM{'previewim'} ) {
		$submittxt = "$post_txt{'148'}";
		$destination = "imsend2";
		$is_preview = 1;
		$post = "imsend";
		$preview = "previewim";
		$submittxt = $post_txt{'148'};
	}
	else {
		$notification = ! $enable_notification || $iamguest ? '' : <<"~;";
    <tr>
      <td class="windowbg"><b>$post_txt{'131'}:</b></td>
      <td class="windowbg"><input type="checkbox" name="notify" value="x"$notify /> <span  class="small">$post_txt{'750'}</span></td>
    </tr>
~;
		$destination = 'post2';
		$submittxt = $post_txt{'105'};
		$is_preview = 1;
		$post = "post";
		$preview = "preview";
	}

	if($INFO{'action'} eq "imgroups") { $destination = "imgroups"; }


	$csubject = $sub;
	&LoadCensorList;	# Load Censor List
	$csubject = &Censor($csubject);
	$message = &Censor($message);
	&ToChars($csubject);
	&ToChars($message);
	require "$templatesdir/$usedisplay.Display.template";

	$yymain .= qq~
<script language="JavaScript1.2" type="text/javascript" src="$ubbcjspath"></script>
<script language="JavaScript1.2" type="text/javascript">
<!--
function showimage() {
   document.images.icons.src="$imagesdir/"+document.postmodify.icon.options[document.postmodify.icon.selectedIndex].value+".gif";
}
//-->
</script>
<table border="0" width="100%" cellspacing="1" cellpadding="3" class="bordercolor" align="center" style="table-layout: fixed;">
  <tr>
    <td class="titlebg"><span class="text1">
    <img src="$imagesdir/$icon.gif" name="icons" border="0" alt="" /> $csubject</span></td>
  </tr><tr>
    <td class="windowbg"><span class="$messageclass"><br />$message<br /><br /></span></td>
  </tr>
</table>
<br />
~;
	$message = $mess;
	&FromChars($message);
	&ToHTML($message);
	if ($error) { $csubject = $error; }
	$yytitle = "$post_txt{'507'} - $csubject";
	$settofield="message";
	&Postpage;
	&template;
	exit;
}

sub Post2 {
	if($iamguest && $enable_guestposting == 0) {	&fatal_error($post_txt{'165'}); }
	my( $email, $ns, $notify, @memberlist, $a, $realname, $membername, $testname, @reserve, @reservecfg, $matchword, $matchcase, $matchuser, $matchname, $namecheck, $reserved, $reservecheck, $newthreadid, @messages, $mnum, $msub, $mname, $memail, $mdate, $musername, $micon, $mstate, $pageindex, $tempname );
	&BoardTotals("load",$currentboard);

	# If poster is a Guest then evaluate the legality of name and email
	if(!${$uid.$username}{'email'}) {
		$FORM{'name'} =~ s/\A\s+//;
		$FORM{'name'} =~ s/\s+\Z//;
		&Preview($post_txt{'75'}) unless ($FORM{'name'} ne '' && $FORM{'name'} ne '_' && $FORM{'name'} ne ' ');
		&Preview($post_txt{'568'}) if(length($FORM{'name'}) > 25);
		&Preview("$post_txt{'76'}") if($FORM{'email'} eq '');
		&Preview("$post_txt{'240'} $post_txt{'69'} $post_txt{'241'}") if($FORM{'email'} !~ /[\w\-\.\+]+\@[\w\-\.\+]+\.(\w{2,4}$)/);
		&Preview("$post_txt{'500'}") if(($FORM{'email'} =~ /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(\.$)/) || ($FORM{'email'} !~ /^.+@\[?(\w|[-.])+\.[a-zA-Z]{2,4}|[0-9]{1,4}\]?$/));
	}

	# Get the form values
	$name = $FORM{'name'};
	$email = $FORM{'email'};
	$subject = $FORM{'subject'};
	$message = $FORM{'message'};
 	$icon = $FORM{'icon'};
	$ns = $FORM{'ns'};
	$ann = $FORM{'ann'};
	$threadid = $FORM{'threadid'};
	$pollthread = $FORM{'pollthread'} || 0;
	if( $threadid =~ /\D/ ) { &fatal_error($post_txt{'337'}); }
	$notify = $FORM{'notify'};
	$message =~ s/([\000-\x09\x0b\x0c\x0e-\x1f\x7f])/\x0d/g;

	if($name && $email) {
		&ToHTML($name);
		$email =~ s/\|//g;
		&ToHTML($email);
		$tempname = $name;	# hold it temporarily so we can put the _'s back later
		$name =~ s/\_/ /g;
	}

	&Preview($post_txt{'75'}) unless($username || $name);
	&Preview($post_txt{'76'}) unless(${$uid.$username}{'email'} || $email);
	if ($pollthread != 2) {	# If user is NOT adding a Poll to an existing thread
	&Preview unless($subject && $subject !~ m~\A[\s_.,]+\Z~);
	&Preview($post_txt{'78'}) unless($message);
	if (length($message)>$MaxMessLen) { &Preview($post_txt{'536'} . " " . (length($message)-$MaxMessLen) . " " . $post_txt{'537'} ); }
	if( $FORM{'preview'} ) { &Preview; }
	&spam_protection;

	my $testsub = $subject;
	$testsub =~ s/[\r\n\ ]//g;
	$testsub =~ s/\&nbsp;//g;
	if ($testsub eq "" && $pollthread != 2){fatal_error("$maintxt{'2'} $testsub");}
	my $testmessage = substr($message,0,120);;
	$testmessage =~ s/[\r\n\ ]//g;
	$testmessage =~ s/\&nbsp;//g;
	$testmessage =~ s~\[table\].*?\[tr\].*?\[td\]~~g;
	$testmessage =~ s~\[/td\].*?\[/tr\].*?\[/table\]~~g;
	$testmessage =~ s/\[.*?\]//g;
	if ($testmessage eq "" && $message ne "" && $pollthread != 2){fatal_error("$maintxt{'2'} $testmessage");}

	$subject =~ s/&amp;/&/g;
	&FromChars($subject);
	$convertstr = $subject;
	$convertcut = 50;
	&CountChars;
	$subject = $convertstr;

	$subject =~ s/[\r\n]//g;
	$message =~ s/\cM//g;
	$message =~ s~\[([^\]]{0,30})\n([^\]]{0,30})\]~\[$1$2\]~g;
	$message =~ s~\[/([^\]]{0,30})\n([^\]]{0,30})\]~\[/$1$2\]~g;
	$message =~ s~(\w+://[^<>\s\n\"\]\[]+)\n([^<>\s\n\"\]\[]+)~$1\n$2~g;
	&FromChars($message);
	&ToHTML($message);
	$message =~ s~\t~ \&nbsp; \&nbsp; \&nbsp;~g;
	$message =~ s~\n~<br />~g;
	&CheckIcon;

	if(-e("$datadir/.txt")) { unlink("$datadir/.txt"); }

	} # end if

	if (!$iamguest) {
		# If not guest, get name and email.
		$name = ${$uid.$username}{'realname'};
		$email = ${$uid.$username}{'email'};
	} else {
		# If user is Guest, then make sure the chosen name
		# is not reserved or used by a member.
		fopen(FILE, "$memberdir/memberlist.txt") || &fatal_error("206 $post_txt{'106'}: $post_txt{'23'} $memberlist.txt", 1);
		@memberlist = <FILE>;
		fclose(FILE);
		&memparse(@memberlist);
		$name .= "(Guest)";
	}


## [Poll Mod] ##
	my @poll_data;
	if ($pollthread) {
		$maxpq ||= 60;
		$maxpo ||= 50;
		$maxpc ||= 0;
		$numpolloptions ||= 8;

		$numcount = 0;
		if ( length($FORM{"question"} ) > $maxpq) { &Preview("$post_polltxt{'40'} $post_polltxt{'34a'} $maxpq $post_polltxt{'34b'} $post_polltxt{'36'}"); }
		$FORM{"question"} =~ s/\&nbsp;/ /g;
		$testspaces = $FORM{"question"};
		$testspaces =~ s/[\r\n\ ]//g;
		$testspaces =~ s/\&nbsp;//g;
		$testspaces =~ s~\[table\].*?\[tr\].*?\[td\]~~g;
		$testspaces =~ s~\[/td\].*?\[/tr\].*?\[/table\]~~g;
		$testspaces =~ s/\[.*?\]//g;
		if (length($testspaces) == 0 && length($FORM{"question"}) > 0 ){ fatal_error("$maintxt{'2'} $testmessage");}
		&ToHTML($FORM{"question"});
		$guest_vote = $FORM{'guest_vote'} || 0;
		$hide_results = $FORM{'hide_results'} || 0;
		$multi_choice = $FORM{'multi_choice'} || 0;
		$poll_comment = $FORM{'poll_comment'} || "";
		if ( length($poll_comment ) > $maxpc) { &Preview("$post_polltxt{'57'} $post_polltxt{'34a'} $maxpc $post_polltxt{'34b'} $post_polltxt{'36'}"); }
		&ToHTML($poll_comment);
		$poll_comment =~ s~\n~<br />~g;
		$poll_comment =~ s~\r~~g;
		push @poll_data, qq~$FORM{"question"}|0|$username|$name|$email|$date|$guest_vote|$hide_results|$multi_choice|||$poll_comment\n~;

		for ($i = 1; $i <= $numpolloptions; $i++){
			if ($FORM{"option$i"}) {
				if (length($FORM{"option$i"}) > $maxpo) { &Preview("$post_polltxt{'7'} $i  $post_polltxt{'34a'} $maxpo $post_polltxt{'34b'} $post_polltxt{'36'}"); }
				$FORM{"option$i"} =~ s/\&nbsp;/ /g;
				$testspaces = $FORM{"option$i"};
				$testspaces =~ s/[\r\n\ ]//g;
				$testspaces =~ s/\&nbsp;//g;
				$testspaces =~ s~\[table\].*?\[tr\].*?\[td\]~~g;
				$testspaces =~ s~\[/td\].*?\[/tr\].*?\[/table\]~~g;
				$testspaces =~ s/\[.*?\]//g;
				if (length($testspaces) == 0 && length($FORM{"option$i"}) > 0 ){ fatal_error("$maintxt{'2'} $testmessage");}
				&ToHTML($FORM{"option$i"});
				$numcount++;
				push @poll_data, qq~0|$FORM{"option$i"}\n~;
			}
		}
		unless ($FORM{"question"}) { &Preview("$post_polltxt{'37'}"); }
		if ($numcount < 2) { &Preview("$post_polltxt{'38'}"); }
	}
## [/Poll Mod] ##

	## File Attachment Mod V3.0 ##
	
	if ($FORM{'file'} ne "") {
		$file = $FORM{'file'};
		$OS = $^O; # operating system name
		if($OS =~ /darwin/i) { $isUNIX = 1; }
		elsif($OS =~ /win/i) { $isWIN = 1; }
		else {$isUNIX = 1;}
		$mylimit = 1024 * $limit;
		$mydirlimit = 1024 * $dirlimit;
		$fixfile = $file;
		$fixfile =~ s/.+\\([^\\]+)$|.+\/([^\/]+)$/$1/;
		$fixfile =~ s/[\(\)\$#%+,\/:?"<>'\*\;|@^!]//g;	#edit in between [ ] to include characters you dont want to allow in filenames (dont put a . there or you wont be able to get any file extensions).
		$fixfile =~ s/ /_/g;			#replaces spaces in filenames with a "_" character.

		if($overwrite == 2 && (-e "$uploaddir/$fixfile")){&fatal_error("$fatxt{'8'}");}
		if(!$overwrite){ # if $overwite = 0 or false, rename file using the checkex sub
			$fixfile = check_existence($uploaddir,$fixfile);
		}
		if ($checkext == 0) { $match = 1; } else {
		foreach $ext (@ext){
			chomp ($ext);
				if (grep /$ext$/i,$fixfile){$match=1; last;}
			}
		}
		if ($match) {
			if ($allowattach == 1 && (($allowguestattach == 0 && $username ne 'Guest') || $allowguestattach == 1)) { 
				$upload_okay = 1;
			}
		} else {
			&write_error("$fatxt{'20'} @ext ($fixfile)"); 
		}
		if ($mydirlimit > 0) {
			&dirstats;
		}
		$filesize = $ENV{'CONTENT_LENGTH'} - $postsize;
		$filesizekb = int ($filesize/1024);
		if ($filesize > $mylimit && $mylimit != 0) {
			$filesizediff = $filesizekb - $limit;
			if ($filesizediff == 1) { $sizevar = "kilobyte"; } else { $sizevar = "kilobytes"; }
			&write_error("$fatxt{'21'} $filesizediff $sizevar $fatxt{'21b'}");
		} elsif ($filesize > $spaceleft && $mydirlimit != 0) {	
			$filesizediff = $filesizekb - $kbspaceleft;
			if ($filesizediff == 1) { $sizevar = "kilobyte"; } else { $sizevar = "kilobytes"; }
			&write_error("$fatxt{'22'} $filesizediff $sizevar $fatxt{'22b'}");
		}

		if ($upload_okay == 1) {
			# create a new file on the server using the formatted ( new instance ) filename
			if(fopen(NEWFILE, ">$uploaddir/$fixfile")){
				if($isWIN){binmode NEWFILE;} # if it's a WIN server, switch to binary mode
				# start reading users HD.
				while (<$filename>){ 
					# print to the new file on the server 
					print NEWFILE; 
				}
				# close the new file on the server and we're done
				fclose (NEWFILE);
			} else {
				# return the server's error message if the new file could not be created
				&fatal_error("$fatxt{'60'} $uploaddir");
			}
		}
					
		# check if file has actually been uploaded, by checking the file has a size
		if(-s "$uploaddir/$fixfile"){
			$upload_ok = 1;
		} else {
			# delete the file as it has no content
			unlink("$uploaddir/$fixfile");
			&fatal_error("$fatxt{'59'} $fixfile");
		}
		&clear_temp;
		## /File Attachment Mod V3.0 ##
	}

	# If no thread specified, this is a new thread.
	# Find a valid random ID for it.
	if($threadid eq '') {
		$newthreadid = &getnewid;
	}
	else { $newthreadid = ''; }
	$mreplies = 0;
	## set announcement flag according to status of current board ##
	if($newthreadid) {
		if($currentboard eq $annboard){$mstate .="a";}
		# This is a new thread. Save it.
		fopen(FILE, "+<$boardsdir/$currentboard.txt", 1) || &write_error("210 $post_txt{'106'}: $post_txt{'23'} $currentboard.txt", 1);
			seek FILE, 0, 0;
			my @buffer = <FILE>;
			truncate FILE, 0;
			seek FILE, 0, 0;
			print FILE qq~$newthreadid|$subject|$name|$email|$date|0|$username|$icon|$mstate\n~;
			print FILE @buffer;
		fclose(FILE);
		fopen(FILE, ">$datadir/$newthreadid.txt") || &write_error("$post_txt{'23'} $newthreadid.txt", 1);
		print FILE qq~$subject|$name|$email|$date|$username|$icon|0|$user_ip|$message|$ns|||$fixfile\n~;
		fclose(FILE);
		$mreplies = 0;
		if ($file) {
			fopen(AMP, ">>$vardir/attachments.txt") || &write_error("209 $txt{'106'}: $txt{'23'} $vardir/attachments.txt");
			print AMP qq~$newthreadid|$mreplies|$subject|$name|$currentboard|$filesizekb|$date|$fixfile\n~;
			fclose(AMP);
		}
		if ($pollthread) { # Save Poll data for new thread
			fopen (POLL, ">$datadir/$newthreadid.poll");
			print POLL @poll_data;
			fclose (POLL);
		}
		## write the ctb file for the new thread
		${$newthreadid}{'board'} = $currentboard;
		${$newthreadid}{'replies'} = 0;
		${$newthreadid}{'views'} = 0;
		${$newthreadid}{'lastposter'} = $iamguest ? qq~Guest-$name~ : $username;
		&MessageTotals("update",$newthreadid);
	} else {
		# This is an old thread. Save it.
		## first load the current ctb info on this existing thread.
		&MessageTotals("load",$threadid);
		if ($pollthread) { # Save new Poll data
			fopen (POLL, ">$datadir/$threadid.poll");
			print POLL @poll_data;
			fclose (POLL);
			$yySetLocation = qq~$scripturl?num=$threadid~;
			&redirectexit;
		} else { # or save new reply data
			($mnum, $msub, $mname, $memail, $mdate, $mreplies, $musername, $micon, $mstate) = split(/\|/,$yyThreadLine);
			$mreplies = ${$threadid}{'replies'};
			if( $mstate =~ /l/i ) { &fatal_error($post_txt{'90'}); }
			if($currentboard eq $annboard && $mstate !~ /a/i){$mstate .="a";} else {$mstate =~ s/a//ig;}
			$mreplies++;
			fopen(BOARDFILE, "+<$boardsdir/$currentboard.txt", 1) || &write_error("211 $post_txt{'106'}: $post_txt{'23'} $currentboard.txt", 1);
			seek BOARDFILE, 0, 0;
			my @buffer = <BOARDFILE>;
			truncate BOARDFILE, 0;
			for ($a = 0; $a < @buffer; $a++) {
				if ( $buffer[$a] =~ m~\A$mnum\|~o ) { $buffer[$a] = ""; last; }
			}
			seek BOARDFILE, 0, 0;
			print BOARDFILE qq~$mnum|$msub|$mname|$memail|$date|$mreplies|$musername|$micon|$mstate\n~;
			print BOARDFILE @buffer;
			fclose(BOARDFILE);
			fopen(THREADFILE, ">>$datadir/$threadid.txt") || &write_error("212 $post_txt{'106'}: $post_txt{'23'} $threadid.txt", 1);
				print THREADFILE qq~$subject|$name|$email|$date|$username|$icon|0|$user_ip|$message|$ns|||$fixfile\n~;
			fclose(THREADFILE);

			if ($fixfile) {
				fopen(AMP, ">>$vardir/attachments.txt") || &write_error("209 $txt{'106'}: $txt{'23'} $vardir/attachments.txt");
				print AMP qq~$mnum|$mreplies|$subject|$name|$currentboard|$filesizekb|$date|$fixfile\n~;
				fclose(AMP);
			}

		} # end poll else
		## update the ctb file for the existing thread with number of replies and lastposter
		${$threadid}{'board'} = $currentboard;
		${$threadid}{'replies'} = $mreplies ;
		${$threadid}{'lastposter'} = $iamguest ? qq~Guest-$name~ : $username;
		&MessageTotals("update",$threadid);
	} # end else

	if(!$iamguest) {
		# Increment post count and lastpost date for the member.
		#Check whether zeropost board
		if (!${$uid.$currentboard}{'zero'}){
			${$uid.$username}{'postcount'}++;
			&UserAccount($username,"update","lastpost");
		} else {
			&UserAccount($username,"update","lastpost");
		}
	}

	# The thread ID, regardless of whether it's a new thread or not.
	$thread = $newthreadid || $threadid;

	# Let's figure out what page number to show
	$start = 0;
	$pageindex = int($mreplies / $maxmessagedisplay);
	$start = $pageindex * $maxmessagedisplay;

	# Notify any members who have notification turned on for this thread.
	if(-e("$datadir/$thread.mail")) { &NotifyUsers; }

	# Mark thread as read for the member.
	&dumplog($currentboard,$date);
	# Mark Board unread for everyone else
	&MarkBoardMails2($currentboard);

	&doaddition;
	&writewritelog;

	if($notify) {
		$INFO{'thread'} = $thread;
		$INFO{'start'} = $start;
		require "$sourcedir/Notify.pl";
		&Notify2;
	} else {
		require "$sourcedir/Notify.pl";
		&NoNotify;
	}

	$yySetLocation = qq~$scripturl?num=$thread/$start#$mreplies~;

	# Update totals for this board.
	$start = $mreplies;
	&redirectexit;
}

sub NotifyUsers {
	my ($notifymail, $nottxt);
	$nottxt="notify_txt";
	$subject = $FORM{'subject'};
	## load up language dependend message text
	opendir(dir,$langdir);
	my @lfilesanddirs = readdir(dir);
	close(dir);
	
	foreach $fld (@lfilesanddirs){
		if(-e "$langdir/$fld/Notify.lng" && $fld =~ m^\A[0-9a-zA-Z_\#\%\-\:\+\?\$\&\~\,\@/]+\Z^){
			require "$langdir/$fld/Notify.lng";
			%{$fld.$nottxt} = %notify_txt;
		}
	}

    # Need to get an exclusive lock on the semaphore file
    # This is so we don't get multiple processes sending mails
    # Will not open the .mail file until lock has been gained.
    # Lock is not released until all mails are sent

	fopen(SEMAPHORE, ">$datadir/$thread.mail.lock") || return 0;
	fopen(NOTIFYCHECK, "$datadir/$thread.mail") || return 0;
	@mails = <NOTIFYCHECK>;
	fclose(NOTIFYCHECK);
	$page = int($mreplies/$maxmessagedisplay)*$maxmessagedisplay;
	# Security from mailbombings
	$i = 0;
	foreach $notifymail (@mails) {
		chomp $notifymail;
		($curuser,$curmail,$curlang,$isboardnotify,$hasviewed) = split (/\|/, $notifymail);
		if (&CheckBoardNotify) { next; }
		if ($curlang eq ""){ $curlang = $lang;}
		if ($curmail ne ${$uid.$username}{'email'} && $hasviewed) {
			&sendmail($curmail,"${$curlang.$nottxt}{'127'}\:  $subject","${$curlang.$nottxt}{'128'}, $subject, ${$curlang.$nottxt}{'129'} $scripturl?num=$thread\/$page\n\n${$curlang.$nottxt}{'131'}\n\n${$curlang.$nottxt}{'130'}");
		$hasviewed = "";
		$i++;
		if ($i > 20) { last; }
		}
	}
	fclose(SEMAPHORE);
    unlink("$datadir/$thread.mail.lock");
	return 1;
}

sub CheckBoardNotify {

	fopen(BDNOTIFY, "$boardsdir/$currentboard.mail");
		my @notifications = <BDNOTIFY>;
	fclose(BDNOTIFY);

	$isonlist = 0;

	foreach $line (@notifications) {
		$line =~ s/[\n\r]//g;
		($curuser,$curmail,$curlang,$notifytype,$hasviewed) = split (/\|/, $line,5);
		if($username eq $curuser && $notifytype == 2) { $isonlist = 1; last;}
	}

	if ($isonlist) {
		return 1;
	} else { return 0; }

}

sub doshowthread {
	my( $line, $trash, $tempname, $tempdate, $temppost );

	&LoadCensorList;	# Load Censor List

if ("$INFO{'start'}") { $INFO{'start'} = "/$INFO{'start'}"; }

	if (@messages) {
		if(@messages <= $cutamount){
			$cutamount = @messages
		}
		$yymain .= qq~
	<br /><br />
<table cellspacing="1" cellpadding="4" width="100%" align="center" class="bordercolor" style="table-layout: fixed;">
	<tr><td align="left" class="titlebg" colspan="2"><span class="text1">
~;
		$showall = qq~$post_cutts{'3'}~;

unless (@messages <= $cutamount) {
	$showall .= qq~ $post_cutts{'3a'} <a href="$scripturl?action=post;num=$threadid;title=Post+reply$INFO{'start'};showall=yes" style="text-decoration: underline;">$post_cutts{'4'}</a> $post_cutts{'5'} ~;
}

		if ($INFO{'showall'} ne '' || $cutamount eq "all"){
			$origcutamount = $cutamount;
			$cutamount = 'all';
			$showall = qq~$post_cutts{'3'} $post_cutts{'3a'} <a href="$scripturl?action=post;num=$threadid;title=Post+reply/$INFO{'start'}" style="text-decoration: underline;"> $post_cutts{'4'}</a> $post_cutts{'6'} ~;
		}
		$yymain .= qq~
<b>$post_txt{'468'} - $post_cutts{'2'} $cutamount $showall</b>
	</span></td></tr>~;
		if ($tsreverse == 1){
			@messages = reverse(@messages);
		}
		if ($INFO{'showall'} ne '' || $cutamount eq "all"){
			$cutamount = 1000;
		}
		for ($amounter; $amounter ne $cutamount; $amounter++){
			($trash, $temprname, $trash, $tempdate, $tempname, $trash, $trash, $trash, $message, $ns) = split(/\|/,$messages[$amounter]);
			$tempdate = &timeformat($tempdate);
			$parseflash = 0;
			$message = &Censor($message);
			my @words = split(/\s/,$message);
			&wrap;
			if ($tempname ne "Guest"){
				&LoadUser($tempname);
				$displaynamelink = qq~<a href="$scripturl?action=viewprofile;username=$tempname">${$uid.$tempname}{'realname'}</a>~;
			} else {
				$displaynamelink = "$temprname";
			}
			if($enable_ubbc) { if(!$yyYaBBCloaded) { require "$sourcedir/YaBBC.pl"; } &DoUBBC; }
			&ToChars($message);
			&wrap2;
			unless ($message eq ''){
				$yymain .= qq~

<tr><td align="left" class="catbg">
<span class="small">$post_txt{'279'}: $displaynamelink</span></td>
<td class="catbg" align="right">
<span class="small">$post_txt{'280'}: $tempdate</span></td>
</tr>
<tr><td class="windowbg2" colspan="2">
<div style="max-height: 150px; width: 100%; overflow: auto;">
<span class="small">$message</span>
</div>
</td></tr>~;
			}
		}
		$yymain .= "</table>\n</div>";
	} else { $yymain .= "<!--no summary-->"; }
}

sub doaddition {
	${$uid.$currentboard}{'messagecount'}++;
	unless( $FORM{'threadid'} ) {
		${$uid.$currentboard}{'threadcount'}++;
		++$threadcount;
	}
	$myname = $iamguest ? qq~Guest-$name~ : $username;
	${$uid.$currentboard}{'lastposttime'} = $date;
	${$uid.$currentboard}{'lastposter'} = $myname;
	${$uid.$currentboard}{'lastpostid'} = $thread;
	${$uid.$currentboard}{'lastreply'} = $mreplies;
	${$uid.$currentboard}{'lastsubject'} = $subject;
	${$uid.$currentboard}{'lasticon'} = $icon;

	&BoardTotals("update",$currentboard);
}

sub writewritelog { 
	if($iamguest) { return; } 
	unless($recentloaded){ &Recent_Load($username);}
	
	unless ( exists($recent{$thread}) ){ 
		$recent{$thread} = 0;
	}
	
	fopen(WLOG, ">$memberdir/$username.wlog",1);
	#print WLOG "$thread\n";
	print WLOG "\$recentloaded = 1\;\n";
	while(my($key, $val) = each(%recent)){
		$val++;
		print WLOG qq~\$recent{\'$key\'} \= $val;\n~;
	}
	print WLOG "\n1;";
	#foreach $curwrite (@writes) { print WLOG "$curwrite\n";      } 
	fclose(WLOG); 
} 

1;

