<?php
  error_reporting(E_ALL);
  ini_set("display_errors", 1);
// Begin Header
  global $URL;
  global $HelpMenu;

  include_once '/usr/home/wcard/Include/Host/Include/iSessionsInc.inc';
  include_once '/usr/home/wcard/Include/Host/DB/Tables/SessionStatusTableConstants.inc';
//  See if Backing up.

  if (!empty($Session->ST->RawRecord[SessionBackup]))
  { if (!empty($Session->BackStop))    // Back up
    { echo  "     <script>\r\n";
      echo  "       window.history.go(-1);\r\n";
      echo  "     </script>\r\n";
      exit();
    }
  }

// See if Signed Agreement

/*  if (!$Session->Admin && !$Session->NoTermsChk && $Session->SessionType && !$Session->SCT->SessionOption[SiteSessionOptionAcceptedTerms])
  { echo "  <script>\r\n";
    echo "    window.location.replace(iPageLink('iTermsOfUse.php').$_SERVER['QUERY_STRING']);\r\n";
    echo "  </script>\r\n";
    exit;
  }*/

  if (isset($NoCache))
  { header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP/1.1
    header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
  }

  if (isset($UnderConstruction)&& (!isset($Session->ST->RawRecord[SessionMemberActingType]) || ($Session->ST->RawRecord[SessionMemberActingType] < 9)))
  { $str = 'location:' . iPageLink('UnderConstruction.php') . '&dt=' . $DocTitle .'&t=' . $Title;
    header($str);
  }

  if (!empty($Session->Browser->Id))
  { echo "<!DOCTYPE HTML><html class='",$Session->Browser->Id,"' lang='en' style='width:99vw; height:100%;'>\r\n";
  }
  else  echo "<!DOCTYPE HTML><html  lang='en' style='width:99vw; height:100%;'>\r\n";
  echo "  <head>\r\n";
  echo "    <meta charset='utf-8'>\r\n";

  if (!empty($Session->MobileRes) && $Session->SuperUser)
  { switch($Session->MobileRes)
    { case 1:   $Session->Width='320';   $Session->Height='480';    break;
    	case 2:   $Session->Width='320';   $Session->Height='568';    break;
     	case 3:   $Session->Width='360';   $Session->Height='640';    break;
     	case 4:   $Session->Width='360';   $Session->Height='740';    break;
     	case 5:   $Session->Width='375';   $Session->Height='667';    break;
     	case 6:   $Session->Width='375';   $Session->Height='812';    break;
     	case 7:   $Session->Width='411';   $Session->Height='731';    break;
     	case 8:   $Session->Width='414';   $Session->Height='736';    break;
     	case 9:   $Session->Width='480';   $Session->Height='853';    break;
    	case 10:  $Session->Width='600';   $Session->Height='960';    break;
     	case 11:  $Session->Width='768';   $Session->Height='1024';   break;
     	case 12:  $Session->Width='800';   $Session->Height='1280';   break;
     	case 13:  $Session->Width='1024';  $Session->Height='1366';   break;
     	case 14:  $Session->Width='1280';  $Session->Height='850';    break;
    }
  }
  if (!empty($Session->Width))
  { echo "    <meta name='viewport' id='viewport' content='width=",$Session->Width,", initial-scale=1.0'>\r\n";
    $Session->SmallScreen = ($Session->Width < 600)?true:false;
  }
  else
  { echo "    <meta name='viewport' id='viewport' content='width=device-width, initial-scale=1.0'>\r\n";
//    echo "     var SmallScreen       = device-width;\r\n";
  }
  if (!empty($Session->PT->RawRecord[PageDocTitle]))
    echo "    <title>",$Session->PT->RawRecord[PageDocTitle],"</title>\r\n";
  else
    echo "    <title>No Title</title>\r\n";
  echo "    <link rel=\"shortcut icon\" href=\"",$URL.'Images/CM-Icon3.gif',"\">\r\n";
  CSSFile('Styles.css');
  if (!empty($MenuFile))
  { if (!$Session->SmallScreen)
      CSSFile('Host/iMenus.css');
    else
      CSSFile('Host/iMenusSmallScreen.css');
  }
?>
      <!--meta http-equiv="content-type" content="text/html; charset=UTF-8"-->
      <meta name="author" content="WTC">

      <!--#exec cgi="/cgi-bin/log.pl" -->

<?php

  JSFile('Host/iAjax.js');

  if (!isset($Std))    JSFile('Host/stdmin.js');
  else if ($Std  == 'std')  JSFile('Host/std.js');

  echo "    <script>\r\n";

  if ($Session->SCT->LogOption(7/*SiteLogJSErrors*/))
    echo "        window.onerror = LogJSErrors;\r\n";

  echo "      var Prev              = 0;\r\n";
  echo "      var Next              = 0;\r\n";
  echo "      var PrevSection       = 0;\r\n";
  echo "      var NextSection       = 0;\r\n";
  echo "      var URL               = '",$URL,"';\r\n";
  echo "      var SessionID         = '",$Session->ID,"';\r\n";
  if ($Session->Admin)
    echo "      var MemberID          = '",(($Session->MemberID)?$Session->MemberID:'0'),"';\r\n";
  echo "      var SmallScreen       = '",$Session->SmallScreen,"';\r\n";
  echo "      var Mobile            = ",((!empty($Session->Mobile))?'true':'false'),";\r\n";
  echo "      var Browser           = '",$Session->Browser->Id,"';\r\n";
  echo "      var SiteName          = '",DB::SiteName(),"';\r\n";
  echo "      var SiteSite          = '",DB::SiteSite(),"';\r\n";
  echo "      var SiteUrl           = '",DB::SiteUrl(),"';\r\n";
  echo "      var MonitorTick       = 0;\r\n";
  echo "      var Monitored         = ",(($Session->SessionType&7) == 4)?1:0,";\r\n";
  echo "      var SessionStatusVar  = ",(!empty($Session->ST->RawRecord[SessionStatus]))?$Session->ST->RawRecord[SessionStatus]:0,";\r\n";
  echo "      var SessionTimeout    = ",(!empty($Session->ST->RawRecord[SessionTimeout]))?$Session->ST->RawRecord[SessionTimeout]:1800,";\r\n";
  echo "      var RecMsgs           = false;\r\n";
  echo "      var Admin             = ",(($Session->Admin)?1:0),";\r\n";
  echo "      var SSL               = ",(($Session->SSL)?1:0),";\r\n";
  echo "      var HTTPType          = '",HTTPType(),"';\r\n";

