<?php

class Report{
	
	var $from=0;
	var $to=0;
	var $interval=0;
	var $query;
	var $nowpage;
	var $result;
	
	var $fields;
	var $fieldstext;
	var $displayfields;
	var $sizefields;
	var $typefields;
	
	var $order;
	var $direction;
	
	var $tableclass="";
	var $navTableClass="";
	var $navLinksClass="";
	var $sortLinksClass="";
	
	var $sortASCpic="";
	var $sortDESCpic="";
	
	var $leftText="&lt;";
	var $leftJumpText="&lt;&lt;";
	var $rightText="&gt;";
	var $rightJumpText="&gt;&gt;";
	
	var $leftImage="";
	var $leftJumpImage="";
	var $rightImage="";
	var $rightJumpImage="";
	
	var $variance=0;
	
	var $columns;
	
	var $ajax=false;
	var $ajaxLoadingText="Loading...";
	
	var $dbuser="";
	var $dbpassword="";
	var $dbhost="";
	var $dbname="";
	
	var $totalsize;
	
	function __Construct($query,$interval="",$order="",$direction=""){
		
		if ($_GET['qinterval']!="") $interval=$_GET['qinterval'];
		
		if (($order!="")&&($_GET['qorder']=="")){
			$_GET['qorder']=$order;
			$_GET['qdirection']=$direction;
		}
		
		if ($interval!="") $this->ConstructInterval($query,$interval);
		else $this->Construct($query);
		$this->nowpage=$this->clean($_SERVER['REQUEST_URI']);
		
		$rows = mysql_fetch_assoc($this->result);
		
		$this->columns = array_keys($rows);
	}
	
	function ConstructInterval($query,$interval){
		$this->from=0;
		$this->to=$interval;
		$this->interval=$interval;
		$this->query=$query;
		
		$from=false;
		
		if ($_GET['qfrom']!=''){
			$this->from=$_GET['qfrom'];
			$this->to=$this->from+$interval;
			$from=true;
		}
		
		if ($_GET['qorder']==""){
			$this->result=mysql_query("select SQL_CALC_FOUND_ROWS * from(".$query.") as x".($from?" limit ".($this->from.",".$interval):""));
		} else{
			$this->result=mysql_query("select SQL_CALC_FOUND_ROWS * from(".$query." order by ".$_GET['qorder']." ".$_GET['qdirection'].") as x".($from?" limit ".($this->from.",".$interval):""));
			$this->order=$_GET['qorder'];
			$this->direction=$_GET['qdirection'];
		}
		
		$this->totalsize=mysql_result(mysql_query("SELECT FOUND_ROWS()"),0,'FOUND_ROWS()');
		
	}
	
	function Construct($query){
		$this->from=0;
		
		$this->query=$query;
		
		if ($_GET['qorder']==""){
			$this->result=mysql_query($query);
		} else{
			$this->result=mysql_query($query." order by ".$_GET['qorder']." ".$_GET['qdirection']);
			$this->order=$_GET['qorder'];
			$this->direction=$_GET['qdirection'];
		}
		
		$this->to=mysql_num_rows($this->result);
		$this->totalsize=mysql_num_rows($this->result);
		
	}
	
	function addDisplayField($name,$display,$sizeOfField=50){
		$this->fields[]=$name;
		$this->fieldstext[]="";
		$this->displayfields[]=$display;
		$this->sizefields[]=$sizeOfField;
		$this->typefields[]="normal";
	}
	
	function addSpecialDisplayField($name,$display,$text,$sizeOfField=50){
		$this->fields[]=$name;
		$this->fieldstext[]=$text;
		$this->displayfields[]=$display;
		$this->sizefields[]=$sizeOfField;
		$this->typefields[]="special";
	}
	
	function setTableClassName($name){
		$this->tableclass=$name;
	}
	
	function setNavigationTableClassName($name){
		$this->navTableClass=$name;
	}
	
	function setSortLinkClassName($name){
		$this->sortLinksClass=$name;
	}
	
