<!--#INCLUDE FILE="control.nxscript"-->

<%
const
  StructuredView = true;

var
  i: integer;
  j: integer;
  classsuff: string;
  id: integer;
  aComponent: TnxComponent;
  aValue: variant;
  aComponents: TStringlist;
  aName: String;
  aObject: TComponent;
  help: TStringlist;
  error: string;
begin
  id:=strtointdef(Request('Index'), -1);
  Server.SetVar('Refresh', '');
  Server.SetVar('Previous', 'Test');
%>

<!--#INCLUDE FILE="header.nxscript"-->

<!-- Write the left menu component list -->

<div id=leftmenu>
<div class=innertube>
<%
  NXServer.RefreshComponents;

  aComponent:=NXServer.GetComponent(8);   
  classsuff:='Active'
  if (aComponent is TnxStateComponent) then
    if NXServer.SettingValueFromName(NXServer.ComponentID(8), 'Active', 0)='0' then
      classsuff:='InActive';

  output.Write('<h3>Server Settings</h3>');

  aComponents:=TStringlist.Create;
  if StructuredView then
  begin
    aComponents.Sorted:=true;
    aComponents.Add(' Main Settings');
  end;

  try
    For i:=0 To NXServer.ComponentCount-1 do
    begin
      if NXServer.SettingCount(NXServer.ComponentID(i)) > 0 then begin
	  aComponent:=NXServer.GetComponent(i);
          if StructuredView then
                if trim(NXServer.ComponentCategory(i))<>'' then
                begin
                  aName:=NXServer.ComponentCategory(i)+', '+NXServer.ComponentDisplayName(i);
                  if aComponents.Indexof(NXServer.ComponentCategory(i))<0 then
                        aComponents.Add(NXServer.ComponentCategory(i));
                end
                else
                  aName:=' Main Settings, '+NXServer.ComponentDisplayName(i);

          aObject:=TComponent.Create(nil);
          aObject.Tag:=i;
          if StructuredView then
                aComponents.AddObject(aName, aObject)
          else
                aComponents.AddObject(NXServer.ComponentDisplayName(i), aObject);
      end;
    end;


    For j:=0 To aComponents.Count-1 do
    begin
      classsuff:='';
      if aComponents.Objects[j]<>nil then
      begin
        i:=TComponent(aComponents.Objects[j]).Tag;
        TComponent(aComponents.Objects[j]).Free;
        aComponents.Objects[j]:=nil;
        aComponent:=NXServer.GetComponent(i);   
        classsuff:='Active';

        if (aComponent is TnxStateComponent) then
          if NXServer.SettingValueFromName(NXServer.ComponentID(i), 'Active', 0)='0'  then
            classsuff:='InActive';
        
        if not SameText(aComponent.Name, 'ServerEngine_ServerSettings') then
          output.Write('<a class="'+classsuff+'" href="index.nxscript?index='+inttostr(i)+'">'+NXServer.ComponentDisplayName(i)+'</a><br>');
      end
      else
        output.Write('<h4>'+aComponents[j]{NXServer.ComponentDisplayName(i)}+'</h4>');
    end;
  finally
    aComponents.Free;
  end;
%>
</div>
</div>

<!-- CSS Main Content Wrapper -->

<div id=maincontent>  
<div class=innertube>

<!-- actual content start --> 



<%
//  Output.Writeln('<p class=noerror>'+Server.GetVar('Error')+'</p>');
  // print status/errors of the last action
  if copy(uppercase(Server.GetVar('Error')), 1, 3)='OK:' then
    Output.Writeln('<p class=noerror>'+Server.GetVar('Error')+'</p>');

  if id<0 then
  begin
%>
    <!--#INCLUDE FILE="startpage.nxscript"-->
<%
    exit;
  end;

  i:=id;  // set i to the current requested component id

  classsuff:='Active'
  if (NXServer.GetComponent(i) is TnxStateComponent) then
    if NXServer.SettingValueFromName(NXServer.ComponentID(i), 'Active', 0)='0' then
      classsuff:='InActive';
  aName:=NXServer.ComponentCategory(i);
%>

  <h3 class="ComponentHeader<%=classsuff%>" ><%=NXServer.ComponentDisplayName(i)%> Settings</h3>
