#!/usr/bin/perl

# 会員管理

use DBI;
use Time::Local;

require './jcode.pl';
require './subfunc.cgi';
require './settings.cgi';

#==================================================================
#	設定
#==================================================================
$html_file = "RegistrationCustomer.html";		# 通常表示用テンプレートＨＴＭＬ
$check_file = "CheckCustomer.html";				# 確認表示用テンプレートＨＴＭＬ
$inputerr_file = "RegistCustomerError.html";	# 入力エラー表示用テンプレートＨＴＭＬ

#==================================================================
#	cookieの取得
#==================================================================
$cookies = $ENV{'HTTP_COOKIE'};
@pairs = split(/ /, $cookies);
foreach $pair (@pairs) {
	($name, $value) = split(/=/, $pair);
	$value =~ s/\;//g;
	$cc{$name} = $value;
}

#==================================================================
#	引数を解析する
#==================================================================
if ($ENV{'REQUEST_METHOD'} eq "POST") {
	read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
} else {
	$buffer = $ENV{'QUERY_STRING'};
}
@pairs = split(/&/,$buffer);
foreach $pair (@pairs)
{
	($name, $value) = split(/=/, $pair);
	$value =~ tr/+/ /;
	$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	$value =~ s/\n//g;
	&jcode'h2z_sjis( *value );
	&jcode'convert(*value,'sjis');
	$PARA{$name} = $value;
}

#==================================================================
#	引数により処理を分岐する
#==================================================================
if( $PARA{mode} eq "" ) {
	if( $cc{LoginID} ne "" ) {
		print "Content-type: text/html\n\n";
		print "<html><head></head><body onLoad=location.href='CustomerKakunin.html';></body></html>\n";
		exit;
	} else {
		$PARA{mode} = "edit";
	}
}
if( $PARA{mode} eq "write" ) {
	&write_data;				# データベースへの書き込み
	# 登録完了の表示
	print "Content-type: text/html\n\n";
	print "<html><head>\n";
	print "<META http-equiv='refresh' content='2; url=../'>\n";
	print "</head><body><center><img src=../image/title/t_kaiin_shinki_end.gif>\n";
	print "<p>■会員登録が完了しました。</p><a href=../><img src=../image/button/b_kaimono.gif border=0>\n";
	print "</center></body></html>\n";
	exit;
}
if( $PARA{mode} eq "taikai" ) {
	# cookieよりログイン名を取得
	if( $cc{LoginID} eq "" ) {
		&disp_html( "RegistrationCustomer" );
		exit;
	}
	$PARA{LoginName} = $cc{LoginID};
	&delete_data;				# データベースからの削除
	# 退会完了の表示
	print "Content-type: text/html\n\n";
	print "<html><head></head><body onLoad=\"parent.leftFrame.location.href='login.cgi?mode=out';\"><center><img src=../image/title/t_taikai_kanryou.gif>\n";
	print "<p>■退会手続きが完了しました。<p><a href=../><img src=../image/button/b_kaimono.gif border=0>\n";
	print "</center></body></html>\n";
	exit;
}
if( $PARA{mode} eq "edit" ) {
	# cookieよりログイン名を取得
	if( $cc{LoginID} ne "" ) {
		$PARA{LoginName} = $cc{LoginID};
	}
	print "Content-type: text/html\n\n";
	&view_html;					# 新規入力または既存データの表示
	exit;
}
if( $PARA{mode} eq "check" ) {
	print "Content-type: text/html\n\n";
	&err_check;					# 入力漏れ、確認入力の不一致のチェック
	&check_html;				# 入力確認画面の表示
	exit;
}
# 戻り時の再表示
print "Content-type: text/html\n\n";
&reload_html;
exit;