	function setNavigationLinkClass($name){
		$this->navLinksClass=$name;
	}
	
	function setSortASCPicture($loc){
		$this->sortASCpic=$loc;
	}
	
	function setSortDESCPicture($loc){
		$this->sortDESCpic=$loc;
	}
	
	
	
	function setLeftText($text){
		$this->leftText=$text;
	}
	
	function setRightText($text){
		$this->rightText=$text;
	}
	
	function setLeftJumpText($text){
		$this->leftJumpText=$text;
	}
	
	function setRightJumpText($text){
		$this->rightJumpText=$text;
	}
	
	
	function setLeftImage($text){
		$this->leftImage=$text;
	}
	
	function setRightImage($text){
		$this->rightImage=$text;
	}
	
	function setLeftJumpImage($text){
		$this->leftJumpImage=$text;
	}
	
	function setRightJumpImage($text){
		$this->rightJumpImage=$text;
	}
	
	function enableNavigationalNumbers($variance){
		$this->variance=$variance;
	}
	
	function enableAjax($database_host,$database_user,$database_password,$database_name){
		$this->dbuser=$database_user;
		$this->dbpassword=$database_password;
		$this->dbhost=$database_host;
		$this->dbname=$database_name;
		$this->ajax=true;
	}
	
	
	
	function setAjaxLoadingText($text){
		$this->ajaxLoadingText=$text;
	}
	
	function getNumberOfResultsPerPageSelector($interval,$maximum){
		
		?> <select id=queselect> <?
		
		for ($r=$interval;$r<=$maximum;$r+=$interval)
			echo "<option value=".$r.($_GET['qinterval']==$r?" selected":"").">".$r."</option>";
		
		?>
		</select> <?
		
	}
	
	function getNumberOfResultsSelectorGoLink($text){
		
		if($this->ajax){
			$this->getNumberOfResultsSelectorGoLinkAjax($text);
			return;
		}

		$page=str_replace("&qfrom=".$_GET['qfrom'],"",$this->nowpage);
		$page=str_replace("&qinterval=".$_GET['qinterval'],"",$page);
		
		$page=str_replace("?qfrom=".$_GET['qfrom'],"",$page);
		$page=str_replace("?qinterval=".$_GET['qinterval'],"",$page);
		$addchar=strpos($page,"?")!==false?"&":"?";
		
		$page.=$addchar."qinterval='+document.getElementById('queselect').value";

		if ($this->order!=""){
			$page.="+'&qorder=".$this->order."&qdirection=".$this->direction."'";
		}
		
		$page="'".$page;
		
		?><a href="javascript:window.location=<? echo $page?>"><? echo $text;?></a><?
	}
	
	function getNumberOfResultsSelectorGoLinkAjax($text){
		
		$page=$this->getParamStringForSort();
		
		$page=str_replace("&qfrom=".$_GET['qfrom'],"",$page);
		$page=str_replace("&qinterval=".$_GET['qinterval'],"",$page);
		
		$page=str_replace("?qfrom=".$_GET['qfrom'],"",$page);
		$page=str_replace("?qinterval=".$_GET['qinterval'],"",$page);
		
		$page="'".$page."&qinterval='+document.getElementById('queselect').value";
		$page.="+'&qorder='+qorderkeep+'&qdirection='+qdirectionkeep";
		
		?><a href="javascript:loadReport(<? echo $page?>)"><? echo $text;?></a><?
		
	}
	
