<?
session_start();

$document = array();
$script = "";
$init = "";

require "../lib/getnpost.php";
require "../lib/config.php";
require "../lib/dbconnect.php";
require "../lib/auth.php";
require "../lib/common.php";

require "community_common.php";

function mkEdit(
	&$document, &$script, &$init, $bdid, $page, $msid, $sn, $ss, $sc, $st
) {
	global $PsycheAdmin;
	global $mscatarray;

	$ste = urlencode($st);

	$result = mysql_query("select
us.usname, ms.msname, ms.mstime, ms.msread, ms.mscat, ms.msdepth,
ms.mssubject, ms.mscontent, ms.msattaches
from messages as ms
left join users as us on ms.usid = us.usid
where ms.msid = '$msid'");

	list($usname, $msname, $mstime, $msread, $mscat, $msdepth,
	$mssubject, $mscontent, $msattaches) = mysql_fetch_row($result);

	if (!$usname) $usname = $msname;

	$mscontent = str_replace("<br />", "", $mscontent);

	if ($msdepth) {
		$mscatstring = <<<EOS
<input type="hidden" id="mscat" name="mscat" value="$mscat" />

EOS;

		$mscatscript = "";
	} else {
		$somsca = sizeof($mscatarray);

		$mscatstring = <<<EOS
<select id="mscat" name="mscat" style="width: 65px;">
<option value="" style="background-color: #f2f2f2;">Á¾·ù</option>

EOS;

		$mscatSelected = array();
		$mscatSelected[$mscat] = " selected=\"selected\"";

		for ($i = 0; $i < $somsca; $i++) {
			$mscatstring .= <<<EOS
<option value="{$mscatarray[$i][0]}"{$mscatSelected[$mscatarray[$i][0]]}>{$mscatarray[$i][1]}</option>

EOS;
		}

		if ($PsycheAdmin) {
			$mscatstring .= <<<EOS
<option value="N"{$mscatSelected['N']}>°øÁö</option>

EOS;
		}

		$mscatstring .= <<<EOS
</select>

EOS;

		$mscatscript = <<<EOS

	if (!f.mscat.selectedIndex) {
		alert("±Û Á¾·ù¸¦ ¼±ÅÃÇØ ÁÖ¼¼¿ä.");
		f.mscat.focus();

		return false;
	}

EOS;
	}

	$atidx = 0;

	if ($msattaches) {
		$result = mysql_query("select atid, atname from attaches
where msid = '$msid'");

		$attachstring .= "";

		while (list($ati, $atn) = mysql_fetch_row($result)) {
			$attachstring .= <<<EOS
$atn
[<input type="checkbox" id="msatdel{$atidx}" name="msatdel{$atidx}" value="$ati" />»èÁ¦]<br />

EOS;

			$atidx++;
		}
	}

	$msattachmax = $atidx + 1;

	$document['main'] .= <<<EOS
<form id="editform" enctype="multipart/form-data"
 action="{$_SERVER['PHP_SELF']}" method="post" onsubmit="return checkEdit();">
            <table width="670" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td>{$document['caption']}</td>
              </tr>
            </table>
            <br>
            <table width="670" border="0" cellspacing="0" cellpadding="0">
<tr>
                <td height="5" bgcolor="87AE49"></td>
              </tr>
              <tr>
                <td height="2"></td>
              </tr>
            </table>
            <table width="670" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td width="15"><img src="/community/images/boxleft.gif" width="15" height="35"></td>
                <td width="640" background="/community/images/boxpat.gif"><table width="640" border="0" cellspacing="0" cellpadding="0">
                    <tr> 
                      <td width="70" align="center"><img src="/community/images/t_subject.gif" width="22" height="11"></td>
                      <td width="1"><img src="/community/images/boxtitle_bar.gif" width="1" height="15"></td>
                      <td style="padding-left:15px">
$mscatstring
<input type="text" id="mssubject" name="mssubject" value="$mssubject" class="box01" style="width: 425px;"></td>
                    </tr>
                  </table></td>
                <td width="15"><img src="/community/images/boxright.gif" width="15" height="35"></td>
              </tr>
            </table>
           
            <table width="668" border="0" cellspacing="0" cellpadding="0">
              <tr> 
                <td width="668" height="30" align="center"> <table width="640" border="0" cellspacing="0" cellpadding="0">
<tr> 
                      <td width="70" align="center"><img src="/community/images/t_name.gif" width="32" height="11"></td>
                      <td width="1"><img src="/community/images/boxtitle_bar.gif" width="1" height="15"></td>
                      <td width="386" style="padding-left:15px">$usname</td>
                      <td width="183" align="right">($mstime, Hit: $msread)</td>
                    </tr>
                  </table>
                </td>
              </tr>
			  
            </table>
			<table width="670" border="0" cellspacing="0" cellpadding="0">
              <tr> 
                <td height="1" bgcolor="E5E3E3"></td>
              </tr>
           
            </table>
			<table width="670" border="0" cellspacing="0" cellpadding="0">
              <tr> 
                <td height="30" align="center" bgcolor="F7F6F6"><table width="640" border="0" cellspacing="0" cellpadding="0">
                    <tr> 
                      <td width="70" align="center"><img src="/community/images/t_file.gif" width="42" height="11"></td>
                      <td width="1"><img src="/community/images/boxtitle_bar.gif" width="1" height="15"></td>
                      <td style="padding-left:15px"> <table width="540" border="0" cellspacing="0" cellpadding="0">
<tr> 
                            <td width="409">
							<table width="100%" border="0" cellspacing="0" cellpadding="2">
<tr> 
                                  <td>$attachstring</td>
                                </tr>
                                <tr> 
                                  <td height="20">
<input type="file" id="msattach{$atidx}" name="msattach{$atidx}" class="box02" /><br />
<input type="hidden" id="msattachmax" name="msattachmax" value="$msattachmax" />
</td>
                                </tr>
                              </table></td>
                            <td width="131" align="right" valign="middle"><a href="javascript:addAttachField()"><img src="/images/file_add.gif" width="66" height="12" border="0"></a></td>
                          </tr>
                        </table></td>
                    </tr>
                  </table></td>
              </tr>
             
            </table>
			<table width="670" border="0" cellspacing="0" cellpadding="0">
              <tr> 
                <td height="1" bgcolor="E5E3E3"></td>
              </tr>
           
            </table>
            <table width="670" border="0" cellspacing="0" cellpadding="10">
              <tr> 
                <td width="85" height="250">&nbsp;</td>
                <td width="585" height="200" valign="top" style="line-height:18px"><textarea id="mscontent" name="mscontent" class="box02" style="width: 500px; height: 284px;">$mscontent</textarea>
                </td>
              </tr>
            </table>
            <table width="670" border="0" cellspacing="0" cellpadding="0">
              <tr> 
                <td height="1" bgcolor="9DBD6B"></td>
              </tr>
              <tr> 
                <td height="25" align="center" bgcolor="F7F6F6">&nbsp;</td>
              </tr>
            </table>
            <table width="670" border="0" cellspacing="0" cellpadding="5">
              <tr> 
                <td width="329"><a href="list.php?bdid=$bdid&page=$page&sn=$sn&ss=$ss&sc=$sc&st=$ste"><img src="/images/b_list.gif" width="52" height="20" border="0"></a></td>
                <td width="321" align="right"><input type="image" src="/images/b_ok.gif" width="52" height="20"> 
                </td>
              </tr>
            </table>
<input type="hidden" id="bdid" name="bdid" value="$bdid" />
<input type="hidden" id="page" name="page" value="$page" />
<input type="hidden" id="msid" name="msid" value="$msid" />
<input type="hidden" name="editsubmit" value="1" />
</form>

EOS;

	$script .= <<<EOS
function addAttachField() {
	elMsAtMax = document.getElementById("msattachmax");
	msatmax = parseInt(elMsAtMax.value);
	elMsAtMax.value = msatmax + 1;

	elFile = document.createElement("input");
	elFile.type = "file";
	elFile.id = "msattach" + msatmax;
	elFile.name = "msattach" + msatmax;
	elFile.className = "box02";
	elMsAtMax.parentNode.insertBefore(elFile, elMsAtMax);

	elBR = document.createElement("br");
	elMsAtMax.parentNode.insertBefore(elBR, elMsAtMax);
}

function checkEdit() {
	f = document.getElementById("editform");

	if (!f.mssubject.value) {
		alert("Á¦¸ñÀ» ÀÔ·ÂÇØ ÁÖ¼¼¿ä.");
		f.mssubject.focus();

		return false;
	}

	if (!f.mscontent.value) {
		alert("³»¿ëÀ» ÀÔ·ÂÇØ ÁÖ¼¼¿ä.");
		f.mscontent.focus();

		return false;
	}

	return true;
}

EOS;
}

## Start

mkBoardCommon($document, $bdid);

if (!$PSYCHEID) {
	header("Location: /");

	return;
}

if ($BDPRIVW == "A" && !$PsycheAdmin) {
	header("Location: /");

	return;
}

if ($editsubmit) {
	$result = mysql_query("select usid from messages
where msid = '$msid'");

	list($usid) = mysql_fetch_row($result);

	if ($usid != $PSYCHEID && !$PsycheAdmin) {
		header("Location: /");

		return;
	}

	if (!$PsycheAdmin) $mssubject = htmlspecialchars($mssubject);

	if (!$PsycheAdmin) {
		$mscontent = htmlspecialchars($mscontent);
	}
	$mscontent = nl2br($mscontent);

	mysql_query("update messages set
mssubject = '$mssubject', mscontent = '$mscontent', mscat = '$mscat'
where msid = '$msid'");

	$msattaches = 0;

	for ($i = 0; $i < $msattachmax; $i++) {
		$msatvar = "msattach" . $i;
		$msatvarD = "msatdel" . $i;

		if ($$msatvarD) {
			$result = mysql_query("select atid, atname
from attaches
where atid = '{$$msatvarD}'");

			list($ati, $atn) = mysql_fetch_row($result);

			$msatpath = $BOARD_ATTACHPATH . "/AT_".$ati."_".$atn;

			unlink($msatpath);

			mysql_query("delete from attaches
where atid = '{$$msatvarD}'");

			$msattaches--;
		} elseif ($_FILES[$msatvar]['size']) {
			$_FILES[$msatvar]['name']
			= str_replace("\\'", "", $_FILES[$msatvar]['name']);

			mysql_query("insert into attaches
(msid, atname, attype) values
('$msid', '{$_FILES[$msatvar]['name']}', '{$_FILES[$msatvar]['type']}')");

			$atid = mysql_insert_id();

			$msatpath = $BOARD_ATTACHPATH . "/AT_" . $atid . "_" .
				    $_FILES[$msatvar]['name'];

			move_uploaded_file(
				$_FILES[$msatvar]['tmp_name'], $msatpath
			);

			$msattaches++;
		}
	}

	if ($msattaches) {
		mysql_query("update messages set
msattaches = msattaches + ($msattaches)
where msid = '$msid'");
	}

	header("Location: read.php?bdid=$bdid&page=$page&msid=$msid");

	return;
}

mkEdit($document, $script, $init, $bdid, $page, $msid, $sn, $ss, $sc, $st);

require "community_template.php";
?>
