<?php 

session_start();

if(!$_SESSION['login']){
	header("Location: login.php");
}

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["save_logs"])) {
	
  $errorArray = array();
  $logs_message = '';
  $error = 'no';

  $logging_level = $_POST["logging_level"];
  $log_registration = $_POST["log_registration"];
  $log_intercom = $_POST["log_intercom"];
  $log_checkin = $_POST["log_checkin"];
  $log_multicast = $_POST["log_multicast"];
  $log_ptt = $_POST["log_ptt"];
  $log_soundmasking = $_POST["log_soundmasking"];

  // read the current file
  $config_file = fopen("/etc/nyquist/log.cfg", "r");
  $config_file_array = array();
  if(!$config_file){
    // show error message
    $logs_message = '<div class="isa_error"><i class="fa fa-times-circle"></i><div class="log_message_content">&nbsp;';
    $logs_message .= 'There was a problem reading the Log Configuration file.';
    $logs_message .= '</div></div>';
   } else { 
      // go through the config file one line at a time
      while(!feof($config_file)) {
          $line_text = fgets($config_file);
          array_push($config_file_array,$line_text  );
      }
      fclose($config_file);
  }

  // search through rows and insert changes
  $edited_array = array();
  foreach($config_file_array as $key => $row){

      if(strpos($row, 'LOG_LEVEL =') !== FALSE){
          $new_row = 'LOG_LEVEL = '. $logging_level . '' . PHP_EOL;
          array_push($edited_array, $new_row);

      } else if(strpos($row, 'LOG_REGISTRATION =') !== FALSE){
          $new_row = 'LOG_REGISTRATION = '. $log_registration . '' . PHP_EOL;
          array_push($edited_array, $new_row);

      } else if(strpos($row, 'LOG_INTERCOM =') !== FALSE){
          $new_row = 'LOG_INTERCOM = '. $log_intercom. '' . PHP_EOL;
          array_push($edited_array, $new_row);

      } else if(strpos($row, 'LOG_CHECKIN =') !== FALSE){
          $new_row = 'LOG_CHECKIN = '. $log_checkin . '' . PHP_EOL;
          array_push($edited_array, $new_row);

      } else if(strpos($row, 'LOG_MULTICAST =') !== FALSE){
          $new_row = 'LOG_MULTICAST = '. $log_multicast . '' . PHP_EOL;
          array_push($edited_array, $new_row);

      } else if(strpos($row, 'LOG_PTT =') !== FALSE){
          $new_row = 'LOG_PTT = '. $log_ptt . '' . PHP_EOL;
          array_push($edited_array, $new_row);

      } else if(strpos($row, 'LOG_SOUNDMASKING =') !== FALSE){
          $new_row = 'LOG_SOUNDMASKING = '. $log_soundmasking. '' . PHP_EOL;
          array_push($edited_array, $new_row);	
      } else {
          array_push($edited_array, $row);
      }
  }

  // create new file
  $new_config_file = '';
  foreach($edited_array as $row){
      $new_config_file .= $row;
  }

  // write the file
  $config_file = fopen("/etc/nyquist/log.cfg", "w");

  if(!$config_file){

      $error = 'yes';

      // show error message
      $logs_message = '<div class="isa_error"><i class="fa fa-times-circle"></i><div class="log_message_content">&nbsp;';
      $logs_message .= 'Save failed. There was a problem writing to the Log Configuration file.';
      $logs_message .= '</div></div>';

  } else {
      // complete write
      fwrite($config_file, $new_config_file);
      fclose($config_file);	

      // show error message
    $logs_message = '<div class="isa_success"><i class="fa fa-check-circle"></i><div class="log_message_content">&nbsp;';
    $logs_message .= 'Log settings saved.';
    $logs_message .= '</div></div><br>';
	  
	// restart application
	$script_path = 'sudo /usr/local/bin/restart_application';
	$run_script = shell_exec($script_path);
  }		
} else {
		
	// set up the log form variables
    $logging_level = 'ERROR';
    $log_registration = '0';
    $log_intercom = '0';
    $log_checkin = '0';
    $log_multicast = '0';
    $log_ptt = '0';
    $log_soundmasking = '0';

    // load config file and read in data

    $config_file = fopen("/etc/nyquist/log.cfg", "r");

    if(!$config_file){
        // show error message
        $logs_message = '<div class="isa_error"><i class="fa fa-times-circle"></i><div class="log_message_content">&nbsp;';
        $logs_message .= 'There was a problem reading the Log Settings file.';
        $logs_message .= '</div></div>';
    } else { 
       // go through the config file one line at a time
      while(!feof($config_file)) {

          $line_text = fgets($config_file);

          if(strpos($line_text, 'LOG_LEVEL =') !== FALSE){
              $logging_level = str_replace("LOG_LEVEL = ","",$line_text);
              $logging_level = trim($logging_level);
          }
          if(strpos($line_text, 'LOG_REGISTRATION =') !== FALSE){
              $log_registration = str_replace("LOG_REGISTRATION = ","",$line_text);
              $log_registration = trim($log_registration);
          }
          if(strpos($line_text, 'LOG_INTERCOM =') !== FALSE){
              $log_intercom = str_replace("LOG_INTERCOM = ","",$line_text);
              $log_intercom = trim($log_intercom);
          }
          if(strpos($line_text, 'LOG_CHECKIN =') !== FALSE){
              $log_checkin = str_replace("LOG_CHECKIN = ","",$line_text);
              $log_checkin = trim($log_checkin);
          }
          if(strpos($line_text, 'LOG_MULTICAST =') !== FALSE){
              $log_multicast = str_replace("LOG_MULTICAST = ","",$line_text);
              $log_multicast = trim($log_multicast);
          }
          if(strpos($line_text, 'LOG_PTT =') !== FALSE){
              $log_ptt = str_replace("LOG_PTT = ","",$line_text);
              $log_ptt = trim($log_ptt);
          }
          if(strpos($line_text, 'LOG_SOUNDMASKING =') !== FALSE){
              $log_soundmasking = str_replace("LOG_SOUNDMASKING = ","",$line_text);
              $log_soundmasking = trim($log_soundmasking);
          }
      }
      fclose($config_file);
    }
}	
		
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["save_status"])) {	
				
  $errorArray = array();
  $status_message = '';
  $error = 'no';

  $network_status_reporting = $_POST["network_status_reporting"];
  $network_status_ip_addr = $_POST["network_status_ip_addr"];
  $network_status_port = $_POST["network_status_port"];
  $network_status_intercom = $_POST["network_status_intercom"];
  $network_status_paging = $_POST["network_status_paging"];
  $network_status_checkin = $_POST["network_status_checkin"];

  // read the current file
  $config_file = fopen("/etc/nyquist/network_reporting.config", "r");
  $config_file_array = array();
  if(!$config_file){
    // show error message
    $status_message = '<div class="isa_error"><i class="fa fa-times-circle"></i><div class="log_message_content">&nbsp;';
    $status_message .= 'There was a problem reading the status reporting settings file.';
    $status_message .= '</div></div>';
   } else { 
      // go through the config file one line at a time
      while(!feof($config_file)) {
          $line_text = fgets($config_file);
          array_push($config_file_array,$line_text  );
      }
      fclose($config_file);
  }

  // search through rows and insert changes
  $edited_array = array();
  foreach($config_file_array as $key => $row){

      if(strpos($row, 'NETWORK_STATUS_REPORTING=') !== FALSE){
          $new_row = 'NETWORK_STATUS_REPORTING='. $network_status_reporting . '' . PHP_EOL;
          array_push($edited_array, $new_row);

      } else if(strpos($row, 'NETWORK_STATUS_IP_ADDR=') !== FALSE){
          $new_row = 'NETWORK_STATUS_IP_ADDR='. $network_status_ip_addr . '' . PHP_EOL;
          array_push($edited_array, $new_row);

      } else if(strpos($row, 'NETWORK_STATUS_PORT=') !== FALSE){
          $new_row = 'NETWORK_STATUS_PORT='. $network_status_port. '' . PHP_EOL;
          array_push($edited_array, $new_row);

      } else if(strpos($row, 'NETWORK_STATUS_INTERCOM=') !== FALSE){
          $new_row = 'NETWORK_STATUS_INTERCOM='. $network_status_intercom . '' . PHP_EOL;
		  if($network_status_intercom==''){
			  array_push($edited_array, $row);
		  } else {
			  array_push($edited_array, $new_row);
		  }
      } else if(strpos($row, 'NETWORK_STATUS_PAGING=') !== FALSE){
          $new_row = 'NETWORK_STATUS_PAGING='. $network_status_paging . '' . PHP_EOL;
		  if($network_status_paging==''){
			  array_push($edited_array, $row);
		  } else {
			  array_push($edited_array, $new_row);
		  }
      } else if(strpos($row, 'NETWORK_STATUS_CHECKIN=') !== FALSE){
          $new_row = 'NETWORK_STATUS_CHECKIN='. $network_status_checkin . '' . PHP_EOL;
		   if($network_status_checkin==''){
			  array_push($edited_array, $row);
		  } else {
			  array_push($edited_array, $new_row);
		  }
      } else {
          array_push($edited_array, $row);
      }
  }

  // create new file
  $new_config_file = '';
  foreach($edited_array as $row){
      $new_config_file .= $row;
  }

  // write the file
  $config_file = fopen("/etc/nyquist/network_reporting.config", "w");

  if(!$config_file){

      $error = 'yes';

      // show error message
      $status_message = '<div class="isa_error"><i class="fa fa-times-circle"></i><div class="log_message_content">&nbsp;';
      $status_message .= 'Save failed. There was a problem writing to the status reporting settings file.';
      $status_message .= '</div></div>';

  } else {
      // complete write
      fwrite($config_file, $new_config_file);
      fclose($config_file);	

      // show error message
    $status_message = '<div class="isa_success"><i class="fa fa-check-circle"></i><div class="log_message_content">&nbsp;';
    $status_message .= 'Status reporting settings saved.';
    $status_message .= '</div></div><br>';
	  
	// restart application
	$script_path = 'sudo /usr/local/bin/restart_application';
	$run_script = shell_exec($script_path);
  }
} else {
	
  // set up the status form variables
  $network_status_reporting = 'OFF';
  $network_status_ip_addr = '';
  $network_status_port = '';
  $network_status_intercom = 'OFF';
  $network_status_paging = 'OFF';
  $network_status_checkin = 'OFF';

  // load config file and read in data

  $config_file = fopen("/etc/nyquist/network_reporting.config", "r");

  if(!$config_file){
      // show error message
      $status_message = '<div class="isa_error"><i class="fa fa-times-circle"></i><div class="log_message_content">&nbsp;';
      $status_message .= 'There was a problem reading the status reporting settings file.';
      $status_message .= '</div></div>';
  } else { 
     // go through the config file one line at a time
    while(!feof($config_file)) {

        $line_text = fgets($config_file);

        if(strpos($line_text, 'NETWORK_STATUS_REPORTING=') !== FALSE){
            $network_status_reporting = str_replace("NETWORK_STATUS_REPORTING=","",$line_text);
            $network_status_reporting = trim($network_status_reporting);
        }
        if(strpos($line_text, 'NETWORK_STATUS_IP_ADDR=') !== FALSE){
            $network_status_ip_addr = str_replace("NETWORK_STATUS_IP_ADDR=","",$line_text);
            $network_status_ip_addr = trim($network_status_ip_addr);
        }
        if(strpos($line_text, 'NETWORK_STATUS_PORT=') !== FALSE){
            $network_status_port = str_replace("NETWORK_STATUS_PORT=","",$line_text);
            $network_status_port = trim($network_status_port);
        }
        if(strpos($line_text, 'NETWORK_STATUS_INTERCOM=') !== FALSE){
            $network_status_intercom = str_replace("NETWORK_STATUS_INTERCOM=","",$line_text);
            $network_status_intercom = trim($network_status_intercom);
        }
        if(strpos($line_text, 'NETWORK_STATUS_PAGING=') !== FALSE){
            $network_status_paging = str_replace("NETWORK_STATUS_PAGING=","",$line_text);
            $network_status_paging = trim($network_status_paging);
        }
        if(strpos($line_text, 'NETWORK_STATUS_CHECKIN=') !== FALSE){
            $network_status_checkin = str_replace("NETWORK_STATUS_CHECKIN=","",$line_text);
            $network_status_checkin = trim($network_status_checkin);
        }
    }
    fclose($config_file);
  }
}
?>

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="description" content="">
<meta name="author" content="Bogen Communications LLC">
<meta name="copyright" content="Bogen Communications LLC">
<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests" />
<link rel="shortcut icon" href="">
<link rel="stylesheet" href="/css/styles.css">
<link rel="stylesheet" href="/css/bootstrap.min.css">
<link rel="stylesheet" href="/css/font-awesome/css/font-awesome.min.css">
<link rel="icon" type="image/png" href="/favicon.png" />
<title><?php echo $_SESSION['device_type']; ?> (<?php echo $_SESSION['device_name']; ?>) - Logs</title>
</head>