	function draw(){
		
		if($this->ajax){
			$this->drawAjax();
			return;
		}
		
		?><table<? if ($this->tableclass!="") echo " class='".$this->tableclass."'"; ?>><tr><?
		
		for ($r=0;$r<count($this->fields);$r++){
			?><th width=<? echo $this->sizefields[$r]; ?>>
			<?
			if ($this->order==$this->fields[$r]){
				if ($this->direction=="ASC") $addon="qorder=".$this->fields[$r]."&qdirection=DESC";
				else $addon="";
			
			}
			else $addon="qorder=".$this->fields[$r]."&qdirection=ASC";
			?>
			<a href=<? echo "\"".$this->getLinkWithAddon($addon)."\""; echo $this->sortLinksClass!=""?(" class='".$this->sortLinksClass."'"):""; ?>>
			<? echo $this->displayfields[$r]; ?>
			
			<?
			if ($this->order==$this->fields[$r]){
				if (($this->direction=="ASC")&&($this->sortASCpic!="")){ ?> <img src=<? echo $this->sortASCpic ?>> <?}
			if (($this->direction=="DESC")&&($this->sortDESCpic!="")){ ?> <img src=<? echo $this->sortDESCpic ?>> <?}
			
			}
			?>
			
			</a></th><?
		}
		
		?></tr><?
		
		for ($r=0;(($r<$this->to)&&($r<mysql_num_rows($this->result)));$r++){
			
			?><tr><?
			
			for ($k=0;$k<count($this->fields);$k++){
				?><td><?
				echo $this->getField($r,$k);
				?></td><?
			}
			
			?></tr><?
			
		}
		
		
		
		?></table><?

	}
	
	function drawAjax(){
		
		?><span id=ajaxreportspan><?
		
		?><table<? if ($this->tableclass!="") echo " class='".$this->tableclass."'"; ?>><tr><?
		
		for ($r=0;$r<count($this->fields);$r++){
			?><th width=<? echo $this->sizefields[$r]; ?>>
			<?
			$addon=$this->getParamStringForSort();
			$qtext="";
			$qtext="'".$this->fields[$r]."',";
			if ($this->order==$this->fields[$r]){
				if ($this->direction=="ASC"){
					$addon.="&qorder=".$this->fields[$r]."&qdirection=DESC";
					$qtext.="'DESC'";
				}
				else{ 
					$addon.="";
					$qtext.="''";
				}
			
			}
			else{
				$addon.="&qorder=".$this->fields[$r]."&qdirection=ASC";
				$qtext.="'ASC'";
			}
			
			?>
			<a href="javascript:loadReportKeep('<? echo $addon; ?>',<?echo $qtext?>)"<? echo $this->sortLinksClass!=""?(" class='".$this->sortLinksClass."'"):""; ?>>
			<? echo $this->displayfields[$r]; ?>
			
			<?
			if ($this->order==$this->fields[$r]){
				if (($this->direction=="ASC")&&($this->sortASCpic!="")){ ?> <img src=<? echo $this->sortASCpic ?>> <?}
				if (($this->direction=="DESC")&&($this->sortDESCpic!="")){ ?> <img src=<? echo $this->sortDESCpic ?>> <?}
			
			}
			?>
			
			</a></th><?
		}
		
		?></tr><?
		
		for ($r=0;(($r<$this->to)&&($r<mysql_num_rows($this->result)));$r++){
			
			?><tr><?
			
			for ($k=0;$k<count($this->fields);$k++){
				?><td><?
				echo $this->getField($r,$k);
				?></td><?
			}
			
			?></tr><?
			
		}
		
		
		
		?></table><?
		
		
		
		
		?></span><?
		
	}
	
	function getNavigationBar(){
		
	if($this->ajax){
			$this->getAjaxNavigationBar();
			return;
		}
		
	if (($this->interval==0)||($this->interval=="")) return;
		
		$prev=$this->hasFirstPage();
		$next=$this->hasLastPage();
		
		?><table<? echo $this->navTableClass==""?"":(" class=\"".$this->navTableClass."\""); ?>><tr><td width=<? echo $this->getNavWidth() ?>><?
		
		if ($prev){
			echo $this->getFirstPage();
			echo " ";
			echo $this->getPreviousPage();
			echo " ";
		}
		
		if ($this->variance!=0){
			echo $this->getNavNumbers();
		}
		
		if ($next){
			echo $this->getNextPage();
			echo " ";
			echo $this->getLastPage();
			echo " ";
		}
		
		
		?></td></tr></table><?
	}
	
