#!/usr/bin/perl
use CGI;
use Pg;
use IUSconf;
use WWW;
use strict;

&main();

sub main{
  my $C = new IUSconf;
  my $W = WWW::new($C);

  $W->{'q'} = new CGI;

  PAGE:{
	$W->{'q'}->param('ok_submit') && do{
		&confirmPage($W) and return;
	};

	$W->{'q'}->param('confirm_submit') && do{
		&endPage($W) and return;
	};
  }

  print $W->{'q'}->header(-type=>'text/html; charset="ISO-8859-2"');
  $W->pHTML("magform1.shtml");
}
sub confirmPage{
  my $W = $_[0];

  return undef unless &testFields($W);

  print $W->{'q'}->header(-type=>'text/html; charset="ISO-8859-2"');
  $W->pHTML("magform2.shtml");
  return 1;
}

sub testFields{
  my $W = $_[0];
  $W->{error}='Nie wybrano numeru!' and return undef unless $W->{'q'}->param('numer');
  $W->{error}='Nie wypełniono pola Miasto!' and return undef unless $W->{'q'}->param('miasto');
  $W->{error}='Nie wypełniono pola Imię!' and return undef unless $W->{'q'}->param('imie');
  $W->{error}='Nie wypełniono pola Nazwisko!' and return undef unless $W->{'q'}->param('nazwisko');
  $W->{error}='Nie wypełniono pola Miasto!' and return undef unless $W->{'q'}->param('miasto');
  $W->{error}='Nie wypełniono pola Kod pocztowy!' and return undef unless $W->{'q'}->param('kod');
  $W->{error}='Nie wypełniono pola Adres!' and return undef unless $W->{'q'}->param('adres');
  $W->{error}='Pole E-mail lub Telefon musi być wypełnione!' and return undef unless $W->{'q'}->param('mail') or $W->{'q'}->param('telefon');
  return 1;
}

sub endPage{
  my $W = $_[0];
  return undef unless &testFields($W);
  &sendMail($W); 
  print $W->{'q'}->header(-type=>'text/html; charset="ISO-8859-2"');
  $W->pHTML("magform3.shtml");
  return 1;
}

sub sendMail{
  my $W = $_[0];
  my $subject = "Zamówienie magazynu";
  my $body = '';

  $body .= "Numery: ".$W->{'q'}->param('numer')."\n";
  $body .= "Imię: ".$W->{'q'}->param('imie')."\n";
  $body .= "Nazwisko: ".$W->{'q'}->param('nazwisko')."\n";
  $body .= "Miasto: ".$W->{'q'}->param('miasto')."\n";
  $body .= "Nazwa firmy: ".$W->{'q'}->param('nazwa_firmy')."\n";
  $body .= "Kod pocztowy: ".$W->{'q'}->param('kod')."\n";
  $body .= "Adres: ".$W->{'q'}->param('adres')."\n";
  $body .= "NIP: ".$W->{'q'}->param('nip')."\n";
  $body .= "E-mail: ".$W->{'q'}->param('mail')."\n" if $W->{'q'}->param('mail');
  $body .= "Telefon: ".$W->{'q'}->param('telefon')."\n" if $W->{'q'}->param('telefon');
  $body .= "Uwagi: ".$W->{'q'}->param('uwagi')."\n";
  $W->sendMail('fundacja@iusetlex.pl','fundacja@iusetlex.pl',$subject,$body);
}

