#!/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;

#SETTINGS
$thisProg  = "keywords.pl";
$newQuery  = "BNCquery.pl";
$main      = "processQuery.pl";

$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");
}

$theQuery= $ENV{'QUERY_STRING'};
$theQuery = "&" . $theQuery . "&";
$theInput = $theQuery;

# 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);
$frequencyHandle = DBI->connect("DBI:mysql:$bwMYSQLfrequency:$bwMYSQLhost:$bwMysqlPort", "$bwMysqlUser", "$bwMysqlPwd") or bark("Can't connect to mysql on $bwMYSQLhost at port: $bwMysqlPort <BR>" . $frequencyHandle::errstr);

($table1, $subcorpus1) = split(/##/, $params{table1});
($table2, $subcorpus2) = split(/##/, $params{table2});


if ($table1 eq $table2) {
	barkFull("The two frequency lists you have chosen are identical.");
}


# The following code is partially prepared for offering tag-restrictions
# This is not yet fully functional and has therefore not yet been made available
# SH - 16.7.2007

if ($params{tag} ne "all") {
	$pos = $params{tag};
	$check=$params{tag}=~m/(ADJ|ADV|ART|CONJ|INTERJ|PREP|PRON|SUBST|VERB|UNC|STOP)/;
	if ($check) {
		$value=$1;
		$lemma_tag = 1;
		# for the display 
		$tagRestriction="any verb" if ($value eq "VERB");
		$tagRestriction="any noun" if ($value eq "SUBST");
		$tagRestriction="any adjective" if ($value eq "ADJ");
		$tagRestriction="any adverb" if ($value eq "ADV");
		$tagRestriction="any pronoun" if ($value eq "PRON");
		$tagRestriction="any preposition" if ($value eq "PREP");
		$tagRestriction="any conjunction" if ($value eq "CONJ");
		$tagRestriction="any article" if ($value eq "ART");
		$tagRestriction="any interjection" if ($value eq "INTERJ");
		$tagRestriction="any punctuation" if ($value eq "STOP");
		$tagRestriction="any unclear" if ($value eq "UNC");
		$db_selector = "G";
	} else {
		$tagRestriction = $pos;
		$db_selector = "T";
	}
} else {
	$tagRegexp = "";
	$tagRestriction="";
	$db_selector = "";
}

if ($params{threshold} eq "5") {
	$threshold = "3.84"
} elsif ($params{threshold} eq "1") {
	$threshold = "6.63"
} elsif ($params{threshold} eq "0.1") {
	$threshold = "10.83"
} elsif ($params{threshold} eq "0.01") {
	$threshold = "15.13"
} else {
	$threshold = "0"
}


$tagRegexp = "";

if ($params{compare} eq "word") {
	if ($table1=~m/_\d+/) {
		$wordFreq1="$bwMYSQLfrequency.allWords${db_selector}_$table1";
		$tagRegexp = "and $bwMYSQLfrequency.allWords${db_selector}_$table1.word regexp \'_$pos\$\' " if ($db_selector);
	} else {
		$wordFreq1="$bwMYSQLtable.${table1}${db_selector}";
		$tagRegexp = "and $bwMYSQLtable.${table1}${db_selector}.word regexp \'_$pos\$\' " if ($db_selector);
	}
	if ($table2=~m/\d/) {
		$wordFreq2="$bwMYSQLfrequency.allWords${db_selector}_$table2";
	} else {
		$wordFreq2="$bwMYSQLtable.${table2}${db_selector}";
	}
	
} elsif ($params{compare} eq "wordtag") {
	if ($table1=~m/_\d/) {
		$wordFreq1="$bwMYSQLfrequency.allWordsT_$table1";
	} else {
		if ($table1 eq "allWords") {
			$wordFreq1="$bwMYSQLtable.allWordsT";
		} elsif ($table1 eq "allWordsS") {
			$wordFreq1="$bwMYSQLtable.allWordsTS";
		} else {
			$wordFreq1="$bwMYSQLtable.allWordsTW";
		}
	}
	if ($table2=~m/_\d/) {
		$wordFreq2="$bwMYSQLfrequency.allWordsT_$table2";
	} else {
		if ($table2 eq "allWords") {
			$wordFreq2="$bwMYSQLtable.allWordsT";
		} elsif ($table2 eq "allWordsS") {
			$wordFreq2="$bwMYSQLtable.allWordsTS";
		} else {
			$wordFreq2="$bwMYSQLtable.allWordsTW";
		}
	}
	
} elsif ($params{compare} eq "lemma") {
	if ($table1=~m/_\d/) {
		$wordFreq1="$bwMYSQLfrequency.allLemma_$table1";
	} else {
		if ($table1 eq "allWords") {
			$wordFreq1="$bwMYSQLtable.allLemma";
		} elsif ($table1 eq "allWordsS") {
			$wordFreq1="$bwMYSQLtable.allLemmaS";
		} else {
			$wordFreq1="$bwMYSQLtable.allLemmaW";
		}
	}
	if ($table2=~m/_\d/) {
		$wordFreq2="$bwMYSQLfrequency.allLemma_$table2";
	} else {
		if ($table2 eq "allWords") {
			$wordFreq2="$bwMYSQLtable.allLemma";
		} elsif ($table2 eq "allWordsS") {
			$wordFreq2="$bwMYSQLtable.allLemmaS";
		} else {
			$wordFreq2="$bwMYSQLtable.allLemmaW";
		}
	}
	
}

if ($subcorpus1 eq "all") {
	$subcorpus1Show = "Whole BNC";
} elsif ($subcorpus1 eq "1")  {
	$subcorpus1Show = "Written component";
} elsif ($subcorpus1 eq "2")  {
	$subcorpus1Show = "Spoken component";
} else {
	$subcorpus1Show = $subcorpus1;
}
if ($subcorpus2 eq "all") {
	$subcorpus2Show = "Whole BNC";
} elsif ($subcorpus2 eq "1")  {
	$subcorpus2Show = "Written component";
} elsif ($subcorpus2 eq "2")  {
	$subcorpus2Show = "Spoken component";
} else {
	$subcorpus2Show = $subcorpus2;
}

# update timestamp in cache
$theTime = int(time);
if ($table1=~m/\d/)  {
	$sqlQuery = "UPDATE frequencyLists set time_of_creation=$theTime where theName='$table1'";
	$bncHandle->do($sqlQuery) or bark("Can't $sqlQuery: " . $bncHandle::errstr);
}
if ($table2=~m/\d/)  {
	$sqlQuery = "UPDATE frequencyLists set time_of_creation=$theTime where theName='$table2'";
	$bncHandle->do($sqlQuery) or bark("Can't $sqlQuery: " . $bncHandle::errstr);
}



$sqlQuery = "SELECT sum(freq) FROM $wordFreq1";
$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;
$totalTable1=$rowArray[0];

$sqlQuery = "SELECT sum(freq) FROM $wordFreq2";
$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;
$totalTable2=$rowArray[0];


if ($params{inst} ne "show all") {
	# set start and end s number
	$start = ($params{chunk} * $params{inst}) - ($params{inst});
	$end = (($params{chunk} + 1) * $params{inst}) - ($params{inst});
	$limit = "LIMIT $start, $params{inst}";
} else {
	$limit = "";
}
if ($params{compare} eq "lemma") {
	$wordOrLemma = "Lemma";
} else {
	$wordOrLemma = "Word";
}

if ($params{theMethod} eq "Calculate keywords!") {
	if ($params{statistics} eq "LL") {
	
		$sqlQuery = "select $wordFreq1.$wordOrLemma, $wordFreq1.freq as freq1, $wordFreq2.freq as freq2, 
		($totalTable1 * ($wordFreq1.freq + $wordFreq2.freq) / ($totalTable1 + $totalTable2)) as E1, 
		($totalTable2 * ($wordFreq1.freq + $wordFreq2.freq) / ($totalTable1 + $totalTable2)) as E2, 
		2 * (($wordFreq1.freq * log($wordFreq1.freq / ($totalTable1 * ($wordFreq1.freq + $wordFreq2.freq) / ($totalTable1 + $totalTable2)))) + 
		($wordFreq2.freq * log($wordFreq2.freq / ($totalTable2 * ($wordFreq1.freq + $wordFreq2.freq) / ($totalTable1 + $totalTable2))))) as theValue 
		from $wordFreq1, $wordFreq2 where $wordFreq1.$wordOrLemma = $wordFreq2.$wordOrLemma 
		and $wordFreq1.freq >= $params{f1} and $wordFreq2.freq >= $params{f2} $tagRegexp having theValue >= $threshold order by theValue desc $limit;
		";
		$displayType = "LL-value";
	} elsif ($params{statistics} eq "MI") {
		# not yet implemented
	} else {
		# not yet implemented
		$sqlQuery = "select $wordFreq1.$wordOrLemma, $wordFreq1.freq as freq1, $wordFreq2.freq as freq2, 
		($totalTable1 * ($wordFreq1.freq + $wordFreq2.freq) / ($totalTable1 + $totalTable2)) as E1, 
		($totalTable2 * ($wordFreq1.freq + $wordFreq2.freq) / ($totalTable1 + $totalTable2)) as E2, 
	
		
		(POW(($wordFreq1.freq - ($totalTable1 * ($wordFreq1.freq + $wordFreq2.freq) / ($totalTable1 + $totalTable2))), 2)
		/ ($totalTable1 * ($wordFreq1.freq + $wordFreq2.freq) / ($totalTable1 + $totalTable2)))
		
		+
		
		(POW(($wordFreq2.freq - ($totalTable2 * ($wordFreq1.freq + $wordFreq2.freq) / ($totalTable1 + $totalTable2))), 2)
		/ ($totalTable2 * ($wordFreq1.freq + $wordFreq2.freq) / ($totalTable1 + $totalTable2)))
		
		+
		
		(POW((($totalTable1 - $wordFreq1.freq) - ($totalTable1 - ($totalTable1 * ($wordFreq1.freq + $wordFreq2.freq) / ($totalTable1 + $totalTable2)))), 2)
		/ ($totalTable1 - ($totalTable1 * ($wordFreq1.freq + $wordFreq2.freq) / ($totalTable1 + $totalTable2))))
		
		
		+
		
		(POW((($totalTable2 - $wordFreq2.freq) - ($totalTable2 - ($totalTable2 * ($wordFreq1.freq + $wordFreq2.freq) / ($totalTable1 + $totalTable2)))), 2)
		/ ($totalTable2 - ($totalTable2 * ($wordFreq1.freq + $wordFreq2.freq) / ($totalTable1 + $totalTable2))))
		
		as theValue
		
		from $wordFreq1, $wordFreq2 where $wordFreq1.$wordOrLemma = $wordFreq2.$wordOrLemma and 
		$wordFreq1.freq >= $params{f1} and $wordFreq2.freq >= $params{f2} order by theValue having theValue >= $threshold desc $limit;
		";
		$displayType = "Chi-square value";
	}
	
	# 	$totalTable1 - ($totalTable1 * ($wordFreq1.freq + $wordFreq2.freq) / ($totalTable1 + $totalTable2)) as E3,
	# 	$totalTable2 - ($totalTable2 * ($wordFreq1.freq + $wordFreq2.freq) / ($totalTable1 + $totalTable2)) as E4,
	$theTitle = "<B>Keywords for '$subcorpus1Show' and '$subcorpus2Show'</B>";
	
} elsif ($params{theMethod} eq "Compare lists!") {
	
	if ($params{empty} eq "f2") {
		$sqlQuery = "SELECT * FROM $wordFreq1 left join $wordFreq2 on $wordFreq1.$wordOrLemma = $wordFreq2.$wordOrLemma where $wordFreq2.freq is NULL order by $wordFreq1.freq desc $limit";
		$theTitle = "<B>Items which only occur in '$subcorpus1Show'</B>";
	} else {
		$sqlQuery = "SELECT * FROM $wordFreq2 left join $wordFreq1 on $wordFreq2.$wordOrLemma = $wordFreq1.$wordOrLemma where $wordFreq1.freq is NULL order by $wordFreq2.freq desc $limit";
		$theTitle = "<B>Items which only occur in '$subcorpus2Show'</B>";
	}
}

$sth = $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery:" . $bncHandle::errstr);
$sth->execute or bark("Can't execute $sqlQuery:" . $sth::errstr);
$countFRes=$sth->rows;
$resArrayResult=$sth->fetchall_arrayref;

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

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

<TITLE>Keywords results page</TITLE>

<META NAME="generator" CONTENT="BBEdit 4.5">
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<TABLE BORDER="0" CELLPADDING="2" CELLSPACING="2" WIDTH="100%">
	<TR WIDTH="100%">
		<TD HEIGHT="26" BGCOLOR="#CCCCFF" COLSPAN="5">
			<P ALIGN="LEFT">$theTitle
		</TD>
	</TR>
END

$theLines="";
for $k ( 0 .. ($#{$resArrayResult}) ) {
	$theWord=$resArrayResult->[$k][0];
	$theFrequency1=$resArrayResult->[$k][1];
	$theFrequency2=$resArrayResult->[$k][2];
	$E1=$resArrayResult->[$k][3];
	$E2=$resArrayResult->[$k][4];
	$LL=$resArrayResult->[$k][5];
	
		
	if ($theFrequency1 > $E1) {
		$colour1 = "CCCCCC";
		$colour2 = "DDDDDD";
		$plusMinus = "+";
	} else {
		$colour2 = "CCCCCC";
		$colour1 = "DDDDDD";
		$plusMinus = "-";
	}
	
	if (($params{showTag}) && (!$theTags) && (!$showLemma)) {
		$theWord=~m/_([A-Z0-2]{3}(_[A-Z0-2]{3})?)\Z/;
		$theWord=$`;
		$theTag=$1;
	}
	$theTag=~s/_/-/;
	
	if ($showLemma) {
		$theWord=~m/_([A-Z]+)\Z/;
		$theWord= $`;
		$theTag=$1;
	} else {
		$theWord=~s/~/ /g;
	}
	$theWord=&output2($theWord);
	$theLink1 = '<A HREF="/' . $bwCGIalias . '/' . $main . '?queryType=simple&qMode=Simple+query+(ignore+case)&qtype=' . $subcorpus1 . '&thMode=INIT&theData=XYZ2&chunk=1&inst=50&max=INIT&qname=INIT&urlTest=yes">';
	$theLink2 = '<A HREF="/' . $bwCGIalias . '/' . $main . '?queryType=simple&qMode=Simple+query+(ignore+case)&qtype=' . $subcorpus2 . '&thMode=INIT&theData=XYZ2&chunk=1&inst=50&max=INIT&qname=INIT&urlTest=yes">';
	
	
	if ($params{compare} eq "lemma") {
		$theSearchWord = $theWord;
		$theSearchWord=~m/_(ADJ|ADV|ART|CONJ|INTERJ|PREP|PRON|SUBST|VERB|UNC|STOP)/;
		$theSearchWord = lc($`);
		$theTag = $1;
		$theQuery = "{" . $theSearchWord . "/" . $theTag . "}";
	} elsif ($params{compare} eq "wordtag") {

		$theSearchWord = $theWord;
		$theSearchWord=~m/_([A-Z0-9]+)\Z/;
		$theSearchWord = lc($`);
		$theTag = $1;
		$theQuery = $theSearchWord . "_" . $theTag;
				
	} else {
		$theQuery = lc($theWord);
	}

	if (($params{compare} eq "wordtag") || ($params{compare} eq "lemma")) {
		$theWord=~m/\A([^_]+)/;
		$theWord = $1;
		$theWord = lc($theWord);
		$theWord = $theWord . $';
	} else {
		$theWord = lc($theWord);	
	}
	$theQuery=~s/(\W)/sprintf("%%%x", ord($1))/eg;
	$theQuery=~s/ /+/g;
	
	
	if ($params{theMethod} eq "Compare lists!") {
		
		if ($params{empty} eq "f2") {
			$theLink1=~s/XYZ2/$theQuery/;
			$theLink2 = "0";
		} else {
			$theLink2=~s/XYZ2/$theQuery/;
			$theLink1 = "0";
		}
		
	} else {
		$theLink1=~s/XYZ2/$theQuery/;
		$theLink2=~s/XYZ2/$theQuery/;
	}
	
	
	$theFrequency1=&makeDecimals($theFrequency1);
	$theFrequency2=&makeDecimals($theFrequency2);
	
	$LL = int(($LL * 100) + 0.5)/ 100;
	
	$lineIndicator = $start + 1 + $k;

	if ($params{theMethod} eq "Compare lists!") {
		if ($params{empty} eq "f2") {
			$theLines .= '<TR VALIGN="Top" BGCOLOR="#DDDDDD"><TD ALIGN="RIGHT" WIDTH="5%"><B>' . "$lineIndicator</B></TD>" . '<TD ALIGN="LEFT" NOWRAP WIDTH="40%" >' . "&nbsp;<B>$theWord</B></TD>"  . '<TD NOWRAP ALIGN=MIDDLE >' . "&nbsp;$theLink1$theFrequency1</A></TD><TD NOWRAP ALIGN=MIDDLE > 0 </TD>";
		} else {
			$theLines .= '<TR VALIGN="Top" BGCOLOR="#DDDDDD"><TD ALIGN="RIGHT" WIDTH="5%"><B>' . "$lineIndicator</B></TD>" . '<TD ALIGN="LEFT" NOWRAP WIDTH="40%" >' . "&nbsp;<B>$theWord</B></TD>"  . '<TD NOWRAP ALIGN=MIDDLE > 0 </TD><TD NOWRAP ALIGN=MIDDLE >' . "&nbsp;$theLink2$theFrequency1</A></TD>";
		}
	} else {
			$theLines .= '<TR VALIGN="Top" BGCOLOR="#DDDDDD"><TD ALIGN="RIGHT" WIDTH="5%"><B>' . "$lineIndicator</B></TD>" . '<TD ALIGN="LEFT" NOWRAP WIDTH="40%" BGCOLOR="#' . $colour1 . '">' . "&nbsp;<B>$theWord</B></TD>"  . '<TD NOWRAP ALIGN=MIDDLE BGCOLOR="#' . $colour1 . '">' . "&nbsp;$theLink1$theFrequency1</A></TD>" . '<TD NOWRAP ALIGN=MIDDLE BGCOLOR="#' . $colour2 . '">' . "&nbsp;$theLink2$theFrequency2</A></TD>" . '<TD ALIGN="CENTER"> ' . $plusMinus . '<TD NOWRAP ALIGN=MIDDLE>' . "&nbsp;$LL</TD>";
	}
}

if (!$countFRes) {
	print <<END;
		</TABLE>
		<H3>There are no words in the BNC that match your requirements</H3>
END
	# print the end of html
	&printFooter();
	
	exit(0);
}

&createNavigationLinks();

print <<END;
	<TR WIDTH="100%">$navBar
		<FORM ACTION="/$bwCGIalias/dlogs.pl" METHOD=GET>
			<TD BGCOLOR="#DDDDDD">
				<SELECT NAME="selected">
					<OPTION>New Keyword calculation</OPTION>
					<OPTION>Download whole list</OPTION>
					<OPTION>New Query</OPTION>
				</SELECT>
				<INPUT TYPE="hidden" NAME="table1" VALUE="$params{table1}">
				<INPUT TYPE="hidden" NAME="table2" VALUE="$params{table2}">
				<INPUT TYPE="hidden" NAME="theMethod" VALUE="$params{theMethod}">
				<INPUT TYPE="hidden" NAME="f1" VALUE="$params{f1}">
				<INPUT TYPE="hidden" NAME="f2" VALUE="$params{f2}">
				<INPUT TYPE="hidden" NAME="empty" VALUE="$params{empty}">
				<INPUT TYPE="hidden" NAME="tag" VALUE="$params{tag}">
				<INPUT TYPE="hidden" NAME="threshold" VALUE="$params{threshold}">
				<INPUT TYPE="hidden" NAME="compare" VALUE="$params{compare}">
				<INPUT TYPE="hidden" NAME="statistics" VALUE="$params{statistics}">
				<INPUT TYPE="hidden" NAME="urlTest" VALUE="yes">
				<INPUT TYPE="SUBMIT" VALUE="Go!">
			</TD>
		</FORM>
		</TR>
		</TABLE>
	<TABLE BORDER="0" CELLPADDING="2" CELLSPACING="2" WIDTH="100%">
END

$addCategory = "";
$addCategory = "<TD ALIGN=\"CENTER\">+/-</TD><TD VALIGN=TOP ALIGN=MIDDLE><B>$displayType</B></TD>" if ($params{theMethod} eq "Calculate keywords!");

print '<TR BGCOLOR="#FFCC66">' . "<TD VALIGN=TOP WIDTH=\"5%\"><B>&nbsp;No.</B></TD><TD VALIGN=TOP WIDTH=\"40%\"><B>&nbsp;$wordOrLemma</B></TD><TD VALIGN=TOP ALIGN=MIDDLE><B>Frequency in '$subcorpus1Show'</B></TD><TD VALIGN=TOP ALIGN=MIDDLE><B>Frequency in '$subcorpus2Show'</B></TD>$addCategory</TR>\n";


print $theLines;
print "</TABLE>";

# print the end of html
&printFooter();

exit(0);

sub createNavigationLinks {
	# print links to other pages
	if ($params{inst} ne "show all") {
		
		$theLink = '<A HREF="/' . $bwCGIalias . '/' . $thisProg . '?XYZ2">';
		$link = "chunk=XXZ1&inst=$paramsEnc{inst}&table1=$paramsEnc{table1}&table2=$paramsEnc{table2}&compare=$paramsEnc{compare}&statistics=$paramsEnc{statistics}&threshold=$paramsEnc{threshold}&empty=$paramsEnc{empty}&f1=$paramsEnc{f1}&f2=$paramsEnc{f2}&theMethod=$paramsEnc{theMethod}&tag=$paramsEnc{tag}&urlTest=yes";
		$theLink =~ s/XYZ2/$link/o;
		$startFont="<FONT SIZE=3 FACE=\"Arial, Helvetica\"><B>";
		$endFont="</B></FONT>";
		if (($params{chunk} -1) >= 1) {
			$theLink1 = $theLink;
			$i = $params{chunk} - 1;
			$theLink1 =~ s/XXZ1/$i/;
			$navBar.= "<TD WIDTH=\"3%\" BGCOLOR=\"#DDDDDD\" NOWRAP><P ALIGN=\"CENTER\">${theLink1}$startFont&nbsp;&lt;&lt;&nbsp;$endFont</A></TD>\n";
		} else {
			$navBar.= "<TD WIDTH=\"3%\" BGCOLOR=\"#DDDDDD\" NOWRAP><P ALIGN=\"CENTER\">$startFont&nbsp;&lt;&lt;&nbsp;$endFont</TD>\n";
		}
		if ($params{inst} == $countFRes) {
			$theLink1 = $theLink;
			$i = $params{chunk} +1;
			$theLink1 =~ s/XXZ1/$i/;
			$navBar.= "<TD WIDTH=\"3%\" BGCOLOR=\"#DDDDDD\" NOWRAP><P ALIGN=\"CENTER\">${theLink1}$startFont&nbsp;&gt;&gt;&nbsp;$endFont</A></TD>\n";
		} else {
			$navBar.= "<TD WIDTH=\"3%\" BGCOLOR=\"#DDDDDD\" NOWRAP><P ALIGN=\"CENTER\">$startFont&nbsp;&gt;&gt;&nbsp;$endFont</TD>\n";
		}

		if ($params{chunk} != 1) {
			$theLink1 = $theLink;
			$theLink1 =~ s/XXZ1/1/;
			$navBar = "<TD WIDTH=\"3%\" BGCOLOR=\"#DDDDDD\" NOWRAP><P ALIGN=\"CENTER\">${theLink1}$startFont&nbsp;I&lt;&nbsp;$endFont</A></TD>$navBar\n";
		} else {
			$navBar = "<TD WIDTH=\"3%\" BGCOLOR=\"#DDDDDD\" NOWRAP><P ALIGN=\"CENTER\">$startFont&nbsp;I&lt;&nbsp;$endFont</TD>$navBar\n";
		}

	}
}