	function getAjaxNavigationBar(){
		if (($this->interval==0)||($this->interval=="")){
			?><span id=ajaxnavspan><?
			?></span><?
			return;
		}
		
		?><span id=ajaxnavspan><?
		
		$prev=$this->hasFirstPage();
		$next=$this->hasLastPage();
		
		?><table<? echo $this->navTableClass==""?"":(" class=\"".$this->navTableClass."\""); ?>><tr><td width=<? echo $this->getNavWidth() ?>><?
		
		if ($prev){
			echo $this->getFirstPageAjax();
			echo " ";
			echo $this->getPreviousPageAjax();
			echo " ";
		}
		
		if ($this->variance!=0){
			echo $this->getNavNumbersAjax();
		}
		
		if ($next){
			echo $this->getNextPageAjax();
			echo " ";
			echo $this->getLastPageAjax();
			echo " ";
		}
		
		
		?></td></tr></table><?
		
		?></span><?
		
	}
	
	function drawAjaxHeadHtml(){
		?>
		
		<script>

		pageLoader=false;

		var qorderkeep;
		var qdirectionkeep;
		
		set('<? echo $this->order?>','<? echo $this->direction?>');

		function set(k,d){
			qorderkeep=k;
			qdirectionkeep=d;
			}

		function loadReportKeep(params,ok,dk){
			qorderkeep=ok;
			qdirectionkeep=dk;
			loadReport(params);
			}
		
		function loadReport(params){

			document.getElementById('ajaxreportspan').innerHTML="<? echo $this->ajaxLoadingText?>";
			<? if($this->interval!=0){?>document.getElementById('ajaxnavspan').innerHTML="<? echo $this->ajaxLoadingText?>";<? } ?>

			var connect = false;
			/*@cc_on @*/
			/*@if (@_jscript_version >= 5)
			try {
			  connect = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
			  try {
			    connect = new ActiveXObject("Microsoft.XMLHTTP");
			  } catch (e2) {
			    connect = false;
			  }
			}
			@end @*/

			if (!connect && typeof XMLHttpRequest != 'undefined') {
			  connect = new XMLHttpRequest();
			}
			pageLoader=connect;

			url = "report.php";

			connect.open("POST", url, true);

			connect.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			connect.setRequestHeader("Content-length", params.length);
			connect.setRequestHeader("Connection", "close");

			connect.onreadystatechange = PageLoadComplete;

			connect.send(params);
			
			}

		function PageLoadComplete(){
			if(pageLoader.readyState == 4){
				
		               if (pageLoader.status==200){
						   result= pageLoader.responseText;
						   arr=result.split("##24##next##");
						   document.getElementById('ajaxreportspan').innerHTML=arr[0];
						   if (arr[1]!="##none##none##")
						   document.getElementById('ajaxnavspan').innerHTML=arr[1];
		               }
		               else{
		                       alert("invalid response from the server.");
		               }
		        }
				return false;
			}

		</script>
		
		<?
		
	}
	
	
	function getParamStringForSort(){
		$ret="change=order";
	
		$ret.="&dbuser=".$this->dbuser;
		$ret.="&dbpassword=".$this->dbpassword;
		$ret.="&dbhost=".$this->dbhost;
		$ret.="&dbname=".$this->dbname;
		
		$ret.="&interval=".$this->interval;
		$ret.="&query=".$this->query;
	
		$ret.="&tableclass=".$this->tableclass;
		$ret.="&navTableClass=".$this->navTableClass;
		$ret.="&navLinkClass=".$this->navLinksClass;
		$ret.="&sortLinkClass=".$this->sortLinksClass;
	
		$ret.="&sortASCpic=".$this->sortASCpic;
		$ret.="&sortDESCpic=".$this->sortDESCpic;
	
		$ret.="&leftText=".$this->leftText;
		$ret.="&leftJumpText=".$this->leftJumpText;
		$ret.="&rightText=".$this->rightText;
		$ret.="&rightJumpText=".$this->rightJumpText;
	
		$ret.="&leftImage=".$this->leftImage;
		$ret.="&leftJumpImage=".$this->leftJumpImage;
		$ret.="&rightImage=".$this->rightImage;
		$ret.="&rightJumpImage=".$this->rightJumpImage;
	
		$ret.="&variance=".$this->variance;
		
		$ret.="&fields=".count($this->fields);
		
		for ($r=0;$r<count($this->fields);$r++){
			$ret.="&field".$r."=".$this->fields[$r];
			$ret.="&fieldstext".$r."=".$this->fieldstext[$r];
			$ret.="&displayfields".$r."=".$this->displayfields[$r];
			$ret.="&sizefields".$r."=".$this->sizefields[$r];
			$ret.="&typefields".$r."=".$this->typefields[$r];
		}
		
		
		return $this->replaceHTMLChars($ret);
		
	}
	
