<?php session_name('iAdmin'); session_start();
if (PHP_SAPI==='cli' || strstr(PHP_SAPI,'cgi')!==false) {
   $newdir=dirname($_SERVER['PHP_SELF']);
   if (is_dir($newdir)) {
      //echo "<!-- New dir: $newdir -->";
      chdir($newdir);
   }
   else {
      //echo "<!-- is not dir: $newdir -->";
   }
}
if (($_REQUEST['SESSION']??false) === 'clear') { session_unset(); }

require('ishop_ahe.inc.php');
/*include("../lib/ado/adodb.inc.php");
include("../lib/generic.inc.php");
include("../lib/visual.inc.php"); */
require_once("ishop_all.inc.php");
require_once("ishop.inc.php");
?><!DOCTYPE html>
<html><head>
<head class="no-js" lang="en">
<meta charset="utf-8" />
<meta http-equiv="x-ua-compatible" content="ie=edge, chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />

<script src="../script/jquery-4.0.0.min.js"></script>
<script src="../script/what-input.5.2.12.min.js"></script>

<!-- Compressed CSS -->
<link rel="stylesheet" href="../script/foundation.6.9.0/css/foundation.min.css">
<link rel="stylesheet" href="../script/foundation.6.9.0/css/foundation-icons.css">
<!-- link rel="stylesheet" href="adm.css" -->

<!-- Compressed JavaScript -->
<script src="../script/foundation.6.9.0/js/foundation.min.js"></script>


