<%
var
  aCount, Changed: integer;
  j: integer;
  newValue: variant;
  w: WideString;
  thetype: string;
  ActiveSetToTrue: boolean;
  ActiveSetToFalse: boolean;
  IsDebug: boolean;

procedure Debug(aString: String);
begin
  if isDEbug then
    Output.Writeln(astring+'<br>');
end;

begin
  isDebug:=false;
%>
<html>

<head>
<%
  if not isDebug then 
    output.writeln('<meta http-equiv="refresh" content="1;url=index.nxscript?index='+Request('Index')+'&set=1">');
%>

<title></title>
</head>

<%
  Debug('Start!<br>');
  ActiveSetToTrue:=false;
  ActiveSetToFalse:=false;
  aCount:=0;
  if Request('SettingCount')<>'' then
    aCount:=Request('SettingCount');
  Debug('Count = '+Request('SettingCount'));
  if aCount=0 then
    exit;

  Server.SetVar('Error', 'OK: Settings saved successfully.');
  Changed := 0
  For j:=0 to aCount-1 do
  begin
    if SameText(Request('Name_'+inttostr(j)), 'Active') then
      If Request('NewValue_'+inttostr(j))<>Request('OLDValue_'+inttostr(j)) then
      begin
        // Active property was changed. If it was set to inactive then we need to set it as first
        // If it was set to active then we need to set it as last property.
        if Request('NewValue_'+inttostr(j))='1' then
          ActiveSetToTrue:=true
        else
          ActiveSetToFalse:=true
      end;
  end;

  if ActiveSetToFalse then
    NXServer.SetSettingValue(Request('ComponentID'), 'Active', 0);

  Debug('ValueCount:'+inttostr(aCount-1));
  For j:=0 To aCount-1 do
  begin
    Debug(Request('Name_'+inttostr(j)));
    // ignore Active property in this loop
    if SameText(Request('Name_'+inttostr(j)), 'Active') then
      continue;
    w:=Request('NewValue_'+inttostr(j));
    Debug('w-NewValue:'+w);
    Debug('OldValue:'+Request('OLDValue_'+inttostr(j)));
    If Request('NewValue_'+inttostr(j))=Request('OLDValue_'+inttostr(j)) then
      Debug('Values are same!');

    If Request('NewValue_'+inttostr(j))<>Request('OLDValue_'+inttostr(j)) then
    begin
      Debug('value changed');
      thetype:=ServerDecode(Request('Type_'+inttostr(j)));
      if (thetype='nxstCaption') then
        Continue;
      if ServerDecode(Request('NewValue_'+inttostr(j)))='' then
      begin
        if (thetype='nxstBoolean') or (thetype='nxstInteger') then
          newValue:=0
        else
          newValue:=ServerDecode(Request('NewValue_'+inttostr(j)));
      end
      else
        newValue:=ServerDecode(Request('NewValue_'+inttostr(j)));

      if (thetype='nxstMultiOption') and SameText(Request('Name_'+inttostr(j)), 'Value') then
      begin
        if NXServer.SetSettingValue(Request('ComponentID'), Request('Name_'+inttostr(j))+'_AsInt', newValue) Then
          changed := changed+1
        else
          Server.SetVar('Error', NXServer.LastErrorMessage);
      end
      else
      if (thetype='nxstUsers') then
      begin
        if NXServer.Setusers(newValue) Then
          changed := changed+1
        else
          Server.SetVar('Error', NXServer.LastErrorMessage);
      end
      else
      begin
        if NXServer.SetSettingValue(Request('ComponentID'), Request('Name_'+inttostr(j)), newValue) Then
          changed := changed+1
        else
          Server.SetVar('Error', NXServer.LastErrorMessage);
      end;
    end;
  end;

  if ActiveSetToTrue then
    if not NXServer.SetSettingValue(Request('ComponentID'), 'Active', 1) then
      Server.SetVar('Error', NXServer.LastErrorMessage);
end.
%>