#!/usr/bin/perl

#============================================================================
	use strict;
	use Mail::Sendmail;
	use CGI qw(-no_xhtml);
	#use CGI::Carp('fatalsToBrowser');
	use lib q(./lib);
	use config;
	use cookie;
	use io;
	use html;
	my $cgi = cgi();
	main();
#============================================================================
sub main {
	
	### init
	my $mailman = undef;
	config::env();

	### do and fix
	$mailman = mailman() if($cgi->param('beskjed'));

	### html top
	html::head($cgi);

	### html content
	if(!$cgi->param('sub')) {
		print io::template(_file=>'top');
		if(defined $mailman) {
			print io::template(
					_file   => 'mailman',
					MAILERR => $ENV{MAILERR},
				);
		}
		else {
			print io::template(_file=>'default');
		}
		print io::template(_file=>'kontakt');

		print io::template(_file=>'vidivici');
		print io::template(_file=>'ivko');
		print io::template(_file=>'danisheyewear');
		print io::template(_file=>'traction');
		print io::template(_file=>'tiara');
		print io::template(_file=>'grosfilley');

		print io::template(_file=>'bottom');
	}
	elsif($cgi->param('sub') eq 'kontakt') {
	}

	### html bottom
	html::footer($cgi);

	### the end
	exit 0;
}
#============================================================================
# HELPER SUBS
#============================================================================
sub mailman {

	### init
	my %recievers = (
		#_generelt => 'nina@brilleagenten.no',
		_default => 'nina@brilleagenten.no',
		#_default => 'jht@gktv.no',
		nina     => 'nina@brilleagenten.no',
	);
	my %mail = (
		Smtp    => "mail.gktv.no",
		Subject => "fra brilleagenten.no",
	);
	my $mottaker = $cgi->param('mottaker') || '';

	### check sender
	if($cgi->param('epost') =~ /\w+\@\w+/) {
		$mail{To}      = ($recievers{$mottaker}) ? $recievers{$mottaker}: $recievers{_default};
		$mail{From}    = $cgi->param('epost');
		$mail{Message} = "\n\n"
			.$cgi->param('navn')
			." ("
			.$cgi->param('epost')
			.") har sendt denne meldingen: \n\n"
			.$cgi->param('beskjed');

		### the end
		if(sendmail %mail) {
			return 1;
		}
		else {
			$ENV{MAILERR} = $Mail::Sendmail::error;
			return 0;
		}
	}

	### the sad end
	else {
		$ENV{MAILERR} = "Du må fylle inn en korrekt epostaddresse";
		return 0;
	}
}
#============================================================================
sub cgi {

	### init
	$cgi = new CGI;
	$cgi->append(
		-name   => '_style',
		-values => config::head('style',  $cgi->param('sub'))
	);
	$cgi->append(
		-name   => '_script',
		-values => config::head('script', $cgi->param('sub'))
	);

	### the end
	return $cgi
}
#============================================================================
1;

