<%!

struct icm_service_info_type
{
  string protocol;
  string service;
  string hostname;
  boolean bind_to_host;
  int keep_alive;
  int proc_timeout;
  boolean active;
  boolean ext_bind;
  int     verify_client;
  int     virt_host_idx;
  string  aclfile;
};

%>

<%
  int serv_count, servno;
  int nr, rc, row;
  string what, browser, fld, is_checked, op, opstr;
  string url, prot, serv, host, lastControlID;
  int verify_client;
  boolean is_admin;
  struct icm_service_info_type icm_serv_info[];
  string token;
  string msg;
  string icon;

  is_admin = __icm_get_system_property ("is_admin");
  browser = __icm_get_browser_type();
  what = anumenc(__icm_get_form_field ("what"));
  lastControlID = __icm_get_form_field ("lastControlID");
  fld = anumenc(__icm_get_form_field ("row_selected"));
  if (fld == "")
    row = 0;
  else
    row = fld;
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <link rel=stylesheet type="text/css" href="../public/css/controls_<%=browser%>.css">
    <link rel=stylesheet type="text/css" href="../public/css/sapbsp.css">
    <title> Active Services </title>
    <script language="JavaScript" type="text/javascript">
      <!--
      function restoreFocus(lastControlID) {
        if (lastControlID != '') {
          var lastUsedControl=document.getElementById(lastControlID);
          if (lastUsedControl != null) {
            lastUsedControl.focus();
          }
        }
      }
      function callSubmitActionKey(ev,keycode,what,token,lastControlID) {
	if (ev.keyCode == keycode) {
	  callSubmitAction(what,lastControlID,token);
	}
      }
      function callSubmitAction(what, lastControlID, token)
      {
        document.servForm.what.value = what;
        document.servForm.token.value = token;
        document.servForm.lastControlID.value = lastControlID;
        document.servForm.submit();
      }
      -->
    </script>
  </head>

  <body class="sapBdy">
