#drawform.cgi
#!/usr/local/bin/perl
#output form fields that "remember" last state of being.
use CGI;
$cgiobject=new CGI;
$cgiobject->use_named_parameters;

#retrieve cookie data
$cookie_data=$cgiobject->cookie("searchform");
if ($cookie_data)
 { &crumble_cookie;
   $greeting="Welcome back!" }
else { &init;
       $greeting="Hello First Timer!" }

print $cgiobject->header;
print $cgiobject->start_html(-title=>'Search form',-bgcolor=>'white');
print "<H2>$greeting</H2>";
&output_form;
print $cgiobject->end_html;


sub init()
 #initialize form field values
 { $search_term="Enter search term here.";
   $result_style="brief";
   $result_perpage=50;
 }

sub output_form()
 #construct and output the form HTML
 { $theform=$cgiobject->startform(-name=>'searchform',
                                  -method=>'get',
                                  -action=>'/cgi-bin/dosearch.cgi');
   #create text input field
   $theform.="Search: ";
   $theform.=$cgiobject->textfield(-name=>'search_term',
                                   -size=>30,
                                   -default=>$search_term);
                                              
   #create two radio buttons
   $theform.="<BR>Results type:";
   $theform.=$cgiobject->radio_group(-name=>'result_style',
                                     -values=>["brief","verbose"],
                                     -default=>$result_style);
                                     
   #create select box
   $theform.="<BR>Results per page:<BR>";
   $theform.=$cgiobject->scrolling_list(-name=>'result_perpage',
                                        -values=>[25,50,100],
                                        -default=>$result_perpage,
                                        -size=>3,
                                        -multiple=>'false');
   #create submit and reset buttons
   $theform.="<BR><BR>";
   $theform.=$cgiobject->submit(-label=>'Submit');
   $theform.=$cgiobject->reset(-label=>'Clear');
   
   $theform.=$cgiobject->endform;                                                                 
   print $theform
 }      


sub crumble_cookie()
 #parses cookie data into variables and values
 { @vars=split(/\|/,$cookie_data);
   foreach $var (@vars)
    { @pair=split(/=/,$var);
      $evalstr='$'.$pair[0].'=';
      $evalstr.="\"$pair[1]\"";
      eval ($evalstr);
    }
 }