#! /usr/bin/perl
###############################################################################
#
# Simple search interface
#
# Written by Behan Webster <behanw@verisim.com>
# Copyright (c) 1996 by Verisim, Inc.  All rights reserved.
#
###############################################################################


require query;


###########################################################################
# The simple query form
#
# depends on the $display, $frames, $number, $query variables
#
sub simpleQueryForm {
    my ($frame, $query) = @_;

	my @query = split(/\+/, $query);
	my $type  = pop(@query);
	   $query = join(' ', @query);

	my $phrase = "selected" if $type eq "p";
	my $all    = "selected" if $type eq "a";
	my $any    = "selected" if $type eq "o";

	my $summary = "selected" if $display eq "s";
	my $jump    = "selected" if $display eq "j";
	my $compact = "selected" if $display eq "c";

	my $defnum  = ($number > 0) ? $number : "all";
	my $target  = "target=results"  if $frame > 1;
	my $advframe = ($frame == 1) ? 0 : 2;

	background($textBack);

print qq{
<center><form action="$simpleCgi" method=get $target>
  <a href="$homeUrl" target=_top>
  <img $simpleLogo align=left>
  </a>
  <input type=hidden name=f value=$frame>
  <input type=hidden name=p value=1>

<table border=0 cellspacing=0 cellpadding=0>
<tr align=center valign=center>
<td rowspan=2>
  Search for
  <select name=t>
    <option value=p $phrase>this phrase
    <option value=a $all>all these words
    <option value=o $any>any of these words
  </select>
  <input type=text name=q size=20 maxlength=200 value="$query">

  <br><nobr>Display results as
  <select name=d>
    <option value=s $summary> summary list
};

print qq{
    <option value=j $jump> jump list
} if $frame > 1;

print qq{
    <option value=c $compact> compact list
  </select></nobr>
  <nobr>Show
  <input type=text name=n value="$defnum" size=5 maxlength=10>
  entries at once.</nobr>
</td>
<td><input type=submit value=Submit></td>
</tr>

<tr align=center valign=center>
<td><input type=reset value=Clear></td>
</tr>
</table>

  <center>
  	<font color=#555555>simple search</font>
	&nbsp;&nbsp;
    <a href="$powerCgi" target=_top>power search</a>
	&nbsp;&nbsp;
    <a href="$advancedCgi?$advframe">advanced search</a>
  </center>
</form></center>
};

}

###########################################################################
# Parse the command arguments
#
if ($#ARGV<0) {
	if ($ENV{'QUERY_STRING'}) {
		#
		# Read key pairs from the get
		#
		splitPairs($ENV{'QUERY_STRING'});
		$frames  = $FORM{'f'};
		$page    = $FORM{'p'};
		$display = $FORM{'d'};
		$number  = $FORM{'n'};
		$query   = $FORM{'q'};
		$type    = $FORM{'t'};
	}

} else {
	#
	# Read the argument list from the command line
	#
	($frames, $page, $display, $number, @query) = @ARGV;
	$type  =  pop(@query);
	$query =  join(' ', @query);
}


#
# Save the old query for later and do some cleaning up
#
$query    =  unarmorURL($query);
$query    =~ tr/"//d;
$query    =~ tr/ !|&0-9/ /s;
$oldquery =  "$query+$type";
$oldquery =~ tr/ /+/;

#
# Set to defaults if these are undefined
#
$type    = "p"  if !$type;

#
# Put together the query
#
if ($type eq "a") {
	# The format is already correct for this option

} elsif ($type eq "o") {
	$query = join(' | ', split(/ /, $query)) if $query;

} else { # Search for a phrase is the default
	$query = qq{"$query"} if $query;
}


#
# Query the index and print the results
#
resultsPage($frames, $page, $query, $oldquery, $simpleCgi, "simpleQueryForm");