<body>
<input name="filename" id="filename" type="hidden" value="">
<input name="device_id" id="device_id" type="hidden" value="<?php echo $_SESSION["device_id"];?>">
<div class="container">

<?php include 'navbar.php';?>

<h4><strong><i class="fa fa-file-text-o"></i> View Logs</strong>&nbsp;&nbsp;<a href="/help/<?php echo $_SESSION['device_id']; ?>/#t=<?php echo $_SESSION['help_logs']; ?>" type="button" class="btn btn-xs btn-primary" target="_blank"><i class="fa fa-question-circle"></i></a></h4>
<div id="status"></div>
<div id="message_container"><?php echo $message; ?></div>
<select name="log_selection" id="log_selection"></select>&nbsp;&nbsp;<span id="activity_indicator"></span>
<br>
<div id="export_container"><span id="file_path"></span>&nbsp;&nbsp;<button style="margin:5px;" class="btn btn-xs btn-primary" id="export_button" onClick="export_logfile();"><i class="fa fa-share"></i>&nbsp;&nbsp;Export</button></div>
<br>
<textarea id="log_content" wrap="off" rows="5" readonly class="form-control"></textarea>
<br>
<hr>
<div class="container">
  <div class="row">
    <div class="col-md-6">
		<form id="logs" name="logs" action="logs.php" method="post">
          <h4><strong><i class="fa fa-file-text-o"></i> Log Settings</strong>&nbsp;&nbsp;<a href="/help/<?php echo $_SESSION['device_id']; ?>/#t=<?php echo $_SESSION['help_logs']; ?>" type="button" class="btn btn-xs btn-primary" target="_blank"><i class="fa fa-question-circle"></i></a></h4>
          <div id="logs_message_container"><?php echo $logs_message; ?></div>
          <table class="table-condensed">
              <tr>
                  <td><strong>Logging Level:</strong></td>
                  <td>
                      <select name="logging_level" id="logging_level">
                          <option value="NONE">NONE</option>
                          <option value="ERROR">ERROR</option>
                          <option value="WARNING">WARNING</option>
                          <option value="INFO">INFO</option>
                      </select>
                  </td>
              </tr>
              <tr>
                  <td><strong><u>Logging Categories</u></strong></td>
                  <td></td>
              </tr>
              <tr>
                  <td><strong>Log Registration:&nbsp;&nbsp;<a data-toggle="tooltip" title="Log information related to station registration."><i class='fa fa-lg fa-info-circle'></i></a></strong></td>
                  <td>
                      <select name="log_registration" id="log_registration">
                          <option value="0">No</option>
                          <option value="1">Yes</option>
                      </select>
                  </td>
              </tr>
              <tr>
                  <td><strong>Log Intercom:&nbsp;&nbsp;<a data-toggle="tooltip" title=" Log information related to intercom calls to/from the appliance as well as talkback parameters."><i class='fa fa-lg fa-info-circle'></i></a></strong></td>
                  <td>
                      <select name="log_intercom" id="log_intercom">
                          <option value="0">No</option>
                          <option value="1">Yes</option>
                      </select>
                  </td>
              </tr>
              <tr>
                  <td><strong>Log Check-In:&nbsp;&nbsp;<a data-toggle="tooltip" title="Log information related to station check-ins."><i class='fa fa-lg fa-info-circle'></i></a></strong></td>
                  <td>
                      <select name="log_checkin" id="log_checkin">
                          <option value="0">No</option>
                          <option value="1">Yes</option>
                      </select>
                  </td>
              </tr>
              <tr>
                  <td><strong>Log Multicast:&nbsp;&nbsp;<a data-toggle="tooltip" title="Log information related to incoming multicast pages or audio distribution."><i class='fa fa-lg fa-info-circle'></i></a></strong></td>
                  <td>
                      <select name="log_multicast" id="log_multicast">
                          <option value="0">No</option>
                          <option value="1">Yes</option>
                      </select>
                  </td>
              </tr>
              <tr>
                  <td><strong>Log PTT:&nbsp;&nbsp;<a data-toggle="tooltip" title="Log information related to Push-To-Talk features."><i class='fa fa-lg fa-info-circle'></i></a></strong></td>
                  <td>
                      <select name="log_ptt" id="log_ptt">
                          <option value="0">No</option>
                          <option value="1">Yes</option>
                      </select>
                  </td>
              </tr>
              <tr>
                  <td><strong>Log Sound Masking:&nbsp;&nbsp;<a data-toggle="tooltip" title="Log information related to amplifier sound masking."><i class='fa fa-lg fa-info-circle'></i></a></strong></td>
                  <td>
                      <select name="log_soundmasking" id="log_soundmasking">
                          <option value="0">No</option>
                          <option value="1">Yes</option>
                      </select>
                  </td>
              </tr>
          </table>
          <button id="save_logs" name="save_logs" class="btn btn-xs btn-success" type="submit"><i class="fa fa-floppy-o"></i>&nbsp;&nbsp;Save</button>
        </form>
	  </div>
    <div class="col-md-6">
	 <?php if(!in_array($_SESSION["device_id"], ['nq-io'])) {  // don't show status reporting for IO Controller ?>
	  <form id="status" name="status" action="logs.php" method="post">
          <h4><strong><i class="fa fa-file-text-o"></i> Status Reporting Settings</strong>&nbsp;&nbsp;<a href="/help/<?php echo $_SESSION['device_id']; ?>/#t=<?php echo $_SESSION['help_logs']; ?>" type="button" class="btn btn-xs btn-primary" target="_blank"><i class="fa fa-question-circle"></i></a></h4>
          <div id="status_message_container"><?php echo $status_message; ?></div>
          <table class="table-condensed">
              <tr>
                  <td><strong>Enable Status Reporting:&nbsp;&nbsp;<a data-toggle="tooltip" title="Enable network-based status reporting."><i class='fa fa-lg fa-info-circle'></i></a></strong></td>
                  <td>
                      <select name="network_status_reporting" id="network_status_reporting">
                          <option value="OFF">No</option>
                          <option value="ON">Yes</option>
                      </select>
                  </td>
              </tr>
			  <tr>
                  <td><strong>Network IP Address:&nbsp;&nbsp;<a data-toggle="tooltip" title="The network IP address where status report messages will be sent."><i class='fa fa-lg fa-info-circle'></i></a></strong></td>
                  <td>
                      <input type="text" class="form-control" name="network_status_ip_addr" id="network_status_ip_addr" placeholder="" style="width:200px;" value="">
                  </td>
              </tr>
			  <tr>
                  <td><strong>Network Port:&nbsp;&nbsp;<a data-toggle="tooltip" title="The network port address where status report messages will be sent."><i class='fa fa-lg fa-info-circle'></i></a></strong></td>
                  <td>
                      <input type="text" class="form-control" name="network_status_port" id="network_status_port" placeholder="" style="width:200px;" value="">
                  </td>
              </tr>
              <tr>
                  <td><strong><u>Status Reporting Categories</u></strong></td>
                  <td></td>
              </tr>
              <tr>
                  <td><strong>Intercom:&nbsp;&nbsp;<a data-toggle="tooltip" title="Status reporting related to Intercom."><i class='fa fa-lg fa-info-circle'></i></a></strong></td>
                  <td>
                      <select name="network_status_intercom" id="network_status_intercom">
                          <option value="OFF">No</option>
                          <option value="ON">Yes</option>
                      </select>
                  </td>
              </tr>
              <tr>
                  <td><strong>Paging:&nbsp;&nbsp;<a data-toggle="tooltip" title="Status reporting related to Paging."><i class='fa fa-lg fa-info-circle'></i></a></strong></td>
                  <td>
                      <select name="network_status_paging" id="network_status_paging">
                          <option value="OFF">No</option>
                          <option value="ON">Yes</option>
                      </select>
                  </td>
              </tr>
			  <?php if($_SESSION['device_id']=='nq-asb'){ // Bridge Device Only ?>
              <tr>
                  <td><strong>Check-In:&nbsp;&nbsp;<a data-toggle="tooltip" title="Status reporting related to Check-In."><i class='fa fa-lg fa-info-circle'></i></a></strong></td>
                  <td>
                      <select name="network_status_checkin" id="network_status_checkin">
                          <option value="OFF">No</option>
                          <option value="ON">Yes</option>
                      </select>
                  </td>
              </tr>
			  <? } ?>
          </table>
          <button id="save_status" name="save_status" class="btn btn-xs btn-success" type="submit" onClick="unlockFields();"><i class="fa fa-floppy-o"></i>&nbsp;&nbsp;Save</button>
        </form>
		<?php } ?>
	  </div>
  </div>