	function replaceHTMLChars($string){
		
		while(strpos($string,"\"")!==false)
			$string=str_replace("\"","d_o_u_b_l_e_q",$string);
			
		while(strpos($string,"'")!==false)
			$string=str_replace("'","s_i_n_g_l_e_q",$string);
			
		while(strpos($string,"&lt;")!==false)
			$string=str_replace("&lt;","&_l_t_;",$string);
			
		while(strpos($string,"&gt;")!==false)
			$string=str_replace("&gt;","&_g_t_;",$string);
			
		while(strpos($string,"<")!==false)
			$string=str_replace("<","&lt;",$string);
			
		while(strpos($string,">")!==false)
			$string=str_replace(">","&gt;",$string);
			
		return $string;
		
	}
	
	function getField($row,$field){
		
		if ($this->typefields[$field]=="normal") return mysql_result($this->result,$row,$this->fields[$field]);
		
		
		$columns=$this->columns;
		
		$text=$this->fieldstext[$field];
		
		for ($r=0;$r<count($columns);$r++){
			
			while(!strpos($text,"{".$columns[$r]."}")===false)
				$text=str_replace("{".$columns[$r]."}",mysql_result($this->result,$row,$columns[$r]),$text);
			
		}
		
		return $text;
		
	}
	
	function clean($text){
		
		$r=strpos($text,"qorder=");
		if ($r){
			$rem="qorder=".$_GET['qorder'];
			$text=str_replace($rem,"",$text);
		}
		
		$r=strpos($text,"qdirection=");
		if ($r){
			$rem="qdirection=".$_GET['qdirection'];
			$text=str_replace($rem,"",$text);
		}
		
		$r=strpos($text,"qfrom=");
		if ($r){
			$rem="qfrom=".$_GET['qfrom'];
			$text=str_replace($rem,"",$text);
		}
		
		while(strpos($text,"&&"))
			$text=str_replace("&&","&",$text);
			
		if ($text[strlen($text)-1]=="&")
			$text=substr($text,0,strlen($text)-1);
			
		return $text;
		
	}
	
	function getLinkWithAddon($addon){
		if (!strpos($this->nowpage,"?")) return $this->nowpage."?".$addon;
		return $this->nowpage."&".$addon;
	}
	
	
	function hasFirstPage(){
		return $this->from!=0;
	}
	
	function hasLastPage(){
		return $this->from<$this->totalsize-$this->interval;
	}
	
	
	
	
	function getLastPage(){
		return "<a href=\"".$this->getLastLink()."\"".($this->navLinksClass!=''?(" class='".$this->navLinksClass."'"):"")." >".($this->rightJumpImage!=""?"<img src=\"".$this->rightJumpImage."\">":$this->rightJumpText)."</a>";
	}
	
	function getLastPageAjax(){
		return "<a href=\"javascript:loadReport('".$this->getLastLinkAjax()."')\"".($this->navLinksClass!=''?(" class='".$this->navLinksClass."'"):"")." >".($this->rightJumpImage!=""?"<img src=\"".$this->rightJumpImage."\">":$this->rightJumpText)."</a>";
	}
	
	function getFirstPage(){
		return "<a href=\"".$this->getFirstLink()."\"".($this->navLinksClass!=''?(" class='".$this->navLinksClass."'"):"")." >".($this->leftJumpImage!=""?"<img src=\"".$this->leftJumpImage."\">":$this->leftJumpText)."</a>";
	}
	
