<%#
Opennet Firmware

Copyright 2010 Rene Ejury <opennet@absorb.it>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

-%>

<% local uci = require "luci.model.uci"
   local cursor = uci.cursor()
   require ("luci.model.opennet.on_usergw")
%>
<script type="text/javascript">//<![CDATA[

function is_running(running) {
  if (running) {
    document.getElementById("update_wan").disabled = "true"
    document.getElementById("update_speed").disabled = "true"
    document.getElementById("update_vpn").disabled = "true"
    document.getElementById("update_mtu").disabled = "true"
    document.getElementById("add_name").disabled = "true"
    try { // only available if ugw is possible
		document.getElementById("enable").disabled = "true"
		document.getElementById("suspend").disabled = "true"
		document.getElementById("running_1").hidden = ""
    } catch(e) {}
    document.getElementById("running_2").hidden = ""
    elems =  document.getElementsByName("select_gw");
    for ( var i=0; i<elems.length; i++)
      elems[i].disabled = "true"
  }
  else if (document.getElementById("running_status").status == "off") {
    document.getElementById("update_wan").disabled = ""
    document.getElementById("update_speed").disabled = ""
    document.getElementById("update_vpn").disabled = ""
    document.getElementById("update_mtu").disabled = ""
    document.getElementById("add_name").disabled = ""
    try { // only available if ugw is possible
		document.getElementById("enable").disabled = ""
		document.getElementById("suspend").disabled = ""
		document.getElementById("running_1").hidden = "true"
    } catch(e) {}
    document.getElementById("running_2").hidden = "true"
    elems =  document.getElementsByName("select_gw");
    button_elem = document.getElementById("hidden_name_button");
    for ( var i = 1; i < elems.length; i++) {
      var name_button_xhr =  new XHR();
      name_button_xhr.get('<%=luci.dispatcher.build_url("opennet", "opennet_2")%>/ugw_tunnel/get_name_button/' + i, null,
        function(x) {
          if (x.responseText) {
            button_elem.innerHTML = x.responseText;
            document.getElementById("name_" + button_elem.getElementsByTagName("h3")[0].getAttribute("count")).innerHTML = x.responseText;
            document.getElementById("ugw_tunnel_active_" + button_elem.getElementsByTagName("h3")[0].getAttribute("count"))
              .setAttribute("status", button_elem.getElementsByTagName("h3")[0].getAttribute("tunnel"))
            document.getElementById("ugw_forward_active_" + button_elem.getElementsByTagName("h3")[0].getAttribute("count"))
              .setAttribute("status", button_elem.getElementsByTagName("h3")[0].getAttribute("forward"))
          }
        }
      );
    }
  }
}

function check_running() {
  var run_xhr = new XHR();
  run_xhr.get('<%=luci.dispatcher.build_url("opennet", "opennet_2")%>/ugw_tunnel/check_running', null,
    function(x) {
      if (x.responseText && x.responseText != "")
        document.getElementById("running_status").status="on"
      else {
        if (document.getElementById("running_status").status == "on") {
          get_wan_values();
          get_speed_values();
          get_mtu_values();
          get_vpn_values();
        }
        document.getElementById("running_status").status="off"
      }
      is_running(x.responseText && x.responseText != "")
    }
  );
}

// this can be used to walk trough all WAN elems and update them step-by-step
// additionally the ping-time will be updated
// params
// elems:   array of all wan section objects
// number:  the current row number, starting from 0
// section: will be ignored
// call: 'check' or 'get', if check values will be updated
function get_wan_value(elems, number, section, call) {
//   alert(number + " " + elems.length)
  if (number >= elems.length) {
    is_running(false);
    return false;
  }

  var wan_section = elems[number++]
  
  var wan_xhr = new XHR();
  wan_xhr.get('<%=luci.dispatcher.build_url("opennet", "opennet_2")%>/ugw_tunnel/' + call + '_wan/' + number, null,
    function(x) {
      if (x.responseText)
        wan_section.innerHTML = x.responseText;

      var name_button_xhr =  new XHR();
      name_button_xhr.get('<%=luci.dispatcher.build_url("opennet", "opennet_2")%>/ugw_tunnel/get_name_button/' + number, null,
        function(x) {
          if (x.responseText)
            document.getElementById("name_" + number).innerHTML = x.responseText;

          var wan_ping_xhr =  new XHR();
          wan_ping_xhr.get('<%=luci.dispatcher.build_url("opennet", "opennet_2")%>/ugw_tunnel/get_wan_ping/' + number, null,
            function(x) {
              if (x.responseText)
                document.getElementById("wan_ping_" + number).innerHTML = x.responseText;
              get_wan_value(elems, number, section, call);
            }
          );
        }
      );
    }
  );
}

