<?
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 "../lib/PHPMailer/class.phpmailer.php";

require "info_common.php";

function mkCooperation(&$document, &$script, &$init) {
	$document['main'] .= <<<EOS
            <table width="748" border="0" cellspacing="0" cellpadding="0">
              <tr> 
                <td>&nbsp;</td>
              </tr>
            </table>
            <table width="748" border="0" cellspacing="0" cellpadding="0">
              <tr> 
                <td width="412" height="55" background="/images/subtitlebox_pat.gif" style="padding-left:30px"><img src="/info/images/subtitle_05.gif" width="188" height="26"></td>
                <td width="336" background="/images/subtitlebox_pat.gif" ><table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr> 
                      <td height="30" align="right" style="padding-right:10px"><img src="/images/locationbar_title.gif" width="65" height="11" align="absmiddle"><font color="2F89C2">È¨ 
                        &gt; »ç¾÷Á¦ÈÞ¹®ÀÇ</font></td>
                    </tr>
                  </table></td>
              </tr>
            </table> 
            <br>
            
            <table width="670" border="0" cellspacing="0" cellpadding="0">
              <tr> 
                <td><img src="/footer/images/caption_03.gif" width="579" height="65"></td>
              </tr>
            </table>
            <br> 
            <table width="670" border="0" cellspacing="0" cellpadding="0">
              <tr> 
                <td height="2"></td>
              </tr>
            </table>
<form id="cpform" enctype="multipart/form-data" action="{$_SERVER['PHP_SELF']}" method="post" onsubmit="return checkform();">
            <table width="670" border="0" cellspacing="0" cellpadding="0">
              <tr> 
                <td height="22" background="/company/images/subjectbox_pat.gif">&nbsp;</td>
              </tr>
            </table>
            <table width="640" border="0" cellspacing="0" cellpadding="0">
              <tr> 
                <td width="90" style="padding: 5px;"><font color="1F8ABF"><img src="/images/ball_black.gif" width="3" height="11"> 
                  È¸»ç¸í</font></td>
                <td width="207" style="padding: 5px;"><input id="cpname" name="cpname" type="text" class="box01" style="width: 130px;"></td>
                <td width="80" style="padding: 5px;"><font color="1F8ABF"><img src="/images/ball_black.gif" width="3" height="11"> 
                  È¨ÆäÀÌÁö</font></td>
                <td width="223" style="padding: 5px;"><input id="cphome" name="cphome" type="text" class="box01" style="width: 190px;"></td>
              </tr>
              <tr> 
                <td height="1" colspan="4" background="/images/dot_01.gif"></td>
              </tr>
              <tr> 
                <td style="padding: 5px;"><font color="1F8ABF"><img src="/images/ball_black.gif" width="3" height="11"> 
                  ´ã´çÀÚ¸í</font></td>
                <td style="padding: 5px;"><input id="cename" name="cename" type="text" class="box01" style="width: 130px;"></td>
                <td style="padding: 5px;"><font color="1F8ABF"><img src="/images/ball_black.gif" width="3" height="11"> 
                  Á÷ÇÔ</font></td>
                <td style="padding: 5px;"><input id="cepos" name="cepos" type="text" class="box01" style="width: 70px;"></td>
              </tr>
              <tr> 
                <td height="1" colspan="4" background="/images/dot_01.gif"></td>
              </tr>
              <tr> 
                <td style="padding: 5px;"><font color="1F8ABF"><img src="/images/ball_black.gif" width="3" height="11"> 
                  ÀüÈ­¹øÈ£</font></td>
                <td colspan="3" style="padding: 5px;"> <input id="cptel" name="cptel" type="text" class="box01" style="width: 130px;"></td>
              </tr>
              <tr> 
                <td height="1" colspan="4" background="/images/dot_01.gif"></td>
              </tr>
<!--
              <tr> 
                <td style="padding: 5px;"><font color="1F8ABF"><img src="/images/ball_black.gif" width="3" height="11"> 
                  Á¦¾ÈºÐ¾ß</font></td>
                <td colspan="3" style="padding: 5px;"><select id="cptype" name="cptype">
                    <option value="">Á¦¾ÈÇÏ½Ç ºÐ¾ß¸¦ ¼±ÅÃÇÏ¼¼¿ä</option>
                  </select></td>
              </tr>
              <tr> 
                <td height="1" colspan="4" background="/images/dot_01.gif"></td>
              </tr>
-->
              <tr> 
                <td style="line-height:18px" style="padding: 5px;"><font color="1F8ABF"><img src="/images/ball_black.gif" width="3" height="11"> 
                  Á¦¾È°³¿ä</font></td>
                <td colspan="3" style="padding: 5px;"><textarea id="cptext" name="cptext" class="box02" style="width: 497px; height: 143px;"></textarea>
                  <br>
                  (ÀÌÇà »çÇ× ¹× ±â´ë È¿°ú¸¦ Áß½ÉÀ¸·Î °£·«È÷ Á¦¾È ³»¿ëÀ» ±â¼úÇØ ÁÖ½Ê½Ã¿À.) </td>
              </tr>
			   <tr> 
                <td height="1" colspan="4" background="/images/dot_01.gif"></td>
              </tr>
			   <tr> 
                <td style="padding: 5px;"><font color="1F8ABF"><img src="/images/ball_black.gif" width="3" height="11"> 
                  Á¦¾È¼­Ã·ºÎ</font></td>
                <td colspan="3" style="padding: 5px; line-height: 18px;"><input id="cpprop" name="cpprop" type="file" class="box01" />
                  <br>
                  (Á¦¾È¼­/¼Ò°³¼­ Ã·ºÎÆÄÀÏÀº ÃÖ´ë 10MBÀÔ´Ï´Ù.<br>
                  ÀÌ ¿ë·®À» ÃÊ°úÇÏ´Â ÆÄÀÏÀÇ °æ¿ì, Á¢¼öµÇÁö ¾Ê´Â Á¡ À¯ÀÇÇÏ½Ê½Ã¿À.) </td>
              </tr>
			   <tr> 
                <td height="1" colspan="4" background="/images/dot_01.gif"></td>
              </tr>
			   <tr> 
                <td style="padding: 5px;"><font color="1F8ABF"><img src="/images/ball_black.gif" width="3" height="11"> 
                  ¼Ò°³¼­Ã·ºÎ</font></td>
                <td colspan="3" style="padding: 5px;"><input id="cpintro" name="cpintro" type="file" class="box01" />
                  <br>
                </td>
              </tr>
            </table>
            <br>
            <table width="670" border="0" cellspacing="0" cellpadding="0">
              <tr> 
                <td height="1" bgcolor="3DA5DA"></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 align="center"><input type="image" src="/footer/images/b_propose.gif" width="115" height="20"> 
                </td>
              </tr>
            </table>
<input type="hidden" name="coopsubmit" value="1" />
</form>

EOS;

	$script .= <<<EOS
function checkform() {
	f = document.getElementById("cpform");

	if (!f.cpname.value) {
		alert("È¸»ç¸íÀ» ÀÔ·ÂÇØ ÁÖ¼¼¿ä.");
		f.cpname.focus();

		return false;
	}

	if (!f.cphome.value) {
		alert("È¨ÆäÀÌÁö¸¦ ÀÔ·ÂÇØ ÁÖ¼¼¿ä.");
		f.cphome.focus();

		return false;
	}

	if (!f.cename.value) {
		alert("´ã´çÀÚ¸íÀ» ÀÔ·ÂÇØ ÁÖ¼¼¿ä.");
		f.cename.focus();

		return false;
	}

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

		return false;
	}

	if (!f.cptel.value) {
		alert("ÀüÈ­¹øÈ£¸¦ ÀÔ·ÂÇØ ÁÖ¼¼¿ä.");
		f.cptel.focus();

		return false;
	}
/*
	if (!f.cptype.selectedIndex) {
		alert("Á¦¾ÈºÐ¾ß¸¦ ¼±ÅÃÇØ ÁÖ¼¼¿ä.");

		return false;
	}
*/
	if (!f.cptext.value) {
		alert("Á¦¾È°³¿ä¸¦ ÀÔ·ÂÇØ ÁÖ¼¼¿ä.");
		f.cptext.focus();

		return false;
	}

	return true;
}

