<?php
ini_set("display_errors","1");
ini_set("display_startup_errors","1");
header("Expires: Thu, 01 Jan 1970 00:00:01 GMT"); 
set_magic_quotes_runtime(0);

include("oldinclude/dbcommon.php");
include("oldinclude/##@TABLE.strShortTableName s##_variables.php");
##if @BUILDER.arrLanguages.len>1##
include("oldinclude/languages.php");
##endif##

##if @BUILDER.bCreateLoginPage##
if(!@$_SESSION["UserID"])
{ 
	$_SESSION["MyURL"]=$_SERVER["SCRIPT_NAME"]."?".$_SERVER["QUERY_STRING"];
	header("Location: login.php?message=expired"); 
	return;
}
if(!CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Search"))
{
	echo "<p>".##message NO_PERMISSIONS##." <a href=\"login.php\">".##message BACK_TO_LOGIN##."</a></p>";
	return;
}
##endif##


include('oldlibs/Smarty.class.php');
$smarty = new Smarty();

$conn=db_connect();

//	Before Process event
if(function_exists("BeforeProcessPrint"))
	BeforeProcessPrint($conn);


$smarty->assign("includes",
"<script language=\"JavaScript\" src=\"oldinclude/jsfunctions.js\">".
"</script>".
"<script language=\"JavaScript\" src=\"oldinclude/jquery.js\">".
"</script>".
"<script>".
"	var bSelected=false;".
"var TEXT_FIRST = \"".##message FIRST##."\";".
"var TEXT_PREVIOUS = \"".##message PREVIOUS##."\";".
"var TEXT_NEXT = \"".##message NEXT##."\";".
"var TEXT_LAST = \"".##message LAST##."\";".
"</script>");

$groupno=0;

if(!$_SESSION[$strTableName."_pagenumber"])
	$_SESSION[$strTableName."_pagenumber"]=1;
if(!$_SESSION[$strTableName."_pagesize"])
	$_SESSION[$strTableName."_pagesize"]=5;

$bAll=false;
$PageSize=$_SESSION[$strTableName."_pagesize"];
if(@$_REQUEST["all"])
{
	$PageSize=-1;
	$bAll=true;
}

$pagestart=($_SESSION[$strTableName."_pagenumber"]-1)*$_SESSION[$strTableName."_pagesize"]+1;
$pageend=$pagestart+$_SESSION[$strTableName."_pagesize"]-1;

// init totals

##foreach @TABLE.arrReportGroupFields as @g##
##if @first##
// group totals
$grouptotals_count = array();
$grouptotals_min = array();
$grouptotals_max = array();
$grouptotals_dispmin = array();
$grouptotals_dispmax = array();
$grouptotals_sum = array();
##endif##
$grouptotals_sum["##@g.strGroupField s##"]=array();
$grouptotals_min["##@g.strGroupField s##"]=array();
$grouptotals_max["##@g.strGroupField s##"]=array();
$grouptotals_dispmin["##@g.strGroupField s##"]=array();
$grouptotals_dispmax["##@g.strGroupField s##"]=array();
$grouptotals_count["##@g.strGroupField s##"]=0;
	##foreach Fields as @f filter @f.bReportPage##
$grouptotals_sum["##@g.strGroupField s##"]["##@f.strName s##"]=0;
	##endfor##
##endfor##

##if @TABLE.bReportPageSummary##
// page totals
$pagetotals_count = 0;
$pagetotals_min = array();
$pagetotals_max = array();
$pagetotals_dispmin = array();
$pagetotals_dispmax = array();
$pagetotals_sum = array();
	##foreach Fields as @f filter @f.bReportPage##
$pagetotals_sum["##@f.strName s##"]=0;
	##endfor##
##endif##

##if @TABLE.bReportGlobalSummary##
// global totals
$globaltotals_count = 0;
$globaltotals_min = array();
$globaltotals_max = array();
$globaltotals_dispmin = array();
$globaltotals_dispmax = array();
$globaltotals_sum = array();
	##foreach Fields as @f filter @f.bReportPage##
$globaltotals_sum["##@f.strName s##"]=0;
	##endfor##
##endif##


$strWhereClause = @$_SESSION[$strTableName."_where"];
##if @TABLE.nSecOptions==ADVSECURITY_VIEW_OWN  && @BUILDER.nLoginMethod==SECURITY_TABLE && @BUILDER.bCreateLoginPage##
if(!$strWhereClause)
	$strWhereClause=whereAdd($strWhereClause,SecuritySQL("Search"));
##endif##

$strSQL = gSQLWhere($strWhereClause);

$strOrderBy=$_SESSION[$strTableName."_order"];
if(!$strOrderBy)
	$strOrderBy=$gstrOrderBy;
$strSQL.=" ".trim($strOrderBy);

//	select all records

$strSQLbak = $strSQL;
if(function_exists("BeforeQueryPrint"))
	BeforeQueryPrint($strSQL,$strWhereClause,$strOrderBy);

if($strSQLbak == $strSQL)
{
	$strSQL = gSQLWhere($strWhereClause);
	$strSQL.=" ".trim($strOrderBy);
}

LogInfo($strSQL);
$rs=db_query($strSQL,$conn);

$start=true;

##foreach @TABLE.arrReportGroupFields as @g##
	$groupstart_##@g.strGroupField g##=0;
	$groupvalue_##@g.strGroupField g##="";
##endfor##

$pages=array();
$page=array();
$rowinfo = array();
$rowcount=0;
$summaryrows=1;
##if Fields[bReportPage && bSum].len##
$summaryrows++;
##endif##
##if Fields[bReportPage && bAvg].len##
$summaryrows++;
##endif##
##if Fields[bReportPage && bMin].len##
$summaryrows++;
##endif##
##if Fields[bReportPage && bMax].len##
$summaryrows++;
##endif##

while($data=db_fetch_array($rs))
{
	$row=array();
//	check if starting new group
##foreach @TABLE.arrReportGroupFields as @g##
	$newgroup_##@g.strGroupField g##=false;
	$firstnewgroup_##@g.strGroupField g##=false;
##endfor##
	$newgroup=false;

##foreach @TABLE.arrReportGroupFields as @g##
	##if @first##
		if($start || $groupstart_##@g.strGroupField g##!=GetGroupStart("##@g.strGroupField s##",$data["##@g.strGroupField s##"]))
		{
	##else##
		if($newgroup || $groupstart_##@g.strGroupField g##!=GetGroupStart("##@g.strGroupField s##",$data["##@g.strGroupField s##"]))
		{
	##endif##
			$newgroup_##@g.strGroupField g##=true;
			if(!$newgroup)
				$firstnewgroup_##@g.strGroupField g##=true;
			$newgroup=true;
		}
	##if @last##
	##endif##
##endfor##
	
//	close groups
	$rowclose=array();
##foreach @TABLE.arrReportGroupFields as @g##
	if(!$start && $newgroup_##@g.strGroupField g##)
	{
		$rowclose["endgroup_##@g.strGroupField g##"]=true;
		$rowcount+=$summaryrows;
		$rowclose["1##@g.strGroupField g##_grval"]=$groupvalue_##@g.strGroupField g##;
		$rowclose["group##@g.strGroupField g##_total_cnt"]=$grouptotals_count["##@g.strGroupField s##"];
	##foreach Fields as @f filter @f.bReportPage && @f.bSum##
//		$rowclose["group##@g.strGroupField g##_total##@f.strName g##_sum"]=$grouptotals_sum["##@g.strGroupField s##"]["##@f.strName s##"];
		$sum=array("##@f.strName s##"=>$grouptotals_sum["##@g.strGroupField s##"]["##@f.strName s##"]);
		$rowclose["group##@g.strGroupField g##_total##@f.strName g##_sum"]=GetData($sum,"##@f.strName s##","##@f.strViewFormat s##");
	##endfor##
	##if Fields[bReportPage && (bAvg || bMin || bMax)].len##
		if($grouptotals_count["##@g.strGroupField s##"])
		{
		##foreach Fields as @f filter @f.bReportPage##
		##if @f.bAvg##
			$avg=array("##@f.strName s##"=>$grouptotals_sum["##@g.strGroupField s##"]["##@f.strName s##"]/$grouptotals_count["##@g.strGroupField s##"]);
			$rowclose["group##@g.strGroupField g##_total##@f.strName g##_avg"]=GetData($avg,"##@f.strName s##","##@f.strViewFormat s##");
		##endif##
		##if @f.bMin##
			$rowclose["group##@g.strGroupField g##_total##@f.strName g##_min"]=$grouptotals_dispmin["##@g.strGroupField s##"]["##@f.strName s##"];
		##endif##
		##if @f.bMax##
			$rowclose["group##@g.strGroupField g##_total##@f.strName g##_max"]=$grouptotals_dispmax["##@g.strGroupField s##"]["##@f.strName s##"];
		##endif##
		##endfor##
		}
	##endif##
	}
##endfor##
##if !@TABLE.arrReportGroupFields.len##
	$groupno++;
##endif##
	if(($PageSize<0 || $groupno>=$pagestart && $groupno<=$pageend) && count($rowclose))
	{
		$rowinfo[]=$rowclose;
	}
##if @TABLE.bReportPageSummary##
	if($bAll && $rowcount>=##@TABLE.nReportLinesPerPage##-$summaryrows)
##else##
	if($bAll && $rowcount>=##@TABLE.nReportLinesPerPage##)
##endif##
	{
		$page["rowinfo"]=$rowinfo;
		$page["showpagesummary"]=true;
		ClosePage();
		$pages[]=$page;
		$rowinfo=array();
		$rowcount=0;
	}
	$start=false;
//	open new group
##foreach @TABLE.arrReportGroupFields as @g##
	if($newgroup_##@g.strGroupField g##)
	{
	##if @first##
		$groupno++;
	##endif##
		$row["newgroup_##@g.strGroupField g##"]=true;
		$row["firstnewgroup_##@g.strGroupField g##"]=$firstnewgroup_##@g.strGroupField g##;

		$groupstart_##@g.strGroupField g## = GetGroupStart("##@g.strGroupField s##",$data["##@g.strGroupField s##"]);
		$groupdisplay_##@g.strGroupField g## = GetGroupStart("##@g.strGroupField s##",$data["##@g.strGroupField s##"]);
		$groupvalue_##@g.strGroupField g##=GetGroupDisplay("##@g.strGroupField s##",$data["##@g.strGroupField s##"]);
		
		$row["1##@g.strGroupField g##_grval"]=$groupvalue_##@g.strGroupField g##;
		
		$grouptotals_count["##@g.strGroupField s##"]=0;
	##foreach Fields as @f filter @f.bReportPage##
		$grouptotals_sum["##@g.strGroupField s##"]["##@f.strName s##"]=0;
	##endfor##
	}
##endfor##

//	display data
	if($PageSize<0 || $groupno>=$pagestart && $groupno<=$pageend)
	{
		$row["havedata"]=true;
/////////////////////////////////
			$keylink="";
##foreach @TABLE.arrKeyFields as @k##
			$keylink.="&key##@index##=".htmlspecialchars(rawurlencode(@$data["##@k s##"]));
##endfor##

##foreach Fields as @f filter @f.bReportPage && (@TABLE.bReportShowDetails || @TABLE.arrReportGroupFields[strGroupField==@f.strName && nGroupInterval==0].len) order nReportPageOrder##

//	##@f.strName## - ##@f.strViewFormat##
			$value="";
	##if @f.strViewFormat==FORMAT_DATABASE_IMAGE##
		##if @f.bShowThumbnail##
				$value.="<a target=_blank href=\"##@TABLE.strShortTableName s##_imager.php?field=##@f.strName uhs##".$keylink."\">";
				$value.= "<img border=0";
				$value.=" src=\"##@TABLE.strShortTableName s##_imager.php?field=##@f.strThumbnail uhs##&alt=##@f.strName uhs##".$keylink."\">";
				$value.= "</a>";
		##else##
				$value = "<img";
			##if @f.ListFormatObj.nImageWidth##
				$value.=" width=##@f.ListFormatObj.nImageWidth##";
			##endif##
			##if @f.ListFormatObj.nImageHeight##
				$value.=" height=##@f.ListFormatObj.nImageHeight##";
			##endif##
				$value.=" border=0";
				$value.=" src=\"##@TABLE.strShortTableName s##_imager.php?field=##@f.strName uhs##".$keylink."\">";
		##endif##
	##elseif @f.strViewFormat==FORMAT_FILE_IMAGE##
			if(CheckImageExtension($data["##@f.strName s##"])) 
			{
		##if @f.bShowThumbnail##
			 	// show thumbnail
				$thumbname="##@f.strThumbnail s##".$data["##@f.strName s##"];
				if(substr("##@f.strhlPrefix s##",0,7)!="http://" && !file_exists(GetUploadFolder("##@f.strName s##").$thumbname))
					$thumbname=$data["##@f.strName s##"];
				$value="<a target=_blank href=\"".htmlspecialchars(AddLinkPrefix("##@f.strName s##",$data["##@f.strName s##"]))."\">";
				$value.="<img";
				if($thumbname==$data["##@f.strName s##"])
				{
			##if @f.ListFormatObj.nImageWidth##
					$value.=" width=##@f.ListFormatObj.nImageWidth##";
			##endif##
			##if @f.ListFormatObj.nImageHeight##
					$value.=" height=##@f.ListFormatObj.nImageHeight##";
			##endif##
				}
				$value.=" border=0";
				$value.=" src=\"".htmlspecialchars(AddLinkPrefix("##@f.strName s##",$thumbname))."\"></a>";
		##else##
				$value="<img";
			##if @f.ListFormatObj.nImageWidth##
				$value.=" width=##@f.ListFormatObj.nImageWidth##";
			##endif##
			##if @f.ListFormatObj.nImageHeight##
				$value.=" height=##@f.ListFormatObj.nImageHeight##";
			##endif##
				$value.=" border=0";
				$value.=" src=\"".htmlspecialchars(AddLinkPrefix("##@f.strName s##",$data["##@f.strName s##"]))."\">";
		##endif##
			}
	##elseif @f.strViewFormat==FORMAT_DATABASE_FILE##
		##if @f.ListFormatObj.strFilename##
			$filename=$data["##@f.ListFormatObj.strFilename s##"];
			if(!$filename)
				$filename="file.bin";
		##else##
			$filename="file.bin";
		##endif##
			if(strlen($data["##@f.strName s##"]))
			{
				$value="<a href=\"##@TABLE.strShortTableName##_getfile.php?filename=".rawurlencode($filename)."&field=##@f.strName uhs##".$keylink."\">";
				$value.=htmlspecialchars($filename);
				$value.="</a>";
			}
	##elseif (@f.strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD || @f.strEditFormat==EDIT_FORMAT_RADIO) && pLookupObj.nLookupType == LT_LOOKUPTABLE##
/*	
			if(strlen($data["##@f.strName s##"]))
			{
				$strdata = make_db_value("##@f.strName s##",$data["##@f.strName s##"]);
				$LookupSQL="SELECT ";
			##if @f.EditFormatObj.pLookupObj.bCustomDisplay##
				$LookupSQL.="##@f.strDisplayField s##";
			##else## 
				$LookupSQL.="##@f.strDisplayField ws##";
			##endif##
				$LookupSQL.=" FROM ##@f.EditFormatObj.pLookupObj.strTable ts## WHERE ##@f.strLinkField ws## = " . $strdata;
			##if EditFormatObj.pLookupObj.strWhere##
				$LookupSQL.=" and (".##EditFormatObj.pLookupObj.strWhere##.")"; 
			##endif##
				LogInfo($LookupSQL);
				$rsLookup = db_query($LookupSQL,$conn);
				$lookupvalue=$data["##@f.strName s##"];
				if($lookuprow=db_fetch_numarray($rsLookup))
					$lookupvalue=$lookuprow[0];
				$value=ProcessLargeText(GetDataInt($lookupvalue,$data,"##@f.strName s##", "##@f.strViewFormat s##"),"field=##@f.strName uhs##".$keylink,"",MODE_PRINT);
			}
			else
				$value="";
*/				
		$value=DisplayLookupWizard("##@f.strName s##",$data["##@f.strName s##"],$data,$keylink,MODE_PRINT);
	##elseif NeedEncode(@f)##
			$value = ProcessLargeText(GetData($data,"##@f.strName s##", "##@f.strViewFormat##"),"field=##@f.strName uhs##".$keylink,"",MODE_PRINT);
	##else##
			$value = GetData($data,"##@f.strName s##", "##@f.strViewFormat##");
	##endif##
	##if @TABLE.bReportShowDetails##
			$row["1##@f.strName g##_value"]=$value;
	##endif##
	##if @TABLE.arrReportGroupFields[strGroupField==@f.strName && nGroupInterval==0].len##
		
		if(@$row["newgroup_##@f.strName g##"])
		{
			$row["1##@f.strName g##_grval"]=$value;
			$groupvalue_##@f.strName g##=$value;
		}
	
	##endif##	
##endfor##
//////////////////////////////////////////////////////////////	

//	update totals
##foreach @TABLE.arrReportGroupFields as @g##
	##foreach Fields as @f filter @f.bReportPage && (@f.bSum || @f.bAvg)##
		$grouptotals_sum["##@g.strGroupField s##"]["##@f.strName s##"]+=$data["##@f.strName s##"];
	##endfor##
	##if Fields[bReportPage && (bMin || bMax)].len##
		if(!$grouptotals_count["##@g.strGroupField s##"])
		{
		##foreach Fields as @f filter @f.bReportPage && @f.bMin##
			$grouptotals_min["##@g.strGroupField s##"]["##@f.strName s##"]=$data["##@f.strName s##"];
			$grouptotals_dispmin["##@g.strGroupField s##"]["##@f.strName s##"]=GetData($data,"##@f.strName s##","##@f.strViewFormat s##");
		##endfor##
		##foreach Fields as @f filter @f.bReportPage && @f.bMax##
			$grouptotals_max["##@g.strGroupField s##"]["##@f.strName s##"]=$data["##@f.strName s##"];
			$grouptotals_dispmax["##@g.strGroupField s##"]["##@f.strName s##"]=GetData($data,"##@f.strName s##","##@f.strViewFormat s##");
		##endfor##
		}
		else
		{
		##foreach Fields as @f filter @f.bReportPage || (@f.bMin || @f.bMax) order nReportPageOrder##
			##if @f.bMin ##
			if($grouptotals_min["##@g.strGroupField s##"]["##@f.strName s##"]>$data["##@f.strName s##"])
			{
				$grouptotals_min["##@g.strGroupField s##"]["##@f.strName s##"]=$data["##@f.strName s##"];
				$grouptotals_dispmin["##@g.strGroupField s##"]["##@f.strName s##"]=GetData($data,"##@f.strName s##","##@f.strViewFormat s##");
			}
			##endif##
			##if @f.bMax##
			if($grouptotals_max["##@g.strGroupField s##"]["##@f.strName s##"]<$data["##@f.strName s##"])
			{
				$grouptotals_max["##@g.strGroupField s##"]["##@f.strName s##"]=$data["##@f.strName s##"];
				$grouptotals_dispmax["##@g.strGroupField s##"]["##@f.strName s##"]=GetData($data,"##@f.strName s##","##@f.strViewFormat s##");
			}
			##endif##
		##endfor##
		}
	##endif##
		$grouptotals_count["##@g.strGroupField s##"]++;
##endfor##
##if @TABLE.bReportPageSummary##
	##foreach Fields as @f filter @f.bReportPage && (@f.bSum || @f.bAvg)##
		$pagetotals_sum["##@f.strName s##"]+=$data["##@f.strName s##"];
	##endfor##
	##if Fields[bReportPage && (bMin || bMax)].len##
		if(!$pagetotals_count)
		{
		##foreach Fields as @f filter @f.bReportPage && @f.bMin##
			$pagetotals_min["##@f.strName s##"]=$data["##@f.strName s##"];
			$pagetotals_dispmin["##@f.strName s##"]=GetData($data,"##@f.strName s##","##@f.strViewFormat s##");
		##endfor##
		##foreach Fields as @f filter @f.bReportPage && @f.bMax##
			$pagetotals_max["##@f.strName s##"]=$data["##@f.strName s##"];
			$pagetotals_dispmax["##@f.strName s##"]=GetData($data,"##@f.strName s##","##@f.strViewFormat s##");
		##endfor##
		}
		else
		{
		##foreach Fields as @f filter @f.bReportPage || (@f.bMin || @f.bMax) order nReportPageOrder##
			##if @f.bMin##
			if($pagetotals_min["##@f.strName s##"]>$data["##@f.strName s##"])
			{
				$pagetotals_min["##@f.strName s##"]=$data["##@f.strName s##"];
				$pagetotals_dispmin["##@f.strName s##"]=GetData($data,"##@f.strName s##","##@f.strViewFormat s##");
			}
			##endif##
			##if @f.bMax##
			if($pagetotals_max["##@f.strName s##"]<$data["##@f.strName s##"])
			{
				$pagetotals_max["##@f.strName s##"]=$data["##@f.strName s##"];
				$pagetotals_dispmax["##@f.strName s##"]=GetData($data,"##@f.strName s##","##@f.strViewFormat s##");
			}
			##endif##
		##endfor##
		}
	##endif##
		$pagetotals_count++;
##endif##
		$rowinfo[]=$row;
		$rowcount++;
##if @TABLE.bReportPageSummary##
		if($bAll && $rowcount>=##@TABLE.nReportLinesPerPage##-$summaryrows)
##else##
		if($bAll && $rowcount>=##@TABLE.nReportLinesPerPage##)
##endif##
		{
			$page["rowinfo"]=$rowinfo;
			$page["showpagesummary"]=true;
			ClosePage();
			$pages[]=$page;
			$rowinfo=array();
			$rowcount=0;
		}
	}
##if @TABLE.bReportGlobalSummary##
	##foreach Fields as @f filter @f.bReportPage && (@f.bSum || @f.bAvg)##
		$globaltotals_sum["##@f.strName s##"]+=$data["##@f.strName s##"];
	##endfor##
	##if Fields[bReportPage && (bMin || bMax)].len##
		if(!$globaltotals_count)
		{
		##foreach Fields as @f filter @f.bReportPage && @f.bMin##
			$globaltotals_min["##@f.strName s##"]=$data["##@f.strName s##"];
			$globaltotals_dispmin["##@f.strName s##"]=GetData($data,"##@f.strName s##","##@f.strViewFormat s##");
		##endfor##
		##foreach Fields as @f filter @f.bReportPage && @f.bMax##
			$globaltotals_max["##@f.strName s##"]=$data["##@f.strName s##"];
			$globaltotals_dispmax["##@f.strName s##"]=GetData($data,"##@f.strName s##","##@f.strViewFormat s##");
		##endfor##
		}
		else
		{
		##foreach Fields as @f filter @f.bReportPage || (@f.bMin || @f.bMax) order nReportPageOrder##
			##if @f.bMin##
			if($globaltotals_min["##@f.strName s##"]>$data["##@f.strName s##"])
			{
				$globaltotals_min["##@f.strName s##"]=$data["##@f.strName s##"];
				$globaltotals_dispmin["##@f.strName s##"]=GetData($data,"##@f.strName s##","##@f.strViewFormat s##");
			}
			##endif##
			##if @f.bMax##
			if($globaltotals_max["##@f.strName s##"]<$data["##@f.strName s##"])
			{
				$globaltotals_max["##@f.strName s##"]=$data["##@f.strName s##"];
				$globaltotals_dispmax["##@f.strName s##"]=GetData($data,"##@f.strName s##","##@f.strViewFormat s##");
			}
			##endif##
		##endfor##
		}
	##endif##
		$globaltotals_count++;
##endif##
}

if($PageSize<0 || $groupno>=$pagestart && $groupno<=$pageend)
{
//	close groups
	$rowclose=array();
##foreach @TABLE.arrReportGroupFields as @g##
	$rowclose["endgroup_##@g.strGroupField g##"]=true;
	$rowcount+=$summaryrows;
	$rowclose["1##@g.strGroupField g##_grval"]=$groupvalue_##@g.strGroupField g##;
	$rowclose["group##@g.strGroupField g##_total_cnt"]=$grouptotals_count["##@g.strGroupField s##"];
	##foreach Fields as @f filter @f.bReportPage && @f.bSum##
//		$rowclose["group##@g.strGroupField g##_total##@f.strName g##_sum"]=$grouptotals_sum["##@g.strGroupField s##"]["##@f.strName s##"];
		$sum=array("##@f.strName s##"=>$grouptotals_sum["##@g.strGroupField s##"]["##@f.strName s##"]);
		$rowclose["group##@g.strGroupField g##_total##@f.strName g##_sum"]=GetData($sum,"##@f.strName s##","##@f.strViewFormat s##");
	##endfor##
	##if Fields[bReportPage && (bAvg || bMin || bMax)].len##
		if($grouptotals_count["##@g.strGroupField s##"])
		{
		##foreach Fields as @f filter @f.bReportPage##
		##if @f.bAvg##
			$avg=array("##@f.strName s##"=>$grouptotals_sum["##@g.strGroupField s##"]["##@f.strName s##"]/$grouptotals_count["##@g.strGroupField s##"]);
			$rowclose["group##@g.strGroupField g##_total##@f.strName g##_avg"]=GetData($avg,"##@f.strName s##","##@f.strViewFormat s##");
		##endif##
		##if @f.bMin##
			$rowclose["group##@g.strGroupField g##_total##@f.strName g##_min"]=$grouptotals_dispmin["##@g.strGroupField s##"]["##@f.strName s##"];
		##endif##
		##if @f.bMax##
			$rowclose["group##@g.strGroupField g##_total##@f.strName g##_max"]=$grouptotals_dispmax["##@g.strGroupField s##"]["##@f.strName s##"];
		##endif##
		##endfor##
	}
	##endif##
##endfor##
	if(count($rowclose))
		$rowinfo[]=$rowclose;
}
if(count($rowinfo))
{
	$page["rowinfo"]=$rowinfo;
##if @TABLE.bReportPageSummary##
	$page["showpagesummary"]=true;
	ClosePage();
	$rowcount+=$summaryrows;
##endif##
}


##if @TABLE.bReportGlobalSummary##
	if($bAll && $rowcount>=##@TABLE.nReportLinesPerPage##-$summaryrows)
	{
		$pages[]=$page;
		$page=array();
	}
	$page["showglobalsummary"]=true;
	$smarty->assign("global_total_cnt",$globaltotals_count);
	##foreach Fields as @f filter @f.bReportPage order nReportPageOrder##
		##if @f.bSum##
//	$smarty->assign("global_total##@f.strName g##_sum",$globaltotals_sum["##@f.strName s##"]);
	$sum=array("##@f.strName s##"=>$globaltotals_sum["##@f.strName s##"]);
	$smarty->assign("global_total##@f.strName g##_sum",GetData($sum,"##@f.strName s##","##@f.strViewFormat s##"));
		##endif##
		##if @f.bAvg || @f.bMin || @f.bMax##
	if($globaltotals_count)
	{
		##if @f.bAvg##
		$avg=array("##@f.strName s##"=>$globaltotals_sum["##@f.strName s##"]/$globaltotals_count);
		$smarty->assign("global_total##@f.strName g##_avg",GetData($avg,"##@f.strName s##","##@f.strViewFormat s##"));
		##endif##
		##if @f.bMin##
		$smarty->assign("global_total##@f.strName g##_min",$globaltotals_dispmin["##@f.strName s##"]);
		##endif##
		##if @f.bMax##
		$smarty->assign("global_total##@f.strName g##_max",$globaltotals_dispmax["##@f.strName s##"]);
		##endif##
	}
		##endif##
	##endfor##
##endif##

$pages[]=$page;
$smarty->assign("pages",$pages);


$mypage=$_SESSION[$strTableName."_pagenumber"];
$maxpages=ceil($groupno/$PageSize);
//	write pagination
if($maxpages>1)
	$smarty->assign("pagination","<script language=\"JavaScript\">WritePagination(".$mypage.",".$maxpages.");
		function GotoPage(nPageNumber)
		{
			window.location='##@TABLE.strShortTableName##_report.php?goto='+nPageNumber;
		}
</script>");

$smarty->assign("gpp1_selected",($PageSize==1)?"selected":"");
$smarty->assign("gpp3_selected",($PageSize==3)?"selected":"");
$smarty->assign("gpp5_selected",($PageSize==5)?"selected":"");
$smarty->assign("gpp10_selected",($PageSize==10)?"selected":"");
$smarty->assign("gpp50_selected",($PageSize==50)?"selected":"");
$smarty->assign("gpp100_selected",($PageSize==100)?"selected":"");
$smarty->assign("gpp0_selected",($PageSize==-1)?"selected":"");


##if @BUILDER.bCreateLoginPage##
$smarty->assign("userid",htmlspecialchars($_SESSION["UserID"]));
##endif##


##if GroupSecurity##
//	table selector
##foreach @BUILDER.Tables as @t filter @t.bMenuItem##
$strPerm = GetUserPermissions("##@t.strDataSourceTable s##");
$smarty->assign("allow_##@t.strShortTableName##",!(strpos($strPerm, "A")===false && strpos($strPerm, "S")===false));
##endfor##
##endif##

##if GroupSecurity##
	$smarty->assign("allow_delete",CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Delete"));
	$smarty->assign("allow_add",CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Add"));
	$smarty->assign("allow_edit",CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Edit"));
	$smarty->assign("allow_export",CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Export"));
	$smarty->assign("allow_search",CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Search"));
	$smarty->assign("allow_deleteorexport",CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Delete") || CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Export") );
##endif##

$pagename = $_SERVER["REQUEST_URI"];
if(!$pagename)
{
	$pagename=basename(__file__);
	$params="";
	foreach($_GET as $k=>$v)
	{
		if(strlen($params))
			$params.="&";
		$params.=rawurlencode($k)."=".rawurlencode($v);
	}
	if(strlen($params))
		$pagename.="?".$params;
}
if(strpos($pagename,"?")===false)
	$pagename.="?pdf=1";
else
	$pagename.="&pdf=1";
$smarty->assign("page",htmlspecialchars($pagename));

if(postvalue("pdf"))
	$smarty->assign("pdf",true);

$smarty->assign("pageurl",$pagename);
if(postvalue("all"))
	$smarty->assign("all",true);


$strSQL=$_SESSION[$strTableName."_sql"];

$templatefile = "##@TABLE.strShortTableName##_print.htm";
if(function_exists("BeforeShowPrint"))
	BeforeShowPrint($smarty,$templatefile);


if(!postvalue("pdf"))
	$smarty->display($templatefile);
else
{
	$page = $smarty->fetch($templatefile);
	$pagewidth=postvalue("width")*1.05;
	$pageheight=postvalue("height")*1.05;
	$landscape=false;
	if($pagewidth>$pageheight)
	{
		$landscape=true;
		if($pagewidth/$pageheight<297/210)
			$pagewidth = 297/210*$pageheight;
	}
	else
	{
		if($pagewidth/$pageheight<210/297)
			$pagewidth = 210/297*$pageheight;
	}
	include("plugins/page2pdf.php");
}

function GetGroupStart($field,$value)
{
##foreach @TABLE.arrReportGroupFields as @g##
##foreach Fields as @f filter @f.strName=@g.strGroupField##
	if($field=="##@g.strGroupField s##")
	{
	##if @g.nGroupInterval==0##
		return $value;
	##elseif IsNumberField(@f)##
		return $value-($value%##@g.nGroupInterval##);
	##elseif IsCharField(@f)##
		return substr($value,0,##@g.nGroupInterval##);
	##elseif IsDateField(@f)##
		$tm = db2time($value);
		if(!count($tm))
			return "";
		##if @g.nGroupInterval==DATE_INTERVAL_YEAR##
		return $tm[0];
		##elseif @g.nGroupInterval==DATE_INTERVAL_QUARTER##
		return $tm[0]*4+floor($tm[1]/3);
		##elseif @g.nGroupInterval==DATE_INTERVAL_MONTH##
		return $tm[0]*12+$tm[1];
		##elseif @g.nGroupInterval==DATE_INTERVAL_WEEK##
		return getweeknumber($tm);
		##elseif @g.nGroupInterval==DATE_INTERVAL_DAY##
		return ($tm[0]*12+$tm[1])*31+$tm[2];
		##elseif @g.nGroupInterval==DATE_INTERVAL_HOUR##
		return (($tm[0]*12+$tm[1])*31+$tm[2])*24+$tm[3];
		##elseif @g.nGroupInterval==DATE_INTERVAL_MINUTE##
		return ((($tm[0]*12+$tm[1])*31+$tm[2])*24+$tm[3])*60+$tm[4];
		##else##
		return $value;
		##endif##
	##endif##
	}
##endfor##
##endfor##

}

function GetGroupDisplay($field,$value)
{
	global $locale_info;
##foreach @TABLE.arrReportGroupFields as @g##
##foreach Fields as @f filter @f.strName=@g.strGroupField##
	if($field=="##@g.strGroupField s##")
	{
	##if @g.nGroupInterval==0##
		return $value;
	##elseif IsNumberField(@f)##
		$start=$value-($value%##@g.nGroupInterval##);
		$end=$start+##@g.nGroupInterval##;
		return $start." - ".$end;
	##elseif IsCharField(@f)##
		return substr($value,0,##@g.nGroupInterval##);
	##elseif IsDateField(@f)##
		$tm = db2time($value);
		if(!count($tm))
			return "";
		##if @g.nGroupInterval==DATE_INTERVAL_YEAR##
		return $tm[0];
		##elseif @g.nGroupInterval==DATE_INTERVAL_QUARTER##
		return $tm[0]."/Q".floor(($tm[1]-1)/4+1);
		##elseif @g.nGroupInterval==DATE_INTERVAL_MONTH##
		return @$locale_info["LOCALE_SABBREVMONTHNAME".$tm[1]]." ".$tm[0];
		##elseif @g.nGroupInterval==DATE_INTERVAL_WEEK##
		$start = getweekstart($tm);
		$end = adddays($start,6);
		return format_shortdate($start)." - ".format_shortdate($end);
		##elseif @g.nGroupInterval==DATE_INTERVAL_DAY##
		return format_shortdate($tm);
		##elseif @g.nGroupInterval==DATE_INTERVAL_HOUR##
		$tm[4]=0;
		$tm[5]=0;
		return format_datetime($tm);
		##elseif @g.nGroupInterval==DATE_INTERVAL_MINUTE##
		$tm[5]=0;
		return format_datetime($tm);
		##else##
		return format_datetime($tm);
		##endif##
	##endif##
	}
##endfor##
##endfor##

}

function ClosePage()
{
	global $page;
	global $pagetotals_count;
	global $pagetotals_min;
	global $pagetotals_max;
	global $pagetotals_dispmin;
	global $pagetotals_dispmax;
	global $pagetotals_sum;
##if @TABLE.bReportPageSummary##
		$page["page_total_cnt"]=$pagetotals_count;
	##foreach Fields as @f filter @f.bReportPage order nReportPageOrder##
		##if @f.bSum##
//		$page["page_total##@f.strName g##_sum"]=$pagetotals_sum["##@f.strName s##"];
		$sum=array("##@f.strName s##"=>$pagetotals_sum["##@f.strName s##"]);
		$page["page_total##@f.strName g##_sum"]=GetData($sum,"##@f.strName s##","##@f.strViewFormat s##");
		##endif##
		##if @f.bAvg || @f.bMin || @f.bMax##
		if($pagetotals_count)
		{
		##if @f.bAvg##
			$avg=array("##@f.strName s##"=>$pagetotals_sum["##@f.strName s##"]/$pagetotals_count);
			$page["page_total##@f.strName g##_avg"]=GetData($avg,"##@f.strName s##","##@f.strViewFormat s##");
		##endif##
		##if @f.bMin##
			$page["page_total##@f.strName g##_min"]=$pagetotals_dispmin["##@f.strName s##"];
		##endif##
		##if @f.bMax##
			$page["page_total##@f.strName g##_max"]=$pagetotals_dispmax["##@f.strName s##"];
		##endif##
		}
		##endif##
	##endfor##
	$pagetotals_count = 0;
	$pagetotals_min = array();
	$pagetotals_max = array();
	$pagetotals_dispmin = array();
	$pagetotals_dispmax = array();
	$pagetotals_sum = array();
	##foreach Fields as @f filter @f.bReportPage##
	$pagetotals_sum["##@f.strName s##"]=0;
	##endfor##
##endif##
}