#!/usr/local/bin/perl

use CGI;
$CGI = new CGI();
print "Content-type:text/html\n\n";
print qq!
<HTML>
<HEAD>
<TITLE>Thanks</TITLE>
</HEAD>
<BODY BGCOLOR = "FFFFFF" TEXT = "000000">
<BLOCKQUOTE>
<FONT FACE = "ARIAL">
Thanks. Keep swingin'.
</FONT>
</BLOCKQUOTE>
</BODY>
</HTML>!;

@params = $CGI->param();

$date = `date`;

@fields = qw(age gender job income country single_or_not
	     time_devoted_per_week skill_level when_started
	     past_exp job_desc free_time num_camps_attended
	     purchase money_spent shoes music styles
	     fav_cds where_info how_got_into_swing);

foreach $param (@fields)
	{
	@value = $CGI->param($param);
	$CGI->param($param, createCommaListFromArray(@value));
	push (@values, $CGI->param($param));
	}
$new_row = join (":", @values);
$new_row .= ":$date";
open (SAVE, ">>./Data/data.file");
print SAVE "$new_row\n";
close (SAVE);

sub createCommaListFromArray
	{
	my (@value) = @_;

	my ($value) = join (", ", @value);
	$value = extractNewLines($value);
	return $value;
	}

sub extractNewLines
        {
        my ($value) = shift;
	$value =~ s/\n/~nl~/g;
        return $value;
        }

require "mail-lib.pl";

if ($CGI->param('client_email'))
	{
	$email_of_sender = $CGI->param('client_email');
	}
else
	{
	$email_of_sender = "selena\@eff.org";
	}

$new_row .= "\n\n";
foreach $param (@fields)
	{
	$new_row .= "$param = " . $CGI->param($param) . "\n";	
	}

$new_row .= "Date: " . $date . "\n";

&send_mail("$email_of_sender","swingstats\@hotmail.com",
           "Swing Survey Results", $new_row);
