#!/usr/bin/perl
########## ########## ########## ########## ##########
#  form.cgi(inquiry)
#  MARS FLAG Corporation.
#  M.Nishida write
#  Update 2019.02.20
########## ########## ########## ########## ##########
use Encode;
require '../../lib/common.pl';

########## ########## ########## ########## ##########
# Define Data
########## ########## ########## ########## ##########
$FORM_TMPL     = '../../tmpl/inquiry_form.tmpl';
$CHECK_TMPL    = '../../tmpl/inquiry_check.tmpl';
$LOG_PATH      = '../../data/inquiry.txt';

@ITEM_SORT = ('last', 'first', 'company', 'post', 'phone', 'email', 'conf', 'text', 'agree');
%ITEMS = ('last'     => '氏名(姓)を入力してください。',
          'first'    => '氏名(名)を入力してください。',
          'company'  => '会社名を入力してください。',
          'post'     => '部署を入力してください。',
          'phone'    => '電話番号を入力してください',
          'email'    => 'E-mailを入力してください。',
          'email_ng' => 'E-mailを正しく入力してください。',
          'conf'     => 'E-mailを確認用と合わせてご確認ください。',
          'text'     => 'お問い合わせ内容を入力してください。',
          'agree'    => '個人情報の取り扱いにご同意願います。');
$ERROR_VIEW = ' style="background:#FFD5CC"';

########## ########## ########## ########## ##########
# Main Source
########## ########## ########## ########## ##########
%in = &ReceiveData();  # CGIデータ取得
%out = %in;
$kind = $in{'k'};  # なし:新規 / c:確認

if($kind eq 'c'){
  # 問い合わせ項目の有無チェック
  foreach $item (@ITEM_SORT){
    if($in{$item} eq ''){
      $out{'error'} .= $ITEMS{$item} . "<br />\n";
      $out{'err_' . $item} = $ERROR_VIEW;
    }elsif($item eq 'email' && $in{'email'} !~ /^([a-zA-Z0-9\.\-\/_]{1,})@([a-zA-Z0-9\.\-\/_]{1,})\.([a-zA-Z0-9\.\-\/_]{1,})$/){
      $out{'error'} .= $ITEMS{$item . '_ng'} . "<br />\n";
      $out{'err_email'} = $ERROR_VIEW;
    }elsif($item eq 'conf' && $in{'conf'} ne $in{'email'}){
      $out{'error'} .= $ITEMS{$item} . "<br />\n";
      $out{'err_email'} = $ERROR_VIEW;
      $out{'err_conf'}  = $ERROR_VIEW;
    }

    $out{$item} =~ s/"/”/g;
    $out{$item} =~ s/</＜/g;
    $out{$item} =~ s/>/＞/g;
    $out{$item} =~ s/&/＆/g;
  }

  if($out{'error'} eq ''){
    $out{'view_text'} = $out{'text'};
    $out{'view_text'} =~ s/\x0D\x0A/<br \/>/g;
    $out{'view_text'} =~ s/\x0D/<br \/>/g;
    $out{'view_text'} =~ s/\x0A/<br \/>/g;

    # ロギング
    ($sec, $min, $hour, $day, $month, $year) = localtime(time);
    $date = sprintf("%04d.%02d.%02d %02d:%02d:%02d", $year+1900, $month+1, $day, $hour, $min, $sec);
    $line = "$date\t$in{'last'}\t$in{'first'}\t$in{'company'}\t$in{'post'}\t$in{'phone'}\t$in{'email'}\t$out{'view_text'}\t";
    &WriteLog($LOG_PATH, $line);

    &PrintPage($CHECK_TMPL, %out);  # 確認ページを表示
  }else{
    &PrintPage($FORM_TMPL, %out);  # 入力ページを表示
  }
}else{
  &PrintPage($FORM_TMPL, %out);  # 入力ページを表示
}

exit;