</div>
</div>
<script src="/js/jquery.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
</body>
</html>
<script>
// on page load
window.onload = function() {  
	
	get_log_files();
	
	document.getElementById('export_container').style.display = 'none';
	
	document.getElementById('logging_level').value = '<? echo $logging_level;?>';
	document.getElementById('log_registration').value = '<? echo $log_registration;?>';
	document.getElementById('log_intercom').value = '<? echo $log_intercom;?>';
	document.getElementById('log_checkin').value = '<? echo $log_checkin;?>';
	document.getElementById('log_multicast').value = '<? echo $log_multicast;?>';
	document.getElementById('log_ptt').value = '<? echo $log_ptt;?>';
	document.getElementById('log_soundmasking').value = '<? echo $log_soundmasking;?>';
	
	// clear logs message after 5 seconds
    setTimeout(function(){ 
        document.getElementById("logs_message_container").innerHTML = '';
    }, 5000);
	
	if(document.getElementById('device_id').value!='nq-io'){
        document.getElementById('network_status_reporting').value = '<? echo $network_status_reporting;?>';
        document.getElementById('network_status_ip_addr').value = '<? echo $network_status_ip_addr;?>';
        document.getElementById('network_status_port').value = '<? echo $network_status_port;?>';
        document.getElementById('network_status_intercom').value = '<? echo $network_status_intercom;?>';
        document.getElementById('network_status_paging').value = '<? echo $network_status_paging;?>';
		if(document.getElementById('device_id').value=='nq-asb'){
			document.getElementById('network_status_checkin').value = '<? echo $network_status_checkin;?>';
		}
        
        // clear status message after 5 seconds
        setTimeout(function(){ 
            document.getElementById("status_message_container").innerHTML = '';
        }, 5000);
			
		if(document.getElementById('network_status_reporting').value=='OFF') {
		 	document.getElementById('network_status_ip_addr').readOnly = true;
			document.getElementById('network_status_port').readOnly = true;
			document.getElementById("network_status_intercom").disabled = true;
			document.getElementById("network_status_paging").disabled = true;
			if(document.getElementById('device_id').value=='nq-asb'){
				document.getElementById('network_status_checkin').value = '<? echo $network_status_checkin;?>';
				document.getElementById("network_status_checkin").disabled = true;
			}
		}
	}
}

