<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");  // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?><html><head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
<BASE HREF="http://<?php echo $_SERVER['HTTP_HOST']; ?>/"/>
<LINK REL="stylesheet" type="text/css" href="ishop/restau.css"/>
<SCRIPT SRC="ishop/restau.js.php"></SCRIPT>
<title>Modern iShopping cart Setup</title>
</head>

<body>
<?php
include("../lib/ado/adodb.inc.php");
include("../lib/generic.inc.php");
include('ishop_all.inc.php');
include('restau_deskx.php');
include('restau.inc.php');
//$include_dbi=true;

$db = &db_open();

$fu=(int)@$_REQUEST['FUNCTION'];

switch ($fu)
{
case 0:
   ask_save($db);
   break;
case 1:
   do_save($db,true);
   break;
case 2:
   echo "<center>";
   unset($_REQUEST['AGENTID']);
   // Save external request
   do_save($db,false);
   echo "</center>";
   break;
}
$db->Close();
?>
</body>
</head>
<?php
//--------------------------------------
function list_names($db, $agentid)
{
	$query="SELECT LOGIN,COMPANY,PHONE,ADDR1,ADDR2,CITY FROM ishop_mbr ".
		" WHERE AGENTID=? AND PHONE LIKE ? ORDER BY COMPANY";
	$xs1='%'.$_REQUEST['TEL'].'%';
   $data=array($agentid, $xs1 );
   if (($rs=$db->Execute($query,$data))===false) {
      echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>";
      return;
   }
?><table><tr>
	<td><div id=PhoneDiv style="height:210;width:740;overflow:hidden;"><table
	border=0 cellpadding=2 cellspacing=0 width=740 class=Torder>
<?php

   $nrec=0;
   while ($o1=$rs->FetchNextObject()) {
      echo "<tr id=pholi".$nrec." onclick=\"phoHili(".$nrec.")\">".
         "<td width=110>".myhtml($o1->LOGIN)."</td>".
         "<td width=240><nobr>".myhtml($o1->COMPANY)."</nobr></td>".
         "<td width=130><b>".myhtml($o1->PHONE)."</b></td>".
         "<td width=260><nobr>".myhtml($o1->ADDR1.' '.$o1->ADDR2.' '.$o1->CITY)."</nobr></td>".
         "</tr>\n";
      $nrec++;
   }
   $rs->Close();

   if ($nrec==0)
   	echo "<tr><td width=740>Δεν υπάρχουν εγγραφές!</td></tr>";

?></table></div></td>
	<td width=7><div class=backSc4><div id=sc4Div class=xscroll></div></div></td>
</tr>
</table>
<button type=button class=LeftUp onclick="phoMoveUp()"></button>
<button type=button class=LeftDown onclick="phoMoveDown()"></button>
<?php
}
//--------------------------------------
function ask_save($db)
{
?>
<center>
<table height=100%>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type=HIDDEN name="FUNCTION" value="1">
<input type=HIDDEN name="TEL" value="<?php echo myhtml($_REQUEST['TEL']); ?>">
<tr><td>Κλήση από <b><?php
   if (empty($_REQUEST['TEL']))
      echo "Αγνωστο αριθμός";
   else
      echo myhtml($_REQUEST['TEL']);
?></b>
<BR>&nbsp;
<?php
	echo "AGENTID=".$_REQUEST['AGENTID']."\n";
   if (empty($_REQUEST['AGENTID'])) {
      echo "<table><tr>".
         "<td>Χειριστής</td>".
         "<td><input type=TEXT name=\"RESELID\"></td>".
         "</tr>".
         "<tr>".
         "<td>Password</td>".
         "<td><input type=PASSWORD name=\"PASSWD\"></td>".
         "</tr></table>";
   }
   else {
		list_names($db, (int)$_REQUEST['AGENTID']);
	}

   echo "<input type=SUBMIT class=FButton id=idok name=\"ok\" value=\"Αποδοχή\">\n".
      "<input type=BUTTON class=FButton onclick=\"external.Close()\" value=\"Απόρριψη\">";
?>
</table>

</td></tr>
</form></table>
</center>
<?php
}
//--------------------------------------
function show_owner($db,$telnr)
{
   while (!empty($telnr) && $telnr{0}=='0')
      $telnr=substr($telnr,1);

   $q2="SELECT COMPANY, NAME, ADDR1, ADDR2, CITY, STATE, COUNTRY, REMARKS COMMENT 
      FROM ishop_mbr 
      WHERE AGENTID=? AND PHONE=?";

	$q3="SELECT T2.COMPANY, T2.NAME, T2.ADDR1, T2.ADDR2, T2.CITY, T2.STATE, T2.COUNTRY, T1.COMMENT FROM ishop_tel T1 
      LEFT JOIN ishop_mbr T2 ON (T2.AGENTID=T1.AGENTID AND T2.LOGIN=T1.MBR_LOGIN) 
      WHERE T1.AGENTID=? AND T1.TELNR=?";

   $s2='';
   do {
      $o2=false;
      $s1=false;
      if (!empty($telnr)) {
         $data=array((int)$_REQUEST['AGENTID'], $telnr);
         if (!($rs2=$db->Execute($q2,$data))) {
            echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>\n";
            continue;
         }
         $o2=$rs2->FetchNextObject();
         $rs2->Close();
         if ($o2===false) { // Master join not found
            $data=array((int)$_REQUEST['AGENTID'],$telnr);
            if (!($rs2=$db->Execute($q3,$data))) {
               echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>\n";
               continue;
            }
            $o2=$rs2->FetchNextObject();
            $rs2->Close();
         }
      }
      else {
         $s1='* Unknown';
      }

      if ($o2!==false) {
         $s1=$o2->COMPANY;
         if (!empty($o2->NAME)) {
            $s1.=' ['.$o2->NAME.']';
         }
         $s1.=' '.$o2->ADDR1.' '.$o2->ADDR2.' '.$o2->CITY.' '.$o2->STATE.' '.$o2->COUNTRY;
         if (!empty($o2->COMMENT)) {
            $s1.=': '.$o2->COMMENT;
         }
         $s2=$s1;
      }
      else if ($s1!==false) {
         $s2=$s1;
      }
   } while (false);
   
   echo "<h5>".myhtml($s2)."</h5>";
}
//--------------------------------------
function do_save($db,$doClose)
{
   $telnr=$_REQUEST['TEL'];
   while (!empty($telnr) && $telnr{0}=='0')
      $telnr=substr($telnr,1);

   do {
      if (empty($_REQUEST['AGENTID'])) {
         $query="SELECT AGENTID FROM reseller WHERE RESELID=? AND PASSWD=?";
         $data=array($_REQUEST['RESELID'], $_REQUEST['PASSWD']);
         if (($rs=$db->Execute($query,$data))===false) {
            echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>";
            break;
         }
         $row=$rs->FetchRow();
         $rs->Close();
         if ($row===false) {
            echo "<h5>Authentication failure!</h5>";
            return;
         }
         $_REQUEST['AGENTID']=$row[0];
      }

      echo "<h5>".date('Y-m-d H:i:s')." ".$_REQUEST['TEL']."</h5>";
      $query="INSERT INTO ishop_telwait ( AGENTID, TELNR, TELDAYTIME) VALUES( ?, ?, NOW() )";
      $data=array( (int)$_REQUEST['AGENTID'], $_REQUEST['TEL'] );
      if (!$db->Execute($query,$data)) {
         echo "<h6>".__LINE__.$db->ErrorMsg()."</h6>";
         break;
      }
      $id=(int)$db->Insert_ID();

      // Show more data
      show_owner($db, $_REQUEST['TEL']);

      // Cause automatic close
?>
<script language="JavaScript">
function closeIt()
{
   try {
      external.Close();
   }
   catch (e) {
      self.close();
   }
}
add_callAlert(<?php echo $id; ?>);
<?php if ($doClose) echo "closeIt();"; ?>
</script>
<?php
   } while (false);

   if ($doClose) {
      echo "<p><button type=button onclick=\"closeIt()\">Close</button>";
   }
}
//--------------------------------------
?>