#!/usr/bin/perl

#お問い合わせメール送信処理
=commentout
	初期設定はinit.plで行います
=cut

use CGI::Carp qw(fatalsToBrowser);
use lib '/home/webmaster/public_html/lib';
use CGI qw(:cgi-lib :cgi);
use DBI;
use Jcode;
use AE;
use AETmpl;
use AEMail;


require '../lib/init.pl';


$zensu = jcode('０１２３４５６７８９')->euc;
$hansu = jcode('0123456789')->euc;

##----------------------------------------------------------------------------

#SetENV------------------------------------------------------------------
$sendmail = '/usr/sbin/sendmail';
$tmpl{'form'}	 = 'tmp_form.html';		#画面テンプレート
$tmpl{'err'}	 = 'tmp_err.html';		#エラー画面テンプレート
$tmpl{'cfm'}	 = 'tmp_cfm.html';		#確認画面
##----------------------------------------------------------------------------

#jcodeを避けるパラメータ名のリスト
$Files = 'filedata';

#パラメータの処理
foreach (param) {
	next unless $_;
	if (ref(param("$_")) eq 'ARRAY') {
		@{$_} = param("$_");
		if (!/^$Files$/o) {
			@{$_} = map {jcode($_)->sjis} @{$_};
		}
	} else {
		${$_} = /^$Files$/o ? param($_) : jcode(param($_))->sjis;
	}
}


# テンプレート振り分け
if ($phone{'furiwake'} eq 'mobile') {
	foreach(keys %tmpl) {
		$tmpl{"$_"} =~ s/\.html/_m.html/;
	}
}

if ($zip1 && $zip2) { $zip = "$zip1-$zip2"; }
if ($tel1 && $tel2 && $tel3) { $tel = "$tel1-$tel2-$tel3"; }






##---------------------------------------------------

if ($mode eq 'cfm'){
	#入力チェック
	&check;
} elsif ($mode eq 'mailsend') {
	#メール送信
	&mailsend;
}



if ($mode eq 'cfm'){
	print "Content-type: text/html\n\n";
	&confirm;
} elsif ($mode eq 'thx') {
	&error($tmpl{'err'}, 'ご利用ありがとうございました。', '送信完了');
} else {
	print "Content-type: text/html\n\n";
	&form;
}

exit;


##---------------------------------------------------







sub form {
	if (@er) {
		$msg = join "<br>\n", @er;
	}
	
	my $section = $/;
	undef $/;
	open(TEMP,"$tmpl{'form'}") or &error($tmpl{'err'}, 'templete error.');
	$html = <TEMP>;
	close(TEMP);
	$/ = $section;
	
	$ken_select = &popup(\@ken, \@ken, $ken || '滋賀県');
	
	$html =~ s/%(\w+)%/${$1}/g;
	$html =~ s/<!--#exec cmd="([^"]+)"-->/&getssi($1)/ge;
	print $html;
}







sub check {
	if(!$regname)				{ push(@er, "・お名前を記入して下さい。");}
	if(!$freename)				{ push(@er, "・ふりがなを記入して下さい。");}
	if(!$zip or !$ken or !$add)	{ push(@er, "・住所の全てを選択・記入して下さい。");}
	if(!$tel)					{ push(@er, "・お電話番号を記入して下さい。");}
	if(!$email)				{ push(@er, "・E-mailアドレスを記入して下さい。");}
	if(@er){
		$mode = '';
	} else {
		$mode = 'cfm';
	}
}














sub confirm {
	#確認画面表示
	$adddisp		 = &nl2br($add);
	$cmtdisp		 = &nl2br($cmt);

	my $section = $/;
	undef $/;
	open(TEMP,"$tmpl{'cfm'}") or &error($tmpl{'err'}, 'templete error.');
	$html = <TEMP>;
	close(TEMP);
	$/ = $section;
	
	
	$html =~ s/%(\w+)%/${$1}/g;
	$html =~ s/<!--#exec cmd="([^"]+)"-->/&getssi($1)/ge;
	print $html;
}









sub mailsend {
	$Mail{'subject'}	 = $subject;
	$Mail{'to'}		 = $mailto;
	$Mail{'from'}		 = $email;
	$add		 =~ s/\r\n|\r|\n/\n　　　　　　　　　 /g;
	$cmt		 =~ s/\r\n|\r|\n/\n　　　　　　　　　 /g;

$Mail{'body'} = <<"EOF";

生活工房PCBoot「お問い合わせ」ページより、
以下のお問い合わせが送信されました。

--------------------------------------------
ふりがな　　　　　 $freename
お名前　　　　　　 $regname
ご住所　　　　　　 〒$zip
　　　　　　　　　 $ken
　　　　　　　　　 $add
電話番号　　　　　 $tel
メール　　　　　　 $email
お問い合わせ内容　 $cmt
- - - - - - - - - - - - - - - - - - - -

--------------------------------------------

EOF
	&AEMail::send(\%Mail) or &error($tmpl{'err'}, $Mail{'error'});
	$mode = 'thx';
}
#Subroutine------------------------------------------------------------------
