#===============================オーダーHTMLドキュメントを生成===========================
sub order_html {
	&get_cookie($cookie_name);
	# データファイル参照
		open(LOG,"$data_file") || &error('FILE OPEN ERROR - data');
		@logs = <LOG>;
		close(LOG);
	if($QUERY{'shu'} eq "------"){ $QUERY{'shu'} = "" }
	if($QUERY{'shu'} eq "" && $COOKIE{'shu'} ne ""){ $QUERY{'shu'} = $COOKIE{'shu'}; }
	if($QUERY{'shu'} eq "j"){ $QUERY{'shu'} = ""; }
	$order = $COOKIE{'order'};
	$sf = 0;
	foreach (@logs) {
		($hdate,$seq,$title,$junle,$image,$yen,$gram,$page,$size,$set_code,$com,$hf,$himg,$samnale,$busu,$mm) = split(/<>/, $_);
		if($QUERY{$seq} ne ""){
			@orders = split(/#!#/,$order);
			$order = ""; $f = 0;
			foreach $od (@orders){
				($seqo,$su) = split(/#!!#/,$od);
				if($seqo == $seq){
					if($QUERY{$seq} <= 0){ &error("$title の冊数の指定が不正です。 - ($QUERY{$seq})"); }
					$su += $QUERY{$seq}; $f = 1;
					if($busu_max >= 0 && $su > $busu_max){ &error("$title の冊数の指定が不正です。1冊につき$busu_max部以下でご注文ください"); }
				}
				$order .= "$seqo#!!#$su#!#";
			}
			if($f != 1){ $order .= "$seq#!!#$QUERY{$seq}#!#"; }
			$sf = 1;
		}
	}

	$cook="ck<>1\,shu<>$QUERY{'shu'}\,order<>$order\,o_shu<>$COOKIE{'o_shu'}\,email<>$COOKIE{'email'}\,email2<>$COOKIE{'email2'}\,name<>$COOKIE{'name'}\,namekana<>$COOKIE{'namekana'}\,zip<>$COOKIE{'zip'}\,add<>$COOKIE{'add'}\,ccode<>$COOKIE{'ccode'}";
	&set_cookie($cookie_name,$cook);
	if($sf == 1){ print "Location: $form_cgi?mode=order\n\n"; }
	open(PRE,"$order_file") || &error('FILE OPEN ERROR - order');
	@htmls = <PRE>;
	close(PRE);
	$html = "";
	foreach (@htmls) { $html .= $_; }
	($headw,$prew,$footw) = split(/<!--pre-->/,$html);
	$i = 0; $prev = ""; $gram_w = 0; $shokei = 0; $mm_w = 0;
	&get_cookie($cookie_name);
	@orders = split(/#!#/,$COOKIE{'order'});
	foreach (@logs) {
		($hdate,$seq,$title,$junle,$image,$yen,$gram,$page,$size,$set_code,$com,$hf,$himg,$samnale,$busu,$mm) = split(/<>/, $_);
		foreach $od (@orders){
			($seqo,$su) = split(/#!!#/,$od);
			if($seqo == $seq){
				$shokei += $yen * $su;
				$gram_w += $gram * $su;
				$mm_w += $mm * $su;
				$prevw = $prew;
				if($mojicode eq 's'){ &jcode'convert(*title,'sjis'); }
				$prevw =~ s/<!--title-->/$title/g;
				$prevw =~ s/<!--yen-->/$yen/g;
				$prevw =~ s/<!--seq-->/$seq/g;
				$prevw =~ s/<!--su-->/$su/g;
				$prev .= $prevw;
				$i++;
			}
		}
	}
	if($i == 0){
		$prev = $prew;
		$prev =~ s/<!--title-->/NO DATA/g;
		$prev =~ s/<!--yen-->/0/g;
		$prev =~ s/<!--su-->/0/g;
	}
	$html = $headw .$prev .$footw;
	# 送料計算
		&soryo_keisan;
	$gokei = $shokei + $soryo;
	# クッキーコード読み込み
		if($QUERY{'ccode'} eq ""){ $QUERY{'ccode'} = $COOKIE{'ccode'}; }
	# 支払い方法クッキー読み込み
		if($QUERY{'o_shu'} eq "" && $QUERY{'ccode'} == 1){ $QUERY{'o_shu'} = $COOKIE{'o_shu'}; }
		$how_sel = &how_select($QUERY{'o_shu'});
	$html =~ s/<!--form_cgi-->/$form_cgi/g;
	$html =~ s/<!--admin_cgi-->/$admin_cgi/g;
	$hidden = "<input type=\"hidden\" name=\"mode\" value=\"preview\">\n";
	if($mojicode eq 's'){ &jcode'convert(*send_shu2,'sjis'); }
	$hidden .= "<input type=\"hidden\" name=\"shu\" value=\"$send_shu2\">\n";
	$hidden .= "<input type=\"hidden\" name=\"shokei\" value=\"$shokei\">\n";
	$hidden .= "<input type=\"hidden\" name=\"soryo\" value=\"$soryo\">\n";
	$hidden .= "<input type=\"hidden\" name=\"order\" value=\"$order\">\n";
	$html =~ s/<!--hidden-->/$hidden/g;
	$html =~ s/<!--form-->/$prev/g;
	if($mojicode eq 's'){ &jcode'convert(*alert,'sjis'); }
	$html =~ s/<!--alert-->/$alert/g;
	$html =~ s/<!--how_sel-->/$how_sel/g;
	# 名前クッキー読み込み
		if($QUERY{'name'} eq "" && $QUERY{'ccode'} == 1){ $QUERY{'name'} = $COOKIE{'name'}; }
		$html =~ s/<!--name-->/$QUERY{'name'}/g;
	# 名前かなクッキー読み込み
		if($QUERY{'namekana'} eq "" && $QUERY{'ccode'} == 1){ $QUERY{'namekana'} = $COOKIE{'namekana'}; }
		$html =~ s/<!--namekana-->/$QUERY{'namekana'}/g;
	# e-mailクッキー読み込み
		if($QUERY{'email'} eq "" && $QUERY{'ccode'} == 1){ $QUERY{'email'} = $COOKIE{'email'}; }
		$html =~ s/<!--email-->/$QUERY{'email'}/g;
	# e-mail確認クッキー読み込み
		if($QUERY{'email2'} eq "" && $QUERY{'ccode'} == 1){ $QUERY{'email2'} = $COOKIE{'email2'}; }
		$html =~ s/<!--email2-->/$QUERY{'email2'}/g;
	# 郵便番号クッキー読み込み
		if($QUERY{'zip'} eq "" && $QUERY{'ccode'} == 1){ $QUERY{'zip'} = $COOKIE{'zip'}; }
		$html =~ s/<!--zip-->/$QUERY{'zip'}/g;
	# 住所クッキー読み込み
		if($QUERY{'add'} eq "" && $QUERY{'ccode'} == 1){ $QUERY{'add'} = $COOKIE{'add'}; }
		$QUERY{'add'} =~ s/<br>/\n/g;
		$html =~ s/<!--add-->/$QUERY{'add'}/g;
	# ccodeクッキー読み込み
		if($QUERY{'ccode'} == 1){ $ckd = " checked"; }else{ $ckd = ""; }
		$ccode = "<input name=\"ccode\" type=\"checkbox\" id=\"ccode\" value=\"1\"$ckd>";
		$html =~ s/<!--ccode-->/$ccode/g;
	$html =~ s/<!--comment-->/$QUERY{'comment'}/g;
	$html =~ s/<!--soryo-->/$soryo/g;
	$html =~ s/<!--send_shu-->/$send_shu/g;
	$html =~ s/<!--shokei-->/$shokei/g;
	$html =~ s/<!--gokei-->/$gokei/g;
	$html =~ s/<!--system-->/$systeminfo/g;
	print "Content-Type: text/html\n\n";
	print $html;
	exit;
}

#===============================オーダープレビューHTMLドキュメントを生成===========================
sub order_prehtml {
	$alert = "";
	if($QUERY{'soryo'} eq "NG"){ $alert .= "送料がオーバーフローしています。<br>\n"; }
	if($QUERY{'shu'} eq ""){ $alert .= "送料が不正です-$QUERY{'shu'}<br>\n"; }
	if($QUERY{'email'} eq ""){ $alert .= "メールアドレスを入力してください<br>\n"; }
	if(!($QUERY{'email'} =~ /(.*)\@(.*)\.(.*)/)){ $alert .= "メールアドレスが不正です<br>\n"; }
	if($email_ck == 1 && $QUERY{'email'} ne $QUERY{'email2'}){ $alert .= "メールアドレスが再入力と一致しません<br>\n"; }
	if($QUERY{'o_shu'} eq ""){ $alert .= "通販方法を選択してください<br>\n"; }
	else{
		open(LOG,"$how_file") || &error('FILE OPEN ERROR - how');
		@hows = <LOG>;
		close(LOG);
		$f = 0;
		foreach(@hows){
			($h_code,$h_name,$h_how,$add_ck) = split(/<>/,$_);
			if($h_code eq $QUERY{'o_shu'}){
				$f = 1;
				$h_how =~ s/<br>/\n/g;
				$h_how =~ s/&lt;/</g;
				$h_how =~ s/&gt;/>/g;
				last;
			}
		}
		if($f != 1){ &error('通販方法が定義されていません。'); }
		if($add_ck == 1 && $QUERY{'name'} eq ""){ $alert .= "お名前を入力してください<br>\n"; }
		if($add_ck == 1 && $inck_kana == 1 && $QUERY{'namekana'} eq ""){ $alert .= "お名前カナを入力してください<br>\n"; }
		if($add_ck == 1 && $QUERY{'zip'} eq ""){ $alert .= "郵便番号を入力してください<br>\n"; }
		if($add_ck == 1 && $QUERY{'add'} eq ""){ $alert .= "住所を入力してください<br>\n"; }
	}
	if($alert ne ""){ $alert = '<b>ERROR:</b><br>' .$alert; &order_html; }
	$QUERY{'add'} =~ s/\r\n/\n/g; $QUERY{'add'} =~ s/\r/\n/g; # 改行の統一
	# ユーザー情報クッキー保存
		&get_cookie($cookie_name);
		if($QUERY{'ccode'} == 1){
			$addwk = $QUERY{'add'};
			$addwk =~ s/\n/<br>/g;
			$cook="ck<>$COOKIE{'ck'}\,shu<>$COOKIE{'shu'}\,order<>$COOKIE{'order'}\,o_shu<>$QUERY{'o_shu'}\,email<>$QUERY{'email'}\,email2<>$QUERY{'email2'}\,name<>$QUERY{'name'}\,namekana<>$QUERY{'namekana'}\,zip<>$QUERY{'zip'}\,add<>$addwk\,ccode<>$QUERY{'ccode'}";
		}else{
			$cook="ck<>$COOKIE{'ck'}\,shu<>$COOKIE{'shu'}\,order<>$COOKIE{'order'}\,o_shu<>\,email<>\,email2<>\,name<>\,namekana<>\,zip<>\,add<>\,ccode<>";
		}
		&set_cookie($cookie_name,$cook);
	$QUERY{'comment'} =~ s/\r\n/\n/g; $QUERY{'comment'} =~ s/\r/\n/g; # 改行の統一
	$hidden = "<input type=\"hidden\" name=\"mode\" value=\"send\">\n";
	$hidden .= "<input type=\"hidden\" name=\"shokei\" value=\"$QUERY{'shokei'}\">\n";
	$hidden .= "<input type=\"hidden\" name=\"soryo\" value=\"$QUERY{'soryo'}\">\n";
	$hidden .= "<input type=\"hidden\" name=\"order\" value=\"$order\">\n";
	$hidden .= "<input type=\"hidden\" name=\"shu\" value=\"$QUERY{'shu'}\">\n";
	$hidden .= "<input type=\"hidden\" name=\"email\" value=\"$QUERY{'email'}\">\n";
	$hidden .= "<input type=\"hidden\" name=\"email2\" value=\"$QUERY{'email2'}\">\n";
	$hidden .= "<input type=\"hidden\" name=\"o_shu\" value=\"$QUERY{'o_shu'}\"\n>";
	$hidden .= "<input type=\"hidden\" name=\"name\" value=\"$QUERY{'name'}\">\n";
	$hidden .= "<input type=\"hidden\" name=\"namekana\" value=\"$QUERY{'namekana'}\">\n";
	$hidden .= "<input type=\"hidden\" name=\"zip\" value=\"$QUERY{'zip'}\">\n";
	$hidden .= "<input type=\"hidden\" name=\"add\" value=\"$QUERY{'add'}\">\n";
	$hidden .= "<input type=\"hidden\" name=\"order\" value=\"$QUERY{'order'}\">\n";
	$hidden .= "<input type=\"hidden\" name=\"comment\" value=\"$QUERY{'comment'}\">\n";
	# データファイル参照
		open(LOG,"$data_file") || &error('FILE OPEN ERROR - data');
		@logs = <LOG>;
		close(LOG);
	$sf = 0;
	foreach (@logs) {
		($hdate,$seq,$title,$junle,$image,$yen,$gram,$page,$size,$set_code,$com,$hf,$himg,$samnale,$busu,$mm) = split(/<>/, $_);
		if($QUERY{$seq} ne ""){
			@orders = split(/#!#/,$order);
			$order = ""; $f = 0;
			foreach $od (@orders){
				($seqo,$su) = split(/#!!#/,$od);
				if($seqo == $seq){
					if($QUERY{$seq} <= 0){ &error("$title の冊数の指定が不正です。 - ($QUERY{$seq})"); }
					$su += $QUERY{$seq}; $f = 1;
					if($busu_max >= 0 && $su > $busu_max){ &error("$title の冊数の指定が不正です。1冊につき$busu_max部以下でご注文ください"); }
				}
				$order .= "$seqo#!!#$su#!#";
			}
			if($f != 1){ $order .= "$seq#!!#$QUERY{$seq}#!#"; }
			$sf = 1;
		}
	}

	if($sf == 1){ print "Location: $form_cgi?mode=order\n\n"; }
	open(PRE,"$preview_file") || &error('FILE OPEN ERROR - preview');
	@htmls = <PRE>;
	close(PRE);
	$html = "";
	foreach (@htmls) { $html .= $_; }
	($headw,$prew,$footw) = split(/<!--pre-->/,$html);
	$i = 0; $prev = "";
	$order = $QUERY{'order'};
	@orders = split(/#!#/,$QUERY{'order'});
	foreach (@logs) {
		($hdate,$seq,$title,$junle,$image,$yen,$gram,$page,$size,$set_code,$com,$hf,$himg,$samnale,$busu,$mm) = split(/<>/, $_);
		foreach $od (@orders){
			($seqo,$su) = split(/#!!#/,$od);
			if($seqo == $seq){
				$prevw = $prew;
				if($mojicode eq 's'){ &jcode'convert(*title,'sjis'); }
				$prevw =~ s/<!--title-->/$title/g;
				$prevw =~ s/<!--yen-->/$yen/g;
				$prevw =~ s/<!--seq-->/$seq/g;
				$prevw =~ s/<!--su-->/$su/g;
				$prev .= $prevw;
				$i++;
			}
		}
	}
	if($i == 0){ &order_html; }
	$html = $headw .$prev .$footw;
	$gokei = $QUERY{'shokei'} + $QUERY{'soryo'};
	$html =~ s/<!--form_cgi-->/$form_cgi/g;
	$html =~ s/<!--admin_cgi-->/$admin_cgi/g;
	$html =~ s/<!--hidden-->/$hidden/g;
	$html =~ s/<!--form-->/$prev/g;
	$html =~ s/<!--alert-->/$alert/g;
	if($mojicode eq 's'){ &jcode'convert(*h_name,'sjis'); }
	$html =~ s/<!--how_sel-->/$h_name/g;
	$html =~ s/<!--name-->/$QUERY{'name'}/g;
	$html =~ s/<!--namekana-->/$QUERY{'namekana'}/g;
	$html =~ s/<!--email-->/$QUERY{'email'}/g;
	$html =~ s/<!--email2-->/$QUERY{'email2'}/g;
	$html =~ s/<!--zip-->/$QUERY{'zip'}/g;
	$QUERY{'add'} =~ s/\n/<br>/g;
	$html =~ s/<!--add-->/$QUERY{'add'}/g;
	$QUERY{'comment'} =~ s/\n/<br>/g;
	$html =~ s/<!--comment-->/$QUERY{'comment'}/g;
	$html =~ s/<!--soryo-->/$QUERY{'soryo'}/g;
	$html =~ s/<!--send_shu-->/$QUERY{'shu'}/g;
	$html =~ s/<!--shokei-->/$QUERY{'shokei'}/g;
	$html =~ s/<!--gokei-->/$gokei/g;
	$html =~ s/<!--system-->/$systeminfo/g;
	print "Content-Type: text/html\n\n";
	print $html;
	exit;
}

#===============================通販方法セレクトルーチン===========================
sub how_select {
	$now_w = $_[0];
	$i = 0; $lev = ''; $sel = "";
	open(LOG,"$how_file") || &error('FILE OPEN ERROR - how');
	@hows = <LOG>;
	close(LOG);
	foreach (@hows){
		($h_code,$h_name,$h_how,$add_ck) = split(/<>/,$_);
		if($now_w eq $h_code){ $sel .= "<input type=\"radio\" name=\"o_shu\" value=\"$h_code\" checked>$h_name<br>\n";}
		else{ $sel .= "<input type=\"radio\" name=\"o_shu\" value=\"$h_code\">$h_name<br>\n";}
		$i++;
	}
	if($mojicode eq 's'){ &jcode'convert(*sel,'sjis'); }
	return $sel;
}

#===============================注文取消しルーチン===========================
sub order_del {
		&get_cookie($cookie_name);
		@orders = split(/#!#/,$COOKIE{'order'});
		$order = "";
		foreach $od (@orders){
			($seq,$su) = split(/#!!#/,$od);
			if($seq ne $QUERY{'seq'} || $su  ne $QUERY{'su'}){ $order .= "$seq#!!#$su#!#"; }
		}
		$cook="ck<>1\,shu<>$QUERY{'shu'}\,order<>$order\,o_shu<>$COOKIE{'o_shu'}\,email<>$COOKIE{'email'}\,email2<>$COOKIE{'email2'}\,name<>$COOKIE{'name'}\,namekana<>$COOKIE{'namekana'}\,zip<>$COOKIE{'zip'}\,add<>$COOKIE{'add'}\,ccode<>$COOKIE{'ccode'}";
		&set_cookie($cookie_name,$cook);
		print "Location: $form_cgi?mode=order\n\n";
}

#===============================送信===========================
sub order_send {
	$alert = "";
	if($QUERY{'soryo'} eq "NG"){ $alert .= "送料がオーバーフローしています。<br>\n"; }
	if($QUERY{'email'} eq ""){ $alert .= "メールアドレスを入力してください<br>\n"; }
	if(!($QUERY{'email'} =~ /(.*)\@(.*)\.(.*)/)){ $alert .= "メールアドレスが不正です<br>\n"; }
	if(!( $QUERY{'email'} =~ /^[0-9a-zA-Z-_.]+@[0-9a-zA-Z-_.]+$/ )){ $alert .= "メールアドレスが不正です<br>\n"; }
	if($email_ck == 1 && $QUERY{'email'} ne $QUERY{'email2'}){ $alert .= "メールアドレスが再入力と一致しません<br>\n"; }
	if($QUERY{'o_shu'} eq ""){ $alert .= "通販方法を選択してください<br>\n"; }
	else{
		open(LOG,"$how_file") || &error('FILE OPEN ERROR - how');
		@hows = <LOG>;
		close(LOG);
		$f = 0;
		foreach(@hows){
			($h_code,$h_name,$h_how,$add_ck) = split(/<>/,$_);
			if($h_code eq $QUERY{'o_shu'}){
				$f = 1;
				$h_how =~ s/<br>/\n/g;
				$h_how =~ s/&lt;/</g;
				$h_how =~ s/&gt;/>/g;
				if($mojicode eq 's'){ &jcode'convert(*h_how,'sjis'); }
				last;
			}
		}
		if($f != 1){ &error('通販方法が定義されていません。'); }
		if($add_ck == 1 && $QUERY{'name'} eq ""){ $alert .= "お名前を入力してください<br>\n"; }
		if($add_ck == 1 && $inck_kana == 1 && $QUERY{'namekana'} eq ""){ $alert .= "お名前カナを入力してください<br>\n"; }
		if($add_ck == 1 && $QUERY{'zip'} eq ""){ $alert .= "郵便番号を入力してください<br>\n"; }
		if($add_ck == 1 && $QUERY{'add'} eq ""){ $alert .= "住所を入力してください<br>\n"; }
	}
	if($alert ne ""){ $alert = '<b>ERROR:</b><br>' .$alert; &order_html; }
	$QUERY{'add'} =~ s/\r\n/\n/g; $QUERY{'add'} =~ s/\r/\n/g; # 改行の統一
	$QUERY{'comment'} =~ s/\r\n/\n/g; $QUERY{'comment'} =~ s/\r/\n/g; # 改行の統一
	# ロック開始
		if ($lockkey == 1) { &lock1; }
		elsif ($lockkey == 2) { &lock2; }
	if(-s $data_file){
		open(LOG,"$data_file") || &error('FILE OPEN ERROR - data');
		@logs = <LOG>;
		close(LOG);
		@orders = split(/#!#/,$QUERY{'order'});
		foreach $od (@orders){
			($seqo,$su) = split(/#!!#/,$od);
			$QUERY{$seqo} = "on";
			$suw = $seqo .'_su';
			$QUERY{$suw} = $su;
		}
		$i = 0; $prev = "";
		@news = ();
		foreach (@logs) {
			($hdate,$seq,$title,$junle,$image,$yen,$gram,$page,$size,$set_code,$com,$hf,$himg,$samnale,$busu,$mm) = split(/<>/, $_);
			if($QUERY{$seq} eq "on"){
				$su = $seq .'_su';
				if($busu_ck >= 0 && $busu - $QUERY{$su} < $busu_ck){ $bwk = $busu - $busu_ck; &error("$title の在庫が不足しています。$bwk以下でご注文ください"); }
				if($QUERY{$su} <= 0){ &error("$title の冊数の指定が不正です - ($QUERY{$su})"); }
				$yenw = $yen * $QUERY{$su};
				$prev .= "$title : $yen円 × $QUERY{$su}冊 ＝ $yenw 円\n";
				$i++;
				$busu -= $QUERY{$su};
				if($busu <= $busu_ck && $busu_ck >= 0){ $hf = 9; $himg = "残部少"; } if($busu <= 0){ $busu = 0; }
				push(@news,"$hdate<>$seq<>$title<>$junle<>$image<>$yen<>$gram<>$page<>$size<>$set_code<>$com<>$hf<>$himg<>$samnale<>$busu<>$mm<>\n");
			}else{ push(@news,$_); }
		}
		if($i == 0){ $prev = ""; }
	}else{
		$prev = "";
	}
	if($prev eq ""){
		$alert = "注文の商品がありません。\n";
		&unlock; # ロック解除
		&order_html;
	}
	if($mojicode eq 's'){ &jcode'convert(*prev,'sjis'); }
	if($mojicode eq 's'){ &jcode'convert(*h_name,'sjis'); }
	$gokei = $QUERY{'shokei'} + $QUERY{'soryo'};
	open(OUT,">$data_file") || &error('FILE OPEN ERROR - data');
	print OUT @news;
	close(OUT);
	if($tno_ck == 2){
		$cookie_namew = $cookie_name .'cnt';
		&get_cookie($cookie_namew);
		$tno = $COOKIE{'cnt'};
	}elsif($tno_ck == 1 || $tno_ck == 3){
		open(TNO,"$tno_file") || &error('ファイルオープンに失敗しました - no');
		$tno = <TNO>;
		close(TNO);
		if($tno_ck == 1){
			$tno =~ s/\n//g;
			if($tno_count <= 1){ $tno_count = 1; }
			$tno += $tno_count;
			open(OUT,">$tno_file") || &error('FILE OPEN ERROR - no');
			print OUT $tno;
			close(OUT);
		}elsif($tno_ck == 3){
			$tno =~ s/\n//g;
			srand;
			$tno_count = int(rand($tno_count));
			if($tno_count <= 1){ $tno_count = 1; }
			$tno += $tno_count;
			open(OUT,">$tno_file") || &error('FILE OPEN ERROR - no');
			print OUT $tno;
			close(OUT);
		}
	}
	while(length($tno) < $tno_keta){ $tno = '0' .$tno; }
	$tno = $tno_top .$tno;
	$subject = $tno .' ' .$subject;
	&unlock; # ロック解除
	#送信メッセージ生成
	# リモートホストを取得（取得できなかった場合はADDR取得）
	if($ENV{'REMOTE_HOST'} ne ""){ $remote = $ENV{'REMOTE_HOST'}; }
	else {
		use Socket;
		$add = inet_aton($ENV{'REMOTE_ADDR'});
		$remote = gethostbyaddr($add,&AF_INET);
	}
	require 'agent.pl';
	($platform, $platform_v, $browser, $browser_v) = &User_Agent($ENV{'HTTP_USER_AGENT'});
	open(USR,"$user_tmp") || &error('ファイルオープンに失敗しました - user');
	@utmpsw = <USR>;
	close(USR);
	$msgw = "";
	foreach $line (@utmpsw) { $msgw .= $line; }
		$msgw =~ s/<!--how-->/$h_how/g;
		$msgw =~ s/<!--host-->/$remote/g;
		$msgw =~ s/<!--ip-->/$ENV{'REMOTE_ADDR'}/g;
		$msgw =~ s/<!--platform-->/$platform $platform_v/g;
		$msgw =~ s/<!--browser-->/$browser $browser_v/g;
		$msgw =~ s/<!--tno-->/$tno/g;
		$msgw =~ s/<!--name-->/$QUERY{'name'}/g;
		$msgw =~ s/<!--namekana-->/$QUERY{'namekana'}/g;
		$msgw =~ s/<!--email-->/$QUERY{'email'}/g;
		$msgw =~ s/<!--zip-->/$QUERY{'zip'}/g;
		$msgw =~ s/<!--add-->/$QUERY{'add'}/g;
		$msgw =~ s/<!--comment-->/$QUERY{'comment'}/g;
		$msgw =~ s/<!--order-->/$prev/g;
		$msgw =~ s/<!--soryo-->/$QUERY{'soryo'}/g;
		$msgw =~ s/<!--send_shu-->/$QUERY{'shu'}/g;
		$msgw =~ s/<!--shokei-->/$QUERY{'shokei'}/g;
		$msgw =~ s/<!--gokei-->/$gokei/g;
	open(MST,"$master_tmp") || &error('ファイルオープンに失敗しました - master');
	@mtmps = <MST>;
	close(MST);
	$msg = "";
	foreach $line (@mtmps) { $msg .= $line; }
		$msg =~ s/<!--host-->/$remote/g;
		$msg =~ s/<!--ip-->/$ENV{'REMOTE_ADDR'}/g;
		$msg =~ s/<!--platform-->/$platform $platform_v/g;
		$msg =~ s/<!--browser-->/$browser $browser_v/g;
		$msg =~ s/<!--o_shu-->/$h_name/g;
		$msg =~ s/<!--tno-->/$tno/g;
		$msg =~ s/<!--name-->/$QUERY{'name'}/g;
		$msg =~ s/<!--namekana-->/$QUERY{'namekana'}/g;
		$msg =~ s/<!--email-->/$QUERY{'email'}/g;
		$msg =~ s/<!--zip-->/$QUERY{'zip'}/g;
		$msg =~ s/<!--add-->/$QUERY{'add'}/g;
		$msg =~ s/<!--comment-->/$QUERY{'comment'}/g;
		$msg =~ s/<!--order-->/$prev/g;
		$msg =~ s/<!--soryo-->/$QUERY{'soryo'}/g;
		$msg =~ s/<!--send_shu-->/$QUERY{'shu'}/g;
		$msg =~ s/<!--shokei-->/$QUERY{'shokei'}/g;
		$msg =~ s/<!--gokei-->/$gokei/g;
	&mail;
	print "Location: $form_cgi?mode=location&name=$QUERY{'name'}&email=$QUERY{'email'}\n\n";
	exit;
}

#================================メール転送処理=================================
sub mail{
	if($QUERY{'email'} eq ""){ &error('メールアドレスを入力してください') }
	if(!($QUERY{'email'} =~ /(.*)\@(.*)\.(.*)/)){ &error('メールアドレスが不正です。'); }
	$subject = &moji_modosu($subject);
	$msg = &moji_modosu($msg);
	$msgw = &moji_modosu($msgw);
	&jcode'convert(*subject,'jis');
	&jcode'convert(*msg,'jis');
	&jcode'convert(*msgw,'jis');

	# 管理者にメール送信
	foreach $mlw (@mailtos){
		$mailtow = $mlw;
		if (!open(MAIL,"| $sendmail $mailtow")) { &error('何らかの原因で送信できませんでした。'); }
			print MAIL "X-Mailer: $systeminfo\n";
			print MAIL "To: $mailtow\n";
			print MAIL "From: $QUERY{'email'}\n";
			print MAIL "Subject: $subject\n";
			print MAIL "Content-Transfer-Encoding: 7bit\n";
			print MAIL "Content-Type: text/plain\; charset=\"ISO-2022-JP\"\n\n";
			print MAIL "$msg";
			close(MAIL);
	}
	# 送信者にメール送信
	if (!open(MAIL,"| $sendmail $QUERY{'email'}")) { &error('何らかの原因で送信できませんでした。'); }
		print MAIL "X-Mailer: $systeminfo\n";
		print MAIL "To: $QUERY{'email'}\n";
		print MAIL "From: $mailtos[0]\n";
		print MAIL "Subject: $subject\n";
		print MAIL "Content-Transfer-Encoding: 7bit\n";
		print MAIL "Content-Type: text/plain\; charset=\"ISO-2022-JP\"\n\n";
		print MAIL "$msgw";
		close(MAIL);
}

#================================ロケーション=================================
sub order_location{
	&get_cookie($cookie_name);
	$cook="ck<>\,shu<>\,order<>\,o_shu<>$COOKIE{'o_shu'}\,email<>$COOKIE{'email'}\,email2<>$COOKIE{'email2'}\,name<>$COOKIE{'name'}\,namekana<>$COOKIE{'namekana'}\,zip<>$COOKIE{'zip'}\,add<>$COOKIE{'add'}\,ccode<>$COOKIE{'ccode'}";
	&set_cookie($cookie_name,$cook);
	$cookie_namew = $cookie_name .'cnt';
	&set_count($cookie_namew,"");
	open(HTML,"$location_file") || &error('ファイルオープンに失敗しました - thanks');
	@html = <HTML>;
	close(HTML);
	$sama = ' 様';
	if($mojicode eq 's'){ &jcode'convert(*sama,'sjis'); }
	if($QUERY{'name'} ne ""){ $QUERY{'name'} .= $sama; }
	print "Content-Type: text/html\n\n";
	foreach $line (@html) {
		$line =~ s/<!--form_cgi-->/$form_cgi/g;
		$line =~ s/<!--name-->/$QUERY{'name'}/g;
		$line =~ s/<!--email-->/$QUERY{'email'}/g;
		$line =~ s/<!--system-->/$systeminfo/g;
		print $line;
	}
	exit;
}
#================================送料計算=================================
sub soryo_keisan{
	if($gram_w > 0){ $gram_w += $ad_gram; }
	if($mm_w > 0){ $mm_w += $ad_mm; }
	open(SEN,"$soryo_file") || &error('FILE OPEN ERROR - soryo');
	@sends = <SEN>;
	close(SEN);
	$soryo = 0; $send_shu = "";
	# 自動送料
		@auto_sen = ();
		foreach (@sends) {
			($kara,$made,$syen,$shu,$mkara,$mmade) = split(/<>/, $_);
			if($gram_w > $kara && $gram_w <= $made){ if(($mkara eq "" && $mmade eq "") || ($mm_w > $mkara && $mm_w <= $mmade)){ push(@auto_sen,$_); } }
			elsif($mm_w > $mkara && $mm_w <= $mmade){ if(($kara eq "" && $made eq "") || ($gram_w > $kara && $gram_w <= $made)){ push(@auto_sen,$_); } }
		}
	# 種別送料
		if($QUERY{'shu'} ne ""){
			@new_sen = ();
			foreach (@sends) {
				($kara,$made,$syen,$shu,$mkara,$mmade) = split(/<>/, $_);
				$shuw = $shu;
				if($mojicode eq 's'){ &jcode'convert(*shuw,'sjis'); }
				if($shuw eq $QUERY{'shu'} && $gram_w > $kara && $gram_w <= $made){ if(($mkara eq "" && $mmade eq "") || ($mm_w > $mkara && $mm_w <= $mmade)){ push(@new_sen,$_); } }
				elsif($shuw eq $QUERY{'shu'} && $mm_w > $mkara && $mm_w <= $mmade){ if(($kara eq "" && $made eq "") || ($gram_w > $kara && $gram_w <= $made)){ push(@new_sen,$_); } }
			}
		}else{ @new_sen = @auto_sen; }
	# 送料オーバーフロー処理
		if($QUERY{'shu'} ne "" && @new_sen < 1){ $alert .= "送料がオーバーフローしたので自動設定に変更しました。<br>"; @new_sen = @auto_sen; $QUERY{'shu'} = ""; }
		if($gram_w > 0 && @new_sen < 1){ $send_shu = 'NG'; $alert .= "送料オーバーフローです。注文数を減らしてください。<br>"; $sendng = 1; $soryo = "NG"; }

	@wks = ();
	foreach (@new_sen) {
		($kara,$made,$syen,$shu,$mkara,$mmade) = split(/<>/, $_);
		$kara = sprintf("%05d",$kara);
		$made = sprintf("%05d",$made);
		$syen = sprintf("%05d",$syen);
		$mkara = sprintf("%05d",$mkara);
		$mmade = sprintf("%05d",$mmade);
		push(@wks,"$syen<>$mkara<>$kara<>$mmade<>$made<>$shu<>\n");
	}
	@wks = sort { $a cmp $b } @wks;
	($syen,$mkara,$kara,$mmade,$made,$shu) = split(/<>/, @wks[0]);
	$kara = sprintf("%d",$kara);
	$made = sprintf("%d",$made);
	$syen = sprintf("%d",$syen);
	$mkara = sprintf("%d",$mkara);
	$mmade = sprintf("%d",$mmade);
	if($sendng != 1 && $soryo == 0 || $soryo > $syen){ $soryo = $syen; $send_shu = $shu; }
	if($sendng != 1 && $gram_w <= 0){ $send_shu = "------"; }
	if($sendng != 1 && $QUERY{'shu'} eq ""){ $send_shuwk = '自動（' .$send_shu .'）'; }else{ $send_shuwk = '自動'; }
	$send_shu2 = $send_shu;
	if($sendng != 1){
	# セレクトルーチン
		@send_shus = ();
		foreach (@sends){
			($kara,$made,$syen,$shu,$mkara,$mmade) = split(/<>/,$_);
			push(@send_shus,$shu);
		}
		@sorted = sort { $a cmp $b } @send_shus;
		@send_shus = (); $last_shu = "";
		foreach $shu (@sorted){
			if($shu ne $last_shu){ push(@send_shus,$shu); }
			$last_shu = $shu;
		}
		if(@send_shus == 1){
			$send_shu = "<input type=\"hidden\" name=\"shu\" value=\"@send_shus[0]\">@send_shus[0]";
			$send_shu2 = @send_shus[0];
		}elsif($soryo_auto == 1){
			$send_shu = "<input type=\"hidden\" name=\"shu\" value=\"\">$send_shuwk";
		}else{
			unshift(@send_shus,$send_shuwk);
			$send_shu = "<select onChange=\"navi(this)\">";
			foreach $shu (@send_shus){
				$shuw = $shu;
				if($mojicode eq 's'){ &jcode'convert(*shuw,'sjis'); }
				if($shu =~ /自動/){ $shuwk = "j"; }else{ $shuwk = $shu; }
				if($QUERY{'shu'} eq $shuw){ $send_shu .= "<option value=\"$form_cgi?mode=order&shu=$shuwk\" selected>$shu</option>\n";}
				else{ $send_shu .= "<option value=\"$form_cgi?mode=order&shu=$shuwk\">$shu</option>\n";}
			}
			$send_shu .= "</select>";
		}
	}
	if($mojicode eq 's'){ &jcode'convert(*send_shu,'sjis'); }

}

return 1;
