<?php
  global $Ajax;
  global $Track;
  global $Log;
  global $Errors;

  include_once '/usr/home/wcard/Include/Host/Include/Site.inc';

  $Ajax         = true;

  $Resp = array();

  if (!isset($_POST['fcn']))
  { $Resp['msg']  = "Unknown Function";
    echo json_encode($Resp);
    return;
  }

  if (isset($_POST['id']))
  { global $cipher;
    include_once HostDBFile("DBEncrypt.inc");
    if (is_numeric($_POST['id']))
    { $_POST['sid'] = $_POST['id'];
      $_POST['id']  = $cipher->encrypt($_POST['id'],true);
    }
    else
    { $_POST['sid'] = $cipher->decrypt($_POST['id'],true);
    }
  }

  function DiagnosticHeader($Label,$page)
  { echo "<div style='width:100%; text-align:center; font-size:large; background-color:#8080FF;'>\r\n";
    echo "  <div class='DebugClose' title='Close Panel' onclick=\"CloseDiagnostic('",$page,"')\">\r\n";
    echo "    <img class='DebugClose' alt='' src='Host/Images/CloseX.png'>\r\n";
    echo "  </div>\r\n";
    echo '  ',$Label,"\r\n";
    echo "</div>\r\n";
  }

  function ShowDiagnosticTableChanged($Table,$Index,$Sort='-1')
  { include_once HostDBFile('DBTblFcns.inc');
    include_once HostDBFile('DBDataFcns.inc');
    include_once HostIncludeFile('EchoOutput.inc');
    $Values = array();
    $Table  = FullTable($Table);
    $Match  = (!empty($_POST['sid']))?($Index."='".$_POST['sid']."'"):'';
    GetValues($Table,$Values,$Match,$Sort);
    EchoTableValues($Table,$Values);
    return;
  }

  function DebugStatus($id,$Msg,$Ok='',$Yes='',$No='',$Cancel='')
  { echo "      <div id='",$id,"' class='DebugStatus'>\r\n";
    echo "        <hr>\r\n";
    echo "          <div id='",$id,"Bar' class='DebugStatusBar'>$Msg</div>\r\n";
    echo "        <hr>\r\n";
    echo "        <div id='",$id,"Btns' class='DebugStatusButtons'>\r\n";
    if(!empty($Ok))
      echo "          <button id='",$id,"Ok' class='DebugButton' onclick=\"",$Ok,"\">Ok</button>\r\n";
    if(!empty($Yes))
      echo "          <button id='",$id,"Yes' class='DebugButton' onclick=\"",$Yes,"\">Yes</button>\r\n";
    if(!empty($No))
      echo "          <button id='",$id,"No' class='DebugButton' onclick=\"",$No,"\">No</button>\r\n";
    if(!empty($Cancel))
      echo "          <button id='",$id,"Cancel' class='DebugButton' onclick=\"",$Cancel,"\">Cancel</button>\r\n";
    echo "        </div>\r\n";
  }

  function SetSessionValue($Field,$Msg,$Page)
  { global $Track;
    global $Log;
    global $Errors;

    include_once HostIncludeFile('SessionsInc.inc');

    if (!empty($_POST['id']))
    { $Status = Session($_POST['id']);
      if (!empty($Field))
      { $Sql = "UPDATE SessionTable SET ".$Field." WHERE SessionID='".$_POST['sid']."'";
        if (!Query($Sql,DBHosting))
        { $Mag .= 'not Updated.';
          $Errors[] = $Msg;
          $Errors[] = $Sql;
          $Errors[] = mysql_error();
        }
        else
        { $Msg .= 'Set.';
          if ($Track) $Log[] = $Msg;
        }
      }
    }
    else $Msg = 'Unspecified Session.';
    DebugStatus('DebugFcns',$Msg,$Ok="CloseDebugPage('".$Page."')");
    return;
  }


  switch($_POST['fcn'])
  { case 'debug_session':
    { global $cipher;
      include HostIncludeFile('Sessions.inc');
      $_SESSION['debug']['session'] = $_POST['sid'];
      echo json_encode($_SESSION['debug']['session']);
      return;
    }
    case 'show_table_data':
    { include HostIncludeFile('Sessions.inc');
      include_once HostDBFile('DBDisplayFcns.inc');
      include_once HostDBFile('DBDataFcns.inc');
      include_once HostDBFile('DBFunctions.inc');
      include_once HostIncludeFile('EchoOutput.inc');
      $Values = array();
      $Tbl    = json_decode($_POST['tbl'],true);
      $Table  = FullTable($Tbl['table']);
      if (empty($Tbl['match']))
      { if (isset($Tbl['id']) && isset($Tbl['index']))
          $Tbl['match'] = $Tbl['index']."='".$Tbl['id']."'";
        else $Tbl['match']  = '';
      }
      $Tbl['count'] = RecordCount($Table['name'],$Tbl['match']);
      DiagnosticHeader($Tbl['header'].'('.$Tbl['count'].')',$Tbl['page']);
      echo "  <div style='width:100%; margin:0px auto; text-align:center; margin-top:5px; margin-bottom:5px; font-size:small; background-color:#D0D0FF;'>\r\n";
      if (!empty($Tbl['sel_table']))
      { echo "    <span style='margin-right:5px;'>\r\n";
        echo "      ",$Tbl['header']," Selector\r\n";
        echo "    </span>\r\n";
        if (empty($Tbl['sel_encrypted']))
          $List = GetColDataByKey($Tbl['sel_table'],$Tbl['sel_display'],$Tbl['sel_key'],$Tbl['sel_db'],$Tbl['sel_match'],$Tbl['sel_sort']);
        else
          $List = GetDecodedColDataByKey($Tbl['sel_table'],$Tbl['sel_display'],$Tbl['sel_key'],$Tbl['sel_db'],$Tbl['sel_match'],$Tbl['sel_sort']);
        ComboOutByKey($List,$Tbl['id'],$_POST['name'].'Selector',25,null,"TableDataSelChanged('".$_POST['name']."')");
      }
      echo "    <span style='margin-left:50px; margin-right:10px;'>\r\n";
      echo "      Page Size\r\n";
      echo "    </span>\r\n";
      echo "    <span style=''>\r\n";
      echo "      <button style='padding:1px;' onclick=\"ShowTableDataSizeChanged('",$_POST['name'],"',25)\">25</button>\r\n";
      echo "      <button style='padding:1px;' onclick=\"ShowTableDataSizeChanged('",$_POST['name'],"',50)\">50</button>\r\n";
      echo "      <button style='padding:1px;' onclick=\"ShowTableDataSizeChanged('",$_POST['name'],"',100)\">100</button>\r\n";
      echo "      <button style='padding:1px;' onclick=\"ShowTableDataSizeChanged('",$_POST['name'],"',200)\">200</button>\r\n";
      echo "      <button style='padding:1px;' onclick=\"ShowTableDataSizeChanged('",$_POST['name'],"',250)\">250</button>\r\n";
      echo "    </span>\r\n";
      echo "  </div>\r\n";
      if ($Tbl['count'] > $Tbl['size'])
      { echo "  <div style='width:100%; margin:0px auto; text-align:center; margin-top:5px; margin-bottom:5px; font-size:small; background-color:#D0D0FF;'>\r\n";
        echo "    <span style='margin-left:50px; margin-right:10px;'>\r\n";
        echo "      Pages\r\n";
        echo "    </span>\r\n";
        echo "    <span style=''>\r\n";
        $j = 1;
        for($i=0;$i<$Tbl['count'];$i+=$Tbl['size'],$j++)
          echo "      <button style='padding:1px;' onclick=\"ShowTableDataPageChanged('",$_POST['name'],"',",$i,")\">",$j,"</button>\r\n";
        echo "    </span>\r\n";
        echo "  </div>\r\n";
      }
      echo "  <div id='",$_POST['name'],"Panel' class='DebugPanel'>\r\n";
      if (empty($Tbl['fields']))  $Tbl['fields']  = '*';
      if (empty($Tbl['low']))
      { if (!empty($Tbl['offset'])) $Tbl['low'] = $Tbl['offset'];
        else                        $Tbl['low'] = null;
      }
      if (empty($Tbl['high']))
      { if (!empty($Tbl['size']))   $Tbl['high'] = $Tbl['size'];
        else                        $Tbl['high'] = 25;
      }
      if (isset($Tbl['low']))       $Tbl['high']  += $Tbl['low'];
      if (empty($Tbl['encrypted']))
        GetValues($Table,$Values,$Tbl['match'],$Tbl['sort'],$Tbl['fields'],$Tbl['low'],$Tbl['high']);
      else
        GetDecryptedValues($Table,$Values,$Tbl['match'],$Tbl['sort'],$Tbl['fields'],$Tbl['low'],$Tbl['high']);
      EchoTableValues($Table,$Values);
      echo "  </div>\r\n";
      return;
    }

// Session Information

    case 'show_session_info':
    { include HostIncludeFile('Sessions.inc');
      include_once HostDBFile('DBDisplayFcns.inc');
      include_once HostDBFile('DBDataFcns.inc');
      include_once HostDBFile('DBFunctions.inc');
      include HostIncludeFile('EchoOutput.inc');
      DiagnosticHeader('Session Information','session_info_page');
      echo "    <div style='width:100%; text-align:center; font-size:large; background-color:#8080FF;'>\r\n";
      echo "      <span style='margin-right:5px;'>\r\n";
      echo "        Session Selector\r\n";
      echo "      </span>\r\n";
      $List = GetColDataByKey('SessionTable','SessionID','SessionID',DBHosting,'',-1);
      ComboOutByKey($List,$_POST['sid'],'SessionInfoSessionSelector',25,null,"SessionSelChanged(this,'show_session_info','session_info_page')");
      echo "    </div>\r\n";
      echo "  <div class='DebugPanel'>\r\n";
      if (empty($_POST['id']))
      { echo "Session Unspecified.\r\n";
        echo "  </div>\r\n";
        return;
      }
      $Values = array();
      $Table  = FullTable('SessionTable');
      GetDecryptedValue($Table,$Values,"SessionID='".$_POST['sid']."'");
      EchoTableValue($Table,$Values['str']);
      echo "  </div>\r\n";
      return;
    }
    case 'show_session_vars':
    { include HostIncludeFile('SessionsInc.inc');
      include_once HostDBFile('DBDisplayFcns.inc');
      include_once HostDBFile('DBDataFcns.inc');
      include_once HostDBFile('DBFunctions.inc');
      include HostIncludeFile('EchoOutput.inc');
      if (!empty($_POST['id']))
      { $Data = GetItem('SessionTable','SessionVariable','SessionID',$_POST['sid'],DBHosting);
        ini_set('session.save_path',"/usr/home/wcard/Sessions");
        session_id($Data);                  // Resume this Session.
        ini_set('session.use_cookies',0);   // Don't use Cookies.
        session_start();                    // Start the session.
        if (session_id()) $SessionData = $_SESSION;
        if (empty($SessionData))
        { $Archive  = GetArchive($_POST['sid']);
          if (empty($Archive['resp'])) $SessionData = $Archive['session'];
        }
      }
      DiagnosticHeader('Session Variables','session_vars_page');
      echo "    <div style='width:100%; text-align:center; font-size:large; background-color:#8080FF;'>\r\n";
      echo "      <span style='margin-right:5px;'>\r\n";
      echo "        Session Selector\r\n";
      echo "      </span>\r\n";
      $List = GetColDataByKey('SessionTable','SessionID','SessionID',DBHosting,'',-1);
      ComboOutByKey($List,$_POST['sid'],'SessionVariableSessionSelector',25,null,"SessionSelChanged(this,'show_session_vars','session_vars_page')");
      echo "    </div>\r\n";
      echo "  <div class='DebugPanel'>\r\n";
      if (empty($_POST['id']))
      { echo "    Unknown Session.\r\n";
        echo "  </div>\r\n";
        return;
      }
      if (empty($SessionData))
      { echo "  <div class='DebugPanel' style='text-align:center;'>\r\n";
        if (!empty($Archive['resp']))
        { if (is_string($Archive['resp']))
            echo "    ",$Archive,"<br>\r\n";
          else
          { foreach ($Archive['resp'] as $resp)
              echo "    ",$resp,"<br>\r\n";
          }
        }
        echo "    Session ",$_POST['sid']," Variables are not Available.\r\n";
        echo "  </div>\r\n";
        return;
      }
      echo "  <div class='DebugPanel'>\r\n";
      EchoValues($SessionData);
      echo "  </div>\r\n";
      return;
    }
    case 'show_session_cookies':
    { include HostIncludeFile('Sessions.inc');
      include HostIncludeFile('EchoOutput.inc');
      DiagnosticHeader('Session Cookies','session_cookies');
      echo "  <div class='DebugPanel'>\r\n";
      if (empty($_COOKIE))
      { echo "There are no cookies set.\r\n";
        echo "  </div>\r\n";
        return;
      }
      EchoCookies($_COOKIE);
      echo "  </div>\r\n";
      return;
    }
    case 'show_browser_info':
    { include HostIncludeFile('Sessions.inc');
      include_once HostDBFile('DBDisplayFcns.inc');
      include_once HostDBFile('DBDataFcns.inc');
      include_once HostDBFile('DBFunctions.inc');
      include HostIncludeFile('EchoOutput.inc');
      DiagnosticHeader('Browser Info','browser_info_page');
      echo "  <div class='DebugPanel'>\r\n";
      echo "    <div style='width:100%; text-align:center; font-size:large; background-color:#8080FF;'>\r\n";
      echo "      <span style='margin-right:5px;'>\r\n";
      echo "        Session Selector\r\n";
      echo "      </span>\r\n";
      $List = GetColDataByKey('SessionTable','SessionID','SessionID',DBHosting,'',-1);
      ComboOutByKey($List,$_POST['sid'],'BrowserSessionSelector',25,null,"SessionSelChanged(this,'show_browser_info','browser_info_page')");
      echo "    </div>\r\n";
      $Agent  = GetItem('SessionTable','SessionAgent','SessionID',$_POST['sid'],DBHosting);
      EchoValues(get_browser($Agent,true));
      echo "  </div>\r\n";
      return;
    }

// Session Functions

    case 'session_agree':     SetSessionValue("SessionAcceptedTerms='2'",'Session Table Session '.$_POST['sid'].' Agreed was ','session_fcn_page');  return;
    case 'session_disagree':  SetSessionValue("SessionAcceptedTerms='1'",'Session Table Session '.$_POST['sid'].' Disagreed was ','session_fcn_page');  return;
    case 'session_no_terms':  SetSessionValue("SessionAcceptedTerms='0'",'Session Table Session '.$_POST['sid'].' Terms Unknown was ','session_fcn_page');  return;
    case 'session_resume':    include HostIncludeFile('SessionsInc.inc');
                              $Status = Session($_POST['id']);
                              $_SESSION['acc']    = time();
                              $_SESSION['status'] = (empty($_SESSION['admin_session']))?9:5;
                              SetSessionValue("SessionStatus='".$_SESSION['status']."', SessionEnd='0', SessionAcc='".$_SESSION['acc']."'",'Session Table Session '.$_POST['sid'].' Resume was ','session_fcn_page');
                              return;
    case 'session_timeout':   include HostIncludeFile('SessionsInc.inc');
                              $Status = Session($_POST['id']);
                              $_SESSION['acc']    = time()-$_SESSION['timeout']-75;
                              $_SESSION['status'] = 3;
                              SetSessionValue("SessionStatus='".$_SESSION['status']."', SessionEnd='0', SessionAcc='".$_SESSION['acc']."'",'Session Table Session '.$_POST['sid'].' Timeout was ','session_fcn_page');
                              return;
    case 'session_expire':    include HostIncludeFile('SessionsInc.inc');
                              $Status = Session($_POST['id']);
                              $_SESSION['acc']    = time()-$_SESSION['timeout']-1800;
                              $_SESSION['status'] = 4;
                              SetSessionValue("SessionStatus='".$_SESSION['status']."', SessionEnd='0', SessionAcc='".$_SESSION['acc']."'",'Session Table Session '.$_POST['sid'].' Expired was ','session_fcn_page');
                              return;
    case 'session_delete':    include HostIncludeFile('SessionsInc.inc');
                              $Status = Session($_POST['id']);
                              if (session_id() != '')
                              { session_destroy();
                                session_commit();
                                unset($_SESSION);
                              }
                              DebugStatus('DebugFcns','Session '.$_POST['sid'].' was Deleted',$Ok="CloseDebugPage('session_fcn_page')");
                              return;
    case 'session_close':
    { include HostIncludeFile('SessionsInc.inc');
      if (empty($_POST['id'])) return;
      $Status = Session($_POST['id']);
      CloseSession(true,true);
      if(isset($_SESSION['id']))
        DebugStatus('DebugFcns','Session '.$_POST['sid'].' was not Closed',$Ok="CloseDebugPage('session_fcn_page')");
      else
        DebugStatus('DebugFcns','Session '.$_POST['sid'].' was Closed',$Ok="CloseDebugPage('session_fcn_page')");
      return;
    }
    case 'session_close_all':
    { include HostIncludeFile('SessionsInc.inc');
      $Msgs = CloseAllSessions($_POST['session']);
      DebugStatus('DebugFcns',$Msgs,$Ok="CloseDebugPage('session_fcn_page')");
      return;
    }
    case 'session_fault':
    { include HostIncludeFile('SessionsInc.inc');
      if (empty($_POST['id'])) return;
      $Status = Session($_POST['id']);
      $_SESSION['status'] = 6;
      echo json_encode(Array('time' => $_SESSION['acc'],'state' => '6'));
      return;
    }

// Session Diagnostics

    case 'show_session_log':
    { include HostIncludeFile('SessionsInc.inc');
      include_once HostDBFile('DBDisplayFcns.inc');
      include_once HostDBFile('DBDataFcns.inc');
      include_once HostDBFile('DBFunctions.inc');
      include HostIncludeFile('EchoOutput.inc');
      $Archive  = array();
      if (!empty($_POST['id']))
      { $Data = GetItem('SessionTable','SessionEnd','SessionID',$_POST['sid'],DBHosting);
        if (empty($Data))
        { $Status = Session($_POST['id']);
          if (!empty($_SESSION['logs']))
            $SessionLogs  = $_SESSION['logs'];
        }
        if (empty($SessionLogs))
        { $Archive  = GetArchive($_POST['sid']);
          if (empty($Archive['resp'])) $SessionLogs  = $Archive['session']['logs'];
        }
      }
      DiagnosticHeader('Session Logs','session_diag_page');
      echo "    <div style='width:100%; text-align:center; font-size:large; background-color:#8080FF;'>\r\n";
      echo "      <span style='margin-right:5px;'>\r\n";
      echo "        Session Selector\r\n";
      echo "      </span>\r\n";
      $List = GetColDataByKey('SessionTable','SessionID','SessionID',DBHosting,'',-1);
      ComboOutByKey($List,$_POST['sid'],'SessionVariableSessionSelector',25,null,"SessionSelChanged(this,'show_session_log','session_diag_page')");
      echo "    </div>\r\n";
      $Count  = (!empty($SessionLogs))?sizeof($SessionLogs):0;
      if ($Count)
      { if ($Count == 1)
          echo "<div style='width:100%; text-align:center; font-size:large; background-color:#8080FF;'>There is 1 Session Log</div>\r\n";
        else
          echo "<div style='width:100%; text-align:center; font-size:large; background-color:#8080FF;'>There are ",$Count," Session Logs</div>\r\n";
        echo "  <div style='width:100%; margin:0px auto; text-align:center; margin-top:5px; margin-bottom:5px; font-size:small; background-color:#D0D0FF;'>\r\n";
        echo "    <button class='DebugButton' title='Delete Session Logs'  onclick='DeleteSessionLogs()'>Delete Session Logs</button>\r\n";
        echo "  </div>\r\n";
        echo "  <div class='DebugPanel'>\r\n";
        foreach($SessionLogs as $Key => $Log)
        { echo "<div style='width:100%; text-align:left; font-size:medium; background-color:#B0B0FF;'><b>Session Log ",$Key+1,"</b></div>\r\n";
          if (empty($Log))
            echo 'Log is Empty.';
          else
            EchoData($Log);
          echo '<br>';
        }
        echo "  </div>\r\n";
        return;
      }
      $ST = (empty($Data))?'Open':'Closed';
      if (!empty($Archive))
      { if (!empty($Archive['resp']))
        { if (is_string($Archive['resp']))
            echo  'The ',$ST, ' Session ',$_POST['sid'],' was not Archived';
          else
          { echo  'The ',$ST, ' Session ',$_POST['sid'],' Archive is Corrupted';
            foreach ($Archive['resp'] as $resp)
              echo "    ",$resp,"<br>\r\n";
          }
        }
        else  if (!empty($Archive['session']))
          echo  'The ',$ST, ' Session ',$_POST['sid'],' was not Logged';
        else
          echo  'The ',$ST, ' Session ',$_POST['sid'],' Variables were not Archived';
      }
      else
        echo  'The ',$ST, ' Session ',$_POST['sid'],' was not Logged';
      echo "  </div>\r\n";
      return;
    }
    case 'show_session_errors':
    { include HostIncludeFile('SessionsInc.inc');
      include_once HostDBFile('DBDisplayFcns.inc');
      include_once HostDBFile('DBDataFcns.inc');
      include_once HostDBFile('DBFunctions.inc');
      include HostIncludeFile('EchoOutput.inc');
      $Archive  = array();
      $Data     = 0;
      if (!empty($_POST['id']))
      { $Data = GetItem('SessionTable','SessionEnd','SessionID',$_POST['sid'],DBHosting);
        if (empty($Data))
        { $Status = Session($_POST['id']);
          if (!empty($_SESSION['errors']))
            $SessionErrors = $_SESSION['errors'];
        }
        else
        { $Archive  = GetArchive($_POST['sid']);
          if (empty($Archive['resp'])) $SessionErrors = $Archive['session']['errors'];
        }
      }
      DiagnosticHeader('Session Errors','session_diag_page');
      echo "    <div style='width:100%; text-align:center; font-size:large; background-color:#8080FF;'>\r\n";
      echo "      <span style='margin-right:5px;'>\r\n";
      echo "        Session Selector\r\n";
      echo "      </span>\r\n";
      $List = GetColDataByKey('SessionTable','SessionID','SessionID',DBHosting,'',-1);
      ComboOutByKey($List,$_POST['sid'],'SessionVariableSessionSelector',25,null,"SessionSelChanged(this,'show_session_errors','session_diag_page')");
      echo "    </div>\r\n";
      $Count  = (!empty($SessionErrors))?sizeof($SessionErrors):0;
      if ($Count)
      { if ($Count == 1)
          echo "<div style='width:100%; text-align:center; font-size:large; background-color:#8080FF;'>There is 1 Session Error Log</div>\r\n";
        else
          echo "<div style='width:100%; text-align:center; font-size:large; background-color:#8080FF;'>There are ",$Count," Session Error Logs</div>\r\n";
        echo "  <div style='width:100%; margin:0px auto; text-align:center; margin-top:5px; margin-bottom:5px; font-size:small; background-color:#D0D0FF;'>\r\n";
        echo "    <button class='DebugButton' title='Delete Session Logs'  onclick='DeleteSessionErrors()'>Delete Session Errors</button>\r\n";
        echo "  </div>\r\n";
        echo "  <div class='DebugPanel'>\r\n";
        foreach($SessionErrors as $Key => $Log)
        { echo "<div style='width:100%; text-align:left; font-size:medium; background-color:#B0B0FF;'><b>Session Error Log ",$Key+1,"</b></div>\r\n";
          if (empty($Log))
            echo 'Error Log is Empty.';
          else
            EchoData($Log);
          echo '<br>';
        }
        echo "  </div>\r\n";
        return;
      }
      $ST = (empty($Data))?'Open':'Closed';
      if (!empty($Archive))
      { if (!empty($Archive['resp']))
        { if (is_string($Archive['resp']))
          { echo  'The ',$ST, ' Session ',$_POST['sid'],' Errors were not Archived';
            echo $Archive['resp'],'<br>';
          }
          else
          { echo  'The ',$ST, ' Session ',$_POST['sid'],' Archive is Corrupted';
            foreach ($Archive['resp'] as $resp)
              echo "    ",$resp,"<br>\r\n";
          }
        }
        else  if (!empty($Archive['session']))
          echo  'The ',$ST, ' Session ',$_POST['sid'],'  Errors were not Logged';
        else
          echo  'The ',$ST, ' Session ',$_POST['sid'],' Variables were not Archived';
      }
      else
        echo  'Session ',$_POST['sid'],' has no Errors';
      echo "  </div>\r\n";
      return;
    }
    case 'show_diagnostics':
    { include HostIncludeFile('SessionsInc.inc');
      include_once HostDBFile('DBDisplayFcns.inc');
      include_once HostDBFile('DBDataFcns.inc');
      include_once HostDBFile('DBFunctions.inc');
      include HostIncludeFile('GetDiagnostics.inc');
      DiagnosticHeader('Diagnostics','session_diag_page');
      echo "    <div style='width:100%; text-align:center; font-size:large; background-color:#8080FF;'>\r\n";
      echo "      <span style='margin-right:5px;'>\r\n";
      echo "        Session Selector\r\n";
      echo "      </span>\r\n";
      $List = GetColDataByKey('SessionTable','SessionID','SessionID',DBHosting,'',-1);
      ComboOutByKey($List,$_POST['sid'],'SessionVariableSessionSelector',25,null,"SessionSelChanged(this,'show_diagnostics','session_diag_page')");
      echo "    </div>\r\n";
      echo "  <div class='DebugPanel'>\r\n";
      if (!empty($_POST['id']))
      { $Status = Session($_POST['id']);
        $Lines  = array();
        $Mask   = intval($_POST['mask'],0);
        GetDiagnosticHTML(&$Lines,$Mask,$_SESSION);
        echo "  <button title='Send Diagnostics' style='background-color:#D0D0FF; font-size:small;' onclick='SendDiagnostics(",$Mask,",",json_encode($Server),")'>Send Diagnostics</button>\r\n";
        echo "</div>\r\n";
        echo Implode($Lines,PHP_EOL);
        return;
      }
      echo "    Unknown Session.\r\n";
      echo "  </div>\r\n";
      return;
    }

// Session Archive

    case 'show_session_archive':
    { include HostIncludeFile('SessionsInc.inc');
      include_once HostDBFile('DBDisplayFcns.inc');
      include_once HostDBFile('DBDataFcns.inc');
      include_once HostDBFile('DBFunctions.inc');
      include HostIncludeFile('EchoOutput.inc');
      DiagnosticHeader('Archived Session Logs','session_archive_page');
      echo "    <div style='width:100%; text-align:center; font-size:large; background-color:#8080FF;'>\r\n";
      echo "      <span style='margin-right:5px;'>\r\n";
      echo "        Session Selector\r\n";
      echo "      </span>\r\n";
      $List = GetColDataByKey('SessionTable','SessionID','SessionID',DBHosting,'',-1);
      ComboOutByKey($List,$_POST['sid'],'SessionVariableSessionSelector',25,null,"SessionSelChanged(this,'show_session_archive','session_archive_page')");
      echo "    </div>\r\n";
      echo "  <div class='DebugPanel'>\r\n";
      if (!empty($_POST['id']))
      { $Archive  = GetArchive($_POST['sid']);
        if (empty($Archive['resp'])) $SessionData = $Archive['session'];
        { echo "<div style='width:100%; text-align:center; font-size:large; background-color:#8080FF;'>Session Variables</div>\r\n";
          if (!empty($Archive['session']))
          { EchoValues($Archive['session']);  // Session Variables
            if (!empty($Archive['session']['errors'])) EchoErrors($Archive['session']['errors']);
            if (!empty($Archive['session']['logs'])) EchoSessionLogs($Archive['session']['logs']);
          }
          else
            echo 'No Session Values Archived';
          echo "<div style='width:100%; text-align:center; font-size:large; background-color:#8080FF;'>Server Variables</div>\r\n";
          if (isset($Archive['server'])) EchoValues($Archive['server']);
          else echo 'Server Variables were not Archived';
          echo "<div style='width:100%; text-align:center; font-size:large; background-color:#8080FF;'>Cookies</div>\r\n";
          if (isset($Archive['cookies'])) EchoValues($Archive['cookies']);
          else echo 'Cookies were not Archived';
          echo "<div style='width:100%; text-align:center; font-size:large; background-color:#8080FF;'>PHP Variables</div>\r\n";
          if (isset($Archive['php_vars'])) EchoValues($Archive['php_vars']);
          else echo 'PHP Variables were not Archived';
          echo "<div style='width:100%; text-align:center; font-size:large; background-color:#8080FF;'>PHP Functions</div>\r\n";
          if (isset($Archive['php_fcns'])) EchoValues($Archive['php_fcns']);
          else echo 'PHP Functions were not Archived';
          echo "<div style='width:100%; text-align:center; font-size:large; background-color:#8080FF;'>Javascript Variables</div>\r\n";
          if (isset($Archive['js_vars'])) EchoValues($Archive['js_vars']);
          else echo 'Javascript Variables were not Archived';
          echo "<div style='width:100%; text-align:center; font-size:large; background-color:#8080FF;'>Javascript Functions</div>\r\n";
          if (isset($Archive['js_fcns'])) EchoValues($Archive['js_fcns']);
          else echo 'Javascript Functions were not Archived';
          echo "  </div>\r\n";
          return;
        }
        if (!empty($Archive['resp']))
        { if (is_string($Archive['resp']))
            echo  $Archive['resp'],"<br>\r\n";
          else
          { foreach ($Archive['resp'] as $resp)
              echo "    ",$resp,"<br>\r\n";
          }
        }
        return;
      }
      echo "Session Unspecified.\r\n";
      echo "  </div>\r\n";
      return;
    }
    case 'session_archive':
    { include HostIncludeFile('SessionsInc.inc');
      include HostIncludeFile('EchoOutput.inc');
      if (!empty($_POST['id']))
      { $Data = GetItem('SessionTable','SessionEnd',$_POST['sid'],DBHosting);
        if (empty($Data))
        { $Status = Session($_POST['id']);
          DiagnosticHeader('Archive Session','session_archive_page');
          echo "    <div style='width:100%; text-align:center; font-size:large; background-color:#8080FF;'>\r\n";
          echo "      <span style='margin-right:5px;'>\r\n";
          echo "        Session Selector\r\n";
          echo "      </span>\r\n";
          $List = GetColDataByKey('SessionTable','SessionID','SessionID',DBHosting,'',-1);
          ComboOutByKey($List,$_POST['sid'],'SessionVariableSessionSelector',25,null,"SessionSelChanged(this,'session_archive','session_archive_page')");
          echo "    </div>\r\n";
          echo "  <div class='DebugPanel'>\r\n";
          if (!empty($_SESSION))
          { $resp = ArchiveSession($_SESSION,$_SESSION['site_control']);
            if (is_string($resp)) echo "    ",$resp,"<br>\r\n";
            else
            { foreach($resp as $Msg)
                echo "    ",$resp,"<br>\r\n";
            }
          }
          else
            echo  "   Unable to Archive Session ",$_POST['sid'],". Session Data is Unavailable.\r\n";
        }
        else
        { DiagnosticHeader('Archive Session','session_archive_page');
          echo "  <div class='DebugPanel'>\r\n";
          echo  "   Unable to Archive Session ",$_POST['sid'],". Session is Closed.\r\n";
        }
      }
      else
      { DiagnosticHeader('Archive Session','session_archive_page');
        echo "  <div class='DebugPanel'>\r\n";
        echo  "   Session was not Specified.\r\n";
      }
      echo "  </div>\r\n";
    }
    case 'archive_diagnostics':
    { $Track  = true;
      include HostIncludeFile('Sessions.inc');
      $msg = ArchiveSession($_SESSION,$_SESSION['site_control']);
      if (!$msg)                  $Resp['msg'][]  = 'Session '.$_SESSION['id']. ' has been Archived.';
      else if (is_number($msg))   $Resp['msg'][]  = 'Session '.$_SESSION['id']. ' has been Archived, '.$Bytes.' written to File';
      else if (is_string($msg))
      { $Resp['msg'][]  = 'Session '.$_SESSION['id']. ' has not been Archived.';
        $Resp['msg'][]  = $msg;
      }
      else
      { $Resp['msg']                  = $msg;
        $Resp['style']['text-align']  = 'left';
      }
      echo json_encode($Resp);
      return;

    }


/*    ISHdrOut('Session Diagnostics','SDPage','Click for Session Diagnostics','DebugSD','DebugCmdHdr');
    echo "      <div id='SDPage' class='DebugCmdBlk' style='display:none;'>\r\n";
    $Count  = (!empty($_SESSION['diag_status']))?sizeof($_SESSION['diag_status']):0;
    echo "        <button class='DebugButton' id='ShowSessionLog' onclick='ShowSessionLog(this)'>",'Session Logs('.$Count.")</button>\r\n";
    echo "        <button class='DebugButton' id='ShowAllDiagnostics' onclick=\"ShowAllDiag(this)\">Show All Diagnostics</button>\r\n";
    echo "        <button class='DebugButton' id='RefreshDiagnostics' onclick=\"RefreshDiagnostics(this)\">Refresh Diagnostics</button>\r\n";
    echo "      </div>\r\n";

*/



    case 'acting_type_chg':
    { include HostIncludeFile('Sessions.inc');
      UpdateValue('MemberTable','MemberActingType',$_POST['value'],'MemberID',$_SESSION['member']);
      UpdateValue('SessionTable','SessionMemberActingType',$_POST['value'],'SessionID',$_SESSION['id']);
      $_SESSION['member_acting_type'] = $_POST['value'];
      return;
    }
    case 'send_diagnostics':
    { include HostIncludeFile('Sessions.inc');
      include HostIncludeFile('GetDiagnostics.inc');
      include HostIncludeFile('EMailToSite.inc');
      $Body   = array('Lines'=>array(),'Text'=>array());
      $Lines  = &$Body['Lines'];
      $Text   = &$Body['Text'];
      $Mask   = intval($_POST['mask'],0);

      GetDiagnosticHTML(&$Lines,$Mask);
      GetDiagnosticText(&$Text,$Mask);

      $Body['text']   = $Text;
      $Body['lines']  = $Lines;
      EMailToSite($_SESSION['member'],'Diagnostic Data',$Body,1,10);
      $Resp['msg']  = 'Diagnostics Sent';
      echo json_encode($Resp);
      return;
    }
    case 'delete_error_msgs':
    { include HostIncludeFile('Sessions.inc');
      if (!isset($_SESSION['errors']))
      { $Resp['msg'] = "There are no error messages to delete.\r\n";
      }
      else
      { $n  = sizeof($_SESSION['errors']);
        unset($_SESSION['errors']);
        $Resp['msg']  = $n . " error messages deleted.\r\n";
      }
      $Resp['elem'][0]['id']    = 'debug_delete_error_btn';
      $Resp['elem'][0]['value'] = 'No Error Msgs';
      $Resp['elem'][1]['id']    = 'error_msgs_text';
      $Resp['elem'][1]['inner'] = 'There are No Error Msgs';
      $Resp['elem'][2]['id']    = 'ShowErrorMessages';
      $Resp['elem'][2]['inner'] = 'Errors('.$Count.')';
      $Resp['status']           = 'ErrorMsgStatus';
      echo json_encode($Resp);
      return;
    }
    case 'delete_session_logs':
    { include HostIncludeFile('Sessions.inc');
      if (!isset($_SESSION['logs']))
      { $Resp['msg'] = "There are no session logs to delete.\r\n";
      }
      else
      { $n  = sizeof($_SESSION['logs']);
        unset($_SESSION['logs']);
        $Resp['msg']  = $n . " session logs deleted.\r\n";
      }
      $Resp['elem'][0]['id']    = 'debug_session_logs_btn';
      $Resp['elem'][0]['value'] = 'No Session Logs';
      $Resp['elem'][1]['id']    = 'session_log';
      $Resp['elem'][1]['inner'] = 'There are No Session Logs';
      echo json_encode($Resp);
      return;
    }
    case 'reset_session_vars':
    { return;
    }
    case 'member_agree':
    { global $Track;
      global $Log;
      global $Errors;

      include HostIncludeFile('Sessions.inc');

      if (!empty($_SESSION['member']))
      { $Sql = "UPDATE MemberTable SET MemberAcceptTerms='2' WHERE MemberID='".$_SESSION['member']."'";
        if (!Query($Sql))
        { $Errors[] = 'Member Table Agreed was not Updated.';
          $Errors[] = $Sql;
          $Errors[] = mysql_error();
          echo json_encode('Member Table Agreed was not Updated.');
        }
        else
        { if ($Track) $Log[] = 'Member Table Agreed was Set.';
          echo json_encode('Member Table Agreed was Set.');
        }
      }
      else echo json_encode('Invalid Member.');
      return;
    }
    case 'member_disagree':
    { global $Track;
      global $Log;
      global $Errors;

      include HostIncludeFile('Sessions.inc');

      if (!empty($_SESSION['member']))
      { $Sql = "UPDATE MemberTable SET MemberAcceptTerms='1' WHERE MemberID='".$_SESSION['member']."'";
        if (!Query($Sql))
        { $Errors[] = 'Member Table Disagreed was not Updated.';
          $Errors[] = $Sql;
          $Errors[] = mysql_error();
          echo json_encode('Member Table Disagreed was not Updated.');
        }
        else
        { if ($Track) $Log[] = 'Member Table Disagreed was Set.';
          echo json_encode('Member Table Disagreed was Set.');
        }
      }
      else echo json_encode('Invalid Session.');
      return;
    }
    case 'member_no_terms':
    { global $Track;
      global $Log;
      global $Errors;

      include HostIncludeFile('Sessions.inc');

      if (!empty($_SESSION['member']))
      { $Sql = "UPDATE MemberTable SET MemberAcceptTerms='0' WHERE MemberID='".$_SESSION['member']."'";
        if (!Query($Sql))
        { $Errors[] = 'Member Table No Terms was not Set.';
          $Errors[] = $Sql;
          $Errors[] = mysql_error();
          echo json_encode('Member Table No Terms was not Set.');
        }
        else
        { if ($Track) $Log[] = 'Member Table No Terms was Set.';
          echo json_encode('Member Table No Terms was Set.');
        }
      }
      else echo json_encode('Invalid Session.');
      return;
    }
    case 'member_cancelled':
    { global $Track;
      global $Log;
      global $Errors;

      include HostIncludeFile('Sessions.inc');
      if (!empty($_SESSION['member']))
      { $Member = EncryptValue($_SESSION['member']);
        $_SESSION['locked_account'] = 2;
        $Sql = "UPDATE MemberTable SET MemberLockoutCount='2' WHERE MemberUserName='".$Member."'";
        if (!Query($Sql))
        { $Errors[] = 'Member Table Cancelled was not Set.';
          $Errors[] = $Sql;
          $Errors[] = mysql_error();
          echo json_encode('Member Table Cancelled was not Set.');
        }
        else
        { if ($Track) $Log[] = 'Member Table Cancelled was Set.';
          echo json_encode('Member Table Cancelled was Set.');
        }
      }
      else echo json_encode('Not a Member.');
      return;
    }
    case 'member_lock':
    { global $Track;
      global $Log;
      global $Errors;

      include HostIncludeFile('Sessions.inc');
      if (!empty($_SESSION['member']))
      { $Member = EncryptValue($_SESSION['member']);
        $_SESSION['locked_account'] = 1;
        $Sql = "UPDATE MemberTable SET MemberLockoutCount='1' WHERE MemberUserName='".$Member."'";
        if (!Query($Sql))
        { $Errors[] = 'Member Record was not Locked.';
          $Errors[] = $Sql;
          $Errors[] = mysql_error();
          echo json_encode('Member Record was not Locked.');
        }
        else
        { if ($Track) $Log[] = 'Member Record was Locked.';
          echo json_encode('Member Record was Locked.');
        }
      }
      else echo json_encode('Not a Member.');
      return;
    }
    case 'member_unlock':
    { global $Track;
      global $Log;
      global $Errors;

      include HostIncludeFile('Sessions.inc');
      if (!empty($_SESSION['member']))
      { $Member = EncryptValue($_SESSION['member']);
        $_SESSION['locked_account'] = 0;
        $Sql = "UPDATE MemberTable SET MemberLockoutCount='0' WHERE MemberUserName='".$Member."'";
        if (!Query($Sql))
        { $Errors[] = 'Member Record was not Unlocked.';
          $Errors[] = $Sql;
          $Errors[] = mysql_error();
          echo json_encode('Member Record was not Unlocked.');
        }
        else
        { if ($Track) $Log[] = 'Member Record was Unlocked.';
          echo json_encode('Member Record was Unlocked.');
        }
      }
      else echo json_encode('Not a Member.');
      return;
    }
    case 'remember':
    { global $Track;
      global $Log;
      global $Errors;

      if (empty($_POST['cookies']))
      { echo json_encode('Cookies are Disabled.');
        return;
      }

      include HostIncludeFile('Sessions.inc');
      if (!empty($_SESSION['member']))
      { $Member = EncryptValue($_SESSION['member']);
        $Name   = ((!empty($_SESSION['admin_session']))?AdminCookie:UserCookie);
        if (setcookie($Name,$Member,time()+3600*24*365,'/','',$_SERVER["HTTPS"],true))
        { if ($Track) $Log[] = '&nbsp;&nbsp;Remember Cookie Set.';
          echo json_encode('Remember Cookie Set.');
          return;
        }
        else
        { if ($Track) $Log[] = '&nbsp;&nbsp;Remember Cookie was not Set.';
          echo json_encode('Remember Cookie was not Set.');
          return;
        }
      }
      if ($Track) $Log[] = '&nbsp;&nbsp;No Member to Remember.';
      echo json_encode('No Member to Remember.');
      return;
    }
    case 'forget':
    { global $Track;
      global $Log;
      global $Errors;

      if (empty($_POST['cookies']))
      { echo json_encode('Cookies are Disabled.');
        return;
      }

      include HostIncludeFile('Sessions.inc');
      if (!empty($_SESSION['member']))
      { $Member = EncryptValue($_SESSION['member']);
        $Name   = ((!empty($_SESSION['admin_session']))?AdminCookie:UserCookie);
        if (setcookie($Name,$Member,1,'/','',$_SERVER["HTTPS"],true))
        { if ($Track) $Log[] = '&nbsp;&nbsp;Remember Cookie Cleared.';
          echo json_encode('Remember Cookie Cleared.');
          return;
        }
        else
        { if ($Track) $Log[] = '&nbsp;&nbsp;Remember Cookie was not Cleared.';
          echo json_encode('Remember Cookie was not Cleared.');
          return;
        }
      }
      if ($Track) $Log[] = '&nbsp;&nbsp;No Member to Forget.';
      echo json_encode('No Member to Forget.');
      return;
    }
    case 'set_user_cookie':
    { global $Track;
      global $Log;
      global $Errors;

      if (empty($_POST['cookies']))
      { echo json_encode('Cookies are Disabled.');
        return;
      }

      include HostIncludeFile('Sessions.inc');
      if (!empty($_SESSION['id']))
      { $ID = $_SESSION['id'];
        if (setcookie(SessionCookie,$ID,time()+3600*24*365,'/','',$_SERVER["HTTPS"],true))
        { if ($Track) $Log[] = '&nbsp;&nbsp;User Session Cookie Set.';
          echo json_encode('User Session Cookie Set.');
          return;
        }
        else
        { if ($Track) $Log[] = '&nbsp;&nbsp;User Session Cookie was not Set.';
          echo json_encode('User Session Cookie was not Set.');
          return;
        }
      }
      if ($Track) $Log[] = '&nbsp;&nbsp;No Session to Set.';
      echo json_encode('No Session to Set.');
      return;
    }
    case 'delete_user_cookie':
    { global $Track;
      global $Log;
      global $Errors;

      if (empty($_POST['cookies']))
      { echo json_encode('Cookies are Disabled.');
        return;
      }

      include HostIncludeFile('Sessions.inc');
      if (!empty($_SESSION['id']))
      { $ID = $_SESSION['id'];
        if (setcookie(SessionCookie,$ID,1,'/','',$_SERVER["HTTPS"],true))
        { if ($Track) $Log[] = '&nbsp;&nbsp;User Session Cookie Cleared.';
          echo json_encode('User Session Cookie Cleared.');
          return;
        }
        else
        { if ($Track) $Log[] = '&nbsp;&nbsp;User Session Cookie was not Set.';
          echo json_encode('User Session Cookie was not Set.');
          return;
        }
      }
      if ($Track) $Log[] = '&nbsp;&nbsp;No Session to Set.';
      echo json_encode('No Session to Set.');
      return;
    }
    case 'set_admin_cookie':
    { global $Track;
      global $Log;
      global $Errors;

      if (empty($_POST['cookies']))
      { echo json_encode('Cookies are Disabled.');
        return;
      }

      include HostIncludeFile('Sessions.inc');
      if (!empty($_SESSION['id']))
      { $ID = $_SESSION['id'];
        if (setcookie(SessionAdminCookie,$ID,time()+3600*24*365,'/','',$_SERVER["HTTPS"],true))
        { if ($Track) $Log[] = '&nbsp;&nbsp;Admin Session Cookie Set.';
          echo json_encode('Admin Session Cookie Set.');
          return;
        }
        else
        { if ($Track) $Log[] = '&nbsp;&nbsp;Admin Session Cookie was not Set.';
          echo json_encode('Admin Session Cookie was not Set.');
          return;
        }
      }
      if ($Track) $Log[] = '&nbsp;&nbsp;No Session to Set.';
      echo json_encode('No Session to Set.');
      return;
    }
    case 'delete_admin_cookie':
    { global $Track;
      global $Log;
      global $Errors;

      if (empty($_POST['cookies']))
      { echo json_encode('Cookies are Disabled.');
        return;
      }

      include HostIncludeFile('Sessions.inc');
      if (!empty($_SESSION['id']))
      { $ID = $_SESSION['id'];
        if (setcookie(SessionAdminCookie,$ID,1,'/','',$_SERVER["HTTPS"],true))
        { if ($Track) $Log[] = '&nbsp;&nbsp;Admin Session Cookie Cleared.';
          echo json_encode('Admin Session Cookie Cleared.');
          return;
        }
        else
        { if ($Track) $Log[] = '&nbsp;&nbsp;Admin Session Cookie was not Set.';
          echo json_encode('Admin Session Cookie was not Set.');
          return;
        }
      }
      if ($Track) $Log[] = '&nbsp;&nbsp;No Session to Set.';
      echo json_encode('No Session to Set.');
      return;
    }
    case 'delete_all_cookies':
    { $Resp   = array();
      $Count  = 0;
      $N      = 0;
      include HostIncludeFile('Sessions.inc');
      foreach ($_COOKIE as $key=>$cookie)
      { $N++;
        if (!setcookie($key,$cookie,1,'/','',$_SESSION['ssl'],'true'))
          $Resp[] = 'Unable to Delete '.$key;
        else
        { $Resp[] = 'Cookie '.$key. ' Deleted';
          $Count++;
        }
      }
      if (empty($_POST['cookies'])) $Resp[] = 'Cookies are Disabled.';
      $Resp[] = $Count.' of '.$N.' Cookies were Deleted.';
      echo json_encode($Resp);
      return;
    }
    case 'session_no_cookies':
    { include HostIncludeFile('Sessions.inc');
      if (empty($_SESSION['id'])) return;
      $_SESSION['status'] = 5;
      echo json_encode(Array('time' => $_SESSION['acc'],'state' => '5'));
      return;
    }
    case 'show_site_options':
    { include HostIncludeFile('Sessions.inc');
      include HostIncludeFile('EchoOutput.inc');
      DiagnosticHeader('Site Options','site_options');
      echo "  <div class='DebugPanel'>\r\n";
      EchoSiteControl($_SESSION['site_control'],'');
      echo "  </div>\r\n";
      return;
    }
    case 'show_guest_sessions':
    { global $Track;
      global $Log;
      global $Errors;

      include HostIncludeFile('Sessions.inc');
      include HostIncludeFile('EchoOutput.inc');

      $Sessions = GetGuestSessions();
      DiagnosticHeader('Guest Sessions','session_guests');
      echo "  <div class='DebugPanel'>\r\n";
      if (empty($Sessions))
      { echo "There are no Guest Sessions.\r\n";
        echo "  </div>\r\n";
        return;
      }
      EchoTableValues(FullTable('SessionTable'),$Sessions);
      echo "  </div>\r\n";
      if ($Track) $_SESSION['logs'][] = $Log;
      return;
    }
    case 'show_server_vars':
    { include HostIncludeFile('Sessions.inc');
      DiagnosticHeader('Server Variables','server_vars');
      echo "  <div class='DebugPanel'>\r\n";
      $Values = $_SESSION['server'];
      if (!empty($Values))
      { ksort($Values);
        foreach($Values as $key => $val) echo $key,' = ',$val,'<br>';
      }
      else echo 'Server Variables not Available';
      echo "  </div>\r\n";
      return;
    }
    case 'show_error_msgs':
    { include HostIncludeFile('Sessions.inc');
      include_once HostIncludeFile('MultiTab.inc');
      DiagnosticHeader('Session Errors','error_msgs');
      $Count  = (!empty($_SESSION['errors']))?sizeof($_SESSION['errors']):0;
      if ($Count)
      { echo "  <div style='width:100%; margin:0px auto; text-align:center; margin-top:5px; margin-bottom:5px; font-size:small; background-color:#D0D0FF;'>\r\n";
        echo "    <button class='DebugButton' title='Delete Error Messages'  onclick='DeleteErrorMsgs()'>Delete Error Msgs</button>\r\n";
        echo "    <button class='DebugButton' title='Brief Error Messages'  onclick='BriefErrorMsgs()'>Brief Error Msgs</button>\r\n";
        echo "    <button class='DebugButton' title='Brief Error Messages with Call Stack' onclick='BriefCallErrorMsgs()'>Brief Call Stack Msgs</button>\r\n";
        echo "    <button class='DebugButton' title='Full Error Message' onclick='FullErrorMsgs()'>Full Error Msgs</button>\r\n";
        echo "    <button class='DebugButton' title='Full Error Message with Call Stack' onclick='FullCallErrorMsgs()'>Full Call Stack Msgs</button>\r\n";
        echo "  </div>\r\n";
        if ($Count == 1)
          echo "<div id='error_msgs_text' style='width:100%; text-align:center; font-size:large; background-color:#8080FF;'>There is 1 Error Message</div>\r\n";
        else
          echo "<div id='error_msgs_text' style='width:100%; text-align:center; font-size:large; background-color:#8080FF;'>There are ",$Count," Error Messages</div>\r\n";
        echo "  <div class='DebugPanel'>\r\n";
        foreach($_SESSION['errors'] as $key => $val)
        { if (isset($val['msg']))
          { echo $key,' = ',$val['msg'],'<br>';
            if (!empty($val['stk']))
            { if (is_array($val['stk']))
              { foreach($val['stk'] as $stk)
                  echo  $stk,'<br>';
              }
              else echo $val['stk'],'<br>';
            }
            if (isset($val['sql']))
            { foreach($val['sql'] as $sql)
                echo  $sql,'<br>';
            }
          }
          else echo $key,' = ',$val,'<br>';
        }
        $D = MTHLblStatus('ErrorMsgStatus','var ErrorMsgStatus',
          MTSBtns('ErrorMsgStatus',
            MTSBtn('Ok','ErrorMsgStatus-Ok','Ok'),
            MTSBtn('Yes','ErrorMsgStatus-Yes','Yes'),
            MTSBtn('No','ErrorMsgStatus-No','No')
          )
        );
        MultiTabs('ErrorMsgStatus',$D);
        echo "  </div>\r\n";
        return;
      }
      echo "  <div class='DebugPanel'>\r\n";
      echo "    There are no Error Messages.\r\n";
      echo "  </div>\r\n";
      return;
    }
    case 'show_php_vars':
    { include HostIncludeFile('Sessions.inc');
      include HostIncludeFile('EchoOutput.inc');
      DiagnosticHeader('PHP Variables','php_vars');
      echo "  <div class='DebugPanel'>\r\n";
      EchoData($_SESSION['php_vars']);
      echo "  </div>\r\n";
      return;
    }
    case 'show_php_fcns':
    { include HostIncludeFile('Sessions.inc');
      include HostIncludeFile('EchoOutput.inc');
      DiagnosticHeader('PHP Functions','php_fcns');
      echo "  <div class='DebugPanel'>\r\n";
      EchoData($_SESSION['php_fcns']);
      echo "  </div>\r\n";
      return;
    }
    case 'show_js_vars':
    { include HostIncludeFile('Sessions.inc');
      include HostIncludeFile('EchoOutput.inc');
      DiagnosticHeader('JS Variables','js_vars');
      echo "  <div class='DebugPanel'>\r\n";
      EchoData($_SESSION['js_vars']);
      echo "  </div>\r\n";
      return;
    }
    case 'show_js_fcns':
    { include HostIncludeFile('Sessions.inc');
      include HostIncludeFile('EchoOutput.inc');
      DiagnosticHeader('JS Functions','js_vars');
      echo "  <div class='DebugPanel'>\r\n";
      EchoData($_SESSION['js_fcns']);
      echo "  </div>\r\n";
      return;
    }

    case 'show_debugger':
    { include HostIncludeFile('Sessions.inc');
      if ($_POST['value'] == 2)  $_SESSION['show_debugger'] = true;
      else                       unset($_SESSION['show_debugger']);
      return;
    }
    case 'show_member_accesses':
    { include HostIncludeFile('Sessions.inc');
      include_once HostDBFile('DBDisplayFcns.inc');
      include_once HostDBFile('DBDataFcns.inc');
      include_once HostDBFile('DBFunctions.inc');
      include_once HostIncludeFile('EchoOutput.inc');
      $Values = array();
      $Table  = FullTable('AccessTable');
      $ID     = (isset($_SESSION['member']))?$_SESSION['member']:'';
//      $List   = GetDecodedColDataByKey('MemberTable','MemberUserName','MemberID');
//      GetValues($Table,$Values,"AccessMemberID='".$ID."'");
      DiagnosticHeader('Member Accesses','member_accesses');
      echo "  <div style='width:100%; margin:0px auto; text-align:center; margin-top:5px; margin-bottom:5px; font-size:small; background-color:#D0D0FF;'>\r\n";
      echo "    <span style='margin-right:5px;'>\r\n";
      echo "      Member Selector\r\n";
      echo "    </span>\r\n";
      ComboOutByKey($List,$ID,'MemberAccesses',25,null,"MemberAccessChanged('MemberAccesses')");
      echo "  </div>\r\n";
      echo "  <div id='MemberAccessPanel' class='DebugPanel'>\r\n";
      EchoTableValues($Table,$Values);
      echo "  </div>\r\n";
      return;
    }
    case 'member_access_changed':
    { include_once HostDBFile('DBTblFcns.inc');
      include_once HostDBFile('DBDataFcns.inc');
      include_once HostIncludeFile('EchoOutput.inc');
      $Values = array();
      $Table  = FullTable('AccessTable');
      GetValues($Table,$Values,"AccessMemberID='".$_POST['id']."'");
      EchoTableValues($Table,$Values);
      return;
    }
    case 'show_member_sessions':
    { include HostIncludeFile('Sessions.inc');
      include_once HostDBFile('DBDisplayFcns.inc');
      include_once HostDBFile('DBDataFcns.inc');
      include_once HostDBFile('DBFunctions.inc');
      include_once HostIncludeFile('EchoOutput.inc');
      $Values = array();
      $Table  = FullTable('SessionTable');
      $ID     = (isset($_SESSION['member']))?$_SESSION['member']:'';
//      $List   = GetDecodedColDataByKey('MemberTable','MemberUserName','MemberID');
//      GetValues($Table,$Values,"SessionMemberID='".$ID."'");
      DiagnosticHeader('Member Sessions','member_sessions');
      echo "  <div style='width:100%; margin:0px auto; text-align:center; margin-top:5px; margin-bottom:5px; font-size:small; background-color:#D0D0FF;'>\r\n";
      echo "    <span style='margin-right:5px;'>\r\n";
      echo "      Member Selector\r\n";
      echo "    </span>\r\n";
      ComboOutByKey($List,$ID,'MemberSessions',25,null,"MemberSessionChanged('MemberSessions')");
      echo "  </div>\r\n";
      echo "  <div id='MemberSessionPanel' class='DebugPanel'>\r\n";
      EchoTableValues($Table,$Values);
      echo "  </div>\r\n";
      return;
    }
    case 'member_session_changed':
    { include_once HostDBFile('DBTblFcns.inc');
      include_once HostDBFile('DBDataFcns.inc');
      include_once HostIncludeFile('EchoOutput.inc');
      $Values = array();
      $Table  = FullTable('SessionTable');
      GetValues($Table,$Values,"SessionMemberID='".$_POST['id']."'",-1);
      EchoTableValues($Table,$Values);
      return;
    }
    case 'show_member_info':
    { include HostIncludeFile('Sessions.inc');
      include HostIncludeFile('EchoOutput.inc');
      $Values = array();
      $Table  = FullTable('MemberTable');
      $ID     = $_SESSION['member'];
      GetDecryptedValue($Table,$Values,"MemberID='".$ID."'");
      DiagnosticHeader('Member Information','member_info');
      echo "  <div class='DebugPanel'>\r\n";
      EchoTableValue($Table,$Values['str']);
      echo "  </div>\r\n";
      return;
    }
    case 'show_person_info':
    { include HostIncludeFile('Sessions.inc');
      include HostIncludeFile('EchoOutput.inc');
      $Values = array();
      $ID     = $_SESSION['member'];
      $ID     = GetItem('MemberTable','MemberPersonID','MemberID',$ID);
      $Table  = FullTable('PersonTable');
      GetDecryptedValue($Table,$Values,"PersonMemberID='".$ID."'");
      DiagnosticHeader('Person Information','person_info');
      echo "  <div class='DebugPanel'>\r\n";
      EchoTableValue($Table,$Values['str']);
      echo "  </div>\r\n";
      return;
    }
    case 'mobile_test':
    { include HostIncludeFile('Sessions.inc');
      if (empty($_SESSION['mobile_test']))
      { $_SESSION['mobile_test'] = 1;
        $Resp['class'] = 'DebugButtonDown';
      }
      else
      { $_SESSION['mobile_test'] = 0;
        $Resp['class'] = 'DebugButton';
      }
      echo json_encode($Resp);
      return;
    }
    case 'bot_test':
    { include HostIncludeFile('Sessions.inc');
      if (empty($_SESSION['bot_test']))
      { $_SESSION['bot_test'] = 1;
        $Resp['class'] = 'DebugButtonDown';
      }
      else
      { $_SESSION['bot_test'] = 0;
        $Resp['class'] = 'DebugButton';
      }
      echo json_encode($Resp);
      return;
    }

    echo "        <button class='DebugButton' title='Show Open Guest Sessions' onclick=\"ShowTableData(this,'OpenSessions','Guest')\">Open Sessions</button>\r\n";
    echo "        <button class='DebugButton' title='Show Closed Guest Sessions' onclick=\"ShowTableData(this,'ClosedSessions','Guest')\">Closed Sessions</button>\r\n";
    echo "        <button class='DebugButton' title='Show All Guest Sessions' onclick=\"ShowTableData(this,'AllSessions','Guest')\">All Sessions</button>\r\n";
    echo "        <button class='DebugButton' title='Show Guest Session Accesses' onclick=\"ShowTableData(this,'SessionAccesses','Guest')\">Accesses</button>\r\n";
    echo "        <button class='DebugButton' id='ShowSessionInfo'  onclick=\"ShowDebugItem('guest_info_page','show_guest_info',this)\">Information</button>\r\n";
    echo "        <div id='guest_info_page' class='DebugVars'>\r\n";
    echo "        </div>\r\n";
    echo "      </div>\r\n";

    ISHdrOut('Member Functions','MFPage','Click for Member Functions','DebugMF','DebugCmdHdr');
    echo "        <div id='MFPage' class='DebugCmdBlk' style='display:none;'>\r\n";
    echo "        <button class='DebugButton' title='Show Open Member Sessions' onclick=\"ShowTableData(this,'OpenSessions','Member')\">Open Sessions</button>\r\n";
    echo "        <button class='DebugButton' title='Show Closed Member Sessions' onclick=\"ShowTableData(this,'ClosedSessions','Member')\">Closed Sessions</button>\r\n";
    echo "        <button class='DebugButton' title='Show All Member Sessions' onclick=\"ShowTableData(this,'AllSessions','Member')\">All Sessions</button>\r\n";
    echo "        <button class='DebugButton' title='Show Member Session Accesses' onclick=\"ShowTableData(this,'SessionAccesses','Member')\">Accesses</button>\r\n";
    echo "        <button class='DebugButton' id='ShowMemberInfo'  onclick=\"ShowDebugItem('member_info_page','show_member_info',this)\">Member Information</button>\r\n";
    echo "        <button class='DebugButton' id='ShowPersonInfo'  onclick=\"ShowDebugItem('member_info_page','show_person_info',this)\">Person Information</button>\r\n";
    echo "        <button class='DebugButton' id='SetMemberOptions'  onclick=\"ShowDebugItem('member_info_page','set_member_options',this)\">Options</button>\r\n";




  }
?>