	function getFirstPageAjax(){
		return "<a href=\"javascript:loadReport('".$this->getFirstLinkAjax()."')\"".($this->navLinksClass!=''?(" class='".$this->navLinksClass."'"):"")." >".($this->leftJumpImage!=""?"<img src=\"".$this->leftJumpImage."\">":$this->leftJumpText)."</a>";
	}
	
	function getPreviousPage(){
		return "<a href=\"".$this->getPreviousLink()."\"".($this->navLinksClass!=''?(" class='".$this->navLinksClass."'"):"")." >".($this->leftImage!=""?"<img src=\"".$this->leftImage."\">":$this->leftText)."</a>";
	}
	
	function getPreviousPageAjax(){
		return "<a href=\"javascript:loadReport('".$this->getPreviousLinkAjax()."')\"".($this->navLinksClass!=''?(" class='".$this->navLinksClass."'"):"")." >".($this->leftImage!=""?"<img src=\"".$this->leftImage."\">":$this->leftText)."</a>";
	}
	
	function getNextPage(){
		return "<a href=\"".$this->getNextLink()."\"".($this->navLinksClass!=''?(" class='".$this->navLinksClass."'"):"")." >".($this->rightImage!=""?"<img src=\"".$this->rightImage."\">":$this->rightText)."</a>";
	}
	
	function getNextPageAjax(){
		return "<a href=\"javascript:loadReport('".$this->getNextLinkAjax()."')\"".($this->navLinksClass!=''?(" class='".$this->navLinksClass."'"):"")." >".($this->rightImage!=""?"<img src=\"".$this->rightImage."\">":$this->rightText)."</a>";
	}
	
	function getNextLink(){
		$l=$this->nowpage;
		$fchar=strpos($this->nowpage,"?")?"&":"?";
		if ($this->order!=""){
			$l.=$fchar."qorder=".$this->order; $fchar="&";
			$l.=$fchar."qdirection=".$this->direction; $fchar="&";
		}
		$l.=$fchar."qfrom=".($this->from+$this->interval);
		return $l;
	}
	
	function getNextLinkAjax(){
		$l=$this->getParamStringForSort();
		$fchar="&";
		if ($this->order!=""){
			$l.=$fchar."qorder=".$this->order;
			$l.=$fchar."qdirection=".$this->direction;
		}
		$l.=$fchar."qfrom=".($this->from+$this->interval);
		return $l;
	}
	
	function getLastLink(){
		
		$l=$this->nowpage;
		$fchar=strpos($this->nowpage,"?")?"&":"?";
		if ($this->order!=""){
			$l.=$fchar."qorder=".$this->order; $fchar="&";
			$l.=$fchar."qdirection=".$this->direction; $fchar="&";
		}
		
		$z=$this->from;
		$r=$this->totalsize;
		
		while($z<$r) $z+=$this->interval;
		$z-=$this->interval;
		
		$l.=$fchar."qfrom=".($z);
		return $l;
		
	}
	
	function getLastLinkAjax(){
		
		$l=$this->getParamStringForSort();
		$fchar="&";
		if ($this->order!=""){
			$l.=$fchar."qorder=".$this->order;
			$l.=$fchar."qdirection=".$this->direction;
		}
		
		$z=$this->from;
		$r=$this->totalsize;
		
		while($z<$r) $z+=$this->interval;
		$z-=$this->interval;
		
		$l.=$fchar."qfrom=".($z);
		return $l;
		
	}
	
	function getPreviousLink(){
		
		$l=$this->nowpage;
		$fchar=strpos($this->nowpage,"?")?"&":"?";
		if ($this->order!=""){
			$l.=$fchar."qorder=".$this->order; $fchar="&";
			$l.=$fchar."qdirection=".$this->direction; $fchar="&";
		}
		
		$l.=$fchar."qfrom=".($this->from-$this->interval);
		return $l;
		
	}
	
