<?
	// error_reporting (E_ALL);
	class BaseGenAgent
	{
		function BaseGenAgent()
		{}

		//
	 	// Process
		//
		
		function preprocess($filename,$ids,$title)
		{
			return "<?xml version='1.0' encoding='Windows-1251'?>\n<document file='$filename' ids='$ids' title='$title'>";			
		}

		function postprocess()
		{
			return "</document>";			
		}

		function process()
		{
			return '';
		}	

		function execute()
		{
			$res = array();
			$s = '';
			$filename = '';

			$s .= $this->preprocess($filename);
			$s .= $this->process();
			$s .= $this->postprocess();
		
			$res[0] = new GenPage();
			$res[0]->applyXML($s);
			
			return $res;
		}	
	
		function addPage($filename,$id,$title,$xml)
		{

			$s = $this->preprocess($filename,$id,$title);
			$s .= $xml; 
			$s .= $this->postprocess();
			
			// print $s;
                        				
		      	$d = new GenPage($filename);
			$d->applyXML($s);
			return $d;
		}

		//
		// Toolbox
		//

		function tHint($title, $message, $link, $mlink)
		{
			$s = '<hint>';
		
			if(strlen($title) > 0) $s .= '<title>'.$title.'</title>';
			$s .= '<message>'.$message.'</message>';
			if(strlen($link) > 0) $s .= "<link location='$link'>$mlink</link>";
			$s .= '</hint>';
			return $s;			

		}

		function tPager($total, $begin, $npp, $ppp, $URL)
		{

		        if(($total % $npp) > 0) $npages = floor($total / $npp) + 1;
	    		else $npages = floor($total / $npp);
			
    			$begin++;
			$cpage = ($begin - ($begin % $npp)) / $npp;   
	    		
    			$bp = $cpage - $ppp / 2 + 1;
    			$ep = $cpage + $ppp / 2;	
	    		if($bp < 1) { $ep = $ep + (1 - $bp); $bp = 1; }; 
		    	if($ep > $npages) { $bp = $bp - ($ep - $npages); $ep = $npages; };
	    		if($bp < 1 || $ep > $npages || $bp > $ep) { $bp = 1; $ep = $npages; }; 
    
			$s = "<pages total='$npages' records='$total'>";
	   		if($bp > 1) {
				$b = ($bp - 2) * $npp + 1;
				$e = ($bp - 1) * $npp;	
 	 	                $iurl = $URL."&amp;begin=".($b - 1);
				$s .= "<ppage begin='$b' end='$e' URL='$iurl'>&lt;</ppage>";
	    		};
    
	    		for($i = $bp; $i < ($ep + 1); $i++) {
				$b = ($i - 1) * $npp + 1;
				$e = $i * $npp;
 	 	                $iurl = $URL."&amp;begin=".($b - 1);
				if($begin >= $b && $begin <= $e) {
					$s .= "<cpage begin='$b' end='$e'>$i</cpage>";
				} else {
					$s .= "<tpage begin='$b' end='$e' URL='$iurl'>$i</tpage>";
				};
	    		};

	    		if($ep < $npages) {
				$b = $ep * $npp + 1;
				$e = ($ep + 1) * $npp;	
 	 	                $iurl = $URL."&amp;begin=".($b - 1);
				$s .= "<npage begin='$b' end='$e' URL='$iurl'>&gt;</npage>";
	    		};
			$s .= "</pages>";			

			return $s;
		}

		function tStatPager($total, $begin, $npp, $ppp, $URL_prefix, $URL_postfix, $fp)
		{

		        if(($total % $npp) > 0) $npages = floor($total / $npp) + 1;
	    		else $npages = floor($total / $npp);
			
    			$begin++;
			$cpage = ($begin - ($begin % $npp)) / $npp;   
	    		
    			$bp = $cpage - $ppp / 2 + 1;
    			$ep = $cpage + $ppp / 2;	
	    		if($bp < 1) { $ep = $ep + (1 - $bp); $bp = 1; }; 
		    	if($ep > $npages) { $bp = $bp - ($ep - $npages); $ep = $npages; };
	    		if($bp < 1 || $ep > $npages || $bp > $ep) { $bp = 1; $ep = $npages; }; 

			$s = "<pages total='$npages' records='$total'>";
	   		if($bp > 1) {
				$b = ($bp - 2) * $npp + 1;
				$e = ($bp - 1) * $npp;	
	 	                $iurl = $URL_prefix.($bp - 1).$URL_postfix;
				if(strlen($fp) > 1 && $bp == 1) $iurl = $fp;

				$s .= "<ppage begin='$b' end='$e' URL='$iurl'>&lt;</ppage>";
	    		};
    
	    		for($i = $bp; $i < ($ep + 1); $i++) {
				$b = ($i - 1) * $npp + 1;
				$e = $i * $npp;
	 	                $iurl = $URL_prefix.($i).$URL_postfix;
                           	if(strlen($fp) > 1 && $i == 1) $iurl = $fp;

				if($begin >= $b && $begin <= $e) {
					$s .= "<cpage begin='$b' end='$e'>$i</cpage>";
				} else {
					$s .= "<tpage begin='$b' end='$e' URL='$iurl'>$i</tpage>";
				};
	    		};

	    		if($ep < $npages) {
				$b = $ep * $npp + 1;
				$e = ($ep + 1) * $npp;	
	 	                $URL_prefix.($ep + 1).$URL_postfix;

				$s .= "<npage begin='$b' end='$e' URL='$iurl'>&gt;</npage>";
	    		};
			$s .= "</pages>";			

			return $s;
		}

		function tMenu($c)
		{
			global $cat_site_menu;
		
			$s = "<menu>";
			$s .= $this->_MenuLevel($cat_site_menu,$c,1);
			$s .= "</menu>";
			return $s;
		}

		function _MenuLevel($a,$c,$l)
		{
			$s = "";
			if(is_array($a)) {
				for($i = 0; $i < count($a); $i++) {
					$p = each($a);
					
					if(is_array($p[value])) {
						$s .= $this->_MenuLevel($p[value],$c,$l + 1);		
					} else {
		                        	if($p[key] == $c)
							$s .= "<smenuitem link='$p[key]' level='$l'>$p[value]</smenuitem>";
						else
							$s .= "<menuitem link='$p[key]' level='$l'>$p[value]</menuitem>";
					};	
				};
			};	
			return $s;
		}

		function tCode($z)
		{
			return "<item><![CDATA[ $z ]]></item>";
		}
	};

?>