EOS;
}

function sendCoopMail(
	$coid, $cpname, $cphome, $cename, $cepos, $cptel,
	$cptype, $cptext, $cpprop, $cpintro
) {
	global $ADMIN_MAIL, $HOME_URL;

	$thistime = date("Y-m-d H:i:s");

	if ($cphome) {
		$cpnstring = <<<EOS
<a href="$cphome" target="_blank">$cpname</a>
EOS;
	} else {
		$cpnstring = $cpname;
	}

	if ($cpprop) {
		$cppstring = <<<EOS
<a href="{$HOME_URL}info/cptransfer.php?coid=$coid&cocat=P">$cpprop</a>
EOS;
	} else {
		$cppstring = "¾øÀ½";
	}

	if ($cpintro) {
		$cpistring = <<<EOS
<a href="{$HOME_URL}info/cptransfer.php?coid=$coid&cocat=I">$cpintro</a>
EOS;
	} else {
		$cpistring = "¾øÀ½";
	}

	$cptext = nl2br($cptext);


	$mailer = new PHPMailer();

	$mailer->IsSMTP();
	$mailer->Host = "localhost";
	$mailer->CharSet = "euc-kr";
	$mailer->IsHTML(true);

	$mailer->From = $ADMIN_MAIL;
	$mailer->FromName = "ÇÁ½ÃÄÉ";

	$mailer->AddAddress($ADMIN_MAIL, "ÇÁ·ÎÀÚÀÌ³Ê");

	$mailer->Subject = "[ÇÁ½ÃÄÉ] »ç¾÷ Á¦ÈÞ ¹®ÀÇ";

	$mailer->Body = <<<EOS

<p>È¨ÆäÀÌÁö¸¦ ÅëÇØ ´ÙÀ½°ú °°ÀÌ »ç¾÷ Á¦¾ÈÀÌ µî·ÏµÇ¾ú½À´Ï´Ù.</p>

<table width="100%">
<tr valign="top">
<td width="15%">È¸»ç¸í</td>
<td widht="85%">$cpnstring</td>
</tr>
<tr valign="top">
<td>´ã´çÀÚ</td>
<td>$cename $cepos</td>
</tr>
<tr valign="top">
<td>ÀüÈ­¹øÈ£</td>
<td>$cptel</td>
</tr>
<tr valign="top">
<td>Á¦¾ÈºÐ¾ß</td>
<td>$cptype</td>
</tr>
<tr valign="top">
<td>Á¦¾È°³¿ä</td>
<td>
$cptext
</td>
</tr>
<tr valign="top">
<td>Á¦¾È¼­</td>
<td>$cppstring</td>
</tr>
<tr valign="top">
<td>¼Ò°³¼­</td>
<td>$cpistring</td>
</tr>
<tr valign="top">
<td>µî·ÏÀÏ½Ã</td>
<td>$thistime</td>
</tr>
</table>

EOS;

	return $mailer->Send();
}