	function getPreviousLinkAjax(){
		
		$l=$this->getParamStringForSort();
		$fchar="&";
		if ($this->order!=""){
			$l.=$fchar."qorder=".$this->order;
			$l.=$fchar."qdirection=".$this->direction;
		}
		
		$l.=$fchar."qfrom=".($this->from-$this->interval);
		return $l;
		
	}
	
	function getFirstLink(){
		$l=$this->nowpage;
		$fchar=strpos($this->nowpage,"?")?"&":"?";
		if ($this->order!=""){
			$l.=$fchar."qorder=".$this->order; $fchar="&";
			$l.=$fchar."qdirection=".$this->direction; $fchar="&";
		}
		
		return $l;
	}
	
	function getFirstLinkAjax(){
		$l=$this->getParamStringForSort();
		$fchar="&";
		if ($this->order!=""){
			$l.=$fchar."qorder=".$this->order;
			$l.=$fchar."qdirection=".$this->direction;
		}
		
		return $l;
	}
	
	function getNavLink($r){
		$l=$this->nowpage;
		$fchar=strpos($this->nowpage,"?")?"&":"?";
		if ($this->order!=""){
			$l.=$fchar."qorder=".$this->order; $fchar="&";
			$l.=$fchar."qdirection=".$this->direction; $fchar="&";
		}
		
		$l.=$fchar."qfrom=".$r;
		return $l;
	}
	
	function getNavLinkAjax($r){
		$l=$this->getParamStringForSort();
		$fchar="&";
		if ($this->order!=""){
			$l.=$fchar."qorder=".$this->order;
			$l.=$fchar."qdirection=".$this->direction;
		}
		
		$l.=$fchar."qfrom=".$r;
		return $l;
	}
	
	function getNavNumbers(){
		$nownumber=$this->getNowNumber();
		$max=$this->getMaxPage();
		
		for ($r=$nownumber-$this->variance;(($r<=$nownumber+$this->variance)&&($r<$max));$r++){
			if ($r>=1){
				if ($r==$nownumber)
				echo $r;
				else {
					
					$from=($r*$this->interval)-$this->interval;
					echo "<a href=\"".$this->getNavLink($from)."\"".($this->navLinksClass!=''?(" class='".$this->navLinksClass."'"):"")." >".$r."</a>";
					
				}
				
				echo " ";
			}
		}
		
	}
	
	function getNavNumbersAjax(){
		$nownumber=$this->getNowNumber();
		$max=$this->getMaxPage();
		
		for ($r=$nownumber-$this->variance;(($r<=$nownumber+$this->variance)&&($r<$max));$r++){
			if ($r>=1){
				if ($r==$nownumber)
				echo $r;
				else {
					
					$from=($r*$this->interval)-$this->interval;
					echo "<a href=\"javascript:loadReport('".$this->getNavLinkAjax($from)."')\"".($this->navLinksClass!=''?(" class='".$this->navLinksClass."'"):"")." >".$r."</a>";
					
				}
				
				echo " ";
			}
		}
		
	}
	
	function getNowNumber(){
		return ($this->from/$this->interval)+1;
	}
	
	function getMaxPage(){
		$r=0;
		$page=0;
		$to=$this->totalsize;
		
		while($r<$to){
			$r+=$this->interval;
			$page++;
		}
		
		return $page+1;
		
	}
	
	function getNavWidth(){
		$c=0;
		for ($r=0;$r<count($this->sizefields);$r++){
			$c+=$this->sizefields[$r];
		}
		
		return $c;
	}
	
	
}

?>




<?

function changeBack($string){
			
		while(strpos($string,"&lt;")!==false)
			$string=str_replace("&lt;","<",$string);
			
		while(strpos($string,"&gt;")!==false)
			$string=str_replace("&gt;",">",$string);
			
		while(strpos($string,"&_l_t_;")!==false)
			$string=str_replace("&_l_t_;","&lt;",$string);
			
		while(strpos($string,"&_g_t_;")!==false)
			$string=str_replace("&_g_t_;","&gt;",$string);
			
		while(strpos($string,"d_o_u_b_l_e_q")!==false)
			$string=str_replace("d_o_u_b_l_e_q","\"",$string);
			
		while(strpos($string,"s_i_n_g_l_e_q")!==false)
			$string=str_replace("s_i_n_g_l_e_q","'",$string);
			
		return $string;
	
}


