#!/usr/bin/perl
#
# BNCweb: A user-friendly interface to the British National Corpus - Version 4.3
# Copyright (C) 2005-10 Hoffmann/Evert/Lehmann
#
# Updates and information available at http://www.bncweb.info
#
# This file is part of BNCweb.
# 
# BNCweb is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
# 
# BNCweb is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

#
# based on BNCweb Version 2.0:
# © 1996/2004 Lehmann/Hoffmann/Schneider
#

use lib "../lib_files";
use bncLibCQPXML;
use DBI;
use CGI;
use bncConfigXML;

$username = $ENV{'REMOTE_USER'};

unless (getParams($ENV{'QUERY_STRING'})) {
	barkFull("incomplete transmition of URL<BR>This problem is likely to occur in combination with proxy servers<BR>turn off your web-proxy and try again");
}

setParam("theQuery", "search") unless ($params{theQuery});

if ($username=~m/[^\w\.]/) {
	barkFull("BNCweb is not compatible with usernames that contain special characters (e.g. \"@\"). Please contact your system administrator and ask for a compatible username.");
}

$bncHandle = DBI->connect("DBI:mysql:$bwMYSQLtable:$bwMYSQLhost:$bwMysqlPort", "$bwMysqlUser", "$bwMysqlPwd") or  $mysql = 0;

if ($bwMysqlAuth) {
	$authLevel = &checkAuthLevel;
} else {
	$authLevel = 1;
}

if ($params{theQuery}) {
	# put default settings into database in case user performs a query for the first time
	$sqlQuery = "SELECT * FROM settings where user='$username'";
	$sth= $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
	$res = $sth->execute or bark("Can't execute $sqlQuery: " . $bncHandle::errstr);
	$res = $sth->rows;
	
	if (!$res) {
		$sqlQuery = "INSERT INTO settings (user, disp_tags, disp_lines, coll_warning, database_col, crlf, statType, 
			delRare, totBNC, `range`, minusrange, disp_color, show_value, view, new_features, markup, historyView, showRandom) 
			VALUES ('$username', 0, 10, 1, 4, 0, 6, 5, 5, 3, -3, 0, 'yes', 'list', 0, 1, 'simple', 'no')";
		$bncHandle->do($sqlQuery) or bark("Can't insert $sqlQuery: " . $bncHandle::errstr);
		$view = "list";
		# set $seen to 0 if you want newfeatures to be displayed for new users
		$seen=1;
	} else {
		# get default view (kwic or list) for display
		@rowArray=$sth->fetchrow_array;
		$view = $rowArray[13];
		$seen = $rowArray[14];
	}
#	if (!$seen) {
#		setParam("theQuery", "newfeatures");
#		$sqlQuery = "UPDATE settings SET new_features=1 WHERE user='$username'";
#		$bncHandle->do($sqlQuery) or bark("Can't update $sqlQuery: " . $bncHandle::errstr);
#	}
} else {
	$view="list";
}

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

print <<END;

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>

<HEAD>
	<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
	<TITLE>BNCweb (CQP-Edition)</TITLE>
</HEAD>
END
print <<'END';

<BODY BGCOLOR="#FFFFFF">
<P>
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">
	<TR VALIGN=TOP>
		<TD>
			<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="6" WIDTH="100%">
				<TR>
					<TD WIDTH="100%" HEIGHT="45" BGCOLOR="#CCCCFF" >
						&nbsp;&nbsp;<font size="4"><b>Main menu</b></font>
					</TD>
				</TR>
			</TABLE>
		</TD>
		<TD>
			<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="6" WIDTH="100%">
				<TR>
					<TD WIDTH="100%" HEIGHT="45" ALIGN="CENTER" VALIGN="MIDDLE" BGCOLOR="#CCCCFF" NOWRAP>
						<font size="5"><B>BNC<I>web</I> (CQP-Edition)</B></font>
					</TD>
				</TR>
			</TABLE>
		</TD>
	</TR>
	<TR>
		<TD VALIGN="TOP" BGCOLOR="#FFFFFF">
			<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="6" WIDTH="100%">
				<TR>
					<TD HEIGHT="22"  NOWRAP BGCOLOR="#99CC99">&#160;&#160;<B>Query options</B></TD>
				</TR>
END
if ($params{theQuery} eq 'search'){
	print '<TR><TD BGCOLOR="#BBBBBB" NOWRAP>&#160;&#160;&#160;Standard query</TD></TR>';
} else {
	print "<TR><TD BGCOLOR=\"#DDDDDD\" NOWRAP>&#160;&#160;&#160;<A HREF=\"/$bwCGIalias/BNCquery.pl?theQuery=search&urlTest=yes\">Standard query</A></TD></TR>\n";
}

if ($params{theQuery} eq 'writtentexts') {
	print '<TR><TD BGCOLOR="#BBBBBB" NOWRAP>&#160;&#160;&#160;Written restrictions</TD></TR>';
} else {
	print "<TR><TD BGCOLOR=\"#DDDDDD\" NOWRAP>&#160;&#160;&#160;<A HREF=\"/$bwCGIalias/BNCquery.pl?theQuery=writtentexts&urlTest=yes\">Written restrictions</A></TD></TR>\n";
}

if ($params{theQuery} eq 'spokentexts') {
	print '<TR><TD BGCOLOR="#BBBBBB" NOWRAP>&#160;&#160;&#160;Spoken restrictions</TD></TR>';
} else {
	print "<TR><TD BGCOLOR=\"#DDDDDD\" NOWRAP>&#160;&#160;&#160;<A HREF=\"/$bwCGIalias/BNCquery.pl?theQuery=spokentexts&urlTest=yes\">Spoken restrictions</A></TD></TR>\n";
}


if ($params{theQuery} eq 'databjkgls') {
	print '<TR><TD BGCOLOR="#BBBBBB" NOWRAP>&#160;&#160;&#160;</TD></TR>' ."\n";
} else {
	print '<TR><TD HEIGHT="22"  NOWRAP BGCOLOR="#FFCC99">&#160;&#160;<B>User-specific functions</B></TD></TR>';
}

if ($params{theQuery} eq 'usersettings') {
	print '<TR><TD BGCOLOR="#BBBBBB" NOWRAP>&#160;&#160;&#160;User settings</TD></TR>';
} else {
	print "<TR><TD BGCOLOR=\"#DDDDDD\" NOWRAP>&#160;&#160;&#160;<A HREF=\"/$bwCGIalias/BNCquery.pl?theQuery=usersettings&urlTest=yes\">User settings</A></TD></TR>\n";
}

if ($params{theQuery} eq 'history') {
	print '<TR><TD BGCOLOR="#BBBBBB" NOWRAP>&#160;&#160;&#160;Query history</TD></TR>';
} else {
	print "<TR><TD BGCOLOR=\"#DDDDDD\" NOWRAP>&#160;&#160;&#160;<A HREF=\"/$bwCGIalias/BNCquery.pl?theQuery=history&user=$username&chunk=1&urlTest=yes\">Query history</A></TD></TR>\n";
}
if ($params{theQuery} eq 'savedQueries') {
	print '<TR><TD BGCOLOR="#BBBBBB" NOWRAP>&#160;&#160;&#160;Saved queries</TD></TR>';
} else {
	print "<TR><TD BGCOLOR=\"#DDDDDD\" NOWRAP>&#160;&#160;&#160;<A HREF=\"/$bwCGIalias/BNCquery.pl?theQuery=savedQueries&user=$username&chunk=1&urlTest=yes\">Saved queries</A></TD></TR>\n";
}
if ($params{theQuery} eq 'categorizedQueries') {
	print '<TR><TD BGCOLOR="#BBBBBB" NOWRAP>&#160;&#160;&#160;Categorized queries</TD></TR>';
} else {
	print "<TR><TD BGCOLOR=\"#DDDDDD\" NOWRAP>&#160;&#160;&#160;<A HREF=\"/$bwCGIalias/BNCquery.pl?theQuery=categorizedQueries&user=$username&chunk=1&urlTest=yes\">Categorized queries</A></TD></TR>\n";
}

if ($params{theQuery} eq 'subcorpus') {
	print '<TR><TD BGCOLOR="#BBBBBB" NOWRAP>&#160;&#160;&#160;Make/edit subcorpora</TD></TR>';
} else {
	print "<TR><TD BGCOLOR=\"#DDDDDD\" NOWRAP>&#160;&#160;&#160;<A HREF=\"/$bwCGIalias/BNCquery.pl?theQuery=subcorpus&urlTest=yes\">Make/edit subcorpora</A></TD></TR>\n";
}

if ($params{theQuery} eq 'upload') {
	print '<TR><TD BGCOLOR="#BBBBBB" NOWRAP>&#160;&#160;&#160;Upload external data file</TD></TR>';
} else {
	print "<TR><TD BGCOLOR=\"#DDDDDD\" NOWRAP>&#160;&#160;&#160;<A HREF=\"/$bwCGIalias/BNCquery.pl?theQuery=upload&urlTest=yes\">Upload external data file</A></TD></TR>\n";
}


print <<'END';
				
	<TR>
		<TD HEIGHT="22"  NOWRAP BGCOLOR="#CC99FF">&#160;&#160;<B>Additional functions</B></TD>
	</TR>
END


if ($params{theQuery} eq 'browse') {
	print '<TR><TD BGCOLOR="#BBBBBB" NOWRAP>&#160;&#160;&#160;Browse a text</TD></TR>';
} else {
	if (!$authLevel) {
		print "<TR><TD BGCOLOR=\"#DDDDDD\" NOWRAP>&#160;&#160;&#160;Browse a text (disabled)</TD></TR>\n";
	} else {
		print "<TR><TD BGCOLOR=\"#DDDDDD\" NOWRAP>&#160;&#160;&#160;<A HREF=\"/$bwCGIalias/BNCquery.pl?theQuery=browse&urlTest=yes\">Browse a text</A></TD></TR>\n";
	}
}

# if ($params{theQuery} eq 'lookup') {
# 	print '<TR><TD BGCOLOR="#BBBBBB" NOWRAP>&#160;&#160;&#160;Word lookup</TD></TR>';
# } else {
# 	print "<TR><TD BGCOLOR=\"#DDDDDD\" NOWRAP>&#160;&#160;&#160;<A HREF=\"/$bwCGIalias/BNCquery.pl?theQuery=lookup&urlTest=yes\">Word lookup</A></TD></TR>\n";
# }


if ($params{theQuery} eq 'keyword') {
	print '<TR><TD BGCOLOR="#BBBBBB" NOWRAP>&#160;&#160;&#160;Scan keywords/titles</TD></TR>';
} else {
	print "<TR><TD BGCOLOR=\"#DDDDDD\" NOWRAP> &#160;&#160;&#160;<A HREF=\"/$bwCGIalias/BNCquery.pl?theQuery=keyword&urlTest=yes\">Scan keywords/titles</A></TD></TR>\n";
}

if ($params{theQuery} eq 'genres') {
	print '<TR><TD BGCOLOR="#BBBBBB" NOWRAP>&#160;&#160;&#160;Explore genre labels</TD></TR>';
} else {
	print "<TR><TD BGCOLOR=\"#DDDDDD\" NOWRAP> &#160;&#160;&#160;<A HREF=\"/$bwCGIalias/BNCquery.pl?theQuery=genres&urlTest=yes\">Explore genre labels</A></TD></TR>\n";
}

if ($params{theQuery} eq 'frequency') {
	print '<TR><TD BGCOLOR="#BBBBBB" NOWRAP>&#160;&#160;&#160;Frequency lists</TD></TR>';
} else {
	print "<TR><TD BGCOLOR=\"#DDDDDD\" NOWRAP> &#160;&#160;&#160;<A HREF=\"/$bwCGIalias/BNCquery.pl?theQuery=frequency&urlTest=yes\">Frequency lists</A></TD></TR>\n";
}
if ($params{theQuery} eq 'keywords_stat') {
	print '<TR><TD BGCOLOR="#BBBBBB" NOWRAP>&#160;&#160;&#160;Keywords</TD></TR>';
} else {
	print "<TR><TD BGCOLOR=\"#DDDDDD\" NOWRAP> &#160;&#160;&#160;<A HREF=\"/$bwCGIalias/BNCquery.pl?theQuery=keywords_stat&urlTest=yes\">Keywords</A></TD></TR>\n";
}

if (($username=~m/\A($bwSuperuser)\Z/) || (&checkSuperUser2())) {
	print '<TR><TD HEIGHT="22"  NOWRAP BGCOLOR="#CCCC99">&#160;&#160;<B>Admin features</B></TD></TR>';
	
	if ($params{theQuery} eq 'adminStatus') {
		print '<TR><TD BGCOLOR="#BBBBBB" NOWRAP>&#160;&#160;&#160;Manage admin access</TD></TR>';
	} else {
		print "<TR><TD BGCOLOR=\"#DDDDDD\" NOWRAP>&#160;&#160;&#160;<A HREF=\"/$bwCGIalias/BNCquery.pl?theQuery=adminStatus&chunk=1&urlTest=yes\">Manage admin access</A></TD></TR>\n";
	}
	if ($params{theQuery} eq 'usageStatistics') {
		print '<TR><TD BGCOLOR="#BBBBBB" NOWRAP>&#160;&#160;&#160;Usage statistics</TD></TR>';
	} else {
		print "<TR><TD BGCOLOR=\"#DDDDDD\" NOWRAP>&#160;&#160;&#160;<A HREF=\"/$bwCGIalias/BNCquery.pl?theQuery=usageStatistics&chunk=1&orderBy=numberQueries&urlTest=yes\">Usage statistics</A></TD></TR>\n";
	}
	if ($params{theQuery} eq 'userLimits') {
		print '<TR><TD BGCOLOR="#BBBBBB" NOWRAP>&#160;&#160;&#160;User limits</TD></TR>';
	} else {
		print "<TR><TD BGCOLOR=\"#DDDDDD\" NOWRAP>&#160;&#160;&#160;<A HREF=\"/$bwCGIalias/BNCquery.pl?theQuery=userLimits&chunk=1&urlTest=yes\">User limits</A></TD></TR>\n";
	}
	if ($params{theQuery} eq 'cachedQueries') {
		print '<TR><TD BGCOLOR="#BBBBBB" NOWRAP>&#160;&#160;&#160;Cached queries</TD></TR>';
	} else {
		print "<TR><TD BGCOLOR=\"#DDDDDD\" NOWRAP>&#160;&#160;&#160;<A HREF=\"/$bwCGIalias/BNCquery.pl?theQuery=cachedQueries&chunk=1&urlTest=yes\">Cached queries</A></TD></TR>\n";
	}
	if ($params{theQuery} eq 'cachedMySQLtables') {
		print '<TR><TD BGCOLOR="#BBBBBB" NOWRAP>&#160;&#160;&#160;Cached MySQL tables</TD></TR>';
	} else {
		print "<TR><TD BGCOLOR=\"#DDDDDD\" NOWRAP>&#160;&#160;&#160;<A HREF=\"/$bwCGIalias/BNCquery.pl?theQuery=cachedMySQLtables&chunk=1&urlTest=yes\">Cached MySQL tables</A></TD></TR>\n";
	}
	if ($params{theQuery} eq 'cachedFrequencyLists') {
		print '<TR><TD BGCOLOR="#BBBBBB" NOWRAP>&#160;&#160;&#160;Cached frequency lists</TD></TR>';
	} else {
		print "<TR><TD BGCOLOR=\"#DDDDDD\" NOWRAP>&#160;&#160;&#160;<A HREF=\"/$bwCGIalias/BNCquery.pl?theQuery=cachedFrequencyLists&chunk=1&urlTest=yes\">Cached frequency lists</A></TD></TR>\n";
	}
	
	
}

print <<'END';

	<TR>
		<TD HEIGHT="22"  NOWRAP BGCOLOR="#FF9999">&#160;&#160;<B>About BNCweb</B></TD>
	</TR>
END

if ($params{theQuery} eq 'book') {
	print '<TR><TD BGCOLOR="#BBBBBB" NOWRAP>&#160;&#160;&#160;<font color="red"><B><i>New:</i></B></font> BNCweb book</TD></TR>';
} else {
	print "<TR><TD BGCOLOR=\"#DDDDDD\" NOWRAP>&#160;&#160;&#160;<font color=\"red\"><B><i>New:</i></B></font> <A HREF=\"/$bwCGIalias/BNCquery.pl?theQuery=book&urlTest=yes\">BNCweb book</A></TD></TR>\n";
}

if ($params{theQuery} eq 'team') {
	print '<TR><TD BGCOLOR="#BBBBBB" NOWRAP>&#160;&#160;&#160;The BNCweb team</TD></TR>';
} else {
	print "<TR><TD BGCOLOR=\"#DDDDDD\" NOWRAP>&#160;&#160;&#160;<A HREF=\"/$bwCGIalias/BNCquery.pl?theQuery=team&urlTest=yes\">The BNCweb team</A></TD></TR>\n";
}

if ($params{theQuery} eq 'newfeatures') {
	print '<TR><TD BGCOLOR="#BBBBBB" NOWRAP>&#160;&#160;&#160;New features</TD></TR>';
} else {
	print "<TR><TD BGCOLOR=\"#DDDDDD\" NOWRAP>&#160;&#160;&#160;<A HREF=\"/$bwCGIalias/BNCquery.pl?theQuery=newfeatures&urlTest=yes\">New features</A></TD></TR>\n";
}

if ($params{theQuery} eq 'bugreports') {
	print '<TR><TD BGCOLOR="#BBBBBB" NOWRAP>&#160;&#160;&#160;Bug reports</TD></TR>';
} else {
	print "<TR><TD BGCOLOR=\"#DDDDDD\" NOWRAP>&#160;&#160;&#160;<A HREF=\"/$bwCGIalias/BNCquery.pl?theQuery=bugreports&urlTest=yes\">Bug reports</A></TD></TR>\n";
}
if ($params{theQuery} eq 'tags') {
	print '<TR><TD BGCOLOR="#BBBBBB" NOWRAP>&#160;&#160;&#160;The CLAWS-5 tagset</TD></TR>';
} else {
	print '<TR><TD BGCOLOR="#DDDDDD" NOWRAP>&#160;&#160;&#160;<A HREF="http://ucrel.lancs.ac.uk/bnc2/bnc2guide.htm#tagset">The CLAWS-5 tagset</A>&#160;&#160;</TD></TR>';
}

if ($params{theQuery} eq 'oxford') {
	print '<TR><TD BGCOLOR="#BBBBBB" NOWRAP>&#160;&#160;&#160;Oxford BNC homepage</TD></TR>';
} else {
	print '<TR><TD BGCOLOR="#DDDDDD" NOWRAP>&#160;&#160;&#160;<A HREF="http://info.ox.ac.uk/bnc/">Oxford BNC homepage</A>&#160;&#160;</TD></TR>';
}

print  <<'END';
			<TR><TD BGCOLOR="#FFFFFF" NOWRAP><small><BR>&nbsp; Version 4.3, January 2010</small></TD></TR>
			</TABLE>
		</TD>
		<TD VALIGN="TOP">
END
if ($params{theQuery} eq 'word') {

	word($params);
	
} elsif ($params{theQuery} eq 'wordspoken') {

	wordspoken($params);
	
} elsif ($params{theQuery} eq 'wordwritten') {

	wordwritten($params);
	
} elsif ($params{theQuery} eq 'search') {

	search($params);
	
} elsif ($params{theQuery} eq 'history') {

	history($params);
	
} elsif ($params{theQuery} eq 'lookup') {

	lookup($params);

} elsif ($params{theQuery} eq 'browse') {

	browse($params);

} elsif ($params{theQuery} eq 'spokentexts') {

	spokentexts($params);

} elsif ($params{theQuery} eq 'writtentexts') {

	writtentexts($params);

} elsif ($params{theQuery} eq 'usersettings') {

	usersettings($params);

} elsif ($params{theQuery} eq 'newfeatures') {

	newfeatures($params);

} elsif ($params{theQuery} eq 'keyword') {

	keywords($params);

} elsif ($params{theQuery} eq 'subcorpus') {

	subcorpus($params);

} elsif ($params{theQuery} eq 'frequency') {

	frequency($params);

} elsif ($params{theQuery} eq 'keywords_stat') {

	keywords_stat($params);

} elsif ($params{theQuery} eq 'genres') {

	genres($params);

} elsif ($params{theQuery} eq 'savedQueries') {

	savedQueries($params);

} elsif ($params{theQuery} eq 'upload') {

	upload($params);

} elsif ($params{theQuery} eq 'categorizedQueries') {

	categorizedQueries($params);

} elsif ($params{theQuery} eq 'team') {

	team($params);

} elsif ($params{theQuery} eq 'bugreports') {

	bugreports($params);

} elsif ($params{theQuery} eq 'cachedQueries') {

	cachedQueries($params);

} elsif ($params{theQuery} eq 'cachedMySQLtables') {

	cachedMySQLtables($params);

} elsif ($params{theQuery} eq 'cachedFrequencyLists') {

	cachedFrequencyLists($params);

} elsif ($params{theQuery} eq 'usageStatistics') {

	usageStatistics($params);

} elsif ($params{theQuery} eq 'userLimits') {

	userLimits($params);

} elsif ($params{theQuery} eq 'adminStatus') {
	adminStatus($params);

} elsif ($params{theQuery} eq 'book') {
	book($params);

} else {

	print $bottomRight;
	
}
print <<END2;

				</TD>
		</TR>
</TABLE>


<script language="JavaScript" type="text/javascript" src="/$bwHTMLalias/wz_tooltip.js"></script>

</BODY>

</HTML>
END2

exit(0);



sub search {
	$sqlQuery = "SELECT username, subcorpus, numfiles, numwords, spowri FROM subcorpora WHERE username='$username' ORDER BY subcorpus";
	$sth1 = $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
	$sth1->execute or bark("Can't execute $sqlQuery: $sth::errstr");
	$res = $sth1->rows;		
		
	if ($params{querySelect} eq "simple") {
		$selected{1} = "SELECTED";
	} elsif ($params{querySelect} eq "CQP") {
		$selected{0} = "SELECTED";
	} elsif ($params{querySelect} eq "simpleC") {
		$selected{2} = "SELECTED";
	} else {
		$selected{1} = "SELECTED";
	}
	
	#subcorpSelect
	
	if ($params{subcorpSelect}) {
		$selected2{$params{subcorpSelect}} = "SELECTED";
	}
	
	$subcorpusShow = "";
	$lastSelection = "";
	if ($res) {
	    for ($i = 0; $i < $res; $i++) {
	    	@rowArray=$sth1->fetchrow_array;
			$theName=$rowArray[0];
			$subcorpus=$rowArray[1];
			$numfiles=$rowArray[2];
			$numwords=$rowArray[3];
			$spowri=$rowArray[4];
			$numwords=&makeDecimals($numwords);
			unless ($subcorpus=~m/\Alast_(spoken|written)\Z/) {
				if ($spowri eq "written") {
					$subcorpusShow .= "<OPTION VALUE = \"$subcorpus\" $selected2{$subcorpus}>Subcorpus: $subcorpus ($numwords words, $numfiles files)\n";
				} else {
					$subcorpusShow .= "<OPTION VALUE = \"$subcorpus\" $selected2{$subcorpus}>Subcorpus: $subcorpus ($numwords words, $numfiles speakers)\n";
				}
			} else {
				if ($spowri eq "written") {
					$lastSelection .= "<OPTION VALUE = \"$subcorpus\">Last written restrictions ($numwords words, $numfiles files)\n";
				} else {
					$lastSelection .= "<OPTION VALUE = \"$subcorpus\">Last spoken restrictions ($numwords words, $numfiles speakers)\n";
				}
			}
	    }
			
		
	} 
	
#3	$showMessage = "<h3>I've already sent you two e-mails asking you to send me the consistency issues file.  Could you please finally do so?! Pulling out
#of the book is one thing, but hindering me to finalize things is another. <BR> Sebastian</h3>"  if ($username eq "davidlee");
#	$showMessage = "<h3>BNCweb is currently being upgraded and may be unavailable (or function only partially) for the next few hours.</h3>";
	
	
	print <<HERE;

				<P><FORM ACTION="/$bwCGIalias/processQuery.pl" METHOD="GET"> 
			<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="6" WIDTH="100%">
				<TR>
					<TD WIDTH="100%" HEIGHT="22" BGCOLOR="#99CC99">
						<H3 ALIGN="CENTER"><B>Standard Query</B>
					</TD>
				</TR>
				<TR>
					<TD WIDTH="100%" >
						<TEXTAREA NAME="theData" ROWS="5" COLS="55" WRAP="physical" STYLE="font-size: 16px;">$params{insertQuery}</TEXTAREA>
						<INPUT TYPE="HIDDEN" NAME="chunk" SIZE="-1" VALUE="1">
						<INPUT TYPE="HIDDEN" NAME="queryType" SIZE="-1" VALUE="CQL">
						<SPACER TYPE="HORIZONTAL" SIZE="10"> 
						<SPACER TYPE="VERTICAL" SIZE="10">
					</TD>
				</TR>
				<TR>
					<TD COLSPAN="2">
						<font size="1">&nbsp;</font>
					</TD>
				</TR>
				
				<TR>
					<TD WIDTH="100%">
						<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
							<TR>
								<TD ALIGN="RIGHT">
									<P>Query mode:&#160;&#160;
								</TD>
								<TD VALIGN = "TOP">									
									<SELECT NAME="qMode">
										<OPTION $selected{0}>CQP syntax</OPTION>
										<OPTION $selected{1}>Simple query (ignore case)</OPTION>
										<OPTION $selected{2}>Simple query (case-sensitive)</OPTION>
									</SELECT><font color="#EEEEEE" onmouseover="return escape('Display query syntax tips here?')">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>
									<font color="grey"><A HREF="/$bwHTMLalias/Simple_query_language.pdf">Simple Query Syntax help</A></font>
								</TD>
							</TR>

							<TR>
								<TD NOWRAP ALIGN="RIGHT" VALIGN = "TOP">
									<P>Number of hits per page:&#160;&#160;
								</TD>
								<TD VALIGN = "TOP">									
									<SELECT NAME="inst">
										<OPTION>count hits</OPTION>
										<OPTION>10 </OPTION>
										<OPTION SELECTED>50 </OPTION>
										<OPTION>100 </OPTION>
										<OPTION>250 </OPTION>
										<OPTION>350 </OPTION>
										<OPTION>500 </OPTION>
										<OPTION>1000 </OPTION>
									</SELECT>
								</TD>
							</TR>
							<TR>
								<TD ALIGN="RIGHT">
									<P>
									
									<INPUT TYPE="HIDDEN" NAME="max" SIZE="-1" VALUE="INIT"> 
									<INPUT TYPE="HIDDEN" NAME="qname" SIZE="-1" VALUE="INIT"> 
									<INPUT TYPE="HIDDEN" NAME="thMode" SIZE="-1" VALUE="INIT">
									<INPUT TYPE="HIDDEN" NAME="thin" SIZE="-1" VALUE="0">
									Restriction:&#160;&#160;
								</TD>
								<TD VALIGN = "TOP">
									
 									<SELECT NAME="qtype">
										<OPTION VALUE="0" SELECTED>None (search whole corpus) </OPTION>
										<OPTION VALUE="1">Written Texts </OPTION>
										<OPTION VALUE="2">Spoken Texts </OPTION>
										$lastSelection
										$subcorpusShow
									</SELECT>
								</TD>
								<INPUT TYPE="hidden" NAME="view" VALUE="$view">
							</TR>
				<TR>
					<TD COLSPAN="2">
						&nbsp;
					</TD>
				</TR>
							<TR>
								<TD>&#160;</TD>
								<TD>
									
									<INPUT TYPE="SUBMIT" name="theAction" VALUE="Start Query">
									<INPUT TYPE="RESET" VALUE="Reset Query">
									<INPUT TYPE="hidden" NAME="urlTest" VALUE="yes">
								</TD>
							</TR>
						</TABLE>
						$showMessage
						<HR>
					$bwCopyright</TD>
				</TR>
			</TABLE> 
		</FORM>
			
HERE

}