## Start

if ($coopsubmit) {
	mysql_query("insert into coop
(cpname, cphome, cename, cepos, cptel, cptype, cptext) values
('$cpname', '$cphome', '$cename', '$cepos', '$cptel', '$cptype', '$cptext')");

	$coid = mysql_insert_id();

	// Ã·ºÎ: cpprop, cpintro

	if ($_FILES['cpprop']['size']) {
		$cpprop = str_replace("\\'", "", $_FILES['cpprop']['name']);

		mysql_query("update coop set
cpprop = '$cpprop', cpptype = '{$_FILES['cpprop']['type']}'
where coid = '$coid'");

		$coatpath = $BOARD_ATTACHPATH . "/CO_" . $coid . "_" . $cpprop;

		move_uploaded_file(
			$_FILES['cpprop']['tmp_name'], $coatpath
		);
	} else {
		$cpprop = "";
	}

	if ($_FILES['cpintro']['size']) {
		$cpintro = str_replace("\\'", "", $_FILES['cpintro']['name']);

		mysql_query("update coop set
cpintro = '$cpintro', cpitype = '{$_FILES['cpintro']['type']}'
where coid = '$coid'");

		$coatpath = $BOARD_ATTACHPATH . "/CO_" . $coid . "_" . $cpintro;

		move_uploaded_file(
			$_FILES['cpintro']['tmp_name'], $coatpath
		);
	} else {
		$cpintro = "";
	}

	sendCoopMail(
		$coid, $cpname, $cphome, $cename, $cepos, $cptel,
		$cptype, $cptext, $cpprop, $cpintro
	);

	header("Location: $PHP_SELF?cook=1");

	return;
}

mkCooperation($document, $script, $init);

if ($cook) {
	$init .= <<<EOS

	alert("»ç¾÷ Á¦¾ÈÀÌ µî·ÏµÇ¾ú½À´Ï´Ù.");

EOS;
}

require "info_template.php";
?>
