#! /usr/bin/perl

use Jcode;
use CGI;

require "../../lib-bin/EdbTksn.pm";

	my %Conf = ();
	my @TITLE = ();

	my @All = ();
	my @Object = ();

	my $img_path = "/img/obj/";
	my $NextProg = "/tksn-bin/Detail.cgi";

	my ($i , $j , $list , $cnt , $title);

	my ($main_fl , $roop_fl1 , $roop_fl2);
	my ($main_tmp , $roop_tmp1 , $roop_tmp2);
	my ($main , $roop , $buf);

	my ($IMAGE , $PRICE , $NEXT_PRG);
	my ($TRAFFIC , $PRICE_FT , $LAND_INFO , $BLD_INFO , $LAYOUT , $USED_ZONE);
	my ($NEW_FLAG , $SPARE_FLAG);

	EdbTksn::GetConf(\%Conf);


	# --- set Titles
	$TITLE[1][1] = "新築一戸建";
	$TITLE[1][0] = "中古一戸建";
	$TITLE[2][1] = "売地";
	$TITLE[2][0] = "建築条件付売地";
	$TITLE[3][1] = "新築マンション";
	$TITLE[3][0] = "中古マンション";
	$TITLE[4][0] = $Conf{'JIGYO_TITLE'};

	# --- get List
	$cnt = EdbTksn::GetObjectTopList(\@All);
	EdbTksn::SortTopList(\@All , \@Object , $#All);


	# --- set Roop Count
	if ($Conf{'TOP_LIST_MAX'} ne "") {
		if ($cnt > $Conf{'TOP_LIST_MAX'}) {
			$cnt = $Conf{'TOP_LIST_MAX'};
		}
	}



	# --- read Template
	$main_tmp = "";
	$main_fl = "./template/recommend.html";
	$main_tmp = EdbTksn::ReadTemplate($main_fl , $Conf{'Code_Code'});

	$roop_tmp1 = "";
	$roop_fl1 = "./template/recommend_roop1.html";
	$roop_tmp1 = EdbTksn::ReadTemplate($roop_fl1 , $Conf{'Code_Code'});


	$roop_tmp2 = "";
	$roop_fl2 = "./template/recommend_roop2.html";
	$roop_tmp2 = EdbTksn::ReadTemplate($roop_fl2 , $Conf{'Code_Code'});




	$roop = "";

	if ($cnt == 0) {

		$roop .= <<HTML;

HTML

	} else {

		$j = 1;
		for ($i = 0; $i < $cnt; $i++) {

			if ($j == 1) {
				$roop .= <<HTML;

HTML
			}

			$NEW_FLAG	= "";
			$SPARE_FLAG	= "";
			$GOKEIYAKU = "";

			if ($Object[$i]{'NEW_FLAG'} == 1) {
				if ($Object[$i]{'NEW_FLAG_URL'} ne "") {
					$NEW_FLAG	= "<a href=\"".$Object[$i]{'NEW_FLAG_URL'}."\" target=\"_blank\"><img src=\"".$Conf{'IMG_PATH_NEW1'}."\" border=\"0\"></a>";
				} else {
					$NEW_FLAG	= "<img src=\"".$Conf{'IMG_PATH_NEW1'}."\">";
				}
			}
			if ($Object[$i]{'SPARE_FLAG'} == 1) {
				if ($Object[$i]{'SPARE_FLAG_URL'} ne "") {
					$SPARE_FLAG	= "<a href=\"".$Object[$i]{'SPARE_FLAG_URL'}."\" target=\"_blank\"><img src=\"".$Conf{'IMG_PATH_SPARE1'}."\" border=\"0\"></a>";
				} else {
					$SPARE_FLAG	= "<img src=\"".$Conf{'IMG_PATH_SPARE1'}."\">";
				}
			}


			$IMAGE		= $img_path.$Object[$i]{'IMG_TOP_FILE'};
			$NEXT_PRG	= $NextProg."?rgst=".$Object[$i]{'REGIST_CD'};

			$TRAFFIC	= EdbTksn::RemakeTrafficStringsForLst($Object[$i]{'BUS_TIME1'} , $Object[$i]{'BUS_STOP1'} , $Object[$i]{'WALK_TIME1'});
			$PRICE_FT	= EdbTksn::RemakePriceString1($Object[$i]{'PRICE_FROM'} , $Object[$i]{'PRICE_TO'});
			if ($Object[$i]{'PRICE_FROM'} ne 0){
			$PRICE		= EdbTksn::RemakePriceString2($Object[$i]{'PRICE_FROM'} , $Object[$i]{'PRICE_TO'});
			} else {
			$PRICE		= EdbTksn::RemakePriceString2($Object[$i]{'PRICE_FROM'} , $Object[$i]{'PRICE_TO'});
			$GOKEIYAKU  = "<li class=\"sold\">ご契約済</li>";
			}
			$LAND_INFO	= EdbTksn::RemakeLandStringsForLst($Object[$i]{'LAND_AREA_FROM'} , $Object[$i]{'LAND_AREA_TO'} , $Object[$i]{'LAND_AREA_BIKO'});
			$BLD_INFO	= EdbTksn::RemakeBuildStringsForLst($Object[$i]{'BLD_AREA_FROM'} , $Object[$i]{'BLD_AREA_TO'} , $Object[$i]{'BLD_AREA_BIKO'});
			$LAYOUT		= EdbTksn::RemakeLayoutString($Object[$i]{'LAYOUT_FROM'} , $Object[$i]{'LAYOUT_TO'});
			$USED_ZONE	= EdbTksn::RemakeYoutoString($Object[$i]{'USED_ZONE1'} , $Object[$i]{'USED_ZONE2'});


			$buf = $roop_tmp1;

			$buf =~ s/-TITLE-/$TITLE[$Object[$i]{'SHUBETSU_CD'}][$Object[$i]{'NEW_OLD_FLAG'}]/;
			$buf =~ s/-NEW_FLAG-/$NEW_FLAG/;
			$buf =~ s/-SPARE_FLAG-/$SPARE_FLAG/;

			$buf =~ s/=NEXT_PRG=/$NEXT_PRG/g;

			$buf =~ s/-TOP_COPY-/$Object[$i]{'TOP_COPY'}/;
			$buf =~ s/-IMAGE-/$IMAGE/;
			$buf =~ s/-LINE1-/$Object[$i]{'LINE1'}/;
			$buf =~ s/-STATION1-/$Object[$i]{'STATION1'}/;
			$buf =~ s/-BUS_TIME1-/$Object[$i]{'BUS_TIME1'}/;
			$buf =~ s/-BUS_STOP1-/$Object[$i]{'BUS_STOP1'}/;
			$buf =~ s/-WALK_TIME1-/$Object[$i]{'WALK_TIME1'}/;
			$buf =~ s/-TRAFFIC-/$TRAFFIC/;
			$buf =~ s/-PRICE-/$PRICE/;
			$buf =~ s/-PRICE_FT-/$PRICE_FT/;
			$buf =~ s/-GOKEIYAKU-/$GOKEIYAKU/;
			$buf =~ s/-LAND_AREA_FROM-/$Object[$i]{'LAND_AREA_FROM'}/;
			$buf =~ s/-LAND_AREA_TO-/$Object[$i]{'LAND_AREA_TO'}/;
			$buf =~ s/-LAND_INFO-/$LAND_INFO/g;

			$buf =~ s/-BLD_AREA_FROM-/$Object[$i]{'BLD_AREA_FROM'}/;
			$buf =~ s/-BLD_AREA_TO-/$Object[$i]{'BLD_AREA_TO'}/;
			$buf =~ s/-BLD_INFO-/$BLD_INFO/;

			$buf =~ s/-LAYOUT_FROM-/$Object[$i]{'LAYOUT_FROM'}/;
			$buf =~ s/-LAYOUT_TO-/$Object[$i]{'LAYOUT_TO'}/;
			$buf =~ s/-LAYOUT-/$LAYOUT/;

			$buf =~ s/-USED_ZONE-/$USED_ZONE/;
			$buf =~ s/-RIMAWARI-/$Object[$i]{'RIMAWARI'}/;
			$buf =~ s/-SHUBETSU_BIKO-/$Object[$i]{'SHUBETSU_BIKO'}/;

			$buf =~ s/-OBJECT_NAME-/$Object[$i]{'OBJECT_NAME'}/;
			$buf =~ s/-LOCATION-/$Object[$i]{'LOCATION'}/;

			$roop .= $buf;

			if ($j == $Conf{'TOP_LIST_LINE'}) {
				$roop .= <<HTML;

HTML
				$roop .= $roop_tmp2;
				$j = 1;
			} else {
				$j++;
			}
		}
		if ($j != 1) {
			for ($i = 0; $i < (($Conf{'TOP_LIST_LINE'} + 1) - $j); $i++) {
					$roop .= <<HTML;

HTML
			}
					$roop .= <<HTML;

HTML

		}
	}



	$out = $main_tmp;
	$out =~ s/=ROOP=/$roop/;


print "Content-type: text/html\n\n";
Jcode::convert(\$out,'sjis');
print $out;

exit;