<%
   if (what == "add_serv") {
     struct icm_service_info_type icp_serv;
     icp_serv.protocol = anumenc(__icm_get_form_field ("prot"));
     icp_serv.service  = anumenc(__icm_get_form_field ("serv"));
     icp_serv.hostname = fqhnenc(__icm_get_form_field ("host"));
     icp_serv.keep_alive = __icm_get_form_field ("keep_alive");
     icp_serv.proc_timeout  = __icm_get_form_field ("proc_timeout");
     icp_serv.verify_client = 1;
     icp_serv.virt_host_idx = -1;
     token = __icm_get_form_field ("token");

     if (__icm_get_form_field ("active") == "X")
        icp_serv.active = TRUE;
     if (__icm_get_form_field ("bind_to_host") == "X")
        icp_serv.bind_to_host = TRUE;
     if (__icm_get_form_field ("ext_bind") == "X")
     	icp_serv.ext_bind = TRUE;

     icp_serv.aclfile = __icm_get_form_field ("aclfile");

     servno = icp_serv.service;
     if ((icp_serv.protocol == "") || (icp_serv.service == ""))
     {
	msg = "Not all required data specified";
	icon = "s_m_warn.gif";
     }
     else if ((servno < 0) || (servno > 65536))
     {
	msg = "Illegal value specified";
	icon = "s_m_warn.gif";
     }
     else
     {
	rc = __icm_execute (4, icp_serv, token);
	if (rc == 0)
	{
	  msg = "Service successfully created";
	  icon = "s_m_info.gif";
	}
	else if (rc == -11) 
	{
	  msg = "Session timeout. Please retry operation.";
	  icon = "s_m_warn.gif";
	}
	else
	{
	  msg = "Service could not be created: " +  __icm_get_error_text(rc) + "(" + rc + ")";
	  icon = "s_m_erro.gif";
    	}
     }
%>

      <form action="" name="servForm" method="POST">
                <input type="hidden" name="what" value="">
                <input type="hidden" name="token" value="">
                <input type="hidden" name="lastControlID" value="<%=lastControlID%>">
      </form>
      <table border="0" cellspacing="0" cellpadding="0" class="sapTbvAlt" width="50%">
	<tr>
	  <td class="sapTbvCellAlt" Colspan="1" align="center">
	    <img src="../public/icons/<%=icon%>" alt="">
	  </td>
	  <td class="sapTbvCellAlt" Colspan="2" align="center">
	    <b><i>Information</i></b>
	  </td>
	  <td class="sapTbvCellAlt" Colspan="1" align="center">
	    <img src="../public/icons/<%=icon%>" alt="">
	  </td>
	</tr>
	<tr>
	  <td class="sapTbvCellStd" Colspan="4">&nbsp;</td>
	</tr>
	<tr>
	  <td class="sapTbvCellStd" Colspan="4">&nbsp;</td>
	</tr>
	<tr>
	  <td class="sapTbvCellStd" Colspan="4" align="center">
	    <b><i><%=msg%></i></b>
	  </td>
	</tr>
	<tr><td class="sapTbvCellStd" Colspan="4">&nbsp;</td></tr>
	<tr><td class="sapTbvCellStd" Colspan="4">&nbsp;</td></tr>

	<tr>
	  <td class="sapTbvCellStd" Colspan="4" align="center">
	  
	    <a href="javascript:callSubmitAction('','<%=lastControlID%>','')" class="sapBtnStd"
	       accesskey="b" 
	    	onKeyUp="javascript:callSubmitActionKey(event,66,'','','<%=lastControlID%>')">
	    <nobr><u>B</u>ack</nobr></a>  
	    
	  </td>
	</tr>
      </table><p>
<%
   }
   else if ((what == "del_serv") || (what == "toggle_serv")) {
     serv_count = __icm_get_serv_info(icm_serv_info);
     if ((row < 0) || (row > serv_count))
	row = 0;
     serv = icm_serv_info[row].service;
     prot = icm_serv_info[row].protocol;
     host = icm_serv_info[row].hostname;
     verify_client = icm_serv_info[row].verify_client;
     if (what == "del_serv") {
	op = "do_del_serv";
	opstr = "delete";
	token = __icm_get_token(5);
     } else {
	if (icm_serv_info[row].active) {
	  op = "do_deact_serv";
	  opstr = "deactivate";
	  token = __icm_get_token(8);
	} else {
	  op = "do_act_serv";
	  opstr = "activate";
	  token = __icm_get_token(7);
        }
     }
%>

   <form action="" name="servForm" method="POST">
    <input type="hidden" name="what" value="">
    <input type="hidden" name="prot_old" value="<%=prot%>">
    <input type="hidden" name="serv_old" value="<%=serv%>">
    <input type="hidden" name="host_old" value="<%=host%>">
    <input type="hidden" name="verify_client_old" value="<%=verify_client%>">
    <input type="hidden" name="token" value="">
    <input type="hidden" name="lastControlID" value="<%=lastControlID%>">
      <table border="0" cellspacing="0" cellpadding="0" class="sapTbvAlt" width="50%">
	<tr>
	  <td class="sapTbvCellAlt" Colspan="1" align="center">
	    <img src="../public/icons/s_m_crit.gif" alt="">
	  </td>
	  <td class="sapTbvCellAlt" Colspan="2" align="center">
	    <b><i>Confirm operation</i></b>
	  </td>
	  <td class="sapTbvCellAlt" Colspan="1" align="center">
	    <img src="../public/icons/s_m_crit.gif" alt="">
	  </td>
	</tr>
	<tr>
	  <td class="sapTbvCellStd" Colspan="4">&nbsp;</td>
	</tr>
	<tr>
	  <td class="sapTbvCellStd" Colspan="4">&nbsp;</td>
	</tr>
	<tr>
	  <td class="sapTbvCellStd" Colspan="4" align="center">
	    <b><i>Do you really want to <%=opstr%> the service <%=serv%> (<%=prot%>)?</i></b>
	  </td>

	</tr>
	<tr>
	  <td class="sapTbvCellStd" Colspan="4">&nbsp;</td>
	</tr>
	<tr>
	  <td class="sapTbvCellStd" Colspan="4">&nbsp;</td>
	</tr>

	<tr>
	  <td class="sapTbvCellStd">&nbsp;</td>
	  <td class="sapTbvCellStd" align="center">
	    <a accesskey="y" 
	       onKeyUp="javascript:callSubmitActionKey(event,89,'<%=op%>', '<%=token%>', '<%=lastControlID%>')"
	       href="javascript:callSubmitAction('<%=op%>', '<%=lastControlID%>', '<%=token%>')" class="sapBtnStd">
		<nobr><u>Y</u>es</nobr></a>
	  </td>
	  <td class="sapTbvCellStd" align="center">
	    <a href="javascript:callSubmitAction('','<%=lastControlID%>','')" class="sapBtnEmph"
	       accesskey="n" 
	       onKeyUp="javascript:callSubmitActionKey(event,78,'','','<%=lastControlID%>')">
	      <nobr><u>N</u>o</nobr></a>
	  </td>
	  <td class="sapTbvCellStd">&nbsp;</td>
	</tr>
      </table><p>
   </form>

<%
   }
   else if ((what == "do_change_serv") || (what == "do_del_serv") ||
	    (what == "do_act_serv") || (what == "do_deact_serv")) {
     struct icm_service_info_type icp_serv_old;
     struct icm_service_info_type icp_serv_new;

     prot = anumenc(__icm_get_form_field ("prot_old"));
     serv = anumenc(__icm_get_form_field ("serv_old"));
     host = fqhnenc(__icm_get_form_field ("host_old"));
     verify_client = anumenc(__icm_get_form_field ("verify_client_old"));
     token = __icm_get_form_field ("token");

     icp_serv_old.protocol = prot;
     icp_serv_old.service  = serv;
     icp_serv_old.hostname = host;
     icp_serv_old.verify_client = verify_client;

     if (what == "do_change_serv") {
	opstr = "changed";
	icp_serv_new.protocol = anumenc(__icm_get_form_field ("prot"));
	icp_serv_new.service  = anumenc(__icm_get_form_field ("serv"));
	icp_serv_new.hostname = fqhnenc(__icm_get_form_field ("host"));
	icp_serv_new.keep_alive = __icm_get_form_field ("keep_alive");
	icp_serv_new.proc_timeout  = __icm_get_form_field ("proc_timeout");
	icp_serv_new.verify_client = icp_serv_old.verify_client;
	icp_serv_new.virt_host_idx = -1;
	icp_serv_new.aclfile  = __icm_get_form_field ("aclfile");

	if (__icm_get_form_field ("active") == "X")
	  icp_serv_new.active = TRUE;
	if (__icm_get_form_field ("bind_to_host") == "X")
	  icp_serv_new.bind_to_host = TRUE;
	if (__icm_get_form_field ("ext_bind") == "X")
	  icp_serv_new.ext_bind = TRUE;

	servno = icp_serv_new.service;
        if ((icp_serv_new.protocol == "") || (icp_serv_new.service == ""))
	  rc = -100;
        else if ((servno < 0) || (servno > 65536))
	  rc = -101;
        else
	  rc = __icm_execute (6, icp_serv_old, icp_serv_new, token);
     } else if (what == "do_del_serv") {
	opstr = "deleted";
	rc = __icm_execute (5, icp_serv_old, token);
     } else if (what == "do_act_serv") {
	opstr = "activated";
	rc = __icm_execute (7, icp_serv_old, token);
     } else {
	opstr = "deactivated";
	rc = __icm_execute (8, icp_serv_old, token);
     }

    if (rc == 0)
    {
	msg = "Service successfully " + opstr;
	icon = "s_m_info.gif";
    }
    else if (rc == -100)
    {
	msg = "Not all required data specified";
	icon = "s_m_warn.gif";
    }
    else if (rc == -101)
    {
	msg = "Illegal value specified";
	icon = "s_m_warn.gif";
    }
    else if (rc == -11) 
    {
	msg = "Session timeout. Please retry operation.";
	icon = "s_m_warn.gif";
    }
    else
    {
	msg = "Service could not be " + opstr + ": " +  __icm_get_error_text(rc) + "(" + rc + ")";
	icon = "s_m_erro.gif";
    }
%>

      <form action="" name="servForm" method="POST">
          <input type="hidden" name="what" value="">
          <input type="hidden" name="token" value="">
          <input type="hidden" name="lastControlID" value="<%=lastControlID%>">
      </form>
    
      <table border="0" cellspacing="0" cellpadding="0" class="sapTbvAlt" width="50%">
	<tr>
	  <td class="sapTbvCellAlt" Colspan="1" align="center">
	    <img src="../public/icons/<%=icon%>" alt="">
	  </td>
	  <td class="sapTbvCellAlt" Colspan="2" align="center">
	    <b><i>Information</i></b>
	  </td>
	  <td class="sapTbvCellAlt" Colspan="1" align="center">
	    <img src="../public/icons/<%=icon%>" alt="">
	  </td>
	</tr>
	<tr>
	  <td class="sapTbvCellStd" Colspan="4">&nbsp;</td>
	</tr>
	<tr>
	  <td class="sapTbvCellStd" Colspan="4">&nbsp;</td>
	</tr>
	<tr>
	  <td class="sapTbvCellStd" Colspan="4" align="center">
	    <b><i><%=msg%></i></b>
	  </td>
	</tr>
	<tr><td class="sapTbvCellStd" Colspan="4">&nbsp;</td></tr>
	<tr><td class="sapTbvCellStd" Colspan="4">&nbsp;</td></tr>

	<tr>
	  <td class="sapTbvCellStd" Colspan="4" align="center">
	  
	    <a href="javascript:callSubmitAction('','<%=lastControlID%>','')" class="sapBtnStd"
	       accesskey="b" 
	       onKeyUp="javascript:callSubmitActionKey(event,66,'','','<%=lastControlID%>')">
	       <nobr><u>B</u>ack</nobr></a>  
	     
	  </td>
	</tr>
      </table><p>
<%
   }
   else
   {
     serv_count = __icm_get_serv_info(icm_serv_info);
     if ((row < 0) || (row > serv_count))
	row = 0;
%>

   <form action="" name="servForm" method="POST">
    <input type="hidden" name="what" value="">
    <input type="hidden" name="prot_old" value="<%=icm_serv_info[row].protocol%>">
    <input type="hidden" name="serv_old" value="<%=icm_serv_info[row].service%>">
    <input type="hidden" name="host_old" value="<%=icm_serv_info[row].hostname%>">
    <input type="hidden" name="verify_client_old" value="<%=icm_serv_info[row].verify_client%>">
    <input type="hidden" name="token" value="<%=token%>">
    <input type="hidden" name="lastControlID" value="<%=lastControlID%>">
    <table border=0 cellspacing=1 cellpadding=0 class="sapTbvAlt">
      <tr>
        <td class="sapTbvCellAlt" Colspan=9>
          <b><i>Active Services</i></b>
        </td>
        <td class="sapTbvCellAlt" align="right">
          <a href="./show_serv.icp" class="sapBtnEmph" title="refresh page"><nobr>Refresh</nobr></a>
        </td>
      </tr>
      <tr>
        <th class="sapTbvCellAlt">
          <i>&nbsp;</i>
        </th>
        <th class="sapTbvCellAlt" width=10>
          <i>Active</i>
        </th>
        <th class="sapTbvCellAlt" width=100>
          <i>Protocol</i>
        </th>
        <th class="sapTbvCellAlt" width=100>
          <i>Services / Port</i>
        </th>
        <th class="sapTbvCellAlt" width=200>
          <i>Hostname / Port</i>
        </th>
        <th class="sapTbvCellAlt" width=80>
          <i>Keep Alive</i>
        </th>
        <th class="sapTbvCellAlt" width=80>
          <i>Proc Timeout</i>
        </th>
        <th class="sapTbvCellAlt" width=80>
          <i>Bind to host</i>
        </th>
        <th class="sapTbvCellAlt" width=80>
          <i>Ext Bind</i>
        </th>
        <th class="sapTbvCellAlt" width=100>
          <i>ACL File</i>
        </th>
      </tr>
      <%
        for (nr = 0; nr < serv_count; nr++)
	{
	  if ((what == "change_serv") && (nr == row)) {
      %>

      <tr>
	<td class="sapTbvCellStd" nowrap align=left>
          <input title="<%=icm_serv_info[nr].protocol%>" type="radio" name="row_selected" value="<%= nr %>" checked>
	</td>
<% if (icm_serv_info[nr].active)
	is_checked = "checked";
   else is_checked = "";
%>
	<td class="sapTbvCellStd" nowrap align=left>
          <input title="Active" type="checkbox" name="active" value="X" <%=is_checked%> >
        </td>
        <td class="sapTbvCellStd">
	  <input title="Protocol" type="text" class="sapEdfTxtDsbl" name="prot"
            value="<%=icm_serv_info[nr].protocol%>" size="15" maxlength="15" readonly>
        </td>
        <td class="sapTbvCellStd">
	  <input title="Services/Port" type="text" class="sapEdfTxtEnbl" name="serv"
            value="<%=icm_serv_info[nr].service%>" size="10" maxlength="15">
        </td>
        <td class="sapTbvCellStd">
	  <input title="Hostname/Port" type="text" class="sapEdfTxtEnbl" name="host"
            value="<%=icm_serv_info[nr].hostname%>" size="32" maxlength="32">
        </td>
        <td class="sapTbvCellStd">
	  <input title="Keep Alive" type="text" class="sapEdfTxtEnbl" name="keep_alive"
            value="<%=icm_serv_info[nr].keep_alive%>" size="5" maxlength="10">
        </td>
        <td class="sapTbvCellStd">
	  <input title="Proc Timeout" type="text" class="sapEdfTxtEnbl" name="proc_timeout"
            value="<%=icm_serv_info[nr].proc_timeout%>" size="5" maxlength="10">
        </td>
<% if (icm_serv_info[nr].bind_to_host)
	is_checked = "checked";
   else is_checked = "";
%>
	<td class="sapTbvCellStd" nowrap align=left>
          <input title="Bind to host" type="checkbox" name="bind_to_host" value="X" <%=is_checked%> >
        </td>

<% if (icm_serv_info[nr].ext_bind)
	is_checked = "checked";
   else is_checked = "";
%>
	<td class="sapTbvCellStd" nowrap align=left>
          <input title="Ext Bind" type="checkbox" name="ext_bind" value="X" <%=is_checked%> >
        </td>
        <td class="sapTbvCellStd">
	  <input title="ACL file" type="text" class="sapEdfTxtEnbl" name="aclfile"
            value="<%=icm_serv_info[nr].aclfile%>" size="32" maxlength="255">
        </td>
      </tr>

      <%
	  } else {
	    if (row == nr)
		is_checked = "checked";
	    else is_checked = "";
      %>
      <tr>
	<td class="sapTbvCellStd" nowrap align=left>
          <input title="<%=icm_serv_info[nr].protocol%>" type="radio" name="row_selected" value="<%= nr %>" <%=is_checked%>>
	</td>
<% if (icm_serv_info[nr].active) { %>
        <td class="sapTbvCellStd" align="center"><img title="Activated" src="../public/icons/s_s_okay.gif" alt="Activated"></td>
<% } else { %>
        <td class="sapTbvCellStd" align="center"><span title="Deactivated">-</span></td>    
<% } %>
        <td class="sapTbvCellStd"><%= icm_serv_info[nr].protocol %></td>
        <td class="sapTbvCellStd"><%= icm_serv_info[nr].service %></td>
        <td class="sapTbvCellStd"><%= icm_serv_info[nr].hostname %></td>
        <td class="sapTbvCellStd"><%= icm_serv_info[nr].keep_alive %></td>
        <td class="sapTbvCellStd"><%= icm_serv_info[nr].proc_timeout %></td>
<% if (icm_serv_info[nr].bind_to_host) { %>
        <td class="sapTbvCellStd" align="center"><img title="Activated" src="../public/icons/s_s_okay.gif" alt="Activated"></td>
<% } else { %>
        <td class="sapTbvCellStd" align="center"><span title="Deactivated">-</span></td>
<% } %>
<% if (icm_serv_info[nr].ext_bind) { %>
        <td class="sapTbvCellStd" align="center"><img title="Activated" src="../public/icons/s_s_okay.gif" alt=""></td>
<% } else { %>
        <td class="sapTbvCellStd" align="center"><span title="Deactivated">-</span></td>
<% } %>
        <td class="sapTbvCellStd"><%= icm_serv_info[nr].aclfile %></td>
      </tr>
      <%
	}
      }
      %>
<%
   if (what == "show_add_serv") {
%>
      <tr>
	<td class="sapTbvCellStd" nowrap align=left>
          <input title="Add Service" type="radio" name="row_selected" value="9999" checked >
	</td>
	<td class="sapTbvCellStd" nowrap align=left>
          <input title="Active" type="checkbox" name="active" value="X" checked >
        </td>
        <td class="sapTbvCellStd">
	    <select title="Protocol" name="prot" class="sapDdlWhl">
	      <option value="">Protocols</option>
	      <option value="HTTP" selected>HTTP</option>
	      <option value="HTTPS">HTTPS</option>
	      <option value="SMTP">SMTP</option>
	      <option value="ROUTER">ROUTER</option>
	      <option value="P4">P4</option>
	      <option value="P4SEC">P4SEC</option>
	      <option value="IIOP">IIOP</option>
	      <option value="IIOPSEC">IIOPSEC</option>
	      <option value="TELNET">TELNET</option>
	    </select>
        </td>
        <td class="sapTbvCellStd">
	  <input title="Services/Port" type="text" class="sapEdfTxtEnbl" name="serv"
            value="" size="15" maxlength="15">
        </td>
        <td class="sapTbvCellStd">
	  <input title="Hostname/Port" type="text" class="sapEdfTxtEnbl" name="host"
            value="" size="32" maxlength="40">
        </td>
        <td class="sapTbvCellStd">
	  <input title="Keep Alive" type="text" class="sapEdfTxtEnbl" name="keep_alive"
            value="60" size="15" maxlength="10">
        </td>
        <td class="sapTbvCellStd">
	  <input title="Proc Timeout" type="text" class="sapEdfTxtEnbl" name="proc_timeout"
            value="60" size="15" maxlength="10">
        </td>
	<td class="sapTbvCellStd" nowrap align=left>
          <input title="Bind to host" type="checkbox" name="bind_to_host" value="X" >
        </td>
	<td class="sapTbvCellStd" nowrap align=left>
          <input title="Ext Bind" type="checkbox" name="ext_bind" value="X" >
        </td>
        <td class="sapTbvCellStd">
	  <input title="ACL file" type="text" class="sapEdfTxtEnbl" name="aclfile"
            value="" size="32" maxlength="255">
        </td>
      </tr>
<%
   }
%>
    </table>
<%
   if (what == "show_add_serv") {
	token = __icm_get_token(4);
%>
    <p>
    <a id="AddNewServiceButton" href="javascript:callSubmitAction('add_serv', 'ChangeServiceButton', '<%=token%>')" class="sapBtnStd">
	<nobr>Add New Service</nobr></a>
    <a id="CancelButton" href="javascript:callSubmitAction('', 'ChangeServiceButton', 'ChangeServiceButton')" class="sapBtnEmph">
	  <nobr>Cancel</nobr></a>
<%
   } else if (what == "change_serv") {
	token = __icm_get_token(6);
%>

    <p>
    
    <a id="ChangeServiceButton" href="javascript:callSubmitAction('do_change_serv', 'ChangeServiceButton', '<%=token%>')" class="sapBtnStd">
	<nobr>Change Service</nobr></a>
    <a id="CancelButton" href="javascript:callSubmitAction('', 'ChangeServiceButton', '')" class="sapBtnEmph">
	  <nobr>Cancel</nobr></a>
<%
   } else if (is_admin) {
%>
    <p>
    <a id="ChangeServiceButton" href="javascript:callSubmitAction('change_serv', 'ChangeServiceButton', '')" class="sapBtnStd">
	<nobr>Change Service</nobr></a>
    <a id="AddServiceButton" href="javascript:callSubmitAction('show_add_serv', 'AddNewServiceButton', '')" class="sapBtnStd">
	<nobr>Add Service</nobr></a>
    <a id="ActivateDeactivateButton" href="javascript:callSubmitAction('toggle_serv', 'ActivateDeactivateButton', '')" class="sapBtnStd">
	<nobr>Activate/Deactivate Service</nobr></a>
    <a id="DeleteServiceButton" href="javascript:callSubmitAction('del_serv', 'DeleteServiceButton', '')" class="sapBtnStd">
	<nobr>Delete Service</nobr></a>
<%
   }
%>
   </form>
<%
   }
%>

  <script language="JavaScript" type="text/javascript">
      <!--
      restoreFocus('<%=lastControlID%>');
      -->
  </script>
  </body>
</html>