sub history {

	$inst=50;
	
	# connect to mysql
	$bncHandle = DBI->connect("DBI:mysql:$bwMYSQLtable:$bwMYSQLhost:$bwMysqlPort", "$bwMysqlUser", "$bwMysqlPwd") or bark("Can't connect to mysql on $bwMYSQLhost at port: $bwMysqlPort <BR>$bncHandle::errstr\n");
	
	
	$historyView = $params{historyView};
	
	$begin=(($params{chunk} - 1) * $inst);
	$end=(($params{chunk} * $inst) - 1);

	unless ($historyView) {
		$sqlQuery = "SELECT historyView FROM settings where user='$username'";
		$sth= $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
		$sth->execute or bark("Can't execute $sqlQuery: " . $sth::errstr);
		$rows = $sth->rows;
		if ($rows) {
			@rowArray=$sth->fetchrow_array;
			$historyView=$rowArray[0];
		} else {
			$historyView="simple";
		}
	}
	$actualUsername = $username;
	
	if ($params{user} ne $username) {
		$username=$params{user} if (($username=~m/\A($bwSuperuser)\Z/) || (&checkSuperUser2()));
	}
		
	if ($historyView eq "simple") {
		$queryViewBut = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=history&user=' . $username . '&chunk=' . $params{chunk} . '&historyView=CQP&urlTest=yes">(Show in CQP Syntax)</A>';
	} else {
		$queryViewBut = '&nbsp;&nbsp;<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=history&user=' . $username . '&chunk=' . $params{chunk} . '&historyView=simple&urlTest=yes">(Show as simple query)</A>';
	}
	
	print <<END;
	
	<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="6" WIDTH="100%">
		<TR>
			<TD WIDTH="100%" HEIGHT="22" BGCOLOR="#FFCC99">
				<H3 ALIGN="CENTER"><B>Query history</B>
			</TD>
		</TR>
		<TR>
			<TD WIDTH="100%">
END
	if (($actualUsername=~m/\A($bwSuperuser)\Z/) || (&checkSuperUser2())) {
		$sqlQuery = "SELECT distinct(user) FROM history order by user";
		$sth1= $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
		$sth1->execute or bark("Can't execute $sqlQuery: $sth1::errstr");
		$res=$sth1->rows;

		print <<END;
			<TABLE BORDER=0 WIDTH=100%>
				<TR>
					<TD WIDTH=20% HEIGHT="30">
						<FORM ACTION="/$bwCGIalias/BNCquery.pl" METHOD="GET">
						<INPUT TYPE="HIDDEN" NAME="theQuery" VALUE="history" SIZE="-1">
						<B>User:</B>
					</TD>
					<TD WIDTH=30%>
						<SELECT NAME="user">
							<OPTION VALUE="all">  all users
							<OPTION VALUE="synErr">  only errors
END
		for ($i = 0; $i < $res; $i++) {
			@mysqlArray=$sth1->fetchrow_array;
			$theUser=$mysqlArray[0];
			print "<OPTION>$theUser\n";
		}
		print <<END;
						</SELECT>
					</TD>
					<TD>
						<INPUT TYPE="HIDDEN" NAME="chunk" SIZE="-1" VALUE="1">
						<INPUT TYPE="hidden" NAME="urlTest" VALUE="yes">
						<INPUT TYPE="SUBMIT" VALUE="Show queries">
					</TD>
				</TR>
			</TABLE>
		</FORM>
END
	

		print "</TD></TR><TR><TD HEIGHT=\"30\" WIDTH=\"100%\" BGCOLOR=\"#CCCCFF\">";
	}
	if ($username eq "all") {
		$sqlQuery = "SELECT * FROM history ORDER BY time_of_query DESC";
	} elsif ($username eq "synErr") {
		$sqlQuery = "SELECT * FROM history WHERE hits = -1 ORDER BY time_of_query DESC";
	} elsif ($username eq "running") {
		$sqlQuery = "SELECT * FROM history WHERE hits = -3 ORDER BY time_of_query DESC";
	} else {
		$sqlQuery = "SELECT * FROM history WHERE user='$username' ORDER BY time_of_query DESC";
	}
	
	$sth2= $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
	$res = $sth2->execute or bark("Can't execute $sqlQuery: $sth2::errstr");
	$table = $sth2->fetchall_arrayref or bark("Can't fetchrow_arrayref $sqlQuery: $sth2::errstr");
	
	$end = ($res -1) if ($end > $res);
	
	if ($res) {
		if ($username eq "all") {
			print <<END1;
				<B>The following searches were conducted with BNCweb:</B>
			</TD>
		</TR>
		<TR>
			<TD>
				<TABLE BORDER="0" CELLPADDING="2" CELLSPACING="6" WIDTH="100%">
					<TR BGCOLOR="#FFCC66">
						<TD>
							<B>No.</B>
						</TD>
						<TD>
							<B>User</B>
						</TD>
						<TD>
							<B>Date</B>
						</TD>
						<TD WIDTH="30%">
							<B>Query $queryViewBut</B>
						</TD>
						<TD>
							<B>Restriction</B>
						</TD>
						<TD NOWRAP>
							<B>No. of hits</B>
						</TD>
					</TR>
END1
		} elsif ($username eq "synErr") {
			print <<END1;
				<B>The following syntax errors were found:</B>
			</TD>
		</TR>
		<TR>
			<TD>
				<TABLE BORDER="0" CELLPADDING="2" CELLSPACING="6" WIDTH="100%">
					<TR BGCOLOR="#FFCC66">
						<TD>
							<B>No.</B>
						</TD>
						<TD>
							<B>User</B>
						</TD>
						<TD>
							<B>Date</B>
						</TD>
						<TD WIDTH="30%">
							<B>Query $queryViewBut</B>
						</TD>
						<TD>
							<B>Restriction</B>
						</TD>
						<TD NOWRAP>
							<B>No. of hits</B>
						</TD>
					</TR>
END1
		} elsif ($username eq "running") {
			print <<END1;
				<B>The following running (or killed) processes were found:</B>
			</TD>
		</TR>
		<TR>
			<TD>
				<TABLE BORDER="0" CELLPADDING="2" CELLSPACING="6" WIDTH="100%">
					<TR BGCOLOR="#FFCC66">
						<TD>
							<B>No.</B>
						</TD>
						<TD>
							<B>User</B>
						</TD>
						<TD>
							<B>Date</B>
						</TD>
						<TD WIDTH="30%">
							<B>Query $queryViewBut</B>
						</TD>
						<TD>
							<B>Restriction</B>
						</TD>
						<TD NOWRAP>
							<B>No. of hits</B>
						</TD>
					</TR>
END1
		} else { 
			if (($actualUsername=~m/\A($bwSuperuser)\Z/) || (&checkSuperUser2())) {
				print "<B>User $username has performed the following searches with BNCweb:</B>";
			}
			print <<END;
				</TD>
			</TR>
		</TABLE>
		
		<TABLE BORDER="0" CELLPADDING="2" CELLSPACING="6" WIDTH="100%">
			<TR BGCOLOR="#FFCC66">
				<TD>
					<B>No.</B>
				</TD>
				<TD>
					<B>Date</B>
				</TD>
				<TD WIDTH="30%" NOWRAP>
					<B>Query $queryViewBut</B>
				</TD>
					<TD>
						<B>Restriction</B>
					</TD>
				<TD NOWRAP>
					<B>No. of hits</B>
				</TD>
			</TR>
END
		}

	
		for ($i = $begin; $i <= $end; $i++) {
			$theUser=$table->[$i][0];
			$link=$table->[$i][1];
			$query=$table->[$i][2];
			$hits=$table->[$i][3];
			$hits = "syntax error" if ($hits == -1);
			$hits = "CQP problem" if ($hits == -3);
			$timestamp=$table->[$i][4];
			$timestamp=~m/(\d{4})\-?(\d{2})\-?(\d{2}) ?(\d{2})\:?(\d{2})\:?(\d{2})/;
			$year=$1;
			$month=$2;
			$day=$3;
			$hour=$4;
			$minute=$5;
			$second=$6;
			$subcorpus=$table->[$i][6];
			$typeOfRestriction=$table->[$i][7];
			$theItems=$table->[$i][8];
			$textOrSpeaker=$table->[$i][9];
			$simpleQuery = $table->[$i][10];
			$queryMode = $table->[$i][11];
			
## ***DEL***
      # if ($query=~s/###//) {
      # 
      #   $longest = 1;
      #   $simpleQuery = "((longest)) " . $simpleQuery;
      #   $query= "((longest)) " . $query;
      # }
			
			$metatext = "";
			
			if ($queryMode eq "simpleC") {
				$case = " (case sensitive)";
			} else {
				$case = "";
			}
			
			# split up original URL into variables
			@inputArray1=split(/\&/, $link);
			foreach $param (@inputArray1) {
				($paramName, $paramValue) = split(/=/, $param, 2);
				
				unless ($paramValue eq '') {
					$paramValue=~s/\+/ /g;
					$paramValue=~s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
					$params1{$paramName}=$paramValue;
				}
			}
			
			# fiddle with elements original URL to make sure the query becomes replicable
			# this is necessary when the query was executed with "last_written" or "last_spoken"
			# change URL to pretend that user selected these metatextual restrictions
			if ($params1{qtype}=~m/last_(written|spoken)/) {
				$subcorpus1 = $subcorpus;
				if ($params1{qtype} eq "last_written") {
					$params1{qtype} = "1";
					$params1{theData} .= "ª" . $subcorpus1 . "##";
				} elsif ($params1{qtype} eq "last_spoken") {
					$params1{qtype} = "2";
					if ($subcorpus1=~m/\) AND \(/) {
						$subcorpus1=~s/\) AND \(/\)å\(/;
					} else {
						$subcorpus1 = "å" . $subcorpus;
					}
					$params1{theData} .= "ª" . $subcorpus1;
				}
				$out = "";
				foreach $pName (keys %params1) {
					$url = $params1{$pName};
					$url=~s/([^\w ])/&hexURL($1)/eg;
					$url=~s/ /\+/g;
					
					$out.= "&$pName=$url" unless $pName eq 'urlTest';
				}
				$out=~s/\A\&//;
				$out.='&urlTest=yes';
				$link = $out;
			}
			$showLink = 1;
			$showLink = 0 if ($hits < 1);
			$displaySubcorpusLink=0;
			
			if ($typeOfRestriction eq "subcorpus") {
				$sqlQuery = "SELECT fnums FROM subcorpora WHERE username='$username' and subcorpus = '$subcorpus'";
	
				$sth5= $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
				$sth5->execute or bark("Can't execute $sqlQuery: $sth2::errstr");
				$res1 = $sth5->rows;	
				if ($res1) {
					@theArray=$sth5->fetchrow_array;
					$originalFnums = $theArray[0];
					
					if ($theItems eq $originalFnums) {
						$display = '<A HREF="/' . $bwCGIalias . '/subcorpAdmin.pl?action=showcorpus&chunk=1&max=INIT&inst=50&subcorpus=' . $subcorpus . '&username=' . $username . '&textOrSpeaker=' . $textOrSpeaker . '&urlTest=yes" onmouseover="return escape(\'Show contents of subcorpus\')">' . $subcorpus . '</A>';
						$displaySubcorpusLink = 1;
					} else {
						$display = "$subcorpus (contents of corpus changed since query)";
						$showLink = 0;
					}
				} else {
					$display = "$subcorpus ";
					$showLink = 0;
				}

				$typeOfRestriction = "Subcorpus: $display";

			} elsif ($typeOfRestriction eq "metatext") {
				
				$subcorpusParts = $subcorpus;
				$subcorpusParts=~s/\'//g;
				$typeOfRestriction = "Metatextual categories: ";
				$oldTypeValue = "";
				
				$metatext = "";
				while ($subcorpusParts=~m/(\w+)=(\d+)/g) {
					$catValue = $&;
					$typeValue = $1;
					$metatext .= "$1$2";
					$typeValue = $typeValues{$typeValue};
					$catValue = $catValues{$catValue};
					unless ($oldTypeValue eq $typeValue) {
						$typeOfRestriction .= "<BR>$typeValue: <i>$catValue</i>";
						$oldTypeValue = $typeValue;
					} else {
						$typeOfRestriction .= ", <i>$catValue</i>";
					}
				}
			} elsif ($typeOfRestriction eq "written") {
				$typeOfRestriction = "Written texts";
			} elsif ($typeOfRestriction eq "spoken") {
				$typeOfRestriction = "Spoken texts";
			}
			$pageSelection = "search";
			if ($metatext) {
				if ($metatext=~m/(scgdom|sdeage|sdecla|sdesex|spolog|sporeg|alltyp(1|2)|age|sex|class|firstLanguage|dialect|education|role|genre([1-9](?!\d)|1\d|2[0-4]))/) {
					$pageSelection = "spokentexts";
				} else {
					$pageSelection = "writtentexts";
				}
				$metatext=~s/\A//g;
				setParam("metatext", "$metatext");
			}
			$query =~ s/</&lt;/g;
			$query =~ s/>/&gt;/g;
			$simpleQuery =~ s/</&lt;/g;
			$simpleQuery =~ s/>/&gt;/g;
			
			if ($showLink) {
				$But = '<A HREF="/' . $bwCGIalias . '/main.pl?' . $link . '&urlTest=yes" onmouseover="return escape(\'Recreate query result\')">' . $hits . '</A>';
			} else {
				$But = $hits;
			}
			if (($simpleQuery) && ($historyView eq "simple"))  {
				setParam("insertQuery", "$simpleQuery");
				if ($queryMode eq "simpleC") {
					$But2 = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=search&querySelect=simpleC&queryMode=simple&insertQuery=' . $paramsEnc{insertQuery} . '&urlTest=yes" onmouseover="return escape(\'Insert query string into query window\')">' . $simpleQuery . '</A>' . $case;
					 if ($metatext) {
						$But3 = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=' . $pageSelection . '&querySelect=simpleC&queryMode=simple&insertQuery=' . $paramsEnc{insertQuery} . '&metatext=' . $paramsEnc{metatext} . '&urlTest=yes" onmouseover="return escape(\'Insert query string and metatextual restrictions into query window\')">Metatextual categories: </A>';
					} elsif ($displaySubcorpusLink) {
						$But3 = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=' . $pageSelection . '&querySelect=simpleC&queryMode=simple&insertQuery=' . $paramsEnc{insertQuery} . '&subcorpSelect=' . $subcorpus . '&urlTest=yes" onmouseover="return escape(\'Display contents of subcorpus\')>Subcorpus</A>: ';
					}
				} else {
					$But2 = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=search&querySelect=simple&queryMode=simple&insertQuery=' . $paramsEnc{insertQuery} . '&urlTest=yes" onmouseover="return escape(\'Insert query string into query window\')">' . $simpleQuery . '</A>' . $case;
					if ($metatext) {
						$But3 = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=' . $pageSelection . '&querySelect=simple&queryMode=simple&insertQuery=' . $paramsEnc{insertQuery} . '&metatext=' . $paramsEnc{metatext} . '&urlTest=yes" onmouseover="return escape(\'Insert query string and metatextual restrictions into query window\')">Metatextual categories: </A>';
					} elsif ($displaySubcorpusLink) {
						$But3 = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=' . $pageSelection . '&querySelect=simple&queryMode=simple&insertQuery=' . $paramsEnc{insertQuery} . '&subcorpSelect=' . $subcorpus . '&urlTest=yes" onmouseover="return escape(\'Display contents of subcorpus\')>Subcorpus</A>: ';
					}
				}
			} else {
				setParam("insertQuery", "$query");
				setParam("querySelect", "CQP");
				$But2 = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=search&querySelect=CQP&insertQuery=' . $paramsEnc{insertQuery} . '&urlTest=yes" onmouseover="return escape(\'Insert query string into query window\')">' . $query .'</A>';
				if ($metatext) {
					$But3 = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=' . $pageSelection . '&querySelect=CQP&insertQuery=' . $paramsEnc{insertQuery} . '&metatext=' . $paramsEnc{metatext} . '&urlTest=yes" onmouseover="return escape(\'Insert query string and metatextual restrictions into query window\')">Metatextual categories: </A>';
				} elsif ($displaySubcorpusLink) {
					$But3 = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=' . $pageSelection . '&querySelect=CQP&insertQuery=' . $paramsEnc{insertQuery} . '&subcorpSelect=' . $subcorpus . '&urlTest=yes" onmouseover="return escape(\'Display contents of subcorpus\')>Subcorpus</A>: ';
				}
			}

			if ($metatext) {
				$typeOfRestriction=~s/Metatextual categories: /$But3/;
			} 
			if ($displaySubcorpusLink) {
				$typeOfRestriction=~s/\ASubcorpus: /$But3/;
			}
			
			$count=$i+1;
			if (($username eq "all") || ($username eq "synErr") || ($username eq "running")) {
				print "<TR BGCOLOR=\"#DDDDDD\"><TD VALIGN=TOP>$count</TD><TD VALIGN=TOP>$theUser</TD><TD VALIGN=TOP NOWRAP>$day.$month.$year, $hour:$minute:$second</TD><TD VALIGN=TOP>$But2</TD><TD VALIGN=TOP>$typeOfRestriction</TD><TD  VALIGN=TOP ALIGN=CENTER>$But</TD></TR>"
			} else {
				print "<TR BGCOLOR=\"#DDDDDD\"><TD VALIGN=TOP>$count</TD><TD VALIGN=TOP NOWRAP>$day.$month.$year, $hour:$minute:$second</TD><TD VALIGN=TOP>$But2</TD><TD VALIGN=TOP>$typeOfRestriction</TD><TD VALIGN=TOP ALIGN=CENTER>$But</TD></TR>"
			}
		}
	
		print "</TABLE><TABLE BORDER=\"0\" CELLPADDING=\"3\" CELLSPACING=\"2\" WIDTH=\"100%\" BGCOLOR=\"#FFFFFF\"><TR><TD>";
		if ($params{chunk} > 1) {
			$indexBut = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=history&user=' . $username . '&chunk=' . ($params{chunk} - 1) . '&urlTest=yes">Newer queries</A>';
			print "[" . $indexBut . "]";
		} else {
			print "[Newer queries]";
		}
		if (($params{chunk} * $inst ) < $res) {
			$indexBut = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=history&user=' . $username . '&chunk=' . ($params{chunk} + 1) . '&urlTest=yes">Older queries</A>';
			print "[" . $indexBut . "]";
		} else {
			print "[Older queries]";
		}
		print "<HR>$bwCopyright</TD></TR></TABLE></TD></TR></TABLE>";
	} else {
		print "<H3>You have not yet performed any queries.</H3>";
	}
}

sub savedQueries {

	$inst=50;
	
	# connect to mysql
	$bncHandle = DBI->connect("DBI:mysql:$bwMYSQLtable:$bwMYSQLhost:$bwMysqlPort", "$bwMysqlUser", "$bwMysqlPwd") or bark("Can't connect to mysql on $bwMYSQLhost at port: $bwMysqlPort <BR>$bncHandle::errstr\n");
	
	
	# delete query if option was chosen
	if ($params{delQuery}) {
		$sqlQuery = "DELETE FROM savedQueries where theName = '$params{delQuery}'";
		unlink("/$bwTempPath/$bwCorpus:$params{delQuery}") if -f "/$bwTempPath/$bwCorpus:$params{delQuery}";
	}
	$bncHandle->do($sqlQuery) or bark("Can't insert $sqlQuery: " . $bncHandle::errstr);

	$begin=(($params{chunk} - 1) * $inst);
	$end=(($params{chunk} * $inst) - 1);
	print <<END;
	
	<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="6" WIDTH="100%">
		<TR>
			<TD WIDTH="100%" HEIGHT="22" BGCOLOR="#FFCC99">
				<H3 ALIGN="CENTER"><B>Saved queries</B>
			</TD>
		</TR>
		<TR>
			<TD WIDTH="100%">
END

	if (($username=~m/\A($bwSuperuser)\Z/) || (&checkSuperUser2())) {
		$sqlQuery = "SELECT distinct(user) FROM savedQueries where saved = 1 order by user";
		$sth1= $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
		$sth1->execute or bark("Can't execute $sqlQuery: $sth1::errstr");
		$res=$sth1->rows;

		print <<END;
			<TABLE BORDER=0 WIDTH=100%>
				<TR>
					<TD WIDTH=20% HEIGHT="30">
						<FORM ACTION="/$bwCGIalias/BNCquery.pl" METHOD="GET">
						<INPUT TYPE="HIDDEN" NAME="theQuery" VALUE="savedQueries" SIZE="-1">
						<B>User:</B>
					</TD>
					<TD WIDTH=30%>
						<SELECT NAME="user">
							<OPTION VALUE="all">  all users
END
		for ($i = 0; $i < $res; $i++) {
			@mysqlArray=$sth1->fetchrow_array;
			$theUser=$mysqlArray[0];
			print "<OPTION>$theUser\n";
		}
		print <<END;
						</SELECT>
					</TD>
					<TD>
						<INPUT TYPE="HIDDEN" NAME="chunk" SIZE="-1" VALUE="1">
						<INPUT TYPE="hidden" NAME="urlTest" VALUE="yes">
						<INPUT TYPE="SUBMIT" VALUE="Show queries">
					</TD>
				</TR>
			</TABLE>
		</FORM>
END
	

		print "</TD></TR><TR><TD HEIGHT=\"30\" WIDTH=\"100%\" BGCOLOR=\"#CCCCFF\">";
	}
	$actualUsername = $username;
	
	if ($params{user} ne $username) {
		$username=$params{user} if (($username=~m/\A($bwSuperuser)\Z/) || (&checkSuperUser2()));
	}
	if ($username eq "all") {
		$sqlQuery = "SELECT * FROM savedQueries where saved = 1 ORDER BY user, theName ASC";
	} else {
		$sqlQuery = "SELECT * FROM savedQueries WHERE user='$username' and saved = 1 ORDER BY theName ASC";
	}
	
	$sth2= $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
	$sth2->execute or bark("Can't execute $sqlQuery: $sth2::errstr");
	$res = $sth2->rows;
	$table = $sth2->fetchall_arrayref or bark("Can't fetchrow_arrayref $sqlQuery: $sth2::errstr");
	
	$end = ($res -1) if ($end > $res);
	$begin = ($begin - $params{inst}) if ($begin > $end);
	
	if ($res) {
		if ($username eq "all") {
			print <<END1;
				<B>The following saved queries exist:</B>
			</TD>
		</TR>
		<TR>
			<TD>
				<TABLE BORDER="0" CELLPADDING="2" CELLSPACING="6" WIDTH="100%">
					<TR BGCOLOR="#FFCC66">
						<TD>
							<B>No.</B>
						</TD>
						<TD>
							<B>User</B>
						</TD>
						<TD WIDTH="70%">
							<B>Name</B>
						</TD>
						<TD ALIGN=CENTER NOWRAP>
							<B>No. of hits</B>
						</TD>
						<TD>
							<B>Date</B>
						</TD>
						<TD>
							<B>Action</B>
						</TD>
					</TR>
END1
		} else { 
			if (($actualUsername=~m/\A($bwSuperuser)\Z/) || (&checkSuperUser2())) {
				print "<B>User $username has the following saved queries:</B>";
			}
			print <<END;
				</TD>
			</TR>
		</TABLE>
		
		<TABLE BORDER="0" CELLPADDING="2" CELLSPACING="6" WIDTH="100%">
			<TR BGCOLOR="#FFCC66">
				<TD>
					<B>No.</B>
				</TD>
				<TD WIDTH="70%">
					<B>Name</B>
				</TD>
				<TD ALIGN=CENTER NOWRAP>
					<B>No. of hits</B>
				</TD>
				<TD>
					<B>Date</B>
				</TD>
				<TD>
					<B>Action</B>
				</TD>

			</TR>
END
		}
	
		for ($i = $begin; $i <= $end; $i++) {
			$theUser = $table->[$i][0];
			$queryID = $table->[$i][1];
			$thMode  = $table->[$i][3];
			$hits    = $table->[$i][4];
			$theName = $table->[$i][7];
			$link    = $table->[$i][8];
			$timestamp=$table->[$i][9];
			$timestamp=~m/(\d{4})\-?(\d{2})\-?(\d{2}) ?(\d{2})\:?(\d{2})\:?(\d{2})/;

			$year=$1;
			$month=$2;
			$day=$3;
			$hour=$4;
			$minute=$5;
			$second=$6;
			
			$link=~s/&thMode=[^&]+//;
			$link=~s/&qname=[^&]+//;
			$link=~s/&queryID=[^&]+//;
			$link=~s/&theID=[^&]+//;
			$thModeLink = $thMode;
			$thModeLink=~s/(\W)/sprintf("%%%x", ord($1))/eg;
			
			$theDispName = $theName;
			$theDispName =~s/\A${username}_//;
			$But  = '<A HREF="/' . $bwCGIalias . '/main.pl?' . $link . '&thMode=' . $thModeLink . '&qname=' . $queryID . '&queryID=' . $queryID . '&theID=' . $queryID . '&recreate=' . $theName . '&qname=' . $theName . '&urlTest=yes">' . $theDispName . '</A>';
			$But2 = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=savedQueries&user=' . $username . '&chunk=' . $params{chunk} . '&delQuery=' . $theName . '&urlTest=yes">Delete</A>';
			$But3 = '<A HREF="/' . $bwCGIalias . '/savedQueryAdmin.pl?action=choose&username=' . $username . '&oldName=' . $theName . '&urlTest=yes">Rename</A>';
			$count=$i+1;
			if ($username eq "all") {
				print "<TR BGCOLOR=\"#DDDDDD\"><TD VALIGN=TOP>$count</TD><TD VALIGN=TOP>$theUser</TD><TD>$But</TD><TD VALIGN=TOP ALIGN=CENTER>$hits</TD><TD VALIGN=TOP NOWRAP>$day.$month.$year $hour:$minute:$second</TD><TD VALIGN=TOP NOWRAP>$But3 $But2</TD></TR>"
			} else {
				print "<TR BGCOLOR=\"#DDDDDD\"><TD VALIGN=TOP>$count</TD><TD>$But</TD><TD VALIGN=TOP ALIGN=CENTER>$hits</TD><TD VALIGN=TOP NOWRAP>$day.$month.$year $hour:$minute:$second</TD><TD VALIGN=TOP NOWRAP>$But3 $But2</TD></TR>"
			}
		}
	
		print "</TABLE><TABLE BORDER=\"0\" CELLPADDING=\"3\" CELLSPACING=\"2\" WIDTH=\"100%\" BGCOLOR=\"#FFFFFF\"><TR><TD>";
		if ($params{chunk} > 1) {
			$indexBut = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=savedQueries&user=' . $username . '&chunk=' . ($params{chunk} - 1) . '&urlTest=yes">Previous page</A>';
			print "[" . $indexBut . "]";
		} else {
			print "[Previous page]";
		}
		if (($params{chunk} * $inst ) < $res) {
			$indexBut = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=savedQueries&user=' . $username . '&chunk=' . ($params{chunk} + 1) . '&urlTest=yes">Next page</A>';
			print "[" . $indexBut . "]";
		} else {
			print "[Next page]";
		}
		print "<HR>$bwCopyright</TD></TR></TABLE></TD></TR></TABLE>";
	} else {
		print <<END;
			<TR>
			<TD WIDTH="100%">
				<BR><H3>You have no saved queries.</H3>
			</TD>
			</TR>
		<TR><TD><HR>$bwCopyright</TD></TR></TABLE></TD></TR></TABLE>
END
	}

}

sub upload {

	# import external data source
	print <<END;
	
	<TABLE BORDER="0" CELLPADDING="2" CELLSPACING="6" WIDTH="100%">
		<TR>
			<TD COLSPAN="2" WIDTH="100%" HEIGHT="22" BGCOLOR="#FFCC99">
				<H3 ALIGN="CENTER"><B>Upload external data file</B>
			</TD>
		</TR>
		<TR BGCOLOR="#DDDDDD">
			<TD WIDTH="30%">
			<FORM ACTION="upload.pl" METHOD="post" ENCTYPE="multipart/form-data"> 
			Select file for upload:
			</TD>
			<TD>
			<INPUT TYPE="file" NAME="corpusPositions"> 
			</TD>
		</TR>
		<TR BGCOLOR="#DDDDDD">
			<TD NOWRAP>
				Enter a name for the new saved query: 
			</TD>
			<TD>
				<INPUT TYPE="text" SIZE="40" NAME="name"> 
			</TD>
		</TR>
		<TR BGCOLOR="#CCCCCC">
			<TD COLSPAN="2" ALIGN="CENTER">
				<INPUT TYPE="submit" NAME="Submit" VALUE="Upload file"> 
			</FORM> 	
			
			</TD>
		</TR>
		<TR>
			<TD COLSPAN="2" BGCOLOR="#EEEEEE">
				<b>Instructions:</B><ul>
				<li>This feature can be used to upload a file to BNCweb in order to create a new saved query. </li>
				<li>The file must contain (only) two columns of corpus positions, which refer to individual hits of a previously 
				executed query that was exported with the "Download feature".</li>
				<li>The name of the saved query can only contain letters, numbers and the underscore character ("_"); 
				it cannot contain any spaces.</li>
				</ul>
			</TD>
		</TR>
		</TABLE>
		<HR>$bwCopyright
END

	
	
}


sub lookup {

print <<END;

	<perl_include> 
		<FORM ACTION="/$bwCGIalias/lookup.pl" METHOD="GET">
			<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="6" WIDTH="100%">
				<TR>
					<TD WIDTH="100%" HEIGHT="22" BGCOLOR="#CC99FF">
						<H3 ALIGN="CENTER">BNC Word lookup
					</TD>
				</TR>
				<TR>
					<TD WIDTH="100%"><SPACER TYPE="HORIZONTAL" SIZE="451"><FONT COLOR="#FFFFFF">.</FONT><BR>
						<BR>
						
						<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
							<TR>
								<TD HEIGHT="32">
									<P ALIGN="RIGHT"><B>Word or pattern:</B>&#160;&#160;
								</TD>
								<TD HEIGHT="32"><INPUT TYPE="TEXT" NAME="theData" SIZE="40"></TD>
							</TR>
							<TR>
								<TD HEIGHT="32">
									<DIV ALIGN="RIGHT">
									<P>
									<SPACER TYPE="VERTICAL" SIZE="10">
									<SPACER TYPE="HORIZONTAL" SIZE="17">
									String matches:&#160;&#160;&#160;
									</DIV>
								</TD>
								<TD HEIGHT="32">
									<SPACER TYPE="VERTICAL" SIZE="10">
 									<SELECT NAME="searchtype">
										<OPTION VALUE="beginning" SELECTED>Beginning of Word </OPTION>
										<OPTION VALUE="containing">Anywhere in Word </OPTION>
										<OPTION VALUE="end">End of Word </OPTION>
										<OPTION VALUE="exact">Complete Word </OPTION>
<!--										<OPTION VALUE="0">Regular Expression </OPTION> --!>
									</SELECT>
								</TD>

							</TR>
							<TR>
								<TD HEIGHT="32">
									<DIV ALIGN="RIGHT">
									<P>
									<SPACER TYPE="VERTICAL" SIZE="10">
									Lemmatisation:&#160;&#160;&#160;
									</DIV>
								</TD>
								<TD HEIGHT="32">
									<SPACER TYPE="VERTICAL" SIZE="10">
 									<SELECT NAME="lemmaScheme">
										<OPTION VALUE="no" SELECTED>None </OPTION>
										<OPTION VALUE="lancaster">Lancaster </OPTION>
									</SELECT>
								</TD>

							</TR>
							<TR>
								<TD HEIGHT="32">
									<DIV ALIGN="RIGHT">
									<P>
									<INPUT TYPE="HIDDEN" NAME="chunk" SIZE="-1" VALUE="1">
									Instances per page:&#160;&#160;
									</DIV>
								</TD>
								<TD HEIGHT="32">									
									<SELECT NAME="inst">
										<OPTION>10 </OPTION>
										<OPTION SELECTED>50 </OPTION>
										<OPTION>100 </OPTION>
										<OPTION>250 </OPTION>
										<OPTION>350 </OPTION>
										<OPTION>500 </OPTION>
										<OPTION>1000 </OPTION>
										<OPTION>2000 </OPTION>
										<OPTION>5000 </OPTION>
									</SELECT>
 									<INPUT TYPE="HIDDEN" NAME="max" SIZE="-1" VALUE="INIT"></TD>
							</TR>
							<TR>
								<TD HEIGHT="32">&#160;</TD>
								<TD HEIGHT="32">
									<SPACER TYPE="VERTICAL" SIZE="10">
									<INPUT TYPE="HIDDEN" NAME="view" SIZE="-1" VALUE="$view">
 									<INPUT TYPE="hidden" NAME="urlTest" VALUE="yes">
 									<INPUT TYPE="SUBMIT" VALUE="Start Query">
 									<INPUT TYPE="RESET" VALUE="Reset Query">
 								</TD>
							</TR>
						</TABLE>

					<HR>
					$bwCopyright
				</TD>
			</TR>
			</FORM>
		</TABLE>
END

}

sub browse {

print <<END;

<perl_include>
			<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="6" WIDTH="100%">
				<TR>
					<TD WIDTH="100%" HEIGHT="22" BGCOLOR="#CC99FF">
						<H3 ALIGN="CENTER">Browse a text:
					</TD>
				</TR>
				<TR>
					<TD WIDTH="100%">
						<SPACER TYPE="HORIZONTAL" SIZE="451">
						<FONT COLOR="#FFFFFF">.</FONT> 
						<TABLE BORDER="0" CELLPADDING="4" CELLSPACING="0">
							<TR>
								<TD>
									<DIV ALIGN="RIGHT">
									<P>
									<FORM ACTION="/$bwCGIalias/context.pl" METHOD="GET">
									<B>Filename:</B>&#160;&#160;
									</DIV>
								</TD>
								<TD VALIGN="TOP">
									<INPUT TYPE="TEXT" NAME="text" SIZE="6" MAXLENGTH="3"> 
									<INPUT TYPE="HIDDEN" NAME="qname" SIZE="-1" VALUE="nosol"> 
									<INPUT TYPE="HIDDEN" NAME="refnum" SIZE="-1" VALUE="0"> 
									<INPUT TYPE="HIDDEN" NAME="theData" SIZE="-1" VALUE="0"> 
									<INPUT TYPE="HIDDEN" NAME="len" SIZE="-1"	VALUE="0"> 
									<INPUT TYPE="HIDDEN" NAME="showTheTag" SIZE="-1" VALUE="0">
									<INPUT TYPE="HIDDEN" NAME="color" SIZE="-1" VALUE="0"> 
								</TD>
							</TR>
							<TR>
								<TD>
									<P ALIGN="RIGHT">&#160;</P>
								</TD>
								<TD VALIGN="TOP">Enter the three-letter file code you want to look at.<BR><BR>
									<SPACER TYPE="VERTICAL" SIZE="21">
								</TD>
							</TR>
							<TR>
								<TD VALIGN="TOP"  NOWRAP>
									<DIV ALIGN="RIGHT">
									<P>
									<SPACER TYPE="HORIZONTAL" SIZE="30">
									<B>Sentence number:&#160;&#160;</B>
									</DIV>
								</TD>
								<TD VALIGN="TOP">
									<INPUT TYPE="TEXT" NAME="begin" SIZE="6" MAXLENGTH="5"> 
									<INPUT TYPE="HIDDEN" NAME="spids" SIZE="-1" VALUE="1"> 
									<INPUT TYPE="HIDDEN" NAME="interval" SIZE="-1" VALUE="11"> 
									<INPUT TYPE="HIDDEN" NAME="first" SIZE="-1" VALUE="yes"> 
								</TD>
							</TR>
							<TR>
								<TD HEIGHT="32" VALIGN="TOP">
									&#160;
								</TD>
								<TD HEIGHT="32" VALIGN="TOP">
									Enter the sentence number where you want to start browsing. If you want to see the
									file from the beginning, you can leave this field empty.
								</TD>
							</TR>
							<TR>
								<TD>&#160;</TD>
								<TD>
								<INPUT TYPE="hidden" NAME="urlTest" VALUE="yes">
								<INPUT TYPE="SUBMIT" VALUE="Submit"> </TD>
							</TR>
						</TABLE>

					<HR>
					$bwCopyright</TD>
				</TR>
			</TABLE>
</perl_include>
END

}
sub spokentexts {

	if ($params{querySelect} eq "simple") {
		$selected{1} = "SELECTED";
	} elsif ($params{querySelect} eq "CQP") {
		$selected{0} = "SELECTED";
	} elsif ($params{querySelect} eq "simpleC") {
		$selected{2} = "SELECTED";
	} else {
		$selected{1} = "SELECTED";
	}
	
	@metatext = split(//, $params{metatext});
	foreach $item (@metatext) {
		$checked{$item}="CHECKED";
	}

	if ($params{showBox} eq "no") {
		$showStuff = "Get text IDs/speaker IDs";
		$showStuff2 = "Make a subcorpus of spoken texts";
	} else {
		$showStuff = "Start Query";
		$showStuff2 = "Restricted Range of Spoken Texts / Speakers";
	}

	print <<END;
	<FORM ACTION="/$bwCGIalias/processQuery.pl" METHOD="GET"></P>
			
			<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="6" WIDTH="100%">
				<TR>
					<TD WIDTH="100%" HEIGHT="22" BGCOLOR="#99CC99" COLSPAN="2">
						<H3 ALIGN="CENTER">$showStuff2</H3>
					</TD>
				</TR>
				<TR>
					<TD WIDTH="100%" COLSPAN="2">
						<TABLE WIDTH="100%" BORDER="0" CELLPADDING="1" CELLSPACING="1" BGCOLOR="#99CC99">
							<TR>
								<TD>
									<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" BGCOLOR="#FFFFFF">
										<TR>
											<TD COLSPAN="2">
												&nbsp;
											</TD>
										
										</TR>
END
	if ($params{showBox} eq "no") {

		print <<END;
										<TR>
											<TD VALIGN="TOP" NOWRAP WIDTH="30%">
												&nbsp;&nbsp;<B>Instructions:</B>
											</TD>
											<TD NOWRAP>
												Tick the categories that you want and press "Get text IDs/Speaker IDs" to get a list of files/speakers that match your selection.&nbsp;&nbsp;
												
												<INPUT TYPE="HIDDEN" NAME="theData" SIZE="-1" VALUE="">
											</TD>
										</TR>

										<TR>
											<TD HEIGHT="45" NOWRAP VALIGN="MIDDLE">
												&nbsp;&nbsp;<B>Number of hits per page:</B>&nbsp;&nbsp;
											</TD>
											<TD>
												<SELECT NAME="inst">
													<OPTION>count hits </OPTION>
													<OPTION>10 </OPTION>
													<OPTION SELECTED>50 </OPTION>
													<OPTION>100 </OPTION>
													<OPTION>250 </OPTION>
													<OPTION>350 </OPTION>
													<OPTION>500 </OPTION>
													<OPTION>1000 </OPTION>
												</SELECT>
												<INPUT TYPE="HIDDEN" NAME="chunk" SIZE="-1" VALUE="1">
												<INPUT TYPE="HIDDEN" NAME="queryType" SIZE="-1" VALUE="CQL">
											</TD>
										</TR>

END
	} else {
	
		print <<END;
										<TR>
											<TD VALIGN="TOP" NOWRAP WIDTH="18%">
												&nbsp;&nbsp;<B>Query string:&nbsp;&nbsp;</B>
											</TD>
											<TD>
												<TEXTAREA NAME="theData" ROWS="5" COLS="65" STYLE="font-size: 16px; WRAP="physical">$params{insertQuery}</TEXTAREA>
											</TD>
										</TR>
										<TR>
											<TD HEIGHT="45" VALIGN="MIDDLE">
												&nbsp;&nbsp;<B>Query mode:</B>&nbsp;&nbsp;
											</TD>
											<TD>									
												<SELECT NAME="qMode">
													<OPTION $selected{0}>CQP syntax</OPTION>
													<OPTION $selected{1}>Simple query (ignore case)</OPTION>
													<OPTION $selected{2}>Simple query (case sensitive)</OPTION>
												</SELECT>
											</TD>
										</TR>

										<TR>
											<TD HEIGHT="45" NOWRAP VALIGN="MIDDLE">
												&nbsp;&nbsp;<B>Number of hits per page:</B>&nbsp;&nbsp;
											</TD>
											<TD>
												<SELECT NAME="inst">
													<OPTION>count hits </OPTION>
													<OPTION>10 </OPTION>
													<OPTION SELECTED>50 </OPTION>
													<OPTION>100 </OPTION>
													<OPTION>250 </OPTION>
													<OPTION>350 </OPTION>
													<OPTION>500 </OPTION>
													<OPTION>1000 </OPTION>
												</SELECT>
												<INPUT TYPE="HIDDEN" NAME="chunk" SIZE="-1" VALUE="1">
												<INPUT TYPE="HIDDEN" NAME="queryType" SIZE="-1" VALUE="CQL">
												
											</TD>
										</TR>

END
	}


print <<END;
										<TR>
											<TD>
												<INPUT TYPE="HIDDEN" NAME="max" SIZE="-1" VALUE="INIT">
												<INPUT TYPE="HIDDEN" NAME="qname" SIZE="-1" VALUE="INIT">
												<INPUT TYPE="HIDDEN" NAME="thMode" SIZE="-1" VALUE="INIT">
												<INPUT TYPE="HIDDEN" NAME="thin" SIZE="-1" VALUE="0">
												<INPUT TYPE="hidden" NAME="qtype" VALUE="2">
												<INPUT TYPE="hidden" NAME="view" VALUE="$view"> &#160;
											</TD>
											<TD HEIGHT="40" VALIGN="MIDDLE">
												<SPACER TYPE="VERTICAL" SIZE="10">
												<INPUT TYPE="SUBMIT" VALUE="$showStuff"> 
												<INPUT TYPE="RESET" VALUE="Reset">
											</TD>
										</TR>
									</TABLE>
								</TD>
							</TR>
						</TABLE>
					</TD>
				</TR>
				<TR>
					<TD WIDTH="100%" COLSPAN="2">
						<SPACER TYPE="VERTICAL" SIZE="8">
 
						<TABLE BORDER="0" CELLPADDING="6" WIDTH="100%">
							<TR>
								<TD COLSPAN="3" BGCOLOR="#99CC99">
									<B>General Restrictions for Spoken Texts:</B>
								</TD>
							</TR>
							<TR>
								<TD BGCOLOR="#CCCCCC">
									<B>Overall:</B>
								</TD>
								<TD BGCOLOR="#CCCCCC">
									<B>Interaction Type:</B>
								</TD>
								<TD BGCOLOR="#CCCCCC">
									<B>Region where Spoken Text was Captured:</B>
								</TD>
							</TR>
							<TR>
								<TD VALIGN="TOP" BGCOLOR="#DDDDDD">
									<INPUT TYPE="HIDDEN" NAME="del" SIZE="-1" VALUE="tw">
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="alltyp1" $checked{alltyp1}> Demographically sampled<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="alltyp2" $checked{alltyp2}> Context-governed<BR>
									<INPUT TYPE="HIDDEN" NAME="del" SIZE="-1" VALUE="n">
								</TD>
								<TD VALIGN="TOP" BGCOLOR="#DDDDDD">
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="spolog1" $checked{spolog1}> Monologue<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="spolog2" $checked{spolog2}> Dialogue<BR>
									<INPUT TYPE="HIDDEN" NAME="del" SIZE="-1" VALUE="n">
								</TD>
								<TD VALIGN="TOP" BGCOLOR="#DDDDDD">
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="sporeg1" $checked{sporeg1}> South<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="sporeg2" $checked{sporeg2}> Midlands<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="sporeg3" $checked{sporeg3}> North<BR>
									<INPUT TYPE="HIDDEN" NAME="del" SIZE="-1" VALUE="n">
								</TD>
							</TR>
							<TR>
								<TD WIDTH="33%" BGCOLOR="#99CC99" COLSPAN="3">
									<B>Genre (<A href="/$bwHTMLalias/genres.html\">description of codes</A>):</B>
								</TD>
							</TR>
							<TR>
								<TD WIDTH="33%" VALIGN="TOP" BGCOLOR="#DDDDDD">
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre1" $checked{genre1}> S:brdcast:discussn<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre2" $checked{genre2}> S:brdcast:documentary<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre3" $checked{genre3}> S:brdcast:news<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre4" $checked{genre4}> S:classroom<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre5" $checked{genre5}> S:consult<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre6" $checked{genre6}> S:conv<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre7" $checked{genre7}> S:courtroom<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre8" $checked{genre8}> S:demonstratn<BR>
								</TD>
								<TD WIDTH="33%" VALIGN="TOP" BGCOLOR="#DDDDDD">
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre9" $checked{genre9}> S:interview<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre10" $checked{genre10}> S:interview:oral_history<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre11" $checked{genre11}> S:lect:commerce<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre12" $checked{genre12}> S:lect:humanities_arts<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre13" $checked{genre13}> S:lect:nat_science<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre14" $checked{genre14}> S:lect:polit_law_edu<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre15" $checked{genre15}> S:lect:soc_science<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre16" $checked{genre16}> S:meeting<BR>
								</TD>
								<TD WIDTH="33%" VALIGN="TOP" BGCOLOR="#DDDDDD">
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre17" $checked{genre17}> S:parliament<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre18" $checked{genre18}> S:pub_debate<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre19" $checked{genre19}> S:sermon<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre20" $checked{genre20}> S:speech:scripted<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre21" $checked{genre21}> S:speech:unscripted<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre22" $checked{genre22}> S:sportslive<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre23" $checked{genre23}> S:tutorial<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre24" $checked{genre24}> S:unclassified<BR>
									<INPUT TYPE="HIDDEN" NAME="del" SIZE="-1" VALUE="n">
								</TD>
							</TR>							
							<TR>
								<TD COLSPAN="3" BGCOLOR="#99CC99">
									<B>Speaker Restrictions:</B>
								</TD>
							</TR>
							<TR>
								<TD BGCOLOR="#CCCCCC">
									<B>Age:</B>
								</TD>
								<TD BGCOLOR="#CCCCCC">
									<B>Sex:</B>
								</TD>
								<TD BGCOLOR="#CCCCCC">
									<B>Social Class:</B>
								</TD>
							</TR>
							<TR>
								<TD VALIGN="TOP" BGCOLOR="#DDDDDD">
									<INPUT TYPE="HIDDEN" NAME="del" SIZE="-1" VALUE="ts">
									<INPUT TYPE="CHECKBOX" NAME="age" VALUE="1" $checked{age1}> 0-14<BR>
									<INPUT TYPE="CHECKBOX" NAME="age" VALUE="2" $checked{age2}> 15-24<BR>
									<INPUT TYPE="CHECKBOX" NAME="age" VALUE="3" $checked{age3}> 25-34<BR>
									<INPUT TYPE="CHECKBOX" NAME="age" VALUE="4" $checked{age4}> 35-44<BR>
									<INPUT TYPE="CHECKBOX" NAME="age" VALUE="5" $checked{age5}> 45-59<BR>
									<INPUT TYPE="CHECKBOX" NAME="age" VALUE="6" $checked{age6}> 60+<BR>
									<INPUT TYPE="CHECKBOX" NAME="age" VALUE="7" $checked{age7}> Unknown<BR>
									<INPUT TYPE="HIDDEN" NAME="del" SIZE="-1" VALUE="n">
								</TD>
								<TD VALIGN="TOP" BGCOLOR="#DDDDDD">
									<INPUT TYPE="CHECKBOX" NAME="sex" VALUE="1" $checked{sex1}> Male<BR>
									<INPUT TYPE="CHECKBOX" NAME="sex" VALUE="2" $checked{sex2}> Female<BR>
									<INPUT TYPE="CHECKBOX" NAME="sex" VALUE="3" $checked{sex3}> Unknown<BR>
									<INPUT TYPE="HIDDEN" NAME="del" SIZE="-1" VALUE="n">
								</TD>
								<TD VALIGN="TOP" BGCOLOR="#DDDDDD">
									<INPUT TYPE="CHECKBOX" NAME="class" VALUE="1" $checked{class1}> AB<BR>
									<INPUT TYPE="CHECKBOX" NAME="class" VALUE="2" $checked{class2}> C1<BR>
									<INPUT TYPE="CHECKBOX" NAME="class" VALUE="3" $checked{class3}> C2<BR>
									<INPUT TYPE="CHECKBOX" NAME="class" VALUE="4" $checked{class4}> DE<BR>
									<INPUT TYPE="CHECKBOX" NAME="class" VALUE="5" $checked{class5}> Unknown<BR>
									<INPUT TYPE="HIDDEN" NAME="del" SIZE="-1" VALUE="n">
								</TD>
							</TR>
							<TR>
								<TD COLSPAN="2" BGCOLOR="#CCCCCC">
									<B>Education:</B>
								</TD>
								<TD BGCOLOR="#CCCCCC">
									<B>First Language:</B>
								</TD>
							</TR>
							<TR>
								<TD COLSPAN="2" VALIGN="TOP" BGCOLOR="#DDDDDD">
									<INPUT TYPE="CHECKBOX" NAME="education" VALUE="1" $checked{education1}> Still in education<BR>
									<INPUT TYPE="CHECKBOX" NAME="education" VALUE="2" $checked{education2}> Left school 14 or under<BR>
									<INPUT TYPE="CHECKBOX" NAME="education" VALUE="3" $checked{education3}> Left school 15/16<BR>
									<INPUT TYPE="CHECKBOX" NAME="education" VALUE="4" $checked{education4}> Left school 17/18<BR>
									<INPUT TYPE="CHECKBOX" NAME="education" VALUE="5" $checked{education5}> Education continued until 19 or over<BR>
									<INPUT TYPE="CHECKBOX" NAME="education" VALUE="6" $checked{education6}> Information not available<BR>
									<INPUT TYPE="HIDDEN" NAME="del" SIZE="-1" VALUE="n">
								</TD>
								<TD VALIGN="TOP" BGCOLOR="#DDDDDD">
									<INPUT TYPE="CHECKBOX" NAME="firstLanguage" VALUE="3" $checked{firstLanguage3}> British English<BR>
									<INPUT TYPE="CHECKBOX" NAME="firstLanguage" VALUE="4" $checked{firstLanguage4}> North American English<BR>
									<INPUT TYPE="CHECKBOX" NAME="firstLanguage" VALUE="5" $checked{firstLanguage5}> Unknown Indian language<BR>
									<INPUT TYPE="CHECKBOX" NAME="firstLanguage" VALUE="1" $checked{firstLanguage1}> German<BR>
									<INPUT TYPE="CHECKBOX" NAME="firstLanguage" VALUE="2" $checked{firstLanguage2}> French<BR>
									<INPUT TYPE="CHECKBOX" NAME="firstLanguage" VALUE="6" $checked{firstLanguage6}> Unknown<BR>
									<INPUT TYPE="HIDDEN" NAME="del" SIZE="-1" VALUE="n">
								</TD>
							</TR>
							<TR>
								<TD BGCOLOR="#CCCCCC" COLSPAN="3">
									<B>Dialect/Accent (a rather unreliable category!):</B>
								</TD>
							</TR>
							<TR>
								<TD VALIGN="TOP" BGCOLOR="#DDDDDD">
									<INPUT TYPE="CHECKBOX" NAME="dialect" VALUE="1" $checked{dialect1}> Canada<BR>
									<INPUT TYPE="CHECKBOX" NAME="dialect" VALUE="2" $checked{dialect2}> German<BR>
									<INPUT TYPE="CHECKBOX" NAME="dialect" VALUE="3" $checked{dialect3}> East Anglia<BR>
									<INPUT TYPE="CHECKBOX" NAME="dialect" VALUE="4" $checked{dialect4}> French<BR>
									<INPUT TYPE="CHECKBOX" NAME="dialect" VALUE="5" $checked{dialect5}> Home Counties<BR>
									<INPUT TYPE="CHECKBOX" NAME="dialect" VALUE="6" $checked{dialect6}> Humberside<BR>
									<INPUT TYPE="CHECKBOX" NAME="dialect" VALUE="7" $checked{dialect7}> Irish<BR>
									<INPUT TYPE="CHECKBOX" NAME="dialect" VALUE="8" $checked{dialect8}> Indian subcontinent<BR>
									<INPUT TYPE="CHECKBOX" NAME="dialect" VALUE="9" $checked{dialect9}> Lancashire<BR>
								</TD>
								<TD BGCOLOR="#DDDDDD">
									<INPUT TYPE="CHECKBOX" NAME="dialect" VALUE="10" $checked{dialect10}> London<BR>
									<INPUT TYPE="CHECKBOX" NAME="dialect" VALUE="11" $checked{dialect11}> Central Midlands<BR>
									<INPUT TYPE="CHECKBOX" NAME="dialect" VALUE="12" $checked{dialect12}> Merseyside<BR>
									<INPUT TYPE="CHECKBOX" NAME="dialect" VALUE="13" $checked{dialect13}> North-east Midlands<BR>
									<INPUT TYPE="CHECKBOX" NAME="dialect" VALUE="14" $checked{dialect14}> Midlands<BR>
									<INPUT TYPE="CHECKBOX" NAME="dialect" VALUE="15" $checked{dialect15}> South Midlands<BR>
									<INPUT TYPE="CHECKBOX" NAME="dialect" VALUE="16" $checked{dialect16}> North-west Midlands<BR>
									<INPUT TYPE="CHECKBOX" NAME="dialect" VALUE="17" $checked{dialect17}> Central northern England<BR>
									<INPUT TYPE="CHECKBOX" NAME="dialect" VALUE="18" $checked{dialect18}> North-east England<BR>
									<INPUT TYPE="CHECKBOX" NAME="dialect" VALUE="19" $checked{dialect19}> Northern England<BR>
								</TD>
								<TD VALIGN="TOP" BGCOLOR="#DDDDDD">
									<INPUT TYPE="CHECKBOX" NAME="dialect" VALUE="21" $checked{dialect21}> Scottish<BR>
									<INPUT TYPE="CHECKBOX" NAME="dialect" VALUE="22" $checked{dialect22}> Lower south-west England<BR>
									<INPUT TYPE="CHECKBOX" NAME="dialect" VALUE="23" $checked{dialect23}> Central south-west England<BR>
									<INPUT TYPE="CHECKBOX" NAME="dialect" VALUE="24" $checked{dialect24}> Upper south-west England<BR>
									<INPUT TYPE="CHECKBOX" NAME="dialect" VALUE="25" $checked{dialect25}> European<BR>
									<INPUT TYPE="CHECKBOX" NAME="dialect" VALUE="26" $checked{dialect26}> American (US)<BR>
									<INPUT TYPE="CHECKBOX" NAME="dialect" VALUE="27" $checked{dialect27}> Welsh<BR>
									<INPUT TYPE="CHECKBOX" NAME="dialect" VALUE="28" $checked{dialect28}> West Indian<BR>
									<INPUT TYPE="CHECKBOX" NAME="dialect" VALUE="20" $checked{dialect20}> Other or unidentifiable
								</TD>
							</TR>
							<TR>
								<TD COLSPAN="3" BGCOLOR="#99CC99">
									<B>Restrictions for Spoken Context-governed Texts:</B>
								</TD>
							</TR>
							<TR>
								<TD COLSPAN="3" BGCOLOR="#CCCCCC">
									<B>Domain:</B>
								</TD>
							</TR>
							<TR>
								<TD COLSPAN="3" VALIGN="TOP" BGCOLOR="#DDDDDD">
									<INPUT TYPE="HIDDEN" NAME="del" SIZE="-1" VALUE="tv">
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="scgdom1" $checked{scgdom1}> Educational/Informative<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="scgdom2" $checked{scgdom2}> Business<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="scgdom3" $checked{scgdom3}> Public/Institutional<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="scgdom4" $checked{scgdom4}> Leisure<BR>
								</TD>
							</TR>
							<TR>
								<TD COLSPAN="3" BGCOLOR="#99CC99">
									<B>Restrictions for Demographically Sampled Texts:</B>
								</TD>
							</TR>
							<TR>
								<TD COLSPAN="3" BGCOLOR="#CCCCCC">
									These restrictions refer to the whole text and are not the same as the speaker restrictions above. 
									Please check the BNC user manual if you need further assistance.
									
								</TD>
							</TR>
							<TR>
								<TD WIDTH="33%" BGCOLOR="#CCCCCC">
									<B>Age of Respondent (not of Speaker!): </B>
								</TD>
								<TD WIDTH="33%" BGCOLOR="#CCCCCC">
									<B>Social Class of Respondent:</B>
								</TD>
								<TD WIDTH="33%" BGCOLOR="#CCCCCC">
									<B>Sex of Respondent:</B>
								</TD>
							</TR>
							<TR>
								<TD WIDTH="33%" VALIGN="TOP" BGCOLOR="#DDDDDD">
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="sdeage1" $checked{sdeage1}> 0-14<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="sdeage2" $checked{sdeage2}> 15-24<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="sdeage3" $checked{sdeage3}> 25-34<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="sdeage4" $checked{sdeage4}> 35-44<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="sdeage5" $checked{sdeage5}> 45-59<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="sdeage6" $checked{sdeage6}> 60+<BR>
									<INPUT TYPE="HIDDEN" NAME="del" SIZE="-1" VALUE="n">
								</TD>
								<TD WIDTH="33%" VALIGN="TOP" BGCOLOR="#DDDDDD">
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="sdecla1" $checked{sdecla1}> AB<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="sdecla2" $checked{sdecla2}> C1<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="sdecla3" $checked{sdecla3}> C2<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="sdecla4" $checked{sdecla4}> DE<BR>
									<INPUT TYPE="HIDDEN" NAME="del" SIZE="-1" VALUE="n">
								</TD>
								<TD WIDTH="33%" VALIGN="TOP" BGCOLOR="#DDDDDD">
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="sdesex1" $checked{sdesex1}> Male<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="sdesex2" $checked{sdesex2}> Female<BR>
									<INPUT TYPE="HIDDEN" NAME="del" SIZE="-1" VALUE="n">
									<INPUT TYPE="HIDDEN" NAME="wrispo" SIZE="-1" VALUE="spoken">
									<INPUT TYPE="hidden" NAME="urlTest" VALUE="yes">
								</TD>
							</TR>
							</TABLE>
						<HR>$bwCopyright
					</TD>
				</TR>
			</TABLE>
			</FORM>
END
}


sub writtentexts {

	if ($params{querySelect} eq "simple") {
		$selected{1} = "SELECTED";
	} elsif ($params{querySelect} eq "CQP") {
		$selected{0} = "SELECTED";
	} elsif ($params{querySelect} eq "simpleC") {
		$selected{2} = "SELECTED";
	} else {
		$selected{1} = "SELECTED";
	}
	
	@metatext = split(//, $params{metatext});
	foreach $item (@metatext) {
		$checked{$item}="CHECKED";
	}

	if ($params{showBox} eq "no") {
		$showStuff = "Get text IDs";
		$showStuff2 = "Make a subcorpus of written texts";
	} else {
		$showStuff = "Start Query";
		$showStuff2 = "Restricted Range of Written Texts";
	}

	print <<END;
	<FORM ACTION="/$bwCGIalias/processQuery.pl" METHOD="GET">
			<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="6" WIDTH="100%">
				<TR>
					<TD WIDTH="100%" HEIGHT="22" BGCOLOR="#99CC99" COLSPAN="2">
						<H3 ALIGN="CENTER">$showStuff2</H3>
					</TD>
				</TR>
				<TR>
					<TD WIDTH="100%" COLSPAN="2">
						<TABLE WIDTH="100%" BORDER="0" CELLPADDING="1" CELLSPACING="1" BGCOLOR="#99CC99">
							<TR>
								<TD>
									<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" BGCOLOR="#FFFFFF">
										<TR>
											<TD COLSPAN="2">
												&nbsp;
											</TD>
										
										</TR>

END

	if ($params{showBox} eq "no") {

		print <<END;
										<TR>
											<TD VALIGN="TOP" NOWRAP WIDTH="30%">
												&nbsp;&nbsp;<B>Instructions:</B>
											</TD>
											<TD COLSPAN="2" NOWRAP>
												Tick the categories that you want and press "Get text IDs" to get a list of files that match your selection.&nbsp;&nbsp;
												
												<INPUT TYPE="HIDDEN" NAME="theData" SIZE="-1" VALUE="">
											</TD>
										</TR>

										<TR>
											<TD HEIGHT="45" NOWRAP VALIGN="MIDDLE">
												&nbsp;&nbsp;<B>Number of hits per page:</B>&nbsp;&nbsp;
											</TD>
											<TD>
												<SELECT NAME="inst">
													<OPTION>count hits </OPTION>
													<OPTION>10 </OPTION>
													<OPTION SELECTED>50 </OPTION>
													<OPTION>100 </OPTION>
													<OPTION>250 </OPTION>
													<OPTION>350 </OPTION>
													<OPTION>500 </OPTION>
													<OPTION>1000 </OPTION>
												</SELECT>
												<INPUT TYPE="HIDDEN" NAME="chunk" SIZE="-1" VALUE="1">
												<INPUT TYPE="HIDDEN" NAME="queryType" SIZE="-1" VALUE="CQL">
											</TD>
										</TR>

END
	} else {
	
		print <<END;
										<TR>
											<TD VALIGN="TOP" NOWRAP WIDTH="18%">
												&nbsp;&nbsp;<B>Query term:&nbsp;&nbsp;</B>
											</TD>
											<TD>
												<TEXTAREA NAME="theData" ROWS="5" COLS="65" STYLE="font-size: 16px; WRAP="physical">$params{insertQuery}</TEXTAREA>
											</TD>
										</TR>

										<TR>
											<TD HEIGHT="45" VALIGN="MIDDLE">
												&nbsp;&nbsp;<B>Query mode:</B>&nbsp;&nbsp;
											</TD>
											<TD>									
												<SELECT NAME="qMode">
													<OPTION $selected{0}>CQP syntax</OPTION>
													<OPTION $selected{1}>Simple query (ignore case)</OPTION>
													<OPTION $selected{2}>Simple query (case sensitive)</OPTION>
												</SELECT>
											</TD>
										</TR>

										<TR>
											<TD HEIGHT="45" VALIGN="MIDDLE" NOWRAP>
												&nbsp;&nbsp;<B>Number of hits per page:</B>&nbsp;&nbsp;
											</TD>
											<TD>
												<SELECT NAME="inst">
													<OPTION>count hits </OPTION>
													<OPTION>10 </OPTION>
													<OPTION SELECTED>50 </OPTION>
													<OPTION>100 </OPTION>
													<OPTION>250 </OPTION>
													<OPTION>350 </OPTION>
													<OPTION>500 </OPTION>
													<OPTION>1000 </OPTION>
												</SELECT>
												<INPUT TYPE="HIDDEN" NAME="chunk" SIZE="-1" VALUE="1">
												<INPUT TYPE="HIDDEN" NAME="queryType" SIZE="-1" VALUE="CQL">
												
											</TD>
										</TR>

END
	}


print <<END;
										<TR>
											<TD>
												<INPUT TYPE="HIDDEN" NAME="max" SIZE="-1" VALUE="INIT">
												<INPUT TYPE="HIDDEN" NAME="qname" SIZE="-1" VALUE="INIT">
												<INPUT TYPE="HIDDEN" NAME="thMode" SIZE="-1" VALUE="INIT">
												<INPUT TYPE="HIDDEN" NAME="thin" SIZE="-1" VALUE="0">
												<INPUT TYPE="hidden" NAME="qtype" VALUE="1">
												<INPUT TYPE="hidden" NAME="view" VALUE="$view"> &#160;
											</TD>
											<TD HEIGHT="40" VALIGN="MIDDLE">
												<SPACER TYPE="VERTICAL" SIZE="10">
												<INPUT TYPE="SUBMIT" VALUE="$showStuff"> 
												<INPUT TYPE="RESET" VALUE="Reset">
											</TD>
										</TR>
									</TABLE>
								</TD>
							</TR>
						</TABLE>
					</TD>
				</TR>

				<TR>
					<TD WIDTH="100%" COLSPAN="2">
						<TABLE BORDER="0" CELLPADDING="4" WIDTH="100%">
							<TR>
								<TD WIDTH="33%" BGCOLOR="#99CC99">
									<B>Publication Date:</B>
								</TD>
								<TD WIDTH="33%" BGCOLOR="#99CC99">
									<B>Medium of Text:</B>
								</TD>
								<TD WIDTH="33%" BGCOLOR="#99CC99">
									<B>Text Sample:</B>
								</TD>
							</TR>
							<TR>
								<TD WIDTH="33%" VALIGN="TOP" BGCOLOR="#DDDDDD">
									<INPUT TYPE="HIDDEN" NAME="del" SIZE="-1" VALUE="tw">
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="alltim1" $checked{alltim1}> 1960-1974<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="alltim2" $checked{alltim2}> 1975-1984<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="alltim3" $checked{alltim3}> 1985-1993<BR>
									<INPUT TYPE="HIDDEN" NAME="del" SIZE="-1" VALUE="n">
								</TD>
								<TD WIDTH="33%" VALIGN="TOP" BGCOLOR="#DDDDDD">
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wrimed1" $checked{wrimed1}> Book<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wrimed2" $checked{wrimed2}> Periodical<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wrimed3" $checked{wrimed3}> Miscellaneous: published<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wrimed4" $checked{wrimed4}> Miscellaneous: unpublished<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wrimed5" $checked{wrimed5}> To-be-spoken<BR>
									<INPUT TYPE="HIDDEN" NAME="del" SIZE="-1" VALUE="n">
								</TD>
								<TD WIDTH="33%" VALIGN="TOP" BGCOLOR="#DDDDDD">
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wrisam1" $checked{wrisam1}> Whole text<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wrisam2" $checked{wrisam2}> Beginning sample<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wrisam3" $checked{wrisam3}> Middle sample<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wrisam4" $checked{wrisam4}> End sample<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wrisam5" $checked{wrisam5}> Composite<BR>
									<INPUT TYPE="HIDDEN" NAME="del" SIZE="-1" VALUE="n">
								</TD>
							</TR>
							<TR>
								<TD COLSPAN="2" BGCOLOR="#99CC99">
									<B>Domain:</B>
								</TD>
								<TD COLSPAN="1" BGCOLOR="#99CC99">
									<B>Derived text type:</B>
								</TD>
							</TR>
							<TR>
								<TD WIDTH="23%" VALIGN="TOP" BGCOLOR="#DDDDDD" NOWRAP>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wridom1" $checked{wridom1}> Imaginative prose<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wridom2" $checked{wridom2}> Informative: Natural and pure sciences<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wridom3" $checked{wridom3}> Informative: Applied science<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wridom4" $checked{wridom4}> Informative: Social science<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wridom5" $checked{wridom5}> Informative: World affairs
								</TD>
								<TD WIDTH="30%" VALIGN="TOP" BGCOLOR="#DDDDDD">
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wridom6" $checked{wridom6}> Informative: Commerce and finance<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wridom7" $checked{wridom7}> Informative: Arts<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wridom8" $checked{wridom8}> Informative: Belief and thought<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wridom9" $checked{wridom9}> Informative: Leisure<BR>
									<INPUT TYPE="HIDDEN" NAME="del" SIZE="-1" VALUE="n">
								</TD>
								<TD WIDTH="40%" VALIGN="TOP" BGCOLOR="#DDDDDD" NOWRAP>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="derivedtexttype0" $checked{derivedtexttype0}> Academic prose<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="derivedtexttype1" $checked{derivedtexttype1}> Fiction and verse<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="derivedtexttype2" $checked{derivedtexttype2}> Non-academic prose and biography<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="derivedtexttype3" $checked{derivedtexttype3}> Newspapers<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="derivedtexttype4" $checked{derivedtexttype4}> Other published written material<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="derivedtexttype5" $checked{derivedtexttype5}> Unpublished written material<BR>
									<INPUT TYPE="HIDDEN" NAME="del" SIZE="-1" VALUE="n">
								</TD>
							</TR>
							<TR>
								<TD WIDTH="23%" BGCOLOR="#99CC99">
									<B>Estimated Circulation Size:</B>
								</TD>
								<TD COLSPAN="1" BGCOLOR="#99CC99">
									<B>Perceived Level of Difficulty:</B>
								</TD>
								<TD COLSPAN="1" BGCOLOR="#99CC99">
									<B>Domicile of Author:</B>
								</TD>
							</TR>
							<TR>
								<TD WIDTH="23%" VALIGN="TOP" BGCOLOR="#DDDDDD">
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wrista1" $checked{wrista1}> Low<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wrista2" $checked{wrista2}> Medium<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wrista3" $checked{wrista3}> High<BR>
									<INPUT TYPE="HIDDEN" NAME="del" SIZE="-1" VALUE="n">
								</TD>
								<TD WIDTH="33%" VALIGN="TOP" BGCOLOR="#DDDDDD">
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wrilev1" $checked{wrilev1}> Low<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wrilev2" $checked{wrilev2}> Medium<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wrilev3" $checked{wrilev3}> High<BR>
									<INPUT TYPE="HIDDEN" NAME="del" SIZE="-1" VALUE="n">
								</TD>
								<TD WIDTH="33%" BGCOLOR="#DDDDDD" COLSPAN="2">
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wriad1" $checked{wriad1}> UK and Ireland<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wriad2" $checked{wriad2}> Commonwealth<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wriad3" $checked{wriad3}> Continental Europe<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wriad4" $checked{wriad4}> USA<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wriad5" $checked{wriad5}> Elsewhere<BR>
									<INPUT TYPE="HIDDEN" NAME="del" SIZE="-1" VALUE="n">
								</TD>
							</TR>
							<TR>
								<TD WIDTH="33%" BGCOLOR="#99CC99">
									<B>Age of Author:</B>
								</TD>
								<TD WIDTH="33%" BGCOLOR="#99CC99">
									<B>Sex of Author:</B>
								</TD>
								<TD WIDTH="33%" BGCOLOR="#99CC99">
									<B>Type of Author:</B>
								</TD>
							</TR>
							<TR>
								<TD WIDTH="33%" VALIGN="TOP" BGCOLOR="#DDDDDD">
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wriaag1" $checked{wriaag1}> 0-14<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wriaag2" $checked{wriaag2}> 15-24<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wriaag3" $checked{wriaag3}> 25-34<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wriaag4" $checked{wriaag4}> 35-44<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wriaag5" $checked{wriaag5}> 45-59<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wriaag6" $checked{wriaag6}> 60+<BR>
									<INPUT TYPE="HIDDEN" NAME="del" SIZE="-1" VALUE="n">
								</TD>
								<TD WIDTH="33%" VALIGN="TOP" BGCOLOR="#DDDDDD">
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wriase1" $checked{wriase1}> Male<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wriase2" $checked{wriase2}> Female<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wriase3" $checked{wriase3}> Mixed<BR>
									<INPUT TYPE="HIDDEN" NAME="del" SIZE="-1" VALUE="n">
								</TD>
								<TD WIDTH="33%" VALIGN="TOP" BGCOLOR="#DDDDDD">
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wriaty1" $checked{wriaty1}> Corporate<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wriaty2" $checked{wriaty2}> Multiple<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wriaty3" $checked{wriaty3}> Sole<BR>
									<INPUT TYPE="HIDDEN" NAME="del" SIZE="-1" VALUE="n">
								</TD>
							</TR>
							<TR>
								<TD WIDTH="33%" BGCOLOR="#99CC99">
									<B>Target Audience Age:</B>
								</TD>
								<TD WIDTH="33%" BGCOLOR="#99CC99" COLSPAN="2">
									<B>Target Audience Sex:</B>
								</TD>
							</TR>
							<TR>
								<TD WIDTH="33%" VALIGN="TOP" BGCOLOR="#DDDDDD">
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wriaud1" $checked{wriaud1}> Child<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wriaud2" $checked{wriaud2}> Teenager<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wriaud3" $checked{wriaud3}> Adult<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="wriaud4" $checked{wriaud4}> Any<BR>
									<INPUT TYPE="HIDDEN" NAME="del" SIZE="-1" VALUE="n">
								</TD>
								<TD WIDTH="33%" VALIGN="TOP" BGCOLOR="#DDDDDD" COLSPAN="2">
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="writas1" $checked{writas1}> Male<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="writas2" $checked{writas2}> Female<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="writas3" $checked{writas3}> Mixed<BR>
									<INPUT TYPE="HIDDEN" NAME="del" SIZE="-1" VALUE="n">
								</TD>
							</TR>
							<TR>
								<TD WIDTH="33%" BGCOLOR="#99CC99" COLSPAN="3">
									<B>Genre (<A href="/$bwHTMLalias/genres.html\">description of codes</A>):</B>
								</TD>
							</TR>
							<TR>
								<TD WIDTH="33%" VALIGN="TOP" BGCOLOR="#DDDDDD" NOWRAP>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre25" $checked{genre25}> W:ac:humanities_arts<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre26" $checked{genre26}> W:ac:medicine<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre27" $checked{genre27}> W:ac:nat_science<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre28" $checked{genre28}> W:ac:polit_law_edu<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre29" $checked{genre29}> W:ac:soc_science<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre30" $checked{genre30}> W:ac:tech_engin<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre31" $checked{genre31}> W:admin<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre32" $checked{genre32}> W:advert<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre33" $checked{genre33}> W:biography<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre34" $checked{genre34}> W:commerce<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre35" $checked{genre35}> W:email<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre36" $checked{genre36}> W:essay:school<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre37" $checked{genre37}> W:essay:univ<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre38" $checked{genre38}> W:fict:drama<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre39" $checked{genre39}> W:fict:poetry<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre40" $checked{genre40}> W:fict:prose<BR>
								</TD>
								<TD WIDTH="33%" VALIGN="TOP" BGCOLOR="#DDDDDD" NOWRAP>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre41" $checked{genre41}> W:hansard<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre42" $checked{genre42}> W:institut_doc<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre43" $checked{genre43}> W:instructional<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre44" $checked{genre44}> W:letters:personal<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre45" $checked{genre45}> W:letters:prof<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre46" $checked{genre46}> W:misc<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre47" $checked{genre47}> W:news_script<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre48" $checked{genre48}> W:newsp:brdsht_nat:arts<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre49" $checked{genre49}> W:newsp:brdsht_nat:commerce<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre50" $checked{genre50}> W:newsp:brdsht_nat:editorial<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre51" $checked{genre51}> W:newsp:brdsht_nat:misc<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre52" $checked{genre52}> W:newsp:brdsht_nat:report<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre53" $checked{genre53}> W:newsp:brdsht_nat:science<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre54" $checked{genre54}> W:newsp:brdsht_nat:social<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre55" $checked{genre55}> W:newsp:brdsht_nat:sports<BR>
								</TD>
								<TD WIDTH="33%" VALIGN="TOP" BGCOLOR="#DDDDDD" NOWRAP>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre56" $checked{genre56}> W:newsp:other:arts<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre57" $checked{genre57}> W:newsp:other:commerce<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre58" $checked{genre58}> W:newsp:other:report<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre59" $checked{genre59}> W:newsp:other:science<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre60" $checked{genre60}> W:newsp:other:social<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre61" $checked{genre61}> W:newsp:other:sports<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre62" $checked{genre62}> W:newsp:tabloid<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre63" $checked{genre63}> W:non_ac:humanities_arts<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre64" $checked{genre64}> W:non_ac:medicine<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre65" $checked{genre65}> W:non_ac:nat_science<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre66" $checked{genre66}> W:non_ac:polit_law_edu<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre67" $checked{genre67}> W:non_ac:soc_science<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre68" $checked{genre68}> W:non_ac:tech_engin<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre69" $checked{genre69}> W:pop_lore<BR>
									<INPUT TYPE="CHECKBOX" NAME="t" VALUE="genre70" $checked{genre70}> W:religion<BR>
									<INPUT TYPE="HIDDEN" NAME="wrispo" SIZE="-1" VALUE="written">
									<INPUT TYPE="hidden" NAME="urlTest" VALUE="yes">
								</TD>
							</TR>
						</TABLE>
						<HR>$bwCopyright
					</TD>
				</TR>
			</TABLE>
			</FORM>
END
}





sub keywords {

print <<END;
			<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="6" WIDTH="100%">
				<TR>
					<TD WIDTH="100%" HEIGHT="22" BGCOLOR="#CC99FF">
						<H3 ALIGN="CENTER">Scan BNC Keywords and Titles
					</TD>
				</TR>
				<FORM ACTION="/$bwCGIalias/keyword_search.pl" METHOD="GET">
				<TR>
					<TD WIDTH="100%"><SPACER TYPE="HORIZONTAL" SIZE="451"><FONT COLOR="#FFFFFF">.</FONT> 
						
						<TABLE BORDER="0" CELLPADDING="4" CELLSPACING="0">
							<TR>
								<TD>
									<DIV ALIGN="RIGHT">
									<P><B>Match:</B>&#160;&#160;
									</DIV>
								</TD>
								<TD BGCOLOR="#FFFFFF"  VALIGN="TOP">
									&#160;
									<INPUT TYPE="RADIO" NAME="andOr" VALUE="and" CHECKED> all words&#160;&#160;&#160;
									<INPUT TYPE="RADIO" NAME="andOr" VALUE="or"> any word
								</TD>
							</TR>
							<TR>
								<TD HEIGHT="32" VALIGN="TOP">
									<P ALIGN="RIGHT"><B>Keyword(s):</B>&#160;&#160;
								</TD>
								<TD HEIGHT="32" VALIGN="TOP">
									<INPUT TYPE="TEXT" NAME="key" SIZE="28" MAXLENGTH="100">&nbsp;&nbsp;<B>Type: </B>&nbsp;&nbsp;
									<SELECT NAME="type">
										<OPTION VALUE="COPAC_">COPAC</OPTION>
										<OPTION VALUE="">descriptive keywords - BNC1 release</OPTION>
									</SELECT>
									&nbsp;&nbsp;
									<INPUT TYPE="hidden" NAME="urlTest" VALUE="yes">
									<INPUT TYPE="SUBMIT" VALUE="Submit">
								</TD>
							</TR>
							<TR>
								<TD HEIGHT="32" VALIGN="TOP">
									<P ALIGN="RIGHT">&#160;
								</TD>
								<TD HEIGHT="32" VALIGN="TOP">
									Searches the document headers for <TT>&lt;keyword&gt;</TT> elements (see the <I>User
									Reference Guide</I>, section 5.3.5: <TT>&lt;txtClass&gt;</TT>). Returns a
									list of files with matching keywords. 
									<SPACER TYPE="VERTICAL" SIZE="19">
								</TD>
							</TR>
							<TR>
								<TD>
									<DIV ALIGN="RIGHT">
									<P>
									</FORM>
									<FORM ACTION="/$bwCGIalias/title_search.pl" METHOD="GET">
									<B>Match:</B>&#160;&#160;
									</DIV>
								</TD>
								<TD>
									<INPUT TYPE="RADIO" NAME="andOr" VALUE="and" CHECKED> all words&#160;&#160;&#160;
									<INPUT TYPE="RADIO" NAME="andOr" VALUE="or"> any word&#160;&#160;&#160;
									<INPUT TYPE="RADIO" NAME="andOr" VALUE="phrase"> phrase
								</TD>
							</TR>
							<TR>
								<TD HEIGHT="32" VALIGN="TOP" NOWRAP>
									<DIV ALIGN="RIGHT">
									<P><SPACER TYPE="HORIZONTAL" SIZE="30"><B>Title word(s):&#160;&#160;</B>
									</DIV>
								</TD>
								<TD HEIGHT="32" VALIGN="TOP">
									<INPUT TYPE="TEXT" NAME="key" SIZE="28" MAXLENGTH="100">
									<INPUT TYPE="hidden" NAME="urlTest" VALUE="yes">
									<INPUT TYPE="SUBMIT" VALUE="Submit">
								</TD>
							</TR>
							<TR>
								<TD HEIGHT="32" VALIGN="TOP">
									&#160;
								</TD>
								<TD HEIGHT="32" VALIGN="TOP">
									Searches the document headers for <TT>&lt;title&gt;</TT> elements (see the <I>User
									Reference Guide</I>, section 5.1.1: <TT>&lt;titleStmt&gt;</TT>). Returns a
									list of files with matching title words.
								</TD>
							</TR>
						</TABLE>

						<HR>
						$bwCopyright
					</TD>
				</TR>
			</TABLE>
END
}

sub genres {
	$options='
										<OPTION>W</OPTION>
										<OPTION>S</OPTION>
										<OPTION>ac</OPTION>
										<OPTION>admin</OPTION>
										<OPTION>advert</OPTION>
										<OPTION>arts</OPTION>
										<OPTION>biography</OPTION>
										<OPTION>brdcast</OPTION>
										<OPTION>brdsht_nat</OPTION>
										<OPTION>classroom</OPTION>
										<OPTION>commerce</OPTION>
										<OPTION>consult</OPTION>
										<OPTION>conv</OPTION>
										<OPTION>courtroom</OPTION>
										<OPTION>demonstratn</OPTION>
										<OPTION>discussn</OPTION>
										<OPTION>documentary</OPTION>
										<OPTION>drama</OPTION>
										<OPTION>editorial</OPTION>
										<OPTION>email</OPTION>
										<OPTION>essay</OPTION>
										<OPTION>fict</OPTION>
										<OPTION>hansard</OPTION>
										<OPTION>humanities_arts</OPTION>
										<OPTION>institut_doc</OPTION>
										<OPTION>instructional</OPTION>
										<OPTION>interview</OPTION>
										<OPTION>lect</OPTION>
										<OPTION>letters</OPTION>
										<OPTION>medicine</OPTION>
										<OPTION>meeting</OPTION>
										<OPTION>misc</OPTION>
										<OPTION>nat_science</OPTION>
										<OPTION>news</OPTION>
										<OPTION>newsp</OPTION>
										<OPTION>non_ac</OPTION>
										<OPTION>oral_history</OPTION>
										<OPTION>other</OPTION>
										<OPTION>parliament</OPTION>
										<OPTION>personal</OPTION>
										<OPTION>poetry</OPTION>
										<OPTION>polit_law_edu</OPTION>
										<OPTION>pop_lore</OPTION>
										<OPTION>prof</OPTION>
										<OPTION>prose</OPTION>
										<OPTION>pub_debate</OPTION>
										<OPTION>religion</OPTION>
										<OPTION>report</OPTION>
										<OPTION>school</OPTION>
										<OPTION>science</OPTION>
										<OPTION>script</OPTION>
										<OPTION>scripted</OPTION>
										<OPTION>sermon</OPTION>
										<OPTION>social</OPTION>
										<OPTION>soc_science</OPTION>
										<OPTION>speech</OPTION>
										<OPTION>sports</OPTION>
										<OPTION>sportslive</OPTION>
										<OPTION>tabloid</OPTION>
										<OPTION>tech_engin</OPTION>
										<OPTION>tutorial</OPTION>
										<OPTION>unclassified</OPTION>
										<OPTION>univ</OPTION>
										<OPTION>unscripted</OPTION>

';
print <<END;
			<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="6" WIDTH="100%">
				<TR>
					<TD WIDTH="100%" HEIGHT="22" BGCOLOR="#CC99FF">
						<H3 ALIGN="CENTER">David Lee's Genre Classification Scheme
					</TD>
				</TR>
				<TR>
					<TD WIDTH="100%"><SPACER TYPE="HORIZONTAL" SIZE="451"><FONT COLOR="#FFFFFF">.</FONT> 
						<TABLE BORDER="0" CELLPADDING="4" CELLSPACING="0">
							<FORM ACTION="/$bwCGIalias/genres_search.pl" METHOD="GET">
							<TR>
								<TD WIDTH="20%" HEIGHT="60" >
									<B>Genre:</B>
								</TD>
								
								<TD WIDTH="20%" HEIGHT="60">
									
									<SELECT NAME="item1">
										<OPTION VALUE="25">W:ac:humanities_arts</OPTION>
										<OPTION VALUE="26">W:ac:medicine</OPTION>
										<OPTION VALUE="27">W:ac:nat_science</OPTION>
										<OPTION VALUE="28">W:ac:polit_law_edu</OPTION>
										<OPTION VALUE="29">W:ac:soc_science</OPTION>
										<OPTION VALUE="30">W:ac:tech_engin</OPTION>
										<OPTION VALUE="31">W:admin</OPTION>
										<OPTION VALUE="32">W:advert</OPTION>
										<OPTION VALUE="33">W:biography</OPTION>
										<OPTION VALUE="34">W:commerce</OPTION>
										<OPTION VALUE="35">W:email</OPTION>
										<OPTION VALUE="36">W:essay:school</OPTION>
										<OPTION VALUE="37">W:essay:univ</OPTION>
										<OPTION VALUE="38">W:fict:drama</OPTION>
										<OPTION VALUE="39">W:fict:poetry</OPTION>
										<OPTION VALUE="40">W:fict:prose</OPTION>
										<OPTION VALUE="41">W:hansard</OPTION>
										<OPTION VALUE="42">W:institut_doc</OPTION>
										<OPTION VALUE="43">W:instructional</OPTION>
										<OPTION VALUE="44">W:letters:personal</OPTION>
										<OPTION VALUE="45">W:letters:prof</OPTION>
										<OPTION VALUE="46">W:misc</OPTION>
										<OPTION VALUE="47">W:news_script</OPTION>
										<OPTION VALUE="48">W:newsp:brdsht_nat:arts</OPTION>
										<OPTION VALUE="49">W:newsp:brdsht_nat:commerce</OPTION>
										<OPTION VALUE="50">W:newsp:brdsht_nat:editorial</OPTION>
										<OPTION VALUE="51">W:newsp:brdsht_nat:misc</OPTION>
										<OPTION VALUE="52">W:newsp:brdsht_nat:report</OPTION>
										<OPTION VALUE="53">W:newsp:brdsht_nat:science</OPTION>
										<OPTION VALUE="54">W:newsp:brdsht_nat:social</OPTION>
										<OPTION VALUE="55">W:newsp:brdsht_nat:sports</OPTION>
										<OPTION VALUE="56">W:newsp:other:arts</OPTION>
										<OPTION VALUE="57">W:newsp:other:commerce</OPTION>
										<OPTION VALUE="58">W:newsp:other:report</OPTION>
										<OPTION VALUE="59">W:newsp:other:science</OPTION>
										<OPTION VALUE="60">W:newsp:other:social</OPTION>
										<OPTION VALUE="61">W:newsp:other:sports</OPTION>
										<OPTION VALUE="62">W:newsp:tabloid</OPTION>
										<OPTION VALUE="63">W:non_ac:humanities_arts</OPTION>
										<OPTION VALUE="64">W:non_ac:medicine</OPTION>
										<OPTION VALUE="65">W:non_ac:nat_science</OPTION>
										<OPTION VALUE="66">W:non_ac:polit_law_edu</OPTION>
										<OPTION VALUE="67">W:non_ac:soc_science</OPTION>
										<OPTION VALUE="68">W:non_ac:tech_engin</OPTION>
										<OPTION VALUE="69">W:pop_lore</OPTION>
										<OPTION VALUE="70">W:religion</OPTION>
										<OPTION VALUE="1">S:brdcast:discussn</OPTION>
										<OPTION VALUE="2">S:brdcast:documentary</OPTION>
										<OPTION VALUE="3">S:brdcast:news</OPTION>
										<OPTION VALUE="4">S:classroom</OPTION>
										<OPTION VALUE="5">S:consult</OPTION>
										<OPTION VALUE="6">S:conv</OPTION>
										<OPTION VALUE="7">S:courtroom</OPTION>
										<OPTION VALUE="8">S:demonstratn</OPTION>
										<OPTION VALUE="9">S:interview</OPTION>
										<OPTION VALUE="10">S:interview:oral_history</OPTION>
										<OPTION VALUE="11">S:lect:commerce</OPTION>
										<OPTION VALUE="12">S:lect:humanities_arts</OPTION>
										<OPTION VALUE="13">S:lect:nat_science</OPTION>
										<OPTION VALUE="14">S:lect:polit_law_edu</OPTION>
										<OPTION VALUE="15">S:lect:soc_science</OPTION>
										<OPTION VALUE="16">S:meeting</OPTION>
										<OPTION VALUE="17">S:parliament</OPTION>
										<OPTION VALUE="18">S:pub_debate</OPTION>
										<OPTION VALUE="19">S:sermon</OPTION>
										<OPTION VALUE="20">S:speech:scripted</OPTION>
										<OPTION VALUE="21">S:speech:unscripted</OPTION>
										<OPTION VALUE="22">S:sportslive</OPTION>
										<OPTION VALUE="23">S:tutorial</OPTION>
										<OPTION VALUE="24">S:unclassified</OPTION>

									</SELECT>
								</TD>
								<TD COLSPAN="2" ALIGN="LEFT">
									<INPUT TYPE="hidden" NAME="urlTest" VALUE="yes">
									<INPUT TYPE="SUBMIT" VALUE="Submit">
									
								</TD>
							</TR>
							</FORM>
							<TR>
								<TD COLSPAN="4">
									<FORM ACTION="/$bwCGIalias/genres_search.pl" METHOD="GET">
									Searches the document headers for 
									<A href="/$bwHTMLalias/genres.html\"><TT>&lt;classCode scheme=&quot;DLee&quot;&gt;</TT> 
									
									elements</A></TT>. 
									It returns a list of files that can be used to compile a subcorpus.<BR><BR>
								</TD>
							</TR>
						</TABLE>
						<TABLE BORDER="0" CELLPADDING="4" CELLSPACING="0">
							<TR>
								<TD WIDTH="26%">
									<FORM ACTION="/$bwCGIalias/genres_search.pl" METHOD="GET">
									<SELECT MULTIPLE SIZE=8 NAME="item1">
									$options
									</SELECT>
								</TD>
								<TD WIDTH="11%" VALIGN="MIDDLE" ALIGN="LEFT"><B>AND</B></TD>
								<TD WIDTH="26%">
									<SELECT MULTIPLE SIZE=8 NAME="item2">
										<OPTION VALUE="">none</OPTION>
										$options
									</SELECT>
								</TD>
								<TD WIDTH="11%" VALIGN="MIDDLE" ALIGN="LEFT"><B>AND</B></TD>
								<TD WIDTH="26%">
									<SELECT MULTIPLE SIZE=8 NAME="item3">
										<OPTION VALUE="">none</OPTION>
										$options
									</SELECT>
								</TD>
							</TR>
							<TR>
								<TD COLSPAN="5" ALIGN="MIDDLE" VALIGN="MIDDLE" HEIGHT="40">
									<INPUT TYPE="hidden" NAME="urlTest" VALUE="yes">
									<INPUT TYPE="SUBMIT" VALUE="Submit">&nbsp;&nbsp;
									<INPUT TYPE="reset" VALUE="Reset">
								</TD>
							</TR>
							<TR>
								<TD COLSPAN="5">
									Searches for individual elements of the genre classification (e.g. <TT>newsp</TT> will
									find all newspapers in the BNC). The three columns are combined with the operator &quot;AND&quot;.
									You can select several elements per column - they will then be combined with the operator &quot;OR&quot;. <BR><BR>
								</TD>
							</TR>
						</TABLE>

						<HR>
						$bwCopyright
					</TD>
				</TR>
			</TABLE>
END
}


sub frequency {

print <<END;
			<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="6" WIDTH="100%">
				<TR>
					<TD WIDTH="100%" HEIGHT="22" BGCOLOR="#CC99FF" COLSPAN="2">
						<H3 ALIGN="CENTER">BNC Frequency Lists
					</TD>
				</TR>
				<TR>
					<TD WIDTH="50%" ALIGN="TOP">
						<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">
							<TR>
								<TD WIDTH="50%" VALIGN="TOP">
						
									<TABLE BORDER="0" CELLPADDING="3" CELLSPACING="3"  BGCOLOR="#FFFFFF" WIDTH="100%">
										<TR>
											<TD HEIGHT="22" COLSPAN="2" BGCOLOR="#FFCC66">
												<B>Word frequencies</B>
											</TD>
										</TR>
										<TR>
											<TD VALIGN="TOP" WIDTH="40%" BGCOLOR="#EEEEEE">
												<FORM ACTION="/$bwCGIalias/freqList.pl" METHOD="GET">
												<INPUT TYPE="HIDDEN" NAME="chunk" SIZE="-1" VALUE="1">
												<P>Choose one or several POS-tags:
											</TD>
											<TD BGCOLOR="#EEEEEE">
												<SELECT MULTIPLE SIZE=7 NAME="tag">
													<OPTION VALUE=""> no restrictions
													<OPTION VALUE="VERB"> any verb
													<OPTION VALUE="SUBST"> any noun
													<OPTION VALUE="ADJ"> any adjective
													<OPTION VALUE="ADV"> any adverb
													<OPTION VALUE="ART"> any article
													<OPTION VALUE="PREP"> any preposition
													<OPTION VALUE="CONJ"> any conjunction
													<OPTION VALUE="PRON"> any pronoun
													<OPTION VALUE="INTERJ"> any interjection
													<OPTION VALUE="STOP"> any punctuation
													<OPTION>AJ0
													<OPTION>AJC
													<OPTION>AJS
													<OPTION>AT0
													<OPTION>AV0
													<OPTION>AVP
													<OPTION>AVQ
													<OPTION>CJC
													<OPTION>CJS
													<OPTION>CJT
													<OPTION>CRD
													<OPTION>DPS
													<OPTION>DT0
													<OPTION>DTQ
													<OPTION>EX0
													<OPTION>ITJ
													<OPTION>NN0
													<OPTION>NN1
													<OPTION>NN2
													<OPTION>NP0
													<OPTION>ORD
													<OPTION>PNI
													<OPTION>PNP
													<OPTION>PNQ
													<OPTION>PNX
													<OPTION>POS
													<OPTION>PRF
													<OPTION>PRP
													<OPTION>TO0
													<OPTION>UNC
													<OPTION>VBB
													<OPTION>VBD
													<OPTION>VBG
													<OPTION>VBI
													<OPTION>VBN
													<OPTION>VBZ
													<OPTION>VDB
													<OPTION>VDD
													<OPTION>VDG
													<OPTION>VDI
													<OPTION>VDN
													<OPTION>VDZ
													<OPTION>VHB
													<OPTION>VHD
													<OPTION>VHG
													<OPTION>VHI
													<OPTION>VHN
													<OPTION>VHZ
													<OPTION>VM0
													<OPTION>VVB
													<OPTION>VVD
													<OPTION>VVG
													<OPTION>VVI
													<OPTION>VVN
													<OPTION>VVZ
													<OPTION>XX0
													<OPTION>ZZ0
													<OPTION>AJ0-AV0
													<OPTION>AJ0-NN1
													<OPTION>AJ0-VVD
													<OPTION>AJ0-VVG
													<OPTION>AJ0-VVN
													<OPTION>AV0-AJ0
													<OPTION>AVP-PRP
													<OPTION>AVQ-CJS
													<OPTION>CJS-AVQ
													<OPTION>CJS-PRP
													<OPTION>CJT-DT0
													<OPTION>CRD-PNI
													<OPTION>DT0-CJT
													<OPTION>NN1-AJ0
													<OPTION>NN1-NP0
													<OPTION>NN1-VVB
													<OPTION>NN1-VVG
													<OPTION>NN2-VVZ
													<OPTION>NP0-NN1
													<OPTION>PNI-CRD
													<OPTION>PRP-AVP
													<OPTION>PRP-CJS
													<OPTION>VVB-NN1
													<OPTION>VVD-AJ0
													<OPTION>VVD-VVN
													<OPTION>VVG-AJ0
													<OPTION>VVG-NN1
													<OPTION>VVN-AJ0
													<OPTION>VVN-VVD
													<OPTION>VVZ-NN2
													<OPTION>PUL
													<OPTION>PUN
													<OPTION>PUQ
													<OPTION>PUR
												</SELECT>
											</TD>
										</TR>
										<TR>
											<TD BGCOLOR="#EEEEEE">
											Word pattern: 
											</TD>
											<TD BGCOLOR="#EEEEEE">
												<SELECT NAME="regex">
													<OPTION VALUE="begin" SELECTED>starting with</OPTION>
													<OPTION VALUE="end">ending in</OPTION>
													<OPTION VALUE="contain">containing</OPTION>
												</SELECT>
												<INPUT TYPE="text" NAME="regexValue" SIZE=16>
											</TD>
										</TR>										<TR>
											<TD BGCOLOR="#EEEEEE">
												<P><SPACER TYPE="HORIZONTAL" SIZE="17">Range of texts:
											</TD>

END

	$sqlQuery = "SELECT subcorpus, textOrSpeaker, fnums FROM subcorpora WHERE username='$username' ORDER BY subcorpus";
	$sth1 = $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
	$sth1->execute or bark("Can't execute $sqlQuery: $sth::errstr");
	$res = $sth1->rows;		
	
	$subCorpDisplay = "";
	if ($res) {
	    for ($i = 0; $i < $res; $i++) {
	    	@rowArray=$sth1->fetchrow_array;
			$subcorpus=$rowArray[0];
			$textOrSpeaker=$rowArray[1];
			$fnums=$rowArray[2];
			@fnumArray = split(/·/, $fnums);
			if ($textOrSpeaker eq "text") {
				$fnumList = "(" . join(" OR ", map { "fnum = $_" } @fnumArray) . ")";
			} else {
				$fnumList = "(" . join(" OR ", map { "snum = $_" } @fnumArray) . ")";
			}
			
			$sqlQuery = "SELECT theName FROM frequencyLists where subcorpus=\"$fnumList\"";
		
			$sth2= $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
			$sth2->execute or bark("Can't execute $sqlQuery: " . $sth::errstr);
			$cached = $sth2->rows;
			
			if ($cached) {
	    		@rowArray2=$sth2->fetchrow_array;
				$theName=$rowArray2[0] . "###" . $subcorpus;
				$theName2=&encodeForm($theName);

				$subCorpDisplay .= "<OPTION VALUE=\"" . $theName2 . "\">Subcorpus: " . $subcorpus . "</OPTION>\n";
			}
		}	
	}


print <<END;

											<TD BGCOLOR="#EEEEEE">
												<SELECT NAME="spowri">
													<OPTION VALUE="all" SELECTED>Whole BNC</OPTION>
													<OPTION VALUE="s">Spoken Texts only</OPTION>
													<OPTION VALUE="w">Written Texts only</OPTION>
													$subCorpDisplay
												</SELECT>
			
											</TD>
										</TR>
										<TR>
											<TD BGCOLOR="#EEEEEE">
												<P><SPACER TYPE="HORIZONTAL" SIZE="17">Range of frequency (optional):
											</TD>
											<TD BGCOLOR="#EEEEEE">
												from <INPUT TYPE="text" NAME="minValue" SIZE=8> to
												<INPUT TYPE="text" NAME="maxValue" SIZE=8>
											</TD>
										</TR>							
										<TR>
											<TD BGCOLOR="#EEEEEE">
												<P><SPACER TYPE="HORIZONTAL" SIZE="17">Show individual tag frequencies:
											</TD>
											<TD BGCOLOR="#EEEEEE">
												<SELECT NAME="showTag">
													<OPTION VALUE="" SELECTED>no</OPTION>
													<OPTION VALUE="1">yes</OPTION>
												</SELECT>
											</TD>
										</TR>
										<TR>
											<TD BGCOLOR="#EEEEEE">
												<P><SPACER TYPE="HORIZONTAL" SIZE="17">Type of ordering:
											</TD>
											<TD BGCOLOR="#EEEEEE">
												<SELECT NAME="direction">
													<OPTION VALUE="desc" SELECTED>descending</OPTION>
													<OPTION VALUE="asc">ascending</OPTION>
												</SELECT>
											</TD>
										</TR>
										<TR>
											<TD BGCOLOR="#EEEEEE">
												
												<P><SPACER TYPE="HORIZONTAL" SIZE="17">Number of items shown per page:&#160;&#160;
												
											</TD>
											<TD BGCOLOR="#EEEEEE">									
												<SELECT NAME="inst">
													<OPTION>10 </OPTION>
													<OPTION SELECTED>50 </OPTION>
													<OPTION>100 </OPTION>
													<OPTION>250 </OPTION>
													<OPTION>350 </OPTION>
													<OPTION>500 </OPTION>
													<OPTION>1000 </OPTION>
												</SELECT>
											</TD>
										</TR>
										<TR>
											<TD ALIGN="RIGHT" BGCOLOR="#DDDDDD">
												<INPUT TYPE="hidden" NAME="freqType" VALUE="word">
												<INPUT TYPE="hidden" NAME="urlTest" VALUE="yes">
												<INPUT TYPE="submit" VALUE="Show list">
											</TD>
											<TD ALIGN="LEFT" BGCOLOR="#DDDDDD">
												<INPUT TYPE="reset" VALUE="Reset">
											</TD>
										</TR>
										</FORM>
									</TABLE>
								</TD>
								<TD WIDTH="50%" VALIGN="TOP">
									<TABLE BORDER="0" CELLPADDING="3" CELLSPACING="3" WIDTH="100%">
										<TR>
											<TD HEIGHT="22" COLSPAN="2" BGCOLOR="#FFCC66">
												<B>Headword or lemma frequencies</B>
											</TD>
										</TR>
										<TR>
											<TD VALIGN="TOP" WIDTH="40%" BGCOLOR="#EEEEEE">
												<FORM ACTION="/$bwCGIalias/freqList.pl" METHOD="GET">
												<INPUT TYPE="HIDDEN" NAME="chunk" SIZE="-1" VALUE="1">
												<P>Choose one or more lemma classes:
											</TD>
											<TD BGCOLOR="#EEEEEE">
												<SELECT MULTIPLE SIZE=7 NAME="lemma">
													<OPTION VALUE="all"> no restrictions
													<OPTION>ADJ </OPTION>
													<OPTION>ADV </OPTION>
													<OPTION>ART </OPTION>
													<OPTION>CONJ </OPTION>
													<OPTION>INTERJ </OPTION>
													<OPTION>PREP </OPTION>
													<OPTION>PRON </OPTION>
													<OPTION>SUBST </OPTION>
													<OPTION>STOP </OPTION>
													<OPTION>VERB </OPTION>
													<OPTION>UNC </OPTION>
												</SELECT>
											</TD>
										</TR>
										<TR>
											<TD BGCOLOR="#EEEEEE">
											Word pattern: 
											</TD>
											<TD BGCOLOR="#EEEEEE">
												<SELECT NAME="regex">
													<OPTION VALUE="begin" SELECTED>starting with</OPTION>
													<OPTION VALUE="end">ending in</OPTION>
													<OPTION VALUE="contain">containing</OPTION>
												</SELECT>
												<INPUT TYPE="text" NAME="regexValue" SIZE=16>
											</TD>
										</TR>
	
										<TR>
											<TD BGCOLOR="#EEEEEE">
												<P><SPACER TYPE="HORIZONTAL" SIZE="17">Range of texts:
											</TD>
											<TD BGCOLOR="#EEEEEE">
												<SELECT NAME="spowri">
													<OPTION VALUE="all" SELECTED>Whole BNC</OPTION>
													<OPTION VALUE="s">Spoken Texts only</OPTION>
													<OPTION VALUE="w">Written Texts only</OPTION>
													$subCorpDisplay
												</SELECT>
			
											</TD>
										</TR>
										<TR>
											<TD BGCOLOR="#EEEEEE">
												<P><SPACER TYPE="HORIZONTAL" SIZE="17">Range of frequency (optional):
											</TD>
											<TD BGCOLOR="#EEEEEE">
												from <INPUT TYPE="text" NAME="minValue" SIZE=8> to
												<INPUT TYPE="text" NAME="maxValue" SIZE=8>
											</TD>
										</TR>							
										<TR>
											<TD BGCOLOR="#EEEEEE">
												<P><SPACER TYPE="HORIZONTAL" SIZE="17">Show individual tag frequencies:
											</TD>
											<TD BGCOLOR="#EEEEEE">
												<SELECT NAME="showTag">
													<OPTION VALUE="" SELECTED>no</OPTION>
													<OPTION VALUE="1">yes</OPTION>
												</SELECT>
											</TD>
										</TR>
										<TR>
											<TD BGCOLOR="#EEEEEE">
												<P><SPACER TYPE="HORIZONTAL" SIZE="17">Type of ordering:
											</TD>
											<TD BGCOLOR="#EEEEEE">
												<SELECT NAME="direction">
													<OPTION VALUE="desc" SELECTED>descending</OPTION>
													<OPTION VALUE="asc">ascending</OPTION>
												</SELECT>
											</TD>
										</TR>
										<TR>
											<TD BGCOLOR="#EEEEEE">
												
												<P><SPACER TYPE="HORIZONTAL" SIZE="17">Number of items shown per page:&#160;&#160;
												
											</TD>
											<TD BGCOLOR="#EEEEEE">									
												<SELECT NAME="inst">
													<OPTION>10 </OPTION>
													<OPTION SELECTED>50 </OPTION>
													<OPTION>100 </OPTION>
													<OPTION>250 </OPTION>
													<OPTION>350 </OPTION>
													<OPTION>500 </OPTION>
													<OPTION>1000 </OPTION>
												</SELECT>
											</TD>
										</TR>
										<TR>
											<TD ALIGN="RIGHT" BGCOLOR="#DDDDDD">
												<INPUT TYPE="hidden" NAME="freqType" VALUE="lemma">
												<INPUT TYPE="hidden" NAME="urlTest" VALUE="yes">
												<INPUT TYPE="submit" VALUE="Show list">
											</TD>
											<TD ALIGN="LEFT" BGCOLOR="#DDDDDD">
												<INPUT TYPE="reset" VALUE="Reset">
											</TD>
										</TR>
									</TABLE>
									</FORM>
								</TD>
						
							</TR>
						</TABLE>
	
						<HR>
						$bwCopyright
					</TD>
				</TR>
			</TABLE>
END
}

sub usersettings {
$sqlQuery = "SELECT user, disp_tags, disp_lines, coll_warning, database_col, crlf, 
			statType, delRare, totBNC, `range`, minusrange, disp_color, show_value, 
			view, markup, historyView, showRandom 
			FROM settings where user='$username'";
$sth = $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
$sth->execute or bark("Can't execute $sqlQuery: $sth::errstr");
$res = $sth->rows;

if ($res) {
	@rowArray=$sth->fetchrow_array;
	$setting{disp_tags}=$rowArray[1];
	$setting{disp_lines}=$rowArray[2];
	$setting{coll_warning}=$rowArray[3];
	$setting{database_col}=$rowArray[4];
	$setting{crlf}=$rowArray[5];
	$setting{statType}=$rowArray[6];
	$setting{delRare}=$rowArray[7];
	$setting{totBNC}=$rowArray[8];
	$setting{range}=$rowArray[9];
	$setting{minusrange}=$rowArray[10];
	$setting{color}=$rowArray[11];
	$setting{show_value}=$rowArray[12];
	$setting{view}=$rowArray[13];
	$setting{markup}=$rowArray[14];
	$setting{historyView}=$rowArray[15];
	$setting{showRandom}=$rowArray[16];
	
	$selected{$setting{disp_tags}}="SELECTED";
	$selected1{$setting{disp_lines}}="SELECTED";
	$selected2{$setting{coll_warning}}="SELECTED";
	$selected3{$setting{database_col}}="SELECTED";
	$selected4{$setting{crlf}}="SELECTED";
	$selected5{$setting{statType}}="SELECTED";
	$selected6{$setting{delRare}}="SELECTED";
	$selected7{$setting{totBNC}}="SELECTED";
	$selected8{$setting{range}}="SELECTED";
	$selected9{$setting{minusrange}}="SELECTED";
	$selected10{$setting{color}}="SELECTED";
	$selected11{$setting{show_value}}="SELECTED";
	$selected12{$setting{view}}="SELECTED";
	$selected13{$setting{markup}}="SELECTED";
	$selected14{$setting{historyView}}="SELECTED";
	$selected15{$setting{showRandom}}="SELECTED";

} else {
	$sqlQuery = "INSERT INTO settings (user, disp_tags, disp_lines, coll_warning, database_col, crlf, statType, delRare, totBNC, `range`, 
		minusrange, disp_color, show_value, view, new_features, markup, historyView, showRandom) VALUES ('$username', 0, 10, 1, 4, 0, 6, 5, 5, 3, -3, 0, 'yes', 'list', 0, 1, 'simple', 'no')";
	$bncHandle->do($sqlQuery) or bark("Can't insert $sqlQuery: " . $bncHandle::errstr);
	$selected{0}="SELECTED";
	$selected1{10}="SELECTED";
	$selected2{1}="SELECTED";
	$selected3{4}="SELECTED";
	$selected4{0}="SELECTED";
	$selected5{2}="SELECTED";
	$selected6{5}="SELECTED";
	$selected7{5}="SELECTED";
	$selected8{3}="SELECTED";
	$selected9{-3}="SELECTED";
	$selected10{0}="SELECTED";
	$selected11{alltyp}="SELECTED";
	$selected12{list}="SELECTED";
	$selected13{1}="SELECTED";
	$selected14{simple}="SELECTED";
	$selected15{'no'}="SELECTED";
}

$theMinus="&#45;";


print <<END;
<FORM ACTION="/$bwCGIalias/dlogs.pl" METHOD="GET"> 
			<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="6" WIDTH="100%">
				<TR>
					<TD WIDTH="100%" HEIGHT="22" BGCOLOR="#FFCC99">
						<H3 ALIGN="CENTER">User Settings
					</TD>
				</TR>
				<TR>
					<TD WIDTH="100%">
						<TABLE BORDER="0" CELLPADDING="4" CELLSPACING="3" WIDTH="100%">
							<TR ALIGN="left" BGCOLOR="#CCCCCC">
								<TD COLSPAN="2">
									<B><BIG>Display options</BIG></B>
							</TR>
							<TR BGCOLOR="#DDDDDD">
								<TD VALIGN="TOP">
									<B>Default view:</B>&#160;&#160;
								</TD>
								<TD VALIGN="TOP">
									<SELECT NAME="view">
										<OPTION VALUE="list" $selected12{list}> Sentence view </OPTION>
										<OPTION VALUE="kwic" $selected12{kwic}> KWIC-view </OPTION>
									</SELECT>
								</TD>
							</TR>
							<TR BGCOLOR="#DDDDDD">
								<TD VALIGN="TOP">
									<B>Default display order of concordances:</B>&#160;&#160;
								</TD>
								<TD VALIGN="TOP">
									<SELECT NAME="showRandom">
										<OPTION VALUE="no" $selected15{no}> corpus order </OPTION>
										<OPTION VALUE="yes" $selected15{yes}> random order </OPTION>
									</SELECT>
								</TD>
							</TR>
							<TR BGCOLOR="#DDDDDD">
								<TD VALIGN="TOP">
									<B>Show simple query translated into CQP-Syntax (in title bar and query history):</B>&#160;&#160;
								</TD>
								<TD VALIGN="TOP">
									<SELECT NAME="historyView">
										<OPTION VALUE="simple" $selected14{simple}> no </OPTION>
										<OPTION VALUE="CQP" $selected14{CQP}> yes </OPTION>
									</SELECT>
								</TD>
							</TR>
							<TR BGCOLOR="#DDDDDD">
								<TD VALIGN="TOP">
									<B>Context display:</B>&#160;&#160;
								</TD>
								<TD VALIGN="TOP">
									<SELECT NAME="disp_tags">
										<OPTION VALUE="0" $selected{0}> without tags </OPTION>
										<OPTION VALUE="1" $selected{1}> with tags </OPTION>
									</SELECT>
								</TD>
							</TR>
							<TR BGCOLOR="#DDDDDD">
								<TD></TD><TD VALIGN="TOP">
									<SELECT NAME="disp_color">
										<OPTION VALUE="0" $selected10{0}> Don't colour word classes </OPTION>
										<OPTION VALUE="1" $selected10{1}> Colour word classes </OPTION>
									</SELECT>
								</TD>
							</TR>
							<TR BGCOLOR="#DDDDDD">
								<TD VALIGN="TOP">
									<P ALIGN="LEFT"><B>Context size:&#160;&#160;</B>
								</TD>
								<TD VALIGN="TOP">
									<SELECT NAME="disp_lines">
										<OPTION VALUE="10" $selected1{10}> 10 &lt;s&gt;-units
										<OPTION VALUE="20" $selected1{20}> 20 &lt;s&gt;-units
										<OPTION VALUE="30" $selected1{30}> 30 &lt;s&gt;-units
										<OPTION VALUE="40" $selected1{40}> 40 &lt;s&gt;-units
										<OPTION VALUE="50" $selected1{50}> 50 &lt;s&gt;-units
										<OPTION VALUE="60" $selected1{60}> 60 &lt;s&gt;-units
									</SELECT>


								</TD>
							</TR>
							<TR BGCOLOR="#DDDDDD">
								<TD VALIGN="TOP">
									<P ALIGN="LEFT"><B>Show tooltips (JavaScript enabled browsers only):&#160;&#160;</B>
								</TD>
								<TD VALIGN="TOP">
									<SELECT NAME="show_value">
										<OPTION VALUE="yes" $selected11{yes}> Yes </OPTION>
										<OPTION VALUE="no" $selected11{no}> No </OPTION>
									</SELECT>

								</TD>
							</TR>
							<TR BGCOLOR="#EEEEEE">
								<TD COLSPAN="2">
									
										When moving the mouse over some links (e.g. in the query result window), additional 
										information will be displayed in tooltips (e.g. the immediate context with 
										part-of-speech tags).
									
								</TD>
							</TR>
							<TR ALIGN="left" BGCOLOR="#CCCCCC">
								<TD COLSPAN="2">
									<B><BIG>Collocation options</BIG></B>
								</TD>
							</TR>

							<TR BGCOLOR="#DDDDDD">
								<TD HEIGHT="32" VALIGN="TOP">
									<P ALIGN="LEFT"><B>Collocation calculation method:</B>&#160;&#160;
								</TD>
								<TD HEIGHT="32" VALIGN="TOP">
									<SELECT NAME="stat_method">
										<OPTION VALUE="1" $selected5{1}> Mutual Information </OPTION>
										<OPTION VALUE="2" $selected5{2}> MI3 </OPTION>
										<OPTION VALUE="3" $selected5{3}> Z-score</OPTION>
										<OPTION VALUE="4" $selected5{4}> T-score </OPTION>
										<OPTION VALUE="6" $selected5{6}> Log-likelihood </OPTION>
										<OPTION VALUE="7" $selected5{7}> Dice coefficient </OPTION>
										<OPTION VALUE="0" $selected5{0}> Rank by frequency </OPTION>
									</SELECT>
								</TD>
							</TR>
							<TR BGCOLOR="#EEEEEE">
								<TD COLSPAN="2">
										Determines the default calculation method for collocations
								</TD>
							</TR>
							<TR BGCOLOR="#DDDDDD">
								<TD HEIGHT="32" VALIGN="TOP">
									<P ALIGN="LEFT"><B>Frequency of collocate in BNC:</B>&#160;&#160;
								</TD>
								<TD HEIGHT="32" VALIGN="TOP">
									<SELECT NAME="totBNC">
										<OPTION $selected7{1}>1
										<OPTION $selected7{2}>2
										<OPTION $selected7{3}>3
										<OPTION $selected7{4}>4
										<OPTION $selected7{5}>5
										<OPTION $selected7{6}>6
										<OPTION $selected7{7}>7
										<OPTION $selected7{8}>8
										<OPTION $selected7{9}>9
										<OPTION $selected7{10}>10
										<OPTION $selected7{15}>15
										<OPTION $selected7{20}>20
										<OPTION $selected7{50}>50
										<OPTION $selected7{100}>100
										<OPTION $selected7{500}>500
										<OPTION $selected7{1000}>1000
										<OPTION $selected7{5000}>5000
										<OPTION $selected7{10000}>10000
										<OPTION $selected7{20000}>20000
										<OPTION $selected7{50000}>50000
									</SELECT>
								</TD>
							</TR>
							<TR BGCOLOR="#EEEEEE">
								<TD COLSPAN="2">
									Collocate must occur at least n times in the BNC to be displayed.
								</TD>
							</TR>
							<TR BGCOLOR="#DDDDDD">
								<TD HEIGHT="32" VALIGN="TOP">
									<P ALIGN="LEFT"><B>Minimum number of collocations:</B>&#160;&#160;
								</TD>
								<TD HEIGHT="32" VALIGN="TOP">
									<SELECT NAME="delRare">
										<OPTION $selected6{1}>1 </OPTION>
										<OPTION $selected6{2}>2 </OPTION>
										<OPTION $selected6{3}>3 </OPTION>
										<OPTION $selected6{4}>4 </OPTION>
										<OPTION $selected6{5}>5 </OPTION>
										<OPTION $selected6{6}>6 </OPTION>
										<OPTION $selected6{7}>7 </OPTION>
										<OPTION $selected6{8}>8 </OPTION>
										<OPTION $selected6{9}>9 </OPTION>
										<OPTION $selected6{10}>10 </OPTION>
										<OPTION $selected6{15}>15 </OPTION>
										<OPTION $selected6{20}>20 </OPTION>
										<OPTION $selected6{50}>50 </OPTION>
										<OPTION $selected6{100}>100 </OPTION>
									</SELECT>
								</TD>
							</TR>
							<TR BGCOLOR="#EEEEEE">
								<TD COLSPAN="2">
									Node and collocate must occur at least n times to be displayed.
								</TD>
							</TR>
							<TR BGCOLOR="#DDDDDD">
								<TD HEIGHT="32" VALIGN="TOP">
									<P ALIGN="LEFT"><B>Calculation range for collocations:</B>&#160;&#160;
								</TD>
								<TD HEIGHT="32" VALIGN="TOP">
								<SELECT NAME="minusrange">
									<OPTION VALUE="-10" $selected9{-10}>10 left
									<OPTION VALUE="-9" $selected9{-9}>9 left
									<OPTION VALUE="-8" $selected9{-8}>8 left
									<OPTION VALUE="-7" $selected9{-7}>7 left
									<OPTION VALUE="-6" $selected9{-6}>6 left
									<OPTION VALUE="-5" $selected9{-5}>5 left
									<OPTION VALUE="-4" $selected9{-4}>4 left
									<OPTION VALUE="-3" $selected9{-3}>3 left
									<OPTION VALUE="-2" $selected9{-2}>2 left
									<OPTION VALUE="-1" $selected9{-1}>1 left
									<OPTION VALUE="1" $selected9{1}>1 right
									<OPTION VALUE="2" $selected9{2}>2 right
									<OPTION VALUE="3" $selected9{3}>3 right
									<OPTION VALUE="4" $selected9{4}>4 right
									<OPTION VALUE="5" $selected9{5}>5 right
									<OPTION VALUE="6" $selected9{6}>6 right
									<OPTION VALUE="7" $selected9{7}>7 right
									<OPTION VALUE="8" $selected9{8}>8 right
									<OPTION VALUE="9" $selected9{9}>9 right
									<OPTION VALUE="10" $selected9{10}>10 right
								</SELECT>
								to
								<SELECT NAME="range">
									<OPTION VALUE="-10" $selected8{-10}>10 left
									<OPTION VALUE="-9" $selected8{-9}>9 left
									<OPTION VALUE="-8" $selected8{-8}>8 left
									<OPTION VALUE="-7" $selected8{-7}>7 left
									<OPTION VALUE="-6" $selected8{-6}>6 left
									<OPTION VALUE="-5" $selected8{-5}>5 left
									<OPTION VALUE="-4" $selected8{-4}>4 left
									<OPTION VALUE="-3" $selected8{-3}>3 left
									<OPTION VALUE="-2" $selected8{-2}>2 left
									<OPTION VALUE="-1" $selected8{-1}>1 left
									<OPTION VALUE="1" $selected8{1}>1 right
									<OPTION VALUE="2" $selected8{2}>2 right
									<OPTION VALUE="3" $selected8{3}>3 right
									<OPTION VALUE="4" $selected8{4}>4 right
									<OPTION VALUE="5" $selected8{5}>5 right
									<OPTION VALUE="6" $selected8{6}>6 right
									<OPTION VALUE="7" $selected8{7}>7 right
									<OPTION VALUE="8" $selected8{8}>8 right
									<OPTION VALUE="9" $selected8{9}>9 right
									<OPTION VALUE="10" $selected8{10}>10 right
								</SELECT>
								</TD>
							</TR>
							<TR BGCOLOR="#EEEEEE">
								<TD COLSPAN="2">
									Determines the range for which collocation values are to be displayed.
								</TD>
							</TR>
<!--							<TR BGCOLOR="#DDDDDD">
								<TD HEIGHT="32" VALIGN="TOP">
									<P ALIGN="LEFT"><B>Collocation sanity check:</B>&#160;&#160;
								</TD>
								<TD HEIGHT="32" VALIGN="TOP"> 
									<SELECT NAME="coll_warning">
										<OPTION VALUE="0" $selected2{0}> Don't warn me </OPTION>
										<OPTION VALUE="1" $selected2{1}> Warn me </OPTION>
									</SELECT>
								</TD>
							</TR>
							<TR BGCOLOR="#EEEEEE">
								<TD COLSPAN="2">
									Displays a warning if the collocation feature is selected even though the search
									was based on a subcorpus of the BNC (other than spoken/written) 
								</TD>
							</TR>
--!>							<TR ALIGN="left" BGCOLOR="#CCCCCC">
								<TD COLSPAN="2">
									<B><BIG>Dowload options</BIG></B>
								</TD>
							</TR>


							<TR BGCOLOR="#DDDDDD">
								<TD VALIGN="TOP">
									<P ALIGN="LEFT"><B>Line breaks:</B>&#160;&#160;
								</TD>
								<TD VALIGN="TOP">
									<SELECT NAME="crlf">
										<OPTION VALUE="0" $selected4{0}> Automatic
										<OPTION VALUE="1" $selected4{1}> Macintosh (OS 9 and below)
										<OPTION VALUE="2" $selected4{2}> Windows
										<OPTION VALUE="3" $selected4{3}> Unix (incl. Mac OS X)
									</SELECT>
								</TD>
							</TR>
							<TR BGCOLOR="#EEEEEE">
								<TD COLSPAN="2">
									When saving to disk, this determines the format of the line breaks. Use this switch
									to override the default setting if you want to use the file on another system.									</BLOCKQUOTE>
								</TD>
							</TR>
							<TR BGCOLOR="#CCCCCC">
								<TD ALIGN="RIGHT">
									<INPUT TYPE="hidden" NAME="selected" VALUE="updatesettings">
									<INPUT TYPE="hidden" NAME="urlTest" VALUE="yes">
									<INPUT TYPE="submit" VALUE="Update Settings">
								</TD>
								<TD ALIGN="LEFT">
									<INPUT TYPE="reset" VALUE="Reset Settings">
								</TD>
							</TR>
						</TABLE>
					<HR>
					$bwCopyright</TD>
				</TR>
			</TABLE>
		</FORM>
END
}

sub newfeatures {

print <<END;
			<P>
			<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="6" WIDTH="100%">
				<TR>
					<TD WIDTH="100%" HEIGHT="22" BGCOLOR="#FF9999">
						<H3 ALIGN="CENTER"><B>New features/version history:</B>
					</TD>
				</TR>
				<TR>
					<TD WIDTH="100%">
						<TABLE BORDER="0">
							<TR>
								<TD ALIGN="LEFT" VALIGN="TOP" WIDTH="30%">January 2010</TD>
								<TD  WIDTH="70%" VALIGN="TOP">
									<B>Version 4.3</B><ul><li> Minor bug-fixes. Updated installation routine.</li></ul>
								</TD>
							</TR>
							<TR>
								<TD ALIGN="LEFT" VALIGN="TOP" WIDTH="30%">October 2008</TD>
								<TD  WIDTH="70%" VALIGN="TOP">
									<B>Version 4.2</B><ul><li> Final update to the Simple query syntax.</li></ul>
								</TD>
							</TR>
							<TR>
								<TD ALIGN="LEFT" VALIGN="TOP" WIDTH="30%">May 2008</TD>
								<TD  WIDTH="70%" VALIGN="TOP">
									<B>Version 4.1</B><ul><li> Lots of minor updates from Version 4.0. Apart from the Simple query syntax, this is the version described in 
									<I>Corpus Linguistics with </I>BNCweb<i> - A practical Guide</i> by Hoffmann, Evert, Smith, Lee and Berglund-Prytz (Peter Lang, 2008).</li></ul>
								</TD>
							</TR>
							<TR>
								<TD ALIGN="LEFT" VALIGN="TOP" WIDTH="30%">November 2007</TD>
								<TD  WIDTH="70%" VALIGN="TOP">
									<B>Version 4.0</B><ul><li> Public release of BNCweb (CQP-edition) for the XML version of the BNC.</li></ul>
								</TD>
							</TR>
							<TR><TD>&nbsp;</TD></TR>
							<TR>
								<TD ALIGN="LEFT" VALIGN="TOP" WIDTH="30%">July 2005</TD>
								<TD VALIGN="TOP">
									<B>Version 3.0</B><ul><li> First version of BNCweb to work with CQP - Zurich University internal use only</li></ul>
								</TD>
							</TR>
							<TR><TD>&nbsp;</TD></TR>
							<TR>
								<TD ALIGN="LEFT" VALIGN="TOP" WIDTH="30%">May 2002</TD>
								<TD VALIGN="TOP">
									<B>Version 2.0</B> <ul><li> First public release</li></ul>
								</TD>
							</TR>
							<TR><TD>&nbsp;</TD></TR>
							<TR>
								<TD ALIGN="LEFT" VALIGN="TOP" WIDTH="30%">1996-2000</TD>
								<TD VALIGN="TOP">
									<B>Version 1.0 </B><ul><li>  Zurich University internal use only</li></ul>
								</TD>
							</TR>
						</TABLE>

	<HR>
$bwCopyright
					</TD>
				</TR>
			</TABLE>

END

}

sub team {

	print <<END;
		<P>
		<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="6" WIDTH="100%">
			<TR>
				<TD WIDTH="100%" HEIGHT="22" BGCOLOR="#FF9999">
					<H3 ALIGN="CENTER">The BNC<I>web</I> team<B>:</B>
				</TD>
			</TR>
			<TR>
				<TD WIDTH="100%">
					<H3></H3>The CQP-edition of BNCweb (Versions 3 and 4) was developed by <B>Sebastian Hoffmann</B> (Lancaster University) and <B>Stefan Evert</B> (University of Osnabr&uuml;ck).<P>
					<P>The original BNCweb interface (versions 1 and 2) was a joint project of three people:</P>
					<P><B>Hans-Martin Lehmann</B> was the project coordinator. He also wrote its core perl scripts.</P>
					<P><B>Sebastian Hoffmann</B> did most of the work concerning the database-centered features (e.g. distribution,
					collocation, sort, and tag-sequence search features). He also adapted BNCweb to work with the BNC World Edition.</P>
					<P><B>Peter Schneider</B> was the system administrator. He assembled a whole range of servers
					and kept the linux operating system up and running. He's also behind much of BNCweb's external appearance.</P>
					<HR>
					$bwCopyright 
					<SPACER TYPE="VERTICAL" SIZE="10">

				</TD>
			</TR>
		</TABLE>
END

}

sub book {

	print <<END;
		<P>
		<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="6" WIDTH="100%">
			<TR>
				<TD WIDTH="100%" HEIGHT="22" BGCOLOR="#FF9999">
					<H3 ALIGN="CENTER">Corpus Linguistics with BNCweb</B>
				</TD>
			</TR>
			<TR>
				<TD WIDTH="100%">
<table cellpadding="6">
<tr>

<td VALIGN="top">
	<img src="/$bwHTMLalias/cover_250.jpg" alt="Cover">
</td>
<td VALIGN="top">
<b>Hoffmann, Sebastian, Evert, Stefan, Smith, Nicholas, Lee, David and Ylva  Berglund Prytz. 2008. <i>Corpus Linguistics with </i>BNCweb<i> - a Practical Guide.</i>  Frankfurt am Main: Peter Lang.</b>
<p><b>Short-listed for the <a href="http://www.baal.org.uk/bkprize_about.htm">BAAL Book Prize</a> 2009</b>.</p>

<P>Back cover description:<br>This book presents a richly illustrated, hands-on discussion of one of the fastest growing fields in linguistics today. 
The authors address key methodological issues in corpus linguistics, such as collocations, keywords and the categorization of concordance lines. 
They show how these topics can be explored step-by-step with BNCweb, a user-friendly web-based tool that supports sophisticated analyses of the 100-million-word British National Corpus. 
Indeed, the BNC and BNCweb have been described by Geoffrey Leech as "an unparalleled combination of facilities for finding out about the English language of the present day" (Foreword). 
The book contains tasks and exercises, and is suitable for undergraduates, postgraduates and experienced corpus users alike.</p>

<p>Sample Chapter: <a href="/$bwHTMLalias/Hoffmann_et_al_sample.pdf">Table of Contents and Chapter 1</a></p>

<p><b>Buying the book:</b> The book is now available through <a href="http://www.amazon.co.uk/s/ref=nb_ss_w_h_?url=search-alias%3Daps&field-keywords=bncweb&x=0&y=0">amazon.co.uk</a>, 
<a href="http://www.amazon.de/Corpus-Linguistics-BNCweb-Practical-Guide/dp/3631563159/ref=sr_1_1?ie=UTF8&s=books&qid=1225564404&sr=1-1">amazon.de</a> and <a href="http://www.amazon.com/Corpus-Linguistics-Bncweb-Practical-English/dp/3631563159/ref=sr_1_1?ie=UTF8&s=books&qid=1231343847&sr=8-1">amazon.com</a>. 
It can also be ordered directly online through <a href="http://www.peterlang.com/index.cfm?vID=56315&vLang=E&vHR=1&vUR=2&vUUR=1">Peter Lang</a>. 
You can also fill in this <a href="/$bwHTMLalias/Corpus_Linguistics_with_BNCweb.pdf">order form</a> and send it to Peter Lang. 
</td>
</tr>
</table>
					<HR>
					$bwCopyright 
					<SPACER TYPE="VERTICAL" SIZE="10">

				</TD>
			</TR>
		</TABLE>
END

}


sub bugreports {

	print <<END;
			<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="6" WIDTH="100%">
			<TR>
				<TD WIDTH="100%" HEIGHT="22" BGCOLOR="#FF9999">
					<H3 ALIGN="CENTER">Bug reports<B>:</B>
				</TD>
			</TR>
			<TR>
				<TD WIDTH="100%">
					<H3></H3>
					<P>We have tried our best to eliminate all bugs and inconsistencies. 
					If you find anything that seems strange to you please let us know. We are also happy for any kind of suggestions 
					(e.g. additional features, improving user-friendliness). Thank you for your help
					in improving BNCweb!</P>
					<P>Please send your comments, questions or bug reports to <A HREF="mailto:bncweb\@mac.com">bncweb\@mac.com
					</A>.</P>
					<HR>
					$bwCopyright 
					<SPACER TYPE="VERTICAL" SIZE="10">

				</TD>
			</TR>
		</TABLE>


END

}

sub subcorpus {
		
	$link = '<A HREF="'. $ENV{'HTTP_REFERER'} . '">back</A>';
	$optionSelector="";
	$thing = 'Location: /' . $bwCGIalias . '/BNCquery.pl?theQuery=writtentexts&showBox=no&urlTest=yes';
	$optionSelector .= "<OPTION SELECTED VALUE=\"$thing\">Written metatextual categories";
	$thing = 'Location: /' . $bwCGIalias . '/BNCquery.pl?theQuery=spokentexts&showBox=no&urlTest=yes';
	$optionSelector .= "<OPTION VALUE=\"$thing\">Spoken metatextual categories";
	$thing = 'Location: /' . $bwCGIalias . '/BNCquery.pl?theQuery=genres&urlTest=yes';
	$optionSelector .= "<OPTION VALUE=\"$thing\">Genre labels";
	$thing = 'Location: /' . $bwCGIalias . '/BNCquery.pl?theQuery=keyword&urlTest=yes';
	$optionSelector .= "<OPTION VALUE=\"$thing\">Keyword/title scan\n";
	$thing = 'Location: /' . $bwCGIalias . '/subcorpAdmin.pl?action=define&username=' . $username . '&textOrSpeaker=text&urlTest=yes';
	$optionSelector .= "<OPTION VALUE=\"$thing\">Manual entry of text IDs\n";
	$thing = 'Location: /' . $bwCGIalias . '/subcorpAdmin.pl?action=define2&username=' . $username . '&textOrSpeaker=speaker&urlTest=yes';
	$optionSelector .= "<OPTION VALUE=\"$thing\">Manual entry of speaker IDs\n";
	if (($username=~m/\A($bwSuperuser)\Z/) || (&checkSuperUser2())) {
		$thing = 'Location: /' . $bwCGIalias . '/BNCquery.pl?theQuery=subcorpus&info=showall&urlTest=yes';
		$optionSelector .= "<OPTION VALUE=\"$thing\">Show subcorpora of all users\n";
	}
	
	print <<END;
	
			<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="6" WIDTH="100%">

				<FORM ACTION="/$bwCGIalias/dlogs2.pl" METHOD=GET>
				<TR>
					<TD WIDTH="30%" HEIGHT="42" NOWRAP>
						<B>Define new subcorpus via:</B>
					</TD>
					<TD WIDTH="70%">
						
						<SELECT NAME="selected">
							$optionSelector
						</SELECT>
						<INPUT TYPE="submit" VALUE="Go">
						
					</TD>
					
				</TR>


				</FORM>
				<TR>
					<TD WIDTH="100%" HEIGHT="22" BGCOLOR="#FFCC99" COLSPAN="2">
						<H3 ALIGN="CENTER"><B>User-defined Subcorpora</B>
					</TD>
				</TR>
				<TR>
					<TD WIDTH="100%" COLSPAN="2">
END

	$qName="INIT";
	
	# connect to mysql
	$bncHandle = DBI->connect("DBI:mysql:$bwMYSQLtable:$bwMYSQLhost:$bwMysqlPort", "$bwMysqlUser", "$bwMysqlPwd") or bark("Can't connect to mysql on $bwMYSQLhost at port: $bwMysqlPort <BR>$bncHandle::errstr\n");
	if (($params{info} =~ m/showall/) && (($username=~m/\A($bwSuperuser)\Z/) || (&checkSuperUser2()))) {
		$sqlQuery = "SELECT username, subcorpus, numfiles, numwords, spowri, textOrSpeaker FROM subcorpora ORDER BY username, subcorpus";
		$sth1 = $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
		$sth1->execute or bark("Can't execute $sqlQuery: $sth::errstr");
		$res = $sth1->rows;
	} else {
		$sqlQuery = "SELECT username, subcorpus, numfiles, numwords, spowri, textOrSpeaker, fnums FROM subcorpora WHERE username='$username' ORDER BY subcorpus";
		$sth1 = $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
		$sth1->execute or bark("Can't execute $sqlQuery: $sth::errstr");
		$res = $sth1->rows;		
	}
	
	if (!$res) {
		print "<H3>&nbsp;There are no subcorpora for user $username</H3>";
		#return();
	} else {
	    if (($params{info} =~ m/showall/) && (($username=~m/\A($bwSuperuser)\Z/) || (&checkSuperUser2()))) {
	    	print "<TABLE BORDER=\"0\" CELLPADDING=\"2\" CELLSPACING=\"4\" WIDTH=\"100%\"><TR><TD BGCOLOR=\"#FFCC66\">&#160;<B>Name of subcorpus</B></TD><TD BGCOLOR=\"#FFCC66\" ALIGN=CENTER><B>Owner</B></TD<TD BGCOLOR=\"#FFCC66\" ALIGN=CENTER><B>No. of texts/speakers</B></TD><TD BGCOLOR=\"#FFCC66\" ALIGN=CENTER><B>No. of files</B></TD><TD BGCOLOR=\"#FFCC66\" ALIGN=CENTER><B>No. of words</B></TD><TD BGCOLOR=\"#FFCC66\"><B>Frequency list</B></TD><TD BGCOLOR=\"#FFCC66\"><B>Action</B></TD></TR>\n";
		} else {
	    	print "<TABLE BORDER=\"0\" CELLPADDING=\"2\" CELLSPACING=\"4\" WIDTH=\"100%\"><TR><TD BGCOLOR=\"#FFCC66\">&#160;<B>Name of subcorpus</B></TD><TD BGCOLOR=\"#FFCC66\" ALIGN=CENTER><B>No. of texts/speakers</B></TD><TD BGCOLOR=\"#FFCC66\" ALIGN=CENTER><B>No. of words</B></TD><TD BGCOLOR=\"#FFCC66\"><B>Frequency list</B></TD><TD BGCOLOR=\"#FFCC66\"><B>Action</B></TD></TR>\n";
	    }
	    $totalSize=0;
	    $first=0;
	    $oldOwner="";

	    for ($i = 0; $i < $res; $i++) {
	    	print "<TR BGCOLOR=\"#DDDDDD\">\n";
	    	@rowArray=$sth1->fetchrow_array;
			$theName=$rowArray[0];
			$subcorpus=$rowArray[1];
			$numfiles=$rowArray[2];
			$numwords=$rowArray[3];
			$wrispo = $rowArray[4];
			$textOrSpeaker = $rowArray[5];
			$fnums = $rowArray[6];
			$numwords=&makeDecimals($numwords);
			if ($textOrSpeaker eq "text") {
				$sp = "texts";
			} else {
				$sp = "speakers";
			}
			
			# check whether frequency lists are cached
			($fnumList, $textOrSpeaker) = &subCorporaList($theName, $subcorpus);
			
			$sqlQuery = "SELECT * FROM frequencyLists where subcorpus=\"$fnumList\"";
		
			$sth= $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
			$sth->execute or bark("Can't execute $sqlQuery: " . $sth::errstr);
			$cached = $sth->rows;

			
			$indexBut = '<TD><A HREF="/' . $bwCGIalias . '/subcorpAdmin.pl?action=showcorpus&chunk=1&max=INIT&inst=50&subcorpus=' . $subcorpus . '&spowri=' . $wrispo . '&username=' . $theName . '&info=' . $paramsEnc{info} . '&textOrSpeaker=' . $textOrSpeaker . '&urlTest=yes">' . $subcorpus . '</A></TD>';
			print "$indexBut\n";

			$wrispo = "spoken" if ($wrispo eq "wrispo");
			
			if (($params{info} =~ m/showall/) && (($username=~m/\A($bwSuperuser)\Z/) || (&checkSuperUser2()))) {
				print "<TD ALIGN=CENTER>$theName</TD><TD ALIGN=CENTER>$numfiles $sp</TD><TD ALIGN=CENTER>$numwords</TD>";
			} else {
				print "<TD ALIGN=CENTER>$numfiles $sp</TD><TD ALIGN=CENTER>$numwords</TD>";
			}
						
			unless ($subcorpus=~m/\Alast_(spoken|written)\Z/) {
				if ($cached) {
					print "<TD>Available</TD>";
				} else {
					$delBut = '<TD><A HREF="/' . $bwCGIalias . '/subcorpAdmin.pl?action=compile&subcorpus=' . $subcorpus . '&username=' . $theName . '&info=' . $paramsEnc{info} . '&urlTest=yes">Compile</TD>';
					print "$delBut\n";
				}
			} else {
				print "<TD>n/a</TD>";
			}
			$delBut = '<TD><A HREF="/' . $bwCGIalias . '/subcorpAdmin.pl?action=delete&subcorpus=' . $subcorpus . '&username=' . $theName . '&info=' . $paramsEnc{info} . '&urlTest=yes">Delete</A>&nbsp;&nbsp;
						   <A HREF="/' . $bwCGIalias . '/subcorpAdmin.pl?action=choose&duplicate=' . $subcorpus . '&subcorpus=new&username=' . $theName . '&info=' . $paramsEnc{info} . '&urlTest=yes">Copy</A>
						   </A></TD>';
			print "$delBut\n";
			print "</TR>\n";
	    }
	    print "</TABLE><TR><TD COLSPAN=\"2\">\n";
	}

	print <<END;
	<HR>$bwCopyright
	</TD></TR></TABLE>
END

}

sub keywords_stat {	
	
	print <<END;
	
			<TABLE BORDER="0" CELLPADDING="6" CELLSPACING="3" WIDTH="100%">

				<TR>
					<TD WIDTH="100%" HEIGHT="22" BGCOLOR="#CC99FF" COLSPAN="4">
						<H3 ALIGN="CENTER"><B>Keywords</B>
					</TD>
				</TR>
END

	$qName="INIT";
	
	# connect to mysql
	$bncHandle = DBI->connect("DBI:mysql:$bwMYSQLtable:$bwMYSQLhost:$bwMysqlPort", "$bwMysqlUser", "$bwMysqlPwd") or bark("Can't connect to mysql on $bwMYSQLhost at port: $bwMysqlPort <BR>$bncHandle::errstr\n");

	if (($params{info} =~ m/showall/) && (($username=~m/\A($bwSuperuser)\Z/) || (&checkSuperUser2()))) {
		$sqlQuery = "SELECT username, subcorpus, numfiles, numwords, spowri, textOrSpeaker FROM subcorpora where subcorpus <> 'last_written' and subcorpus <> 'last_spoken' ORDER BY username, subcorpus";
		$sth1 = $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
	} else {
		$sqlQuery = "SELECT username, subcorpus, numfiles, numwords, spowri, textOrSpeaker, fnums FROM subcorpora WHERE username='$username' and subcorpus <> 'last_written' and subcorpus <> 'last_spoken' ORDER BY subcorpus";
		$sth1 = $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
	}
	
	$sth1->execute or bark("Can't execute $sqlQuery: $sth::errstr");
	$res = $sth1->rows;
	
	$cacheList = "<OPTION VALUE=\"allWords##all\">Whole BNC
					<OPTION VALUE=\"allWordsW##1\">Written component
					<OPTION VALUE=\"allWordsS##2\">Spoken component
	";

	
	if (!$res) {
		# only do keywords for precompiled lists
	} else {
		for ($i = 0; $i < $res; $i++) {
			@rowArray=$sth1->fetchrow_array;
			$subcorpUsername=$rowArray[0];
			$subcorpus=$rowArray[1];
#			$numfiles=$rowArray[2];
			$numwords=$rowArray[3];
#			$wrispo = $rowArray[4];
#			$textOrSpeaker = $rowArray[5];
#			$fnums = $rowArray[6];
			$numwords=&makeDecimals($numwords);
			if ($textOrSpeaker eq "text") {
				$sp = "texts";
			} else {
				$sp = "speakers";
			}
			
			# check whether frequency lists are cached
			($fnumList, $textOrSpeaker) = &subCorporaList($subcorpUsername, $subcorpus);
			
			$sqlQuery = "SELECT theName FROM frequencyLists where subcorpus=\"$fnumList\"";
		
			$sth2= $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
			$sth2->execute or bark("Can't execute $sqlQuery: " . $sth2::errstr);
			$cached = $sth2->rows;
			
			if ($cached) {
				@rowArray=$sth2->fetchrow_array;
				$theCachedName= $rowArray[0] . "##" . $subcorpus;
				
				$cacheList .= "<OPTION VALUE=\"$theCachedName\">$subcorpus";
			}
		}
	}

print <<END;
	<TR BGCOLOR="#DDDDDD">
		<TD WIDTH="20%" HEIGHT="42" NOWRAP>
		<FORM ACTION="/$bwCGIalias/keywords.pl" METHOD=GET>
			<B>Select frequency list 1:</B>
		</TD>
		<TD WIDTH="30%" ALIGN=LEFT>
			<SELECT NAME="table1">
				$cacheList
			</SELECT>
			
			
		</TD>
		<TD WIDTH="20%" HEIGHT="42" NOWRAP>
			<B>Select frequency list 2:</B>
		</TD>
		<TD WIDTH="30%">
			
			<SELECT NAME="table2">
				$cacheList
			</SELECT>
			
		</TD>
		
	</TR>
	<TR BGCOLOR="#DDDDDD">
		<TD HEIGHT="42">
			<B>Compare:</B>
		</TD>
		<TD COLSPAN="3">
			<SELECT NAME="compare">
				<OPTION VALUE="word">Words
				<OPTION VALUE="wordtag">Word + POS-tag combinations
				<OPTION VALUE="lemma">Lemmas
			</SELECT>
		</TD>
	</TR>
	<TR BGCOLOR="#EEEEEE"><TD COLSPAN="4"></TD></TR>
	<TR BGCOLOR="#CCCCCC">
		<TD HEIGHT="2" COLSPAN=4><B>Options for keyword analysis:</B></TD>
	</TR>
	<TR BGCOLOR="#DDDDDD">
	<TD HEIGHT="42">
			<B>Min. freq(list 1):</B>
		</TD>
		<TD >
			<SELECT NAME="f1">
				<OPTION>1
				<OPTION>2
				<OPTION>3
				<OPTION>4
				<OPTION selected>5
				<OPTION>6
				<OPTION>7
				<OPTION>8
				<OPTION>9
				<OPTION>10
				<OPTION>15
				<OPTION>20
				<OPTION>50
				<OPTION>100
				<OPTION>500
				<OPTION>1000
			</SELECT>
		</TD>
		<TD HEIGHT="42">
			<B>Min. freq(list 2):</B>
		</TD>
		<TD >
			<SELECT NAME="f2">
				<OPTION>1
				<OPTION>2
				<OPTION>3
				<OPTION>4
				<OPTION selected>5
				<OPTION>6
				<OPTION>7
				<OPTION>8
				<OPTION>9
				<OPTION>10
				<OPTION>15
				<OPTION>20
				<OPTION>50
				<OPTION>100
				<OPTION>500
				<OPTION>1000
			</SELECT>
		</TD>
	</TR>
	
	<TR BGCOLOR="#DDDDDD">
		<TD HEIGHT="42">
			<B>Method:</B>
		</TD>
		<TD>
			<SELECT NAME="statistics">
				<OPTION VALUE="LL">Log-likelihood
				<OPTION VALUE="X2">Chi-square
			</SELECT>
		</TD>
		<TD>
			<B>Significance threshold:</B>
		</TD>
		<TD>
			<SELECT NAME="threshold">
				<OPTION VALUE="5">5%
				<OPTION VALUE="1">1%
				<OPTION VALUE="0.1">0.1%
				<OPTION SELECTED VALUE="0.01">0.01%
				<OPTION VALUE="0">show all
			</SELECT>
		</TD>
	</TR>
	<TR BGCOLOR="#CCCCCC">
		<TD ALIGN=MIDDLE COLSPAN="4">
			<INPUT TYPE="submit" name="theMethod" value="Calculate keywords!">
		</TD>
	</TR>
						<INPUT TYPE="HIDDEN" NAME="tag" SIZE="-1" VALUE="all">
						<INPUT TYPE="HIDDEN" NAME="chunk" SIZE="-1" VALUE="1">
						<INPUT TYPE="HIDDEN" NAME="inst" SIZE="-1" VALUE="50">
						<INPUT TYPE="hidden" NAME="urlTest" VALUE="yes">
	<TR BGCOLOR="#EEEEEE"><TD COLSPAN="4"></TD></TR>
	<TR BGCOLOR="#CCCCCC">
		<TD HEIGHT="2" COLSPAN=4><B>Options for comparing frequency lists:</B></TD>
	</TR>
	<TR BGCOLOR="#DDDDDD">
		<TD HEIGHT="42" COLSPAN="2">
			<B>Display words that only occur in:</B>
		</TD>
		<TD COLSPAN="2">
			<SELECT NAME="empty">
				<OPTION VALUE="f2">Frequency list 1
				<OPTION VALUE="f1">Frequency list 2
			</SELECT>
		</TD>
	</TR>
	<TR BGCOLOR="#CCCCCC">
		<TD ALIGN=MIDDLE COLSPAN="4">
			<INPUT TYPE="submit" name="theMethod" value="Compare lists!">
		</TD>
	</TR>
	
	</FORM>
END
	
	print "</TABLE><TR><TD COLSPAN=\"2\">\n";

	print <<END;
	<HR>$bwCopyright
	</TD></TR></TABLE>
END

}

sub categorizedQueries {

	$inst=50;
	
	# connect to mysql
	$bncHandle = DBI->connect("DBI:mysql:$bwMYSQLtable:$bwMYSQLhost:$bwMysqlPort", "$bwMysqlUser", "$bwMysqlPwd") or bark("Can't connect to mysql on $bwMYSQLhost at port: $bwMysqlPort <BR>$bncHandle::errstr\n");
	$categorizeHandle = DBI->connect("DBI:mysql:$bwMYSQLcategorizetable:$bwMYSQLhost:$bwMysqlPort", "$bwMysqlUser", "$bwMysqlPwd");

	print <<END;
	
	<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="6" WIDTH="100%">
		<TR>
			<TD WIDTH="100%" HEIGHT="22" BGCOLOR="#FFCC99">
				<H3 ALIGN="CENTER"><B>Categorized queries</B>
			</TD>
		</TR>
END

	
	# delete query if option was chosen
	if ($params{delQuery}) {
		
			$But2 = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=categorizedQueries&user=' . $username . '&chunk=' . $params{chunk} . '&delQuery=' . $queryID . '&urlTest=yes">Delete</A>';
		
		if ($params{doublecheck} eq "yes") {
			
			$sqlQuery = "DROP TABLE if exists $bwMYSQLcategorizetable.$params{delQuery} ";
			$bncHandle->do($sqlQuery) or bark("Can't insert $sqlQuery: " . $bncHandle::errstr);
				
			$sqlQuery = "DELETE FROM categorize where queryID = '$params{delQuery}'";
			$bncHandle->do($sqlQuery) or bark("Can't insert $sqlQuery: " . $bncHandle::errstr);
			unlink("/$bwTempPath/$bwCorpus:$params{delQuery}") if -f "/$bwTempPath/$bwCorpus:$params{delQuery}";
			setParam("doublecheck", "");
		} else {
			$theDispName = $params{delQuery};
			$theDispName =~s/\A${username}_cat_//;


			print <<END;
			<TR  BGCOLOR="#DDDDDD">
				<TD WIDTH="100%">
					<H3>You are about to delete the categorized query &quot;$theDispName&quot;. This action cannot be reversed. </h3>
					
					<FORM ACTION="/$bwCGIalias/BNCquery.pl" METHOD="GET">
						Please confirm that you want to go ahead with this action: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
						<INPUT TYPE="HIDDEN" NAME="chunk" SIZE="-1" VALUE="$params{chunk}">
						<INPUT TYPE="HIDDEN" NAME="delQuery" SIZE="-1" VALUE="$params{delQuery}">
						<INPUT TYPE="HIDDEN" NAME="doublecheck" SIZE="-1" VALUE="yes">
						<INPUT TYPE="HIDDEN" NAME="delQuery" SIZE="-1" VALUE="$params{delQuery}">
						<INPUT TYPE="HIDDEN" NAME="user" SIZE="-1" VALUE="$params{user}">
						<INPUT TYPE="HIDDEN" NAME="theQuery" SIZE="-1" VALUE="categorizedQueries">
						<INPUT TYPE="hidden" NAME="urlTest" VALUE="yes">
						<INPUT TYPE="SUBMIT" VALUE="Yes - delete!">
					</FORM>
				</TD>
			</TR>
END
		}
	}

	$begin=(($params{chunk} - 1) * $inst);
	$end=(($params{chunk} * $inst) - 1);

	if (($username=~m/\A($bwSuperuser)\Z/) || (&checkSuperUser2())) {
		$sqlQuery = "SELECT distinct(user) FROM categorize order by user";
		$sth1= $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
		$sth1->execute or bark("Can't execute $sqlQuery: $sth1::errstr");
		$res=$sth1->rows;

		print <<END;
		<TR>
			<TD WIDTH="100%">
			<TABLE BORDER=0 WIDTH=100%>
				<TR>
					<TD WIDTH=20% HEIGHT="30">
						<FORM ACTION="/$bwCGIalias/BNCquery.pl" METHOD="GET">
						<INPUT TYPE="HIDDEN" NAME="theQuery" VALUE="categorizedQueries" SIZE="-1">
						<B>User:</B>
					</TD>
					<TD WIDTH=30%>
						<SELECT NAME="user">
							<OPTION VALUE="all">  all users
END
		for ($i = 0; $i < $res; $i++) {
			@mysqlArray=$sth1->fetchrow_array;
			$theUser=$mysqlArray[0];
			print "<OPTION>$theUser\n";
		}
		print <<END;
						</SELECT>
					</TD>
					<TD>
						<INPUT TYPE="HIDDEN" NAME="chunk" SIZE="-1" VALUE="1">
						<INPUT TYPE="hidden" NAME="urlTest" VALUE="yes">
						<INPUT TYPE="SUBMIT" VALUE="Show queries">
					</TD>
				</TR>
			</TABLE>
		</FORM>
END
	

		print "</TD></TR><TR><TD HEIGHT=\"30\" WIDTH=\"100%\" BGCOLOR=\"#CCCCFF\">";
	}
	$actualUsername = $username;
	
	if ($params{user} ne $username) {
		$username=$params{user} if (($username=~m/\A($bwSuperuser)\Z/) || (&checkSuperUser2()));
	}
	if ($username eq "all") {
		$sqlQuery = "SELECT * FROM categorize ORDER BY user, theName ASC";
	} else {
		$sqlQuery = "SELECT * FROM categorize WHERE user='$username' ORDER BY theName ASC";
	}
		
	$sth2= $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
	$sth2->execute or bark("Can't execute $sqlQuery: $sth2::errstr");
	$res = $sth2->rows;
	$table = $sth2->fetchall_arrayref or bark("Can't fetchrow_arrayref $sqlQuery: $sth2::errstr");
	
	$end = ($res -1) if ($end > $res);
	$begin = ($begin - $params{inst}) if ($begin > $end);
	
	if ($res) {
		if ($username eq "all") {
			print <<END1;
		<TR>
			<TD WIDTH="100%">
				<B>The following categorized queries exist:</B>
			</TD>
		</TR>
		<TR>
			<TD>
				<TABLE BORDER="0" CELLPADDING="2" CELLSPACING="6" WIDTH="100%">
					<TR BGCOLOR="#FFCC66">
						<TD>
							<B>No.</B>
						</TD>
						<TD>
							<B>User</B>
						</TD>
						<TD WIDTH="30%">
							<B>Name</B>
						</TD>
						<TD>
							<B>Categories</B>
						</TD>
						<TD ALIGN=CENTER NOWRAP>
							<B>No. of hits</B>
						</TD>
						<TD ALIGN=CENTER NOWRAP>
							<B>Categorized</B>
						</TD>
						<TD>
							<B>Date of last change</B>
						</TD>
						<TD>
							<B>Action</B>
						</TD>
					</TR>
END1
		} else { 
			if (($actualUsername=~m/\A($bwSuperuser)\Z/) || (&checkSuperUser2())) {
				print "<TR><TD WIDTH=\"100%\"><B>User $username has the following categorized queries:</B>";
			}
			print <<END;
				</TD>
			</TR>
		</TABLE>
		
		<TABLE BORDER="0" CELLPADDING="2" CELLSPACING="6" WIDTH="100%">
			<TR BGCOLOR="#FFCC66">
				<TD>
					<B>No.</B>
				</TD>
				<TD NOWRAP>
					<B>Name of set</B>
				</TD>
				<TD>
					<B>Categories</B>
				</TD>
				<TD ALIGN=CENTER NOWRAP>
					<B>No. of hits</B>
				</TD>
				<TD ALIGN=CENTER NOWRAP>
					<B>Completed</B>
				</TD>
				<TD>
					<B>Date af last change</B>
				</TD>
				<TD>
					<B>Action</B>
				</TD>

			</TR>
END
		}
	
		for ($i = $begin; $i <= $end; $i++) {
			$theUser = $table->[$i][0];
			$queryID = $table->[$i][1];
			$thMode  = $table->[$i][3];
			$hits    = $table->[$i][4];
			$theName = $table->[$i][6];
			$link    = $table->[$i][7];
			$timestamp=$table->[$i][8];
			$theValues=$table->[$i][9];
			
			$theValuesShow = $theValues;
			$theValuesShow=~s/\W/, /g;
			
			$timestamp=~m/(\d{4})\-?(\d{2})\-?(\d{2}) ?(\d{2})\:?(\d{2})\:?(\d{2})/;

			#$timestamp=~m/(\d{4})(\d{2})(\d{2})/;
			$year=$1;
			$month=$2;
			$day=$3;
			$hour=$4;
			$minute=$5;
			$second=$6;
			
			$sqlQuery = "select count(*) from $theName where catName <> 'NULL'";

			$sth5= $categorizeHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $categorizeHandle::errstr);
			$sth5->execute or bark("Can't execute $sqlQuery: " . $sth5::errstr);
			$res = $sth->rows;
			@array=$sth5->fetchrow_array;
			$categorized=$array[0];

			$percent = int((($categorized / $hits) * 10000) + 0.5)/ 100;
			
			$link=~s/&thMode=[^&]+//;
			$link=~s/&qname=[^&]+//;
			$link=~s/&queryID=[^&]+//;
			$thModeLink = $thMode;
			$thModeLink=~s/(\W)/sprintf("%%%x", ord($1))/eg;
			
			$theDispName = $theName;
			$theDispName =~s/\A${username}_cat_//;
			$But  = '<A HREF="/' . $bwCGIalias . '/main.pl?' . $link . '&thMode=' . $thModeLink . '&qname=' . $queryID . '&queryID=' . $queryID . '&recreate=' . $theName . '&dbname=' . $theName . '&categorize=yes&urlTest=yes">' . $theDispName . '</A>';
# 			$But2 = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=categorizedQueries&user=' . $username . '&chunk=' . $params{chunk} . '&delQuery=' . $queryID . '&urlTest=yes">Delete set</A>';
# 			$But4 = '<A HREF="/' . $bwCGIalias . '/categorizeAdmin.pl?action=addValue&user=' . $username . '&dbname=' . $theName . '&urlTest=yes">Add categories</A>';
# 			$But3 = '<A HREF="/' . $bwCGIalias . '/categorizeAdmin.pl?action=separate&user=' . $username . '&dbname=' . $theName . '&urlTest=yes">Separate categories</A>';
			
			$thing = 'Location: /' . $bwCGIalias . '/categorizeAdmin.pl?action=addValue&user=' . $username . '&dbname=' . $theName . '&urlTest=yes';
			$form = "<SELECT NAME=\"selected\"><OPTION VALUE=\"$thing\">Add categories\n";
			$thing = 'Location: /' . $bwCGIalias . '/categorizeAdmin.pl?action=separate&user=' . $username . '&dbname=' . $theName . '&urlTest=yes';
			$form = $form . "<OPTION SELECTED VALUE=\"$thing\">Separate categories\n";
			$thing = 'Location: /' . $bwCGIalias . '/BNCquery.pl?theQuery=categorizedQueries&user=' . $username . '&chunk=' . $params{chunk} . '&delQuery=' . $queryID . '&urlTest=yes';
			$form = $form . "<OPTION VALUE=\"$thing\">Delete complete set\n</SELECT><INPUT TYPE=\"submit\" VALUE=\"Go\">";
			
			
			
			$count=$i+1;
			if ($username eq "all") {
				print "<FORM ACTION=\"/$bwCGIalias/dlogs2.pl\" METHOD=GET><TR BGCOLOR=\"#DDDDDD\"><TD VALIGN=TOP>$count</TD><TD VALIGN=TOP>$theUser</TD><TD VALIGN=TOP>$But</TD><TD VALIGN=TOP ALIGN=LEFT>$theValuesShow</TD><TD VALIGN=TOP ALIGN=CENTER>$hits</TD><TD NOWRAP VALIGN=TOP ALIGN=CENTER>$categorized (${percent}%)</TD><TD VALIGN=TOP NOWRAP>$day.$month.$year $hour:$minute:$second</TD><TD NOWRAP VALIGN=TOP>$form</TD></TR></FORM>";
			} else {
				print "<FORM ACTION=\"/$bwCGIalias/dlogs2.pl\" METHOD=GET><TR BGCOLOR=\"#DDDDDD\"><TD VALIGN=TOP>$count</TD><TD VALIGN=TOP>$But</TD><TD VALIGN=TOP ALIGN=LEFT>$theValuesShow</TD><TD VALIGN=TOP ALIGN=CENTER>$hits</TD><TD NOWRAP VALIGN=TOP ALIGN=CENTER>$categorized (${percent}%)</TD><TD VALIGN=TOP NOWRAP>$day.$month.$year $hour:$minute:$second</TD><TD NOWRAP VALIGN=TOP>$form</TD></TR></FORM>";
			}
		}
	
		print "</TABLE><TABLE BORDER=\"0\" CELLPADDING=\"3\" CELLSPACING=\"2\" WIDTH=\"100%\" BGCOLOR=\"#FFFFFF\"><TR><TD>";
		if ($params{chunk} > 1) {
			$indexBut = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=categorizedQueries&user=' . $username . '&chunk=' . ($params{chunk} - 1) . '&urlTest=yes">Previous page</A>';
			print "[" . $indexBut . "]";
		} else {
			print "[Previous page]";
		}
		if (($params{chunk} * $inst ) < $res) {
			$indexBut = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=categorizedQueries&user=' . $username . '&chunk=' . ($params{chunk} + 1) . '&urlTest=yes">Next page</A>';
			print "[" . $indexBut . "]";
		} else {
			print "[Next page]";
		}
		print "<HR>$bwCopyright</TD></TR></TABLE></TD></TR></TABLE>";
	} else {
		print <<END;
			<TR>
			<TD WIDTH="100%">
				<BR><H3>You have no categorized queries.</H3>
			</TD>
			</TR>
		<TR><TD><HR>$bwCopyright</TD></TR></TABLE></TD></TR></TABLE>
END
	}

}

sub cachedQueries {

	&checkSuperUser();
		
	$inst=50;
	if (!$params{orderType}) {
		$orderType = "fileSize";
	} else {
		$orderType = $params{orderType};
	}
	
	# connect to mysql
	$bncHandle = DBI->connect("DBI:mysql:$bwMYSQLtable:$bwMYSQLhost:$bwMysqlPort", "$bwMysqlUser", "$bwMysqlPwd") or bark("Can't connect to mysql on $bwMYSQLhost at port: $bwMysqlPort <BR>$bncHandle::errstr\n");
	
	# delete query if option was chosen
	if ($params{delQuery}) {
		$sqlQuery = "DELETE FROM savedQueries where queryID = '$params{delQuery}'";
		$bncHandle->do($sqlQuery) or bark("Can't insert $sqlQuery: " . $bncHandle::errstr);
		unlink("/$bwTempPath/$bwCorpus:$params{delQuery}") if -f "/$bwTempPath/$bwCorpus:$params{delQuery}";
	}
	
	&deleteCachedQueries(0) if ($params{deleteAll});

	$sqlQuery = "SELECT count(*), sum(fileSize) FROM savedQueries where saved = 0";
	$sth = $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
	$sth->execute or bark("Can't execute $sqlQuery: ". $sth::errstr);
	@rowArray=$sth->fetchrow_array;
	$numFiles=$rowArray[0];
	$totalFileSize=$rowArray[1];

	$percent = int((($totalFileSize / $bwFileSize) * 10000) + 0.5)/ 100;
	
	$totalFileSize = makeDecimals($totalFileSize);
	
	
	$begin=(($params{chunk} - 1) * $inst);
	$end=(($params{chunk} * $inst) - 1);
	
	if ($numFiles) {
		$But = ' - <A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=cachedQueries&orderType=' . $orderType . '&chunk=1&deleteAll=yes&urlTest=yes">Delete complete cache</A>';
		if ($numFiles == 1) {
			$But2 = ": $numFiles file; $totalFileSize bytes on disk ($percent%)";
		} else {
			$But2 = ": $numFiles files; $totalFileSize bytes on disk ($percent%)";
		}
	} else {
		$But = "";
		$But2 = "";
	}
	print <<END;
	
	<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="6" WIDTH="100%">
		<TR>
			<TD WIDTH="100%" HEIGHT="22" BGCOLOR="#FFCC99">
				<H3 ALIGN="CENTER"><B>Cached queries$But2</B>$But
			</TD>
		</TR>
END

	$sqlQuery = "SELECT queryID, query, hits, fileSize, date_of_saving FROM savedQueries where saved = 0 ORDER BY $orderType desc";

	$sth2= $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
	$sth2->execute or bark("Can't execute $sqlQuery: $sth2::errstr");
	$res = $sth2->rows;
	$table = $sth2->fetchall_arrayref or bark("Can't fetchrow_arrayref $sqlQuery: $sth2::errstr");
	
	$end = ($res -1) if ($end > $res);
	$begin = ($begin - $params{inst}) if ($begin > $end);
	
	if ($res) {

	$But1 = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=cachedQueries&chunk=' . $params{chunk} . '&orderType=date_of_saving&urlTest=yes">Date</A>';
	$But2 = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=cachedQueries&chunk=' . $params{chunk} . '&orderType=fileSize&urlTest=yes">Size on Disk</A>';

			print <<END1;
		<TR>
			<TD>
				<TABLE BORDER="0" CELLPADDING="2" CELLSPACING="6" WIDTH="100%">
					<TR BGCOLOR="#FFCC66">
						<TD WIDTH="5%">
							<B>No.</B>
						</TD>
						<TD WIDTH="30%">
							<B>Query ID</B>
						</TD>
						<TD ALIGN=LEFT NOWRAP>
							<B>Query term (without restrictions)</B>
						</TD>
						<TD ALIGN=CENTER NOWRAP>
							<B>No. of hits</B>
						</TD>
						<TD>
							<B>$But1</B>
						</TD>
						<TD NOWRAP>
							<B>$But2</B>
						</TD>
						<TD>
							<B>Action</B>
						</TD>
					</TR>
END1

		for ($i = $begin; $i <= $end; $i++) {
			$queryID = $table->[$i][0];
			$theQuery= $table->[$i][1];
			$hits    = $table->[$i][2];
			$fileSize= $table->[$i][3];
			$timestamp=$table->[$i][4];
			
			$fileSize = makeDecimals($fileSize);
			$hits = makeDecimals($hits);
			
			$timestamp=~m/(\d{4})\-?(\d{2})\-?(\d{2}) ?(\d{2})\:?(\d{2})\:?(\d{2})/;
			$year=$1;
			$month=$2;
			$day=$3;
			$hour=$4;
			$minute=$5;
			$second=$6;
			
			
			$But = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=cachedQueries&orderType=' . $orderType . '&chunk=' . $params{chunk} . '&delQuery=' . $queryID . '&urlTest=yes">Delete</A>';
			$count=$i+1;
			print "<TR BGCOLOR=\"#DDDDDD\"><TD VALIGN=TOP>$count</TD><TD VALIGN=TOP>$queryID</TD><TD VALIGN=TOP ALIGN=LEFT>$theQuery</TD><TD VALIGN=TOP ALIGN=RIGHT>$hits</TD><TD VALIGN=TOP NOWRAP>$day-$month-$year $hour:$minute:$second</TD><TD VALIGN=TOP ALIGN=RIGHT>$fileSize</TD><TD NOWRAP VALIGN=TOP>$But</TD></TR>"
		}
	
		print "</TABLE><TABLE BORDER=\"0\" CELLPADDING=\"3\" CELLSPACING=\"2\" WIDTH=\"100%\" BGCOLOR=\"#FFFFFF\"><TR><TD>";
		if ($params{chunk} > 1) {
			$indexBut = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=cachedQueries&orderType=' . $orderType . '&chunk=' . ($params{chunk} - 1) . '&urlTest=yes">Previous page</A>';
			print "[" . $indexBut . "]";
		} else {
			print "[Previous page]";
		}
		if (($params{chunk} * $inst ) < $res) {
			$indexBut = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=cachedQueries&orderType=' . $orderType . '&chunk=' . ($params{chunk} + 1) . '&urlTest=yes">Next page</A>';
			print "[" . $indexBut . "]";
		} else {
			print "[Next page]";
		}
		print "<HR>$bwCopyright</TD></TR></TABLE></TD></TR></TABLE>";
	} else {
		print "<TR><TD><H3>There are currently no cached queries.</H3><HR>$bwCopyright</TD></TR>";
	}

}

sub cachedMySQLtables {

	&checkSuperUser();
	
	$inst=50;
	if (!$params{orderType}) {
		$orderType = "numRows";
	} else {
		$orderType = $params{orderType};
	}
	
	# connect to mysql
	$bncHandle = DBI->connect("DBI:mysql:$bwMYSQLtable:$bwMYSQLhost:$bwMysqlPort", "$bwMysqlUser", "$bwMysqlPwd") or bark("Can't connect to mysql on $bwMYSQLhost at port: $bwMysqlPort <BR>$bncHandle::errstr\n");
	$userHandle = DBI->connect("DBI:mysql:$bwMYSQLusertable:$bwMYSQLhost:$bwMysqlPort", "$bwMysqlUser", "$bwMysqlPwd") or bark("Can't connect to mysql on $bwMYSQLhost at port: $bwMysqlPort <BR>$userHandle::errstr\n");
	
	# delete query if option was chosen
	if ($params{delTable}) {
		$sqlQuery = "DELETE FROM tableLog where db_name = '$params{delTable}'";
		$bncHandle->do($sqlQuery) or bark("Can't insert $sqlQuery: " . $bncHandle::errstr);
		$sqlQuery = "DROP TABLE $params{delTable}";
		$userHandle->do($sqlQuery) or bark("Can't $sqlQuery: " . $userHandle::errstr);
	}

	&deleteCachedMySQLTables(0) if ($params{deleteAll});

	$sqlQuery = "SELECT count(*), sum(numRows) FROM tableLog";
	$sth = $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
	$sth->execute or bark("Can't execute $sqlQuery: ". $sth::errstr);
	@rowArray=$sth->fetchrow_array;
	$numTables=$rowArray[0];
	$totalFileSize=$rowArray[1];
	$percent = int((($totalFileSize / $bwMaxMySQLSize) * 10000) + 0.5)/ 100;

	$totalFileSize = makeDecimals($totalFileSize);

	$begin=(($params{chunk} - 1) * $inst);
	$end=(($params{chunk} * $inst) - 1);


	if ($numTables) {
		$But = ' - <A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=cachedMySQLtables&orderType=' . $orderType . '&chunk=1&deleteAll=yes&urlTest=yes">Delete complete cache</A>';
		if ($numTables == 1) {
			$But2 = ": $numTables table; $totalFileSize rows ($percent%)";
		} else {
			$But2 = ": $numTables tables; $totalFileSize rows ($percent%)";
		}
	} else {
		$But = "";
		$But2 = "";
	}


	print <<END;
	
	<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="6" WIDTH="100%">
		<TR>
			<TD WIDTH="100%" HEIGHT="22" BGCOLOR="#FFCC99">
				<H3 ALIGN="CENTER"><B>Cached MySQL tables$But2</B> $But
			</TD>
		</TR>
END

	$sqlQuery = "SELECT db_name, query, numRows, date_of_saving FROM tableLog ORDER BY $orderType desc";

	$sth2= $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
	$sth2->execute or bark("Can't execute $sqlQuery: $sth2::errstr");
	$res = $sth2->rows;
	$table = $sth2->fetchall_arrayref or bark("Can't fetchrow_arrayref $sqlQuery: $sth2::errstr");
	
	$end = ($res -1) if ($end > $res);
	$begin = ($begin - $params{inst}) if ($begin > $end);
	
	if ($res) {

	$But1 = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=cachedMySQLtables&chunk=' . $params{chunk} . '&orderType=date_of_saving&urlTest=yes">Date</A>';
	$But2 = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=cachedMySQLtables&chunk=' . $params{chunk} . '&orderType=numRows&urlTest=yes">Size (rows)</A>';

			print <<END1;
		<TR>
			<TD>
				<TABLE BORDER="0" CELLPADDING="2" CELLSPACING="6" WIDTH="100%">
					<TR BGCOLOR="#FFCC66">
						<TD WIDTH="5%">
							<B>No.</B>
						</TD>
						<TD WIDTH="30%">
							<B>Name of table</B>
						</TD>
						<TD ALIGN=LEFT NOWRAP>
							<B>Query term (without restrictions)</B>
						</TD>
						<TD>
							<B>$But1</B>
						</TD>
						<TD NOWRAP>
							<B>$But2</B>
						</TD>
						<TD>
							<B>Action</B>
						</TD>
					</TR>
END1

		for ($i = $begin; $i <= $end; $i++) {
			$dbname   = $table->[$i][0];
			$theQuery = $table->[$i][1];
			$numRows  = $table->[$i][2];
			$timestamp= $table->[$i][3];
			
			$numRows = makeDecimals($numRows);
			
			$timestamp=~m/(\d{4})\-?(\d{2})\-?(\d{2}) ?(\d{2})\:?(\d{2})\:?(\d{2})/;
			$year=$1;
			$month=$2;
			$day=$3;
			$hour=$4;
			$minute=$5;
			$second=$6;
			
			
			$But = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=cachedMySQLtables&orderType=' . $orderType . '&chunk=' . $params{chunk} . '&delTable=' . $dbname . '&urlTest=yes">Delete</A>';
			$count=$i+1;
			print "<TR BGCOLOR=\"#DDDDDD\"><TD VALIGN=TOP>$count</TD><TD VALIGN=TOP>$dbname</TD><TD VALIGN=TOP ALIGN=LEFT>$theQuery</TD><TD VALIGN=TOP NOWRAP>$day-$month-$year $hour:$minute:$second</TD><TD ALIGN=\"RIGHT\" VALIGN=TOP>$numRows</TD><TD NOWRAP VALIGN=TOP>$But</TD></TR>"
		}
	
		print "</TABLE><TABLE BORDER=\"0\" CELLPADDING=\"3\" CELLSPACING=\"2\" WIDTH=\"100%\" BGCOLOR=\"#FFFFFF\"><TR><TD>";
		if ($params{chunk} > 1) {
			$indexBut = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=cachedMySQLtables&orderType=' . $orderType . '&chunk=' . ($params{chunk} - 1) . '&urlTest=yes">Previous page</A>';
			print "[" . $indexBut . "]";
		} else {
			print "[Previous page]";
		}
		if (($params{chunk} * $inst ) < $res) {
			$indexBut = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=cachedMySQLtables&orderType=' . $orderType . '&chunk=' . ($params{chunk} + 1) . '&urlTest=yes">Next page</A>';
			print "[" . $indexBut . "]";
		} else {
			print "[Next page]";
		}
		print "<HR>$bwCopyright</TD></TR></TABLE></TD></TR></TABLE>";
	} else {
		
		print "<TR><TD><H3>There are no cached MySQL tables.</H3><HR>$bwCopyright</TD></TR></H3>";
	}

}

sub cachedFrequencyLists {

	&checkSuperUser();
	
	$inst=50;
	if (!$params{orderType}) {
		$orderType = "numRows";
	} else {
		$orderType = $params{orderType};
	}
	
	# connect to mysql
	$bncHandle = DBI->connect("DBI:mysql:$bwMYSQLtable:$bwMYSQLhost:$bwMysqlPort", "$bwMysqlUser", "$bwMysqlPwd") or bark("Can't connect to mysql on $bwMYSQLhost at port: $bwMysqlPort <BR>$bncHandle::errstr\n");
	$userHandle = DBI->connect("DBI:mysql:$bwMYSQLusertable:$bwMYSQLhost:$bwMysqlPort", "$bwMysqlUser", "$bwMysqlPwd") or bark("Can't connect to mysql on $bwMYSQLhost at port: $bwMysqlPort <BR>$userHandle::errstr\n");
	
	# delete query if option was chosen
	if ($params{delTable}) {
		$sqlQuery = "DELETE FROM frequencyLists where theName = '$params{delTable}'";
		$bncHandle->do($sqlQuery) or bark("Can't insert $sqlQuery: " . $bncHandle::errstr);
		$sqlQuery = "DROP TABLE if exists $params{delTable}";
		$userHandle->do($sqlQuery) or bark("Can't $sqlQuery: " . $userHandle::errstr);
	}
	&deleteCachedFreqlists(0) if ($params{deleteAll});

	$sqlQuery = "SELECT count(*), sum(numRows) FROM frequencyLists";
	$sth = $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
	$sth->execute or bark("Can't execute $sqlQuery: ". $sth::errstr);
	@rowArray=$sth->fetchrow_array;
	$numTables=$rowArray[0];
	$totalFileSize=$rowArray[1];
	$percent = int((($totalFileSize / $bwMaxFreqlistMySQL) * 10000) + 0.5)/ 100;

	$totalFileSize = makeDecimals($totalFileSize);

	$begin=(($params{chunk} - 1) * $inst);
	$end=(($params{chunk} * $inst) - 1);

	if ($numTables) {
		$But = ' - <A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=cachedFrequencyLists&orderType=' . $orderType . '&chunk=1&deleteAll=yes&urlTest=yes">Delete complete cache</A>';
		if ($numTables == 1) {
			$But2 = ": $numTables table; $totalFileSize rows ($percent%)";
		} else {
			$But2 = ": $numTables tables; $totalFileSize rows ($percent%)";
		}
	} else {
		$But = "";
		$But2 = "";
	}


	print <<END;
	
	<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="6" WIDTH="100%">
		<TR>
			<TD WIDTH="100%" HEIGHT="22" BGCOLOR="#FFCC99">
				<H3 ALIGN="CENTER"><B>Cached Frequency lists$But2</B> $But
			</TD>
		</TR>
END

	$sqlQuery = "SELECT theName, numRows, FROM_UNIXTIME(time_of_creation) FROM frequencyLists ORDER BY $orderType desc";

	$sth2= $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
	$sth2->execute or bark("Can't execute $sqlQuery: $sth2::errstr");
	$res = $sth2->rows;
	$table = $sth2->fetchall_arrayref or bark("Can't fetchrow_arrayref $sqlQuery: $sth2::errstr");
	
	$end = ($res -1) if ($end > $res);
	$begin = ($begin - $params{inst}) if ($begin > $end);
	
	if ($res) {

	$But1 = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=cachedFrequencyLists&chunk=' . $params{chunk} . '&orderType=time_of_creation&urlTest=yes">Date of creation</A>';
	$But2 = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=cachedFrequencyLists&chunk=' . $params{chunk} . '&orderType=numRows&urlTest=yes">Size (rows)</A>';

			print <<END1;
		<TR>
			<TD>
				<TABLE BORDER="0" CELLPADDING="2" CELLSPACING="6" WIDTH="100%">
					<TR BGCOLOR="#FFCC66">
						<TD WIDTH="5%">
							<B>No.</B>
						</TD>
						<TD WIDTH="30%">
							<B>Name of table</B>
						</TD>
						<TD>
							<B>$But1</B>
						</TD>
						<TD NOWRAP>
							<B>$But2</B>
						</TD>
						<TD>
							<B>Action</B>
						</TD>
					</TR>
END1

		for ($i = $begin; $i <= $end; $i++) {
			$dbname   = $table->[$i][0];
			$numRows  = $table->[$i][1];
			$timestamp= $table->[$i][2];
			
			$numRows = makeDecimals($numRows);
						
			
			$But = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=cachedFrequencyLists&orderType=' . $orderType . '&chunk=' . $params{chunk} . '&delTable=' . $dbname . '&urlTest=yes">Delete</A>';
			$count=$i+1;
			print "<TR BGCOLOR=\"#DDDDDD\"><TD VALIGN=TOP>$count</TD><TD VALIGN=TOP>$dbname</TD><TD VALIGN=TOP NOWRAP>$timestamp</TD><TD ALIGN=\"RIGHT\">$numRows</TD><TD NOWRAP VALIGN=TOP>$But</TD></TR>"
		}
	
		print "</TABLE><TABLE BORDER=\"0\" CELLPADDING=\"3\" CELLSPACING=\"2\" WIDTH=\"100%\" BGCOLOR=\"#FFFFFF\"><TR><TD>";
		if ($params{chunk} > 1) {
			$indexBut = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=cachedFrequencyLists&orderType=' . $orderType . '&chunk=' . ($params{chunk} - 1) . '&urlTest=yes">Previous page</A>';
			print "[" . $indexBut . "]";
		} else {
			print "[Previous page]";
		}
		if (($params{chunk} * $inst ) < $res) {
			$indexBut = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=cachedMySQLtables&orderType=' . $orderType . '&chunk=' . ($params{chunk} + 1) . '&urlTest=yes">Next page</A>';
			print "[" . $indexBut . "]";
		} else {
			print "[Next page]";
		}
		print "<HR>$bwCopyright</TD></TR></TABLE></TD></TR></TABLE>";
	} else {
		print "<TR><TD><H3>There are no cached frequency lists.</H3><HR>$bwCopyright</TD></TR></H3>";
	}

}


sub usageStatistics {

	unless (($username=~m/\A($bwSuperuser)\Z/) || (&checkSuperUser2())) {

		print <<END;
		<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="6" WIDTH="100%">
			<TR>
				<TD WIDTH="100%" HEIGHT="22" BGCOLOR="#FFCC99">
					<H3 ALIGN="CENTER"><B>You are not authorised to access this page! Please don't fiddle with the URL... ;-)</B>
				</TD>
			</TR>
		</TABLE>
END
		print "<HR>$bwCopyright</TD></TR></TABLE></TD></TR></TABLE>";
		exit(0);	
	}

	# connect to mysql
	$bncHandle = DBI->connect("DBI:mysql:$bwMYSQLtable:$bwMYSQLhost:$bwMysqlPort", "$bwMysqlUser", "$bwMysqlPwd") or bark("Can't connect to mysql on $bwMYSQLhost at port: $bwMysqlPort <BR>$bncHandle::errstr\n");
	$userHandle = DBI->connect("DBI:mysql:$bwMYSQLusertable:$bwMYSQLhost:$bwMysqlPort", "$bwMysqlUser", "$bwMysqlPwd") or bark("Can't connect to mysql on $bwMYSQLhost at port: $bwMysqlPort <BR>$userHandle::errstr\n");

	$inst=50;
	
	$link = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=history&user=###&chunk=1&urlTest=yes">###</A>';
	

	if (!$params{timeSpan}) {
		$timeSpan = "all";
	} else {
		$timeSpan = $params{timeSpan};
	}
	($sec,$min,$hour,$day,$month,$year,$wday,$yday,$isdst) = localtime(time);
	$year = 1900 + $year;
	$month++;
	
	if ($timeSpan eq "all") {
		$timeSpanShow = "complete query history";
	} elsif ($timeSpan eq "month") {
		$timeSpanShow = "past month";
		$month--;
		if ($month == 0) {
			$month = 12;
			$year--;
		}
	} elsif ($timeSpan eq "year") {
		$timeSpanShow = "past year";
		$year--;
	} elsif ($timeSpan eq "week") {
		$timeSpanShow = "past week";
		if ($day > 7) {
			$day = $day - 7;
		} else {
			
			$extra = 7 - $day;
			if ($month > 1) {
				$month--;
			} else {
				$month = 12;
				$year--;
			}

			if ($month=~m/\A0?(1|3|5|7|8|10|12)\Z/) {
				$day = 31 - $extra;
			} elsif ($month=~m/\A0?(4|6|9|11)\Z/) {
				$day = 30 - $extra;
			} else {
				# I'm ignoring years where February has 20 days...
				$day = 28 - $extra;
			}
							
		}
		
		
	} elsif ($timeSpan eq "today") {
		$timeSpanShow = "today";
	}

	$day = "0" . $day if ($day=~m/\A\d\Z/);
	$month = "0" . $month if ($month=~m/\A\d\Z/);
	$theTimeStamp = $year . "-" .  $month . "-" . $day;

	$begin=(($params{chunk} - 1) * $inst);
	$end=(($params{chunk} * $inst) - 1);
	
	
	$orderBy = $params{orderBy};
	if ($orderBy eq "user") {
		$direction = "asc";
	} else {
		$direction = "desc";
	}
	if ($timeSpan eq "all") {
		$sqlQuery = "select user, count(*) as numberQueries from history group by user order by $orderBy $direction";
		$sqlQuery1 = "select count(*) from history ";
	} else {
		$sqlQuery = "select user, count(*) as numberQueries from history where time_of_query >= \"$theTimeStamp\" group by user order by $orderBy desc";
		$sqlQuery1 = "select count(*) from history where time_of_query >= \"$theTimeStamp\"";
	}
	

	$sth13 = $bncHandle->prepare($sqlQuery1) or bark("Can't prepare $sqlQuery1: " . $bncHandle::errstr);
	$sth13->execute or bark("Can't execute $sqlQuery1: $sth13::errstr");
	$res = $sth13->rows;

	if ($res) {
		@rowArray=$sth13->fetchrow_array;
		$countAll=$rowArray[0];
	} else {
		$countAll = 0;
	}


	print <<END;
	
	<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="6" WIDTH="100%">
		<TR>
			<TD WIDTH="100%" HEIGHT="22" BGCOLOR="#FFCC99">
				<H3 ALIGN="CENTER"><B>Usage statistics ($timeSpanShow) - $countAll queries in total</B>
			</TD>
		</TR>
END
	


	
	$sth2= $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
	$sth2->execute or bark("Can't execute $sqlQuery: $sth2::errstr");
	$res = $sth2->rows;
	$table = $sth2->fetchall_arrayref or bark("Can't fetchrow_arrayref $sqlQuery: $sth2::errstr");
	
	$end = ($res -1) if ($end > $res);
	$begin = ($begin - $params{inst}) if ($begin > $end);
	
	if ($res) {


		$queryViewBut1 = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=usageStatistics&chunk=' . $params{chunk} . '&timeSpan=' . $params{timespan} . '&orderBy=user&urlTest=yes">User</A>';
		$queryViewBut2 = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=usageStatistics&chunk=' . $params{chunk} . '&timeSpan=' . $params{timespan} . '&orderBy=numberQueries&urlTest=yes">Number of queries</A>';



			print <<END1;
		<TR>
			<TD>
				<TABLE BORDER="0" CELLPADDING="2" CELLSPACING="6" WIDTH="100%">
					<TR BGCOLOR="#FFCC66">
						<TD WIDTH="5%">
							<B>No.</B>
						</TD>
						<TD WIDTH="70%">
							<B>$queryViewBut1</B>
						</TD>
						<TD ALIGN=CENTER NOWRAP>
							<B>$queryViewBut2</B>
						</TD>
					</TR>
END1

		for ($i = $begin; $i <= $end; $i++) {
			$user   = $table->[$i][0];
			$numOfQueries = $table->[$i][1];
			
			$numOfQueries = makeDecimals($numOfQueries);
			
			$linkNew = $link;
			$linkNew=~s/###/$user/g;
			
			
			$count=$i+1;
			print "<TR BGCOLOR=\"#DDDDDD\"><TD VALIGN=TOP>$count</TD><TD VALIGN=TOP>$linkNew</TD><TD VALIGN=TOP ALIGN=CENTER>$numOfQueries</TD></TR>"
		}
	
		print "</TABLE><TABLE BORDER=\"0\" CELLPADDING=\"3\" CELLSPACING=\"2\" WIDTH=\"100%\" BGCOLOR=\"#FFFFFF\"><TR><TD>";
		if ($params{chunk} > 1) {
			$indexBut = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=usageStatistics&chunk=' . ($params{chunk} - 1) . '&timeSpan=' . $timeSpan . '&orderBy=' . $orderBy . '&urlTest=yes">Previous page</A>';
			print "[" . $indexBut . "]";
		} else {
			print "[Previous page]";
		}
		if (($params{chunk} * $inst ) < $res) {
			$indexBut = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=usageStatistics&chunk=' . ($params{chunk} + 1) . '&timeSpan=' . $timeSpan . '&orderBy=' . $orderBy . '&urlTest=yes">Next page</A>';
			print "[" . $indexBut . "]";
		} else {
			print "[Next page]";
		}
		$But00 = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=usageStatistics&chunk=1&timeSpan=today&orderBy=' . $orderBy . '&urlTest=yes">Today</A>';
		$But0 = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=usageStatistics&chunk=1&timeSpan=week&orderBy=' . $orderBy . '&urlTest=yes">Past week</A>';
		$But1 = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=usageStatistics&chunk=1&timeSpan=month&orderBy=' . $orderBy . '&urlTest=yes">Past month</A>';
		$But2 = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=usageStatistics&chunk=1&timeSpan=year&orderBy=' . $orderBy . '&urlTest=yes">Past year</A>';
		$But3 = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?theQuery=usageStatistics&chunk=1&timeSpan=all&orderBy=' . $orderBy . '&urlTest=yes">All queries</A>';
		print "&nbsp;&nbsp;&nbsp;&nbsp;||&nbsp;&nbsp;&nbsp;&nbsp;Show usage statistics for: ";
		
		if ($timeSpan ne "today") {
			print "[" . $But00 . "]";
		} else {
			print "[Today]"
		}
		if ($timeSpan ne "week") {
			print "[" . $But0 . "]";
		} else {
			print "[Past week]"
		}
		if ($timeSpan ne "month") {
			print "[" . $But1 . "]";
		} else {
			print "[Past month]"
		}
		if ($timeSpan ne "year") {
			print "[" . $But2 . "]";
		} else {
			print "[Past year]"
		}
		if ($timeSpan ne "all") {
			print "[" . $But3 . "]";
		} else {
			print "[All queries]"
		}



		print "<HR>$bwCopyright</TD></TR></TABLE></TD></TR></TABLE>";
	} else {
		print "<TR><TD><H3>No queries were made over the period you have chosen.</H3><HR>$bwCopyright</TD></TR>";
	}


}


sub userLimits {

	unless (($username=~m/\A($bwSuperuser)\Z/) || (&checkSuperUser2())) {

		print <<END;
		<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="6" WIDTH="100%">
			<TR>
				<TD WIDTH="100%" HEIGHT="22" BGCOLOR="#FFCC99">
					<H3 ALIGN="CENTER"><B>You are not authorised to access this page! Please don't fiddle with the URL... ;-)</B>
				</TD>
			</TR>
		</TABLE>
END
		print "<HR>$bwCopyright</TD></TR></TABLE></TD></TR></TABLE>";
		exit(0);	
	}

	# connect to mysql
	$bncHandle = DBI->connect("DBI:mysql:$bwMYSQLtable:$bwMYSQLhost:$bwMysqlPort", "$bwMysqlUser", "$bwMysqlPwd") or bark("Can't connect to mysql on $bwMYSQLhost at port: $bwMysqlPort <BR>$bncHandle::errstr\n");

	
	
	if (!$params{action}) {
		$action = "display";
	} else {
		$action = $params{action};
	}

	if ($action eq "add") {

		if ($params{theUsers}) {
			$theUsers = $params{theUsers};

			$theUsers =~ s/\r+/ /g;
			$theUsers =~ s/\n+/ /g;
			$theUsers =~ s/\+/ /g;
			$theUsers =~ s/\s+/\,/g;
			$theUsers =~ s/\,+/·/g;
			if ($theUsers=~m/·/) {
				@theUsers = split (/·/, $theUsers);
			} else {
				$theUsers[0]=$theUsers;
			}
			
			foreach $theUser (@theUsers) {
				$theUserQuote = $bncHandle->quote($theUser);
				$sqlQuery = "INSERT INTO userLimits (user, collocationMax, sortMax, distMax) values ($theUserQuote, $bwDefaultCollocation, $bwDefaultSort, $bwDefaultDist)";
				$bncHandle->do($sqlQuery) or bark("Can't insert $sqlQuery:" . $bncHandle::errstr);
				
			}		
		}
		$action = "display";
	} elsif ($action eq "changeValues") {
		$theUserQuote = $bncHandle->quote($params{user});
		$sqlQuery = "UPDATE userLimits SET collocationMax = $params{collocMax}, sortMax = $params{sortMax}, distMax = $params{distMax} where user = $theUserQuote";
		$bncHandle->do($sqlQuery) or bark("Can't insert $sqlQuery:" . $bncHandle::errstr);
		$action = "display";
	} elsif ($action eq "delete") {
		$theUserQuote = $bncHandle->quote($params{user});
		$sqlQuery = "DELETE FROM userLimits where user = $theUserQuote";
		$bncHandle->do($sqlQuery) or bark("Can't $sqlQuery:" . $bncHandle::errstr);
		$action = "display";
	}
	
	if ($action eq "display") {
	
	print <<END;
	
	<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="6" WIDTH="100%">
		<TR>
			<TD WIDTH="100%" HEIGHT="22" BGCOLOR="#FFCC99">
				<H3 ALIGN="CENTER"><B>User limits</B>
			</TD>
		</TR>
END
	
		$sqlQuery = "select user, collocationMax, sortMax, distMax from userLimits order by user asc";
		
		$sth2= $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
		$sth2->execute or bark("Can't execute $sqlQuery: $sth2::errstr");
		$res = $sth2->rows;
		$table = $sth2->fetchall_arrayref or bark("Can't fetchrow_arrayref $sqlQuery: $sth2::errstr");
		
		$bwDefaultCollocationDec=&makeDecimals($bwDefaultCollocation);
		$bwDefaultSortDec=&makeDecimals($bwDefaultSort);
		$bwDefaultDistDec=&makeDecimals($bwDefaultDist);
		print <<END1;
			<TR>
				<TD>
					<TABLE BORDER="0" CELLPADDING="2" CELLSPACING="6" WIDTH="100%">
						<TR BGCOLOR="#FFCC66">
							<TD WIDTH="5%">
								<B>No.</B>
							</TD>
							<TD WIDTH="70%">
								<B>User</B>
							</TD>
							<TD ALIGN=CENTER NOWRAP>
								<B>Collocation feature</B>
							</TD>
							<TD ALIGN=CENTER NOWRAP>
								<B>Sort feature</B>
							</TD>
							<TD ALIGN=CENTER NOWRAP>
								<B>Distribution feature</B>
							</TD>
							<TD ALIGN=CENTER NOWRAP>
								<B>Action</B>
							</TD>
						</TR>
						<TR BGCOLOR="#CCCCCC">
							<TD WIDTH="5%">
								&nbsp;
							</TD>
							<TD WIDTH="70%">
								DEFAULT*
							</TD>
							<TD ALIGN=CENTER NOWRAP>
								$bwDefaultCollocationDec
							</TD>
							<TD ALIGN=CENTER NOWRAP>
								$bwDefaultSortDec
							</TD>
							<TD ALIGN=CENTER NOWRAP>
								$bwDefaultDistDec
							</TD>
							<TD ALIGN=CENTER NOWRAP>
								&nbsp;
							</TD>
						</TR>
						
END1

		
		if ($res) {
	
			
			for $i ( 0 .. ($#{$table}) ) {
				$user      = $table->[$i][0];
				$collocMax = $table->[$i][1];
				$sortMax   = $table->[$i][2];
				$distMax   = $table->[$i][3];
				$collocMax=&makeDecimals($collocMax);
				$sortMax=&makeDecimals($sortMax);
				$distMax=&makeDecimals($distMax);
				$count=$i+1;
				$userLink = $user;
				$userLink=~s/(\W)/sprintf("%%%x", ord($1))/eg;	
				$but = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?action=change&theQuery=userLimits&user=' . $userLink . '&urlTest=yes">Change</A>';
				$but2 = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?action=delete&theQuery=userLimits&user=' . $userLink . '&urlTest=yes">Delete</A>';
				print "<TR BGCOLOR=\"#DDDDDD\"><TD VALIGN=TOP>$count</TD><TD VALIGN=TOP>$user</TD><TD VALIGN=TOP ALIGN=CENTER>$collocMax</TD><TD VALIGN=TOP ALIGN=CENTER>$sortMax</TD><TD VALIGN=TOP ALIGN=CENTER>$distMax</TD><TD VALIGN=TOP ALIGN=CENTER>$but2&nbsp;&nbsp;&nbsp;$but</TD></TR>"
			}
	
		
			print "<TR><TD COLSPAN =\"6\">* The default values can be changed in the library file bncConfigXML.pm.</TD></TR></TABLE>";
	
		}
	
		print <<END;
		<FORM ACTION="/$bwCGIalias/BNCquery.pl" METHOD=GET>
		<INPUT TYPE="hidden" NAME="action" VALUE="add">
		<INPUT TYPE="hidden" NAME="theQuery" VALUE="userLimits">
		
		<TABLE BORDER="0" WIDTH="100%" CELLPADDING=3 CELLSPACING=6 BGCOLOR="#FFFFFF">
		<TR>
			<TD HEIGHT="27" COLSPAN="2" BGCOLOR="#DDDDDD">
				Enter usernames that you wish to add to the list (use commas or spaces to separate the individual usernames):
			</TD>
		</TR>
		<TR>
			<TD BGCOLOR="#DDDDDD">
				<TEXTAREA NAME="theUsers" ROWS="3" COLS="58" WRAP="physical"></TEXTAREA>
			</TD>
			<TD BGCOLOR="#DDDDDD">
				<INPUT TYPE="hidden" NAME="urlTest" VALUE="yes">
				<INPUT TYPE="submit" VALUE="Add users">
				</FORM>
			</TD>
		</TR>
		</TABLE>
END

		print "<HR>$bwCopyright</TD></TR></TABLE>";
	} elsif ($action eq "change") {
		print <<END;
		
		<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="6" WIDTH="100%">
			<TR>
				<TD WIDTH="100%" HEIGHT="22" BGCOLOR="#FFCC99">
					<H3 ALIGN="CENTER"><B>User limits</B>
				</TD>
			</TR>
END
		
		$theUserQuote = $bncHandle->quote($params{user});
		
		$sqlQuery = "select collocationMax, sortMax, distMax from userLimits where user = $theUserQuote";
		$sth = $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
		$sth->execute or bark("Can't execute $sqlQuery: ". $sth::errstr);
		@rowArray=$sth->fetchrow_array;
		$collocationMax=$rowArray[0];
		$sortMax=$rowArray[1];
		$distMax=$rowArray[2];
	
		print <<END;
		<FORM ACTION="/$bwCGIalias/BNCquery.pl" METHOD=GET>
		<INPUT TYPE="hidden" NAME="action" VALUE="changeValues">
		<INPUT TYPE="hidden" NAME="theQuery" VALUE="userLimits">
		<INPUT TYPE="hidden" NAME="user" VALUE="$params{user}">
		
		<TABLE BORDER="0" CELLPADDING="2" CELLSPACING="6" WIDTH="100%">
			<TR BGCOLOR="#FFCC66">
				<TD WIDTH="20%">
					<B>User</B>
				</TD>
				<TD ALIGN=CENTER NOWRAP>
					<B>Collocation feature</B>
				</TD>
				<TD ALIGN=CENTER NOWRAP>
					<B>Sort/frequency list feature</B>
				</TD>
				<TD ALIGN=CENTER NOWRAP>
					<B>Distribution feature</B>
				</TD>
			</TR>
			<TR BGCOLOR="#DDDDDD">
				<TD WIDTH="20%">
					<B>$params{user}</B>
				</TD>
				<TD ALIGN=CENTER NOWRAP>
					<input name="collocMax" type="text" VALUE="$collocationMax">
				</TD>
				<TD ALIGN=CENTER NOWRAP>
					<input name="sortMax" type="text" VALUE="$sortMax">
				</TD>
				<TD ALIGN=CENTER NOWRAP>
					<input name="distMax" type="text" VALUE="$distMax">
				</TD>
			</TR>
			<TR>
			<TD ALIGN=MIDDLE COLSPAN="4" BGCOLOR="#DDDDDD">
				<INPUT TYPE="hidden" NAME="urlTest" VALUE="yes">
				<INPUT TYPE="submit" VALUE="Save changes" >
				</FORM>
			</TD>
		</TR>
		</TABLE>
END

		
	print "<HR>$bwCopyright</TD></TR></TABLE>";
	}

}


sub adminStatus {

	unless (($username=~m/\A($bwSuperuser)\Z/) || (&checkSuperUser2())) {

		print <<END;
		<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="6" WIDTH="100%">
			<TR>
				<TD WIDTH="100%" HEIGHT="22" BGCOLOR="#FFCC99">
					<H3 ALIGN="CENTER"><B>You are not authorised to access this page! Please don't fiddle with the URL... ;-)</B>
				</TD>
			</TR>
		</TABLE>
END
		print "<HR>$bwCopyright</TD></TR></TABLE></TD></TR></TABLE>";
		exit(0);	
	}

	# connect to mysql
	$bncHandle = DBI->connect("DBI:mysql:$bwMYSQLtable:$bwMYSQLhost:$bwMysqlPort", "$bwMysqlUser", "$bwMysqlPwd") or bark("Can't connect to mysql on $bwMYSQLhost at port: $bwMysqlPort <BR>$bncHandle::errstr\n");

	
	if ($bwSuperuser=~m/\|/) {
		@superUsers = split(/\|/, $bwSuperuser);
		foreach $superUser (@superUsers) {
			$defaultUsers .= '<TR BGCOLOR="#CCCCCC"> <TD WIDTH="5%"> &nbsp; </TD> <TD WIDTH="20%">' . $superUser . '*</TD> <TD ALIGN="CENTER">yes</TD> </TR>';
		}
	} else {
		$defaultUsers = '<TR BGCOLOR="#CCCCCC"> <TD WIDTH="5%"> &nbsp; </TD> <TD WIDTH="20%">' . $bwSuperuser . '*</TD> <TD ALIGN="CENTER">yes</TD> </TR>';
	}
							
	if (!$params{action}) {
		$action = "display";
	} else {
		$action = $params{action};
	}

	if ($action eq "add") {

		if ($params{theUsers}) {
			$theUsers = $params{theUsers};

			$theUsers =~ s/\r+/ /g;
			$theUsers =~ s/\n+/ /g;
			$theUsers =~ s/\+/ /g;
			$theUsers =~ s/\s+/\,/g;
			$theUsers =~ s/\,+/·/g;
			if ($theUsers=~m/·/) {
				@theUsers = split (/·/, $theUsers);
			} else {
				$theUsers[0]=$theUsers;
			}
			
			foreach $theUser (@theUsers) {
				$theUserQuote = $bncHandle->quote($theUser);
				$sqlQuery = "INSERT INTO adminStatus (user, adminStatus) values ($theUserQuote, 'yes')";
				$bncHandle->do($sqlQuery) or bark("Can't insert $sqlQuery:" . $bncHandle::errstr) unless ($theUser=~m/\A($bwSuperuser)\Z/);
				
			}		
		}
		$action = "display";
	} elsif ($action eq "update") {
		$theUserQuote = $bncHandle->quote($params{user});

		$sqlQuery = "delete from adminStatus";
		$bncHandle->do($sqlQuery) or bark("Can't insert $sqlQuery:" . $bncHandle::errstr);
		
		while ($ENV{'QUERY_STRING'}=~m/user=([^\&]+)/g) {
			$theUser = $1;
			$theUserQuote = $bncHandle->quote($theUser);
			$sqlQuery = "INSERT INTO adminStatus (user, adminStatus) values ($theUserQuote, 'yes')";
			$bncHandle->do($sqlQuery) or bark("Can't insert $sqlQuery:" . $bncHandle::errstr);
			
		}
		
 		$action = "display";
	}
	
	if ($action eq "display") {
	
	print <<END;
	
	<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="6" WIDTH="100%">
		<TR>
			<TD WIDTH="100%" HEIGHT="22" BGCOLOR="#FFCC99">
				<H3 ALIGN="CENTER"><B>Manage admin access</B>
			</TD>
		</TR>
END
	
		$sqlQuery = "select user, adminStatus from adminStatus order by user asc";
		
		$sth2= $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
		$sth2->execute or bark("Can't execute $sqlQuery: $sth2::errstr");
		$res = $sth2->rows;
		$table = $sth2->fetchall_arrayref or bark("Can't fetchrow_arrayref $sqlQuery: $sth2::errstr");
		
		$bwDefaultCollocationDec=&makeDecimals($bwDefaultCollocation);
		$bwDefaultSortDec=&makeDecimals($bwDefaultSort);
		$bwDefaultDistDec=&makeDecimals($bwDefaultDist);
		print <<END1;
			<TR>
				<TD>
					<TABLE BORDER="0" CELLPADDING="2" CELLSPACING="6" WIDTH="100%">
						<FORM ACTION="/$bwCGIalias/BNCquery.pl" METHOD=GET>
						<INPUT TYPE="hidden" NAME="action" VALUE="update">
						<INPUT TYPE="hidden" NAME="theQuery" VALUE="adminStatus">
						<TR BGCOLOR="#FFCC66">
							<TD WIDTH="5%">
								<B>No.</B>
							</TD>
							<TD WIDTH="80%">
								<B>User</B>
							</TD>
							<TD ALIGN=CENTER NOWRAP>
								<B>Administrator access</B>
							</TD>
						</TR>
						$defaultUsers
END1

		
		if ($res) {
	
			
			for $i ( 0 .. ($#{$table}) ) {
				$user      = $table->[$i][0];
				$admin     = $table->[$i][1];
				$count=$i+1;
				$checked{$user} = "CHECKED" if $admin eq "yes";
				
				$userLink = $user;
				$userLink=~s/(\W)/sprintf("%%%x", ord($1))/eg;	
				$but = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?action=change&theQuery=adminStatus&user=' . $userLink . '&urlTest=yes">Change</A>';
				$but2 = '<A HREF="/' . $bwCGIalias . '/BNCquery.pl?action=delete&theQuery=adminStatus&user=' . $userLink . '&urlTest=yes">Delete</A>';
				print "<TR BGCOLOR=\"#DDDDDD\"><TD VALIGN=TOP>$count</TD><TD VALIGN=TOP>$user</TD><TD VALIGN=TOP ALIGN=CENTER><INPUT TYPE=\"CHECKBOX\" NAME=\"user\" VALUE=\"$user\" $checked{$user}></TD></TR>"
			}
	
		
		}
	
		print <<END;
				<TR BGCOLOR=#CCCCCC>
					<TD COLSPAN =\"2\">* The admin status of this user is set in bncConfigXML.pm.</TD>
					<TD ALIGN="CENTER">
						<INPUT TYPE="hidden" NAME="urlTest" VALUE="yes">
						<INPUT TYPE="submit" VALUE="Update">
						</FORM>
					</TD>
				</TR>
				<TR><TD COLSPAN="3"></TD><TR>
				<FORM ACTION="/$bwCGIalias/BNCquery.pl" METHOD=GET>
				<INPUT TYPE="hidden" NAME="action" VALUE="add">
				<INPUT TYPE="hidden" NAME="theQuery" VALUE="adminStatus">
				
				<TR>
					<TD HEIGHT="27" COLSPAN="3" BGCOLOR="#DDDDDD">
						Enter usernames that you wish to add to the list (use commas or spaces to separate the individual usernames):
					</TD>
				</TR>
				<TR>
					<TD BGCOLOR="#DDDDDD" COLSPAN="2">
						<TEXTAREA NAME="theUsers" ROWS="3" COLS="58" WRAP="physical"></TEXTAREA>
					</TD>
					<TD BGCOLOR="#DDDDDD" ALIGN="CENTER">
						<INPUT TYPE="hidden" NAME="urlTest" VALUE="yes">
						<INPUT TYPE="submit" VALUE="Add users">
						</FORM>
					</TD>
				</TR>
				<TR BGCOLOR="#EEEEEE">
					<TD COLSPAN="3">
					Users with admin rights can:
					<ul>
						<li>View the query history of all users</li>
						<li>View (and delete) saved queries of all users</li>
						<li>View (and delete) categorized queries of all users</li>
						<li>View (and delete) subcorpora of all users</li>
						<li>Change user limits for all users</li>
						<li>Add and remove other admin users</li>
					</ul>
					</TD>
				</TR>
			</TABLE>
END

		print "<HR>$bwCopyright</TD></TR></TABLE>";
	}

}
