<?php
/**
 * お問い合わせ
 */
class KIND {
  const CHECK = 1;
  const SEND  = 2;
  const RET   = 3;
}
session_start();

include 'contact.ini';
include 'common.inc';


/**
 * 初期値設定
 */
$kind = NULL;
$txt_name        = "";
$rad_sex          = "";
$txt_sex          = "";
$txt_job          = "";
$txt_num          = "";
$rad_parking      = "";
$txt_parking      = "";
$txt_mail         = "";
$txt_remail       = "";



// $_REQUESTから値を変数にセット
foreach ($_REQUEST as $key => $value) {
  ${$key} = stripcslashes($value);
  //${$key} = mb_convert_encoding(stripcslashes($value), "EUC-JP","UTF-8");
}

// 項目情報配列
$content["name"] = array("name"=>"お名前", "val"=>$txt_name, "req"=>true);
$content["sex"] = array("name"=>"性別", "val"=>($kind == KIND::CHECK ? $rad_sex : $txt_sex), "req"=>true);
$content["job"] = array("name"=>"ご職業", "val"=>$txt_job, "req"=>true);
$content["num"] = array("name"=>"人数", "val"=>$txt_num, "req"=>true);
$content["parking"] = array("name"=>"駐車場", "val"=>($kind == KIND::CHECK ? $rad_parking : $txt_parking), "req"=>true);
$content["mail"] = array("name"=>"メールアドレス", "val"=>$txt_mail, "req"=>true);
$content["remail"] = array("name"=>"メールアドレス再入力", "val"=>$txt_remail, "req"=>true);


/**
 * 初期値設定
 */
if (strlen($kind) == 0) {
  $txt_name        = "";
  $rad_sex          = "";
  $txt_sex          = "";
  $txt_job          = "";
  $txt_mail         = "";
  $txt_remail       = "";
  $rad_parking      = "";
  $txt_parking      = "";
  $txt_num          = "";
}

/**
 * エラーチェック
 */
$err_message = "";
if ($kind == KIND::CHECK || $kind == KIND::SEND) {

  foreach ($content as $key => $koumoku) {
    if (!is_array($koumoku["val"]) && $koumoku["req"] && strlen($koumoku["val"]) == 0) {
      if ($key == "sex" || $key == "parking") {
        $err_message .= $koumoku["name"]."を選択してください。<br>";
      } else {
        $err_message .= $koumoku["name"]."を入力してください。<br>";
      }
    }
    if ($key == "mail" && strlen($koumoku["val"]) > 0) {
      if (!preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $koumoku["val"])) {
        $err_message .= $koumoku["name"]."は正しいメールアドレスを入力してください。<br>";
      }
    }
      if ($key == "remail" && strlen($koumoku["val"]) > 0 && $content["mail"]["val"] != $koumoku["val"]) {
      $err_message .= $koumoku["name"]."はメールアドレスと異なります。<br>";
    }
  }
  // エラー有
  if (strlen($err_message) > 0) {
    $kind = "";
  }
}

/**
 * メール送信
 */
$result = true;
$result_message = "";
if ($kind == KIND::SEND) {
  // メール送信
  //$result_message = sendMail($ADMISSION_TO, $ADMISSION_FROM, $ADMISSION_SUBJECT, $content);
  $result_to_admin = sendMailtoAdmin($ADMISSION_TO, $ADMISSION_FROM, $ADMISSION_SUBJECT, $content);
  $result_to_customer = sendMailtoCustomer($ADMISSION_CUST_FROM, $ADMISSION_CUST_SUBJECT, $content);

  $result_message = "自治体のあり方シンポジウム参加お申込み完了";
  if (!$result_to_admin || !$result_to_customer) {
    $result_message = "予期せぬエラー";
    $result = false;
  }
}

if ($kind == KIND::RET) {
  $kind = "";
}

// メインタイトル設定
$main_title = strlen($result_message) == 0 ? "自治体のあり方シンポジウム参加お申込み" : $result_message;


/**
 * カンマ追加
 * @param string $val
 * @param type $setval
 * @return string
 */
function addComma($val, &$setval) {
  if (strlen($setval) != 0) {
    $setval .= "、";
  }
  $setval .= $val;
}

