#!/usr/bin/perl

use CGI;
use DBI;
unshift(@INC, '.');
use ipac_cfg;

my $q = new CGI;
my $path="https://" . $q->server_name();
my $cgi=$path . "/cgi-bin";             
my $dbh;
my $sth;
my @row;
my $options = "0";
my $user = $ENV{REMOTE_USER};
my $tar_name;

if ($ENV{HTTPS} ne "on" || $user ne "admin") {
    print $q->header(-type=>"text/html;", -status=>'403 Forbidden'),
	    $q->start_html('Forbidden'),
	    $q->h1('You dont have permission to access this script'),
	    print $user, "fuck<P>", $ENV{HTTPS};
	    $q->end_html;
    exit;
}

if ($q->param('tar_name') ne "") {
	print "Location: $path/stat\n\n";
}

print  $q->header,
       $q->start_html(-title=>'Add a tariff');

print "<table><tr><td>".
	"<IMG BORDER=0 ALT=\"ipac-ng logo\"".
	"SRC=\"$path/poweredby.png\"></td><td>".
	"<center><font color=\"#ff0000\" size=6>ipac-ng add tariff</font></center>".
	"</td></tr></table><A HREF=\"http://sf.net/projects/ipac-ng\">".
    	"ipac-ng home page</A>&nbsp;&nbsp;";
print "<A HREF=\"$path/stat\">Go to main page</A>&nbsp;&nbsp;";
print $ipac_cfg::menu;

$dbh = DBI->connect("dbi:Pg:dbname=$ipac_cfg::pg_dbname",                  
                        $ipac_cfg::pg_login, $ipac_cfg::pg_pwd,            
                                     { RaiseError => 1, AutoCommit => 0 });
				     
print $q->startform,
	"New tariff name:&nbsp;", $q->textfield('tar_name'), "&nbsp;",
	$q->submit(),
	$q->endform;

if (!$q->param('tar_name')) {
	$dbh->disconnect  or warn $dbh->errstr;
	print $q->end_html;
	exit;
}

$tar_name=$q->param('tar_name');

$sth = $dbh->prepare("select tariff_name from tariffs_names where tariff_name=?");
$sth->execute($tar_name);
@row=$sth->fetchrow_array;
$sth->finish;
if ($row[0] ne "") {
	print "Error: tariff name '$tar_name' already exist";
	$dbh->disconnect or warn $dbh->errstr;
	print $q->end_html;
	exit;
}
$dbh->do(q{insert into tariffs_names (tariff_name) values (?)}, undef, $tar_name);
$dbh->commit;
$dbh->disconnect  or warn $dbh->errstr;
print $q->end_html;