if ($_POST['qfrom']!="") $_GET['qfrom']=$_POST['qfrom'];
if ($_POST['qinterval']!="") $_GET['qinterval']=$_POST['qinterval'];

if ($_POST['change']=="order"){

	mysql_connect(changeBack($_POST['dbhost']),changeBack($_POST['dbuser']),changeBack($_POST['dbpassword']));
	mysql_select_db(changeBack($_POST['dbname']));
	
	$_GET['qorder']=changeBack($_POST['qorder']);
	$_GET['qdirection']=changeBack($_POST['qdirection']);
	
	if (($_POST['interval']!="")&&($_POST['interval']!=0))
	$rep= new Report(changeBack($_POST['query']),$_POST['interval']);
	else
	$rep= new Report(changeBack($_POST['query']));
	
	if ($_POST['tableclass']!="") $rep->setTableClassName(changeBack($_POST['tableclass']));
	if ($_POST['navTableClass']!="") $rep->setNavigationTableClassName(changeBack($_POST['navTableClass']));
	if ($_POST['navLinkClass']!="") $rep->setNavigationLinkClass(changeBack($_POST['navLinkClass']));
	if ($_POST['sortLinkClass']!="") $rep->setSortLinkClassName(changeBack($_POST['sortLinkClass']));
	
	if ($_POST['sortASCpic']!="") $rep->setSortASCPicture(changeBack($_POST['sortASCpic']));
	if ($_POST['sortDESCpic']!="") $rep->setSortDESCPicture(changeBack($_POST['sortDESCpic']));
	
	if ($_POST['leftText']!="") $rep->setLeftText(changeBack($_POST['leftText']));
	if ($_POST['leftJumpText']!="") $rep->setLeftJumpText(changeBack($_POST['leftJumpText']));
	if ($_POST['rightText']!="") $rep->setRightText(changeBack($_POST['rightText']));
	if ($_POST['rightJumpText']!="") $rep->setRightJumpText(changeBack($_POST['rightJumpText']));
	
	if ($_POST['leftImage']!="") $rep->setLeftImage(changeBack($_POST['leftImage']));
	if ($_POST['leftJumpImage']!="") $rep->setLeftJumpImage(changeBack($_POST['leftJumpImage']));
	if ($_POST['rightImage']!="") $rep->setRightImage(changeBack($_POST['rightImage']));
	if ($_POST['rightJumpImage']!="") $rep->setRightJumpImage(changeBack($_POST['rightJumpImage']));
	
	if (($_POST['variance']!=="")&&($_POST['variance']!=0)) $rep->enableNavigationalNumbers($_POST['variance']);
	
	$fields=$_POST['fields'];
	
	for ($r=0;$r<$fields;$r++){
		
		if ($_POST['typefields'.$r]=="normal")
			$rep->addDisplayField(changeBack($_POST['field'.$r]),changeBack($_POST['displayfields'.$r]),changeBack($_POST['sizefields'.$r]));
		else
			$rep->addSpecialDisplayField(changeBack($_POST['field'.$r]),changeBack($_POST['displayfields'.$r]),changeBack($_POST['fieldstext'.$r]),changeBack($_POST['sizefields'.$r]));
		
	}
	
	$rep->enableAjax(changeBack($_POST['dbhost']),changeBack($_POST['dbuser']),changeBack($_POST['dbpassword']),changeBack($_POST['dbname']));
	
	$rep->draw();
	
	echo "##24##next##";
	
	if (($_POST['interval']!="")&&($_POST['interval']!=0))
	$rep->getNavigationBar();
	else if ($_POST['qinterval']!="")
	$rep->getNavigationBar();
	else echo "##none##none##";
	
}

?>