<!-- LINK REL="stylesheet" type="text/css" href="adm.css"  -->
<SCRIPT SRC="xadm.js.php"></SCRIPT>
<title>Modern iShopping cart Setup</title>
<?php
/*
  MORETYPE=0/1 --> txt/htm
*/
$sync_customer=array();
//--------------------------------------
function ABC_DropItems()
{
   global $x2;
   $db=&$x2->db;

	// HEADER
	$tarray=array(
	   array("ADMIN", 0, 'Main'),
	   array('', 0, 'ABC convert Items')
	   );
	$x2->make_header($tarray);
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST name="goForm">
<input type=hidden name="FUNCTION">
</form>
<?php

   $query="UPDATE ishop_item SET TRKEY='' ";
   if (!$db->Execute($query)) {
      echo "<h6>".$db->ErrorMsg()."</h6>\n";
   }
}
//--------------------------------------
function ABC_MakeItems()
{
   global $x2;
   $db=&$x2->db;

		// HEADER
	$tarray=array(
	   array("ADMIN", 0, 'Main'),
	   array('', 0, 'ABC convert Items')
	   );
	$x2->make_header($tarray);

   $query="SELECT CODE,NAME,AGENTID FROM ishop_item WHERE (TRKEY='' OR TRKEY IS NULL)";
   if (!($rs=$db->Execute($query))) {
      echo "<h6>".$db->ErrorMsg()."</h6>\n";
      return;
   }
   echo "<table>\n";
   $nrec=0;
   while ($row=$rs->FetchRow()) {
      $newstr=ToEnglish($row[0]." ".$row[1]);
      echo "<tr";
      if ($nrec++%2) echo " class=HILINE";
      echo ">\n".
         "<td>".myhtml($row[2])."</td>\n".
         "<td>".myhtml($row[0])."</td>\n".
         "<td>".myhtml($row[1])."</td>\n".
         "<td>".myhtml($newstr)."</td>\n".
         "</tr>\n";
      $query2="UPDATE ishop_item SET TRKEY=? WHERE AGENTID=? AND CODE=?";
      $data2=array($newstr, $row[2], $row[0]);
      if (!$db->Execute($query2,$data2))
         tr_echo( "<h6>".$db->ErrorMsg()."</h6>" );
   }
   $rs->Close();
   echo "</table>\n".
      "<small>End of file</small>\n";
}
//--------------------------------------
function ABC_DropMoMain(&$x2)
{
   $db=&$x2->db;

		// HEADER
	$tarray=array(
	   array("ADMIN", 0, 'Main'),
	   array('', 0, 'ABC drop Mo_Main')
	   );
	$x2->make_header($tarray);
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST name="goForm">
<input type=hidden name="FUNCTION">
</form>
<?php
   $query="UPDATE mo_main SET TRKEY=\"\"";
   if (!$db->Execute($query))
      echo( "<h6>".$db->ErrorMsg()."</h6>" );
   else
      echo "<h6>Done !</h6>";
}
//--------------------------------------
function ABC_MakeMoMain(&$x2)
{
   $db=&$x2->db;

   $t1=time();
 	$tmax=(int)ini_get('max_execution_time');

	// HEADER
	$tarray=array(
	   array("ADMIN", 0, 'Main'),
	   array('', 0, 'ABC make Mo_Main')
	   );
	$x2->make_header($tarray);
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST name="goForm">
<input type=hidden name="FUNCTION">
</form>
<?php

   $query="SELECT AGENTID,MOID,FAMILYNAME,FIRSTNAME,COMPANY FROM mo_main WHERE TRKEY=\"\" OR TRKEY IS NULL";
   $query2="UPDATE mo_main SET TRKEY=? WHERE AGENTID=? AND MOID=?";

   if (!($rs=$db->Execute($query))) {
      echo "<h6>".$db->ErrorMsg()."</h6>\n";
      return;
   }
   $nrec=0;
   while ($row=$rs->FetchRow()) {
      $t2=time();
      if (($t2-$t1)>($tmax-15)) {
         echo "<h6>Time elapsed!</h6>";
         break;
      }

      $newstr=ToEnglish($row[2]." ".$row[3]." ".$row[4]);
      $data2=array($newstr, $row[0], $row[1]);
      if (!$db->Execute($query2,$data2))
         tr_echo( "<h6>".$db->ErrorMsg()."</h6>" );
      else
         $nrec++;
   }
   $rs->Close();

   echo "<h6>Done for $nrec records!</h6>";
}
//--------------------------------------
function ABC_DropCustomers()
{
   global $x2;
   $db=&$x2->db;

		// HEADER
	$tarray=array(
	   array("ADMIN", 0, 'Main'),
	   array('', 0, 'ABC convert Customers')
	   );
	$x2->make_header($tarray);
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST name="goForm">
<input type=hidden name="FUNCTION">
</form>
<?php
   $query="UPDATE ishop_mbr SET TRKEY='' ";
   if (!$db->Execute($query)) {
      tr_echo( "<h6>".$db->ErrorMsg()."</h6>" );
   }
}
//--------------------------------------
function ABC_MakeCustomers()
{
   global $x2;
   $db=&$x2->db;

		// HEADER
	$tarray=array(
	   array("ADMIN", 0, 'Main'),
	   array('', 0, 'ABC convert Customers')
	   );
	$x2->make_header($tarray);
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST name="goForm">
<input type=hidden name="FUNCTION">
</form>
<?php

   $query="SELECT LOGIN,COMPANY,AGENTID FROM ishop_mbr WHERE (TRKEY='' OR TRKEY IS NULL)";
   if (!($rs=$db->Execute($query))) {
      echo "<h6>".$db->ErrorMsg()."</h6>\n";
      return;
   }
   echo "<table>\n";
   $nrec=0;
   while ($row=$rs->FetchRow()) {
      $newstr=ToEnglish($row[0]." ".$row[1]);
      echo "<tr";
      if ($nrec++%2) echo " class=HILINE";
      echo ">\n".
         "<td>".myhtml($row[2])."</td>\n".
         "<td>".myhtml($row[0])."</td>\n".
         "<td>".myhtml($row[1])."</td>\n".
         "<td>".myhtml($newstr)."</td>\n".
         "</tr>\n";
      $query2="UPDATE ishop_mbr SET TRKEY=? WHERE AGENTID=? AND LOGIN=?";
      $data2=array($newstr, $row[2], $row[0]);
      if (!$db->Execute($query2,$data2))
         tr_echo( "<h6>".$db->ErrorMsg()."</h6>" );
   }
   $rs->Close();
   echo "</table>\n".
      "<small>End of file</small>\n";
}
//--------------------------------------
function main_menu()
{
   global $x2;
   $db = &$x2->db;

   // HEADER
	$tarray=array(
		array('', 1, 'ctr_center')
		);
	$x2->make_header($tarray);
?>
<script language=javascript>
var d = new Date();
var tz = d.getTimezoneOffset();
d.setMonth( d.getMonth()+1 );
document.cookie='TIMEZONEOFFSET='+tz+'; expires='+d.toUTCString()+'; path=/';
</script>
<?php
	if (!isset($x2->Agp['DECIMALSEP']) || strlen($x2->Agp['DECIMALSEP'])==0) {
      $x2->Agp['DECIMALSEP']=',';

		$t1='agent_main';
		$prim=array('AGENTID', 'PNAME');
		$data=array('AGENTID'=>$x2->agentid, 'PNAME'=>'DECIMALSEP', 'PVALUE'=>$x2->Agp['DECIMALSEP']);
      if (!ReplaceEx($db,$t1,$data,$prim))
         echo "<h6>".$db->ErrorMsg()."</h6>";
   }

   if ($x2->reselid==0)
      return main_menu_agent($x2);
   else
      return main_menu_resel($x2);
}
//--------------------------------------
function main_menu_resel(&$x2)
{
 	$db=&$x2->db;

 	$mok=array();
   $query="SELECT PNAME FROM reselprefex
      WHERE AGENTID=? AND RESELID=? AND PGROUP='main_menu' AND PVALUE='1' ";
   $data=array($x2->agentid, $x2->reselid);
   if (($rs=$db->Execute($query,$data))==false) {
      echo "<h6>".$db->ErrorMsg()."</h6>\n";
		return;
   }
   while ($row=$rs->FetchRow()) {
      $mok[]=$row[0];
   }
   $rs->Close();

   // More selections
   $taxis_menu = 0;
   $query = "SELECT PVALUE FROM reselpref WHERE RESELID=? AND PNAME='TAXIS_MENU' ";
   $data = array($x2->reselid);
   if (($rs=$db->Execute($query,$data))==false) {
      echo "<h6>".$db->ErrorMsg()."</h6>\n";
		return;
   }
   if ($row=$rs->FetchRow()) {
      $taxis_menu = (int)$row[0];
   }
   $rs->Close();

   include('ishop_m1.inc.php');
   // Add license handler
   $x2->add_license_handler($mmenu);
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST  name="goForm">
<input type=HIDDEN name="FUNCTION">
<input type=HIDDEN name="RECORDID">
<table border=0 cellspacing=4 cellpadding=5>
<?php
   // First pass
   echo "<tr>";
   foreach ($mmenu as $key=>$val) {
      $n=count($val);
      $fi=false;
      for ($i=0;$i<$n;$i++) {
         $v2=$val[$i];
         if (!is_array($v2))
            continue;

         if (in_array($v2[1], $mok)) {
            $fi=true;
            break;
         }
      }
      if ($fi) {
         echo "<td class=bgLabel><b>".$x2->xxlabel($key)."</b></td>\n";
      }
   }
   echo "</tr>\n";

   // Second pass
   echo "<tr class=bgButton valign=top>";
   foreach ($mmenu as $key=>$val) {
      $n=count($val);
      $fi=false;
      for ($i=0;$i<$n;$i++) {
         $v2=$val[$i];
         if (!is_array($v2)) {
            if ($fi) {
               echo "<br>&nbsp;";
            }
            continue;
         }

         if (in_array($v2[1], $mok)) {
            if (!$fi) {
               echo "<td style=\"font-weight:bold;\">";
               $fi=true;
            }
            echo "<li><a href=\"".$v2[0]."\">";
            $arw=explode('/',$v2[1]);
            if (count($arw)==2) {
               echo myhtml($arw[0])." ".$x2->xxlabel($arw[1]);
            }
            else {
               echo $x2->xxlabel($v2[1]);
            }
            echo "</a></li>";
         }
      }
      if ($fi) {
         echo "</td>\n";
      }
   }
   echo "</tr>";

   if ($taxis_menu) {
?>
      <tr>
         <td class=bgLabel>Επιλογές Taxis</td>
      </tr>
      <tr>
         <td class=bgButton style="font-weight:bold;">
            <a href="javascript:goFX('ishop_taxi.php', 97)">AESKEY list</a> |
            <a href="javascript:goFX('ishop_taxi.php', 60)">Esend Z</a> |
            <!-- a href="javascript:goFX('ishop_taxi.php', 70)">myDATA</a -->
            <a href="javascript:show_popup('poptaxis')">myDATA</a>
         </td>
      </tr>
<?php
   }

   echo "</table></form>";

   if ($taxis_menu) {
      echo '<script>function winRet(x){ goFX(\'ishop_taxi.php\', x); }</script>';
      $selx=array(
         array(104, '/img/right1.gif', 'Αποστολές (ίδια έκδοση)' ),
         array(105, '/img/left1.gif', 'Παραλαβές (τρίτων)' ),
      );
      draw_popup($selx,false,'poptaxis');
   }
}
//--------------------------------------
function make_tr_control($ids, $vis, $label)
{
   echo "<tr><td class=bgLabel><b><a href=\"javascript:void(0)\" onclick=\"showHideArea2(this,'{$ids}')\"
      class=\"LocalHref\">[".($vis==0?'+':'-')."]</a></b>$label</td></tr>";
}
//--------------------------------------
function open_tr_control($ids, $label)
{
   global $x2;

   $visible=(int)@$x2->Agp["CPANEL_$ids"];

   echo "<div class=\"bgLabel mtab\"><b><a href=\"javascript:void(0)\" onclick=\"showHideArea2(this,'{$ids}')\"
      class=\"LocalHref\">[".($visible==0?'+':'-')."]</a></b>$label</div>
      <div id=$ids class=bgButton style=\"padding:4px;";
   if ( $visible==0 ) {
      echo " display:none;";
   }
   echo "\">";
}
//--------------------------------------
function main_menu_agent(&$x2)
{
 	$db=&$x2->db;

 	// BODY
 	if (!array_key_exists ('NOSUBDIRS', $x2->Agp)) {
		$query="INSERT INTO agent_main (AGENTID,PNAME,PVALUE) VALUES(?, 'NOSUBDIRS', ?)";
		$data=array($x2->agentid, '1');
		if (!$db->Execute($query,$data))
			echo "<h6>".$db->ErrorMsg()."</h6>\n";
		else
			$x2->Agp['NOSUBDIRS']="1";
	}
 	else if (@$x2->Agp['NOSUBDIRS']!=1) {
 		echo "<h5>Please reset you FilePos to 'all in one'</h5>\n";
 	}
?>
<script language="JavaScript">
function setLangid()
{
	var f1=document.goForm;
	var o1=document.getElementById('LANGID');
	i=o1.selectedIndex;
	if (i<0) i=0;
	f1.RECORDID.value=i; //o1.options[i].value;
	f1.FUNCTION.value=101;
	f1.submit();
}
function winRet(x)
{
	switch (x)
	{
	case 1: // pricelist groups
	   goFX('ishop_wre.php',160);
      break;
   case 2: // time table
      goFX('ishop_pritime.php',150);
      break;
   case 3: // min. consumption
      goFX('ishop_mbr.php',140);
      break;
   case 4: // horizontal analysis
	   goFX('ishop_wre.php',230);
      break;
   }
}
</script>

<style>
.mtab { width:150px; padding:4px; border: 1px solid #B4D4D3; }
</style>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST name="goForm">
<input type=HIDDEN name="FUNCTION">
<input type=HIDDEN name="RECORDID">
<?php
   if ($x2->opetype==2) { // E-Invoice
?>
<table>
<tr><td class=bgLabel>E-Invoice</td></tr>
<tr class=bgButton>
	<td>
		<b>
		<a href="javascript:goFX('ishop_eafdss.php',56)"><?php $x2->xprint('EAFDSS'); ?></a> |
   	<a href="javascript:goFX('ishop_einv.php',54)"><?php $x2->xprint('sign_avail'); ?></a> |
   	<a href="javascript:goFX('ishop_einv.php',45)"><?php $x2->xprint('statistics_day'); ?></a> |
      <a href="javascript:goFX('ishop_smen.php',150)"><?php $x2->xprint('salesmen'); ?></a> |
   	<a href="javascript:goTo(43);"><?php $x2->xprint('c_passwd'); ?></a>
   </b></td>
</tr>
</table>
<?php
   }
   else if ($x2->opetype==7) { // TaxCenter
?>
<table>
<tr><td class=bgLabel>E-Invoice</td></tr>
<tr class=bgButton>
	<td><b>
      <a href="javascript:goTo(20);"><?php $x2->xprint('gen_info'); ?></a> |
      <a href="javascript:goFX('ishop_smen.php',150)"><?php $x2->xprint('salesmen'); ?></a> ||
      <a href="javascript:goFX('ishop_taxi.php',1)">TaxCenter (view)</a> |
      <a href="javascript:goFX('ishop_taxi.php',5)">Στοιχεία ΕΑΦΔΣΣ</a> |
      <a href="javascript:goFX('ishop_taxi.php',60)">Esend Z</a> |
      <!-- a href="javascript:goFX('ishop_taxi.php',70)">myDATA</a --> |
      <a href="javascript:show_popup('poptaxis')">myDATA</a> |
      <a href="javascript:goFX('ishop_taxi.php',20)">Στοιχεία S.TXT</a> |
      <a href="javascript:goFX('ishop_taxi.php',45)">Remote params</a> |
      <a href="javascript:goFX('ishop_taxi.php',28)">Σύγκριση</a> ||
      <a href="javascript:goFX('ishop_taxi.php',100)">Setup</a> |
      <a href="javascript:goFX('ishop_wre.php',260)"><?php $x2->xprint('eafdss_grp'); ?></a> |
      <a href="javascript:goTo(43);"><?php $x2->xprint('c_passwd'); ?></a>
   </b></td>
</tr>
</table>
<?php
   }
   else { // Not 'E-Invoice'
      open_tr_control('m_files', $x2->xxlabel('Files') );
?>
  		<b>
  		<?php if ($x2->opetype==5) { /* Itineraries */ ?>
  		  <a href="javascript:goTo(20);"><?php $x2->xprint('gen_info'); ?></a> |
  		  <a href="javascript:goFX('ishop_ascii.php',1);"><?php $x2->xprint('use_ascii'); ?></a> |
  		  <a href="javascript:goFX('ishop_gmrec.php',40);"><?php $x2->xprint('GPS_modems'); ?></a>
  		<?php } else if ($x2->opetype==6) { /* PayNet */ ?>
  		  <a href="javascript:goTo(20);"><?php $x2->xprint('gen_info'); ?></a> |
  		  <a href="javascript:goFX('ishop_ascii.php',1);"><?php $x2->xprint('use_ascii'); ?></a> |
  		  <a href="javascript:goFX('ishop_paynet.php',1);">PayNet</a> |
        <a href="javascript:goFX('ishop_payapo.php',1);">PayApo</a>
  		<?php } else { ?>
        <a href="javascript:goFX('ishop_ctg.php',140);"><?php $x2->xprint('pg_detail'); ?></a> |
  		  <a href="javascript:goFX('ishop_item.php',150);"><?php $x2->xprint('item_detail'); ?></a> |
  		  <a href="javascript:goFX('ishop_stock.php',1)"><?php $x2->xprint('stocks'); ?></a> |
        <a href="javascript:goTo(20);"><?php $x2->xprint('gen_info'); ?></a> |
  		  <a href="javascript:goFX('ishop_ascii.php',1);"><?php $x2->xprint('use_ascii'); ?></a> |
  		  <a href="javascript:goFX('ishop_prts.php',150)"><?php $x2->xprint('printpos'); ?></a> |
  		  <a href="javascript:goFX('ishop_tel.php',1)"><?php $x2->xprint('tel_calls'); ?></a> |
  		  <a href="javascript:goTo(43);"><?php $x2->xprint('c_passwd'); ?></a><br>

  		  <a href="javascript:goFX('ishop_item.php',143);"><?php $x2->xprint('subcodes'); ?></a> |
  		  <a href="javascript:goFX('ishop_unit.php',150);"><?php $x2->xprint('measure_units'); ?></a> |
  		  <a href="javascript:goFX('ishop_xtra.php',150);">Extras</a> |
  		  <a href="javascript:goFX('ishop_recipe.php',150);"><?php $x2->xprint('recipes'); ?></a> |
  		  <a href="javascript:goFX('ishop_dplan.php',1)"><?php $x2->xprint('level_plan'); ?></a> |
  		  <a href="javascript:goFX('ishop_filo.php',1);"><?php $x2->xprint('rooms'); ?></a> |
  		  <a href="javascript:goFX('ishop_filo.php',20);"><?php $x2->xprint('redeems'); ?></a> |
  		  <a href="javascript:goFX('ishop_area.php',1);"><?php $x2->xprint('srv_areas'); ?></a> |
  		  <a href="javascript:goFX('ishop_gmrec.php',40);"><?php $x2->xprint('GPS_modems'); ?></a> |
  		  <a href="javascript:goFX('ishop_mappt.php',1)"><?php $x2->xprint('routes'); ?></a>
  		<?php } ?>
      </b>
		</div>
<?php if ($x2->opetype!=5 && $x2->opetype!=6) { // Not 'Itineraries' and not 'PayNet'
?>
<img src="/img/clear.gif" height=4>
<?php
   open_tr_control('m_wk_center', $x2->xxlabel('wk_center') );
?>
   <b>
   <?php $x2->xprint('orders') ?>:
   <a href="javascript:goFX('ishop_ord.php?ORDTYPE=0',1)"><?php $x2->xprint('sales'); ?></a> |
	<a href="javascript:goFX('ishop_ord.php?ORDTYPE=1',1)"><?php $x2->xprint('purchases'); ?></a> |
   <a href="javascript:goFX('ishop_ord.php?ORDTYPE=2',1)"><?php $x2->xprint('inhousemv'); ?></a> |
   <a href="javascript:goFX('ishop_elipsi.php',1)"><?php $x2->xprint('shortages'); ?></a> |
   <a href="javascript:goFX('ishop_ord.php',15)">WEB alert</a><br>

	<a href="javascript:goFX('apo_desktop.php',2)"><?php $x2->xprint('inventory'); ?></a> ||
	<a href="javascript:goTo(45)"><?php $x2->xprint('synchronization'); ?></a> ||
	<a href="javascript:goFX('ishop_proj.php',1)"><?php $x2->xprint('project'); ?></a> ||
	<a href="javascript:goFX('ishop_usage.php',1)"><?php $x2->xprint('f_usage'); ?></a> |
	<a href="javascript:goFX('ishop_stat.php','0');"><?php $x2->xprint('stat'); ?></a> |
	<a href="javascript:goFX('ishop_stacart.php','1');"><?php $x2->xprint('stat'); ?> Cart</a> |
	<a href="javascript:goTo(300)"><?php $x2->xprint('trans_units'); ?></a> ||
	<a href="javascript:goFX('ishop_edu.php',1)">EDU</a> ||
   <a href="javascript:goFX('ishop_taxi.php',1)">TaxCenter</a> (view) |
   <a href="javascript:goFX('ishop_taxi.php',100)">Setup</a> |
   <a href="javascript:goFX('ishop_wre.php',260)"><?php $x2->xprint('eafdss_grp'); ?></a>
   <br>

	<a href="javascript:goFX('ishop_dplan.php',40)"><?php $x2->xprint('events'); ?></a> |
	<a href="javascript:goFX('ishop_dplan.php',10)"><?php $x2->xprint('reserv_prgm'); ?></a> |
	<a href="javascript:goFX('ishop_dplan.php',25)"><?php $x2->xprint('reserv_diary'); ?></a> |
	<!-- <a href="javascript:goDeleteFX('ishop_dplan.php',99)">Deep clean</a> | -->
	<a href="javascript:goFX('ishop_dplan.php',50)">FRONT</a>
   </b>
   </div>

<img src="/img/clear.gif" height=4>

<?php
   open_tr_control('m_b2b', 'B2B');
?>
	<b>
   	<a href="javascript:goFX('ishop_mbr.php',50)"><?php $x2->xprint('mbr_b2b'); ?></a> |
   	<a href="javascript:goFX('ishop_mbr.php',250)"><?php $x2->xprint('suppliers'); ?></a> |
   	<a href="javascript:goFX('ishop_wre.php',150)"><?php $x2->xprint('warehouse'); ?></a> |
   	<a href="javascript:show_popup(document.getElementById('popmenu1'))"><?php $x2->xprint('pricelists'); ?></a> |
      <a href="javascript:goFX('ishop_bonus.php',1)">Bonus</a><br>

   	<a href="javascript:goFX('ishop_wre.php',170)"><?php $x2->xprint('dlv_methods'); ?></a> |
   	<a href="javascript:goFX('ishop_wre.php',180)"><?php $x2->xprint('pay_methods'); ?></a> |
   	<a href="javascript:goFX('ishop_isu.php',150)"><?php $x2->xprint('issues'); ?></a> |
   	<a href="javascript:goFX('ishop_que.php',150)"><?php $x2->xprint('questions'); ?></a> |
   	<a href="javascript:goFX('ishop_note.php',150)"><?php $x2->xprint('notes'); ?></a> |
   	<nobr><a href="javascript:goFX('ishop_daily.php',1)"><?php $x2->xprint('daily_trans'); ?></a></nobr><br>

   	<a href="javascript:goFX('ishop_contact.php',150)"><?php $x2->xprint('contacts'); ?></a> |
   	<a href="javascript:goFX('ishop_contact.php',161)">SMS <?php $x2->xprint('messages'); ?></a> |
   	<a href="javascript:goFX('ishop_contact.php',300)">Email <?php $x2->xprint('messages'); ?></a> ||
   	<a href="javascript:goFX('ishop_logis.php',10)"><?php $x2->xprint('GL'); ?></a>
   </b>
   </div>
<?php } ?>

<img src="/img/clear.gif" height=4>

<?php
   open_tr_control('m_salesmen', $x2->xxlabel('salesmen') );
?>
		<b>
		<?php if ($x2->opetype==5 || $x2->opetype==6) { ?>
		   <a href="javascript:goFX('ishop_smen.php',150)"><?php $x2->xprint('salesmen'); ?></a> |
		   <a href="javascript:goTo(22)">Sessions</a> |
         <a href="javascript:goTo(19)">Versions</a> |
		<?php } else { ?>
      	<a href="javascript:goFX('ishop_smen.php',150)"><?php $x2->xprint('salesmen'); ?></a> |
      	<a href="javascript:goFX('ishop_plan.php',150)"><?php $x2->xprint('plan'); ?></a> |
   	   <a href="javascript:goFX('ishop_plan.php',201)"><?php $x2->xprint('visits'); ?></a> |
   	   <a href="javascript:goFX('ishop_expe.php',150)"><?php $x2->xprint('expenses'); ?></a> |
   	   <a href="javascript:goFX('ishop_msg.php',3)"><?php $x2->xprint('messages'); ?></a> |
   	   <a href="javascript:goFX('ishop_offer.php',1)"><?php $x2->xprint('offers'); ?></a> |
   	   <a href="javascript:goTo(22)">Sessions</a> |
         <a href="javascript:goTo(19)">Versions</a> |
   	   <a href="javascript:alert('<?php $x2->xprint('_msg1'); ?>')">Desktop Login</a><br>
   	   <a href="javascript:goFX('ishop_prts.php',160)"><?php $x2->xprint('pda_prouts'); ?></a> |
   	   <a href="javascript:goFX('ishop_prts.php',170)"><?php $x2->xprint('srv_prouts'); ?></a>
      <?php } ?>
      </b>
   </div>
<?php
   }
?>

&nbsp;
<table cellspacing=0 cellpadding=0 border=0>
<tr valign=top>
	<td class=bgLabel style="padding:5px;">Language selection</td>
	<td rowspan=2 bgcolor=black><img src="/img/clear.gif" width=1></td>
	<td class=bgLabel style="padding:5px;"><?php $x2->xprint('goto'); ?></td>
</tr>
<tr>
	<td class=bgButton align=center><?php
		$langs=array();
		$query="SELECT LANGID, LANGDES FROM ishop_langui ORDER BY LANGID";
		if (($rs=$db->Execute($query))===false) {
			echo "<h6>".$db->ErrorMsg()."</h6>\n";
		}
		else {
			while ($o1=$rs->FetchNextObject()) {
				$langs[$o1->LANGID]=$o1->LANGDES;
			}
			$rs->Close();

			$lang_must_exist=array(0=>'English', 1=>'Greek', 2=>'German', 3=>'Spanish', '4'=>'French', 5=>'Italian');

			foreach ($lang_must_exist as $k1=>$la1) {
            if (array_key_exists($k1,$langs))
               continue;

            $langs[$k1]=$la1;
            $query="INSERT INTO ishop_langui ( LANGID, LANGDES ) VALUES(?,?)";
				$data=array($k1,$la1);
				if (!$db->Execute($query,$data))
					echo "<h6>".$db->ErrorMsg()."</h6>\n";
         }

			echo "<select id=\"LANGID\">\n";
			prAssocSelect($langs, $x2->lang);
			echo "</select>".
				"<button type=button onclick=\"setLangid()\"><img src=\"to_right.gif\"></button>";
		}
	?></td>
	<td class=bgButton style="padding:5px;"><b>
     <script language=javascript>
      function my_ishop(lpath)
      {
         var url='http://'+location.host+'/cart/index.php?LPATH='+lpath;
         window.open(url,'ishop');
      }
     </script><?php
         if ($x2->opetype!=2 && $x2->opetype!=5 && $x2->opetype!=7) {
            echo "<a href=\"javascript:my_ishop('".$x2->lpath."')\">".
	 	         $x2->xxlabel('my_ishop')."</a> |
	 	         <a href=\"javascript:newWinPDA()\">User PDA</a> |\n";
	 	   }
	 	?>
   	<a href="http://www.incahellas.com" target="inca">INCA Hellas</a>
	  </b></td>
</tr>
</table>
</form>
<!-- img src="progress.jpg" width=1 height=1 -->
<?php
if ($x2->opetype==7) {
      $selx=array(
         array(104, '/img/right1.gif', 'Αποστολές (ίδια έκδοση)' ),
         array(105, '/img/left1.gif', 'Παραλαβές (τρίτων)' ),
      );
      draw_popup($selx,false,'poptaxis');
   }
?>
<script>
function winRet(x){ goFX('ishop_taxi.php', x); }
function save_cpanel_pref2(ids,vis)
{
   var url='ishop_jsonfu.php?FUNCTION=4&ID='+ids+'&VIS='+vis;
   $.get(url);
}
function showHideArea2(oref,ids)
{
   $('#'+ids).toggle();
   var vis=$('#'+ids).css('display')=='none'?0:1;
   $(oref).text( $('#'+ids).css('display')=='none'?'[+]':'[-]' );
   save_cpanel_pref2(ids,vis);
}
</script>
<?php
   $selx1=array(
      array(1, '/img/go_next.gif', $x2->xxlabel('groups') ),
      array(4, '/img/go_next.gif', $x2->xxlabel('horz_analysis') ),
		array(2, '/img/go_next.gif', $x2->xxlabel('schedule') ),
		array(3, '/img/go_next.gif', $x2->xxlabel('min_consumpt') )
   );
	draw_popup($selx1,false,'popmenu1');
}
//--------------------------------------
function su_update_pass()
{
 	global $x2;
 	$db=&$x2->db;

 	if (strcmp($_REQUEST['PASS1'],$_REQUEST['PASS2'])!=0) {
		echo "<h6>Password mismatch error. Please try again!</h6>\n";
		return false;
	}

   $query="UPDATE agents SET PASSWD=? WHERE AGENTID=0";
   $data=array($_REQUEST['PASS1']);
 	if (!$db->Execute($query,$data)) {
 		echo "<h6>".$db->ErrorMsg()."</h6>\n";
 		return false;
 	}
   else {
      return true;
   }
}
//--------------------------------------
function update_pass()
{
 	global $x2;
   $db=&$x2->db;

 	if (strcmp($_REQUEST['PASS1'],$_REQUEST['PASS2'])!=0) {
		echo "<h6>Password mismatch error. Please try again!</h6>\n";
		return;
	}

   if ((int)$x2->reselid==0) {
      // Agent
    	$query="UPDATE agents SET PASSWD=? WHERE AGENTID=?";
 	   $data=array($_REQUEST['PASS1'], $x2->agentid);
 	}
   else {
      // Reseller
      $query="UPDATE reseller SET PASSWD=? WHERE RESELID=?";
      $data=array($_REQUEST['PASS1'], $x2->reselid);
   }
 	if (!$db->Execute($query,$data))
 		echo "<h6>".$db->ErrorMsg()."</h6>\n";
}
//--------------------------------------
function su_system_logs()
{
   global $x2;

 	// HEADER
  	$tarray=array(
		array('ADMIN', 0, 'Main'),
		array('', 0, 'System logs')
		);
	$x2->make_header($tarray);
?>
<b>
<ul>
<li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?FUNCTION=551&LOGFILE=/var/log/phplog">Php log</a>
<li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?FUNCTION=551&LOGFILE=/var/log/xferlog">Xferlog</a>
<li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?FUNCTION=551&LOGFILE=/var/log/httpd/access_log">Httpd/access_log</a>
<li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?FUNCTION=551&LOGFILE=/var/log/httpd/error_log">Httpd/error_log</a>
</ul>
</b>
<?php
}
//--------------------------------------
function su_php_log()
{
   global $x2;

   if (isset($_REQUEST['LOGFILE']))
	   $_SESSION['LOGFILE']=$_REQUEST['LOGFILE'];

	$logfile=$_SESSION['LOGFILE'];
	$ar1=explode('/', $logfile);
	$fname=$ar1[count($ar1)-1];


   // HEADER
  	$tarray=array(
		array('ADMIN', 0, 'Main'),
		array('javascript:goTo(550)', 0, 'System logs'),
		array('javascript:goTo(551)', 0, "$fname(r)")
		);
	$x2->make_header($tarray);

	// BODY
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST name="goForm">
<input type=hidden name="FUNCTION" value="551">
<input type=HIDDEN name="RSPAGE">
<?php
   $pg=getPage();
	$pgsz=getPageSize();

   $exclfilter=array();
   $ar1=explode(',', strtr(@$_REQUEST['EXCLFILTER'],';',','));
   foreach ($ar1 as $s1) {
      if (strlen($s1)>0)
         $exclfilter[]=$s1;
   }
   $nex=count($exclfilter);

   if (($fp=fopen($logfile,'r'))===false) {
      echo "Failed to open $logfile\n";
      return;
   }
   echo "<table>
      <tr class=\"bgHeader\">
      <td>Nr</td>
      <td>Data</td>
      </tr>\n";

   $lines=array();
   while (!feof($fp)) {
      $x=ftell($fp);
      $buf=fgets($fp,1024);
      $fi=false;
      for ($j=0;$j<$nex;$j++) {
         if (strpos($buf,$exclfilter[$j])!==false) {
            $fi=true;
            break;
         }
      }
      if (!$fi) // No match for a filter was fount
         $lines[]=$x;
   }
   $nLines=count($lines);
   $epage=ceil( (float)$nLines / (float)$pgsz );

   // Correct $pg
   if ($pg>$epage)
      $pg=$epage;

   $t1=($pg-1)*$pgsz;
   if ($t1 >= $nLines && $nLines>0)
      $t1=$nLines-1;

   $nrec=0;
   while ($nrec<$pgsz) {
      if ($t1>=$nLines)
         break;

      fseek($fp, $lines[$t1++] );
      if (($buf=fgets($fp,1024))===false)
         break;

      echo "<tr";
      if (($nrec++)%2) echo " class=\"HILINE\"";
      echo ">
         <td>".$t1."</td>
         <td>".myhtml($buf)."</td>
         </tr>\n";
   }
   fclose($fp);

   echo "<div id=tbar2>";
   print_page_navEx($pg,$epage,551);
	echo "Exclude filter (comma separated list):<input type=TEXT name=\"EXCLFILTER\"
      value=\"".myhtml(@$_REQUEST['EXCLFILTER'])."\"><button type=button
      onclick=\"document.goForm.submit()\"><img src=\"to_right.gif\"></button></div>\n".
		"</form>\n";
}
//--------------------------------------
function su_request_new_pass()
{
 	global $x2;
 	$db=&$x2->db;

   // HEADER
  	$tarray=array(
		array('ADMIN', 0, 'Main')
		);
	$x2->make_header($tarray);

 	// BODY
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="goForm">
<input type=hidden name="FUNCTION" value="541">
<table>
<tr>
	 <td class=bgLabel>New password</td>
	 <td class=bgInput><input type=PASSWORD name="PASS1" MAXLENGTH=8></td>
</tr>
<tr>
	 <td class=bgLabel>Verify</td>
	 <td class=bgInput><input type=PASSWORD name="PASS2" MAXLENGTH=8></td>
</tr>
<tr>
	 <td colspan=2 class=bgButton align=right><input type=SUBMIT name="ok" value="Update">
</tr>
</table>
</form>
<?php
}
//--------------------------------------
function request_new_pass()
{
 	global $x2;
   $db=&$x2->db;

 	// HEADER
  	$tarray=array(
		array("CENTER", 1, 'ctr_center'),
		array('', 1, 'c_passwd')
		);
	$x2->make_header($tarray);
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="goForm">
<input type=hidden name="FUNCTION" value="44">
<table>
<tr>
	 <td class=bgLabel>New password</td>
	 <td class=bgInput><input type=PASSWORD name="PASS1" MAXLENGTH=8></td>
</tr>
<tr>
	 <td class=bgLabel>Verify</td>
	 <td class=bgInput><input type=PASSWORD name="PASS2" MAXLENGTH=8></td>
</tr>
<tr>
	 <td colspan=2 class=bgButton align=right>
      <input type=SUBMIT name="ok" value="Update">
	   <input type=BUTTON value="Cancel" onclick="goFX('ishop_m2.php',100)">
	 </td>
</tr>
</table>
</form>

<?php
}
//--------------------------------------
function update_generic()
{
 	global $x2;
   $db=&$x2->db;

	$vars=array('TITLE', 'HREF', 'EMAIL', 'CURRENCY', 'CURRE2ND', 'CURR2REL',
		'CURR2VAL', 'DECIMAL', 'DECI2ND', 'THOUSANDSEP', 'DECIMALSEP',
		'NOIMAGE', 'TIMEZONE', 'EAFDSSHOST', 'EAFDSSID',
      'EAFDSSTIMEOUT', 'SSLAVAIL', 'SSLPORT', 'EMAILFROM', 'SMTPSERVER'  );

	$v2=$vars;
	array_walk($v2,'squote');
	$query="DELETE FROM agent_main WHERE AGENTID=? AND PNAME IN (".implode(',',$v2).")";
	$data=array($x2->agentid);
	if (!$db->Execute($query,$data)) {
		echo "<h6>".$db->ErrorMsg()."</h6>\n";
		return;
	}

	// multiple values insertion !
	$data=array();
	$query="INSERT INTO agent_main (AGENTID,PNAME,PVALUE) VALUES (?,?,?)";
	$n=count($vars);
   /*
	if (!isset($_REQUEST['RQMOREINFO'])) {
      $_REQUEST['RQMOREINFO']="0";
   }
   */
	for ($i=0;$i<$n;$i++) {
		if (!isset($_REQUEST[ $vars[$i] ]))
			continue;

      $data=array( $x2->agentid, $vars[$i], $_REQUEST[ $vars[$i] ] );
      if (!$db->Execute($query,$data)) {
		   echo "<h6>".$db->ErrorMsg()."</h6>\n";
		   return;
	   }
	}
}
//--------------------------------------
function request_sync()
{
   global $x2, $sync_customer;
   $db=&$x2->db;

   $sync_tidepth_type=array(
      $x2->xlabel('days'),
      $x2->xlabel('months')  );

   // HEADER
	$tarray=array(
		array("javascript:goTo(100)", 1, 'ctr_center'),
		array('', 1, 'synchronization')
		);
	$x2->make_header($tarray);

   // BODY
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="goForm">
<input type=hidden name="FUNCTION" value="46">
<table border=0>

<tr class=bgButton>
 <td colspan=2><b><?php $x2->xprint('sync_glst'); ?></b></td>
</tr>

<tr>
 <td colspan=2>Καθορίζει ποιά δεδομένα θα μεταφερθούν στον επόμενο
 	συγχρονισμό με το PDA (batch).</td>
</tr>


<tr>
 <td class=bgLabel><?php $x2->xprint('customers'); ?></td>
 <td class=bgInput><select name="SYNC_CUSTOMER"><?php
     prAssocSelect($sync_customer, (string)@$x2->Agp['SYNC_CUSTOMER'] );
     ?></select></td>
</tr>

<tr>
 <td class=bgLabel><?php $x2->xprint('time_depth'); ?></td>
 <td class=bgInput><select name="SYNC_TIDEPTH_NR"><?php
         prSelect(1, 120, (int)@$x2->Agp['SYNC_TIDEPTH_NR'] );
      ?></select>
      <select name="SYNC_TIDEPTH_TYPE"><?php
         prAssocSelect($sync_tidepth_type, @$x2->Agp['SYNC_TIDEPTH_TYPE'] );
      ?></select></td>
</tr>

<tr>
  <td></td>
  <td class=bgButton><input type=submit name="ok"
 	  value="<?php $x2->xprint('upd'); ?>">
     <input type=submit name="TESTONLY"
 	  value="<?php $x2->xprint('test'); ?>"></td>
</tr>

</table>
</form>

<?php
}
//--------------------------------------
function test_sync(&$db)
{
   global $x2, $sync_customer;

	$now=getdate();
   $mon=(int)$now['mon'];
   $mday=(int)$now['mday'];

   $dt=1;
   if (isset($_REQUEST['SYNC_TIDEPTH_NR']))
      $dt=(int)$_REQUEST['SYNC_TIDEPTH_NR'];

   $stt=0;
   if (isset($_REQUEST['SYNC_TIDEPTH_TYPE']))
      $stt=(int)$_REQUEST['SYNC_TIDEPTH_TYPE'];
   if ($stt==0)
      $mday-=$dt;
   else
      $mon-=$dt;

   $ti1=mktime( 0,0,0, $mon, $mday, $now['year'] );
   $fromday=date('Y-m-d',$ti1);

   // HEADER
	$tarray=array(
		array("javascript:goTo(100)", 1, 'ctr_center'),
		array('javascript:goTo(45)', 1, 'synchronization')
		);
	$x2->make_header($tarray);
?>
<!-- BODY -->
<table border=0>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="goForm">
<input type=hidden name="FUNCTION" value="45">
</form>

<tr>
   <td class=bgLabel><?php $x2->xprint('customers'); ?></td>
   <td><kbd><?php echo $sync_customer[ $_REQUEST['SYNC_CUSTOMER'] ]; ?></kbd></td>
</tr>

<tr>
   <td class=bgLabel><?php $x2->xprint('from_date'); ?></td>
   <td><kbd><?php echo date('d-m-Y',$ti1); ?></kbd></td>
</tr>

</table>
<table>
<tr class=bgHeader>
   <td>Κατηγορία</td>
   <td>Πωλητής</td>
   <td>Count</td>
</tr>
<?php
   switch ((int)$_REQUEST['SYNC_CUSTOMER'])
   {
      case 0: // ολοι ανεξαιρέτως
         $query="SELECT 'ALL', COUNT(*) F1 FROM ishop_g11 WHERE AGENTID=? AND g11cat=1 AND g11dat>=?";
         $data=array($x2->agentid, $fromday );
         break;
      case 1: // ολοι του πωλητή
   	   $query="SELECT RESELID, COUNT(*) F1 FROM ishop_g11 ".
            " LEFT JOIN ishop_mbr ON (ishop_mbr.AGENTID=ishop_g11.AGENTID AND LOGIN=g11acc) ".
            " WHERE ishop_g11.AGENTID=? AND g11cat=1 AND g11dat>=? ".
            " GROUP BY RESELID ".
            " ORDER BY RESELID";
   	   $data=array($x2->agentid, $fromday);
   	   break;
   	case 2: // δρομολογιο
   	   $query="SELECT RESELID, COUNT(*) F1 FROM ishop_tplan ".
            " JOIN ishop_g11 ON (ishop_tplan.AGENTID=ishop_g11.AGENTID AND LOGIN=g11acc) ".
            " WHERE ishop_g11.AGENTID=? AND g11cat=1 AND g11dat>=? AND PLANDAYTIME>=NOW() ".
            " GROUP BY RESELID ".
            " ORDER BY RESELID";
   	   $data=array($x2->agentid, $fromday);
   	   break;
   }

   if (($rs=$db->Execute($query,$data))===false) {
		echo "<h6>$query: ".$db->ErrorMsg()."</h6>\n";
		return;
   }
   $nrec=0;
   while ($row=$rs->FetchRow()) {
      echo "<tr";
      if ($nrec++%2) echo " class=HILINE";
      echo ">\n".
         "<td>Καρτέλα Λογιστικής</td>\n".
         "<td>".$row[0]."</td>\n".
         "<td>".$row[1]."</td>\n".
         "<td>".$x2->xxlabel('records')."</td></tr>\n";
   }
   $rs->Close();
   if ($nrec==0)
      tr_echo( "<h6>Δεν υπάρχουν εγγραφές λογιστικής!</h6>" );

   //...................................
   tr_echo("<hr>");
   //...................................
   switch ((int)$_REQUEST['SYNC_CUSTOMER'])
   {
      case 0: // ολοι ανεξαιρέτως
         $query="SELECT 'ALL', COUNT(*) F1 FROM ishop_s80 WHERE AGENTID=? AND s80cat=1 AND s80dat>=?";
         $data=array($x2->agentid, $fromday );
         break;
      case 1: // ολοι του πωλητή
   	   $query="SELECT RESELID, COUNT(*) F1 FROM ishop_s80 ".
            " LEFT JOIN ishop_mbr ON (ishop_mbr.AGENTID=ishop_s80.AGENTID AND LOGIN=s80acc) ".
            " WHERE ishop_s80.AGENTID=? AND s80cat=1 AND s80dat>=? ".
            " GROUP BY RESELID ".
            " ORDER BY RESELID";
   	   $data=array($x2->agentid, $fromday);
   	   break;
   	case 2: // δρομολογιο
   	   $query="SELECT RESELID, COUNT(*) F1 FROM ishop_tplan ".
            " JOIN ishop_g11 ON (ishop_tplan.AGENTID=ishop_s80.AGENTID AND LOGIN=s80acc) ".
            " WHERE ishop_s80.AGENTID=? AND s80cat=1 AND s80dat>=? AND PLANDAYTIME>=NOW() ".
            " GROUP BY RESELID ".
            " ORDER BY RESELID";
   	   $data=array($x2->agentid, $fromday);
   	   break;
   }

   if (($rs=$db->Execute($query,$data))===false) {
		echo "<h6>$query: ".$db->ErrorMsg()."</h6>\n";
		return;
   }
   $nrec=0;
   while ($row=$rs->FetchRow()) {
      echo "<tr";
      if ($nrec++%2) echo " class=HILINE";
      echo ">\n".
         "<td>Καρτέλα Αποθήκης</td>\n".
         "<td>".$row[0]."</td>\n".
         "<td>".$row[1]."</td>\n".
         "<td>".$x2->xxlabel('records')."</td></tr>\n";
   }
   $rs->Close();
   if ($nrec==0)
      tr_echo( "<h6>Δεν υπάρχουν εγγραφές αποθήκης!</h6>" );

   echo "</table>\n";
}
//--------------------------------------
function update_sync()
{
 	global $x2;
   $db=&$x2->db;

 	if (isset($_REQUEST['TESTONLY']))
 	   return test_sync($db);

	$query="REPLACE INTO agent_main (AGENTID,PNAME,PVALUE) VALUES (?,'SYNC_CUSTOMER',?), ".
      " (?,'SYNC_TIDEPTH_NR',?), (?,'SYNC_TIDEPTH_TYPE',?)";

	$data=array(
      $x2->agentid, $_REQUEST['SYNC_CUSTOMER'],
      $x2->agentid, $_REQUEST['SYNC_TIDEPTH_NR'],
      $x2->agentid, $_REQUEST['SYNC_TIDEPTH_TYPE'] );

	if (!$db->Execute($query,$data))
		echo "<h6>".$db->ErrorMsg()."</h6>\n";

   // Redraw main_menu
   main_menu();
}
//--------------------------------------
function do_test(&$x2)
{
	$db=&$x2->db;

	// HEADER
	$tarray=array(
		array("javascript:goTo(100)", 1, 'ctr_center'),
		array('', 0, 'Test')
		);
	$x2->make_header($tarray);

	$ninv=(int)$_REQUEST['NINV'];
	$ar1=explode('-',$_REQUEST['NITEM']);
	$nitem=array();
	$nitem[0]=(int)$ar1[0];
	if (count($ar1)>=2) {
		$nitem[1]=(int)$ar1[1];
	}

	if (empty($_REQUEST['OUTNAME'])) {
		echo "<h6>Empy DIRNAME</h6>";
		return;
	}

	$cust=array();
	$query="SELECT LOGIN FROM ishop_mbr WHERE AGENTID=?";
	$data=array($x2->agentid);
	if (($rs=$db->Execute($query,$data))===false) {
		echo __LINE__.$db->ErrorMsg();
		return;
	}
	while ($row=$rs->FetchRow()) {
		$cust[]=$row[0];
	}
	$rs->Close();

	$item=array();
	$query="SELECT CODE FROM ishop_item WHERE AGENTID=?";
	$data=array($x2->agentid);
	if (($rs=$db->Execute($query,$data))===false) {
		echo __LINE__.$db->ErrorMsg();
		return;
	}
	while ($row=$rs->FetchRow()) {
		$item[]=$row[0];
	}
	$rs->Close();

	$nc=count($cust); // total number of customers
	$ni=count($item); // total number of items
	for ($i=0;$i<$ninv;$i++) {
		clearstatcache();
		if (!file_exists($_REQUEST['OUTNAME']))
			mkdir($_REQUEST['OUTNAME']);

		$j=0;
		do {
			$fout=$_REQUEST['OUTNAME'].FSEP.$j.".txt";
			$j++;
		} while (file_exists($fout));

		if (($fp=fopen($fout,"w"))===false) {
			echo "Failed to open ".$fout;
			return;
		}

		$ic=mt_rand(0,$nc-1);
		$query="SELECT * FROM ishop_mbr WHERE AGENTID=? AND LOGIN=?";
		$data=array($x2->agentid, $cust[$ic]);
		if (($rs=$db->Execute($query,$data))===false) {
			echo __LINE__.$db->ErrorMsg();
			break;
		}
		$o1=$rs->FetchNextObject();
		$rs->Close();
		if ($o1===false) {
			echo "Could not find customer ".myhtml($cust[$ic]);
			continue;
		}

		$Tqua=0;
		$Tval=0;
		$Tvat=0;

		// Number of items for the invoice
		if (count($nitem)==2) {
			$nx=mt_rand($nitem[0], $nitem[1]);
		}
		else {
			$nx=$nitem[0];
		}
		// Get collection of item codes
		$ar1=array();
		for ($i2=0;$i2<$nx;$i2++) {
			$ar1[]=$item[mt_rand(0,$ni-1)];
		}
		// make query
		$query="SELECT * FROM ishop_item WHERE AGENTID=? AND CODE IN (".implode(',',array_fill(0, count($ar1), '?')).")";
		$data=array_merge( array($x2->agentid), $ar1 );
		if (($rs=$db->Execute($query,$data))===false) {
			echo __LINE__.$db->ErrorMsg();
			break;
		}
		$line=0;
		$page=0;
		while ($o2=$rs->FetchNextObject()) {
			if ($line>=12) {
				// restart
				$s1="\x0C";
				fwrite($fp,$s1);
				$line=0;
			}

			if ($line==0) {
				// Print header
				$s1=$o1->COMPANY."\n".$o1->JOB."\n".$o1->ADDR1."\n".$o1->CITY."\n".$o1->STATE."\n".
				$o1->PHONE."\n".$o1->VATNO."\n".$o1->DOY."\n\n";
				fwrite($fp,$s1);
				pre($s1);

				$s1=date('d-m-Y H:i:s')." ΤΙΜΟΛΟΓΙΟ ".sprintf("%10d", mt_rand())."      Σελίδα ".(++$page)."\n\n";
				fwrite($fp,$s1);
				pre($s1);
			}

			$qua=mt_rand(1,10);
			$val=$qua*$o2->PRICE2;
			$vvat=round($val*0.19, 2);
			$s1=sprintf("%-20s %-50s %4d %4.2lf 19%% %6.2lf %4.2lf\n", $o2->CODE, $o2->NAME, $qua, $o2->PRICE2, $val, $vvat);
			fwrite($fp,$s1);
			pre($s1);

			$Tqua += $qua;
			$Tval += $val;
			$Tvat += $vvat;

			++$line;
		}
		$rs->Close();

		// Print empty lines
		while ($line++ < 12) {
			$s1="\n";
			fwrite($fp,$s1);
		}

		// Print footer
		$s1=sprintf("\n%-20s %-50s %4s %7s     %6.2lf %4.2lf\n", "", "Σύνολα", $Tqua, "", $Tval, $Tvat);
		fwrite($fp,$s1);
		pre($s1);

		$s1=sprintf("%-20s %-50s %4s %7s     %6.2lf\n", "", "Γενικό σύνολο", "", "", $Tval+$Tvat);
		fwrite($fp,$s1);
		pre($s1);

		fclose($fp);
	}
}
//--------------------------------------
function ask_test(&$x2)
{
	$db=&$x2->db;

	// HEADER
	$tarray=array(
		array("javascript:goTo(100)", 1, 'ctr_center'),
		array('', 0, 'Test')
		);
	$x2->make_header($tarray);
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST name="goForm">
<input type=HIDDEN name="FUNCTION" value="201">
<table>
<tr>
	<td class=bgLabel>Invoices</td>
	<td class=bgInput><input type=TEXT name="NINV" value="1"></td>
</tr>
<tr>
	<td class=bgLabel>Items (min-max)</td>
	<td class=bgInput><input type=TEXT name="NITEM" value="3-5"></td>
</tr>
<tr>
	<td class=bgLabel>Output</td>
	<td class=bgInput><select name="OUTTYPE">
		<option value="0" SELECTED>Directory
		<option value="1">Printer
		</select><br>
		<input type=TEXT name="OUTNAME" value="c:\tmp\test1"></td>
</tr>
<tr>
	<td></td>
	<td class=bgButton><input type=SUBMIT value="Test"></td>
</tr>
</table>
</form>
<?php
}
//--------------------------------------
function show_php_config()
{
   global $x2;

   // HEADER
	$tarray=array(
		array("javascript:goTo(100)", 1, 'ctr_center'),
		array('javascript:goTo(20)', 1, 'gen_info'),
		array('', 0, 'PHP config')
		);
	$x2->make_header($tarray);

	$gr1=new cXRID('grid22');

	$gr1->cols=array(
      array('Name', 150),
      array('Value', 350)
   );

   $ar1=explode(',', "enable_dl, magic_quotes_gpc, magic_quotes_runtime, asp_tags, precision, serialize_precision, safe_mode,
      max_execution_time, max_input_time, memory_limit, log_errors, variables_order, post_max_size,
      default_mimetype, default_charset, include_path, file_uploads, upload_max_filesize, allow_url_fopen,
      default_socket_timeout");
   foreach ($ar1 as $var1) {
      $gr1->rows[]=array($var1, myhtml(ini_get(trim($var1))) );
   }

   $ar1=explode(',',  'IMAP:imap_8bit, MBSTRING:mb_strlen');
   foreach ($ar1 as $str1) {
      $ar2=explode(':', $str1);
      if (count($ar2)==2) {
         $gr1->rows[]=array($ar2[0], function_exists($ar2[1])?'True':'' );
      }
   }

   $gr1->sort="str,str";
   $gr1->bottom_margin=20;
   $gr1->play();

   echo "<small>End of data</small>";
}
//--------------------------------------
function show_all_properties()
{
   global $x2;
   $db=&$x2->db;

   // HEADER
	$tarray=array(
		array("javascript:goTo(100)", 1, 'ctr_center'),
		array('javascript:goTo(20)', 1, 'gen_info'),
		array('', 1, 'all_properties')
		);
	$x2->make_header($tarray);

	$query="SELECT PNAME,PVALUE FROM agent_main WHERE AGENTID=? ORDER BY PNAME";
	$data=array($x2->agentid);
	if (($rs=$db->Execute($query,$data))===false) {
      echo "<h6>".$db->ErrorMsg()."</h6>";
      return;
   }

   $gr1=new cXRID('grid21');

   $gr1->cols=array(
      array('Name', 150),
      array('Value', 350)
   );

   while ($o1=$rs->FetchNextObject()) {
      $gr1->rows[]=array(
         myhtml($o1->PNAME),
         myhtml($o1->PVALUE)
         );
   }
   $rs->Close();

   $gr1->sort="str,str";
   $gr1->bottom_margin=20;
   $gr1->play();

   echo "<small>End of data</small>";
}
//--------------------------------------
function show_x2_vars()
{
   global $x2;

   // HEADER
	$tarray=array(
		array("javascript:goTo(100)", 1, 'ctr_center'),
		array('', 0, 'X2 variables')
		);
	$x2->make_header($tarray);

   $ar1=explode("\n",var_export($x2,true));
   echo "<pre id=fullpg1 class=fullPage style=\"background-color:#eeeec8\">";
   $n=count($ar1);
   for ($i=0;$i<$n;$i++) {
      /*
		$ar2=explode(' ',$ar1[$i]);
		if (in_array('$user',$ar2) || in_array('$password',$ar2))
			continue;
			*/
		if (stristr($ar1[$i], 'pass')!==false)
		   continue;

		echo $ar1[$i]."\n";
	}
   echo "</pre>";
}
//--------------------------------------
function show_php_vars()
{
   global $x2;

   // HEADER
	$tarray=array(
		array("javascript:goTo(100)", 1, 'ctr_center'),
		array('', 0, 'PHP variables')
		);
	$x2->make_header($tarray);
?>
<table>

<tr>
	<td colspan=2 class=bgHeader><a
      href="javascript:void(0)" onclick="showHideArea('inigetall')"
	   class="LocalHref">[+]</a>
      Configuration options</td>
</tr>
<tbody id=inigetall class=hidden>
<tr class=bgLabel>
   <td>Name</td>
   <td>Local Value</td>
</tr>
<?php
   $data=ini_get_all();
   $nrec=0;
   foreach ($data as $k=>$v) {
      if (!isset($v['local_value']) || strlen($v['local_value'])==0)
         continue;

      echo "<tr";
      if ($nrec++%2>0) echo " class=HILINE";
      echo ">".
         "<td>".$k."</td>".
         "<td>".myhtml($v['local_value'])."</td>".
         "</tr>\n";
   }
?>
</tbody>

<tr>
	<td colspan=2 class=bgHeader><a
      href="javascript:void(0)" onclick="showHideArea('definedconst')"
	   class="LocalHref">[+]</a>
      Defined constants</td>
</tr>
<tbody id=definedconst class=hidden>
<tr class=bgLabel>
   <td>Name</td>
   <td>Value</td>
</tr>
<?php
   $data=get_defined_constants();
   ksort($data);
   $nrec=0;
   foreach ($data as $k=>$v) {
      echo "<tr";
      if ($nrec++%2>0) echo " class=HILINE";
      echo ">".
         "<td>".$k."</td>".
         "<td>".myhtml($v)."</td>".
         "</tr>\n";
   }
?>
</tbody>
</table><small>End of data</small>
<?php
}
//--------------------------------------
function drop_session()
{
   global $x2;
   $db=&$x2->db;

   $ar1=explode(':',$_REQUEST['RECORDID']);
   if (count($ar1)==2) {
      $query="DELETE FROM ishop_session_ctl WHERE APPLI=? AND SESSID=?";
      $data=$ar1;
      if (!$db->Execute($query,$data))
   	   echo "<h6>".$db->ErrorMsg()."</h6>\n";
   }
   else {
	   $data=array( $_REQUEST['RECORDID'] );

      $query="DELETE FROM session WHERE SESSIONID=?";
      if (!$db->Execute($query,$data))
   	   echo "<h6>".$db->ErrorMsg()."</h6>\n";

      $query="DELETE FROM session_data WHERE SESSIONID=?";
      if (!$db->Execute($query,$data))
   	   echo "<h6>".$db->ErrorMsg()."</h6>\n";
   }
}
//--------------------------------------
function session_details()
{
	global $x2;
   $db=&$x2->db;

   // HEADER
	$tarray=array(
		array("javascript:goTo(100)", 1, 'ctr_center'),
		array('javascript:goTo(22)', 0, 'Sessions')
		);
	$x2->make_header($tarray);

   // BODY
	echo "Λεπτομέρειες για το session <b>".myhtml($_REQUEST['RECORDID'])."</b>\n";
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="goForm">
	<input type=hidden name="FUNCTION">
	<input type=hidden name="RECORDID" value="<?php echo $_REQUEST['RECORDID']; ?>">
<table border=0>
<tr class=bgHeader>
	<td>Name</td>
	<td>Value</td>
</tr>
<?php
	$query="SELECT PNAME,PVALUE FROM session_data WHERE SESSIONID=?";
	$data=array($_REQUEST['RECORDID']);
	if ($rs=$db->Execute($query,$data)) {
		$nrec=0;
		while ($o1=$rs->FetchNextObject()) {
			echo "<tr";
			if ($nrec++%2>0)
				echo " class=HILINE";
			echo ">".
				"<td>".myhtml($o1->PNAME)."</td>".
				"<td>".myhtml($o1->PVALUE)."</td>".
				"</tr>\n";
		}
		$rs->Close();
	}
	else
		tr_echo($db->ErrorMsg);
?>
<tr class=bgButton>
   <td colspan=2>
      <input type=BUTTON value="Refresh" onclick="goTo(24)">
   </td>
</tr>
</table>
</form>
<?php
}
//--------------------------------------
function apply_gps_agentid(&$x2)
{
   global $x2;
   $db=&$x2->db;

   $query="UPDATE ishop_gmimei SET AGENTID=? WHERE IMEI=?";
   $data=array($_REQUEST['RECORDID'], $_REQUEST['IMEI']);
   if (!$db->Execute($query,$data))
      echo "<h6>".$db->ErrorMsg()."</h6>";
}
//--------------------------------------
function ask_google_api(&$x2)
{
   global $x2;
   $db=&$x2->db;

   // HEADER
	$tarray=array(
		array("ADMIN", 0, 'Main'),
	   array('javascript:goTo(560)', 0, 'GPS anonymous'),
	   array('', 0, 'Google API key'),
	);
	$x2->make_header($tarray);

	$query="SELECT PVALUE FROM agent_main WHERE AGENTID=0 AND PNAME='GOOGLE_API_KEY' ";
	if (($rs=$db->Execute($query))===false) {
      echo "<h6>".$db->ErrorMsg()."</h6>";
      return;
   }
   $o1=$rs->FetchNextObject();
   $rs->Close();

	$f1=new cFORM('goForm');
   $f1->vars=array( 'FUNCTION'=>'563' );
   $f1->invars[]=new cINVAR(0, 1, 'edit');
	$f1->invars[]=new cINVAR(1, 0, 'Google API key', array('TYPE'=>'TEXT', 'NAME'=>'GAKEY', 'VALUE'=>(@$o1->PVALUE), 'SIZE'=>60 ) );
	$f1->invars[]=new cINVAR(2, 3, false, array('NAME'=>'ok', 'VALUE'=>'upd', 'TYPE'=>'SUBMIT') );
   $f1->run();
}
//--------------------------------------
function upd_google_api(&$x2)
{
   global $x2;
   $db=&$x2->db;

   // HEADER
	$tarray=array(
		array("ADMIN", 0, 'Main'),
	   array('javascript:goTo(560)', 0, 'GPS anonymous'),
	   array('', 0, 'Google API key'),
	);
	$x2->make_header($tarray);

	// Update
	$t1='agent_main';
	$prim=array('AGENTID', 'PNAME');
	$data=array('AGENTID'=>0, 'PNAME'=>'GOOGLE_API_KEY', 'PVALUE'=>trim($_REQUEST['GAKEY']) );
	if (ReplaceEx($db,$t1,$data,$prim)==0) {
      echo "<h6>".$db->ErrorMsg()."</h6>";
   }

   // Re-read
	$query="SELECT PVALUE FROM agent_main WHERE AGENTID=0 AND PNAME='GOOGLE_API_KEY' ";
	if (($rs=$db->Execute($query))===false) {
      echo "<h6>".$db->ErrorMsg()."</h6>";
      return;
   }
   $o1=$rs->FetchNextObject();
   $rs->Close();

	$f1=new cFORM('goForm');
   $f1->vars=array( 'FUNCTION'=>'562' );
   $f1->invars[]=new cINVAR(0, 1, 'preview');
	$f1->invars[]=new cINVAR(1, 0, 'Google API key', array('TYPE'=>'BOLD', 'VALUE'=>(@$o1->PVALUE) ) );
	$f1->invars[]=new cINVAR(2, 3, false, array('NAME'=>'ok', 'VALUE'=>'edit', 'TYPE'=>'SUBMIT') );
   $f1->run();
}
//--------------------------------------
function su_acivate_eafdss(&$x2)
{
   $db=&$x2->db;

   $query="UPDATE einv_eafdss SET ACTIVE=1 WHERE EAFDSS=?";
   $data=array($_REQUEST['RECORDID']);
   if (!$db->Execute($query,$data))
      echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>\n";
}
//--------------------------------------
function su_deacivate_eafdss(&$x2)
{
   $db=&$x2->db;

   $query="UPDATE einv_eafdss SET ACTIVE=0 WHERE EAFDSS=?";
   $data=array($_REQUEST['RECORDID']);
   if (!$db->Execute($query,$data))
      echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>\n";
}
//--------------------------------------
function su_rename_eafdss(&$x2)
{
   $db=&$x2->db;

   if ($_REQUEST['NEWNAME']===$_REQUEST['EAFDSS']) {
      echo "<h5>Same name!</h5>";
      return;
   }

   if (mb_strlen($_REQUEST['NEWNAME'])!=11) {
      echo "<h5>Name should be exactly 11 characters!</h5>";
      return;
   }

   $query="SELECT COUNT(*) FROM einv_signs WHERE EAFDSS=?";
   $data=array($_REQUEST['EAFDSS']);
   if (($rs=$db->Execute($query,$data))===false) {
      echo $db->ErrorMsg();
      return;
   }
   $row=$rs->FetchRow();
   $n=(int)@$row[0];
   $rs->Close();
   if ($n>0) {
      echo "<h5>EAFDSS ".myhtml($_REQUEST['EAFDSS'])." already has transactions!</h5>";
      return;
   }

   $query="UPDATE einv_eafdss SET EAFDSS=? WHERE EAFDSS=?";
   $data=array($_REQUEST['NEWNAME'], $_REQUEST['EAFDSS']);
   if (!$db->Execute($query,$data))
      echo $db->ErrorMsg();
}
//--------------------------------------
function su_agent_transf_eafdss(&$x2)
{
   $db=&$x2->db;

   $eafdss=$_REQUEST['EAFDSS'];
   $newAgentid=(int)$_REQUEST['AGENTIDFOREA'];

   $db->StartTrans();

   do {
      // read old value
      $query="SELECT AGENTID FROM einv_eafdss WHERE EAFDSS=?";
      $data=array($eafdss);
      if (($rs=$db->Execute($query,$data))===false) {
         echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>\n";
         break;
      }
      $row=$rs->FetchRow();
      $rs->Close();
      if ($row===false) {
         echo "<h6>Record [$eafdss] not found!</h6>\n";
         break;
      }

      $oldAgentid=$row[0];

      if ($oldAgentid==$newAgentid) {
         echo "<h6>Same Agent Id. No transfer will take place!</h6>\n";
         break;
      }

      // Delete old records
      $query="DELETE FROM einv_signs WHERE EAFDSS=?";
      $data=array($eafdss);
      if (!$db->Execute($query, $data)) {
         echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>\n";
         break;
      }

      $query="UPDATE einv_eafdss SET AGENTID=? WHERE EAFDSS=?";
      $data=array($newAgentid, $eafdss );
      if (!$db->Execute($query, $data)) {
         echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>\n";
         break;
      }

      // Update totals
      update_einv_addsigns();

   } while (false);

   $db->CompleteTrans();
}
//--------------------------------------
function su_agent_clr_eafdss_trans(&$x2)
{
   $db=&$x2->db;

   $eafdss=$_REQUEST['RECORDID'];

   $db->StartTrans();
   do {
      $query="DELETE FROM einv_signs WHERE EAFDSS=?";
      $data=array($eafdss);
      if (!$db->Execute($query,$data)) {
         echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>\n";
         break;
      }

      // Update totals
      update_einv_addsigns();

   } while (false);
   $db->CompleteTrans();
}
//--------------------------------------
function su_agent_del_eafdss(&$x2)
{
   $db=&$x2->db;

   $eafdss=$_REQUEST['RECORDID'];

   $db->StartTrans();
   do {
      $query="DELETE FROM einv_signs WHERE EAFDSS=?";
      $data=array($eafdss);
      if (!$db->Execute($query,$data)) {
         echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>\n";
         break;
      }

      $query="DELETE FROM einv_eafdss WHERE EAFDSS=?";
      $data=array($eafdss);
      if (!$db->Execute($query,$data)) {
         echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>\n";
         break;
      }

      // Update totals
      update_einv_addsigns();

   } while (false);
   $db->CompleteTrans();
}
//--------------------------------------
function su_check_eafdss_docids()
{
   global $x2;
   $db=&$x2->db;

   // HEADER
	$tarray=array(
		array("ADMIN", 0, 'Main'),
      array('javascript:goTo(556)', 0, 'EAFDSS'),
	   array('', 0, 'Check EAFDSS DocIds'),
	);
	$x2->make_header($tarray);

   update_einv_addsigns(false);
}
//--------------------------------------
function update_einv_addsigns($doupdate=true)
{
   global $x2;
   $db=&$x2->db;

   $rows=array();
   $query="SELECT DOCID, SIGNQUA, SIGNAVL, 0 FC FROM einv_addsigns ORDER BY DOCID";
   $data=false;
   if (($rs=$db->Execute($query,$data))===false) {
      echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>\n";
      return;
   }
   while ($o1=$rs->FetchnextObject()) {
      $rows[$o1->DOCID] = $o1;
   }
   $rs->Close();

   $query="SELECT DOCID, COUNT(*) FC FROM einv_signs ORDER BY DOCID";
   $data=false;
   if (($rs=$db->Execute($query,$data))===false) {
      echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>\n";
      return;
   }
   while ($o1=$rs->FetchNextObject()) {
      if (!array_key_exists($o1->DOCID, $rows)) {
         echo "<h6>DOCID ".$o1->DOCID." not in array!</h6>";
         continue;
      }

      $rows[$o1->DOCID]->FC=$o1->FC;        echo "Adding...";
   }
   $rs->Close();

   if (!$doupdate) {
      pre($rows);
      return;
   }

   $query="UPDATE einv_addsigns SET SIGNAVL=? WHERE DOCID=?";
   foreach ($rows as $o1) {
      $avail=$o1->SIGNQUA - $o1->FC;
      if ($o1->SIGNAVL!=$avail) {
         $data=array($avail, (int)$o1->DOCID);
         if (!$db->Execute($query,$data)) {
            echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>\n";
            break;
         }
      }
   }
}
//--------------------------------------
function su_agent_add_eafdss2(&$x2)
{
   $db=&$x2->db;

   $t1='einv_eafdss';
   $prim=array('AGENTID', 'EAFDSS');
   $data=array('AGENTID'=>(int)$_REQUEST['AGENTIDFOREA'], 'EAFDSS'=>$_REQUEST['EAFDSS'],
      'ACTIVE'=>empty($_REQUEST['ACTIVE'])?0:1);
   if (ReplaceEx($db, $t1, $data, $prim)==0)
      echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>\n";
}
//--------------------------------------
function su_agent_add_eafdss(&$x2)
{
   $db=&$x2->db;

   $query="SELECT AGENTID FROM agents WHERE LPATH=?";
	$data=array($_SESSION['LPATH']);
	if (($rs=$db->Execute($query,$data))===false) {
      echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>\n";
      return;
   }
	$o1=$rs->FetchNextObject();
	$rs->Close();
	if ($o1===false) {
	   echo "<h6>Could not find agent ".myhtml($_REQUEST['LPATH'])."</h6>";
	   return;
	}

   $t1='einv_eafdss';
   $prim=array('AGENTID', 'EAFDSS');
   $data=array('AGENTID'=>$o1->AGENTID, 'EAFDSS'=>$_REQUEST['EAFDSS'],
      'ACTIVE'=>isset($_REQUEST['ACTIVE'])?1:0);
   if (ReplaceEx($db, $t1, $data, $prim)==0)
      echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>\n";
}
//--------------------------------------
function su_agent_add_units(&$x2)
{
   $db=&$x2->db;

   $query="SELECT AGENTID FROM agents WHERE LPATH=?";
	$data=array($_SESSION['LPATH']);
	if (($rs=$db->Execute($query,$data))===false) {
      echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>\n";
      return;
   }
	$o1=$rs->FetchNextObject();
	$rs->Close();
	if ($o1===false) {
	   echo "<h6>Could not find agent ".myhtml($_REQUEST['LPATH'])."</h6>";
	   return;
	}

   $db->StartTrans();

   $ar1=explode('-',$_REQUEST['DOCDATE']);
   if (count($ar1)!=3) {
      echo "<h6>Error in date format!</h6>";
      return;
   }

   $docdate = implode('-', array_reverse($ar1));

   do {
      $query="INSERT INTO einv_addsigns (AGENTID, DOCNR, DOCDATE, SIGNQUA, SIGNAVL) VALUES(?,?,?,?,?)";
      $qua=(int)$_REQUEST['SIGNQUA'];
      $data=array($o1->AGENTID, $_REQUEST['DOCNR'], $docdate, $qua, $qua );
      if (!$db->Execute($query,$data)) {
         echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>\n";
         break;
      }

   } while (false);

   $db->CompleteTrans();
}
//--------------------------------------
function check_einv_files(&$x2)
{
   $db=&$x2->db;

   require_once('ishop_tables.php');
	check_tables_einv($db);

   umessage("Operation completed!");
}
//--------------------------------------
function su_list_all_eafdss(&$x2)
{
   $db=&$x2->db;

   // HEADER
	$tarray=array(
		array("ADMIN", 0, 'Main'),
	   array('javascript:goTo(556)', 0, 'E-Invoice all EAFDSS(r)')
	);
	$x2->make_header($tarray);

   // BODY
   $agents=array();
   $query="SELECT AGENTID, LPATH FROM agents WHERE AGENTID>0 ORDER BY AGENTID";
   if (($rs=$db->Execute($query))===false) {
      echo __LINE__.$db->Errormsg();
      return;
   }
   while ($o1=$rs->FetchNExtObject()) {
      $agents[$o1->AGENTID] = $o1->AGENTID.'# '.$o1->LPATH;
   }
   $rs->Close();

   $query="SELECT T1.EAFDSS, T1.AGENTID, T1.ACTIVE, T2.LPATH, COUNT(T3.PROGRESSIVE) FC FROM einv_eafdss T1
      LEFT JOIN agents T2 ON (T2.AGENTID=T1.AGENTID)
      LEFT JOIN einv_signs T3 ON (T3.EAFDSS=T1.EAFDSS)
      GROUP BY T1.EAFDSS, T1.AGENTID, T1.ACTIVE, T2.LPATH
      ORDER BY T1.EAFDSS";
   if (($rs=$db->Execute($query))===false) {
      echo __LINE__.$db->Errormsg();
      return;
   }

   $gr1=new cXRID('grid23');

   $gr1->cols=array(
      array('Eafdss', 100),
      array('Active', 60),
      array('Agentid', 60),
      array('Lpath', 100),
      array('Signed docs', 100)
   );

   while ($o1=$rs->FetchnextObject()) {
      $row1=array();

      $row1[]=$o1->EAFDSS;
      $row1[]=array('data'=>($o1->ACTIVE?'v':'x'), 'style'=>'font-weight:bold; color:'.($o1->ACTIVE?'#00a000':'#ff0000') );
      $row1[]=$o1->AGENTID;
      $row1[]=$o1->LPATH;
      $row1[]=$o1->FC;

      $gr1->rows[]=$row1;
   }
   $rs->Close();

   $gr1->click='oclick';
   $gr1->play();
?>
   <div id=tbar2>
      <button type=button onclick="show_popup('popEAFDSS')">Add EAFDSS</button>
      &nbsp;
      <b><a href="javascript:goTo(568)">Check DOCIDs</a></b>
   </div>

<script>
var id;
function oclick(stage,row,col)
{
   id=this.cells(row,0).getValue();
   document.FTRANSFER.EAFDSS.value=id;
   document.FRENAME.EAFDSS.value=id;
   document.FRENAME.NEWNAME.value=id;
   $('#t-eafdss').text(id);
   show_popup('popmenu');
   return false;
}
function winRet(x)
{
	switch (x)
	{
	case 559: // Activate
   case 564: // Deactivate
      goRecord(x,id);
      break;
   case 565: // Delete
   case 567: // Clear transactions
      goRecDelete(x,id);
      break;
   case 566: // Transfer to other Agent
      show_popup('popTRANSFER');
      break;
   case 569:
      show_popup('popRENAME');
      break;
	}
}
</script>

   <div id=popTRANSFER class=POPMENU>
   <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST name="FTRANSFER">
   <input type=HIDDEN name="FUNCTION" value="566">
   <input type=HIDDEN name="EAFDSS">
   <table border=0 cellpadding=4>
   <tr>
      <td class=bgHeader colspan=2>Transfer EAFDSS</td>
   </tr>
   <tr>
      <td>EAFDSS</td>
      <td id="t-eafdss"></td>
   </tr>
   <tr>
      <td>New Agent</td>
      <td><select name="AGENTIDFOREA">
         <?php prAssocSelect($agents,0); ?>
      </select></td>
   </tr>
   <tr>
      <td>Active</td>
      <td><input type=CHECKBOX name="ACTIVE"> Yes</td>
   </tr>
   <tr>
      <td></td>
      <td>
         <input type=SUBMIT value="Update">
         <input type=BUTTON value="Cancel" onclick="$('#popTRANSFER').hide()">
      </td>
   </tr>
   </table>
   </form>
   </div>

   <div id=popRENAME class=POPMENU>
   <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST name="FRENAME">
   <input type=HIDDEN name="FUNCTION" value="569">
   <input type=HIDDEN name="EAFDSS">
   <table border=0 cellpadding=4>
   <tr>
      <td class=bgHeader colspan=2>Transfer EAFDSS</td>
   </tr>
   <tr>
      <td>EAFDSS</td>
      <td id="t-eafdss2"></td>
   </tr>
   <tr>
      <td>New Name</td>
      <td><input type=TEXT name="NEWNAME" maxlength=11></td>
   </tr>
   <tr>
      <td></td>
      <td>
         <input type=SUBMIT value="Update">
         <input type=BUTTON value="Cancel" onclick="$('#popRENAME').hide()">
      </td>
   </tr>
   </table>
   </form>
   </div>

   <div id=popEAFDSS class=POPMENU>
   <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST onsubmit="return checkEafdss(this)">
   <input type=HIDDEN name="FUNCTION" value="558">
   <table border=0 cellpadding=4>
   <tr>
      <td class=bgHeader colspan=2>Add EAFDSS</td>
   </tr>
   <tr>
      <td>Agent</td>
      <td><select name="AGENTIDFOREA">
         <?php prAssocSelect($agents,0); ?>
      </select></td>
   </tr>
   <tr>
      <td>EAFDSS (serial-11)</td>
      <td><input type=TEXT name="EAFDSS" MAXLENGTH=11></td>
   </tr>
   <tr>
      <td>Active</td>
      <td><input type=CHECKBOX name="ACTIVE"> Yes</td>
   </tr>
   <tr>
      <td></td>
      <td>
         <input type=SUBMIT value="Update">
         <input type=BUTTON value="Cancel" onclick="$('#popEAFDSS').hide()">
      </td>
   </tr>
   </table>
   </form>
   </div>
<?php
   $selx=array(
	   array(559, '/img/ok_box.png', 'Activate' ),
		array(564, '/img/cancel_box.png', 'Deactivate' ),
		array(565, '/img/button_drop.png', $x2->xxlabel('Delete') ),
      array(566, '/img/transfer.png', 'Transfer to other Agent' ),
      array(569, '/img/rename.png', 'Rename' ),
      array(567, '/img/drop.png', 'Clear transactions' )
	);
   draw_popup($selx,false);
}
//--------------------------------------
function su_agent_signavail_upd()
{
   global $x2;
   $db=&$x2->db;

   $query="UPDATE einv_addsigns SET DOCNR=?, DOCDATE=? WHERE AGENTID=? AND DOCID=?";
   $data=array(
      $_REQUEST['DOCNR'],
      implode('-',array_reverse(explode('-',$_REQUEST['DOCDATE']))),
      $_SESSION['AGENTID'],
      (int)$_REQUEST['DOCID']
   );
   if (!$db->Execute($query,$data)) {
      echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>\n";
   }
}
//--------------------------------------
function su_agent_signavail_list(&$x2)
{
   $db=&$x2->db;

   // HEADER
	$tarray=array(
		array("ADMIN", 0, 'Main'),
	   array('javascript:goTo(545)', 0, 'E-Invoice signholes(r)')
	);
	$x2->make_header($tarray);

	// BODY
?>
<script>
var actv;
var eafdss;
var popmenu4;
function shoPopup3(b,s1)
{
   actv=b;
   eafdss=s1;
   show_popup('popmenu3');
}
var edtId;
var edtNr;
var edtDate;
function oclick(stage,row,col)
{
   edtId=this.cells(row,0).getValue();
   edtNr=this.cells(row,1).getValue();
   edtDate=this.cells(row,2).getValue().replace(/\//g, '-');
   show_popup('popmenu5');
}
function winRet(x)
{
   switch (x)
   {
   case 543: // activate
      if (actv) {
         window.alert('Already activated!');
      }
      else {
         goRecord(x,eafdss);
      }
      break;
   case 544: // deactivate
      if (actv) {
         goRecord(x,eafdss);
      }
      else {
         window.alert('Already deactivated!');
      }
      break;
   case 548:
      goRecDelete(x,eafdss);
      break;
   case 574:
      $('#edtID').val(edtId);
      $('#edtNR').val(edtNr);
      $('#edtDATE').val(edtDate);
      showHide('popmenu5',0);
      popmenu4.dialog('open');
      break;
   }
}
</script>
<?php
	if (isset($_REQUEST['LPATH']))
	   $_SESSION['LPATH']=$_REQUEST['LPATH'];

 	$query="SELECT AGENTID FROM agents WHERE LPATH=?";
	$data=array($_SESSION['LPATH']);
	if (($rs=$db->Execute($query,$data))===false) {
      echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>\n";
      return;
   }
	$o1=$rs->FetchNextObject();
	$rs->Close();
	if ($o1===false) {
	   echo "<h6>Could not find agent ".myhtml($_REQUEST['LPATH'])."</h6>";
	   return;
	}
   $_SESSION['AGENTID'] = (int)$o1->AGENTID;

   $f1=new cFORM('goForm');
   $f1->vars=array( 'FUNCTION'=>'', 'RECORDID'=>'' );
   $f1->run();

   $query="SELECT EAFDSS, ACTIVE FROM einv_eafdss WHERE AGENTID=?";
   $data=array($o1->AGENTID);
   if (($rs=$db->Execute($query,$data))===false) {
      echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>";
      return;
   }
   echo "<div class=bgLine><td>EAFDSS: <b>";
   while ($o2=$rs->FetchNextObject()) {
      if ($o2->ACTIVE==0) {
         echo "[<a href=\"javascript:shoPopup3(false,'".$o2->EAFDSS."')\" style=\"color:red;\"
            title=\"Inactive\">".$o2->EAFDSS."</a>]\n";
      }
      else {
         echo "[<a href=\"javascript:shoPopup3(true,'".$o2->EAFDSS."')\" style=\"color:#00A000;\"
            title=\"Active\">".$o2->EAFDSS."</a>]\n";
      }
   }
   $rs->Close();
   echo "</b>&nbsp;<button type=button
      onclick=\"show_popup(document.getElementById('popmenu2'))\">Add EAFDSS</button>
      </div>";

   $query="SELECT DOCID, DOCNR, DOCDATE, SIGNQUA, SIGNAVL, LASTUPDATE FROM einv_addsigns
      WHERE AGENTID=?
      ORDER BY DOCID";
   $data=array($o1->AGENTID);
   if (($rs=$db->Execute($query,$data))===false) {
      echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>";
      return;
   }

	$gr1=new cXRID('grid10');

   array_push($gr1->cols,
		array( 'Id', 120),
		array( 'Document nr.', 120),
		array( 'Document date', 160),
		array( 'Quantity', 100),
      array( 'Available', 100),
      array( 'Last update', 160)
   );

   $Tavail=0;
   while ($o2=$rs->FetchNextObject()) {
      $row1=array();

      $row1[]=myhtml($o2->DOCID);
      $row1[]=myhtml($o2->DOCNR);
      $row1[]=GrDate($o2->DOCDATE);
      $row1[]=$o2->SIGNQUA;
      $row1[]=$o2->SIGNAVL;
      $row1[]=GrDateTime($o2->LASTUPDATE);

      $gr1->rows[]=$row1;

      $Tavail += $o2->SIGNAVL;
   }
   $rs->Close();

   // Execute
   $gr1->click='oclick';
   $gr1->play();
?>
   <div id=tbar2>
      <b>Current signholes: <?php echo $Tavail; ?></b>
      &nbsp;<button type=button onclick="show_popup(document.getElementById('popmenu'))">Add more</button>
   </div>

   <div id=popmenu class=POPMENU>
   <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST>
   <input type=HIDDEN name="FUNCTION" value="546">
   <table border=0 cellpadding=4>
   <tr>
      <td class=bgHeader colspan=2>Add more signholes</td>
   </tr>
   <tr>
      <td>Document number</td>
      <td><input type=TEXT name="DOCNR"></td>
   </tr>
   <tr>
      <td>Document date</td>
      <td><input type=TEXT name="DOCDATE" value="<?php echo date('d-m-Y'); ?>" class=idate></td>
   </tr>
   <tr>
      <td>Quantity</td>
      <td><input type=TEXT name="SIGNQUA"></td>
   </tr>
   <tr>
      <td></td>
      <td>
         <input type=SUBMIT value="Update">
         <input type=BUTTON value="Cancel" onclick="showHide('popmenu',0)">
      </td>
   </tr>
   </table>
   </form>
   </div>

   <div id=popmenu2 class=POPMENU>
   <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST onsubmit="return checkEafdss(this)">
   <input type=HIDDEN name="FUNCTION" value="547">
   <table border=0 cellpadding=4>
   <tr>
      <td class=bgHeader colspan=2>Add EAFDSS</td>
   </tr>
   <tr>
      <td>EAFDSS (serial-11)</td>
      <td><input type=TEXT name="EAFDSS" MAXLENGTH=11></td>
   </tr>
   <tr>
      <td>Active</td>
      <td><input type=CHECKBOX name="ACTIVE"> Yes</td>
   </tr>
   <tr>
      <td></td>
      <td>
         <input type=SUBMIT value="Update">
         <input type=BUTTON value="Cancel" onclick="showHide('popmenu2',0)">
      </td>
   </tr>
   </table>
   </form>
   </div>

   <div id=popmenu4 title="Update Document Info">
   <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST>
   <input type=HIDDEN name="FUNCTION" value="574">
   <table border=0 cellpadding=4>
   <tr>
      <td>Document Id</td>
      <td><input type=TEXT id=edtID name="DOCID" READONLY></td>
   </tr>
   <tr>
      <td>Document number</td>
      <td><input type=TEXT id=edtNR name="DOCNR"></td>
   </tr>
   <tr>
      <td>Document date</td>
      <td><input type=TEXT id=edtDATE name="DOCDATE" class=idate></td>
   </tr>
   <tr>
      <td></td>
      <td>
         <input type=SUBMIT value="Update">
         <input type=BUTTON value="Cancel" onclick="popmenu4.dialog('close')">
      </td>
   </tr>
   </table>
   </form>
   </div>
<script>
$(function(){
   $('.idate').datepicker();
   popmenu4 = $('#popmenu4').dialog({autoOpen:false, modal:true, resizable:false, width:400});
} );
</script>
<?php
   $selx=array(
	   array(543, '/img/ok_box.png', 'Activate' ),
		array(544, '/img/cancel_box.png', 'Deactivate' ),
		array(548, '/img/button_drop.png', $x2->xxlabel('Delete') )
	);
   draw_popup($selx,false,'popmenu3');

   $selx=array(
	   array(574, '/img/button_edit.png', 'Edit' )
   );
   draw_popup($selx,false,'popmenu5');
}
//--------------------------------------
function list_gps_anon(&$x2)
{
   $db=&$x2->db;

   // HEADER
	$tarray=array(
		array("ADMIN", 0, 'Main'),
	   array('javascript:goTo(560)', 0, 'GPS anonymous(r)')
	);
	$x2->make_header($tarray);

	// BODY
?>
<script language=JavaScript>
function oclick(stage,row,col)
{
   var id=this.cells(row,0).getValue();
   document.goForm.IMEI.value = id;
   show_popup(document.getElementById('popmenu'));
   try {
      var o1=document.getElementById('imei');
      o1.innerHTML = id;
   }
   catch(e) {}
   return false;
}
function winRet(x)
{
	switch (x)
	{
	case 1:
	   queryNewWin();
		goRecordEx('ishop_item.php',162,icode);
		break;
	}
}
</script>
<?php
	$f1=new cFORM('goForm');
   $f1->vars=array( 'FUNCTION'=>'', 'IMEI'=>'', 'RECORDID'=>'' );
   $f1->run();

	$query="SELECT T1.IMEI, T3.ROBJECT, T1.AGENTID, T2.LPATH FROM ishop_gmimei T1
      LEFT JOIN agents T2 ON (T2.AGENTID=T1.AGENTID)
      LEFT JOIN ishop_gmobject T3 ON (T3.AGENTID=T1.AGENTID AND T3.OBJID=T1.OBJID)
      ORDER BY T1.IMEI";
   if (($rs=$db->Execute($query))===false) {
      echo "<h6>".$db->ErrorMsg()."</h6>";
      return;
   }

	$gr1=new cXRID('grid14');

   array_push($gr1->cols,
		array( 'IMEI', 120),
		array( 'Object', 120),
		array( 'Agent', 160)
   );

   while ($o1=$rs->FetchNextObject()) {
      $row1=array();

      $row1[]=myhtml($o1->IMEI);
      $row1[]=myhtml($o1->ROBJECT);
      $row1[]=$o1->AGENTID>0?myhtml($o1->AGENTID.'# '.$o1->LPATH):'<b>Anonymous</b>';

      $gr1->rows[]=$row1;
   }
   $rs->Close();

   // Execute
	$gr1->click='oclick';
   $gr1->play();

   echo "<div id=tbar2>
      <b><a href=\"javascript:goTo(562)\">Google API key</a></b>
      </div>";

   echo "<div id=popmenu class=POPMENU><table border=0 cellpadding=4>
      <tr><td>Select <i>Agent Id</i> to (re)assign on Record Id <b>#<span id=imei></span></b>:</td></tr>
      <tr><td><div style=\"height:100px; overflow-y:scroll;\">";
   $query="SELECT AGENTID, LPATH FROM agents ORDER BY AGENTID";
   if (($rs=$db->Execute($query))===false) {
      echo "<h6>".$db->ErrorMsg()."</h6>";
   }
   else {
      while ($o1=$rs->FetchNextObject()) {
         echo "<b><a href=\"javascript:goRecord(561,".$o1->AGENTID.")\">".myhtml($o1->AGENTID."# ".$o1->LPATH)."</a></b><br>";
      }
      $rs->Close();
   }
   echo "</div></td></tr></table></div>";
}
//--------------------------------------
function add_hasp_lic()
{
   global $x2;
   $db=&$x2->db;

   $clicense=trim( $_REQUEST['CLICENSE'] );
   $se=encode_decode_data(2, $clicense);
   if (($ar1=unserialize($se))===false) {
      echo "<h6>Data error (authentication failed)!</h6>";
      return;
   }
   if (empty($ar1['HASPID'])) {
      echo "<h6>Data error (authentication failed)!</h6>";
      return;
   }

   //pre($ar1);

   $t1='hasp_lic';
   $prim=array( 'HASPID' );
   $data=array(
      'HASPID'=>$ar1['HASPID'],
      'CLICENSE'=>$clicense,
      'LASTEXPUPD'=>date('Y-m-d H:i:s')
      );
   if (!ReplaceEx($db,$t1,$data,$prim)) {
      echo $db->ErrorMsg();
   }
}
//--------------------------------------
function show_versions()
{
   global $x2;
   $db=&$x2->db;

   // HEADER
	$tarray=array(
		array("javascript:goTo(100)", 1, 'ctr_center'),
		array('', 0, 'Versions')
		);
	$x2->make_header($tarray);

   print_all_versions();
}
//--------------------------------------
function list_sessions($issuper)
{
   global $x2;
   $db=&$x2->db;

   // HEADER
	if ($issuper) {
		$tarray=array(
		   array("ADMIN", 0, 'Main'),
	   	array('javascript:goTo(542)', 0, 'Sessions(r)')
		   );
	}
	else {
		$tarray=array(
			array("javascript:goTo(100)", 1, 'ctr_center'),
			array('javascript:goTo(22)', 0, 'Sessions(r)')
			);
	}
	$x2->make_header($tarray);

   // BODY
	function get_re_name($uid)
	{
      global $x2;
      $db=&$x2->db;

      $re_name=false;
      $query="SELECT RE_NAME FROM reseller WHERE RESELID=?";
      $data=array($uid);
      if (($rs3=$db->Execute($query,$data))===false) {
         echo "<h6>".$db->ErrorMsg()."</h6>";
      }
      else {
         if ($row3=$rs3->FetchRow()) {
            $re_name=$row3[0];
         }
         $rs3->Close();
      }
      return $re_name;
   }

   //
   // Create (if not exists) random key
   //
   $query="SELECT COUNT(*) FROM agent_main where AGENTID=0 AND PNAME='RANDHASP' ";
   $data=false;
   if (($rs=$db->Execute($query,$data))===false) {
      echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>\n";
   }
   else {
      $row=$rs->FetchRow();
      $rs->Close();
      if ((int)@$row[0]==0) {
         $query="INSERT INTO agent_main (AGENTID, PNAME, PVALUE) VALUES(0, 'RANDHASP', ?)";
         $data=array( 'RAND'.mt_rand(10000000, 99999999) );
         if (!$db->Execute($query,$data))
	        echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>\n";
      }
   }

   // Delete existing property-info so that getHaspInfo() will regenerate:
	$query="DELETE FROM agent_main WHERE AGENTID=0 AND (PNAME='XTD_P1' OR PNAME='XTD_P2')";
	if (!$db->Execute($query))
	   echo "<h6>".$db->ErrorMsg()."</h6>\n";

   $x2->getHaspInfo2();

   include("ishop_lic.inc.php");
   if (!isset($licSpec))
      return;
?>
<style>
.boLow {border-bottom:1px solid #404040;}
.boTop {border-top:1px solid #404040;}
</style>
<table>
<tr>
	<td colspan=3 class=bgHeader><?php $x2->xprint('instalic'); ?></td>
</tr>
<tr>
	<td class="bgLabel boLow"><b>Serial</b></td>
   <td class="bgLabel boLow">&nbsp;</td>
	<td class="bgInput boLow"><b><?php
      echo implode(' ', explode('#',@$x2->lic['zSerial']) );
   ?></b></td>
</tr>
<?php
   foreach ($licSpec as $spec) {
      echo "<tr>";
      $n=count($spec);
      switch ($n)
      {
      case 1:
         echo "<td colspan=2 class=bgLabel>".myhtml($spec[0])."</td>";
         break;
      case 3:
         if (strpos($spec[2],'s')!==false) {
            echo "<td></td><td class=bgLabel>".myhtml($spec[0])."</td>";
         }
         else {
            echo "<td colspan=2 class=bgLabel>".myhtml($spec[0])."</td>";
         }

         $x = (int)@$x2->lic[ $spec[1] ];
         if (strpos($spec[2],'b')!==false) { // bool
            echo "<td class=bgInput><b>".($x>0?"Yes":"No")."</b></td>";
         }
         else { // numeric
            echo "<td class=bgInput><b>$x</b></td>";
         }
      }
      echo "</tr>";
   }
?>
<tr>
	<td colspan=2 class="bgLabel boTop">Expires</td>
	<td class="bgInput boTop"><b><?php
      $ar1=explode('#', $x2->lic['zEndDate']??'');
      foreach ($ar1 as $v1) {
         if (strlen($v1)==8) {
            if ($v1==20991231)
               echo "Never expires";
            else
               echo GrDate($v1);
         }
         else {
            echo myhtml($v1);
         }
         // Put separator
         echo "\n";
      }
   ?></b></td>
</tr>
</table>
<p>
<?php
	if ($issuper) {
      $haspid='';
      $ar1=explode('#',@$x2->lic['zSerial'] );
      for ($i=0;$i<count($ar1);$i++) {
         if (!empty($ar1[$i])) {
            $haspid = $ar1[$i];
            break;
         }
      }
?>
      <script>
      function query_lic()
      {
         var url='http://www.incahellas.com/inca/pda_lic.htm?HASPID=<?php echo $haspid; ?>';
         window.open(url);
      }
      </script>
      <div id=addlic style="display:none" title="Update license">
         <form method=POST action="<?php echo $_SERVER['PHP_SELF']; ?>">
         <input type=hidden name="FUNCTION" value="549">
         Insert license key:<br>
         <textarea name="CLICENSE" style="width:480px; height:150px;"></textarea><br>
         <input type=submit value="Add license">
         </form>
      </div>
      <div class=bgButton2>
         <button type=button onclick="$('#addlic').dialog({resizable: false, modal:true, width:520}).dialog('open')">Add license</button>
         <button type=button onclick="query_lic()">Internet query license</button>
      </div>
<?php
		return;
   }
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="goForm">
<p>
	<input type=hidden name="FUNCTION">
	<input type=hidden name="RECORDID">
</p>
<table border=0>
<tr class=bgHeader>
   <td>Application</td>
	<td>Session Id</td>
	<td>Created</td>
	<td>Reseler</td>
	<td>Last usage</td>
	<td>Time inactive</td>
</tr>
<?php
   //
   // First group of logins (generic Pocket.ERP)
   //
   $query="SELECT SESSIONID, CREDAYTIME, LASTUSAGE, APPLI, NOW()-LASTUSAGE F1 FROM session ORDER BY CREDAYTIME";
   $query2="SELECT PNAME,PVALUE FROM session_data WHERE SESSIONID=?";
   if (!$rs=$db->Execute($query)) {
		tr_echo("<h6>".$db->ErrorMsg()."</h6>");
		return;
	}
	$nrec=0;
	while ($o1=$rs->FetchNextObject()) {
		$props=array();
	   $data=array($o1->SESSIONID);
		if (!$rs2=$db->Execute($query2,$data)) {
			tr_echo("<h6>".$db->ErrorMsg()."</h6>");
			continue;
		}
		while ($o2=$rs2->FetchNextObject()) {
			$props[$o2->PNAME]=$o2->PVALUE;
		}
		$rs2->Close();

		if (!isset($props['AGENTID']) || (int)$props['AGENTID']!=$x2->agentid)
			continue;

		echo "<tr";
		if (($nrec++%2)>0)
			echo " class=HILINE";
		echo ">".
		   "<td>".(empty($o1->APPLI)?'Pocket.ERP':myhtml($o1->APPLI))."</td>".
			"<td>".myhtml($o1->SESSIONID)."</td>".
			"<td>".GrDateTime($o1->CREDAYTIME)."</td>".
			"<td>";
	   $uid=false;
		if (isset($props['OPERID']) && (int)$props['OPERID']>=0)
         $uid=(int)$props['OPERID'];
      else if (isset($props['RESELID']) && (int)$props['RESELID']>=0)
         $uid=(int)$props['RESELID'];
      if ($uid!==false) {
         echo $uid.'# ';

         if (($re_name=get_re_name($uid))!==false)
            echo myhtml($re_name);
		}
		else {
		   echo "Admin";
		}
		echo "</td>".
			"<td>";
      if ((int)$o1->LASTUSAGE>0)
         echo GrDateTime($o1->LASTUSAGE);
      echo "</td>".
		   "<td>";
      if ((int)$o1->LASTUSAGE>0) {
            $dt=$o1->F1;
            if ($dt<60)
               echo $dt." sec";
            else if ($dt<3600)
               echo ((int)($dt/60))." min.";
            else if ($dt<86400)
               echo ((int)($dt/3600))." hours";
            else
               echo ((int)($dt/86400))." days";
      }
		echo "</td>".
			"<td><a href=\"javascript:goRecord(24,'".myhtml($o1->SESSIONID).
			"')\"><img src=\"/img/button_browse.png\" border=0 alt=\"Details\"></a></td>".
			"<td>";
      if (strcmp($o1->SESSIONID, (string)@$_REQUEST['SESSIONID'])==0) {
         echo "<b style=\"color:red\">Self</b>";
      }
      else {
         echo "<a href=\"javascript:goRecDelete(23,'".myhtml($o1->SESSIONID).
			   "')\"><img src=\"/img/button_drop.png\" border=0 alt=\"Drop\"></a>";
		}
      echo "</td>".
			"</tr>\n";
	}
	$rs->Close();

   //
   // Second group of logins
   //
	$query="SELECT T1.*, T2.AGENTID FROM ishop_session_ctl T1
      LEFT JOIN reseller T2 ON (T2.RESELID=T1.ULOGIN)
      WHERE T1.AGENTID=? OR T2.AGENTID=?
      ORDER BY T1.LASTUSAGE ";
   $data=array($x2->agentid, $x2->agentid);
	if (!$rs=$db->Execute($query,$data)) {
		tr_echo("<h6>$query: ".$db->ErrorMsg()."</h6>");
		return;
	}
	while ($o1=$rs->FetchNextObject()) {
      $ar1=explode('#',$o1->ULOGIN);
		if (count($ar1)==1) {
         if ($o1->AGENTID>0 && $o1->AGENTID!=$x2->agentid)
            continue;

		   $reselid=(int)$ar1[0];
		}
		else if (count($ar1)==2) {
         if ((int)$ar1[0]!=$x2->agentid)
            continue;

		   $reselid=(int)$ar1[1];
		}
		else {
		   $reselid=-1;
		}

      echo "<tr";
		if (($nrec++%2)>0)
			echo " class=HILINE";
		echo ">".
		   "<td>".myhtml($o1->APPLI)."</td>".
			"<td>".myhtml($o1->SESSID)."</td>".
			"<td></td>".
			"<td>".myhtml($o1->ULOGIN)."# ";
		if (($re_name=get_re_name($reselid))!==false) {
         echo myhtml($re_name);
      }
      echo "</td>".
			"<td>";
      if ((int)$o1->LASTUSAGE>0)
         echo date('d/m/Y H:i:s',$o1->LASTUSAGE);
      echo "</td>".
		   "<td>";
      if ((int)$o1->LASTUSAGE>0) {
            $dt=time() - $o1->LASTUSAGE;
            if ($dt<60)
               echo $dt." sec";
            else if ($dt<3600)
               echo ((int)($dt/60))." min.";
            else if ($dt<86400)
               echo ((int)($dt/3600))." hours";
            else
               echo ((int)($dt/86400))." days";
      }
		echo "</td>".
			"<td></td>".
			"<td>";
      if (strcmp($o1->APPLI,'BackOffice')==0 && strcmp($o1->SESSID,session_id())==0) {
         echo "<b style=\"color:red;\">Self</b>";
      }
      else {
         echo "<a href=\"javascript:goRecDelete(23,'".addslashes($o1->APPLI.':'.$o1->SESSID)."')\">".
            "<img src=\"/img/button_drop.png\" border=0 alt=\"Drop\"></a>";
      }
      echo "</td>".
			"</tr>\n";
   }
	$rs->Close();
?>
</table>
</form>
<small>End of data</small>
<?php
}
//--------------------------------------
function upd_sms()
{
   global $x2;
   $db=&$x2->db;

   $t1='agent_main';
   $prim=array('AGENTID', 'PNAME');

   $ar1=array('SMS_API_ID', 'SMS_USER', 'SMS_PASSWORD', 'SMS_CA_FULLPATH', 'SMS_COMM_METHOD',
      'SMS_PROXY_SERVER', 'SMS_PROXY_SSL', 'SMS_PROXY_LOGIN', 'SMS_PROXY_PASSWD' );
   foreach ($ar1 as $k1) {
      $data=array('AGENTID'=>$x2->agentid, 'PNAME'=>$k1, 'PVALUE'=>trim((string)@$_REQUEST[$k1]) );
      if (ReplaceEx($db, $t1, $data, $prim)==0)
         echo "<h6>".$db->ErrorMsg()."</h6>";
   }
}
//--------------------------------------
function upd_gmap()
{
   global $x2;
   $db=&$x2->db;

   $t1='agent_main';
   $prim=array('AGENTID', 'PNAME');

   $ar1=array('GMAP_HREF', 'GMAP_WIDTH', 'GMAP_HEIGHT', 'GMPA_DELTASECONDS', 'GMPA_DELTAMETERS', 'GMPA_MINSPEED' );
   foreach ($ar1 as $k1) {
      $data=array('AGENTID'=>$x2->agentid, 'PNAME'=>$k1, 'PVALUE'=>(string)@$_REQUEST[$k1] );
      if (ReplaceEx($db, $t1, $data, $prim)==0)
         echo "<h6>".$db->ErrorMsg()."</h6>";
   }

   $db->StartTrans();

   do {
      $query="DELETE FROM agent_main WHERE AGENTID=? AND PNAME LIKE 'GMAP_TEMPLATE%' ";
      $data=array($x2->agentid);
      if (!$db->Execute($query,$data)) {
         echo "<h6>".$db->ErrorMsg()."</h6>";
         break;
      }

      $query="INSERT INTO agent_main (AGENTID, PNAME, PVALUE) VALUES(?,?,?)";
      $n=strlen($_REQUEST['GMAP_TEMPLATE']);
      for ($i=0;$i<$n;$i+=255) {
         $data=array($x2->agentid, 'GMAP_TEMPLATE'.($i/255), substr($_REQUEST['GMAP_TEMPLATE'],$i,255) );
         if (!$db->Execute($query,$data)) {
            echo "<h6>".$db->ErrorMsg()."</h6>";
            break;
         }
      }
   } while (false);

   $db->CompleteTrans();
}
//--------------------------------------
function request_sms()
{
   global $x2;
   $db=&$x2->db;

   // HEADER
	$tarray=array(
		array("javascript:goTo(100)", 1, 'ctr_center'),
		array('javascript:goTo(20)', 1, 'gen_info'),
		array('', 1, 'SMS')
		);
	$x2->make_header($tarray);

	// BODY
   $ar1=explode('/',DOCUMENT_ROOT);
   array_pop($ar1);
   array_push($ar1, 'internal', 'cacert.pem');
   $capath=implode('/', $ar1);

   $f1=new cFORM('goForm');
	$f1->vars=array( 'FUNCTION'=>'30' );
	$f1->open();

   if (!array_key_exists($k='SMS_COMM_METHOD', $x2->Agp)) {
      $x2->Agp[$k]=2; // default
   }
?>
<table>
<tr>
   <td class=bgLabel>SMS request method</td>
   <td class=bgInput colspan=2>
      <input id=sms_direct type=RADIO name="SMS_COMM_METHOD" <?php if ($x2->Agp['SMS_COMM_METHOD']==0) echo 'CHECKED'; ?> value="0">
         <label for="sms_direct">Direct send</label>
   </td>
</tr>
<tr>
   <td></td>
   <td></td>
   <td class=bgInput>&nbsp;</td>
   <td class=bgLabel>Api Id</td>
   <td class=bgInput><input type=TEXT name="SMS_API_ID" value="<?php echo myhtml(@$x2->Agp['SMS_API_ID']); ?>"></td>
</tr>
<tr>
   <td></td>
   <td></td>
   <td class=bgInput>&nbsp;</td>
   <td class=bgLabel>Username</td>
   <td class=bgInput><input type=TEXT name="SMS_USER" value="<?php echo myhtml(@$x2->Agp['SMS_USER']); ?>"></td>
</tr>
<tr>
   <td></td>
   <td></td>
   <td class=bgInput>&nbsp;</td>
   <td class=bgLabel>Password</td>
   <td class=bgInput><input type=PASSWORD name="SMS_PASSWORD" value="<?php echo myhtml(@$x2->Agp['SMS_PASSWORD']); ?>"></td>
</tr>

<tr>
   <td></td>
   <td class=bgInput colspan=2>
      <input id=sms_master type=RADIO name="SMS_COMM_METHOD" <?php if ($x2->Agp['SMS_COMM_METHOD']==2) echo 'CHECKED'; ?> value="2">
         <label for="sms_master">Master</label>
   </td>
</tr>
<tr>
   <td></td>
   <td></td>
   <td class=bgInput>&nbsp;</td>
</tr>

<tr>
   <td></td>
   <td class=bgInput colspan=2>
      <input id=sms_remote type=RADIO name="SMS_COMM_METHOD" <?php if ($x2->Agp['SMS_COMM_METHOD']==1) echo 'CHECKED'; ?> value="1">
         <label for="sms_remote">Remote proxy</label>
   </td>
</tr>
<tr>
   <td></td>
   <td></td>
   <td class=bgInput>&nbsp;</td>
   <td class=bgLabel>Remote server</td>
   <td class=bgInput><input type=TEXT name="SMS_PROXY_SERVER" value="<?php echo myhtml(@$x2->Agp['SMS_PROXY_SERVER']); ?>">
      Name or IP</td>
</tr>
<tr>
   <td></td>
   <td></td>
   <td class=bgInput>&nbsp;</td>
   <td class=bgLabel>Remote SSL</td>
   <td class=bgInput><input id=proxy_ssl type=CHECKBOX name="SMS_PROXY_SSL" <?php if (!empty($x2->Agp['SMS_PROXY_SSL'])) echo 'CHECKED'; ?> value="1">
      <label for="proxy_ssl">Use 'https://'</label></td>
</tr>
<tr>
   <td></td>
   <td></td>
   <td class=bgInput>&nbsp;</td>
   <td class=bgLabel>Remote login</td>
   <td class=bgInput><input type=TEXT name="SMS_PROXY_LOGIN" value="<?php echo myhtml(@$x2->Agp['SMS_PROXY_LOGIN']); ?>"></td>
</tr>
<tr>
   <td></td>
   <td></td>
   <td class=bgInput>&nbsp;</td>
   <td class=bgLabel>Remote password</td>
   <td class=bgInput><input type=PASSWORD name="SMS_PROXY_PASSWD" value="<?php echo myhtml(@$x2->Agp['SMS_PROXY_PASSWD']); ?>"></td>
</tr>
<tr>
   <td class=bgLabel>Certifcate full pathname</td>
   <td class=bgInput colspan=4><input type=TEXT id=ca_fullpath name="SMS_CA_FULLPATH" value="<?php echo myhtml(@$x2->Agp['SMS_CA_FULLPATH']); ?>" style="width:400px;">
      <button type=button onclick="setDefault()">Default</button>
      </td>
</tr>
<tr>
   <td></td>
   <td class=bgButton colspan=4><input type=submit name="ok" value="<?php $x2->xprint('upd'); ?>"></td>
</tr>
</table>
</form>
<script>
$(function(){
      $('input:text').width(200);
      $('input:password').width(200);
      $('#ca_fullpath').width(400);
   } );
function setDefault()
{
   document.goForm.SMS_CA_FULLPATH.value='<?php echo myjs($capath); ?>';
}
</script>
<?php
}
//--------------------------------------
function request_gmap()
{
   global $x2;
   $db=&$x2->db;

   // HEADER
	$tarray=array(
		array("javascript:goTo(100)", 1, 'ctr_center'),
		array('javascript:goTo(20)', 1, 'gen_info'),
		array('', 1, 'Maps')
		);
	$x2->make_header($tarray);

	// BODY
   $f1=new cFORM('goForm');
	$f1->vars=array( 'FUNCTION'=>'28' );
	$f1->open();
?>
<table>
<tr>
   <td class=bgLabel><?php $x2->xprint('base_url'); ?></td>
   <td class=bgInput><input type=TEXT name="GMAP_HREF" value="<?php echo myhtml(@$x2->Agp['GMAP_HREF']); ?>"></td>
</tr>
<tr>
   <td class=bgLabel><?php $x2->xprint('Map_width'); ?></td>
   <td class=bgInput><input type=TEXT name="GMAP_WIDTH" value="<?php echo myhtml(@$x2->Agp['GMAP_WIDTH']); ?>"></td>
</tr>
<tr>
   <td class=bgLabel><?php $x2->xprint('Map_height'); ?></td>
   <td class=bgInput><input type=TEXT name="GMAP_HEIGHT" value="<?php echo myhtml(@$x2->Agp['GMAP_HEIGHT']); ?>"></td>
</tr>
<tr valign=top>
   <td class=bgLabel>Map Template</td>
   <td class=bgInput><table><tr>
      <td><textarea name="GMAP_TEMPLATE" cols=50 rows=10><?php
         $i=0;
         while (isset($x2->Agp["GMAP_TEMPLATE$i"])) {
            echo myhtml($x2->Agp["GMAP_TEMPLATE$i"]);
            $i++;
         }
      ?></textarea></td>
      <td valign=bottom><i>&lt;MPNAME/&gt;</i> stands for info title,<br>
      while <i>&lt;MPDESC/&gt;</i> stands for info text</td>
      </tr></table>
   </td>
</tr>
<tr>
   <td class=bgLabel>Minimimum time delta</td>
   <td class=bgInput><input type=TEXT name="GMPA_DELTASECONDS" value="<?php echo myhtml(@$x2->Agp['GMPA_DELTASECONDS']); ?>" size=5>
      seconds</td>
</tr>
<tr>
   <td class=bgLabel>Minimimum space delta</td>
   <td class=bgInput><input type=TEXT name="GMPA_DELTAMETERS" value="<?php echo myhtml(@$x2->Agp['GMPA_DELTAMETERS']); ?>" size=5>
      meters</td>
</tr>
<tr>
   <td class=bgLabel>Minimimum speed</td>
   <td class=bgInput><input type=TEXT name="GMPA_MINSPEED" value="<?php echo myhtml(@$x2->Agp['GMPA_MINSPEED']); ?>" size=5>
      km/h</td>
</tr>
<tr>
   <td></td>
   <td class=bgButton><input type=submit name="ok" value="<?php $x2->xprint('upd'); ?>"></td>
</tr>
</table>
</form>
<?php
}
//--------------------------------------
function upd_ordPlus()
{
   global $x2;
   $db=&$x2->db;

   $t1='agent_main';
   $prim=array('AGENTID', 'PNAME');

   $_REQUEST['PRN_Z_WIDTH'] = implode(';',$_REQUEST['PRN_Z_WIDTH_P']);

   $ar1=array('ALLOW12BUTTON', 'DLV_BUTTON_INFO', 'SCANDISTANCE', 'SCANDISTUNIT',
      'POS_ORDVIEW_FILTER', 'HANDLE_TICKETS', 'MIN_PRICE', 'POS_CHARGE_ROOM',
      'ABC_CUSTOMER', 'PRN_Z_WIDTH' );

   // Normalization
   if (toFloat($_REQUEST['MIN_PRICE'])<0.01)
      $_REQUEST['MIN_PRICE']=0.01;

   foreach ($ar1 as $k1) {
      $data=array('AGENTID'=>$x2->agentid, 'PNAME'=>$k1, 'PVALUE'=>(string)@$_REQUEST[$k1] );
      if (ReplaceEx($db, $t1, $data, $prim)==0)
         echo "<h6>".$db->ErrorMsg()."</h6>";
   }

   // Complex update: TAKEAWAY_CUSTOMER
   do {
      $tac=trim($_REQUEST['TAKEAWAY_CUSTOMER']);
      $data=array('AGENTID'=>$x2->agentid, 'PNAME'=>'TAKEAWAY_CUSTOMER', 'PVALUE'=>$tac );
      if (ReplaceEx($db, $t1, $data, $prim)==0) {
         echo "<h6>".$db->ErrorMsg()."</h6>";
         break;
      }
      if (empty($tac))
         break;

      // Should not be a 'table'
      $t1='ishop_mbr';
      $prim=array('AGENTID', 'LOGIN');
      $data=array('AGENTID'=>$x2->agentid, 'LOGIN'=>$tac, 'COMPANY'=>'Take Away', 'EMAIL'=>'' );
      $data['TRKEY']=ToEnglish( $tac." ".$data['COMPANY'] );
      if (ReplaceEx($db, $t1, $data, $prim)==0) {
         echo "<h6>".$db->ErrorMsg()."</h6>";
         break;
      }
   } while (false);
}
//--------------------------------------
function request_ordPlus()
{
   global $x2;
   $db=&$x2->db;

   // HEADER
	$tarray=array(
		array("javascript:goTo(100)", 1, 'ctr_center'),
		array('javascript:goTo(20)', 1, 'gen_info'),
		array('', 0, @$x2->operTypes[$x2->opetype] )
		);
	$x2->make_header($tarray);

	// BODY
   $f1=new cFORM('goForm');
	$f1->vars=array( 'FUNCTION'=>'26' );
	$f1->open();
?>
<table>
<tr>
   <td class=bgLabel>1/2 button</td>
   <td class=bgInput><input type=CHECKBOX name="ALLOW12BUTTON" <?php if ((int)@$x2->Agp['ALLOW12BUTTON']>0) echo "CHECKED"; ?> value="1"></td>
</tr>
<tr>
   <td class=bgLabel><?php $x2->xprint('dlv_button_info'); ?></td>
   <td class=bgInput><select name="DLV_BUTTON_INFO"><?php
      $ar1=array('Customer code', 'Customer name', 'Order Id');
      prAssocSelErr($ar1,(int)@$x2->Agp['DLV_BUTTON_INFO']);
      ?></select></td>
</tr>
<tr>
   <td class=bgLabel><?php $x2->xprint('takeaway_customer'); ?></td>
   <td class=bgInput><input type=TEXT name="TAKEAWAY_CUSTOMER" value="<?php echo myhtml((string)@$x2->Agp['TAKEAWAY_CUSTOMER']); ?>"></td>
</tr>
<tr>
   <td class=bgLabel><?php $x2->xprint('scan_distance'); ?></td>
   <td class=bgInput>
      <input type=TEXT name="SCANDISTANCE" value="<?php
         echo empty($x2->Agp['SCANDISTANCE'])?10:(int)$x2->Agp['SCANDISTANCE'];
      ?>" size=10>
      <select name="SCANDISTUNIT"><?php
      $ar1=array('SECOND'=>'Seconds', 'MINUTE'=>'Minutes', 'HOUR'=>'Hours' );
      prAssocSelErr( $ar1, empty($x2->Agp['SCANDISTUNIT'])?'MINUTE':$x2->Agp['SCANDISTUNIT'] );
      ?></select>
   </td>
</tr>
<tr>
   <td class=bgLabel><?php $x2->xprint('review_orders'); ?></td>
   <td class=bgInput><select name="POS_ORDVIEW_FILTER"><?php
      $ar1=array('All', 'Operators only');
      prAssocSelErr( $ar1, (int)@$x2->Agp['POS_ORDVIEW_FILTER'] );
   ?></select></td>
</tr>
<tr>
   <td class=bgLabel><?php $x2->xprint('handle_tickets'); ?></td>
   <td class=bgInput><select name="HANDLE_TICKETS"><?php
      $ar1=array('As money', 'As discount');
      prAssocSelErr( $ar1, (int)@$x2->Agp['HANDLE_TICKETS'] );
   ?></select></td>
</tr>
<tr>
   <td class=bgLabel><?php $x2->xprint('min_item_price'); ?></td>
   <td class=bgInput><input type=TEXT name="MIN_PRICE" value="<?php
      echo isset($x2->Agp['MIN_PRICE'])?toFloat($x2->Agp['MIN_PRICE']):"0.50";
   ?>" size=10></td>
</tr>
<tr>
   <td class=bgLabel><?php $x2->xprint('charge_rooms'); ?></td>
   <td class=bgInput><input type=CHECKBOX name="POS_CHARGE_ROOM" <?php if ((int)@$x2->Agp['POS_CHARGE_ROOM']>0) echo "CHECKED"; ?> value="1"></td>
</tr>
<tr>
   <td class=bgLabel><?php $x2->xprint('abc_customer'); ?></td>
   <td class=bgInput><input type=CHECKBOX name="ABC_CUSTOMER" <?php if ((int)@$x2->Agp['ABC_CUSTOMER']>0) echo "CHECKED"; ?> value="1"></td>
</tr>
<tr>
   <td class=bgLabel><?php $x2->xprint('prn_z_width'); ?></td>
   <td class=bgInput><?php
      $ar1=explode(';', (string)$x2->Agp['PRN_Z_WIDTH']);
      for ($i=0;$i<4;$i++) {
         echo "<input type=TEXT name=\"PRN_Z_WIDTH_P[]\" value=\"".((int)@$ar1[$i])."\" size=3>";
      }
      $x2->xprint('chars');
   ?></td>
</tr>
<tr>
   <td></td>
   <td class=bgButton><input type=submit name="ok" value="<?php $x2->xprint('upd'); ?>"></td>
</tr>
</table>
</form>
<?php
}
//--------------------------------------
function request_generic()
{
 	global $x2;
   $db=&$x2->db;

 	$currency=array( '&euro;' => 'Euro',
		'US&#36;' => 'U.S. Dollar',
		'ATS' => 'Austrian Schilling',
		'BEF' => 'Belgian Franc',
		'CYP' => 'Cyprus Pound',
		'DKK' => 'Danish Krone',
		'NLG' => 'Dutch Guilder',
		'FIM' => 'Finnish Mark',
		'FRF' => 'French Franc',
		'DEM' => 'German Mark',
		'GBP' => 'Great Britain Pound',
		'IEP' => 'Irish Punt',
		'ITL' => 'Italian Lira',
		'NOK' => 'Norwegian Krone',
		'PTE' => 'Portuguese Escudo',
		'ESP' => 'Spanish Peseta',
		'SEK' => 'Swedish Krona',
		'CHF' => 'Swiss Frank',
		'GRD' => 'Greek Drahma' );

	$curre2nd=array( "-" => "Without" ) + $currency;

   $separator=array( ' ' => 'no separator',
		'.' => 'dot (.)',
		',' => 'comma (,)',
		'_' => 'underscore (_)',
		'&nbsp;' => 'space' );

	$curr2rel=array( 0 => '*', 1 => '/' );

	$facelook=array( 0=>'Green', 1=>'Brown', 2=>'Grass', 3=>'Gray scale' );

	$noimage=array( 0=>'NoImage.gif', 1=>'Clear.gif', 2=>'Not found #', 3=>'Not availble (v2)' );

	// HEADER
	$tarray=array(
		array("javascript:goTo(100)", 1, 'ctr_center'),
		array('', 1, 'gen_info')
		);
	$x2->make_header($tarray);

	// BODY
?>
<table border=0 width=100%>
<tr class=bgButton2>
	 <td><b>
	<?php
   if ($x2->opetype!=5 && $x2->opetype!=7) { ?>
      <a href="javascript:goFX('ishop_pay.php',24)"><?php $x2->xprint('ccards'); ?></a> |
	   <a href="javascript:goFX('ishop_fpos.php',26)"><?php $x2->xprint('file_pos'); ?></a> |
	   <a href="javascript:goFX('ishop_ups.php',10);"><?php $x2->xprint('ups_rules'); ?></a> |
	   <a href="javascript:goFX('ishop_vat.php',22);"><?php $x2->xprint('vat_mai'); ?></a> |
	   <a href="javascript:goFX('ishop_stacc.php',1);"><?php $x2->xprint('stocking'); ?></a> |
	   <a href="javascript:goTo(25)"><?php echo @$x2->operTypes[$x2->opetype]; ?></a> |
	   <a href="javascript:goFX('ishop_cart.php',31)">Cart</a> |
	<?php } ?>
	   <a href="javascript:goTo(29)">SMS</a> |
	   <a href="javascript:goTo(27)"><?php $x2->xprint('maps'); ?></a> |
	   <a href="javascript:goTo(49)"><?php $x2->xprint('all_properties'); ?></a> |
	   <a href="javascript:goTo(31)">PHP config</a>
   </b></td>
</tr>
</table>
<p></p>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="goForm">
<input type=hidden name="FUNCTION" value="21">
<table>
<tr>
   <td class=bgLabel>Τίτλος επικεφαλίδας</td>
   <td class=bgInput><input type=text name="TITLE" value="<?php echo myhtml(@$x2->Agp['TITLE']); ?>" size=40 maxLength=40></td>
</tr>
<tr>
   <td class=bgLabel>Main URL</td>
   <td class=bgInput><input type=text name="HREF" value="<?php echo myhtml(@$x2->Agp['HREF']); ?>" size=40 maxLength=128></td>
</tr>
<tr>
   <td class=bgLabel>Agent Email</td>
   <td class=bgInput><input type=text name="EMAIL" value="<?php echo myhtml(@$x2->Agp['EMAIL']); ?>" size=40 maxLength=80>
      <small>Comma separated list</small></td>
</tr>
<tr>
   <td class=bgLabel>Email From</td>
   <td class=bgInput><input type=text name="EMAILFROM" value="<?php echo myhtml(@$x2->Agp['EMAILFROM']); ?>" size=40 maxLength=80>
      </td>
</tr>
<tr>
   <td class=bgLabel>SMTP server</td>
   <td class=bgInput><input type=text name="SMTPSERVER" value="<?php echo myhtml(@$x2->Agp['SMTPSERVER']); ?>" size=40 maxLength=80>
      </td>
</tr>
<tr>
   <td class=bgLabel>SSL</td>
   <td class=bgInput><input type=CHECKBOX <?php if (!empty($x2->Agp['SSLAVAIL'])) echo 'CHECKED'; ?> name="SSLAVAIL" value="1"> <?php $x2->xprint('yes'); ?>
      <b>Port:</b><input type=TEXT name="SSLPORT" value="<?php echo myhtml(@$x2->Agp['SSLPORT']); ?>" size=5 maxlength=5>(if not default)</td>
</tr>
<tr>
   <td class=bgLabel><?php $x2->xprint('nomisma'); ?></td>
   <td class=bgInput><SELECT name="CURRENCY"><?php
	   prAssocSelect($currency,@$x2->Agp['CURRENCY']);
	?></SELECT>
   <b>2nd:</b><SELECT name="CURRE2ND">
   <?php
	   prAssocSelect($curre2nd,@$x2->Agp['CURRE2ND']);
	?></SELECT>
   <b>=1st</b><SELECT name="CURR2REL">
	<?php prAssocSelect($curr2rel,@$x2->Agp['CURR2REL']); ?>
	</SELECT>
	<input type=TEXT name="CURR2VAL" value="<?php echo myhtml(@$x2->Agp['CURR2VAL']); ?>"
			maxlength=10 size=5>
   </td>
</tr>
<tr>
   <td class=bgLabel><?php $x2->xprint('decimals'); ?></td>
   <td class=bgInput><input type=text name="DECIMAL"
	 value="<?php echo myhtml(@$x2->Agp['DECIMAL']); ?>" size=3 maxLength=1>
    <b>2nd:</b><input type=text name="DECI2ND"
 	 value="<?php echo myhtml(@$x2->Agp['DECI2ND']); ?>" size=3 maxLength=1></td>
</tr>
<tr>
   <td class=bgLabel><?php $x2->xprint('separator'); echo ' ('; $x2->xprint('thousands'); ?>):</td>
   <td class=bgInput><select name="THOUSANDSEP">
   <?php prAssocSelect($separator,@$x2->Agp['THOUSANDSEP']); ?>
   </select> <b>(<?php $x2->xprint('decimals'); ?>):</b><select name="DECIMALSEP">
   <?php prAssocSelect($separator,@$x2->Agp['DECIMALSEP']); ?>
   </select></td>
</tr>
<tr>
	 <td class=bgLabel>No image</td>
	 <td class=bgInput><SELECT name="NOIMAGE">
	 	  <?php prAssocSelect($noimage,@$x2->Agp['NOIMAGE']); ?>
		  </SELECT></td>
</tr>
<tr>
   <td class=bgLabel>EAFDSS HOST</td>
   <td class=bgInput><input type=text name="EAFDSSHOST" value="<?php echo myhtml(@$x2->Agp['EAFDSSHOST']); ?>" size=40 maxLength=80></td>
</tr>
<tr>
   <td class=bgLabel>EAFDSS Id</td>
   <td class=bgInput><input type=text name="EAFDSSID" value="<?php echo myhtml(@$x2->Agp['EAFDSSID']); ?>"
      maxLength=80>
      <b>Timeout:</b><input type=text name="EAFDSSTIMEOUT" value="<?php
      echo myhtml(@$x2->Agp['EAFDSSTIMEOUT']); ?>" size=3 maxLength=8>seconds</td>
</tr>
<tr>
   <td class=bgLabel>Local timezone</td>
   <td class=bgInput><select name="TIMEZONE"><?php
      $ar1=DateTimeZone::listIdentifiers ();
      prAssocSelect($ar1, (string)@$x2->Agp['TIMEZONE'], 1); // use only 'VALUE' member of array
      ?></select></td>
</tr>
<tr>
	 <td></td>
	 <td class=bgButton>
	 	<input type=reset name="reset" value="<?php $x2->xprint('reset'); ?>">
		<input type=submit name="ok" value="<?php $x2->xprint('upd'); ?>"></td>
</tr>
</table>
</form>
<?php
}
//--------------------------------------
function m1_logout()
{
   global $x2;
   $db=&$x2->db;

	$query="DELETE FROM ishop_session_ctl WHERE APPLI='BackOffice' AND SESSID=?";
	$data=array( session_id() );
	if (!$db->Execute($query,$data)) {
		echo "<h6>".$db->ErrorMsg()."</h6>\n";
		return;
	}

 	umessage("Successfully logged out!");
}
//--------------------------------------
function add_agent(&$db)
{
   $max=0;
   $query="SELECT MAX(AGENTID) FROM agents";
   if (($rs=$db->Execute($query))===false) {
		echo "<h6>$query: ".$db->ErrorMsg()."</h6>\n";
		return;
	}
	if ($row=$rs->FetchRow()) {
   	$max=(int)$row[0];
	}
	$rs->Close();

 	$query="INSERT INTO agents (AGENTID,LPATH,PASSWD) VALUES(?,?,'1234')";
 	$data=array( $max+1, $_REQUEST['LPATH'] );
 	if (!$db->Execute($query,$data)) {
		echo "<h6>$query: ".$db->ErrorMsg()."</h6>\n";
		return;
	}

	$query="SELECT AGENTID FROM agents WHERE LPATH=?";
	$data=array( $_REQUEST['LPATH'] );
   if (($rs=$db->Execute($query,$data))===false) {
		echo "<h6>".$db->ErrorMsg()."</h6>\n";
		return;
	}
   $o1=$rs->FetchNextObject();
   $rs->Close();
   if ($o1===false) {
		echo "<h6>Could not locate new record!</h6>";
		return;
	}

	if (empty($_REQUEST['LPATH'])) {
      $_REQUEST['LPATH']="New agent ".$o1->AGENTID;

      $query="UPDATE agents SET LPATH=? WHERE AGENTID=?";
      $data=array($_REQUEST['LPATH'], $o1->AGENTID);
      if (!$db->Execute($query,$data)) {
         echo "<h6>".$db->ErrorMsg()."</h6>\n";
         return;
      }
   }

   $query="INSERT INTO agent_main (AGENTID, PNAME, PVALUE) VALUES (?,?,?)";
	$props=array(
		'VATDATA' => '13 23 36 6.5 9 16 36 5',
		'VATDES1EN' => 'Greek generic',
      'VATDES2EN' => 'Greek reduced (Greek Islands, Borderland)',
      'VATOFMERCH' => 2,
      'DECIMALSEP' => '1',
      'XTRA_AUTOCOPY' => '1',
      'MLANGPREF'=>'1', // 1=Greek
      'KEEP_PRPRE'=>'1' // days to keep printouts
	);
	foreach ($props as $k=>$v) {
		$data=array($o1->AGENTID, $k, $v);
		if (!$db->Execute($query,$data)) {
			echo "<h6>$query: ".$db->ErrorMsg()."</h6>\n";
			break;
		}
	}

	// Add reseller
	$query="INSERT INTO reseller (AGENTID, PASSWD, RE_NAME) VALUES(?,?,?)";
	$data=array($o1->AGENTID, '1', 'New salesman');
	if (!$db->Execute($query,$data)) {
		echo "<h6>$query: ".$db->ErrorMsg()."</h6>\n";
		return;
	}

	if (($reselid=(int)$db->Insert_ID())==0) {
		return;
	}
	else {
		echo "<script language=javascript>window.alert('A new salesman with id ".$reselid." has been added.')</script>";
	}

	$t1='reselpref';
	$prim=array('RESELID', 'PNAME');
	$data=array('RESELID'=>$reselid, 'PNAME'=>'DESKTOP_LOGIN', 'PVALUE'=>1);
	if (!ReplaceEx($db, $t1, $data, $prim))
		echo "<h6>".$db->ErrorMsg()."</h6>\n";
}
//--------------------------------------
function del_lex()
{
   global $x2;
   $db=&$x2->db;

	$query="DELETE FROM ishop_lang WHERE SHORTHAND=?";
	$data=array($_REQUEST['RECORDID']);
	if (!$db->Execute($query,$data))
		echo "<h6>".$db->ErrorMsg()."</h6>\n";
}
//--------------------------------------
function remake_lex_mysql($all_lang_words)
{
   global $x2;
   $db=&$x2->db;

   $query="REPLACE INTO ishop_langdic (SHORTHAND, LANGID, WLABEL) VALUES";
   $data=array();
   $comma=false;
   foreach ($all_lang_words as $ar1) {
		if (!is_array($ar1))
			continue;

		if (!$comma)
		   $comma=true;
		else
		   $query .= ", ";

	   $query .= "(?,?,?)";
	   array_push($data, $ar1[0], 0, $ar1[1]);

      $n=count($ar1);
      for ($i=2;$i<$n;$i++) {
			if (empty($ar1[$i]))
				continue;

         $query .= ", (?,?,?)";
         array_push($data, $ar1[0], $i-1, $ar1[$i] );
      }
	}
	if (!$db->Execute($query,$data)) {
      echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>\n";
      return;
   }

   /*
   $query="SELECT * FROM ishop_langdic";
   if (($rs=$db->Execute($query))===false) {
      echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>\n";
      return;
   }
   echo "<table>";
   while ($row=$rs->FetchRow()) {
      echo "<r>";
      $n=count($row);
      for ($i=0;$i<$n;$i++) {
         echo "<td>".myhtml($row[$i])."</td>";
      }
      echo "</tr>";
   }
   $rs->Close();
   echo "<table>";
   */
}
//--------------------------------------
function remake_lex_generic($all_lang_words)
{
   global $x2;
   $db=&$x2->db;

   $t1='ishop_langdic';
   $prim=array('LANGID', 'SHORTHAND');

   foreach ($all_lang_words as $ar1) {
		if (!is_array($ar1))
			continue;

		$data=array('SHORTHAND'=>$ar1[0], 'LANGID'=>0, 'WLABEL'=>$ar1[1] );
		if (!ReplaceEx($db,$t1,$data,$prim))
			echo "<h6>".$db->ErrorMsg()."</h6>\n";

      $n=count($ar1);
      for ($i=2;$i<$n;$i++) {
			if (empty($ar1[$i]))
				continue;

         $data=array('SHORTHAND'=>$ar1[0], 'LANGID'=>($i-1), 'WLABEL'=>$ar1[$i] );
         if (!ReplaceEx($db,$t1,$data,$prim))
           	echo "<h6>".$db->ErrorMsg()."</h6>\n";
      }
   }
}
//--------------------------------------
function remake_lex()
{
   global $x2;
   $db=&$x2->db;

   // Load messages
	@include('messages.php');

	do {
		if (!isset($all_lang_words))
			break;

		if (is_mysql($db))
		   remake_lex_mysql($all_lang_words);
		else // mssql, oci8
		   remake_lex_generic($all_lang_words);

   } while (false);

   // Clear entries that are o.k. for both languages 0+1 (select: join one to one)
   $query="SELECT T1.SHORTHAND FROM ishop_lang T1 ".
      " INNER JOIN ishop_langdic T2 ON (T2.SHORTHAND=T1.SHORTHAND AND T2.LANGID=0) ".
      " INNER JOIN ishop_langdic T3 ON (T3.SHORTHAND=T1.SHORTHAND AND T3.LANGID=1) ";
   $qdel="DELETE FROM ishop_lang WHERE SHORTHAND=?";
   if ($rs=$db->Execute($query)) {
      while ($row=$rs->FetchRow()) {
         $db->Execute($qdel,$row);
      }
      $rs->Close();
   }
   else {
   	echo "<h6>".$db->ErrorMsg()."</h6>\n";
   }
}
//--------------------------------------
function add_langui()
{
   global $x2;
   $db=&$x2->db;

	$query="INSERT INTO ishop_langui (LANGID, LANGDES) VALUES(?,?)";
   $data=array( $_REQUEST['LANGID'], $_REQUEST['LANGDES'] );
   if (!$db->Execute($query,$data))
   	echo "<h6>".$db->ErrorMsg()."</h6>\n";
}
//--------------------------------------
function del_langui()
{
   global $x2;
   $db=&$x2->db;

	$query="DELETE FROM ishop_langui WHERE LANGID=?";
   $data=array( $_REQUEST['RECORDID'] );
   if (!$db->Execute($query,$data))
   	echo "<h6>".$db->ErrorMsg()."</h6>\n";
}
//--------------------------------------
function list_langui(&$x2)
{
	$db=&$x2->db;

   // HEADER
	$tarray=array(
		array('ADMIN', 0, 'Main'),
		array('javascript:goTo(503)', 0, 'Lexicon'),
		array('', 0, 'Languages')
		);
	$x2->make_header($tarray);

	$query="SELECT * FROM ishop_langui ORDER BY LANGID";
	if (($rs=$db->Execute($query))===false) {
		echo "<h6>".$db->ErrorMsg()."</h6>\n";
		return;
	}
?>
<form method="post" name="goForm" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type=hidden name="FUNCTION" value="531">
<input type=hidden name="RECORDID">
<table>
<tr class=bgHeader>
	<td>Language Id</td>
	<td>Description</td>
</tr>
<?php
	$nrec=0;
	while ($o1=$rs->FetchNextObject()) {
		echo "<tr";
		if ($nrec++%2>0) echo " class=HILINE";
		echo ">".
			"<td>".$o1->LANGID."</td>".
			"<td>".myhtml($o1->LANGDES)."</td>";
		if ($o1->LANGID>=100) {
			echo "<td><a href=\"javascript:goRecDelete(532,'".$o1->LANGID."')\"><img ".
				" src=\"/img/button_drop.png\" border=0></a></td>";
		}
		echo "</tr>\n";
	}
	$rs->Close();
?>
<tr class=bgInput>
	<td>New <select name="LANGID"><?php
     	prSelect(201,220);
	?></select></td>
	<td><input type=TEXT name="LANGDES"></td>
</tr>
<tr class=bgButton>
	<td colspan=2><input type=SUBMIT name="ok" value="Update"></td>
</tr>
</table>
</form>
<?php
}
//--------------------------------------
function show_lex_pending()
{
   global $x2;
   $db=&$x2->db;

   // HEADER
	$tarray=array(
		array('ADMIN', 0, 'Main'),
		array('javascript:goTo(503)', 0, 'Lexicon (refresh)')
		);
	$x2->make_header($tarray);

   // CLEANUP
   $query="SELECT T1.SHORTHAND FROM ishop_lang T1
      LEFT JOIN ishop_langdic T2 ON (T2.SHORTHAND=T1.SHORTHAND)
      WHERE T2.SHORTHAND IS NOT NULL";
   if (($rs=$db->Execute($query))===false) {
      echo "<h6>".$db->ErrorMsg()."</h6>\n";
      return;
   }
   while ($row=$rs->FetchRow()) {
      echo $row[0]."<br>";
   }
   $rs->Close();

   // BODY
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST name="goForm">
<input type=hidden name="FUNCTION">
<input type=hidden name="RECORDID">
<table>
<tr>
   <td class=bgLabel><b>Pending</b></td>
   <td><table>
<?php
	$query="SELECT * FROM ishop_lang ORDER BY SHORTHAND";
	if ($rs=$db->Execute($query)) {
      $nrec=0;
		while ($o1=$rs->FetchNextObject()) {
			echo "<tr";
			if ($nrec++%2) echo " class=HILINE";
         echo ">\n".
            "<td>".myhtml($o1->SHORTHAND)."</td>\n".
            "<td>".myhtml($o1->LASTQUERY)."</td>\n".
            "<td>".myhtml($o1->PROGNAME)."</td>\n".
            "<td><a href=\"javascript:goRecDelete(507,'".myhtml($o1->SHORTHAND).
			   "')\"><img src=\"/img/button_drop.png\" border=0 alt=\"Drop\"></a></td>\n".
            "</tr>";
      }
		$rs->Close();
		if ($nrec==0)
		   tr_echo("No records in file!");
	}
	else {
      echo "<h6>".$db->ErrorMsg()."</h6>\n";
   }
?>
   </table></td>
</tr>
<tr>
	<td></td>
	<td class=bgButton>
		<input type=BUTTON name="la" value="Languages" onclick="goTo(530)">
		<input type=BUTTON name="co" value="Contents" onclick="goTo(529)">
		Textout file:<input type=TEXT name="TEXTOUT">
		<button type=button onclick="goTo(508)"><img src="to_right.gif"></button>
	</td>
</tr>
</table>
</form>
<?php
}
//--------------------------------------
function update_lexicon()
{
   global $x2;
	$db=&$x2->db;

	$prim=array('SHORTHAND', 'LANGID');
	$data=array( 'WLABEL'=>trim($_REQUEST['WLABEL']),
		'SHORTHAND'=>$_REQUEST['SHORTHAND'], 'LANGID'=>$_REQUEST['LANGID']);
	if (!ReplaceEx($db,'ishop_langdic',$data,$prim))
		echo "<h6>".$db->ErrorMsg()."</h6>\n";
}
//--------------------------------------
function show_lex_content(&$x2)
{
	$db=&$x2->db;

   // HEADER
	$tarray=array(
		array('ADMIN', 0, 'Main'),
		array('javascript:goTo(503)', 0, 'Lexicon'),
		array('javascript:goTo(529)', 0, 'Contents(r)'),
		);
	$x2->make_header($tarray);

	$query="SELECT * FROM ishop_langui ORDER BY LANGID";
	if (($rs=$db->Execute($query))===false) {
		echo "<h6>".$db->ErrorMsg()."</h6>\n";
		return;
	}
	$langs=array();
	while ($o1=$rs->FetchNextObject()) {
		$langs[]=$o1;
	}
	$rs->Close();

	$pg=getPage();
	$pgsz=getPageSize('grid1');

	$query="SELECT T0.SHORTHAND, T1.WLABEL W1, T2.WLABEL W2, T3.WLABEL W3, T4.WLABEL W4 FROM ishop_langdic T0
		   LEFT JOIN ishop_langdic T1 ON (T1.SHORTHAND=T0.SHORTHAND AND T1.LANGID=0)
         LEFT JOIN ishop_langdic T2 ON (T2.SHORTHAND=T0.SHORTHAND AND T2.LANGID=1)
         LEFT JOIN ishop_langdic T3 ON (T3.SHORTHAND=T0.SHORTHAND AND T3.LANGID=2)
         LEFT JOIN ishop_langdic T4 ON (T4.SHORTHAND=T0.SHORTHAND AND T4.LANGID=3)
         GROUP BY T1.SHORTHAND
         ORDER BY T1.SHORTHAND";

   if (!$rs=$db->PageExecute($query,$pgsz,$pg)) {
		echo "<h6>".$db->ErrorMsg()."</h6>\n";
		return;
	}

	// BODY
?>
<script language=JavaScript>
var g_obj=null;
var g_row=null;
var g_str=null;
var g_key=null;
var g_i=null;

var langid=[ <?php
   $n=count($langs);
   for ($i=0;$i<$n;$i++) {
	  if ($i>0) echo ', ';
	  echo $langs[$i]->LANGID;
   }
   echo "];\n";
?>

function oclick(stage,row,col)
{
   if ((g_i=col)==0)
      return false;

   g_obj=this;
   g_row = row;
   g_str = this.cells(g_row, g_i).getValue();
   g_key = this.cells(g_row, 0).getValue();

   if (g_i==1 && g_str.length>0) { // English
      show_popup(document.getElementById('popmenu'));
   }
   else {
      process_click();
   }

   return false;
}
function process_click()
{
	if (g_i == 0)
		return;

   var s2=null;

   if (g_i==2) { // Greek
      if (g_str.length>0) {
         if (window.confirm('Μετάφραση Ελληνικά ΣΕ Αγγλικά?')) {
            var url='http://babelfish.altavista.com/tr?trtext='+escape(g_str)+'&lp=el_en';
            var owin=window.open(url,'babelfish');
            owin.focus();
            g_i=2; // English
            g_str = this.getDataProperty("text", g_row, g_i);
         }
      }
   }

   // Default action:
   var label=null;

   switch (g_i)
   {
   case 1:
      label='English';
      break;
   case 2:
      label='Greek';
      break;
   case 3:
      label='German';
      break;
   case 4:
      label='Spanish';
      break;
   default:
      label='lang #'+i;
      break;
   }

   var s2=window.prompt('New '+label+' value for '+g_key, g_str);

   if (s2!=null)
      process_answer(s2);
}
function process_answer(s2)
{
	var f1=document.goForm;
	f1.SHORTHAND.value = g_key;
	f1.LANGID.value = g_i - 1;
	f1.WLABEL.value = s2;
	f1.submit();
}
var t1_t;
var t1_id;
var g_owin;
var oPopup = window.createPopup();
oPopup.document.body.innerHTML='<h5>Please wait...</h5>';
function get_data()
{
   try {
      if (g_owin.document.readyState=='loaded' || g_owin.document.readyState=='complete') {
         window.clearInterval(t1_id);
         process_answer(g_owin.document.getElementById('result').innerText);
         g_owin.close();
      }
      else {
         var t2=new Date();
         if ((t2.getTime() - t1_t.getTime()) > 60000)
            window.clearInterval(t1_id);
      }
   }
   catch (e)  {
      window.clearInterval(t1_id);
   }
}
function winRet(x)
{
	switch (x)
	{
   case 1:
      showHide(document.getElementById('popmenu'),0);
      process_click();
      break;
	case 2: // English to German
	   var url='http://babelfish.altavista.com/tr?trtext='+escape(g_str)+'&lp=en_de';
      g_owin=window.open(url,'babelfish');
      window.focus();
      g_i=3; // German
      g_str = g_obj.cells(g_row, g_i).getValue();
      //process_click();
      t1_t=new Date();
      t1_id=window.setInterval(get_data,500);
      oPopup.show(0,0,150,50,document.body);
	   break;
	case 3: // English to Spanish
	   var url='http://babelfish.altavista.com/tr?trtext='+escape(g_str)+'&lp=en_es';
      g_owin=window.open(url,'babelfish');
      window.focus();
      g_i=4; // Spanish
      g_str = g_obj.cells(g_row, g_i).getValue();
      //process_click();
      t1_t=new Date();
      t1_id=window.setInterval(get_data,500);
      oPopup.show(0,0,150,50,document.body);
	   break;
	}
}
</script>
<?php
   $f1=new cFORM('goForm');
   $f1->vars=array( 'FUNCTION'=>'533', 'SHORTHAND'=>'', 'LANGID'=>'', 'WLABEL'=>'' );
   $f1->run();

	$gr1=new cXRID('grid1');

   array_push($gr1->cols,
		array( 'Shorthand', 120) );

	foreach ($langs as $o1) {
		array_push($gr1->cols,
			array( $o1->LANGDES, 150) );
	}

	$n=count($langs);
	while ($o2=$rs->FetchNextObject()) {
      $row1=array();

      $row1[]=myhtml($o2->SHORTHAND);
      $row1[]=myhtml($o2->W1);
      $row1[]=myhtml($o2->W2);
      $row1[]=myhtml($o2->W3);
      $row1[]=myhtml($o2->W4);

      $gr1->rows[]=$row1;
	}
	$epage=$rs->LastPageNo();
	$rs->Close();

	// Execute
	$gr1->click='oclick';
   $gr1->play();
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST name="F2">
<input type=hidden name="FUNCTION" value="535">
<div id=tbar2>
	<?php print_page_navEx($pg,$epage,529); ?>&nbsp;Empty data
		<select name="LANGID"><?php
			foreach ($langs as $o1) {
				if ($o1->LANGID<2)
					continue;

				echo "<option value=\"".$o1->LANGID."\">".myhtml($o1->LANGDES)."\n";
			}
		?></select>
		<input type=submit name="ok" value="Export">
		<input type=submit name="FILTER" value="List">
</div>
</form>
<?php
   $selx1=array(
      array(1, '/img/button_edit.png', 'Edit English' ),
      '<hr>',
      array(2, '/img/go_next.gif', 'Translate English to German' ),
      array(3, '/img/go_next.gif', 'Translate English to Spanish' )
   );
	draw_popup($selx1,false);
}
//--------------------------------------
function export_lex_nodata()
{
	global $x2;
	$db=&$x2->db;

   // HEADER
	$tarray=array(
		array('ADMIN', 0, 'Main'),
		array('javascript:goTo(503)', 0, 'Lexicon'),
		array('javascript:goTo(529)', 0, 'Contents'),
		array('', 0, 'Export empty data')
		);
	$x2->make_header($tarray);

	if (isset($_REQUEST['FILTER']))
		return show_lex_content($x2);

	$query="SELECT T1.WLABEL FROM ishop_langdic T1 ".
		" LEFT JOIN ishop_langdic T2 ON (T2.SHORTHAND=T1.SHORTHAND AND T2.LANGID=?) ".
		" WHERE T1.LANGID=0 AND (T2.WLABEL='' OR T2.WLABEL IS NULL) ".
		" ORDER BY T1.SHORTHAND";
	$data=array($_REQUEST['LANGID']);
	if (($rs=$db->Execute($query,$data))===false) {
		echo "<h6>".$db->ErrorMsg()."</h6>\n";
		return;
	}
	echo "<table>\n";
	while ($o1=$rs->FetchNextObject()) {
		echo "<tr>".
			"<td>".myhtml($o1->WLABEL).".</td>".
			"</tr>\n";
	}
	$rs->Close();
	echo "</table>\n";
}
//--------------------------------------
function apply_myhtml(&$v,$k)
{
   $v=myhtml($v);
}
//--------------------------------------
function do_db_query()
{
   global $x2;
   $db=&$x2->db;

   $tarray=array(
		array('ADMIN', 0, 'Main'),
		array('javascript:goTo(581)', 0, 'DB Query'),
		array('', 0, 'Results')
		);
	$x2->make_header($tarray);

	// BODY
	$pg=getPage();
	$pgsz=getPageSize('grid20');

	if (!empty($_REQUEST['QRY']))
	   $_SESSION['QRY']=$_REQUEST['QRY'];

   $query=$_SESSION['QRY'];
   $ar1=explode(' ',$query);
   if (count($ar1)<2) {
      pre($query);
      echo "<h6>Incorrect query!</h6>";
      return ask_db_query();
   }

   if (strcasecmp($ar1[0],"SELECT")==0 || strcasecmp($ar1[0],"EXPLAIN")==0 || strcasecmp($ar1[0],"SHOW")==0 ) {
	   if (($rs=$db->PageExecute($query,$pgsz,$pg))===false) {
         echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>";
         return ask_db_query();
      }

      $gr1=new cXRID('grid20');

      $n=$rs->FieldCount();
	   for ($i=0;$i<$n;$i++) {
		   $fld=$rs->FetchField($i);
		   $gr1->cols[]=array($fld->name,100);
	   }

      while ($row=$rs->FetchRow()) {
         array_walk($row,'apply_myhtml');
         $gr1->rows[]=$row;
      }
      $epage=$rs->LastPageNo();
      $rs->Close();

      $gr1->play();

      echo "<div id=tbar2>";
      print_page_navEx($pg,$epage,582);
      echo "</div>";
   }
   else if (strcasecmp($ar1[0],"SHOW")==0) {
	   if (($rs=$db->Execute($query))===false) {
         echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>";
         return ask_db_query();
      }

      $gr1=new cXRID('grid20');

      $n=$rs->FieldCount();
	   for ($i=0;$i<$n;$i++) {
		   $fld=$rs->FetchField($i);
		   $gr1->cols[]=array($fld->name,100);
	   }

      while ($row=$rs->FetchRow()) {
         array_walk($row,'apply_myhtml');
         $gr1->rows[]=$row;
      }
      $epage=0;
      $rs->Close();

      $gr1->play();

      echo "<div id=tbar2>";
      print_page_navEx($pg,$epage,582);
      echo "</div>";
   }
   else {
      if (!$db->Execute($query)) {
         echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>";
         return ask_db_query();
      }

      echo "<h6>Records affected = ".$db->Affected_Rows()."</h6>";
   }
}
//--------------------------------------
function ask_db_query()
{
   global $x2;
   $db=&$x2->db;

   $tarray=array(
		array('ADMIN', 0, 'Main'),
		array('', 0, 'DB Query')
		);
	$x2->make_header($tarray);
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST name="goForm">
<input type=hidden name="FUNCTION" value="582">
<table>
<tr>
   <td class=bgLabel>Query</td>
</tr>
<tr>
   <td class=bgInput><textarea name="QRY" style="width:600px; height:300px;"><?php
      if (isset($_SESSION['QRY'])) {
         echo myhtml($_SESSION['QRY']);
      }
   ?></textarea></td>
</tr>
<tr>
   <td class=bgButton><input type=submit value="Query"></td>
</tr>
</table>
</form>
<?php
}
//--------------------------------------
function su_check_nulls(&$x2)
{
   $db=&$x2->db;

	// HEADER ( for su_agent_list() )
  	$tarray=array(
		array('ADMIN', 0, 'Main'),
		array('', 0, 'Check nulls'),
		);
	$x2->make_header($tarray);

   require_once('ishop_tables.php');
   check_nulls($db);
   echo "Check finished!";
}
//--------------------------------------
function textout_lex(&$x2)
{
   $db=&$x2->db;

	// HEADER ( for su_agent_list() )
  	$tarray=array(
		array('ADMIN', 0, 'Main'),
		array('javascript:goTo(503)', 0, 'Lexicon'),
		array('', 0, 'Textout'),
		);
	$x2->make_header($tarray);

	$subq="SELECT LANGID, WLABEL FROM ishop_langdic WHERE SHORTHAND=? ORDER BY LANGID";

	$query="SELECT DISTINCT(SHORTHAND) FROM ishop_langdic ORDER BY SHORTHAND";
	if (($rs=$db->Execute($query))===false) {
		echo "<h6>".$db->ErrorMsg()."</h6>\n";
		return;
	}
	if (($fp=fopen($_REQUEST['TEXTOUT'], "w"))===false) {
		echo "<h6>Can not open file: ".myhtml($_REQUEST['TEXTOUT'])."</h6>\n";
		$rs->Close();
		return;
	}
	fwrite($fp,'<?php $all_lang_words=array( ' );
	while ($o1=$rs->FetchNextObject()) {

		$ar1=array();
		$data=array($o1->SHORTHAND);
		if (($rs2=$db->Execute($subq,$data))===false) {
			echo "<h6>".$db->ErrorMsg()."</h6>\n";
			break;
		}
		while ($o2=$rs2->FetchNextObject()) {
			$ar1[$o2->LANGID]=$o2->WLABEL;
			$maxk=$o2->LANGID;
		}
		$rs2->Close();

		if (empty($ar1))
			continue;

		for ($i=0;$i<$maxk;$i++) {
			if (!array_key_exists($i,$ar1))
				$ar1[$i]='';
		}
		ksort($ar1);

		fwrite($fp, "array('".$o1->SHORTHAND."'");
		for ($i=0;$i<=$maxk;$i++) {
			fwrite($fp, ", '".addslashes($ar1[$i])."'");
		}
		fwrite($fp, "),\r\n" );
	}
	fwrite($fp,"false); ?>\r\n");
	fclose($fp);
	$rs->Close();

	echo "<h6>Done!</h6>";
}
//--------------------------------------
function su_reset_passwd()
{
   global $x2;
   $db=&$x2->db;

 	if (empty($_REQUEST['PASSWD'])) {
		echo "<h6>Empty password!</h6>\n";
		return;
	}

 	$query="UPDATE agents SET PASSWD=? WHERE LPATH=?";
 	$data=array( $_REQUEST['PASSWD'], $_REQUEST['LPATH'] );
 	if (!$db->Execute($query,$data))
		echo "<h6>".$db->ErrorMsg()."</h6>\n";
}
//......................................
function su_set_opetype(&$x2)
{
   $db=&$x2->db;

   $o1=false;
 	$query="SELECT AGENTID FROM agents WHERE LPATH=?";
	$data=array($_REQUEST['LPATH']);
	if ($rs=$db->Execute($query,$data)) {
		$o1=$rs->FetchNextObject();
		$rs->Close();
	}
	else
		echo "<h6>".$db->ErrorMsg()."</h6>\n";

	if ($o1===false) {
		echo "<h6>Could not find agent!</h6>\n";
		return;
	}

	$keys=array('OPERATION_TYPE', 'TITLE', 'EMAIL' );
   foreach ($_REQUEST as $k1=>$v1) {
      if (strncmp($k1, 'OPE_', 4)==0) {
         if (!in_array($k1,$keys))
            $keys[]=$k1;
      }
   }

   $t1='agent_main';
	$prim=array('AGENTID', 'PNAME');
	foreach ($keys as $k1) {
   	$data=array( 'AGENTID'=>$o1->AGENTID, 'PNAME'=>$k1, 'PVALUE'=>$_REQUEST[$k1] );
      if (!ReplaceEx($db, $t1, $data, $prim))
		   echo "<h6>".$db->ErrorMsg()."</h6>\n";
	}
}
//......................................
function su_agent_menu(&$x2)
{
   $db=&$x2->db;

   // HEADER
  	$tarray=array(
		array('ADMIN', 0, 'Main'),
		array('', 0, 'Agent menu')
		);
	$x2->make_header($tarray);

	$o1=false;
	$query="SELECT T1.AGENTID, T1.PASSWD FROM agents T1
      WHERE T1.LPATH=?";
	$data=array($_REQUEST['RECORDID']);
	if (($rs=$db->Execute($query,$data))===false) {
      echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>\n";
		return;
	}
	$o1=$rs->FetchNextObject();
	$rs->Close();
	if (empty($o1)) {
		echo "<h6>Could not find agent!</h6>\n";
		return;
	}

	$query="SELECT PNAME, PVALUE FROM agent_main
      WHERE AGENTID=? AND (PNAME='OPERATION_TYPE' OR PNAME LIKE 'OPE_%' OR PNAME='TITLE' OR PNAME='EMAIL')";
	$data=array($o1->AGENTID);
	if (($rs=$db->Execute($query,$data))===false) {
      echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>\n";
		return;
	}
   while ($o2=$rs->FetchNextObject()) {
      $ar1[$o2->PNAME]=$o2->PVALUE;
   }
	$rs->Close();
?>
<form method=POST action="<?php echo $_SERVER['PHP_SELF']; ?>" name="goForm">
<input type=hidden name="FUNCTION">
<input type=hidden name="LPATH" value="<?php echo $_REQUEST['RECORDID']; ?>">
<table>
<tr>
	<td class=bgLabel>Agent</td>
	<td class=bgInput><b><?php echo myhtml($_REQUEST['RECORDID']); ?></b></td>
</tr>
<tr>
   <td class=bgLabel>Name</td>
   <td class=bgInput><input type=TEXT name="OPE_NAME" value="<?php
      echo myhtml(@$ar1['OPE_NAME']); ?>" SIZE=50 MAXLENGTH=255></td>
</tr>
<tr>
   <td class=bgLabel>Diacretitc title</td>
   <td class=bgInput><input type=TEXT name="TITLE" value="<?php
      echo myhtml(@$ar1['TITLE']); ?>" SIZE=50 MAXLENGTH=255></td>
</tr>
<tr>
   <td class=bgLabel>Address (1)</td>
   <td class=bgInput><input type=TEXT name="OPE_ADDR1" value="<?php
      echo myhtml(@$ar1['OPE_ADDR1']); ?>" SIZE=50 MAXLENGTH=255></td>
</tr>
<tr>
   <td class=bgLabel>Address (2)</td>
   <td class=bgInput><input type=TEXT name="OPE_ADDR2" value="<?php
      echo myhtml(@$ar1['OPE_ADDR2']); ?>" SIZE=50 MAXLENGTH=255></td>
</tr>
<tr>
   <td class=bgLabel>ZIP</td>
   <td class=bgInput><input type=TEXT name="OPE_ZIP" value="<?php
      echo myhtml(@$ar1['OPE_ZIP']); ?>" SIZE=50 MAXLENGTH=255></td>
</tr>
<tr>
   <td class=bgLabel>Town</td>
   <td class=bgInput><input type=TEXT name="OPE_TOWN" value="<?php
      echo myhtml(@$ar1['OPE_TOWN']); ?>" SIZE=50 MAXLENGTH=255></td>
</tr>
<tr>
   <td class=bgLabel>V.A.T. no.</td>
   <td class=bgInput><input type=TEXT name="OPE_VATNR" value="<?php
      echo myhtml(@$ar1['OPE_VATNR']); ?>" SIZE=50 MAXLENGTH=255></td>
</tr>
<tr>
   <td class=bgLabel>DOY</td>
   <td class=bgInput><input type=TEXT name="OPE_DOY" value="<?php
      echo myhtml(@$ar1['OPE_DOY']); ?>" SIZE=50 MAXLENGTH=255></td>
</tr>
<tr>
   <td class=bgLabel>Phone</td>
   <td class=bgInput><input type=TEXT name="OPE_PHONE" value="<?php
      echo myhtml(@$ar1['OPE_PHONE']); ?>" SIZE=50 MAXLENGTH=255></td>
</tr>
<tr>
   <td class=bgLabel>FAX</td>
   <td class=bgInput><input type=TEXT name="OPE_FAX" value="<?php
      echo myhtml(@$ar1['OPE_FAX']); ?>" SIZE=50 MAXLENGTH=255></td>
</tr>
<tr>
   <td class=bgLabel>Email</td>
   <td class=bgInput><input type=TEXT name="EMAIL" value="<?php
      echo myhtml(@$ar1['EMAIL']); ?>" SIZE=50 MAXLENGTH=255></td>
</tr>
<tr>
	<td class=bgLabel>Operation Type</td>
	<td class=bgInput>
		<select name="OPERATION_TYPE"><?php
		prAssocSelect($x2->operTypes, (int)@$ar1['OPERATION_TYPE']);
		?></select></td>
</tr>
<tr>
   <td></td>
   <td class=bgButton><input type=BUTTON value="Update" onClick="goTo(505)">
      <b><?php
      if ((int)@$ar1['OPERATION_TYPE']==2) {
         echo "<a href=\"javascript:goTo(545)\">";
      }
      echo "E-Invoice signholes";
      if ((int)@$ar1['OPERATION_TYPE']==2) {
         echo "</a>";
      }
      ?></b></td>
</tr>
<tr>
   <td>&nbsp;</td>
</tr>
<tr>
	<td class=bgLabel>New Password</td>
	<td class=bgInput><input type=PASSWORD name="PASSWD"></td>
</tr>
<tr>
   <td></td>
   <td class=bgButton><input type=BUTTON value="Reset" onClick="goTo(506)"></td>
</tr>
</table>
</form>
<?php
}
//......................................
function su_reset_resell_pwd()
{
   global $x2;
   $db=&$x2->db;

   $query="UPDATE reseller SET PASSWD=? WHERE RESELID=?";
   $data=array( $_REQUEST['RECORDID'], (int)$_REQUEST['RESELID']);
   if (!$db->Execute($query,$data)) {
      echo "<h6>".$db->ErrorMsg()."</h6>";
   }
}
//......................................
function su_reset_reseller(&$x2)
{
	$db=&$x2->db;

   $ar1=array('reseller', 'reselprefex', 'reseldoc');
   foreach ($ar1 as $table1) {
   	$query="UPDATE $table1 SET AGENTID=? WHERE RESELID=?";
	   $data=array( (int)$_REQUEST['RECORDID'], (int)$_REQUEST['RESELID']);
   	if (!$db->Execute($query,$data)) {
         echo "<h6>".$db->ErrorMsg()."</h6>";
      }
   }
}
//......................................
function su_reselist_all(&$x2)
{
	$db=&$x2->db;

	$tarray=array(
		array('ADMIN', 0, 'Main'),
		array('', 0, 'All Resellers')
		);
	$x2->make_header($tarray);
?>
<script language=JavaScript>
function oclick(stage,row,col)
{
   var id=this.cells(row,2).getValue(); // existing reseller id

   document.goForm.RESELID.value = id;
   var rval = id + ' ' + this.cells(row,4).getValue();
   $('#rid').html( rval );
   $('#rid2').html( rval );

   show_popup('popmenu');
}
function winRet(x)
{
	switch (x)
	{
	case 1:
      show_popup('popmenuRA');
	   queryNewWin();
		break;
   case 2:
      $('#resetpwd').dialog({modal:true});
      break;
	}
   return false;
}
function goUpdatePwd()
{
   goRecord( 586, $('#newpwd').val() );
}
</script>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST name="goForm">
<input type=hidden name="FUNCTION">
<input type=hidden name="RECORDID">
<input type=hidden name="RESELID">
</form>
<div id=resetpwd title="Reset password" style="display:none">
<table>
<tr>
   <td>Reseller Id</td>
   <td id=rid2></td>
</tr>
<tr>
   <td>New password</td>
   <td><input type=password id=newpwd></td>
</tr>
<tr>
   <td></td>
   <td><button type=button onclick="goUpdatePwd()">Update</button></td>
</tr>
</table>
</div>
<?php
   $gr1=new cXRID('grid8');
   $gr1->cols=array("Agent Id", "Lpath", "Reseller Id", "Password", "Fullname", "Phone", "Comments",
		"Warehouse", "Device Id" );
   $gr1->XMLfile="ishop_csv.php?FUNCTION=3&iAdmin=".session_id();
   $gr1->bottom_margin=10;
   $gr1->click='oclick';
   $gr1->sort="int,str,int,str,str,str,str,int,int"; // must be comma delimited, no spaces
   $gr1->play();

   echo "<div id=tbar2><small>End of data</small></div>";

   $selx=array(
   	array(1, '/img/go_next.gif', 'Assign to another reseller' ),
   	array(2, '/img/go_next.gif', 'Reset password' )
   	);
   draw_popup($selx,false);

   echo "<div id=popmenuRA class=POPMENU><table border=0 cellpadding=4>
      <tr><td>Select <i>Agent Id</i> to (re)assign on Reseller Id <b>#<span id=rid></span></b>:</td></tr>
      <tr><td><div style=\"height:100px; overflow-y:scroll;\">";
   $query="SELECT AGENTID, LPATH FROM agents WHERE AGENTID>0 ORDER BY AGENTID";
   $data=false;
   if (($rs=$db->Execute($query,$data))===false) {
      echo "<h6>".$db->ErrorMsg()."</h6>";
   }
   else {
      while ($o1=$rs->FetchNextObject()) {
         echo "<b><a href=\"javascript:goRecord(552,$o1->AGENTID)\">".myhtml($o1->AGENTID."# ".$o1->LPATH)."</a></b><br>";
      }
      $rs->Close();
   }
   echo "</div></td></tr></table></div>";
}
//......................................
function su_resel_list(&$x2)
{
	$db=&$x2->db;

	$tarray=array(
		array('ADMIN', 0, 'Main'),
		array('', 0, 'Agent Resellers')
		);
	$x2->make_header($tarray);
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST name="goForm">
<input type=hidden name="FUNCTION">
<input type=hidden name="RECORDID" value="<?php echo $_REQUEST['RECORDID']; ?>">
<input type=hidden name="RESELID">
<input type=hidden name="REPASS">
<table>
<tr class=bgHeader>
	<td>Agent Id</td>
   <td>Lpath</td>
   <td>Operation</td>
   <td>Company</td>
</tr>
<?php
	$query="SELECT T0.AGENTID, T0.LPATH, T1.PVALUE F1, T2.PVALUE F2 FROM agents T0
      LEFT JOIN reseller ON (reseller.AGENTID=T0.AGENTID)
      LEFT JOIN agent_main T1 ON (T1.AGENTID=T0.AGENTID AND T1.PNAME='OPERATION_TYPE')
      LEFT JOIN agent_main T2 ON (T2.AGENTID=T0.AGENTID AND T2.PNAME='TITLE')
      WHERE T0.AGENTID=?
      GROUP BY T0.AGENTID, T0.LPATH, T1.PVALUE, T2.PVALUE
		ORDER BY T0.AGENTID";
	$data=array($_REQUEST['RECORDID']);
	if (($rs=$db->Execute($query,$data))===false) {
		tr_echo( "<h6>".$db->ErrorMsg()."</h6>\n" );
	}
	else {
		if ($o1=$rs->FetchNextObject()) {
			echo "<tr>".
				"<td>".myhtml($o1->AGENTID)."</td>\n".
            "<td><b><a href=\"javascript:goRecord(504,'".myhtml($o1->LPATH)."')\">".
				myhtml($o1->LPATH)."</a></b></td>".
				"<td>".$x2->operTypes[(int)$o1->F1]."</td>\n".
				"<td>".myhtml($o1->F2)."</td>\n".
				"</tr>\n";
		}
		else {
			tr_echo("<h6>Agent not found</h6>\n");
		}
		$rs->Close();
	}
?>
</table>
</form>
<script language="JavaScript">
function goReseller(id,pwd)
{
   var f1=document.goForm;
   f1.RESELID.value=id;
   f1.REPASS.value=pwd;
   f1.FUNCTION.value=1;
   f1.submit();
}
</script>
<p></p>
<?php
   $gr1=new cXRID('grid13');
   $gr1->cols=array("Reseller Id", "Password", "Fullname", "Phone", "Comments",
		"Warehouse", "Device Id");
	$gr1->XMLfile="ishop_csv.php?FUNCTION=2&RECORDID=".$_REQUEST['RECORDID']."&iAdmin=".session_id();
   $gr1->play();
?>
<div id=tbar2><b><a href="javascript:goTo(526)">All resellers</a></b></div>
<?php
}
//......................................
function renumber_agent()
{
   global $x2;
   $db=&$x2->db;

   // OLDNAME, NEWID
   $query="SELECT COUNT(*) FROM agents WHERE AGENTID=?";
   $data=array( (int)$_REQUEST['NEWID'] );
   if (($rs=$db->Execute($query,$data))===false) {
      echo __LINE__.$db->ErrorMsg();
      return;
   }
   $row=$rs->FetchRow();
   $rs->Close();
   if ($row[0]>0) {
      umessage("ID ".myhtml($_REQUEST['NEWID'])." already exists!");
      return;
   }

   $query="SELECT AGENTID FROM agents WHERE LPATH=?";
   $data=array($_REQUEST['OLDNAME']);
   if (($rs=$db->Execute($query,$data))===false) {
      echo __LINE__.$db->ErrorMsg();
      return;
   }
   $o1=$rs->FetchNExtObject();
   $rs->Close();
   if ($o1===false) {
      umessage("LPATH ".myhtml($_REQUEST['OLDNAME'])." not found!");
      return;
   }
   $oldid = $o1->AGENTID;

   $db->StartTrans();
   do {
      $tables=$db->MetaTables();
      foreach ($tables as $t1) {
         $cols=$db->MetaColumnNames($t1);
			array_walk($cols,'array_toLower');
			if (in_array('agentid',$cols)) {
            $query="UPDATE $t1 SET AGENTID=? WHERE AGENTID=?";
            $data=array( (int)$_REQUEST['NEWID'], $oldid);
            if (!$db->Execute($query,$data)) {
               echo __LINE__.$db->ErrorMsg()." (Table $t1)";
               break;
            }
         }
      }
      umessage("Operation completed!");
   } while (false);
   $db->CompleteTrans();
}
//......................................
function del_agent(&$x2)
{
	$db=&$x2->db;

	// HEADER
  	$tarray=array(
		array('ADMIN', 0, 'Main')
		);
	$x2->make_header($tarray);

 	// BODY
?>
<b>Agent deletion</b>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST name="goForm">
<input type=hidden name="FUNCTION" value="<?php echo $_REQUEST['FUNCTION']; ?>">
<?php
	do {
		if (empty($_REQUEST['DELAGENT'])) {
			echo "No records selected!\n";
			break;
		}

		$n1=count($_REQUEST['DELAGENT']);
		$tables=$db->MetaTables();

		echo "<table>";
		$nrec=0;
		foreach ($tables as $t1) {
			echo "<tr";
			if ($nrec++%2>0) echo " class=HILINE";
			echo "><td>".$t1."</td><td>";

			$cols=$db->MetaColumnNames($t1);
			array_walk($cols,'array_toLower');
			if (in_array('agentid',$cols)) {
				$query="DELETE FROM $t1 WHERE AGENTID=".( (int)$_REQUEST['DELAGENT'][0] );
				for ($i=1;$i<$n1;$i++) {
					$query.=" OR AGENTID=".( (int)$_REQUEST['DELAGENT'][$i] );
				}
				if (!$db->Execute($query)) {
					echo "<h6>".$db->ErrorMsg()."</h6>";
				}
				else {
					echo "Done";
				}
			}

			echo "</td></tr>\n";
		}
		echo "</table>";

	} while (false);

  	echo "<small>End of file</small>";
}
//......................................
function clear_transactions()
{
   global $x2;
   $db=&$x2->db;

   // HEADER
  	$tarray=array(
		array('ADMIN', 0, 'Main'),
      array('', 0, 'Clear transactions')
		);
	$x2->make_header($tarray);

	if (empty($_REQUEST['DELAGENT'])) {
      echo "<h6>No data!</h6>";
	   return;
	}

 	// BODY
   $query="SELECT ORDERID FROM order_main WHERE AGENTID=?";
   $data=array( $_REQUEST['DELAGENT'][0] );
   $n=count($_REQUEST['DELAGENT']);
   for ($i=1;$i<$n;$i++) {
      $query.=" OR AGENTID=? ";
      $data[]=$_REQUEST['DELAGENT'][$i];
   }
   if (($rs=$db->Execute($query,$data))===false) {
      echo "<h6>$query: ".$db->ErrorMsg()."</h6>";
      return;
   }
   $rows=$rs->GetRows();
   $rs->Close();

   do {
      if (empty($rows))
         break;

      $tables=array('order_line', 'order_prop');
      $n=count($rows);
      foreach ($tables as $t1) {
         $query="DELETE FROM $t1 WHERE ORDERID=?";
         $data=array( $rows[0][0] );
         for ($i=1;$i<$n;$i++) {
            $query.=" OR ORDERID=?";
            $data[]=$rows[$i][0];
         }
         if (!$db->Execute($query,$data)) {
            echo "<h6>$query: ".$db->ErrorMsg()."</h6>";
            return;
         }
      }

   } while (false);

   $tables=array('order_main', 'ishop_cash', 'ishop_zno',
       'ishop_s80', 'ishop_s81', 'ishop_g11', 'ishop_d32');

   $n=count($_REQUEST['DELAGENT']);
   foreach ($tables as $t1) {
      $query="DELETE FROM $t1 WHERE AGENTID=?";
      $data=array( $_REQUEST['DELAGENT'][0] );
      for ($i=1;$i<$n;$i++) {
         $query.=" OR AGENTID=? ";
         $data[]=$_REQUEST['DELAGENT'][$i];
      }
      if (!$db->Execute($query,$data))
         echo "<h6>$query: ".$db->ErrorMsg()."</h6>";
   }

   echo "<h5>Done!</h5>";
}
//......................................
function draw_main_admin()
{
   $is_inca = (strcmp($_SERVER["HTTP_HOST"], "www.incahellas.com") == 0
               || strcmp($_SERVER["HTTP_HOST"], "www.incahellas.gr") == 0);

   echo '<ul>
         <li><a href="javascript:goTo(571)">Agents</a></li>
         <li><a href="javascript:goTo(526)">All resellers</a></li>
         <li><a href="javascript:goTo(542)">Sessions</a></li>
         <li>';
      if (strcmp($_SERVER["HTTP_HOST"], "www.incahellas.com") == 0)
         echo "<a href=\"javascript:goTo(550)\">System logs</a>";
      else
         echo "System logs";
      echo '</li>
         <li><a href="javascript:goTo(556)">EAFDSS</a></li>
      </ul>';

   echo '<ul>
      <li>';
      $label='Master License';
      if ($is_inca || !empty($_SESSION['WWWLicense']) )
         echo "<a href=\"javascript:goFX('adm_license.php',10)\">$label</a>";
      else
         echo $label;
   echo '</li>
      <li>';
      $label='CE License';
      if ($is_inca || !empty($_SESSION['PdaLicense']))
         echo "<a href=\"javascript:goFX('adm_license.php',1)\">$label</a>";
      else
         echo $label;
   echo '</li>
      </ul>';

   echo '<ul>
         <li><a href="javascript:goFX(\'adm_iupd.php\',1)">iUpdate (registrations)</a></li>
         <li><a href="javascript:goFX(\'adm_iupd.php\',7)">iUpdate (user messages)</a></li>
      <li>';
      $label='iUpdate (licenses)';
      if ($is_inca || !empty($_SESSION['UPDLicense']))
         echo "<a href=\"javascript:goFX('adm_iupd.php',20)\">$label</a>";
      else
         echo $label;
   echo '</li>
      <li><a href="javascript:goFX(\'adm_iupd.php\',5)">iUpdate (requests logbook)</a></li>
      </ul>';

   echo '<hr>
      <ul>
         <li><a href="javascript:goFX(\'adm_iupd.php\',36)">System parameters</a></li>
         <li><a href="javascript:goFX(\'ishop_ftp.php\',1)">FTP users</a></li>
      </ul>';
}
//......................................
function draw_main_gps()
{
   echo '<ul>
      <li><a href="javascript:goTo(560)" class="B">GPS devices</a></li>
      </ul>';
}
//......................................
function draw_main_organosis()
{
   global $x2;
   $db=&$x2->db;

   echo '<ul>
         <li><a href="javascript:goTo(503)">Lexicon</a></li>
         <li><a href="javascript:goTo(510)">Units</a></li>
         <li><a href="javascript:goFX(\'ishop_stat.php\',0);">Statistics</a></li>
         <li><a href="javascript:goTo(570)">Check nulls</a></li>
      </ul>
      <ul>
         <li><a href="javascript:goTo(517)">DB File Info</a></li>
         <li><a href="javascript:'.(is_mysql($db)?'goTo(518)':'window.alert(\'Option available only for MySql!\')').'>DB Status</a></li>
         <li><a href="javascript:'.(is_mysql($db)?'goTo(519)':'window.alert(\'Option available only for MySql!\')').'>DB Vars.</a></li>
         <li><a href="javascript:goTo(581)">DB Query</a></li>
         <li><a href="javascript:goTo(516)">Generic files</a><br></li>
         <li><a href="javascript:goTo(557)">E-Invoice files</a><br></li>
      </ul>
      <ul>
         <li><a href="javascript:goTo(540)">Change password</a></li>
         <li><a href="javascript:goTo(537)">Params</a></li>
      </ul>
      <hr>
      <ul>
         <li><a href="javascript:goFX(\'ishop_celog.php\',100)">View log</a></li>
         <li><a href="javascript:goFX(\'ishop_celog.php\',101)">Log parameters</a></li>
         <li><a href="javascript:show_popup(document.getElementById(\'popmenu1\'))">CE Data</a></li>
         <li><a href="javascript:goFX(\'lex_main.php\',1)">Lex main</a></li>
      </ul>';
}
//......................................
function draw_main_abc()
{
   echo 'Drop:
      <ul>
         <li><a href="javascript:goTo(520)">Customers</a></li>
         <li><a href="javascript:goTo(522)">Items</a></li>
         <li><a href="javascript:goTo(524)">iPhone</a></li>
      </ul>
      Make:
      <ul>
         <li><a href="javascript:goTo(521)">Customers</a></li>
         <li><a href="javascript:goTo(523)">Items</a></li>
         <li><a href="javascript:goTo(525)">iPhone</a></li>
      </ul>';
}
//......................................
function su_agent_main()
{
   global $x2, $PdaLicense;
   $db=&$x2->db;

   // HEADER
  	$tarray=array(
		array('', 0, 'Main')
		);
	$x2->make_header($tarray);

	//$ionian = (strpos($_SERVER["HTTP_HOST"],'ionian')!==false) /*|| ($_SERVER['SERVER_ADDR']=='132.147.200.236')*/;

	// BODY
?>
<div class="grid-container">
   <h1 class="h4">Main</h1>
   <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST name="goForm">
   <input type=hidden name="FUNCTION">
   <div class="grid-x grid-margin-x">
      <div class="cell medium-3">
         <div class="top-bar">Admin</div>
         <?php draw_main_admin(); ?>
      </div>

      <div class="cell medium-3">
         <div class="top-bar">GPS</div>
         <?php draw_main_gps(); ?>
      </div>

      <div class="cell medium-3">
         <div class="top-bar">Organosis</div>
         <?php draw_main_organosis(); ?>
      </div>

      <div class="cell medium-3">
         <div class="top-bar">ABC</div>
         <?php draw_main_abc(); ?>
      </div>
   </div>
   </form>
</div>
<?php
}
//......................................
function rename_agent(&$x2)
{
   $db=&$x2->db;

   $query="UPDATE agents SET LPATH=? WHERE LPATH=?";
   $data=array($_REQUEST['NEWNAME'], $_REQUEST['OLDNAME']);
   if (!$db->Execute($query,$data))
      echo "<h6>".$db->ErrorMsg()."</h6>";
}
//......................................
function su_admin_frame()
{
?>
<div style="display:flex; gap:10px; height:100%; width:100%">
   <iframe
      src="<?php echo $_SERVER['PHP_SELF']; ?>?FUNCTION=584"
      style="flex:1; height:100%; border:1px solid #ccc;">
   <iframe
      src="<?php echo $_SERVER['PHP_SELF']; ?>?FUNCTION=0"
      name="RightPane"
      style="flex:1; height:100%; border:1px solid #ccc;">
</div>
<?php
}
//......................................
function su_adm_frame_left(&$x2)
{
   $db=&$x2->db;

?>
<div style="width:100%; background: url(ttop.gif); height:35px; overflow:hidden;">
   <a href="http://www.incahellas.com" target=_top><img
		   src="logo.gif" alt="iShopping cart logo" border=0></a>
</div>

<div class=MainMenu style="padding:4px; border-top: 2px outset #d0d0d0; border-bottom: 2px outset #d0d0d0;">
   <b>Admin</b>
</div>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST name="goForm" target="RightPane">
<input type=hidden name="FUNCTION">
<input type=hidden name="RECORDID">
</form>
<?php
   $agents=array();
   $query="SELECT AGENTID, LPATH FROM agents ORDER BY LPATH";
   if (($rs=$db->Execute($query))===false) {
      echo __LINE__.$db->Errormsg();
      return;
   }
   while ($o1=$rs->FetchNextObject()) {
      $agents[$o1->AGENTID]=$o1->LPATH;
   }
   $rs->Close();

   echo "<div style=\"font-weight:bold; padding:4px;\">";
   if (array_key_exists(0,$agents)) {
      echo "<a href=\"".$_SERVER['PHP_SELF']."?FUNCTION=600&RECORDID=".myhtml($agents[0])."\" target=RightPane>".myhtml($agents[0])."</a><hr>";
   }
   foreach ($agents as $k=>$lpath) {
      if ($k!=0) {
         echo "<a href=\"".$_SERVER['PHP_SELF']."?FUNCTION=600&RECORDID=".myhtml($lpath)."\" target=RightPane>".myhtml($lpath)."</a><br>";
      }
   }
   echo "</div>";

   echo "<div id=tbar2 style=\"font-weight:bold\"><a href=\"javascript:goTo(500)\" target=_parent>Main</a></div>";
}
//......................................
function su_agent_list(&$x2)
{
   $db=&$x2->db;

   // HEADER
  	$tarray=array(
		array('ADMIN', 0, 'Main'),
		array('javascript:goTo(571)', 0, 'Agents(r)')
		);
	$x2->make_header($tarray);

 	// BODY
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST name="goForm">
<input type=hidden name="FUNCTION" value="<?php echo 571; /*$_REQUEST['FUNCTION'];*/ ?>">
<input type=hidden name="RECORDID">
<table class="maintable"></table>
<?php
   $gr1=new cXRID('grid9');
   //$gr1->bottom_margin=80;
?>
<script>
   $(function(){
      $('.maintable').DataTable({
         'lengthMenu': [ 15, 25, 50, 75, 100 ],
         ajax: 'ishop_csv.php?FUNCTION=1',
         columns: [
            { title: 'Agent Id'},
            { title: 'Lpath'},
            { title: 'Password'},
            { title: 'Operation'},
            { title: 'Company'},
            { title: 'Resellers'},
            { title: 'Ctgs.'},
            { title: 'Items'},
            { title: 'Mbrs.'},
            { title: 'External'},
            { title: 'Extra-1'}
         ]
      });
   });
   //$gr1->XMLfile="ishop_csv.php?FUNCTION=1&iAdmin=".session_id();
   //$gr1->XMLfile="ishop_csv.php?FUNCTION=1";
	// execute
   //$gr1->play();

   function fill_at_sel(osel, q1)
   {
      if (q1.error.length) {
         window.alert(q1.error);
         return;
      }

      var n=osel.options.length;
      for (var i=n-1;i>=0;i--) {
         osel.remove(i);
      }

      var n=q1.data.length;
      for (i=0;i<n;i++) {
         var opt1=document.createElement("OPTION");
         opt1.text=q1.data[i].TABLE;
         osel.add(opt1);
      }
   }
   function load_file_list(osel)
   {
      $.get('ishop_jsonfu.php?FUNCTION=5', function(data,st){
         var local = new Function("return "+data)();
         if (local.length>0) {
            fill_at_sel(osel, local[0]);
         }
      } );
   }
   function tableReload()
   {
      var osel=document.getElementById('atable'); // select object
      var i=osel.selectedIndex;
      if (i<0)
         return;

      var t1=osel.options(i).text;
      grid9.clearAll(false);
      grid9.setColumnLabel(11, t1 );

      //	provide new data URL - plain text comma-separated file
      var new_url = 'ishop_csv.php?FUNCTION=1&iAdmin=<?php echo session_id(); ?>&AFILES=' + t1;
      grid9.loadXML(new_url);

      // Hide menu window
      showHide('popfiles',0);
   }
   function showPopfiles()
   {
      show_popup(document.getElementById('popfiles'));

      var o1=document.getElementById('atable');
      if (o1.options.length<=2) {
         load_file_list(o1);
      }
   }
   function agentGo(f1)
   {
      f1.FUNCTION.value='501';
      f1.submit();
   }
   function onTodo()
   {
      var i=document.getElementById('todo').selectedIndex;
      switch (i)
      {
      case 0: // Clear data
         if (!window.confirm('All transactions for checked agents will be cleared!'))
            return;
         goTo(534);
         break;
      case 1: // delete agent
         if (!window.confirm('All tables for checked agents will be deleted!'))
            return;
         goTo(502);
         break;
      }
   }

   var id=null;
   function onAgent(lpath)
   {
      id=lpath;
      show_popup('popmenu');
   }
   function winRet(x)
   {
      switch (x)
      {
      case 1: // login
         goRecord(600,id);
         break;
      case 2: // edit
         goRecord(504,id);
         break;
      case 3: // iShopping cart
         var owin=window.open('/cart/index.php?LPATH='+id, 'cart');
         owin.focus();
         break;
      case 4:
         goRecordEx('ishop_taxi.php',200,id);
         break;
      case 100:
         goFX('ishop_celog.php',100);
         break;
      case 101:
         goFX('ishop_celog.php',101);
         break;
      case 572:
         document.fo2.OLDNAME.value=id;
         show_popup('poprename');
         break;
      case 573:
         document.fo3.OLDNAME.value=id;
         show_popup('poprenumber');
         break;
      }
   }
</script>
<div xid=tbar2>
   <!--
      <span style="margin:1px; border=1px solid black; padding-left:2px;">
         <b>Add new agent</b>
         Lpath:<input type=TEXT name="LPATH">
         <input type=BUTTON value="Add" onClick="agentGo(this.form)">
		</span>
		&nbsp;
		<input type=BUTTON value="Extra file(s)" onClick="showPopfiles()">
		With all checked:<select id=todo>
		<option value="0">Clear data
		<option value="1">Delete agent
      </select><button type=button onclick="onTodo()"><img src="to_right.gif" border=0></button>
   -->
   <button type=button class="button" onclick="goTo({fx:583, target:'_parent'})">Frame admin.</button>
</div>
</form>
<?php
   // Popup must be drawn last in page in order to have a proper z-index
	$selx=array(
	   array(1, '/img/users.png',  'Login' ),
	   '<hr>',
		array(2, '/img/button_edit.png', $x2->xxlabel('edit') ),
      array(572, '/img/rename.png', $x2->xxlabel('rename') ),
      array(573, '/img/transfer.png', 'Renumber' ),
      array(4, '/img/sms.png',  'SMS units' ),
      array(3, '/img/cart.gif',  'iShopping cart' )
   );
   draw_popupFN($selx);
?>
<div id=popfiles xclass=POPMENU class="dropdown-pane" data-dropdown data-auto-focus="true">
   <table boder=0>
   <tr><td class=bgLabel>Check files to add:</td></tr>
   <tr><td class=bgInput><select id=atable SIZE=10><option>Loading<option>please wait...</select></td></tr>
   <tr><td class=bgButton><button type=button onclick="tableReload()">Apply</button></td></tr>
   </table>
</div>

<div id=poprename xclass=POPMENU class="dropdown-pane" data-dropdown data-auto-focus="true">
   <b>Rename agent</b>
   <form action="<?php echo $_SERVER['PHP_SELF']; ?>" name="fo2" method=POST>
   <input type=HIDDEN name="FUNCTION" value="572">
   <input type=HIDDEN name="OLDNAME">
   <table boder=0>
   <tr><td class=bgLabel>New name:</td></tr>
   <tr><td class=bgInput><input type=TEXT name="NEWNAME"></td></tr>
   <tr><td class=bgButton><input name="ok" value="Update" type=submit></td></tr>
   </table>
   </form>
</div>

<div id=poprenumber xclass=POPMENU class="dropdown-pane" data-dropdown data-auto-focus="true">
   <b>Renumber agent</b>
   <form action="<?php echo $_SERVER['PHP_SELF']; ?>" name="fo3" method=POST>
   <input type=HIDDEN name="FUNCTION" value="573">
   <input type=HIDDEN name="OLDNAME">
   <table boder=0>
   <tr><td class=bgLabel>New ID:</td></tr>
   <tr><td class=bgInput><input type=TEXT name="NEWID"></td></tr>
   <tr><td class=bgButton><input name="ok" value="Update" type=submit></td></tr>
   </table>
   </form>
</div>
<?php
}
//--------------------------------------
function su_show_user_page()
{
   global $x2;
   $db=&$x2->db;

   // HEADER
  	$tarray=array(
		array("ADMIN", 0, 'Main'),
		array("javascript:goTo(510)", 1, 'Units'),
		array("", 0, "User analysis")
		);
	$x2->make_header($tarray);
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST name="goForm">
<input type=hidden name="FUNCTION">
<input type=hidden name="TRDATE" value="<?php echo $_REQUEST['TRDATE']; ?>">
</form>
Date <b><?php echo GrDate($_REQUEST['TRDATE']); ?></b>
<!-- BODY -->
<?php

   $query="SELECT ishop_usage.AGENTID, LPATH, PHP_SELF, COUNT(*) AS F2, SUM(BYTES) AS F3, AVG(BYTES) AS F4
      FROM ishop_usage
      LEFT JOIN agents ON (agents.AGENTID=ishop_usage.AGENTID)
      WHERE TRDATE LIKE ? AND DCCODE=0
      GROUP BY ishop_usage.AGENTID, PHP_SELF
      ORDER BY LPATH, PHP_SELF";
   $data=array( $_REQUEST['TRDATE'].'%' );
   if (!$rs=$db->Execute($query,$data)) {
      echo "<h6>".$db->ErrorMsg()."</h6>\n";
      return;
   }

   echo "<table>\n".
      "<tr class=\"bgHeader\">\n".
      "<td>Lpath</td>\n".
      "<td>Page</td>\n".
      "<td>Transactions</td>\n".
      "<td>Units</td>\n".
      "<td>Average/trans.</td>\n".
      "</tr>\n";

   $nrec=0;
   while ($o1=$rs->FetchNextObject()) {
      echo "<tr";
      if (($nrec++)%2) echo " class=\"HILINE\"";
      echo ">\n".
         "<td>".myhtml($o1->LPATH)."</td>\n".
         "<td>".myhtml($o1->PHP_SELF)."</td>\n".
         "<td>".$o1->F2."</td>\n".
         "<td align=right>".GNFormat($o1->F3,0)."</td>\n".
         "<td align=right>".GNFormat($o1->F4,0)."</td>\n".
         "</tr>\n";
   }
   echo "<tr class=bgButton>\n".
      "<td colspan=10><b><a href=\"javascript:goTo(514)\">User group</a></b></td>\n".
      "</tr>\n".
      "</table>\n";

   $rs->Close();
}
//--------------------------------------
function su_show_users()
{
   global $x2;
   $db=&$x2->db;

   // HEADER
  	$tarray=array(
		array("ADMIN", 0, 'Main'),
		array("javascript:goTo(510)", 1, 'Units'),
		array("", 0, "User group")
		);
	$x2->make_header($tarray);
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST name="goForm">
<input type=hidden name="FUNCTION">
<input type=hidden name="TRDATE" value="<?php echo $_REQUEST['TRDATE']; ?>">
</form>
Date <b><?php echo GrDate($_REQUEST['TRDATE']); ?></b>
<!-- BODY -->
<?php

   $query="SELECT ishop_usage.AGENTID, LPATH, COUNT(*) AS F2, SUM(BYTES) AS F3
      FROM ishop_usage LEFT JOIN agents ON (agents.AGENTID=ishop_usage.AGENTID)
      WHERE TRDATE LIKE ? AND DCCODE=0
      GROUP BY ishop_usage.AGENTID
      ORDER BY LPATH";
   $data=array( $_REQUEST['TRDATE'].'%' );
   if (!$rs=$db->Execute($query,$data)) {
      echo "<h6>".$db->ErrorMsg()."</h6>\n";
      return;
   }

   echo "<table>\n".
      "<tr class=\"bgHeader\">\n".
      "<td>Lpath</td>\n".
      "<td>Transactions</td>\n".
      "<td>Units</td>\n".
      "</tr>\n";

   $nrec=0;
   while ($o1=$rs->FetchNextObject()) {
      echo "<tr";
      if (($nrec++)%2) echo " class=\"HILINE\"";
      echo ">\n".
         "<td>".myhtml($o1->LPATH)."</td>\n".
         "<td>".$o1->F2."</td>\n".
         "<td align=right>".GNFormat($o1->F3,0)."</td>\n".
         "</tr>\n";
   }
   echo "<tr class=bgButton>\n".
      "<td colspan=10><b><a href=\"javascript:goTo(515)\">User analysis</a></b></td>\n".
      "</tr>\n".
      "</table>\n";

   $rs->Close();
}
//--------------------------------------
class cSQLINFO {
	var $_grid;
	var $title='';
	var $query='';
	var $click=false;
	var $bmargin=20;
	var $checkbox=false;
	var $_prClosingDiv;

	function __construct($gridname, $uPrintClosingDiv=false)
	{
		$this->_grid=$gridname;
		$this->_prClosingDiv=$uPrintClosingDiv;
	}

	function Execute()
	{
		global $x2;
		$db=&$x2->db;

		// HEADER
  	   $tarray=array(
		   array("ADMIN", 0, 'Main'),
		   array('', 0, $this->title)
		   );
	   $x2->make_header($tarray);

	   // BODY
	   $f1=new cFORM('goForm');
	   $f1->vars=array( 'FUNCTION'=>'', 'TRDATE'=>'', 'RECORDID'=>'' );
	   $f1->run();

     	if (!$rs=$db->Execute($this->query)) {
      	echo "<h6>".$db->ErrorMsg()."</h6>\n";
      	return;
  		}

		$gr1=new cXRID($this->_grid);

		if ($this->checkbox)
		    $gr1->cols[]=array( '.sal', 30);

		$n=$rs->FieldCount();
		for ($i=0;$i<$n;$i++) {
			 $fld=$rs->FetchField($i);
			 $gr1->cols[]=array($fld->name,100);
		}

      $nrec=0;
		while ($row=$rs->FetchRow()) {
			$row1=array();

			if ($this->checkbox)
			   $row1[]="<input type=CHECKBOX name=\"ID[]\" value=\"".myhtml($row[0])."\">";

			if (!strcasecmp($row[0], 'version'))
				$row1[]="<b>".$row[0]."</b>";
			else
				$row1[]=myhtml($row[0]);

			for ($i=1;$i<$n;$i++)
				$row1[]=myhtml($row[$i]);

         $gr1->rows[]=$row1;
         $nrec++;
		}
   	$rs->Close();

   	// In(de)crease default margin
   	if ($this->bmargin!==false) {
		   $gr1->bottom_margin = $this->bmargin;
		}

   	// Execute
   	if ($this->click!==false) {
			$gr1->click=$this->click;
		}
   	$gr1->play();

   	if ($this->_prClosingDiv) {
			echo "<div id=tbar2><small>End of data</small></div>";
		}
	}
}
//--------------------------------------
function su_db_status()
{
   global $x2;
   $db=&$x2->db;

 	$si=new cSQLINFO('grid2', true);
 	$si->title="Database Status";
 	$si->query="SHOW STATUS";
 	$si->Execute();
}
//--------------------------------------
function su_db_vars()
{
   global $x2;
   $db=&$x2->db;

 	$si=new cSQLINFO('grid5', true);
 	$si->title="Database Variables";
 	$si->query="SHOW VARIABLES";
 	$si->Execute();
}
//--------------------------------------
function su_drop_table()
{
   global $x2;
   $db=&$x2->db;

   // HEADER
  	$tarray=array(
	   array("ADMIN", 0, 'Main'),
	   array('', 0, 'File information')
	   );
	$x2->make_header($tarray);

   $query="DROP TABLE ".$_REQUEST['RECORDID'];
   if (!$db->Execute($query))
      echo "<h6>$query: ".$db->ErrorMsg()."</h6>";
   else
      echo "<h5>Table ".$_REQUEST['RECORDID']." dropped!<br>You need logout and then press [RESET]</h5>";
}
//--------------------------------------
function su_table_groupdata()
{
   $si=new cSQLINFO('grid7', true);
 	$si->title="File group data";
 	$si->query="SELECT T1.AGENTID, T2.LPATH, COUNT(*) 'RECORDS' FROM ".$_REQUEST['RECORDID']." T1
      LEFT JOIN agents T2 ON (T2.AGENTID=T1.AGENTID)
      GROUP BY T1.AGENTID, T2.LPATH";
 	$si->Execute();
}
//--------------------------------------
function su_table1_details(&$x2)
{
	global $ADODB_FETCH_MODE;
	$db=&$x2->db;

	// HEADER
  	$tarray=array(
	   array("ADMIN", 0, 'Main'),
	   array('', 0, 'File information')
	   );
	$x2->make_header($tarray);

	echo "<table>
	  <tr><td class=bgLine>Table <b>".myhtml($_REQUEST['RECORDID'])."</b>
     </td></tr>";
	$f1=new cFORM('goForm');
	$f1->vars=array( 'FUNCTION'=>'', 'RECORDID'=>$_REQUEST['RECORDID'] );
	$f1->open();
	echo "</form></table>";

	if (is_mysql($db)) {
      $query="DESCRIBE ".$_REQUEST['RECORDID'];
   }
   else if (strpos($db->databaseType,'mssql')!==false) { // mssql, etc.
      $query="EXEC sp_columns @table_name = '".$_REQUEST['RECORDID']."' ";
   }
   else {
      echo "<h6>Not yet implemented for Database type ".$db->databaseType."</h6>";
      return;
   }
   // Save old fetch mode
   $ofm=$ADODB_FETCH_MODE;
   // Reset fetch mode
   $ADODB_FETCH_MODE=ADODB_FETCH_ASSOC;
   // Execute query
   $rs=$db->Execute($query);
   // Recover old fetch mode
   $ADODB_FETCH_MODE=$ofm;
   // Process results
   if ($rs===false) {
      echo "<h6>".$db->ErrorMsg()."</h6>";
      return;
   }

	$gr1=new cXRID('grid3');

	$n=$rs->FieldCount();
	for ($i=0;$i<$n;$i++) {
      $of1=$rs->FetchField($i);
      array_push($gr1->cols, array($of1->name, 60) );
   }

   $has_agid=false;
	$nrec=0;
	while ($row=$rs->FetchRow()) {
      $row1=array_values($row);
      if (in_array('AGENTID',$row1))
         $has_agid=true;

      $gr1->rows[]=$row1;
   }
   $rs->Close();
	$gr1->play();

	echo "<div id=tbar2>".
      "<button type=button onclick=\"goTo(528)\">RecordSet Info</button>".
      "<button type=button ";
   if ($has_agid) {
      echo " onclick=\"goTo(539)\"";
   }
   else {
      echo " DISABLED ";
   }
   echo ">Group data Info</button>".
      "<button type=button onclick=\"goExDelete(536)\">Drop file</button>".
      "</div>\n";
}
//--------------------------------------
function su_table_rsinfo(&$x2)
{
   $db=&$x2->db;

	// HEADER
  	$tarray=array(
	   array("ADMIN", 0, 'Main'),
	   array('', 0, 'File information')
	   );
	$x2->make_header($tarray);

   $f1=new cFORM('goForm');
	$f1->vars=array( 'FUNCTION'=>'', 'RECORDID'=>$_REQUEST['RECORDID'] );
	$f1->run();

   $query="SELECT * FROM ".$_REQUEST['RECORDID']." LIMIT 1";
	if (($rs=$db->Execute($query))===false) {
		echo "<h6>".$db->ErrorMsg()."</h6>";
		return;
	}

	$gr1=new cXRID('grid4');

	$n=$rs->FieldCount();
	for ($i=0;$i<$n;$i++) {
      $fld=$rs->FetchField($i);
      $ar1=get_object_vars($fld);

      if ($i==0) {
         foreach ($ar1 as $k=>$v) {
            $gr1->cols[]=array($k,50);
         }
      }

      $gr1->rows[]=array_values($ar1);
   }
	$rs->Close();

	$gr1->play();
}
//--------------------------------------
function su_clean_agent_grp()
{
   global $x2;
   $db=&$x2->db;

   $t1=$_REQUEST['RECORDID'];

   $query="DELETE FROM $t1 WHERE AGENTID=?";
   $data=array( (int)$_REQUEST['RECORDID2'] );
   if (!$db->Execute($query, $data))
      echo "<h6>".$db->Errormsg()."</h6>";
}
//--------------------------------------
function su_copy_agent_grp()
{
   global $x2;
   $db=&$x2->db;

   $t1=$_REQUEST['RECORDID'];

   if (($cols=$db->MetaColumnNames($t1))===false) {
      echo "<h6>".$db->ErrorMsg()."</h6>";
      return;
   }

   $autoinc=false;
   $query="SHOW COLUMNS FROM $t1";
   if (($rs=$db->Execute($query))===false) {
      echo "<h6>".$db->ErrorMsg()."</h6>";
      return;
   }
   while ($o1=$rs->FetchNextObject()) {
      if (strcasecmp($o1->EXTRA,'AUTO_INCREMENT')==0) {
         $autoinc=$o1->FIELD;
         // On;y one field can be auto_incremnt
         break;
      }
   }
   $rs->Close();

   $fa2=array();
   foreach ($cols as $c1) {
      if (strcasecmp($c1,'AGENTID')!=0 && strcasecmp($c1,$autoinc)!=0) {
         $fa2[]=$c1;
      }
   }

   $src=(int)$_REQUEST['RECORDID2'];
   $dest=(int)$_REQUEST['RECORDID3'];

   $db->StartTrans();
   do {
      $query="DELETE FROM $t1 WHERE AGENTID=$dest";
      if (!$db->Execute($query)) {
         echo "<h6>".$db->Errormsg()."</h6>";
         break;
      }

      $query="INSERT INTO $t1 (AGENTID,".implode(',',$fa2).") SELECT $dest AGENTID,".implode(',',$fa2)." FROM $t1 WHERE AGENTID=$src";
      if (!$db->Execute($query)) {
         echo "<h6>".__LINE__.$db->Errormsg()."</h6>";
         break;
      }
   } while (false);
   $db->CompleteTrans();
}
//--------------------------------------
function su_table_agent_grp()
{
   global $x2;
   $db=&$x2->db;

   // HEADER
  	$tarray=array(
		   array("ADMIN", 0, 'Main'),
		   array('', 0, 'Group by agent')
		   );
	$x2->make_header($tarray);

   $gr1=new cXRID('grid19');

   $gr1->cols=array(
      array('Agent Id', 100),
      array('Alias', 150),
      array('Count', 100)
   );

   $t1=$_REQUEST['RECORDID'];
   $cols=$db->MetaColumnNames($t1);
   if (!in_array('AGENTID',$cols)) {
      echo "<h6>AGENTID not in field list of [$t1] table!</h6>";
      return;
   }

   $ags=array();
   $query="SELECT AGENTID, LPATH FROM agents WHERE AGENTID>0 ORDER BY AGENTID";
   if (($rs=$db->Execute($query))===false) {
      echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>";
      return;
   }
   while ($o1=$rs->FetchNextObject()) {
      $ags[$o1->AGENTID]=$o1->AGENTID.'# '.$o1->LPATH;
   }
   $rs->Close();
?>
<div id=dlgcpyto title="Copy data to other agent" style="display:none;">
<form>
<table>
<tr>
   <td>Copy #<b id=selid></b> to</td>
   <td><select id=cpyto><?php prAssocSelect($ags,0); ?></select></td>
</tr>
<tr>
   <td></td>
   <td><input type=button value="Do copy" onclick="goDoCopy()"></td>
</tr>
</table>
</form>
</div>
<div class=bgLine>Analysis for <b><?php echo $t1; ?></b></div>
<script language="JavaScript">
var id;
function oclick(stage, row, col)
{
   // global id
	id = this.cells(row, 0).getValue();
	show_popup(document.getElementById('popmenu'));
	return false;
}
function winRet(x)
{
   switch (x)
   {
   case 554:
      $('#selid').text(id);
      $('#dlgcpyto').dialog('open');
      break;
   case 555:
      goRecDelete(555,'<?php echo $t1; ?>',id);
      break;
   }
}
function goDoCopy()
{
   var osel=document.getElementById('cpyto');

   var i=osel.selectedIndex;
   if (i<0) i=0;

   if (osel.options[i].value==id) {
      window.alert('Can not select same agent!')
      return;
   }

   goRecord(554, '<?php echo $t1; ?>', id, osel.options[i].value);
}
$('#dlgcpyto').dialog( {autoOpen:false, modal:true, resizable:false} );
</script>
<?php
   $query="SELECT T1.AGENTID, T2.LPATH, COUNT(*) FROM $t1 T1
      LEFT JOIN agents T2 ON T2.AGENTID=T1.AGENTID
      GROUP BY T1.AGENTID, T2.LPATH";
   if (($rs=$db->Execute($query))===false) {
      echo "<h6>".$db->ErrorMsg()."</h6>";
      return;
   }
   while ($row=$rs->FetchRow()) {
      $gr1->rows[]=$row;
   }
   $rs->Close();

 	$gr1->click='oclick';
 	$gr1->play();

 	// Popup must be drawn last in page in order to have a proper z-index
	$selx=array(
	   array(554, '/img/copy.gif', 'Copy to' ),
	   array(555, '/img/button_drop.png', 'Clean up' )
	);
	draw_popup($selx,true);
}
//--------------------------------------
function convert_db2utf8()
{
   global $x2;
   $db=&$x2->db;

   // HEADER
  	$tarray=array(
      array("ADMIN", 0, 'Main'),
      array("javascript:goTo(517)", 0, 'DB File Info'),
		array('', 0, 'Convert to Utf-8')
		);
	$x2->make_header($tarray);

	// BODY
	$query="SHOW TABLE STATUS";
	if (($rs=$db->Execute($query))===false) {
	   echo "<h6>".$db->ErrorMsg()."</h6>";
	   return;
   }
   echo "<table>";
   while ($o1=$rs->FetchNextObject()) {
      echo "<tr><td>".$o1->NAME."</td>
         <td>";
      if (strcasecmp(substr($o1->COLLATION,0,4), 'utf8')!=0) {
         $query="ALTER TABLE ".$o1->NAME." CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci";
         if (!$db->Execute($query))
            echo "<span style=\"color:red\">".$db->ErrorMsg()."</span>";
         else
            echo "Converted";
      }
      else {
         echo "Already o.k.";
      }
      echo "</td></tr>";
   }
   echo "</table>";
   $rs->Close();

   echo "<small>End of data</small>";
}
//--------------------------------------
function convert_lastupdate()
{
   global $x2, $ADODB_FETCH_MODE;
   $db=&$x2->db;

   if (is_mysql($db)) {
      echo "<h6>Operation supported only for MySQL</h6>";
      return;
   }

   $tbls = $db->MetaTables();

   // Save old fetch mode
   $ofm=$ADODB_FETCH_MODE;
   // Reset fetch mode
   $ADODB_FETCH_MODE=ADODB_FETCH_ASSOC;

   foreach ($tbls as $table) {
      $query="DESCRIBE $table";
      $rs=$db->Execute($query);

      // Process results
      if ($rs===false) {
         echo "<h6>$table: ".$db->ErrorMsg()."</h6>";
         continue;
      }
      while ($row=$rs->FetchRow()) {
         if ($row['Field']==='LASTUPDATE') {
            if ($row['Default']!=='CURRENT_TIMESTAMPX') {
               $query = "ALTER TABLE $table CHANGE LASTUPDATE LASTUPDATE TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP";
               if (!$db->Execute($query)) {
                  echo "<h6>$table: ".$db->ErrorMsg()."</h6>";
               }
               else {
                  echo "Done for $table\n";
               }
            }
            break;
         }
      }
      $rs->Close();
   }

   // Recover old fetch mode
   $ADODB_FETCH_MODE=$ofm;
}
//--------------------------------------
function su_table_info()
{
   global $x2;
   $db=&$x2->db;
?>
<script language="JavaScript">
var id;
function oclick(stage, row, col)
{
   if (col>=1) {
      // global id
	  id = this.cells(row, 1).getValue();
	  show_popup(document.getElementById('popmenu'));
	}
	return false;
}
function winRet(x)
{
	goRecordEx('ishop_m2.php',x,id);
}
function goExportDB()
{
   var ar1=new Array();
   $('input[name^="ID"]:checked').each(function(){
      ar1.push( $(this).val() );
      });
   if (ar1.length==0) {
      window.alert('No tables selected!');
      return;
   }
   var f1=document.goForm;
   f1.TABLES.value = ar1.join(';');
   goFX('ishop_export.php',501);
}
</script>
<form method=POST name="goForm">
<input type=hidden name="TABLES">
<?php
 	$si=new cSQLINFO('grid6');
 	$si->title="Table Information";
 	$si->checkbox=true;

 	if (is_mysql($db)) {
    	$si->query="SHOW TABLE STATUS";
   }
   else if (strpos($db->databaseType,'mssql')!==false) { // mssql, etc.
    	$si->query="SELECT * FROM sysobjects WHERE xtype='U' ORDER BY name";
   }
   else {
      echo "<h6>Not yet implemented for Database type ".$db->databaseType."</h6>";
   }

 	$si->click='oclick';
 	$si->bmargin=false;
 	$si->Execute();
?>
<div id=tbar2>
   <button type=button onclick="goTo(580)">Convert all to UTF-8</button>
   <button type=button onclick="goTo(585)">Convert LASTUPDATE</button>
   <button type=button onclick="goExportDB('ishop_export.php',501)">Export selected</button>
</div>
</form>
<?php
 	// Popup must be drawn last in page in order to have a proper z-index
	$selx=array(
	   array(527, '/img/properties16x16.png', 'Table analysis' ),
	   array(553, '/img/browse16x16.png', 'Group by agent' )
	);
	draw_popup($selx,true);
}
//--------------------------------------
function su_ask_params()
{
   global $x2;
   $db=&$x2->db;

   $tarray=array(
	   array("ADMIN", 0, 'Main'),
	   array('', 0, 'Generic parameters')
	   );
   $x2->make_header($tarray);

   // BODY
   $ar1=array();
   $query="SELECT PNAME, PVALUE FROM agent_main WHERE AGENTID=0 AND
      PNAME IN ('VOICEIP', 'CLR_INACTIVE_SESS', 'PRINTOUT_CATALOG', 'DETACHED_EXE_CATALOG')";
   if (($rs=$db->Execute($query))===false)
      echo "<h6>".$db->ErrorMsg()."</h6>\n";
   else {
      while ($o1=$rs->FetchNextObject()) {
         $ar1[$o1->PNAME]=$o1->PVALUE;
      }
      $rs->Close();
   }
?>
<script language="javascript">
function speak_test()
{
   var voip=document.goForm.VOICEIP.value;
   var url='/pda/ishop_cebt.php?VOICEIP='+encodeURIComponent(voip);
   ow1=window.open(url,'voip','top=100,left=100,height=200,width=300');
   ow1.focus();
}
</script>
<?php
   $f1=new cFORM('goForm');
	$f1->vars=array( 'FUNCTION'=>'538' );
	$f1->open();
?>
<table>
<tr>
   <td class=bgLabel>Voice IP</td>
   <td class=bgInput>
      <input type=TEXT name="VOICEIP" value="<?php echo myhtml(@$ar1['VOICEIP']); ?>">
      <input type=button name="test" value="Voice test" onclick="speak_test()">
   </td>
</tr>
<tr>
   <td class=bgLabel>Clear inactive (mobile) sessions after</td>
   <td class=bgInput><input type=TEXT name="CLR_INACTIVE_SESS" value="<?php
      echo isset($ar1['CLR_INACTIVE_SESS'])?(int)$ar1['CLR_INACTIVE_SESS']:180;
      ?>">
      seconds</td>
</tr>
<tr>
   <td class=bgLabel>PDA Printout catalog</td>
   <td class=bgInput><input type=TEXT name="PRINTOUT_CATALOG" value="<?php
      echo isset($ar1['PRINTOUT_CATALOG'])?$ar1['PRINTOUT_CATALOG']:"c:\\tmp\\printout";
      ?>" size=50>
   </td>
</tr>
<tr>
   <td class=bgLabel>Detached execution catalog</td>
   <td class=bgInput><input type=TEXT name="DETACHED_EXE_CATALOG" value="<?php
      echo isset($ar1['DETACHED_EXE_CATALOG'])?$ar1['DETACHED_EXE_CATALOG']:"c:\\tmp\\detached";
      ?>" size=50>
   </td>
</tr>
<tr>
   <td></td>
   <td class=bgButton>
      <input type=submit name="ok" value="Update">
   </td>
</tr>
</table>
</form>
<?php
}
//--------------------------------------
function su_save_params()
{
   global $x2;
   $db=&$x2->db;

   $t1='agent_main';
   $prim=array( 'AGENTID', 'PNAME' );
   $ar1=array('VOICEIP', 'CLR_INACTIVE_SESS', 'PRINTOUT_CATALOG', 'DETACHED_EXE_CATALOG');
   foreach ($ar1 as $k1) {
      $data=array( 'AGENTID'=>0, 'PNAME'=>$k1, 'PVALUE'=>trim($_REQUEST[$k1]) );
      if (ReplaceEx($db,$t1,$data,$prim)==0)
         echo "<h6>".$db->ErrorMsg()."</h6>\n";
   }
}
//--------------------------------------
function su_show_units()
{
   global $x2;
   $db=&$x2->db;

   // HEADER
   $tarray=array(
	   array("ADMIN", 0, 'Main'),
	   array('', 1, 'Units')
	   );
   $x2->make_header($tarray);
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST name="goForm">
<input type=hidden name="FUNCTION">
<input type=hidden name="TRDATE">
</form>
<!-- BODY -->
<?php
   $query="SELECT XDATE( TRDATE ) AS F1, COUNT(*) AS F2, SUM(BYTES) AS F3 FROM ishop_usage
      WHERE DCCODE=0
      GROUP BY XDATE(TRDATE)
      ORDER BY XDATE(TRDATE)";
   if (!$rs=$db->Execute($query)) {
      echo "<h6>".$db->ErrorMsg()."</h6>\n";
      return;
   }

   echo "<table>\n".
      "<tr class=\"bgHeader\">\n".
      "<td>Date</td>\n".
      "<td>Transactions</td>\n".
      "<td>Units</td>\n".
      "</tr>\n";

   $nrec=0;
   while ($o1=$rs->FetchNextObject()) {
      echo "<tr";
      if (($nrec++)%2) echo " class=\"HILINE\"";
      echo ">\n".
         "<td><b><a href=\"javascript:goUsagePro(511,'".$o1->F1."')\">".GrDate($o1->F1)."</a></b></td>\n".
         "<td>".$o1->F2."</td>\n".
         "<td align=right>".GNFormat($o1->F3,0)."</td>\n".
         "<td><a href=\"javascript:goUsagePro(514,'".$o1->F1."')\"><img src=\"/img/users.png\" border=0></a></td>\n".
         "</tr>\n";
   }
   echo "<tr class=bgButton>\n".
      "<td colspan=10><small>End of file</small></td>\n".
      "</tr>\n".
      "</table>\n";

   $rs->Close();
}
//--------------------------------------
function show_units(&$db)
{
   global $x2;

   // HEADER
	$tarray=array(
		array("javascript:goTo(100)", 1, 'ctr_center'),
		array('', 1, 'Units')
		);
	$x2->make_header($tarray);

	// BODY
	$pg=getPage();
	$pgsz=getPageSize('grid15');

	$f1=new cFORM('goForm');
   $f1->vars=array( 'FUNCTION'=>'', 'TRDATE'=>'', 'RSPAGE'=>'' );
   $f1->run();

   $units=0;
   $query="SELECT UNITS FROM agents WHERE AGENTID=?";
   $data=array($x2->agentid);
   if ($rs=$db->Execute($query,$data)) {
      if ($row=$rs->FetchRow()) $units=$row[0];
      $rs->Close();
   }
   echo "Units left: <b>".number_format($units,0)."</b><BR>&nbsp;\n";

   $query="SELECT XDATE( TRDATE ) AS F1, COUNT(*) AS F2, SUM(BYTES) AS F3 ".
      " FROM ishop_usage WHERE AGENTID=? AND DCCODE=0 GROUP BY F1 ORDER BY F1 DESC";
   $data=array($x2->agentid);
   if (!$rs=$db->PageExecute($query,$pgsz,$pg,$data)) {
      echo "<h6>".$db->ErrorMsg()."</h6>\n";
      return;
   }

   $gr1=new cXRID('grid15');

   array_push($gr1->cols,
		array( 'Date', 120),
		array( 'Transactions', 100 ),
		array( 'Units', 100 )
		);

	$nrec=0;
   while ($o1=$rs->FetchNextObject()) {
      $row1=array(
         "<b><a href=\"javascript:goUsagePro(301,'".$o1->F1."')\">".GrDate($o1->F1)."</a></b>",
         $o1->F2,
         GNFormat($o1->F3,0)
         );

      $gr1->rows[]=$row1;
   }
   $epage=$rs->LastPageNo();
   $rs->Close();

   // Execute
   $gr1->play();

   echo "<div id=tbar2>";
   print_page_navEx($pg,$epage,300);
   echo "</div>";
}
//--------------------------------------
function su_unit_groups()
{
   global $x2;
   $db=&$x2->db;

   // HEADER
	$tarray=array(
		array("ADMIN", 0, 'Main'),
		array("javascript:goTo(510)", 1, 'Units'),
		array('', 0, 'Date analysis')
		);
	$x2->make_header($tarray);
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST name="goForm">
<input type=hidden name="FUNCTION">
<input type=hidden name="TRDATE" value="<?php echo myhtml($_REQUEST['TRDATE']); ?>">
</form>
<!-- BODY -->
<?php
   $x2->xprint('date');
   echo " <b>".GrDate($_REQUEST['TRDATE'])."</b>\n";

   $query="SELECT PHP_SELF, SUM(BYTES) AS F1, AVG(BYTES) AS F2, COUNT(*) AS F3 ".
      " FROM ishop_usage ".
      " WHERE TRDATE LIKE ? AND DCCODE=0 ".
      " GROUP BY PHP_SELF ORDER BY PHP_SELF";
   $data=array( $_REQUEST['TRDATE'].' %' );
   if (!$rs=$db->Execute($query,$data)) {
      echo "<h6>".$db->ErrorMsg()."</h6>\n";
      return;
   }

   echo "<table>\n".
      "<tr class=\"bgHeader\">\n".
      "<td>Page</td>\n".
      "<td>Transactions</td>\n".
      "<td>Total Bytes</td>\n".
      "<td>Average/trans.</td>\n".
      "</tr>\n";

   $nrec=0;
   while ($o1=$rs->FetchNextObject()) {
      echo "<tr";
      if (($nrec++)%2) echo " class=\"HILINE\"";
      echo ">\n".
         "<td>".myhtml($o1->PHP_SELF)."</td>\n".
         "<td align=right>".$o1->F3."</td>\n".
         "<td align=right>".GNFormat($o1->F1,0)."</td>\n".
         "<td align=right>".GNFormat($o1->F2,0)."</td>\n".
         "</tr>\n";
   }

   echo "<tr><td colspan=10 class=bgButton><b>\n".
      "<a href=\"javascript:goTo(514)\">User group</a> |\n".
      "<a href=\"javascript:goTo(515)\">User analysis</a> |\n".
      "<a href=\"javascript:goTo(512)\">Details</a>\n".
      "</b></td></tr></table>\n";

   $rs->Close();
}
//--------------------------------------
function unit_groups(&$db)
{
   global $x2;

   // HEADER
  	$tarray=array(
		array("javascript:goTo(100)", 1, 'ctr_center'),
		array("javascript:goTo(300)", 1, 'Units'),
		array("", 1, 'Analysis')
		);
	$x2->make_header($tarray);
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST name="goForm">
<input type=hidden name="FUNCTION">
<input type=hidden name="TRDATE" value="<?php echo myhtml($_REQUEST['TRDATE']); ?>">
</form>
<!-- BODY -->
<?php
   $x2->xprint('date');
   echo " <b>".GrDate($_REQUEST['TRDATE'])."</b>\n";

   $query="SELECT PHP_SELF, SUM(BYTES) AS F1, AVG(BYTES) AS F2, COUNT(*) AS F3 ".
      " FROM ishop_usage ".
      " WHERE AGENTID=? AND TRDATE LIKE ? AND DCCODE=0 ".
      " GROUP BY PHP_SELF ORDER BY PHP_SELF";
   $data=array($x2->agentid, $_REQUEST['TRDATE'].' %' );
   if (!$rs=$db->Execute($query,$data)) {
      echo "<h6>".$db->ErrorMsg()."</h6>\n";
      return;
   }

   $gr1=new cXRID('grid16');

   array_push($gr1->cols,
      array( 'Page', 150),
      array( 'Transactions', 100),
      array( 'Total Bytes', 100),
      array( 'Average/trans.', 100)
      );

   $nrec=0;
   while ($o1=$rs->FetchNextObject()) {
      $row1=array(
         myhtml($o1->PHP_SELF),
         $o1->F3,
         GNFormat($o1->F1,0),
         GNFormat($o1->F2,0)
         );

      $gr1->rows[]=$row1;
   }
   $rs->Close();

   // Execute
   $gr1->play();

   echo "<div id=tbar2><b><a href=\"javascript:goTo(302)\">".
      $x2->xxlabel('detail')."</a></b></div>";
}
//--------------------------------------
function su_unit_details()
{
   global $x2;
   $db=&$x2->db;

   // HEADER
  	$tarray=array(
		array("ADMIN", 0, 'Main'),
		array("javascript:goTo(510)", 1, 'Units'),
		);
	$x2->make_header($tarray);

	// BODY
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST name="goForm">
<input type=hidden name="FUNCTION" value="512">
<input type=hidden name="TRDATE" value="<?php echo $_REQUEST['TRDATE']; ?>">
<input type=HIDDEN name="RSPAGE">
</form>
<?php
   $pg=getPage();
	$pgsz=getPageSize('grid17');

   $query="SELECT PHP_SELF, FUNCNR, BYTES, LPATH, ".
      " TRDATE, ELAPSED, DCCODE FROM ishop_usage ".
      " LEFT JOIN agents ON(agents.AGENTID=ishop_usage.AGENTID) ".
      " WHERE TRDATE LIKE ? ORDER BY TRID";
   $data=array( $_REQUEST['TRDATE'].' %' );
   if (!$rs=$db->PageExecute($query,$pgsz,$pg,$data)) {
      echo "<h6>".$db->ErrorMsg()."</h6>\n";
      return;
   }

   $gr1=new cXRID('grid17');

   array_push($gr1->cols,
      array( 'Date/Time', 120),
      array( 'Lpath', 120),
      array( 'Page', 120),
      array( 'Function', 80),
      array( 'Bytes', 80),
      array( 'Time elapsed', 80),
      array( 'D/C', 80)
      );

   $nrec=0;
   while ($o1=$rs->FetchNextObject()) {
      $row1=array(
         GrDateTime($o1->TRDATE),
         myhtml($o1->LPATH),
         myhtml($o1->PHP_SELF),
         $o1->FUNCNR,
         $o1->BYTES,
         $o1->ELAPSED,
         "<span style=\"color:".($o1->DCCODE==0?'red">X':'#008000">Π')."</span>"
         );

      $gr1->rows[]=$row1;
   }
   $epage=$rs->LastPageNo();
   $rs->Close();

   // Execute
   $gr1->play();

   echo "<div id=tbar2>";
   print_page_navEx($pg,$epage,512);
	echo "</div>";
}
//--------------------------------------
function unit_details(&$db)
{
   global $x2;

   // HEADER
  	$tarray=array(
		array("javascript:goTo(100)", 1, 'ctr_center'),
		array("javascript:goTo(300)", 1, 'Units'),
		);
	$x2->make_header($tarray);

	// BODY
	$f1=new cFORM('goForm');
   $f1->vars=array( 'FUNCTION'=>'302', 'TRDATE'=>$_REQUEST['TRDATE'], 'RSPAGE'=>'' );
   $f1->run();

   $pg=getPage();
	$pgsz=getPageSize('grid18');

   $query="SELECT PHP_SELF, FUNCNR, BYTES, ".
      " TRDATE, ELAPSED, DCCODE FROM ishop_usage ".
      " WHERE AGENTID=? AND TRDATE LIKE ? ORDER BY TRID";
   $data=array($x2->agentid, $_REQUEST['TRDATE'].' %' );
   if (!$rs=$db->PageExecute($query,$pgsz,$pg,$data)) {
      echo "<h6>".$db->ErrorMsg()."</h6>\n";
      return;
   }

   $gr1=new cXRID('grid18');

   array_push($gr1->cols,
      array( 'Date/Time', 120),
      array( 'Page', 120),
      array( 'Function', 80),
      array( 'Bytes', 80),
      array( 'Time elapsed', 80),
      array( 'D/C', 80)
      );

   $nrec=0;
   while ($o1=$rs->FetchNextObject()) {
      $row1=array(
         GrDateTime($o1->TRDATE),
         myhtml($o1->PHP_SELF),
         $o1->FUNCNR,
         $o1->BYTES,
         $o1->ELAPSED,
         "<span style=\"color:".($o1->DCCODE==0?'red">X':'#008000">Π')."</span>"
         );

      $gr1->rows[]=$row1;
   }
   $epage=$rs->LastPageNo();
   $rs->Close();

   // Execute
   $gr1->play();

   echo "<div id=tbar2>";
   print_page_navEx($pg,$epage,302);
	echo "</div>";
}
//--------------------------------------
function try_agent_auth(&$db)
{
   $query="SELECT PASSWD FROM agents WHERE LPATH=?";
   $data=array($_REQUEST['RECORDID']);
   if (($rs=$db->Execute($query,$data))===false) {
      echo "<h6>".$db->ErrorMsg()."</h6>\n";
      return false;
   }
   $o1=$rs->FetchNextObject();
   $rs->Close();
   if ($o1===false) {
      echo "<h6>Agent login failed!</h6>\n";
      return false;
   }

   $_REQUEST['LPATH']=$_REQUEST['RECORDID'];
   $_REQUEST['PASSWD']=$o1->PASSWD;
   return true;
}
//--------------------------------------
function show_login_message()
{
?>
<div id=loginmsg title="Login message" style="display:none">
<img src="/img/greece.png">
Παρακαλούμε μην χρησιμοποιείτε το πλήκτρο <i>back</i> του Web browser, αλλά μόνο
τους συνδέσμους (links) και τα κουμπιά υποβολής.
<hr>
<img src="/img/uk.png">
Please do not use your browser's <i>back</i> button, but do only follow links or
use submit buttons.
</div>
<script>
if (!external || !external.Text || external.Text.indexOf('UniStart')==-1) {
   //$('#loginmsg').dialog( {resizable: false, width:400, modal:false} );
}
</script>
<?php
}
//--------------------------------------
function main_login(&$x2, &$message)
{
	$x2->fu=process_login($message);

	if ($x2->fu>0 && $x2->fu<500) {
		if (empty($x2->lic['X1'])) {
			$x2->fu=0;
			$message='Backoffice license not valid or not available!';
		}
	}

	if ($x2->fu>0) {
      check_for_messages();
      if ($x2->fu<500) {
         // not admin; check for tables (and abort on error)
         check_for_tables();
      }
      show_login_message();
	}
	else {
      echo "<script language=javascript>
         if (window.opener!=null) {
				window.alert('".addslashes($message)."');
            self.close();
         }
         </script>";
   }
}
//--------------------------------------
function set_lang(&$x2)
{
 	$db=&$x2->db;

 	$t1='agent_main';
 	$prim=array('AGENTID', 'PNAME');
 	$data=array('AGENTID'=>$x2->agentid, 'PNAME'=>'MLANGPREF', 'PVALUE'=>$_REQUEST['RECORDID']);
 	if (!ReplaceEx($db,$t1,$data,$prim))
 		echo "<h6>".$db->ErrorMsg()."</h6>\n";

 	$x2->lang=(int)$_REQUEST['RECORDID'];
}
//--------------------------------------
function open_frames()
{
	global $x2;

	echo "<frameset rows=\"*,1\"  FRAMEBORDER=0 FRAMESPACING=0 MARGINHEIGHT=0 MARGINWIDTH=0 name=\"FSET\">\n".
		"<frame src=\"".$_SERVER['PHP_SELF']."?FUNCTION=".
		($x2->isadmin==1000?600:200)."\" name=\"THEMAIN\"/>\n".
		"<frame src=\"about:blank\" name=\"HELPER\"/>\n".
		"</frameset>\n".
		"</html>\n";
}
/* ===========================================================
	End of functions
=========================================================== */
?>
</head>
<?php
//
// MAIN code
//
if (HTTP_HOST==='www.e-invoice.gr')
   $_SESSION['EINVONLY']=1;

if ((int)@$argv>0 && ($j=strrpos(__FILE__, PHP_OS=='WINNT'?'\\':'/'))!==false) {
   @chdir(substr(__FILE__,0,$j));
}

$prof=new cPROFILER1();

if ((int)@$argc>1)
   parse_str($argv[1],$_REQUEST);

$message='';
$db = &db_open();
$x2 = new cX2($db); // fill $fu

if ($x2->fu!=583) {
   echo "<body>";
}

?>
<script>
window.history.forward(1);
</script>
<?php

if ($x2->fu==1) { // confirm login
   main_login($x2, $message);
}
else if ($x2->fu==2) {
   if ( !$x2->validate_session() ) { // err
		$x2->fu = 0;
      $x2->lpath="Unknown";
  	}
	else { // ok
      if ($x2->isadmin==1000) { // ok superuser
			$x2->fu = 500;
   		$x2->lpath="Superuser";
   		$data=array( 'LPATH'=>$x2->lpath );
		   $x2->addArray($data);
   	}
		else { // ok, normal
			$x2->read_agent_main();
			$x2->fu = 100;
		}
	}
	show_login_message();
}
else if ($x2->fu > 0) {
 	if ( !$x2->validate_session() ) { // err
		$x2->fu = 0;
      $x2->lpath="Unknown";
  	}
	else { // ok
	   do {
   	   if ($x2->fu == 600) {
	         if (try_agent_auth($db)) {
	            main_login($x2, $message);
	            break;
	         }
         }

   		$x2->read_agent_main();

   		$sync_customer=array(
            $x2->xlabel('all_of_company'),
            $x2->xlabel('all_of_salesman')
         );
      } while (false);
	}
}

if ($x2->fu != 583 && $x2->fu != 584) {
   //print_page_header($x2->fu); // ishop.inc.php
}

if (!empty($message))
	umessageT($message);

class cTIMER2 {
   var $t1;

   function __construct()
   {
      $this->t1=time();
      echo "<h5>Server time (start)= ".date('Y-m-d H:i:s', $this->t1)."</h5>\n";
   }

   function check_files(&$db)
   {
      require_once('ishop_tables.php');
 	   check_tables($db);
 	   check_innodb($db);
 	   echo "<h5>Database files created/ confirmed!</h5>\n";
   }

   function load_lexicon(&$db)
   {
      // Load lexicon
      remake_lex();
      echo "<h5>Lexicon (re)loaded!</h5>";
   }

   function finalize()
   {
      echo "<h5>Time elapsed= ".(time()-$this->t1)." seconds</h5>
         <table width=80%><tr><td class=bgButton><b>
         <a href=\"javascript:location.reload(true)\">Refresh</a> |
         <a href=\"ishop_m2.php\">Login</a>
         </b></td></tr></table>\n";
   }
}

switch ($x2->fu)
{
case -4:
   require_once('ishop_tables.php');
   $ct2=new cTIMER2();
   chk_tables_generic($db);
   $ct2->finalize();
   break;
case -3: // Reload lexicon
   $ct2=new cTIMER2();
   $ct2->load_lexicon($db);
   $ct2->finalize();
 	break;
case -2: // Recheck files
   $ct2=new cTIMER2();
   $ct2->check_files($db);
   $ct2->finalize();
 	break;
case -1: // Reset all
   $ct2=new cTIMER2();
   $ct2->check_files($db);
   $ct2->load_lexicon($db);
   $ct2->finalize();
 	break;
case 0:
	request_login();
	break;
case 3:
	m1_logout();
	request_login();
	break;
case 19:
   show_versions();
   break;
case 20:
	request_generic();
	break;
case 21:
	update_generic();
	main_menu();
	break;
case 22:
   list_sessions($issuper=false);
   break;
case 23:
	drop_session();
   list_sessions($issuper=false);
   break;
case 24:
   session_details();
   break;
case 25:
   request_ordPlus();
   break;
case 26:
   upd_ordPlus();
	request_generic();
	break;
case 27:
   request_gmap();
   break;
case 28:
   upd_gmap();
   request_generic();
   break;
case 29:
   request_sms();
   break;
case 30:
   upd_sms();
   request_generic();
   break;
case 31:
   show_php_config();
   break;
case 43:
	request_new_pass();
	break;
case 44:
	update_pass();
	m1_logout();
	request_login();
	break;
case 45:
   request_sync();
   break;
case 46:
   update_sync();
   break;
case 47:
   show_license($db);
   break;
case 48:
   show_php_vars();
   break;
case 49:
   show_all_properties();
   break;
case 50:
   show_x2_vars();
   break;
/*
case 54:
   agent_signavail_list($x2);
   break;
case 55:
   agent_signrequest_list($x2);
   break;
*/
case 100:
	main_menu();
	if ($x2->opetype!=2 && $x2->opetype!=7) {
      // Not 'E-Invoice' type
   	php_checks(true);
   }
	break;
case 101:
	set_lang($x2);
	main_menu();
	break;
case 200:
	ask_test($x2);
	break;
case 201:
	do_test($x2);
	break;
case 300:
   show_units($db);
   break;
case 301:
   unit_groups($db);
   break;
case 302:
   unit_details($db);
   break;
case 500:
   su_agent_main();
   break;
case 501:
	add_agent($db);
   su_agent_list($x2);
   break;
case 502:
	del_agent($x2);
	break;
case 503:
	show_lex_pending(); // all data
	break;
case 504:
   su_agent_menu($x2);
   break;
case 505:
	su_set_opetype($x2);
   su_agent_list($x2);
   break;
case 506:
	su_reset_passwd();
   su_agent_list($x2);
   break;
case 507:
	del_lex();
	show_lex_pending(); // all data
	break;
case 508:
	textout_lex($x2);
	break;
case 509:
	su_resel_list($x2);
	break;
case 510:
   su_show_units();
   break;
case 511:
   su_unit_groups();
   break;
case 512:
   su_unit_details();
   break;
case 514:
   su_show_users();
   break;
case 515:
   su_show_user_page();
   break;
case 516:
   require_once('ishop_tables.php');
   //check_tables($db);
   chk_tables_generic($db);
   //echo "<h6>Done!</h6>\n";
   su_agent_main();
   break;
case 517:
	su_table_info();
	break;
case 518:
	su_db_status();
	break;
case 519:
	su_db_vars();
	break;
case 520:
   ABC_DropCustomers();
   break;
case 521:
   ABC_MakeCustomers();
   break;
case 522:
   ABC_DropItems();
   break;
case 523:
   ABC_MakeItems();
   break;
case 524:
   ABC_DropMoMain($x2);
   break;
case 525:
   ABC_MakeMoMain($x2);
   break;
case 526:
	su_reselist_all($x2);
	break;
case 527:
	su_table1_details($x2);
	break;
case 528:
	su_table_rsinfo($x2);
	break;
case 529:
	show_lex_content($x2);
	break;
case 530:
	list_langui($x2);
	break;
case 531:
	add_langui();
	list_langui($x2);
	break;
case 532:
	del_langui();
	list_langui($x2);
	break;
case 533:
	update_lexicon();
	show_lex_content($x2);
	break;
case 534:
   clear_transactions();
   break;
case 535:
   export_lex_nodata();
   break;
case 536:
   su_drop_table();
   su_table_info();
   break;
case 537:
   su_ask_params();
   break;
case 538:
   su_save_params();
   su_agent_main();
   break;
case 539:
   su_table_groupdata();
   break;
case 540:
   su_request_new_pass();
   break;
case 541:
   if (su_update_pass()) {
   	m1_logout();
	   request_login();
	}
   else {
      su_agent_main();
   }
	break;
case 542:
	list_sessions($issuper=true);
	break;
case 543:
   su_acivate_eafdss($x2); // $_REQUEST['RECORDID'] --> einv_eafdss.EAFDSS
   su_agent_signavail_list($x2);
   break;
case 544:
   su_deacivate_eafdss($x2); // $_REQUEST['RECORDID'] --> einv_eafdss.EAFDSS
   su_agent_signavail_list($x2);
   break;
case 545:
   su_agent_signavail_list($x2);
   break;
case 546:
   su_agent_add_units($x2);
   su_agent_signavail_list($x2);
   break;
case 547:
   su_agent_add_eafdss($x2);
   su_agent_signavail_list($x2);
   break;
case 548:
   su_agent_del_eafdss($x2); // $_REQUEST['RECORDID'] --> einv_eafdss.EAFDSS
   su_agent_signavail_list($x2);
   break;
case 549:
   add_hasp_lic();
	list_sessions($issuper=true);
	break;
case 550:
   su_system_logs();
   break;
case 551:
   su_php_log();
   break;
case 552:
   su_reset_reseller($x2);
	su_reselist_all($x2); // (526)
	break;
case 553:
   su_table_agent_grp();
   break;
case 554:
   su_copy_agent_grp();
   su_table_agent_grp();
   break;
case 555:
   su_clean_agent_grp();
   su_table_agent_grp();
   break;
case 556:
   su_list_all_eafdss($x2);
   break;
case 557:
   check_einv_files($x2);
   su_agent_main();
   break;
case 558:
   su_agent_add_eafdss2($x2);
   su_list_all_eafdss($x2);
   break;
case 559:
   su_acivate_eafdss($x2); // $_REQUEST['RECORDID'] --> einv_eafdss.EAFDSS
   su_list_all_eafdss($x2);
   break;
case 560:
   list_gps_anon($x2);
   break;
case 561:
   apply_gps_agentid($x2);
   list_gps_anon($x2);
   break;
case 562:
   ask_google_api($x2);
   break;
case 563:
   upd_google_api($x2);
   break;
case 564:
   su_deacivate_eafdss($x2); // $_REQUEST['RECORDID'] --> einv_eafdss.EAFDSS
   su_list_all_eafdss($x2);
   break;
case 565:
   su_agent_del_eafdss($x2); // $_REQUEST['RECORDID'] --> einv_eafdss.EAFDSS
   su_list_all_eafdss($x2);
   break;
case 566:
   su_agent_transf_eafdss($x2); // $_REQUEST['EAFDSS'] --> einv_eafdss.EAFDSS
   su_list_all_eafdss($x2);
   break;
case 567:
   su_agent_clr_eafdss_trans($x2);
   su_list_all_eafdss($x2);
   break;
case 568:
   su_check_eafdss_docids();
   break;
case 569:
   su_rename_eafdss($x2);
   su_list_all_eafdss($x2);
   break;
case 570:
   su_check_nulls($x2);
   break;
case 571:
   su_agent_list($x2);
   break;
case 572:
   rename_agent($x2);
   su_agent_list($x2);
   break;
case 573:
   renumber_agent();
   su_agent_list($x2);
   break;
case 574:
   su_agent_signavail_upd();
   su_agent_signavail_list($x2);
   break;
case 575:
   ask_lex_words();
   break;
case 580:
   convert_db2utf8();
   break;
case 581:
   ask_db_query();
   break;
case 582:
   do_db_query();
   break;
case 583:
   su_admin_frame();
   break;
case 584:
   su_adm_frame_left($x2);
   break;
case 585:
   convert_lastupdate();
	su_table_info();
	break;
case 586:
   //su_reset_reseller($x2);
   su_reset_resell_pwd();
	su_reselist_all($x2); // (526)
	break;
default:
   echo "<h6>Incorrect function number: $x2->fu</h6>";
   main_menu();
   break;
}
$db->Close();

?>
<script type="text/javascript">
window.history.forward();
</script>
<?php
   if ($x2->fu != 583) {
      echo "<!-- TE=".$prof->elapsed()." sec -->";
   }
?>
</body mu="<?php showMemUsage(); ?>">
</html>
