#!/usr/bin/perl
#

use 5.006;
use DBI;
use CGI;
use lib qw(/mnt/sda/tmm/handlers/genc);
use TClubMD5;

my $server = "127.0.0.1";
my $user = $primary_user;
my $passwd = $primary_passwd;
my $dbase = $primary_dbase;
my $port = $primary_port;

my $query = new CGI;

print "Content-Type: text/html; charset=UTF-8\n\n";

my $par = $query->param('par');
my $ru_en = $query->param('l');
my $who = $query->param('who');
my $token = $query->param('token');
my $language = $query->param('lang');
my $db = $query->param('db');

my $coid = $query->param('coid');

my $session = TClubMD5::md5_hex($who.$salt);

die("get_response.pl: token is $token, session is $session!\n") if ($token ne $session);
die("psssooff!\n") if (in_list($who,$pssoff_guys_string));

my %court_labels = ('clay'=>'clay','brick'=>'red clay',
				'greenset'=>'greenset','asphalt'=>'asphalt',
				'paint'=>'painted concrete','concrete'=>'concrete',
				'wood'=>'wood','artgrass'=>'artgrass','grass'=>'grass','carpet'=>'carpet',
			        'hard'=>'hard','rubber'=>'rubber','synthetics'=>'synthetics','taraflex'=>'taraflex');
				
if ($language eq 'russian') {%court_labels = ('clay'=>'глина','brick'=>'грунт',
				'greenset'=>'гринсет','asphalt'=>'асфальт',
				'paint'=>'краш.бетон','concrete'=>'бетон',
				'wood'=>'дерево','artgrass'=>'иск.трава','grass'=>'трава','carpet'=>'ковер',
			        'hard'=>'хард','rubber'=>'резина','synthetics'=>'синтетика','taraflex'=>'тарафлекс');
}


($dbase,$server,$port,$user,$passwd) = TClubMD5::get_connect_req($ru_en);

$dbase = 'clone' if ($db eq 'three');#hack

$dbconn=DBI->connect("dbi:Pg:dbname=$dbase;port=$port;host=$server",$user, $passwd);

$comm = "select distinct e_type from courts where a_location_id='$coid'";
&getTable;

my $xml = "<SELECT name=new_court_type id=sel_court_type SIZE=1 style=\"width:90px;font-size:12px;\">";
$xml .= "<option value=''>&nbsp;" if ($ntuples  >1);

for (my $i=0; $i<$ntuples;$i++) {

my $a = ${${$listresult}[$i]}[0];

$xml .= "<OPTION VALUE=\"$a\">".$court_labels{$a} 
}

$xml .= "</SELECT>";

print $xml;

$dbconn->disconnect;
$query->delete_all;
exit;

sub getTable {

	$result=$dbconn->prepare($comm);
    	$result->execute;
	&dBaseError($result, $comm."  (".$result->rows()." rows found)") if ($result->rows() ==
	-2);
	
	$listresult = $result->fetchall_arrayref;
	$ntuples = $result->rows();

};

sub dBaseError {

    my ($check, $message) = @_;
    print "<H4><FONT COLOR=BLACK><P>$message<BR>Error: ".$check->errstr."</FONT></H4>";
    die("Action failed on command:$message  Error_was:$DBI::errstr");
};

sub utf2koi {
my $pvdcoderwin=shift;

$pvdcoderwin=~ s/п�/А/g;
$pvdcoderwin=~ s/п‘/Б/g;
$pvdcoderwin=~ s/п’/В/g;
$pvdcoderwin=~ s/п“/Г/g;
$pvdcoderwin=~ s/п”/Д/g;
$pvdcoderwin=~ s/п•/Е/g;
$pvdcoderwin=~ s/п–/Ж/g;
$pvdcoderwin=~ s/п—/З/g;
$pvdcoderwin=~ s/п˜/И/g;
$pvdcoderwin=~ s/п™/Й/g;
$pvdcoderwin=~ s/пš/К/g;
$pvdcoderwin=~ s/п›/Л/g;
$pvdcoderwin=~ s/пœ/М/g;
$pvdcoderwin=~ s/п�/Н/g;
$pvdcoderwin=~ s/пž/О/g;
$pvdcoderwin=~ s/пŸ/П/g;
$pvdcoderwin=~ s/п /Р/g;
$pvdcoderwin=~ s/п¡/С/g;
$pvdcoderwin=~ s/п¢/Т/g;
$pvdcoderwin=~ s/пe/У/g;
$pvdcoderwin=~ s/п¤/Ф/g;
$pvdcoderwin=~ s/п¥/Х/g;
$pvdcoderwin=~ s/п¦/Ц/g;
$pvdcoderwin=~ s/п§/Ч/g;
$pvdcoderwin=~ s/п¨/Ш/g;
$pvdcoderwin=~ s/п©/Щ/g;
$pvdcoderwin=~ s/п¬/Ь/g;
$pvdcoderwin=~ s/п«/Ы/g;
$pvdcoderwin=~ s/пª/Ъ/g;
$pvdcoderwin=~ s/п­/Э/g;
$pvdcoderwin=~ s/п®/Ю/g;
$pvdcoderwin=~ s/п¯/Я/g;

$pvdcoderwin=~ s/п°/а/g;
$pvdcoderwin=~ s/п±/б/g;
$pvdcoderwin=~ s/п²/в/g;
$pvdcoderwin=~ s/пE/г/g;
$pvdcoderwin=~ s/п´/д/g;
$pvdcoderwin=~ s/пµ/е/g;
$pvdcoderwin=~ s/п¶/ж/g;
$pvdcoderwin=~ s/п·/з/g;
$pvdcoderwin=~ s/п¸/и/g;
$pvdcoderwin=~ s/п¹/й/g;
$pvdcoderwin=~ s/пº/к/g;
$pvdcoderwin=~ s/п»/л/g;
$pvdcoderwin=~ s/п¼/м/g;
$pvdcoderwin=~ s/п½/н/g;
$pvdcoderwin=~ s/п¾/о/g;
$pvdcoderwin=~ s/я€/р/g;
$pvdcoderwin=~ s/я�/с/g;
$pvdcoderwin=~ s/я‚/т/g;
$pvdcoderwin=~ s/яƒ/у/g;
$pvdcoderwin=~ s/я„/ф/g;
$pvdcoderwin=~ s/я…/х/g;
$pvdcoderwin=~ s/я†/ц/g;
$pvdcoderwin=~ s/я‡/ч/g;
$pvdcoderwin=~ s/яˆ/ш/g;
$pvdcoderwin=~ s/я‰/щ/g;
$pvdcoderwin=~ s/яŒ/ь/g;
$pvdcoderwin=~ s/я‹/ы/g;
$pvdcoderwin=~ s/яŠ/ъ/g;
$pvdcoderwin=~ s/я�/э/g;
$pvdcoderwin=~ s/яŽ/ю/g;

$pvdcoderwin=~ s/я�/я/g;
$pvdcoderwin=~ s/п¿/п/g;

return $pvdcoderwin;
}

sub in_list {
my $guy = shift;
my $str = shift;
my $comma = shift;

if (defined($comma)) {
return 1 if ($str eq $guy || $str =~ /^$guy\,/ || $str =~ /\,$guy\,/ || $str =~ /\,$guy$/);
return 0;
} else {
return 1 if ($str eq $guy || $str =~ /^$guy\#/ || $str =~ /\#$guy\#/ || $str =~ /\#$guy$/);
return 0;
}
}
