#!/usr/local/bin/perl

use strict;
use CGI qw(param);
my $admin_text_file = "$ENV{DOCUMENT_ROOT}/admin/wordlist.lib";
my $admin_graphic_values_file = "$ENV{DOCUMENT_ROOT}/admin/graphic_values.lib";
my $temp_directory = "/tmp";
my $this_script="/";
my $this_cgi_script = "$ENV{SCRIPT_NAME}";
my $this_url = "http://$ENV{HTTP_HOST}";



if (param('adwording') ne "") {
&analyze_create_report();
}
elsif (param('res') ne "") {
&give_results();
}
else {
&front_page();
}


sub front_page() {


# clean up old sessions which were not deleted
opendir (USER_SESSIONS, "$temp_directory");
my  @oldsessions = grep(/\.adspeak/,readdir(USER_SESSIONS));
  closedir (USER_SESSIONS);
foreach my $oldsess (@oldsessions)
    {
    if (-M "$temp_directory/$oldsess" > .5)
      {
      unlink("$temp_directory/$oldsess");
      }
    }
 # clean up old sessions which were not deleted


print "Content-type: text/html\n\n";

print qq~<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />

<title>Carlin Ad-Speak Calculator</title>
<script type="text/javascript" >
<!--

function verify_words() {

if (document.form.adwording.value == '') {
alert('Please enter your Ad Copy To Test.');
document.form.adwording.focus();
return false;
}
return true;
}
// -->
</script>
<style type="text/css">
body { background-color:white; font: 14px Arial,Helvetica,Tahoma,Verdana,Sans-Serif;}
</style>
</head>
<body>
<div style="background-color:#e6f0f7;border:solid 1px #ccc;width:500px;">
<div style="width:426px;margin: 0 auto;padding:33px 20px 36px 20px;">
<b>The Carlin Ad-Speak Calculator</b> - Measure your copy for the overuse of
jargon, cliches, corporate-speak and unsubstantiated claims.<br /><br />
~;

print qq~<p><b>Test Your Ad Copy:</b></p>
<form method="post" action="$this_cgi_script" >
<textarea name="adwording" rows="8" cols="58" style="width:422px;"></textarea>
<br />
<div style="text-align:center;"><input type="submit" name="work" value="Analyze Your Copy" /></div>

</form>
</div>
</div>
</body>
</html>
   ~;

}


sub analyze_create_report() {

my @words = ();
my $total_words = 0;
my %hit_words = ();
my @hit_word_count = ();
my $hit_word_count = 0;
my $entered_list = param('adwording');
my $cleaned_entered_list = lc($entered_list);
my $phrase_count = 0;
#$cleaned_entered_list =~ s/\n/ /g;
#$cleaned_entered_list =~ s/\r/ /g;
$cleaned_entered_list =~ s/[^A-Z,a-z]//g;
my $hit_word_count_instances=0;
my $entered_list2 = $entered_list;
$entered_list2 =~ s/[^A-Za-z0-9\s+]//g;
my @enteredwords = split(/\W+/, $entered_list2);

foreach my $word (@enteredwords) {
$total_words++;
 }
open (FILE, "$admin_text_file");
while (<FILE>) {
chomp;
my $add_word2 = $_;
$add_word2 =~ s/[^A-Z,a-z]//g;
my $length = length($add_word2);
push @words, lc("$length$_");
}
close(FILE);


 my $word_count = 0;
foreach my $words (sort {$b <=> $a} @words) {
my $checkword = $words;
$checkword =~ s/[^A-Z,a-z]//g;
my $count=0;
while ($cleaned_entered_list =~ /$checkword/i) {
my $words_no_numbers = $words;
$words_no_numbers =~ s/^\d+//;
if ($hit_words{$words_no_numbers} == 0) {
$hit_word_count_instances++;
}
$hit_words{$words_no_numbers}++;

 my @wordswords = split(/\W+/, $words_no_numbers);
foreach my $word (@wordswords) {
$hit_word_count++;
 }
 $cleaned_entered_list =~ s/$checkword//i;

 }


}

my $add_speak_rate = int(($hit_word_count/$total_words)*100);

#if ($add_speak_rate >= 100) {
#$hit_word_count = $total_words;
#$add_speak_rate = 100;
#}

print "Content-type: text/html\n\n";
print qq~<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>Carlin Ad-Speak Calculator</title>
<style type="text/css">
/*body { background-color:white; font-size: 24pt; font-family: arial, sans-serif;}*/
body { background-color:white; font: 14px Arial,Helvetica,Tahoma,Verdana,Sans-Serif; }
h4 { font: 18px  Arial,Helvetica,Tahoma,Verdana,Sans-Serif; font-weight:bold;}
.adrate  { font: 16px  Arial,Helvetica,Tahoma,Verdana,Sans-Serif; font-weight:bold;}
</style>
</head>
<body>
<div style="float:left;width:45%">
<h4>Your Ad-Speak Calculator Results</h4>
~;

print qq~
<p><span class="adrate">Your Ad-Speak Rate:</span> <b>$add_speak_rate\%</b></p>

<p>We found <b>$hit_word_count_instances</b> instance~;
print "s" if ($hit_word_count_instances > 1);
print qq~ of ad-speak related keywords or phrases.</p>
<p>Ad-speak calculation performed on $total_words word~;
print "s" if ($total_words > 1);
print qq~.</p>
~;
if ($hit_word_count_instances > 0) {
print qq~
We found the following ad-speak keywords or phrases:
 <div style="margin-left:30px;padding-top:5px;font-weight:bold;">
 ~;

 foreach my $wd (sort (keys %hit_words)) {
 print qq~$wd<br />\n~;
 }

 print qq~</div>
 ~;
 }
 print qq~
</div>
<div style="float:left; width:45%">
 <div style="width:325px;">~;
 if ($add_speak_rate > 10) {
 print qq~<img src="./images/dial-extreme.jpg" width="323" height="215" alt="Extreme" />~;
 }
 elsif ($add_speak_rate >= 6) {
 print qq~<img src="./images/dial-high.jpg" width="323" height="215" alt="High" />~;
 }
 elsif ($add_speak_rate > 2) {
 print qq~<img src="./images/dial-moderate.jpg" width="323" height="215" alt="Moderate" />~;
 }
 else {
 print qq~<img src="./images/dial-low.jpg" width="323" height="215" alt="Low" />~;
 }

 print qq~<div style="background-color:#e6f0f7;border:solid 1px #ccc;width:300px;padding:5px;margin: 20px auto;text-align:left;">
 <span class="adrate">Ad-Speak Ratings Explained</span><br />
 <div style="margin-left:20px;"><b>Low:</b> 2\% or less<br />
 <b>Moderate:</b> 3 - 5\%<br />
 <b>High:</b> 6 - 10\%<br />
 <b>Extreme:</b> more than 10%</div>
 </div>
</div>
</div>
</body>
</html>
 ~;



#print "Location: $this_url$this_script?re=1\n\n";

}

exit;
1;