function unlockFields(){
	document.getElementById("network_status_intercom").disabled = false;
	document.getElementById("network_status_paging").disabled = false;
	if(document.getElementById('device_id').value=='nq-asb'){
		document.getElementById("network_status_checkin").disabled = false;
	}
}
	
function get_log_files(){
	 
	var xmlhttp=new XMLHttpRequest();
    	xmlhttp.onreadystatechange=function() {
        	if (xmlhttp.readyState==4 && xmlhttp.status==200) {
				//console.log(xmlhttp.responseText);
				document.getElementById("log_selection").innerHTML = xmlhttp.responseText;
        	}
		}
	    xmlhttp.open("GET","scripts.php?s=get_log_files",true);
   		xmlhttp.send();
}	
	
function loadLogText(file_name){
	
	// reset scroll to top
	scroll_top();
	
	// start activitiy indicator
  	document.getElementById("activity_indicator").innerHTML = '<strong><i class="fa fa-refresh fa-spin fa-fw margin-bottom"></i>&nbsp;Opening ...</strong>';
	
	// clear error
	document.getElementById("message_container").innerHTML = '';
	
	// set file name
	document.getElementById("filename").value = file_name;
	
	// clear file path
	document.getElementById("file_path").innerHTML = '';

	// hide export button
	document.getElementById('export_container').style.display = 'none';
	 
	var xmlhttp=new XMLHttpRequest();
    	xmlhttp.onreadystatechange=function() {
        	if (xmlhttp.readyState==4 && xmlhttp.status==200) {
				//console.log(xmlhttp.responseText);
				
				if(xmlhttp.responseText=='0'){
					
					window.location.href = "/login.php";
					
				} else if(xmlhttp.responseText==''){
					
					// stop activitiy indicator
  					document.getElementById("activity_indicator").innerHTML = '';
					
					document.getElementById("log_content").innerHTML = '';
					
					document.getElementById("message_container").innerHTML = '<div class="isa_warning"><i class="fa fa-warning"></i><div class="message_content">There was no text returned from the log file.</div></div><br>';
					
					// clear message after 5 seconds
					setTimeout(function(){ 
						document.getElementById("message_container").innerHTML = '';
					}, 5000);
				} else {
					
					// set log_content
					document.getElementById("log_content").innerHTML = xmlhttp.responseText;

					// stop activitiy indicator
					document.getElementById("activity_indicator").innerHTML = '';

					// update file path
					document.getElementById("file_path").innerHTML = '/var/log/' + file_name;

					// show export button
					document.getElementById('export_container').style.display = 'block';
				}
        	}
		}
	    xmlhttp.open("GET","scripts.php?s=get_log_text&f=" + file_name,true);
   		xmlhttp.send();
}