//  echo "      window.addEventListener('resize',function() { location.reload(); });\r\n";

  if (empty($Session->PT->PageOption(PageOptionNoTimerBar)))
  { echo "      var SessionCounter    = SessionTimeout;\r\n";
    echo "      var SessionTimer      = SessionCounter;\r\n";
?>
      var TimerBar;
      var TimerMarker;
      var TimerText;

      function ResetTimeoutTimer()
      { SessionCounter = SessionTimeout;
        SessionTimer   = SessionCounter;
      }

      function ChkSessionStatus()
      { if (SessionCounter < 0)
<?php  echo "        { if ((SessionCounter >= -600) && ",($Session->PT->PageOption(PageOptionNoTimeout))?1:0,")\r\n";
       echo "            SessionStatusVar  = ",SessionTimedOut,";\r\n";
       echo "          else if ((SessionCounter >= -1200) && ",($Session->PT->PageOption(PageOptionNoExpire))?1:0,")\r\n";
       echo "            SessionStatusVar  = ",SessionExpired,";\r\n"  ?>
        }
        return;
      }

      function SessionTimerEvent()
      { SessionCounter--;
        if (MonitorTick >= 5)
        { if (RecMsgs) GetMsg();
          MonitorTick = 0;
        }
        else MonitorTick++;

        if(typeof SessionUpdate == 'function')
        { SessionUpdate();
        }
        else if (SessionCounter < 0)
        { clearInterval(Timer);
<?php
          if(!$Session->PT->PageOption(PageOptionNoTimeout))
          { if(!isset($Session->PT->RawRecord[PageTimeOut]))
              echo  "          location.assign('",iPageLink('iTimeOut.php'),"');\r\n";
            else if(!empty($Session->PT->RawRecord[PageTimeOut]))
              echo  "          location.assign('",iPageLink($Session->PT->RawRecord[PageTimeOut]),"');\r\n";
            else
              echo  "          return;\r\n";
          }
?>
        }
        if (!TimerBar)
        { TimerBar    = document.getElementById('TimeOutBar');
          if (TimerBar)
          { TimerMarker = document.getElementById('TimeOutMarker');
            TimerText   = document.getElementById('TimeOutText');
            var w = Math.ceil((60/SessionTimer)*100);
              if (w < 1) w = 1;
<?php       if ($Session->Browser->Id != 'MSIE')
              echo "              TimerBar.style.background = 'linear-gradient(to right, #FF0000 '+w+'%, #8080FF '+(w*2)+'%, #C0D0FF 100%)';\r\n";
            else
              echo "              TimerBar.style.background = '#C0D0FF';\r\n";
?>
          }
        }
        else
        { var w  = ((TimerBar.offsetWidth)*((SessionTimer-SessionCounter)/SessionTimer));
          var s;
          var hh = Math.floor(SessionCounter/ 3600),
              mm = Math.floor(SessionCounter/ 60) % 60,
              ss = Math.floor(SessionCounter) % 60;
          TimerText.innerHTML  = (hh ? (hh < 10 ? "0" : "") + hh + ":" : "") + ((mm < 10) && hh ? "0" : "") + mm + ":" + (ss < 10 ? "0" : "") + ss;
          if (TimerMarker)  TimerMarker.style.left = (-(Math.round(w)))+'px';
        }
//        var DebugSetTimer = document.getElementById('set_timer');
//        if (DebugSetTimer && (document.activeElement != DebugSetTimer))
//        { var hh = Math.floor(SessionCounter/ 3600),
//              mm = Math.floor(SessionCounter/ 60) % 60,
//              ss = Math.floor(SessionCounter) % 60;
//          DebugSetTimer.value = (hh ? (hh < 10 ? "0" : "") + hh + ":" : "") + ((mm < 10) && hh ? "0" : "") + mm + ":" + (ss < 10 ? "0" : "") + ss;
//        }
      }
<?php
      echo "    if ((SessionTimer != 0) || (SessionStatusVar == 2))\r\n";
      echo "      var Timer = setInterval(SessionTimerEvent,1000);\r\n";
  }
  echo "    </script>\r\n";
?>


<?php
  if ($Session->Browser->Id == 'MSIE')
  { echo "<!--[if lt IE 9]>\r\n";
      JSFile('Host/html5shiv.js');
    echo "<!--[endif]-->\r\n";
  }
  if (($Session->ST->Flags&DBTrack) && $Session->Admin)
  { $st = json_encode($Log);
    echo "<script>\r\n";
    echo "  var SessionState = eval(",$st,");\r\n";
    echo "</script>\r\n";
  }
?>
