{***************************************************************************}
{ TMS IntraWeb Component Pack Pro                                           }
{ for Delphi & C++Builder                                                   }
{                                                                           }
{ written by TMS Software                                                   }
{            copyright © 2002 - 2007                                        }
{            Email : info@tmssoftware.com                                   }
{            Web : http://www.tmssoftware.com                               }
{                                                                           }
{ The source code is given as is. The author is not responsible             }
{ for any possible damage done due to the use of this code.                 }
{ The component can be freely used in any application. The complete         }
{ source code remains property of the author and may not be distributed,    }
{ published, given or sold in any form as such. No parts of the source      }
{ code can be included in any other component or application without        }
{ written authorization of the author.                                      }
{***************************************************************************}

unit IWAutoFormFill;

interface

{$I TMSDEFS.INC}

uses
  {$IFDEF LINUX}
  QControls, QGraphics, QIWPicCntnr, QForms,
  {$ELSE}
  Windows, Graphics, Controls, IWPicCntnr, Forms,
  {$ENDIF}
  {$IFDEF DELPHI6_LVL}
  Types,
  {$ENDIF}
  Classes,
  IWControl, IWTypes, SysUtils, IWCompButton,
  IWCompEdit, IWCompLabel, IWCompCheckbox, IWExtCtrls, IWHTMLTag, IWCompListbox,
  IWTMSBase
  {$IFDEF TMSIW6}
  , IWRenderContext, IWColor
  {$ENDIF}
  {$IFDEF TMSIW7}
  , IWBaseHTMLControl, IWScriptEvents
  {$ENDIF}  
  {$IFDEF TMSIW9}
  , IWXMLTag
  {$ENDIF}
  ;

type

  {$IFDEF TMSIW9}
  TIWAsyncClickEvent = procedure (Sender: TObject; EventParams: TStringList; Value: Integer) of object;
  {$ENDIF}

  TTIWGradientDirection = (gdHorizontal, gdVertical);
  TTIWPersistMode = (pmServer, pmCookies);

  TFormControlItem = class(TCollectionItem)
  private
    {$IFDEF TMSIW6}
    FControl: TIWCustomControl;
    {$ELSE}
    FControl: TIWControl;
    {$ENDIF}
  published
    {$IFDEF TMSIW6}
    property Control: TIWCustomControl read FControl write FControl;
    {$ELSE}
    property Control: TIWControl read FControl write FControl;
    {$ENDIF}
  end;


  TFormControls = class(TCollection)
  private
    function GetItem(Index: Integer): TFormControlItem;
    procedure SetItem(Index: Integer; const Value: TFormControlItem);
  public
    constructor Create;
    function Add: TFormControlItem;
    function Insert(Index: Integer): TFormControlItem;
    property Items[Index: Integer]: TFormControlItem read GetItem write SetItem; default;
  end;


  TIWAFFHighLight = class(TPersistent)
  private
    FBold: Boolean;
    FBackColor: TIWColor;
    FForeColor: TIWColor;
    FItalic: Boolean;
    FUnderline: Boolean;
  public
    constructor Create;
    destructor Destroy; override;
  published
    property Bold: Boolean read FBold write FBold;
    property BackColor: TIWColor read FBackColor write FBackColor;
    property ForeColor: TIWColor read FForeColor write FForeColor;
    property Italic: Boolean read FItalic write FItalic;
    property Underline: Boolean read FUnderline write FUnderline;
  end;

  TIWAFFDropDown = class(TPersistent)
  private
    FBackColor: TIWColor;
    FBackColorTo: TIWcolor;
    FBorder: Boolean;
    FBorderColor: TIWColor;
    FBorderWidth: Integer;
    FForeColor: TIWColor;
    FFormat: string;
    FGradientDirection: TTIWGradientDirection;
    FHighLightStyle: TIWAFFHighLight;
    FHoverColor: TIWColor;
    FHoverForeColor: TIWColor;
    procedure SetBackColor(const Value: TIWColor);
  public
    constructor Create;
    destructor Destroy; override;
  published
    property BackColor: TIWColor read FBackColor write SetBackColor;
    property BackColorTo: TIWColor read FBackColorTo write FBackColorTo;
    property Border: Boolean read FBorder write FBorder;
    property BorderColor: TIWColor read FBorderColor write FBorderColor;
    property BorderWidth: integer read FBorderWidth write FBorderWidth;
    property ForeColor: TIWColor read FForeColor write FForeColor;
    property Format: string read FFormat write FFormat;
    property GradientDirection: TTIWGradientDirection read FGradientDirection write FGradientDirection;
    property HighLightStyle: TIWAFFHighLight read FHighLightStyle write FHighLightStyle;
    property HoverColor: TIWColor read FHoverColor write FHoverColor;
    property HoverForeColor: TIWColor read FHoverForeColor write FHoverForeColor;
  end;


  TTIWAutoFormFill = class(TIWCustomEdit)
  private
    FDropDown: TIWAFFDropDown;
    FPersistMode: TTIWPersistMode;
    FPresetSort: Boolean;
    FPresetValues: TStringList;
    FFormControls: TFormControls;
    {$IFDEF TMSIW9}
    FOnAsyncClick: TIWAsyncClickEvent;
    {$ENDIF}
    procedure SetPresetValues(const Value: TStringList);
    procedure SetFormControls(const Value: TFormControls);
  protected
    function HTMLClr(color: TColor): string;
    procedure SetValue(const AValue: string); override;
    {$IFDEF TMSIW72_NEW}
    function get_ShouldRenderTabOrder: Boolean; override;
    {$ENDIF}
  public
    {$IFDEF TMSIW72}
    function get_HasTabOrder: boolean; override;
    {$ENDIF}
    {$IFDEF TMSIW6}
    procedure IWPaint; override;
    function RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag; override;
    {$ELSE}
    procedure Paint; override;
    function RenderHTML: TIWHTMLTag; override;
    {$ENDIF}
    {$IFDEF TMSIW9}
    function RenderAsync(AContext: TIWBaseHTMLComponentContext): TIWXMLTag; override;
    {$ENDIF}
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Enabled;
    property DropDown: TIWAFFDropDown read FDropDown write FDropDown;
    property FormControls: TFormControls read FFormControls write SetFormControls;
    property PersistMode: TTIWPersistMode read FPersistMode write FPersistMode;
    property PresetSort: Boolean read FPresetSort write FPresetSort;
    property PresetValues: TStringList read FPresetValues write SetPresetValues;
    {$IFDEF TMSIW9}
    property OnAsyncClick: TIWAsyncClickEvent read FOnAsyncClick write FOnAsyncClick;
    {$ENDIF}
  end;

implementation

uses
  {$IFDEF LINUX}
  QImgList,
  {$ELSE}
  ImgList, ShellAPI, CommCtrl,
  {$ENDIF}
  IWApplication, IWServerControllerBase,
  SWStrings, SWSystem, IWAppForm;

{ TTIWAutoFormFill }
{$IFDEF TMSIW9}
function TTIWAutoFormFill.RenderAsync(
  AContext: TIWBaseHTMLComponentContext): TIWXMLTag;
begin

  Result := TIWXMLTag.CreateTag('control');
  try
    Result.AddStringParam('id', HTMLName);
    Result.AddStringParam('type', 'TTIWAUTOFORMFILL');
    RenderAsyncCommonProperties( AContext, Result );
  except
    FreeAndNil(Result);
  end;

end;
{$ENDIF}

constructor TTIWAutoFormFill.Create(AOwner: TComponent);
begin
  inherited;
  {$IFDEF TMSIW6}
  SetRenderSize(True);
  {$ELSE}
  FRenderSize := True;
  {$ENDIF}
  FNeedsFormTag := True;
  {$IFDEF TMSIW6}
  {TODO}
  {$ELSE}
  FSupportedScriptEvents := 'OnChange,OnSelect';
  {$ENDIF}

  FDropDown := TIWAFFDropDown.Create;
  FDropDown.FBackColor := clWhite;
  FDropDown.FBackColorTo := clWhite;
  FDropDown.FHoverColor := clLime;
  FDropDown.FHighLightStyle.ForeColor := clRed;
  FDropDown.FBorderWidth := 1;

  FPresetValues := TStringList.Create;
  FFormControls := TFormControls.Create;
end;

destructor TTIWAutoFormFill.Destroy;
begin
  FDropDown.Free;
  FPresetValues.Free;
  FFormControls.Free;
  inherited;
end;

{$IFDEF TMSIW72_NEW}
function TTIWAutoFormFill.get_ShouldRenderTabOrder: boolean;
begin
  Result := false;
end;
{$ENDIF}

{$IFDEF TMSIW72}
function TTIWAutoFormFill.get_HasTabOrder: boolean;
begin
  {$IFNDEF TMSIW72_NEW}
  Result := false;
  {$ELSE}
  Result := true;
  {$ENDIF}
end;
{$ENDIF}

{$IFDEF TMSIW6}
procedure TTIWAutoFormFill.IWPaint;
{$ELSE}                                              
procedure TTIWAutoFormFill.Paint;
{$ENDIF}
begin
{$IFDEF TMSIW6}
  with Canvas do
{$ELSE}
  with FCanvas do
{$ENDIF}
  begin
    Brush.Color := clBtnFace;
    Pen.Color := clBtnFace;
    Rectangle(0,0,Width - 1,Height - 1);
    TextOut(8,6,'AutoFormFill');
  end;
end;

procedure TTIWAutoFormFill.SetValue(const AValue: string);
begin
  if Enabled then
  begin
    inherited SetValue(AValue);
  end;
end;

procedure TTIWAutoFormFill.SetFormControls(const Value: TFormControls);
begin
  FFormControls.Assign(Value);
end;

procedure TTIWAutoFormFill.SetPresetValues(const Value: TStringList);
begin
  FPresetValues.Assign(Value);
end;


function TTIWAutoFormFill.HTMLClr(color: TColor):string;
begin
  Result := ColorToRGBString(color);
end;

{$IFDEF TMSIW6}
function TTIWAutoFormFill.RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag;
{$ELSE}
function TTIWAutoFormFill.RenderHTML: TIWHTMLTag;
{$ENDIF}
var
  htmlres: string;
  ajaxcall: string;
  s: string;
  strHL, strHLE: string;
  strFS, ddstyle: string;
  GradientType: integer;

   function StripCRLF(s:string): string;
   begin
     while pos(#13,s) > 0 do
       delete(s,pos(#13,s),1);
     while pos(#10,s) > 0 do
       delete(s,pos(#10,s),1);
     Result := s;
   end;

   function MakeScript(Name:string): string;
   var
   i: integer;
   begin

      {$IFDEF TMSIW9}
      if Assigned(FOnAsyncClick) then
      begin
        ajaxcall := 'processAjaxEvent(''onClick'', '
          + HTMLControlImplementation.IWCLName
          + ',''' + HTMLName + '.' + 'DoOnAsyncClick' + ''','
          + 'true' + ', null, '
          + 'true' + ');';

        Acontext.WebApplication.RegisterCallBack(HTMLName+'.DoOnAsyncClick', DoOnAsyncClick);
      end;
      {$ENDIF}

            strHL := EmptyStr;
            strHLE := EmptyStr;

            if (DropDown.HighlightStyle.BackColor <> clNone) then
                strHL := strHL + 'background-color:' + HTMLClr(DropDown.HighlightStyle.BackColor) + ';';

            if (DropDown.HighlightStyle.ForeColor <> clNone) then
                strHL := strHL + 'color:' + HTMLClr(DropDown.HighlightStyle.ForeColor) + ';';

            if (DropDown.HighlightStyle.Bold) then
                strHL := strHL + 'font-weight:bold;';

            if (DropDown.HighlightStyle.Italic) then
                strHL := strHL + 'font-style:italic;';

            if (DropDown.HighlightStyle.Underline) then
                strHL := strHL + 'text-decoration:underline;';

            if (Length(strHL) > 0) then
            begin
                strHL := '<span style=\''' + strHL + '\''>';
                strHLE := '</span>';
            end;

//            ctrlColl := EmptyStr;
//            if FormControls <> nil then
//              ctrlColl := FormControl.HTMLName;


     Result := ''#13

        + 'var extdiv = document.getElementById("' + HTMLName + '");'#13
        + 'extdiv.style.position = "absolute";'#13
        + 'extdiv.style.top = 0;'#13
        + 'extdiv.style.left = 0;'#13
        + 'extdiv.style.width = "100%";'#13
        + 'extdiv.style.height = 0;'#13
        + 'extdiv.style.zIndex = 9999;'#13
        + HTMLName + 'affAddAllEvents();'#13
        ;

            if (PersistMode = pmServer) then
            begin

                if (Pos('#', PresetValues.GetText()) >= 0) then
                begin
                    s := StripCRLF(PresetValues.GetText());
                    s := StringReplace(s, ';', '~', [rfReplaceAll]);

                      while pos('#', s) > 0 do
                      begin
                        Result := Result + HTMLName + 'arValues[' + HTMLName + 'arValues.length] = "'
                          + copy(s, 1, pos('#', s)-1) + '";'#13;

                          Delete(s, 1, pos('#', s));
                      end;

                    if (PresetSort) then
                        Result := Result + '  ' + HTMLName + 'arValues.sort();'#13;
                end;
            end;

        {$IFDEF TMSIW6}
        TIWComponent40Context(AContext).AddToInitProc(Result + EOL);
        {$ELSE}
        TIWAppForm(Form).AddToInitProc(Result + EOL);
        {$ENDIF}

 //prevent submit while dropdown is visible

          Result :=   '<Script Language="JavaScript">'#13

//          Result := Result

            + ' isIE = navigator.appName.indexOf("Microsoft") != -1;'#13
            + 'var ' + HTMLName + 'firstBlur = true;'#13
            + 'var ' + HTMLName + 'firstAValue;'#13
            + 'var ' + HTMLName + 'si = -1;'#13
            + 'var ' + HTMLName + 'm = -1;'#13
            + 'var ' + HTMLName + 'arValues = new Array;'#13

            + 'var arSub = new Array();'#13
            + 'function affDisableSubmit()'#13
            + '{'#13
            + '  if (arSub.length == 0)'#13
            + '  {'#13
            + '    for (var i = 0; i < document.forms.length; i++)'#13
            + '    {'#13
            + '      arSub[arSub.length] = document.forms[i].onsubmit;'#13
            + '      document.forms[i].onsubmit = function(){return false;};'#13
            + '    }'#13
            + '  }'#13
            + '}'#13

            + 'function affEnableSubmit()'#13
            + '{'#13
            + '  if (arSub.length > 0)'#13
            + '  {'#13
            + '    for (var i = 0; i < arSub.length; i++)'#13
            + '    {'#13
            + '      document.forms[i].onsubmit = arSub[i];'#13
            + '      arSub = new Array();'#13
            + '    }'#13
            + '  }'#13
            + '}'#13;


            Result := Result
                + 'function affAddEvent( obj, type, fn ) {'#13
                + '  if ( obj.attachEvent ) {'#13
                + '    obj["e"+type+fn] = fn;'#13
                + '    obj[type+fn] = function(){obj["e"+type+fn]( window.event );}'#13
                + '    obj.attachEvent("on"+type, obj[type+fn] );'#13
                + '  } else'#13
                + '    obj.addEventListener( type, fn, false );'#13
                + '}'#13;


            if FormControls.Count > 0 then
            begin
                Result := Result
                    + '    function ' + HTMLName + 'affFctChange(el, e)'#13
                    + '    {'#13
                    + '      ' + HTMLName + 'm = -1;'#13
                    + '      var key;'#13
                    + '      if (isIE) key = event.keyCode; else key = e.which;'#13
                    + '      document.getElementById("' + HTMLName + 'dd").innerHTML = "";'#13
                    + '      var arTemp = ' + HTMLName + 'arValues.slice();'#13
                    + '      for (j = 0; j < ' + HTMLName + 'arValues.length; j++)'#13
                    + '      {'#13
                    + '        var found = false;'#13
                    + '        var sVal = ' + HTMLName + 'arValues[j].split("~");'#13
                    + '        for (var k = 0; k < sVal.length; k++)'#13
                    + '        {'#13
                    + '          if (sVal[k].toLowerCase().indexOf(el.value.toLowerCase()) == 0)'#13
                    + '            found = true;'#13
                    + '        }'#13
                    + '        if (found)'#13
                    + '        {'#13
                    + '          ' + HTMLName + 'm++;'#13
                    + '          var myVal = arTemp[j];'#13
                    + '          var tmpVal = myVal.substring(myVal.toLowerCase().indexOf(el.value.toLowerCase()), myVal.toLowerCase().indexOf(el.value.toLowerCase()) + el.value.length);'#13
                    + '          myVal = myVal.replace(tmpVal, "' + strHL + '" + tmpVal + "' + strHLE + '");'#13;

                if (DropDown.Format = EmptyStr) then
                begin
                    Result := Result
                        + '          myVal = myVal.replace(/~/g, ", ").substring(0, myVal.replace(/~/g, ", ").length)'#13
                end
                else
                begin
                    Result := Result
                        + '          sMyVal = myVal.split("~");'#13
                        + '          myVal = "'+DropDown.Format+'";'#13
                        + '          for (var n = 0; n < sMyVal.length; n++)'#13
                        + '            myVal = myVal.replace("%" + (n+1) + "", sMyVal[n]);'#13
                        ;
                end;

                Result := Result
                    + '          document.getElementById("' + HTMLName + 'dd").innerHTML += '

                    + '"<div id=\''' + HTMLName + 'mydiv"+' + HTMLName + 'm+"\'' style=\''width:auto;padding:2px;\'' onmouseover=\'''
                    + HTMLName + 'affe(" + ' + HTMLName + 'm + ")\'' onmouseout=\'''
                    + HTMLName + 'affl(" + ' + HTMLName + 'm + ")\'' onmousedown=\'''
                    + HTMLName + 'affc(" + j + ")\''><input type=\''hidden\'' id=\'''
                    + HTMLName + 'myIndex"+' + HTMLName + 'm+"\'' value=\''"+j+"\''>" + myVal + "</div>";'#13
                    + '        }'#13
                    + '      }'#13
                    + '     if ((document.getElementById("' + HTMLName + 'dd").innerHTML != "") '
                    + '&& (el.value != ""))'#13
                    + '     {'#13
                    + '       affDisableSubmit();'#13
                    + '       var mydiv = document.getElementById("' + HTMLName + 'dd");'#13
                    + '       mydiv.style.top = (el.offsetTop + el.offsetHeight + 1) + "px";'#13
                    + '       mydiv.style.left = (el.offsetLeft) + "px";'#13
                    + '       if (el.parentNode.id == el.id + "DIV"){'#13 //TIWAdvEdit Compatibility
                    + '         mydiv.style.top = (parseInt(mydiv.style.top) + el.parentNode.offsetTop) + "px";'#13
                    + '         mydiv.style.left = (parseInt(mydiv.style.left) + el.parentNode.offsetLeft) + "px";'#13
                    + '       }'#13
                    + '       mydiv.style.display = "";'#13

                    + '      if ( (key == 13) || (key == 27) )'#13
                    + '         ' + HTMLName + 'fctBlur()'#13
                    + '      else if (key == 40)'#13
                    + '      {'#13
                    + '        if (' + HTMLName + 'si < ' + HTMLName + 'm)'#13
                    + '          ' + HTMLName + 'affe(' + HTMLName + 'si+1);'#13
                    + '        else'#13
                    + '          ' + HTMLName + 'affe(' + HTMLName + 'si);'#13
                    + '      }'#13
                    + '      else if (key == 38) '#13
                    + '      {'#13
                    + '        if (' + HTMLName + 'si > 0) '#13
                    + '          ' + HTMLName + 'affe(' + HTMLName + 'si-1);'#13
                    + '        else'#13
                    + '          ' + HTMLName + 'si = -1;'#13
                    + '      }'#13
                    + '      else'#13
                    + '        ' + HTMLName + 'si = -1;'#13
                    + '     }'#13
                    + '     else'#13
                    + '       ' + HTMLName + 'fctBlur();'#13

                    + '    }'#13;
            end;

              Result := Result
                    + '    function ' + HTMLName + 'fctBlur()'#13
                    + '   {'#13
                    + '     ' + HTMLName + 'm = -1;'#13
                    + '     ' + HTMLName + 'si = -1;'#13
                    + '     document.getElementById("' + HTMLName + 'dd").style.display = "none";'#13
                    + '     document.getElementById("' + HTMLName + 'HiddenField").value = ""'#13;


                for i := 0 to FormControls.Count - 1 do
                begin
                    s := FormControls.Items[i].Control.HTMLName;
                    if (Length(s) > 0) then
                        Result := Result + ' + document.getElementById("' + s + '").value + "~"'#13;
                end;


                Result := Result + ';'#13;

                if (PersistMode = pmCookies) then
                begin
                    Result := Result + HTMLName + 'SavePersistent("'
                        + HTMLName + 'dd",document.getElementById("' + HTMLName
                        + 'HiddenField").value);'#13;
                end;

                Result := Result
                    + '       affEnableSubmit();'#13
                    + '    }'#13

                    + '  function '+HTMLName+'affAddAllEvents(){'#13;

                for i := 0 to FormControls.Count - 1 do
                begin
                    s := FormControls.Items[i].Control.HTMLName;
                    if (Length(s) > 0) then
                    begin
                        Result := Result
                            + '    if (document.getElementById("' + s + '") != null)'#13
                            + '    {'#13


                            + '      affAddEvent(document.getElementById("' + s + '") , "click", function (e)'#13
                            + '        {'#13
                            + '          document.onmouseup = function()'#13
                            + '          {'#13
                            + '            document.getElementById("' + s + '").focus();'#13
                            + '            document.onmouseup = null;'#13
                            + '          };'#13
                            + '        }'#13
                            + '      );'#13


                            + '      affAddEvent(document.getElementById("' + s + '") , "keyup", function (e)'#13
                            + '        {'#13
                            + '          return ' + HTMLName + 'affFctChange(document.getElementById("' + s + '"), e);'#13
                            + '        }'#13
                            + '      );'#13

                            + '      affAddEvent(document.getElementById("' + s + '") , "keydown", function (e)'#13
                            + '     {'#13
                            + '       var key;'#13
                            + '       if (isIE) key = event.keyCode; else key = e.which;'#13
                            + '       if (key == 13)'#13
                            + '        {'#13
                            + '          if (document.getElementById("' + HTMLName + 'myIndex" + ' + HTMLName + 'si))'#13
                            + '          {'#13
                            + '            var index = document.getElementById("' + HTMLName + 'myIndex" + ' + HTMLName + 'si).value;'#13
                            + '            ' + HTMLName + 'affc(index);'#13
                            + '          document.getElementById("' + s + '").focus();'#13
                            + '            return false;'#13
                            + '          }'#13
                            + '        }'#13
                            + '     }'#13
                            + '      );'#13

                            + '      affAddEvent(document.getElementById("' + s + '") , "blur", function ()'#13
                            + '     {'#13
                            + '        ' + HTMLName + 'fctBlur();'#13
                            + '     }'#13
                            + '      );'#13

                            + '     document.getElementById("' + s + '").autocomplete = "off";'#13
                            + '    }'#13
                            + '    else'#13
                            + '    {'#13
                            + '        alert("Control assigned to ' + s + ' doesn''t exist")'#13
                            + '    }'#13;
                    end;
                end;

          Result := Result
            + '}'#13
            
            + 'function ' + HTMLName + 'affe(index)'#13
            + '{'#13
            + '  for (var i = 0; i <= ' + HTMLName + 'm; i++)'#13
            + '    ' + HTMLName + 'affl(i);'#13
            + '  var el = document.getElementById("' + HTMLName + 'mydiv"+index);'#13
            + '  el.style.backgroundColor = "' + HTMLClr(DropDown.HoverColor) + '";'#13
            + '  el.style.color = "' + HTMLClr(DropDown.HoverForeColor) + '";'#13
            + '  ' + HTMLName + 'si = index;'#13
            + '}'#13
            + 'function ' + HTMLName + 'affl(index)'#13
            + '{'#13
            + '  var el = document.getElementById("' + HTMLName + 'mydiv"+index);'#13
            + '  el.style.backgroundColor = "";'#13
            + '  el.style.color = "' + HTMLClr(DropDown.ForeColor) + '";'#13
            + '  ' + HTMLName + 'si = -1;'#13
            + '}'#13

            + 'function ' + HTMLName + 'affc(index)'#13
            + '{'#13
            + ' var myval = ' + HTMLName + 'arValues[parseInt(index)];'#13
            + ' sVal = myval.split("~");'#13;

            for i := 0 to FormControls.Count - 1 do
            begin
                s := FormControls.Items[i].Control.HTMLName;
                Result := Result
                    + '    if (document.getElementById("' + s + '") != null)'#13
                    + '    {'#13
                    + '        document.getElementById("' + s + '").value = sVal[' + IntToStr(i) + '];'#13
                    + '    }//'#13;
            end;

            Result := Result
                + '}'#13;

       if (PersistMode = pmCookies) then
       begin

        Result := Result + HTMLName + 'LoadPersistent("' + HTMLName + 'dd");'#13;

        Result := Result
            + 'function affGetCookieValue(s,n) {'#13
            + ' s=affRemoveBlanks(s)'#13
            + ' var pairs=s.split(";")'#13
            + ' for(var i=0;i<pairs.length;++i) {'#13
            + ' var pairSplit=pairs[i].split("=")'#13
            + '  su = pairSplit[0];'#13
            + '  if (su.charAt(0) == "|")'#13
            + '      su  = su.substring(1,su.length);'#13
            + '  if(su==n) {'#13
            + '  if(pairSplit.length>1) return (pairSplit[1])'#13
            + '  else return ""'#13
            + '   }'#13
            + '  }'#13
            + ' return ""'#13
            + '}'#13

            + 'function affRemoveBlanks(s) {'#13
            + ' var temp=""'#13
            + ' for(var i=0;i<s.length;++i) {'#13
            + ' var c=s.charAt(i)'#13
            + '  if(c!=" ") temp += c;'#13
            + '  else temp += "|";'#13
            + '  }'#13
            + ' return temp'#13
            + '}'#13

            + 'function affInsertBlanks(s) {'#13
            + ' var temp=""'#13
            + ' for(var i=0;i<s.length;++i) {'#13
            + ' var c=s.charAt(i)'#13
            + '  if(c!="|") temp += c;'#13
            + '  else temp += " ";'#13
            + '  }'#13
            + ' return temp'#13
            + '}'#13

            + 'function ' + HTMLName + 'LoadPersistent(CompName)'#13
            + ' {'#13
            + '  s = document.cookie;'#13
            + '  s = affGetCookieValue(s,CompName);'#13
            + '  s = affInsertBlanks(s);'#13
            + '  var splitS = s.split("#");'#13
            + '  for (i = 1; i < splitS.length; i++)'#13
            + '    ' + HTMLName + 'arValues[' + HTMLName + 'arValues.length] = splitS[i];'#13;

        if (PresetSort) then
          Result := Result + '  ' + HTMLName + 'arValues.sort();'#13;

        Result := Result
            + ' }'#13
            + 'function ' + HTMLName + 'SavePersistent(CompName,AValue)'#13
            + ' {'#13
            + '  if (' + HTMLName + 'firstBlur)'#13
            + '  {'#13
            + '    p = document.cookie;'#13
            + '    p = affGetCookieValue(p,CompName);'#13
            + '    p = affInsertBlanks(p);'#13
            + '    if (p.indexOf(AValue) < 0)'#13
            + '      AValue = p + "#" + AValue;'#13
            + '    else'#13
            + '      AValue = p;'#13
            + '    ' + HTMLName + 'firstAValue = p;'#13
            + '    ' + HTMLName + 'firstBlur = false;'#13
            + '  }'#13
            + '  else'#13
            + '  {'#13
            + '    if (' + HTMLName + 'firstAValue.indexOf(AValue) < 0)'#13
            + '      AValue = ' + HTMLName + 'firstAValue + "#" + AValue;'#13
            + '    else'#13
            + '      AValue = ' + HTMLName + 'firstAValue;'#13
            + '  }'#13
            + '  expirationDate ="Thursday, 01-Dec-11 12:00:00 GMT"'#13
            + '  s = CompName+"="+AValue+";expires="+expirationDate;'#13
            + '  document.cookie = s;'#13
            + ' }'#13;
        end;

      Result := Result + '</Script>';

   end;

begin

        htmlres := htmlres +
          '<input type="hidden" value="" name="' + HTMLName + 'HT" id="' + HTMLName + 'HiddenField">';

//DropDown
                strFS := EmptyStr;

//                if (Font.Name != string.Empty)
//                    strFS += "font-family:" + Font.Name + ";";

//                if (Font.Bold)
//                    strFS = strFS + 'font-weight:bold;';

//                if (Font.Italic)
//                    strFS += "font-style:italic;";

//                if (Font.Underline)
//                    strFS += "text-decoration:underline;";

//                if (Font.Size.ToString() != string.Empty)
//                    strFS += "font-size:" + Font.Size + ";";

                ddstyle := strFS + 'border:'+IntToStr(DropDown.BorderWidth)+'px solid '+HTMLClr(DropDown.BorderColor)
                    +';display:none;position:absolute;top:0px;left:0px;z-index:9999;background:'+HTMLClr(DropDown.BackColor)
                    +';color:'+HTMLClr(DropDown.ForeColor)+';width:auto;height:auto;padding:2px;cursor:pointer;';

                GradientType := 0;
                if (DropDown.GradientDirection = gdHorizontal) then
                    GradientType := 1;

                if ( (DropDown.BackColor <> clNone) and (DropDown.BackColorTo <> clNone) ) then
                    ddstyle := ddstyle + 'FILTER: progid:DXImageTransform.Microsoft.Gradient(GradientType=' + IntToStr(GradientType)
                        + ',StartColorStr=' + HTMLClr(DropDown.BackColor)
                        + ',EndColorStr=' + HTMLClr(DropDown.BackColorTo) + ')';

                htmlres := htmlres + '<div style="' + ddstyle + '" id="' + HTMLName + 'dd">'
                + '</div>';

                // end DropDown

    htmlres := htmlres +
     MakeScript(HTMLName);

  Result := TIWHTMLTag.CreateTag('DIV');
  Result.Contents.AddText(htmlres);
end;


{TTIWAFFDropDown}

procedure TIWAFFDropDown.SetBackColor(const Value: TIWColor);
begin
  FBackColor := Value;
  //Invalidate;
end;

constructor TIWAFFDropDown.Create;
begin
  inherited;
  FHighLightStyle := TIWAFFHighLight.Create;
end;

destructor TIWAFFDropDown.Destroy;
begin
  inherited;
  FHighLightStyle.Free;
end;

{TTIWAFFHighLight}
constructor TIWAFFHighLight.Create;
begin
  inherited;
end;

destructor TIWAFFHighLight.Destroy;
begin
  inherited;
end;

{ TFormControls }

function TFormControls.Add: TFormControlItem;
begin
  Result := TFormControlItem(inherited Add);
end;

constructor TFormControls.Create;
begin
  inherited Create(TFormControlItem);
end;

function TFormControls.GetItem(Index: Integer): TFormControlItem;
begin
  Result := TFormControlItem(inherited Items[Index]);
end;

function TFormControls.Insert(Index: Integer): TFormControlItem;
begin
  Result := TFormControlItem(inherited Insert(Index));
end;

procedure TFormControls.SetItem(Index: Integer;
  const Value: TFormControlItem);
begin
  inherited Items[Index] := Value;
end;


end.