function export_logfile()
{      
    var textToWrite = document.getElementById("file_path").innerHTML + '\n\n' + document.getElementById("log_content").value;

    var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});

    var fileNameToSaveAs = document.getElementById("filename").value + '.txt';

    var downloadLink = document.createElement("a");
	
    downloadLink.download = fileNameToSaveAs;

    downloadLink.innerHTML = "";

    window.URL = window.URL || window.webkitURL;

    downloadLink.href = window.URL.createObjectURL(textFileAsBlob);

    downloadLink.onclick = destroyClickedElement;

    downloadLink.style.display = "none";
	
	if(document.getElementById("filename").value==''){
		// do nothing
	} else {
		document.body.appendChild(downloadLink);
    	downloadLink.click();
	}

}
function destroyClickedElement(event)
{
    document.body.removeChild(event.target);
}
	
$('#log_selection').on('change', function() {
	
	loadLogText(this.value);
})
$(function() {
  	$('#network_status_reporting').change(function() {
		
		var selectedValue = $(this).val();
		if(selectedValue=='OFF'){
		 	document.getElementById('network_status_ip_addr').readOnly = true;
			document.getElementById('network_status_port').readOnly = true;
			document.getElementById("network_status_intercom").disabled = true;
			document.getElementById("network_status_paging").disabled = true;
			if(document.getElementById('device_id').value=='nq-asb'){
				document.getElementById("network_status_checkin").disabled = true;
			}
		} else {
			document.getElementById('network_status_ip_addr').readOnly = false;
			document.getElementById('network_status_port').readOnly = false;
			document.getElementById("network_status_intercom").disabled = false;
			document.getElementById("network_status_paging").disabled = false;
			if(document.getElementById('device_id').value=='nq-asb'){
				document.getElementById("network_status_checkin").disabled = false;
			}
		}
    })
});
// scroll top
function scroll_top(){
	var textarea = document.getElementById('log_content');
	textarea.scrollTop = 0;
}
// scroll bottom 
function scroll_bottom(){
	var textarea = document.getElementById('log_content');
	textarea.scrollTop = textarea.scrollHeight;
}
//  tooltip
$(function () {
  $('[data-toggle="tooltip"]').tooltip()
})
</script>