#==================================================================
#	データ入力用ＨＴＭＬ作成
#==================================================================
sub view_html {

	open(HTML,"$html_file") || die "error!\n";
	flock (HTML2, 2);
	@HTML = <HTML>;
	flock (HTML2, 8);
	close(HTML);

	open(HTML2,"../head.html") || die "error!\n";
	flock (HTML2, 2);
	@HEAD_HTML = <HTML2>;
	flock (HTML2, 8);
	close(HTML2);

	if( $PARA{LoginName} ne "" ) {
		# 登録データの読み出し
		$dbh = DBI->connect($data_source,$user_name,$user_pass) || die "CONNECT ERROR $DBI::ERRSTR";
		$sql = "SELECT * FROM customer WHERE Id=?;";
		$sth = $dbh->prepare($sql);
		$sth->execute( "$PARA{LoginName}" ) || die $sth->errstr;
		@value = $sth->fetchrow_array;
		$sth->finish;
		$dbh->disconnect;
	}

	$flg = 0;
	$title = "";
	foreach $line (@HTML) {
		if( index( $line, '#INCLUDE_HEAD#' ) >= 0 ) {
			foreach $line2 (@HEAD_HTML) {
				print $line2;
			}
			$line = "<!-- INCLUDE OK -->\n";
		}
		if( $PARA{LoginName} ne "" ) {
			# 登録データの表示
			$line =~ s/(#)(\d\d)(#)/$value[$2]/;	# 単純なデータの表示

			# タイトル
			$title = "会員変更";
			$line =~ s/(#)(TITLE)(#)/$title/;

			# ログイン名の変更の禁止
			if( index( $line, "LoginName" ) >= 0 ) {
				$line =~ s/>/ readonly>/;
			}

			# ラジオボタンの表示
			if( index( $line, "Sex" ) >= 0 ) {
				$line =~ s/(")($value[13])(")/"$value[13]" checked/;	# 単純なデータの表示
			}

			# オプション選択の初期値表示
			if( index( $line, "Prefecture" ) >= 0 ) { $flg = 3; }	# 都道府県
			if( index( $line, "/select" ) >= 0 ) { $flg = 0; }
			if( $flg != 0 ) {
				$name = "value=\"$value[$flg]\"";
				if( index( $line, "$name" ) >= 0 ) {
					$line =~ s/>/ selected>/;
				}
			}

			# 特殊表示
			if( index( $line, "Zip" ) >= 0 ) {
				# 郵便番号（111-1111のように分解）
				$zip1 = $value[2] / 10000;
				$zip2 = $value[2] % 10000;
				$buf = sprintf( "%03d", $zip1 );
				$line =~ s/(#)(ab)/$buf/;
				$buf = sprintf( "%04d", $zip2 );
				$line =~ s/(#)(cd)/$buf/;
			}
			if( index( $line, "Year" ) >= 0 ) {
				# 生年月日（YYYY年MM月DD日のように分解）
				$year = $value[14] / 10000;
				$month = $value[14] / 100 % 100;
				$day = $value[14] % 100;
				$buf = sprintf( "%04d", $year );
				$line =~ s/(#)(ab)/$buf/;
				$buf = sprintf( "%02d", $month );
				$line =~ s/(#)(cd)/$buf/;
				$buf = sprintf( "%02d", $day );
				$line =~ s/(#)(ef)/$buf/;
			}
			$line =~ s/#MESSAGE#//g;
		} else {
			# タイトル
			$title = "会員登録";
			$line =~ s/(#)(TITLE)(#)/$title/;
			# 未入力状態の表示
			$msg = "<br>既に会員登録をされている方で、登録内容を修正される場合は先にログインを行ってください。<br>";
			$msg = $msg . "<font color=red>会員登録をされますと、「お名前」、「ご住所」等のお客様情報を毎回ご入力いただく手間が省け、とても便利です。<br>";
			$msg = $msg . "また、会員になられますと「※ギフト機能\」がご利用頂けます。</font><br>";
			$msg = $msg . "※ギフト機能\・・・お届先登録をしておく事で「カートを見る」画面にて商品ごとにお届先を指定し配送できる機能\";
			$line =~ s/#MESSAGE#/$msg/g;
			$line =~ s/(#)(\d\d)(#)//g;
			$line =~ s/(#)(ab)//g;
			$line =~ s/(#)(cd)//g;
			$line =~ s/(#)(ef)//g;
		}
		print $line;
	}
}

#==================================================================
#	入力確認用ＨＴＭＬ作成
#==================================================================
sub check_html {

	$melma = "希望しない";
	if( $PARA{melma} == 1 ) { $melma = "希望する"; }

	open(HTML,"$check_file") || die "error!\n";
	flock (HTML, 2);
	@HTML = <HTML>;
	flock (HTML, 8);
	close(HTML);

	open(HTML2,"../head.html") || die "error!\n";
	flock (HTML2, 2);
	@HEAD_HTML = <HTML2>;
	flock (HTML2, 8);
	close(HTML2);

	$flg = 0;
	foreach $line (@HTML) {
		if( index( $line, '#INCLUDE_HEAD#' ) >= 0 ) {
			foreach $line2 (@HEAD_HTML) {
				print $line2;
			}
			$line = "<!-- INCLUDE OK -->\n";
		}
		# タイトル
		$line =~ s/(#)(TITLE)(#)/$PARA{Title}/;
		# ログイン名
		$line =~ s/(#)(LOGINNAME)(#)/$PARA{LoginName}/;
		# パスワード
		$line =~ s/(#)(PASSWORD)(#)/$PARA{Password}/;
		# メールアドレス
		$line =~ s/(#)(MAILADDRESS)(#)/$PARA{EMailAddress}/;
		# 氏名
		$line =~ s/(#)(NAME)(#)/$PARA{Name}/;
		# フリガナ
		$line =~ s/(#)(RUBY)(#)/$PARA{Ruby}/;
		# 性別
		$line =~ s/(#)(SEX)(#)/$PARA{Sex}/;
		# 生年月日
		$line =~ s/(#)(YEAR)(#)/$PARA{Year}/;
		$line =~ s/(#)(MONTH)(#)/$PARA{Month}/;
		$line =~ s/(#)(DAY)(#)/$PARA{Day}/;
		# 自宅〒
		$line =~ s/(#)(ZIP1)(#)/$PARA{Zip1}/;
		$line =~ s/(#)(ZIP2)(#)/$PARA{Zip2}/;
		# 住所
		$line =~ s/(#)(PREFECTURE)(#)/$PARA{Prefecture}/;
		$line =~ s/(#)(ADDRESS1)(#)/$PARA{Address1}/;
		$line =~ s/(#)(ADDRESS2)(#)/$PARA{Address2}/;
		# 電話番号
		$line =~ s/(#)(TEL)(#)/$PARA{Tel}/;
		# ＦＡＸ番号
		$line =~ s/(#)(FAX)(#)/$PARA{Fax}/;
		# メルマガ配信
		$line =~ s/(#)(MELMA)(#)/$melma/;
		$line =~ s/(#)(MELMAGA)(#)/$PARA{melma}/;
		# 最終注文日
		$line =~ s/(#)(LASTORDERDATE)(#)/$PARA{LastOrderDate}/;
		# 累計購入金額
		$line =~ s/(#)(TOTAL)(#)/$PARA{TotalAmountOfMoney}/;

		print $line;
	}
}

#==================================================================
#	戻り時の再表示
#==================================================================
sub reload_html {

	open(HTML,"$html_file") || die "error!\n";
	flock (HTML, 2);
	@HTML = <HTML>;
	flock (HTML, 8);
	close(HTML);

	open(HTML2,"../head.html") || die "error!\n";
	flock (HTML2, 2);
	@HEAD_HTML = <HTML2>;
	flock (HTML2, 8);
	close(HTML2);

	$flg = 0;
	foreach $line (@HTML) {
		if( index( $line, '#INCLUDE_HEAD#' ) >= 0 ) {
			foreach $line2 (@HEAD_HTML) {
				print $line2;
			}
			$line = "<!-- INCLUDE OK -->\n";
		}
		$name = "";
		# ログイン名
		if( index( $line, "LoginName" ) >= 0 ) { $name = 'LoginName'; }
		# メールアドレス
		if( index( $line, "EMailAddress\"" ) >= 0 ) { $name = 'EMailAddress'; }
		if( index( $line, "EMailAddressCheck" ) >= 0 ) { $name = 'EMailAddressCheck'; }
		# 氏名
		if( index( $line, "\"Name" ) >= 0 ) { $name = 'Name'; }
		# フリガナ
		if( index( $line, "Ruby" ) >= 0 ) { $name = 'Ruby'; }
		# 住所
		if( index( $line, "Address1" ) >= 0 ) { $name = 'Address1'; }
		if( index( $line, "Address2" ) >= 0 ) { $name = 'Address2'; }
		# 電話番号
		if( index( $line, "Tel" ) >= 0 ) { $name = 'Tel'; }
		# ＦＡＸ番号
		if( index( $line, "Fax" ) >= 0 ) { $name = 'Fax'; }
		if( $name ne "" ) {
			$line =~ s/(#)(\d\d)(#)/$PARA{$name}/;	# 単純なデータの表示
		}

		# ログイン名の変更の禁止
		if( index( $line, "LoginName" ) >= 0 ) {
			if( $PARA{Title} eq "会員変更" ) {
				$line =~ s/>/ readonly>/;
			}
		}

		# タイトル
		$line =~ s/(#)(TITLE)(#)/$PARA{Title}/;

		# ラジオボタンの表示
		if( index( $line, "Sex" ) >= 0 ) {
			$line =~ s/(")($PARA{Sex})(")/"$PARA{Sex}" checked/;	# 単純なデータの表示
		}

		# オプション選択の初期値表示
		if( index( $line, "Prefecture" ) >= 0 ) { $flg = 3; }	# 都道府県
		if( index( $line, "/select" ) >= 0 ) { $flg = 0; }
		if( $flg != 0 ) {
			$name = "value=\"$PARA{Prefecture}\"";
			if( index( $line, "$name" ) >= 0 ) {
				$line =~ s/>/ selected>/;
			}
		}

		# 特殊表示
		if( index( $line, "Zip" ) >= 0 ) {
			# 郵便番号（111-1111のように分解）
			$line =~ s/(#)(ab)/$PARA{Zip1}/;
			$line =~ s/(#)(cd)/$PARA{Zip2}/;
		}
		if( index( $line, "Year" ) >= 0 ) {
			# 生年月日（YYYY年MM月DD日のように分解）
			$line =~ s/(#)(ab)/$PARA{Year}/;
			$line =~ s/(#)(cd)/$PARA{Month}/;
			$line =~ s/(#)(ef)/$PARA{Day}/;
		}
		$line =~ s/#MESSAGE#//g;
		$line =~ s/(#)(\d\d)(#)//g;
		print $line;
	}
}

#==================================================================
#	データを登録する
#==================================================================
sub write_data {

	$dbh = DBI->connect($data_source,$user_name,$user_pass) || die "CONNECT ERROR $DBI::ERRSTR";

	$sql = "SELECT * FROM customer WHERE Id=?;";
	$sth = $dbh->prepare($sql);
	$sth->execute( "$PARA{LoginName}" ) || die $sth->errstr;

  # 郵便番号を３桁−４桁に桁揃えする
  $zip1 = 0 + $PARA{Zip1};
  $zip2 = 0 + $PARA{Zip2};
  $zip = sprintf( "%03d%04d", $zip1, $zip2 );

	# 生年月日をＹＹＹＹＭＭＤＤに桁揃えする
	$year = 0 + $PARA{Year};
	$month = 0 + $PARA{Month};
	$day = 0 + $PARA{Day};
	$birthday = sprintf( "%04d%02d%02d", int($year), int($month), int($day) );

	# 本日日付
	($sec,$min,$hour,$day,$mon,$year,$wday) = localtime(time);
	$nowdate = sprintf("%04d%02d%02d",$year+1900,$mon+1,$day);

	if( $sth->rows == 0 ) {
		# 新規登録
		$sql = "INSERT INTO customer VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
		$sth = $dbh->prepare($sql);
		$sth->bind_param( 1, $PARA{LoginName} );					# ＩＤ
		$sth->bind_param( 2, $PARA{Password} );						# パスワード
		$sth->bind_param( 3, $zip );											# 郵便番号
		$sth->bind_param( 4, $PARA{Prefecture} );					# 都道府県
		$sth->bind_param( 5, $PARA{Address1} );						# 住所（市区町村番地）
		$sth->bind_param( 6, $PARA{Address2} );						# 住所（建物／アパート名）
		$sth->bind_param( 7, $PARA{Name} );								# 名前
		$sth->bind_param( 8, $PARA{Ruby} );								# フリガナ
		$sth->bind_param( 9, $PARA{Tel} );								# 電話番号
		$sth->bind_param( 10, $PARA{EMailAddress} );			# e-mailアドレス
		$sth->bind_param( 11, 0 );												# 最終注文日
		$sth->bind_param( 12, 0 );												# 累計購入金額
		$sth->bind_param( 13, $PARA{Fax} );								# ＦＡＸ番号
		$sth->bind_param( 14, $PARA{Sex} );								# 性別
		$sth->bind_param( 15, $birthday );								# 生年月日
		$sth->bind_param( 16, $PARA{melma} );							# メルマガ配信
		$sth->bind_param( 17, "" );										# Filler
		$sth->bind_param( 18, $nowdate );								# 入会日（本日）
		$sth->bind_param( 19, 0 );										# 更新日
		$sth->bind_param( 20, 0 );										# 退会フラグ
		$sth->execute || die print $sth->errstr;
	} else {
		#更新
		$sql = "UPDATE customer SET Id=?,Password=?,ZipCode=?,Address1=?,Address2=?,Address3=?,Name=?,Ruby=?,Telephone=?,MailAddress=?";
		$sql = $sql . ",LastOrderDate=?,TotalAmountOfMoney=?,Fax=?,Sex=?,Birthday=?,Melmaga=?,MantDate=? WHERE Id=?;";
		$sth = $dbh->prepare($sql);
		$sth->bind_param( 1, $PARA{LoginName} );						# ＩＤ
		$sth->bind_param( 2, $PARA{Password} );							# パスワード
		$sth->bind_param( 3, $zip );									# 郵便番号
		$sth->bind_param( 4, $PARA{Prefecture} );						# 都道府県
		$sth->bind_param( 5, $PARA{Address1} );							# 住所（市区町村番地）
		$sth->bind_param( 6, $PARA{Address2} );							# 住所（建物／アパート名）
		$sth->bind_param( 7, $PARA{Name} );								# 名前
		$sth->bind_param( 8, $PARA{Ruby} );								# フリガナ
		$sth->bind_param( 9, $PARA{Tel} );								# 電話番号
		$sth->bind_param( 10, $PARA{EMailAddress} );					# e-mailアドレス
		$sth->bind_param( 11, $PARA{LastOrderDate} );					# 最終注文日
		$sth->bind_param( 12, $PARA{TotalAmountOfMoney} );				# 累計購入金額
		$sth->bind_param( 13, $PARA{Fax} );								# ＦＡＸ番号
		$sth->bind_param( 14, $PARA{Sex} );								# 性別
		$sth->bind_param( 15, $birthday );								# 生年月日
		$sth->bind_param( 16, $PARA{melma} );							# メルマガ配信
		$sth->bind_param( 17, $nowdate );								# 生年月日
		$sth->bind_param( 18, $PARA{LoginName} );						# ＩＤ
		$sth->execute || die print $sth->errstr;
	}

	$sth->finish;
	$dbh->disconnect;
}

#==================================================================
#	データを削除する
#==================================================================
sub delete_data {
	
	$dbh = DBI->connect($data_source,$user_name,$user_pass) || die "CONNECT ERROR $DBI::ERRSTR";

	# 会員DB
	$sql = "DELETE from customer WHERE Id=?;";
	$sth = $dbh->prepare($sql);
	$sth->execute( "$PARA{LoginName}" ) || die print $sth->errstr;
	$sth->finish;

	# 届け先DB	
	$sql = "DELETE from receiver WHERE CustomerId=?;";
	$sth = $dbh->prepare($sql);
	$sth->execute( "$PARA{LoginName}" ) || die print $sth->errstr;
	$sth->finish;
	
	# 注文履歴
	$sql = "DELETE from orders WHERE CustomerId=?;";
	$sth = $dbh->prepare($sql);
	$sth->execute( "$PARA{LoginName}" ) || die print $sth->errstr;
	$sth->finish;
	
	$dbh->disconnect;
}

#==================================================================
#	必須入力のチェック
#==================================================================
sub err_check {

	$err_message = "";

	# 各必須項目の未入力チェック
	if( $PARA{LoginName} eq "" ) {
		$err_message = $err_message . "\n■ログイン名が入力されていません。<BR>\n";
	}
	else {
		# 登録済みのログイン名のチェック
		if ( $cc{LoginID} eq "" ) {
			$dbh = DBI->connect($data_source,$user_name,$user_pass) || die "CONNECT ERROR $DBI::ERRSTR";
			$sql = "SELECT * FROM customer WHERE Id=?;";
			$sth = $dbh->prepare($sql);
			$sth->execute( "$PARA{LoginName}" ) || die $sth->errstr;
			if( $sth->rows > 0 ) {
				$err_message = $err_message . "\n■ログイン名は既に使用されています。<BR>\n";
			}
			else {
				# 半角英数のチェック
				if( $PARA{LoginName} =~ /[^\w\-]/ ) {
					$err_message = $err_message . "\n■ログイン名は半角英数のみです。<BR>\n";
				} else {
					# 文字数の判定
					if( length( $PARA{LoginName} ) < 4 ) {
						$err_message = $err_message . "\n■ログイン名が短すぎます。<BR>\n";
					}
				}
			}
			$sth->finish;
			$dbh->disconnect;
		}
	}
	if( $PARA{Password} eq "" ) {
		$err_message = $err_message . "■パスワードが入力されていません。<BR>\n";
	}
	else {
		# 半角英数のチェック
		if( $PARA{Password} =~ /[^\w\-]/ ) {
			$err_message = $err_message . "\n■パスワードは半角英数のみです。<BR>\n";
		} else {
			# 文字数の判定
			if( length( $PARA{Password} ) < 4 ) {
				$err_message = $err_message . "■パスワードが短すぎます。<BR>\n";
			}
		}
	}
	if( $PARA{PasswordCheck} eq "" ) {
		$err_message = $err_message . "■パスワード（確認用）が入力されていません。<BR>\n";
	}
	if( $PARA{Password} ne $PARA{PasswordCheck} ) {
		$err_message = $err_message . "■パスワードとパスワード（確認用）が不一致です。<BR>\n";
	}
	$mailok = 0;
	if ( $PARA{EMailAddress} =~ /^[^@]+@[^.]+\..+/ ) {
	} else {
		$err_message = $err_message . "■メールアドレスが正しくありません。<BR>\n";
		$mailok = 1;
	}
	if( $PARA{EMailAddress} eq "" ) {
		$err_message = $err_message . "■メールアドレスが入力されていません。<BR>\n";
		$mailok = 1;
	}
	if( $PARA{EMailAddressCheck} eq "" ) {
		$err_message = $err_message . "■メールアドレス（確認用）が入力されていません。<BR>\n";
		$mailok = 1;
	}
	if( $PARA{EMailAddress} ne $PARA{EMailAddressCheck} ) {
		$err_message = $err_message . "■メールアドレスとメールアドレス（確認用）が不一致です。<BR>\n";
		$mailok = 1;
	}
	if( $mailok == 0 && $cc{LoginID} eq "" ) {	# メールアドレスがすでに登録済みでないか？
		$dbh = DBI->connect($data_source,$user_name,$user_pass) || die "CONNECT ERROR $DBI::ERRSTR";
		$sql = "SELECT * FROM customer WHERE MailAddress=?;";
		$sth = $dbh->prepare($sql);
		$sth->execute( "$PARA{EMailAddress}" ) || die $sth->errstr;
		if( $sth->rows > 0 ) {
			$err_message = $err_message . "■このメールアドレスは既に登録済みです。<BR>\n";
		}
		$sth->finish;
		$dbh->disconnect;
	}
	if( $PARA{Name} eq "" ) {
		$err_message = $err_message . "■氏名が入力されていません。<BR>\n";
	}
	if( $PARA{Ruby} eq "" ) {
		$err_message = $err_message . "■フリガナが入力されていません。<BR>\n";
	}
	if( $PARA{Sex} eq "" ) {
		$err_message = $err_message . "■性別が入力されていません。<BR>\n";
	}
	if( $PARA{Year} eq "" ) {
		$err_message = $err_message . "■生年月日（年）が入力されていません。<BR>\n";
	}
	if( $PARA{Year} < int(1900) ) {
		$err_message = $err_message . "■生年月日（年）を西暦4桁で入力して下さい。<BR>\n";
	}
	if( $PARA{Month} eq "" ) {
		$err_message = $err_message . "■生年月日（月）が入力されていません。<BR>\n";
	}
	if( $PARA{Day} eq "" ) {
		$err_message = $err_message . "■生年月日（日）が入力されていません。<BR>\n";
	}
	if( &DateExists( $PARA{Year}, $PARA{Month}, $PARA{Day} ) == 0 ) {
		$err_message = $err_message . "■生年月日が正しくありません。<BR>\n";
	}
	if( $PARA{Zip1} eq "" or $PARA{Zip2} eq "" ) {
		$err_message = $err_message . "■郵便番号が入力されていません。<BR>\n";
	} else {
		if( length( $PARA{Zip1} ) != 3 || length( $PARA{Zip2} ) != 4 ) {
			$err_message = $err_message . "■郵便番号が正しくありません。<BR>\n";
		} else {
			if( $PARA{Zip1} =~ /[^0-9]/ || $PARA{Zip2}=~ /[^0-9]/ ) {
				$err_message = $err_message . "■郵便番号が正しくありません。<BR>\n";
			}
		}
	}
	if( $PARA{Prefecture} eq "--- 選択して下さい ---" ) {
		$err_message = $err_message . "■都道府県が入力されていません。<BR>\n";
	}
	if( $PARA{Address1} eq "" ) {
		$err_message = $err_message . "■住所（市区町村、番地、部屋番号）が入力されていません。<BR>\n";
	}
	if( $PARA{Tel} eq "" ) {
		$err_message = $err_message . "■電話番号が入力されていません。<BR>\n";
	}
	if( $PARA{Tel} =~ /[^0-9|-]/ ) {
		$err_message = $err_message . "■電話番号は半角数字で入力して下さい。<BR>\n";
	}
	if( $PARA{Fax} ne "" ) {
		if( $PARA{Fax} =~ /[^0-9|-]/ ) {
			$err_message = $err_message . "■ＦＡＸ番号は半角数字で入力して下さい。<BR>\n";
		}
	}

	if( $err_message ne "" ) {
		# エラー
		### 隠し入力に既存入力値を保存しておく ###
		# タイトル
		$err_message = $err_message . '<INPUT size="20" type="hidden" maxlength="20" name="Title" value="' . $PARA{Title} . "\">\n";
		# ログイン名
		$err_message = $err_message . '<INPUT size="16" type="hidden" maxlength="16" name="LoginName" value="' . $PARA{LoginName} . "\">\n";
		# メールアドレス
		$err_message = $err_message . '<INPUT size="40" type="hidden" maxlength="40" name="EMailAddress" value="' . $PARA{EMailAddress} . "\">\n";
		$err_message = $err_message . '<INPUT size="40" type="hidden" maxlength="40" name="EMailAddressCheck" value="' . $PARA{EMailAddressCheck} . "\">\n";
		# 氏名
		$err_message = $err_message . '<INPUT size="20" type="hidden" maxlength="20" name="Name" value="' . $PARA{Name} . "\">\n";
		# フリガナ
		$err_message = $err_message . '<INPUT size="32" type="hidden" maxlength="32" name="Ruby" value="' . $PARA{Ruby} . "\">\n";
		# 性別
		$err_message = $err_message . '<INPUT size="5" type="hidden" maxlength="5" name="Sex" value="' . $PARA{Sex} . "\">\n";
		# 生年月日
		$err_message = $err_message . '<INPUT size="4" type="hidden" maxlength="4" name="Year" value="' . $PARA{Year} . "\">\n";		# 年
		$err_message = $err_message . '<INPUT size="2" type="hidden" maxlength="2" name="Month" value="' . $PARA{Month} . "\">\n";	# 月
		$err_message = $err_message . '<INPUT size="2" type="hidden" maxlength="2" name="Day" value="' . $PARA{Day} . "\">\n";			# 日
		# 自宅〒
		$err_message = $err_message . '<INPUT size="3" type="hidden" maxlength="3" name="Zip1" value="' . $PARA{Zip1} . "\">\n";
		$err_message = $err_message . '<INPUT size="4" type="hidden" maxlength="4" name="Zip2" value="' . $PARA{Zip2} . "\">\n";
		# 都道府県
		$err_message = $err_message . '<INPUT size="22" type="hidden" maxlength="22" name="Prefecture" value="' . $PARA{Prefecture} . "\">\n";
		# 住所
		$err_message = $err_message . '<INPUT size="50" type="hidden" maxlength="50" name="Address1" value="' . $PARA{Address1} . "\">\n";
		$err_message = $err_message . '<INPUT size="50" type="hidden" maxlength="50" name="Address2" value="' . $PARA{Address2} . "\">\n";
		# 電話番号
		$err_message = $err_message . '<INPUT size="15" type="hidden" maxlength="15" name="Tel" value="' . $PARA{Tel} . "\">\n";
		# ＦＡＸ番号
		$err_message = $err_message . '<INPUT size="15" type="hidden" maxlength="15" name="Fax" value="' . $PARA{Fax} . "\">\n";
		# 最終注文日
		$err_message = $err_message . '<INPUT size="8" type="hidden" maxlength="8" name="LastOrderDate" value="' . $PARA{LastOrderDate} . "\">\n";
		# 累計購入金額
		$err_message = $err_message . '<INPUT size="10" type="hidden" maxlength="10" name="TotalAmountOfMoney" value="' . $PARA{TotalAmountOfMoney} . "\">\n";

		open(HTML,"$inputerr_file") || die "error!\n";
		flock (HTML, 2);
		@HTML = <HTML>;
		flock (HTML, 8);
		close(HTML);

		open(HTML2,"../head.html") || die "error!\n";
		flock (HTML2, 2);
		@HEAD_HTML = <HTML2>;
		flock (HTML2, 8);
		close(HTML2);
	
		foreach $line (@HTML) {
			if( index( $line, '#INCLUDE_HEAD#' ) >= 0 ) {
				foreach $line2 (@HEAD_HTML) {
					print $line2;
				}
				$line = "<!-- INCLUDE OK -->\n";
			}
			$line =~ s/(#)(TITLE)(#)/$PARA{Title}/;	# タイトル
			$line =~ s/(#)(aa)(#)/$err_message/;		#エラーメッセージの表示
			print $line;
		}
		exit;
	}
}

sub DateExists {
	my($year, $month, $day) = @_;
	my(@mlast) = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	
	if ($month < 1 || 12 < $month) { return 0; }
	
	if ($month == 2) {
		if ( (($year % 4 == 0) && ($year % 100 != 0)) || ($year % 400 == 0) ) {
			$mlast[1]++;
		}
	}
	
	if ($day < 1 || $mlast[$month-1] < $day) { return 0; }
	
	return 1;
}