?>
<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="UTF-8">
    <title><?= $main_title?> | 常磐大学・常磐短期大学</title>
    <meta name="description" content="常磐大学・常磐短期大学の総合情報をご覧いただけます。">
    <meta name="keywords" content="常磐大学,常磐短期大学,常磐大学大学院,Tokiwa University,茨城,水戸,心理,教育,人間,総合政策,キャリア,幼児,保育,看護,就職,地域連携,生涯学習,受験生サイト,TOKIWA ROAD,オープンキャンパス,キャリアサポート">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1, minimum-scale=1, maximum-scale=1">
    <meta name="format-detection" content="telephone=no">
    <link rel="shortcut icon" href="../../../../../common/img/favicon.ico" />
    <link rel="icon" type="image/vnd.microsoft.icon" href="../../../../../common/img/favicon.ico">
    <link rel="stylesheet" type="text/css" href="css/style.css" />
    <link rel="stylesheet" type="text/css" href="../../../../../common/css/style.css" />
    <link rel="stylesheet" type="text/css" href="../../../../../common/css/second_style.css" />
    <link rel="stylesheet" type="text/css" href="../../../../css/information.css" />
    <script type="text/javascript" src="../../../../../common/js/jquery-1.12.4.min.js"></script>
    <script type="text/javascript" src="../../../../../common/js/common.js"></script>
    <script type="text/javascript" src="../../../../../common/js/analyticstracking.js"></script>
  </head>
  <body>
    <div class="wrapper" id="wrap">
      <script type="text/javascript">writeHtml('../../../../../', '', 'global-navi');</script><!-- /.global-navi -->
      <div class="container">
        <main>
          <div class="contact-box">
            <div class="contactContent">
              <div class="title">
                <h1>人口減少社会における自治体のあり方シンポジウム　in常磐大学<br>参加申込フォーム</h1>
              </div>
              <?php if (strlen($result_message) == 0) : ?>
              <div class="main-sentence">
                <?php if (strlen($kind) == 0) : ?>
                <p>■ 必要事項をご記入のうえ、お申し込みください。<br>■ 個人情報の取り扱いには十分配慮し、公開研修会等開催以外の目的に使用することはありません。</p>
                <?php else : ?>
                <p>この内容でよろしければ送信ボタンを押してください。</p>
                <?php endif; ?>
              </div>
              <form method="post" id="contact">
                <?php if (strlen($err_message) > 0) : ?>
                <div id="err_message"><?=$err_message?></div>
                <?php endif; ?>
                <div class="inner">
                  <table>
                    <tr>
                      <th>お名前<br class="br"><span>(必須)</span></th>
                      <td><?php if ($kind == KIND::CHECK) print htmlspecialchars($txt_name); ?><input type="<?= ($kind == KIND::CHECK) ? "hidden" : "text"; ?>" name="txt_name" id="txt_name" value="<?= htmlspecialchars($txt_name)?>"></td>
                    </tr>
                    <tr>
                      <th>性別<br class="br"><span>(必須)</span></th>
                      <td><?php  $txt_sex = "";
                                  switch ($rad_sex) {
                                    case "1": $txt_sex = "男性"; break;
                                    case "2": $txt_sex = "女性"; break;
                                  }
                                  if ($kind == KIND::CHECK) : print htmlspecialchars($txt_sex); ?>
                          <input type="hidden"  name="rad_sex" id="rad_sex" value="<?= htmlspecialchars($rad_sex)?>" >
                          <?php else : ?>
                          <label><input type="radio" name="rad_sex" id="rad_sex" value="1" <?php if ($rad_sex == "1") print 'checked';?>>男</label>
                          <label><input type="radio" name="rad_sex" id="rad_sex" value="2" <?php if ($rad_sex == "2") print 'checked';?>>女</label>
                          <?php endif; ?>
                          <input type="hidden"  name="txt_sex" id="txt_sex" value="<?= htmlspecialchars($txt_sex)?>" >
                      </td>
                    </tr>
                    <tr>
                      <th>参加人数<br class="br"><span>(必須)</span></th>
                      <td><?php if ($kind == KIND::CHECK) print htmlspecialchars($txt_num); ?><input type="<?= ($kind == KIND::CHECK) ? "hidden" : "text"; ?>" name="txt_num" id="txt_num" value="<?= htmlspecialchars($txt_num)?>"></td>
                    </tr>
                    <tr>
                      <th>駐車場のご利用<br class="br"><span>(必須)</span></th>
                      <td><?php  $txt_parking = "";
                                  switch ($rad_parking) {
                                    case "1": $txt_parking = "利用する"; break;
                                    case "2": $txt_parking = "利用しない"; break;
                                  }
                                  if ($kind == KIND::CHECK) : print htmlspecialchars($txt_parking); ?>
                          <input type="hidden"  name="rad_parking" id="rad_parking" value="<?= htmlspecialchars($rad_parking)?>" >
                          <?php else : ?>
                          <label><input type="radio" name="rad_parking" id="rad_parking" value="1" <?php if ($rad_parking == "1") print 'checked';?>>利用する</label>
                          <label><input type="radio" name="rad_parking" id="rad_parking" value="2" <?php if ($rad_parking == "2") print 'checked';?>>利用しない</label>
                          <?php endif; ?>
                          <input type="hidden"  name="txt_parking" id="txt_parking" value="<?= htmlspecialchars($txt_parking)?>" >
                      </td>
                    </tr>
                    <tr>
                      <th>ご職業<br class="br"><span>(必須)</span></th>
                      <td><?php if ($kind == KIND::CHECK) print htmlspecialchars($txt_job); ?><input type="<?= ($kind == KIND::CHECK) ? "hidden" : "text"; ?>" name="txt_job" id="txt_job" value="<?= htmlspecialchars($txt_job)?>"></td>
                    </tr>
                    <tr>
                      <th>メールアドレス<br class="br"><span>(必須)</span></th>
                      <td><?php if ($kind == KIND::CHECK) print htmlspecialchars($txt_mail); ?><input type="<?= ($kind == KIND::CHECK) ? "hidden" : "text"; ?>" name="txt_mail" id="txt_mail" value="<?= htmlspecialchars($txt_mail)?>"></td>
                    </tr>
                    <tr>
                      <th>メールアドレス<br class="br">再入力<br class="br"><span>(必須)</span></th>
                      <td><?php if ($kind == KIND::CHECK) print htmlspecialchars($txt_remail); ?><input type="<?= ($kind == KIND::CHECK) ? "hidden" : "text"; ?>" name="txt_remail" id="txt_remail" value="<?= htmlspecialchars($txt_remail)?>"></td>
                    </tr>
                  </table>

                  <input type="hidden" name="kind" value="<?=$kind?>">
                </div>
                <div class="annotation">※上記の情報は常磐大学・地方自治力向上プロジェクト・吉田ゼミナールへ送信されます。</div>
                <div id="btns">
                  <?php if (strlen($kind) == 0) : ?>
                  <button type="button" id="btn_check">確認画面へ</button>
                  <?php elseif ($kind == KIND::CHECK) : ?>
                  <button type="button" id="btn_send">送　信</button>
                  <button type="button" id="btn_return">戻　る</button>
                  <?php endif; ?>
                </div>
              </form>
              <?php else : ?>
                <div class="result-sentence">
                  <?php if ($result) : ?>
                  <p>お申込みを送信しました。<br>ありがとうございました。</p>
                  <?php else : ?>
                  <p>予期せぬエラーが発生したため、お申込みが完了しませんでした。</p>
                  <?php endif;?>
                </div>
                <div id="btns">
                  <!--button type="button" id="btn_close">閉じる</button-->
                </div>
              <?php endif; ?>
            </div><!-- /.contactContent -->
          </div><!-- /.contact-box -->

          <section class="area-inner second-inner">
            <div class="table-responsive">
              <table class="type-base">
                <caption class="cap-title">お問い合わせ先</caption>
                <tbody>
                  <tr>
                    <th>住所</th>
                    <td>〒310-8585 水戸市見和1-430-1<br>常磐大学学事センター</td>
                  </tr>
                  <tr>
                    <th>TEL</th>
                    <td>029-232-2524</td>
                  </tr>
                </tbody>
              </table>
            </div>
            <div class="space30"></div>
          </section>

        </main>
        <script type="text/javascript">writeHtml('../../../../../', '', 'footer');</script><!-- /.footer -->
      </div><!-- /.container -->
    </div><!-- /.wrapper -->
<script type="text/javascript">
$(function () {
  /**
   * 確認・送信
   */
  $("#btn_check, #btn_send, #btn_return, #btn_close").click(function () {
    var str = $(this).attr("id");
    var kind = null;

    switch(str) {
      case "btn_check": kind = <?= KIND::CHECK ?>; break;
      case "btn_send": kind = <?= KIND::SEND ?>; break;
      case "btn_return": kind = <?= KIND::RET ?>; break;
      case "btn_close":
            window.close(); return false;
            break;
    }

    // 実行
    $('[name=kind]').val(kind);
    $('#contact').submit();

  });
});
</script>
  </body>
</html>