<?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("BeforeProcessReport"))
	BeforeProcessReport($conn);


//	process reqest data, fill session variables

if(!count($_POST) && !count($_GET))
{
	$sess_unset = array();
	foreach($_SESSION as $key=>$value)
		if(substr($key,0,strlen($strTableName)+1)==$strTableName."_" &&
			strpos(substr($key,strlen($strTableName)+1),"_")===false)
			$sess_unset[] = $key;
	foreach($sess_unset as $key)
		unset($_SESSION[$key]);
}

if(@$_REQUEST["a"]=="advsearch")
{
	$_SESSION[$strTableName."_asearchnot"]=array();
	$_SESSION[$strTableName."_asearchopt"]=array();
	$_SESSION[$strTableName."_asearchfor"]=array();
	$_SESSION[$strTableName."_asearchfor2"]=array();
	$tosearch=0;
	$asearchfield = postvalue("asearchfield");
	$_SESSION[$strTableName."_asearchtype"] = postvalue("type");
	if(!$_SESSION[$strTableName."_asearchtype"])
		$_SESSION[$strTableName."_asearchtype"]="and";
	foreach($asearchfield as $field)
	{
		$gfield=GoodFieldName($field);
		$asopt=postvalue("asearchopt_".$gfield);
		$value1=postvalue("value_".$gfield);
		$type=postvalue("type_".$gfield);
		$value2=postvalue("value1_".$gfield);
		$not=postvalue("not_".$gfield);
		if($value1 || $asopt=='Empty')
		{
			$tosearch=1;
			$_SESSION[$strTableName."_asearchopt"][$field]=$asopt;
			if(!is_array($value1))
				$_SESSION[$strTableName."_asearchfor"][$field]=$value1;
			else
				$_SESSION[$strTableName."_asearchfor"][$field]=combinevalues($value1);
			$_SESSION[$strTableName."_asearchfortype"][$field]=$type;
			if($value2)
				$_SESSION[$strTableName."_asearchfor2"][$field]=$value2;
			$_SESSION[$strTableName."_asearchnot"][$field]=($not=="on");
		}
	}
	if($tosearch)
		$_SESSION[$strTableName."_search"]=2;
	else
		$_SESSION[$strTableName."_search"]=0;
	$_SESSION[$strTableName."_pagenumber"]=1;
}

##if @BUILDER.arrLanguages.len>1##
if(@$_REQUEST["language"])
	$_SESSION["language"]=@$_REQUEST["language"];
$smarty->assign("language",mlang_getcurrentlang());
##endif##

if(@$_REQUEST["orderby"])
	$_SESSION[$strTableName."_orderby"]=@$_REQUEST["orderby"];

if(@$_REQUEST["pagesize"])
{
	$_SESSION[$strTableName."_pagesize"]=@$_REQUEST["pagesize"];
	$_SESSION[$strTableName."_pagenumber"]=1;
}

if(@$_REQUEST["goto"])
	$_SESSION[$strTableName."_pagenumber"]=@$_REQUEST["goto"];


//	process reqest data - end

$smarty->assign("includes",
"<script language=\"JavaScript\" src=\"oldinclude/jsfunctions.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>");


//	process session variables
//	order by
$strOrderBy="";
$order_ind=-1;

##foreach Fields as @f filter @f.bReportPage##
$smarty->assign("order_dir_##@f.strName g##","a");
##endfor##

if(@$_SESSION[$strTableName."_orderby"])
{
	$order_field=substr($_SESSION[$strTableName."_orderby"],1);
	$order_dir=substr($_SESSION[$strTableName."_orderby"],0,1);
	$order_ind=GetFieldIndex($order_field);

	##foreach Fields as @f filter @f.bReportPage##
	$smarty->assign("order_dir_##@f.strName g##","a");
	if($order_field=="##@f.strName s##")
	{
		if($order_dir=="a")
		{
			$smarty->assign("order_dir_##@f.strName g##","d");
			$img="up";
		}
		else
			$img="down";
		$smarty->assign("order_image_##@f.strName g##","<img src=\"images/".$img.".gif\" border=0>");
	}
	##endfor##

	if($order_ind)
	{
		if($order_dir=="a")
			$strOrderBy="order by ".($order_ind)." asc";
		else 
			$strOrderBy="order by ".($order_ind)." desc";
	}
}
if(!$strOrderBy)
	$strOrderBy=$gstrOrderBy;

//	make sql "select" string

$strWhereClause="";
//$strSQL = $gstrSQL;

//	add search params