// this can be used to walk trough all WAN elems and update them step-by-step
// params
// elems:   array of all wan section objects
// number:  the current row number, starting from 0
// section: the section, for instance speed
// call: 'check' or 'get', if check values will be updated
function get_value(elems, number, section, call) {
  if (number >= elems.length) {
    is_running(false);
    return false;
  }

  var wan_section = elems[number++]
  
  var wan_xhr = new XHR();
  wan_xhr.get('<%=luci.dispatcher.build_url("opennet", "opennet_2")%>/ugw_tunnel/' + call + '_' + section + '/' + number, null,
    function(x) {
      if (x.responseText)
        wan_section.innerHTML = x.responseText;
      get_value(elems, number, section, call);
    }
  );
}

function update_values(section, call_function) {
  // hide all current values and show spinners
  var elems =  document.getElementsByName(section);
  for ( var i=0; i<elems.length; i++)
    elems[i].style.display = 'none'
  elems =  document.getElementsByName(section + "_spinner");
  for ( var i=0; i<elems.length; i++)
    elems[i].style.display = ''

  is_running(true);

  call_function(document.getElementsByName(section + "_section"), 0, section, "update")
  return false;
}

function update_wan_values() {
  if (confirm("<%:Check route trough WAN and ping-time of Gateways (again). This will take approximately 5 seconds per Gateway.%>"))
    update_values("wan", get_wan_value)

  return false;
}

function get_wan_values() {
//   alert("get_wan_values")
  get_wan_value(document.getElementsByName("wan_section"), 0, "wan", "get")
}

function update_speed_values() {
  if (confirm("<%:Check speed to and from the Gateways (again). This will take approximately 1 minute per Gateway.%>"))
    update_values("speed", get_value)

  return false;
}

function get_speed_values() {
  get_value(document.getElementsByName("speed_section"), 0, "speed", "get")
}

function update_mtu_values() {
  if (confirm("<%:Check MTU to and from the Gateways (again). This will take approximately 5 minutes per Gateway.%>"))
    update_values("mtu", get_value)

  return false;
}

function get_mtu_values() {
  get_value(document.getElementsByName("mtu_section"), 0, "mtu", "get")
}

function update_vpn_values() {
  if (confirm("<%:Check VPN availability of Gateways (again). This will take approximately 10 seconds per Gateway.%>"))
    update_values("vpn", get_value)

  return false;
}

function get_vpn_values() {
  get_value(document.getElementsByName("vpn_section"), 0, "vpn", "get")
}
//]]></script>

<script type="text/javascript">//<![CDATA[

if (window.attachEvent) {window.attachEvent('onload', init);}
else if (window.addEventListener) {window.addEventListener('load', init, false);}
else {document.addEventListener('load', init, false);} 
// 
//   window .onload = function test() {init();}
function init() {
  document.getElementById("running_status").status="on"
  check_running();
  setInterval('check_running()', 10000 )
          get_wan_values();
          get_speed_values();
          get_mtu_values();
          get_vpn_values();
}
//]]></script>

<!-- hidden label to store running status and name button -->
<div id="running_status" hidden="true" status="on"/>
<div id="hidden_name_button" hidden="true"><h3 count="1" /></div>

<h3><%:Usergateway-Status%></h3>
  <div class="cbi-map">
    <fieldset class="cbi-section">
      <fieldset class="cbi-section-node">
        <h4 id="running_2" hidden="true"><div class="errorbox"><%:automated check is running, manual modifications are temporarily disabled%></div><br /><br /></h4>
        <form id="main_form" method="post" action="<%=REQUEST_URI%>">
          <table class="cbi-section-table">
            <tr class="cbi-section-table-titles">
              <th class="cbi-section-table-cell right-border" colspan = "6" style="width: 30%"><%:Internet Sharing Status Overview%></th>
              <th class="cbi-section-table-cell" colspan = "5" ><%:Status Details, all checks have to be successful%></th>
            </tr>
            <tr class="cbi-section-table-titles">
              <th colspan = "3"  style="width: 2%;"/>
              <th class="cbi-section-table-cell"><% print(luci.i18n.string([[<abbr title='click to forward to this gateway immediately'>Gateway-Name</abbr>]])) %></th>
              <th class="cbi-section-table-cell"><% print(luci.i18n.string([[<abbr title='If Tunnel is activated, a central Gateway-IP is mapped to the local Router. Multiple Tunnels can exist parallel.'>UGW-Tunnel</abbr>]])) %></th>
              <th class="cbi-section-table-cell right-border"><% print(luci.i18n.string([[<abbr title='If Forwarding is activated, all received VPN-Traffic is routed to the outside Gateway. Only one forward can exist at any time.'>VPN-Forwarding</abbr>]])) %></th>
              <th class="cbi-section-table-cell"><% print(luci.i18n.string([[<abbr title='Indicates if route to Gateway points trough WAN-device, this is required. If not check/reconfigure your WAN connection, maybe it is not working.'>WAN</abbr>]])) %></th>
              <th class="cbi-section-table-cell"><% print(luci.i18n.string([[<abbr title='Indicates the Ping-Time in miliseconds. If Gateway cannot be pinged or the ping-time is high, it is probably (temporarily) down.'>Ping-Time</abbr>]])) %></th>
              <th class="cbi-section-table-cell"><% print(luci.i18n.string([[<abbr title='Indicates Upload / Download Speed.'>Speed</abbr>]])) %></th>
              <th class="cbi-section-table-cell"><% print(luci.i18n.string([[<abbr title='Indicates if the Gateway supports VPN for the users. This is at least required to sucessfully use VPN-Forwarding.'>VPN-Status</abbr>]])) %></th>
              <th class="cbi-section-table-cell"><% print(luci.i18n.string([[<abbr title='Maximum Transmission Unit - match of configured and measured values is required to share your Internet in any case'>MTU-Status</abbr>]])) %></th>
              <th />
            </tr>