<p>
<% if NXServer.ComponentName(i)='ServerEngine_AliasHandler' then 
  Output.Writeln('<form id="changecomponent" action="changesettings.nxscript" method="POST">')
 else 
  Output.Writeln('<form id="changecomponent" action="changesettings.nxscript" method="GET">');
%>
  <!-- Component ID and Category 
  
  <table class=componentinfo align=left>
    <tr>
		  <td class=identifier width="20%">Component ID</td>
			<td class=value width="90%"><%=NXServer.ComponentID(i)%></td>
		</tr>

    <tr>
		  <td class=identifier >Category</td>
			<td class=value><% Output.WriteLn(aName); %>
      </td>
		</tr>
	</table>
  <br>

	only needed for debug purposes -->

  <!-- Component settings table --> 

  <table class=propertytable align=left>
    <tr class=header>
      <td class=header width="20%">Setting</td>
      <td class=header width="80%">Value</td>
    </tr>

  <%
    For j:=0 To NXServer.SettingCount(NXServer.ComponentID(i))-1 do
    begin
      if NXServer.SettingVisible(NXServer.ComponentID(i), j) then
      begin
        aValue:=FixValue(NXServer.SettingValueFromIndex(NXServer.ComponentID(i), j), NXServer.SettingType(NXServer.ComponentID(i), j));
  %>

      <tr>
        <td class=identifier><%=NXServer.SettingLabel(NXServer.ComponentID(i), j)%></td>
        <td class=newvalue><% RenderControl(NXServer.ComponentID(i), NXServer.GetComponent(i), NXServer.SettingType(NXServer.ComponentID(i), j), j,
          NXServer.SettingName(NXServer.ComponentID(i), j), aValue, true); %>
        <input type=hidden name="Name<%=j%>" value="<%=NXServer.SettingName(NXServer.ComponentID(i), j)%>" />
        </td>
      </tr>
  <%
      end;
    end;
  %>

  </table>

  <!-- Action buttons -->

  <input class=submitform type="button" value="Reset Values" onClick="window.location.reload(true)" ></input>
  <input type=submit class=submitform value="Save Changes" /></input>

  <!-- set the hidden fields needed to change components -->
  <input type=hidden name="ComponentID" value="<%=NXServer.ComponentID(i)%>" />
  <input type=hidden name="ComponentDisplayName" value="<%=NXServer.ComponentDisplayName(i)%>" />
  <input type=hidden name="SettingCount" value="<%=NXServer.SettingCount(NXServer.ComponentID(i))%>" />
  <input type=hidden name="Index" value="<%=inttostr(i)%>"/>
  </form>
  </p>

<!-- actual content end --> 

<!-- help content start -->
<div id=help>
<h3>Help for <%=NXServer.ComponentDisplayName(i)%></h3>

<%
  help:=TStringlist.Create;
  try
    aname:='Adminroot\'+NXServer.ComponentName(i)+'.help';
    if fileexists(aName) then
    begin
      help.LoadFromFile(aName);
      for i:=0 to help.count-1 do
        Output.Writeln(help[i]);
    end
    else
      Output.Writeln('Sorry no help for this page. (File "'+aName+'" does not exist.)');
  finally
    help.free;
  end;
%>
</div>
</div>
</div>

</div>
</div>
<script language="javascript">
<%
  error := StringReplace(Server.GetVar('Error'), '\', '\\', [rfReplaceAll]);
  error := StringReplace(error, '''', '\''', [rfReplaceAll]);
  error := StringReplace(error, '<BR>', '\n', [rfReplaceAll, rfIgnoreCase]);
  if not (copy(uppercase(Server.GetVar('Error')), 1, 3)='OK:') then
  begin
    if copy(uppercase(Server.GetVar('Error')), 1, 8)='WARNING:' then
       Output.Writeln('alert(''Warning: \n\n'+error+''');')
    else if Trim(Server.GetVar('Error'))<>'' then
       Output.Writeln('alert(''Error: \n\n'+error+''');');
  end;

 
  Server.SetVar('Error', ' ');
%>
</script>
</html>

<%
end.
%>