if(@$_SESSION[$strTableName."_search"]==2)
//	 advanced search
{
	$sWhere="";
	foreach(@$_SESSION[$strTableName."_asearchfor"] as $f => $sfor)
		{
			$strSearchFor=trim($sfor);
			$strSearchFor2="";
			$type=@$_SESSION[$strTableName."_asearchfortype"][$f];
			if(array_key_exists($f,@$_SESSION[$strTableName."_asearchfor2"]))
				$strSearchFor2=trim(@$_SESSION[$strTableName."_asearchfor2"][$f]);
			if($strSearchFor!="" || true)
			{
				if (!$sWhere) 
				{
					if($_SESSION[$strTableName."_asearchtype"]=="and")
						$sWhere="1=1";
					else
						$sWhere="1=0";
				}
				$strSearchOption=trim($_SESSION[$strTableName."_asearchopt"][$f]);
				if($where=StrWhereAdv($f, $strSearchFor, $strSearchOption, $strSearchFor2,$type))
				{
					if($_SESSION[$strTableName."_asearchnot"][$f])
						$where="not (".$where.")";
					if($_SESSION[$strTableName."_asearchtype"]=="and")
	   					$sWhere .= " and ".$where;
					else
	   					$sWhere .= " or ".$where;
				}
			}
		}
		$strWhereClause = whereAdd($strWhereClause,$sWhere);
//		$strSQL = AddWhere($strSQL,$sWhere);
	}

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

$strSQL = gSQLWhere($strWhereClause);

//	order by
$strSQL.=" ".trim($strOrderBy);


$groupno=0;

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

if(!$_SESSION[$strTableName."_pagesize"])
##if @TABLE.arrReportGroupFields.len>0##
	$_SESSION[$strTableName."_pagesize"]=5;
##else##
	$_SESSION[$strTableName."_pagesize"]=20;
##endif##

$PageSize=$_SESSION[$strTableName."_pagesize"];

$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##

//	save SQL for use in "Export" and "Printer-friendly" pages

$_SESSION[$strTableName."_sql"] = $strSQL;
$_SESSION[$strTableName."_where"] = $strWhereClause;
$_SESSION[$strTableName."_order"] = $strOrderBy;

//	select all records

$strSQLbak = $strSQL;
if(function_exists("BeforeQueryReport"))
	BeforeQueryReport($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##

$rowinfo = array();

while($data=db_fetch_array($rs))
{
	$row=array();
//	check if starting new group
##foreach @TABLE.arrReportGroupFields as @g##
	$newgroup_##@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;
			$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;
		$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;
	$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;

		$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_LIST);
			}
			else
				$value="";
*/				
		$value=DisplayLookupWizard("##@f.strName s##",$data["##@f.strName s##"],$data,$keylink,MODE_LIST);
	##elseif NeedEncode(@f)##
			$value = ProcessLargeText(GetData($data,"##@f.strName s##", "##@f.strViewFormat##"),"field=##@f.strName uhs##".$keylink,"",MODE_LIST);
	##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;
	}
##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;
	$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;
}

$smarty->assign("rowinfo",$rowinfo);

##if @TABLE.bReportPageSummary##
	$smarty->assign("page_total_cnt",$pagetotals_count);
	##foreach Fields as @f filter @f.bReportPage order nReportPageOrder##
		##if @f.bSum##
//	$smarty->assign("page_total##@f.strName g##_sum",$pagetotals_sum["##@f.strName s##"]);
	$sum=array("##@f.strName s##"=>$pagetotals_sum["##@f.strName s##"]);
	$smarty->assign("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);
		$smarty->assign("page_total##@f.strName g##_avg",GetData($avg,"##@f.strName s##","##@f.strViewFormat s##"));
		##endif##
		##if @f.bMin##
		$smarty->assign("page_total##@f.strName g##_min",$pagetotals_dispmin["##@f.strName s##"]);
		##endif##
		##if @f.bMax##
		$smarty->assign("page_total##@f.strName g##_max",$pagetotals_dispmax["##@f.strName s##"]);
		##endif##
	}
		##endif##
	##endfor##
##endif##

##if @TABLE.bReportGlobalSummary##
	$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##

if($groupno)
	$smarty->assign("rowsfound",true);
	
$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>");

##if @TABLE.arrReportGroupFields.len##
$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":"");
##else##
$smarty->assign("rpp10_selected",($PageSize==10)?"selected":"");
$smarty->assign("rpp20_selected",($PageSize==20)?"selected":"");
$smarty->assign("rpp50_selected",($PageSize==50)?"selected":"");
$smarty->assign("rpp100_selected",($PageSize==100)?"selected":"");
$smarty->assign("rpp500_selected",($PageSize==500)?"selected":"");
$smarty->assign("gpp0_selected",($PageSize==-1)?"selected":"");
##endif##

##if @BUILDER.bCreateLoginPage##
$smarty->assign("userid",htmlspecialchars($_SESSION["UserID"]));
$smarty->assign("guest",$_SESSION["AccessLevel"] == ACCESS_LEVEL_GUEST);
##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_export",CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Export"));
	$smarty->assign("allow_search",CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Search"));
##endif##



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

##if @BUILDER.bDynamicPermissions##
$smarty->assign("admin",@$_SESSION["IsAdmin"]);
##endif##

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

$smarty->display($templatefile);

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##-1;
		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##

}
?>