<%          local count = 1
            while count <= ugw_status.usergateways_no do
              local name = cursor:get("on-usergw", "opennet_ugw"..count, "name")
              if not name then name = "" end
%>
                <tr id="cbi-network-lan" class="cbi-section-table-row">
<!--                        begin of UP-DOWN-Buttons -->
                  <td class="cbi-value-field">
                    <% if count ~= 1 then %>
                      <input class="cbi-button cbi-button-up" type="submit" value="<%=count-1%>" title="<%:up%>" name="down_section" />
                    <% end %>
                  </td>
                  <td class="cbi-value-field">
                    <% if count ~= ugw_status.usergateways_no then %>
                      <input class="cbi-button cbi-button-down" type="submit" value="<%=count%>" title="<%:down%>" name="down_section" />
                    <% end %>
                  </td>
                  <td class="cbi-value-field">
                    <input class="cbi-button cbi-button-del" type="submit" value="<%=count%>" title="<%:delete%>" name="del_section" />
                  </td>
<!--                        end of UP-DOWN-Buttons -->
<!--                        begin of Gateway-Name (and IP) -->
                  <td id="name_<%=count%>">
                    <h3><input class="cbi-button" type="submit" title="<%:[[No IP-Address found for Gateway-Name, Gateway cannot be used]]%>" name="select_gw" value="<%=name%>" disabled="true" /></h3>
                  </td>
<!--                        end of Gateway-Name (and IP) -->
<!--                        begin of Gateway Status -->
                  <td class="cbi-value-field">
                    <div id="ugw_tunnel_active_<%=count%>" class="ugw-tunnel_active" status="irrelevant">&nbsp;</div>
                  </td>
                  <td class="cbi-value-field right-border">
                    <div id="ugw_forward_active_<%=count%>" class="ugw-tunnel_active" status="irrelevant">&nbsp;</div>
                  </td>
<!--                        end of Gateway Status -->
<!--                        begin of other status section -->
                  <td name="wan_section" class="cbi-value-field">
                    <img class='loading_img_small' src='<%=resource%>/icons/loading.gif' alt='<%:Loading%>' />
                  </td>
                  <td id="wan_ping_<%=count%>" class="cbi-value-field">
                    <img class='loading_img_small' src='<%=resource%>/icons/loading.gif' alt='<%:Loading%>' />
                  </td>
                  <td name="speed_section" class="cbi-value-field">
                    <img class='loading_img_small' src='<%=resource%>/icons/loading.gif' alt='<%:Loading%>' />
                  </td>
                  <td name="vpn_section" class="cbi-value-field">
                    <img class='loading_img_small' src='<%=resource%>/icons/loading.gif' alt='<%:Loading%>' />
                  </td>
                  <td name="mtu_section" class="cbi-value-field">
                    <img class='loading_img_small' src='<%=resource%>/icons/loading.gif' alt='<%:Loading%>' />
                  </td>
<!--                        end of other status section -->
                </tr>
<%            count = count + 1
            end %>
            <tr id="cbi-network-lan" class="cbi-section-table-row">
              <td colspan="3" />
              <th><input type="value" name="new_gateway_name" /></th>
              <td colspan="1" />
              <td class="cbi-value-field cbi-value-field-add right-border"><input id="add_name" class="cbi-button cbi-button-add" type="submit" value="<%:add%>"/></td>
              <td colspan="2"><input id="update_wan" class="cbi-button" type="submit" title="<%:This will take approximately 5 seconds per Gateway%>" value="<%:check WAN-Connection%>" onclick="return update_wan_values();"/></td>
              <td><input id="update_speed" class="cbi-button" type="submit" title="<%:This will take approximately 1 minute per Gateway%>" value="<%:check Speed%>" onclick="return update_speed_values();"/></td>
              <td><input id="update_vpn" class="cbi-button" type="submit" title="<%:This will take approximately 10 seconds per Gateway%>" value="<%:check VPN%>" onclick="return update_vpn_values();"/></td>
              <td><input id="update_mtu" class="cbi-button" type="submit" title="<%:This will take approximately 5 minutes per Gateway%>" value="<%:check MTU%>" onclick="return update_mtu_values();"/></td>
            </tr>
          </table>
        </form>
      </fieldset>
    </fieldset>
  </div>
