{***************************************************************************}
{ TMS IntraWeb Component Pack Pro                                           }
{ for Delphi & C++Builder                                                   }
{                                                                           }
{ written by TMS Software                                                   }
{            copyright © 2002 - 2009                                        }
{            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 IWTMSCheckList;

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, IWFont,
  IWCompEdit, IWCompLabel, IWCompCheckbox, IWExtCtrls, IWHTMLTag, IWCompListbox,
  IWTMSBase
  {$IFDEF TMSIW6}
  , IWRenderContext, IWColor
  {$ENDIF}
  {$IFDEF TMSIW9}
  , IWXMLTag
  {$ENDIF}
  ;

type

  TTIWGradientDirection = (gdHorizontal, gdVertical);
  TTIWHelpTypes = (htNone, htLabel, htHint);

  {$IFDEF TMSIW9}
  TIWAsyncCheckClick = procedure (Sender: TObject; EventParams: TStringList; Index: Integer; Check: bool) of object;
  {$ENDIF}


  TTIWCheckList = class(TIWCustomEdit)
  private
    FBorderWidth: Integer;
    FColor: TColor;
    FPopUpColor: TColor;
    FPopUpColorTo: TColor;
    FPopUpColorGradientDirection: TTIWGradientDirection;
    FPopUpHeight: Integer;
    FPopUpWidth: Integer;
    FBorderColor: TColor;
    FFlat: Boolean;
    FAlignment: TAlignment;
    FFocusColor: TColor;
    FSelectAll: Boolean;
    FAdvanceOnReturn: Boolean;
    FSubmitOnReturn: Boolean;
    FList: TStringList;
    FSelected: TList;
    FCheckAllBox: bool;
    FCheckAllHelp: TTIWHelpTypes;
    FCheckAllText: string;
    FUnCheckAllText: string;    
    FDropDownFont: TIWFont;
    FCheckClick: integer;
    {$IFDEF TMSIW9}
    FOnAsyncCheckClick: TIWAsyncCheckClick;
    procedure DoOnAsyncCheckClick(AParams: TStringList);
    {$ENDIF}
    procedure SetBorderColor(const Value: TColor);
    procedure SetBorderWidth(const Value: Integer);
    procedure SetColor(const Value: TColor);
    procedure SetFlat(const Value: Boolean);
    procedure SetAlignment(const Value: TAlignment);
    procedure SetList(const Value: TStringList);
    function GetSelected(i: integer): boolean;
    procedure SetSelected(i: Integer; value: boolean);
    procedure SetDropDownFont(const Value: TIWFont);
  protected
    function HTMLClr(color: TColor): string;
    procedure SetValue(const value:string); override;
  public
    {$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;
    property Selected[i: Integer]: boolean read GetSelected write SetSelected;
    property Text;
  published
    property AdvanceOnReturn: Boolean read FAdvanceOnReturn write FAdvanceOnReturn;
    property Alignment: TAlignment read FAlignment write SetAlignment;
    property BorderColor: TColor read FBorderColor write SetBorderColor;
    property BorderWidth: Integer read FBorderWidth write SetBorderWidth;
    property Color: TColor read FColor write SetColor default clWindow;
    property DropDownFont: TIWFont read FDropDownFont write SetDropDownFont;
    property PopUpColor: TColor read FPopUpColor write FPopUpColor;
    property PopUpColorTo: TColor read FPopUpColorTo write FPopUpColorTo;
    property PopUpColorGradientDirection: TTIWGradientDirection read FPopUpColorGradientDirection write FPopUpColorGradientDirection;
    property PopUpHeight: Integer read FPopUpHeight write FPopUpHeight;
    property PopUpWidth: Integer read FPopUpWidth write FPopUpWidth;
    property FocusColor: TColor read FFocusColor write FFocusColor default clWindow;
    property Flat: Boolean read FFlat write SetFlat;
    property Items: TStringList read FList write SetList;
    property SelectAll: Boolean read FSelectAll write FSelectAll;
    property SubmitOnReturn: Boolean read FSubmitOnReturn write FSubmitOnReturn;
    property CheckAllBox: bool read FCheckAllBox write FCheckAllBox;
    property CheckAllHelp: TTIWHelpTypes read FCheckAllHelp write FCheckAllHelp;
    property CheckAllText: string read FCheckAllText write FCheckAllText;
    property UnCheckAllText: string read FUnCheckAllText write FUnCheckAllText;
    {$IFDEF TMSIW9}
    property OnAsyncCheckClick: TIWAsyncCheckClick read FOnAsyncCheckClick write FOnAsyncCheckClick;
    {$ENDIF}
  end;


  TTIWCheckListBox = class(TIWCustomEdit)
  private
    FBorderWidth: Integer;
    FBGColorTo: TColor;
    FBGColorGradientDirection: TTIWGradientDirection;
    FBorderColor: TColor;
    FList: TStringList;
    FSelected: TList;
    FCheckAllBox: bool;
    FCheckAllHelp: TTIWHelpTypes;
    FCheckAllText: string;
    FUnCheckAllText: string;
    FCheckClick: integer;
    {$IFDEF TMSIW9}
    FOnAsyncCheckClick: TIWAsyncCheckClick;
    procedure DoOnAsyncCheckClick(AParams: TStringList);
    procedure DoOnAsyncCheckAllClick(AParams: TStringList);
    {$ENDIF}
    procedure SetBorderColor(const Value: TColor);
    procedure SetBorderWidth(const Value: Integer);
    procedure SetList(const Value: TStringList);
    function GetSelected(i: integer): boolean;
    procedure SetSelected(i: Integer; value: boolean);
  protected
    function HTMLClr(color: TColor): string;
    procedure SetValue(const value:string); override;
  public
    {$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;
    property Selected[i: Integer]: boolean read GetSelected write SetSelected;
    property Text;
  published
    property BorderColor: TColor read FBorderColor write SetBorderColor;
    property BorderWidth: Integer read FBorderWidth write SetBorderWidth;
    property BGColorTo: TColor read FBGColorTo write FBGColorTo;
    property BGColorGradientDirection: TTIWGradientDirection read FBGColorGradientDirection write FBGColorGradientDirection;
    property Items: TStringList read FList write SetList;
    property CheckAllBox: bool read FCheckAllBox write FCheckAllBox;
    property CheckAllHelp: TTIWHelpTypes read FCheckAllHelp write FCheckAllHelp;
    property CheckAllText: string read FCheckAllText write FCheckAllText;
    property UnCheckAllText: string read FUnCheckAllText write FUnCheckAllText;
    {$IFDEF TMSIW9}
    property OnAsyncCheckClick: TIWAsyncCheckClick read FOnAsyncCheckClick write FOnAsyncCheckClick;
    property OnAsyncExit;
    {$ENDIF}
  end;

implementation

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

procedure Draw3DCtrl(FCanvas: TCanvas; Left,Top,Right,Bottom: Integer);
begin
  with FCanvas do
  begin
    Pen.Color := clBlack;
    MoveTo(Left,Bottom);
    LineTo(Left,Top);
    LineTo(Right,Top);

    Pen.Color := clSilver;
    LineTo(Right,Bottom);
    LineTo(Left,Bottom);

    Pen.Color := clGray;
    LineTo(Left + 1,Bottom);
    LineTo(Left + 1,Top + 1);
    LineTo(Right - 1,Top + 1);
    Pen.Color := Brush.Color;
    Rectangle(Left+1,Top+1,Right-1,Bottom-1);
  end;
end;






{ TTIWCheckList }
{$IFDEF TMSIW9}
procedure TTIWCheckList.DoOnAsyncCheckClick(AParams: TStringList);
begin
  if Assigned(OnAsyncCheckClick) then
    OnAsyncCheckClick(Self, AParams, FCheckClick, Selected[FCheckClick]);
end;

function TTIWCheckList.RenderAsync(
  AContext: TIWBaseHTMLComponentContext): TIWXMLTag;
begin
  Result := TIWXMLTag.CreateTag('control');
  try
    Result.AddStringParam('id', HTMLName);
    Result.AddStringParam('type', 'TTIWCHECKLIST');
    RenderAsyncCommonProperties( AContext, Result );

  except
    FreeAndNil(Result);
  end;

end;
{$ENDIF}

constructor TTIWCheckList.Create(AOwner: TComponent);
begin
  inherited;
  FBorderWidth := 1;
  FBorderColor := clBlack;
  FColor := clWindow;
  FFocusColor := clWindow;
  {$IFDEF TMSIW6}
  SetRenderSize(True);
  {$ELSE}
  FRenderSize := True;
  {$ENDIF}
  FNeedsFormTag := True;
  {$IFDEF TMSIW6}
  {TODO}
  {$ELSE}
  FSupportedScriptEvents := 'OnChange,OnSelect';
  FSupportsInput := true;
  FSupportsSubmit := true;
  {$ENDIF}

  FList := TStringList.Create;
  FSelected := TList.Create;
  FPopUpColor := clWhite;
  FPopupColorTo := clNone;
  Text := '';

  FCheckAllBox := false;
  FCheckAllHelp := htNone;
  FCheckAllText := 'Check All';
  FUnCheckAllText := 'UnCheck All';

  Font.FontName := 'Arial';
  Font.Size := 10;
  FDropDownFont := TIWFont.Create;
end;

destructor TTIWCheckList.Destroy;
begin
  FList.Free;
  FSelected.Free;
  FDropDownFont.Free;
  inherited;
end;

procedure TTIWCheckList.SetSelected(i: Integer; value: boolean);
begin
  while (i >= FSelected.Count) do
    FSelected.Add(TObject(false));

  FSelected.Items[i] := TObject(value);
end;

function TTIWCheckList.GetSelected(i: Integer): boolean;
begin
  if (i < FSelected.Count) then
    Result := boolean(FSelected.Items[i])
  else
    Result := False;
end;

{$IFDEF TMSIW6}
procedure TTIWCheckList.IWPaint;
{$ELSE}
procedure TTIWCheckList.Paint;
{$ENDIF}
var
  R: TRect;
begin
  if not FFlat then
  begin
    Canvas.Font.Assign(Self.Font);
    Canvas.Brush.Color := Color;
    Canvas.Pen.Color := BorderColor;
    Canvas.Pen.Width := BorderWidth;
    Draw3DCtrl(Canvas,0, 0, Width - 18, Height - 1);
    R := Rect(2, 2, Width - 2, Height - 2);
  end
  else
  begin
    Canvas.Font.Assign(Self.Font);
    Canvas.Brush.Color := Color;
    Canvas.Pen.Color := BorderColor;
    Canvas.Pen.Width := BorderWidth;
    R := Rect(2, 2, Width - 19, Height - 2);
    Canvas.Rectangle(R);
  end;

  R := rect(width-18,2,width,20);
  {$IFNDEF LINUX}
  DrawFrameControl(Canvas.Handle,r,DFC_SCROLL,DFCS_SCROLLDOWN);
  {$ENDIF}

  R := Rect(2, 2, Width - 20, Height - 2);

  InflateRect(R,-1,-1);
  {$IFNDEF LINUX}
  SetBKMode(Canvas.Handle,TRANSPARENT);
  case Alignment of
  taLeftJustify: DrawText(Canvas.Handle,PChar(Text),Length(Text),R,DT_LEFT);
  taRightJustify: DrawText(Canvas.Handle,PChar(Text),Length(Text),R,DT_RIGHT);
  taCenter: DrawText(Canvas.Handle,PChar(Text),Length(Text),R,DT_CENTER);
  end;
  {$ENDIF}
end;


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

procedure TTIWCheckList.SetValue(const value:string);
var
  s:string;
  i: Integer;
begin
  inherited SetValue(value);
  FSelected.Clear;
  FCheckClick := 1;

  s := ',' + value + ',';

  for  i := 1 to FList.Count do
  begin
    if pos(','+ IntToStr(i - 1) +',',s) > 0 then
      Selected[i - 1] := true;

    if pos(','+ HTMLName +'box' + IntToStr(i - 1) + ',',s) > 0 then
      FCheckClick := (i - 1);
  end;
end;

procedure TTIWCheckList.SetDropDownFont(const Value: TIWFont);
begin
  FDropDownFont.Assign(Value);
end;

{$IFDEF TMSIW6}
function TTIWCheckList.RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag;
{$ELSE}
function TTIWCheckList.RenderHTML: TIWHTMLTag;
{$ENDIF}
var
  htmlres, buttonval: string;
  i: Integer;
  gradient, fontstyle, fontstyleDD: string;
  _sChkAll, _sUnChkAll, _hsChkAll, _hsUnChkAll, caScript, caLabeltext, caHinttext, scrollh: string;
  allchecked: string;
  redscroll: integer;
  checkflag: boolean;
  ajaxcall: string;
                     
   function MakeAlign(AAlignment: TAlignment):string;
   begin
     Result := '';
     case AAlignment of
     taRightJustify: Result := ' style=text-align:right;';
     taCenter: Result := ' style=text-align:center;';
     end;
   end;

   function MakeScript(Name:string): string;
   begin

      {$IFDEF TMSIW9}
      if Assigned(FOnAsyncCheckClick) then
      begin
        ajaxcall := 'if(doasync) processAjaxEvent(''onCheckClick'', '
          + HTMLControlImplementation.IWCLName
          + ',''' + HTMLName + '.' + 'DoOnAsyncCheckClick' + ''','
          + 'true' + ', null, '
          + 'true' + ');';

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

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

        + 'var ' + HTMLName + 'hideWindowedControls = true;'#13
        + 'var ' + HTMLName + 'DivStartLeft;'#13
        + 'var ' + HTMLName + 'DivEndLeft;'#13
        + 'var ' + HTMLName + 'DivStartTop;'#13
        + 'var ' + HTMLName + 'DivEndTop;'#13
        + 'var ' + HTMLName + 'isIE;'#13
        + 'var ' + HTMLName + 'BUTTONTop;'#13
        + 'var ' + HTMLName + 'BUTTONETop;'#13
        + 'var ' + HTMLName + 'BUTTONLeft;'#13
        + 'var ' + HTMLName + 'BUTTONELeft;'#13

      + 'function '+HTMLName+'showWindowedObjects(show,ALeft,ATop,ARight,ABottom) {'#13
      +   HTMLName + 'getPositionDiv();'#13
      + ' var ALeft = '+HTMLName+'DivStartLeft;'#13
      + ' var ATop = '+HTMLName+'DivStartTop;'#13
      + ' var ARight = '+HTMLName+'DivEndLeft;'#13
      + ' var ABottom = '+HTMLName+'DivEndTop;'#13
      + ' isIE = navigator.appName.indexOf("Microsoft") != -1;'#13
      + ' hideWindowedControls = true;'#13
      + ' if (hideWindowedControls) {'#13
      + '   var windowedObjectTags = new Array("SELECT", "IFRAME", "OBJECT","APPLET","EMBED");'#13
      + '   var windowedObjects = new Array();'#13
      + '   var j=0;'#13
      + '   if (isIE == true) {'#13
      + '     for (var i=0; i<windowedObjectTags.length; i++) {'#13
      + '       var tmpTags = document.all.tags(windowedObjectTags[i]);'#13
      + '       if (tmpTags.length > 0) {'#13
      + '         for (var k=0; k<tmpTags.length; k++) {'#13
      + '           windowedObjects[j++] = tmpTags[k];'#13
      + '         }'#13
      + '       }'#13
      + '     }'#13
      + '   }'#13
      + '   else {'#13
      + '     for (var i=0; i<windowedObjectTags.length; i++) {'#13
      + '       var tmpTags = document.getElementsByTagName(windowedObjectTags[i]);'#13
      + '       if (tmpTags.length > 0) {'#13
      + '         for (var k=0; k<tmpTags.length; k++) {'#13
      + '          if (tmpTags[k].style.visibility != "hidden")'#13
      + '           windowedObjects[j++] = tmpTags[k];'#13
      + '         }'#13
      + '       }'#13
      + '     }'#13
      + '   }'#13
      + '   for (var i=0; i<windowedObjects.length; i++) {'#13
      + '     if (show)'#13
      + '      { windowedObjects[i].style.visibility = "visible";'#13
      + '      }'#13
      + '     else {'#13

      + 'if (windowedObjects[i].style.left == "") {'#13
      + '  var cell = windowedObjects[i];'#13
      + '  var leftBegin = 0;'#13
		  + '  var elem = cell;'#13
      + '  do'#13
			+ '    leftBegin += elem.offsetLeft;'#13
      + '  while ((elem = elem.offsetParent));'#13
      + '  leftInt = leftBegin;}'#13
      + 'else {'#13
      + '  var leftObject = document.getElementById(windowedObjects[i].id).style.left;'#13
      + '  var leftInt = parseInt(leftObject.substring(0,leftObject.length - 2));}'#13
      + '  if (windowedObjects[i].style.top == ""){'#13
      + 'var cell = windowedObjects[i];'#13
      + 'var topBegin = 0;'#13
      + 'elem = cell;'#13
      + 'do'#13
      + '  topBegin += elem.offsetTop;'#13
      + 'while ((elem = elem.offsetParent))'#13
      + 'topInt = topBegin;}'#13
      + 'else {'#13
      + '  var topObject = windowedObjects[i].style.top;'#13
      + '  var topInt = parseInt(topObject.substring(0,topObject.length - 2));}'#13
      + '  var widthObject = windowedObjects[i].offsetWidth;'#13
      + '  var heightObject = windowedObjects[i].offsetHeight;'#13
      + '  var rightInt = leftInt + parseInt(widthObject);'#13
      + '  var bottomInt = topInt + parseInt(heightObject);'#13

      + 'var topRegion = 0;'#13
      + 'var leftRegion = 0;'#13
      + ' var elem = windowedObjects[i];'#13
      + ' do {'#13
      + '   if (elem.tagName.toUpperCase() != "BODY") {'#13
      + '     if (elem.id.indexOf("REGION") >= 0) {'#13
      + '       var c = elem;'#13
      + '       do {'#13
      + '         leftRegion += c.offsetLeft;'#13
      + '         topRegion += c.offsetTop;'#13
      + '       }'#13
      + '       while ((c = c.offsetParent))'#13
      + '       break;'#13
      + '     }'#13
      + '  }'#13
      + '} while ((elem = elem.offsetParent))'#13

      + ' leftInt = leftInt + leftRegion;'#13
      + ' topInt = topInt + topRegion;'#13
      + ' bottomInt = bottomInt + topRegion;'#13
      + ' rightInt = rightInt + leftRegion;'#13

      + '  if ((((leftInt >= ALeft) && (leftInt <= ARight)) | ((rightInt >= ALeft)'#13
      + '&& (rightInt <= ARight)) | ((leftInt <= ALeft) && (rightInt >= ALeft)))'#13
      + '&& (((topInt >= ATop) && (topInt <= ABottom)) | ((bottomInt >= ATop) && (bottomInt <= ABottom))))'#13
      + '  windowedObjects[i].style.visibility = "hidden";   }'#13
      + '   }'#13
      + ' }'#13
      + '}'#13

				+ ' function '+HTMLName+'getPositionDiv() {'#13
				+ '   var divObject = document.getElementById("'+HTMLName+'checklist");'#13
				+ '   var caledit = document.getElementById("'+HTMLName+'ED");'#13
				+ '   '#13
				+ '   var cell = divObject;'#13
				+ '   var leftBegin = 0;'#13
				+ '   var topBegin = 0;'#13        
				+ '   var elem = cell;'#13
				+ '   do{'#13
				+ '     leftBegin += elem.offsetLeft;'#13
        + '     topBegin += elem.offsetTop;'#13
				+ '   }'#13
				+ '   while ((elem = elem.offsetParent));'#13
				+ ' '#13
				+ '   '+HTMLName+'DivStartLeft = leftBegin;'#13
				+ '   '+HTMLName+'DivStartTop = topBegin + 3 + caledit.offsetHeight;'#13
				+ '   '+HTMLName+'DivEndLeft = '+HTMLName+'DivStartLeft + divObject.offsetWidth;'#13
				+ '   '+HTMLName+'DivEndTop = '+HTMLName+'DivStartTop + divObject.offsetHeight;'#13
  			+ ' }'#13


        + ' var dohide = 0;'
        + 'function '+HTMLName+'showdiv(){ '#13
        +    HTMLName + 'showWindowedObjects(false);'#13
        + ' if (document.getElementById("'+HTMLName+'checklist").style.visibility == "hidden"){'#13
        + ' document.getElementById("'+HTMLName+'checklist").style.visibility = "visible";'#13
        + ' document.onmouseup = '+HTMLName+'docMouseUp;'#13
        + ' }else {'#13
        + '	document.getElementById("'+HTMLName+'checklist").style.visibility = "hidden";'#13
        + ' } '#13
        + '} '#13

        + ' function '+HTMLName+'docMouseUp(){ '#13
        + ' if (dohide == 0) {'#13
        + '  document.getElementById("'+HTMLName+'checklist").style.visibility = "hidden";'#13
        + '  document.onmouseup = "";'#13
        +    HTMLName + 'showWindowedObjects(true);'#13
        + ' } else {'#13
        + ' dohide = 0; '#13
        + ' } '#13
        + '} '#13

        + 'function '+HTMLName+'resetdoc(){'#13
        + ' dohide = 1; '#13
        + '}'#13

        + 'function '+HTMLName+'UpdateList(doasync,ctrl){'#13
        + '	var editor = document.getElementById("'+HTMLName+'ED");'#13
        + '	var input = FindElem("'+HTMLName+'");'#13
        + '	editor.value = "";'#13
        + '	input.value = "";'#13
        + '  for (i=0;i<'+IntToStr(FList.Count)+';i++){'#13
        + '	  boxid = "'+HTMLName+'box" + i;'#13
        + '	  if (document.getElementById(boxid).checked){'#13
        + '	   editor.value += document.getElementById(boxid).value + ", ";'#13
        + '	   input.value += i + ",";'#13
        + '    }'#13
        + '  }'#13
        + '	editor.value = editor.value.substr(0,editor.value.length-2);'#13
        + ' if (ctrl)'#13
        + '  input.value += ctrl.id;'#13
        + ajaxcall
        + '}'#13

        + 'function ' + HTMLName + 'Focus(ctrl) {'#13;

      if FFocusColor <> FColor then
        Result := Result
        + 'ctrl.style.backgroundColor = '#39+HTMLClr(FFocusColor)+#39''#13;
      if FSelectAll then
        Result := Result
        + 'var rNew = ctrl.createTextRange();'#13
        + 'rNew.moveStart('#39'character'#39', 0) ;'#13
        + 'rNew.select();'#13;

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

  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;


begin
  fontstyleDD := '';

  allchecked := 'false';
  checkflag := true;
  for i:=1 to FSelected.Count do
  begin
    if not (Selected[i-1]) then
      checkflag := false;
  end;

  if (FSelected.Count >= FList.Count) and (checkflag) then
    allchecked := 'true';

	_sChkAll := '';
	_sUnChkAll := '';
	_hsChkAll := '';
	_hsUnChkAll := '';
  scrollh := IntToStr(Height);

	if ((CheckAllBox) and (CheckAllHelp = htLabel)) then
	begin
    _sChkAll := CheckAllText;
  	_sUnChkAll := UnCheckAllText;
 	end;

	if ((CheckAllBox) and (CheckAllHelp = htHint)) then
	begin
		_hsChkAll := CheckAllText;
		_hsUnChkAll := UnCheckAllText;
	end;

  		caScript := '<Script Language="JavaScript">'#13
				+ ' var '+HTMLName+'allchecked = '+allchecked+'; '#13
				+ 'function '+HTMLName+'CheckAll(){'#13
				+ ' if ('+HTMLName+'allchecked){ '#13
				+ '   for (var i=0;i<'+IntToStr(Items.Count)+';i++){ '#13
				+ '	    boxid = "'+HTMLName+'box" + i;'#13
				+ '      document.getElementById(boxid).checked = false;  }'#13
				+ '   '+HTMLName+'allchecked = false; '#13
				+ '   document.getElementById("'+HTMLName+'labeltext").innerHTML = "'+_sChkAll+'"; '#13
				+ '   document.getElementById("'+HTMLName+'boxCA").title = "'+_hsChkAll+'"; '#13
				+ '   } else { '#13
				+ '   for (var i=0;i<'+IntToStr(Items.Count)+';i++){ '#13
				+ '	    boxid = "'+HTMLName+'box" + i;'#13
				+ '      document.getElementById(boxid).checked = true; }'#13
				+ '   document.getElementById("'+HTMLName+'labeltext").innerHTML = "'+_sUnChkAll+'"; '#13
				+ '   document.getElementById("'+HTMLName+'boxCA").title = "'+_hsUnChkAll+'"; '#13
				+ '   '+HTMLName+'allchecked = true; '#13
				+ '   } '#13
				+ ' '+HTMLName+'UpdateList(false); '#13
				+ '} '#13
  			+ '</Script>'#13;

  if (PopUpColor <> clNone) and (PopUpColorTo <> clNone) then
    gradient := ' background:'+HTMLClr(FPopUpColor)+';FILTER: progid:DXImageTransform.Microsoft.Gradient(GradientType=' +
        IntToStr(1 - Integer(PopUpColorGradientDirection)) + ', StartColorStr='#39
        + HTMLClr(PopUpColor) + #39', EndColorStr='#39 + HTMLClr(PopUpColorTo) + #39');'
  else
    gradient := ' background:'+HTMLClr(FPopUpColor)+';';

     {$IFNDEF TMSIW9}
     if (Font.CSSStyle = '') then
     {$ENDIF}
     begin
       {$IFDEF TMSIW6}
  //     fontstyle := ' style="' + Font.FontToStringStyle(AContext.WebApplication.Browser) + '"';
       fontstyle := Font.FontToStringStyle(AContext.WebApplication.Browser);
       {$ELSE}
//       fontstyle := ' style="' + Font.FontToStringStyle(WebApplication.Browser) + '"';
       fontstyle := Font.FontToStringStyle(WebApplication.Browser);
       {$ENDIF}
     end;
     {$IFNDEF TMSIW9}
     if (DropDownFont.CSSStyle = '') then
     {$ENDIF}
     begin
       {$IFDEF TMSIW6}
       fontstyleDD := DropDownFont.FontToStringStyle(AContext.WebApplication.Browser);
       {$ELSE}
       fontstyleDD := DropDownFont.FontToStringStyle(WebApplication.Browser);
       {$ENDIF}
     end;
                                                                           

  htmlres :=  caScript + MakeScript(HTMLName);

  htmlres := htmlres
     + '<style>.testmystyle {font-family:"Verdana";font-size:15pt}</style>'#13
     + '<table cellspacing="0" cellpadding="0" border="0" style="border-collapse: collapse"><tr>'#13
     + '  <td rowspan="2">'#13
     + '    <input type="hidden" name="' + HTMLName +'"'
     {$IFNDEF TMSIW9}
     + ' class="'+font.CSSStyle+'"'
     {$ENDIF}
     + ' id="' + HTMLName +'">'
     + '    <input type=' + IIF(FPasswordPrompt, 'password', 'input') + ' name="' + HTMLName +'ED"' + {' class="'+font.CSSStyle+'"}' id="' + HTMLName +'ED"'
     + IIF(MaxLength > 0, ' maxlength=' + IntToStr(MaxLength))
     + ' style="' + fontstyle + ' width:' + inttostr(width-18) + 'px;'
     + IIF(FFlat, ' border-style:solid;border-width:'+inttostr(FBorderWidth)+ 'px;border-color:'+HTMLClr(FBorderColor)+';')
     + IIF((Color <> clWindow) and (Color <> clNone), ' background-color:'+HTMLClr(Color)+';')
     + '"'
     + ' size=' + IntToStr((Width div 7)-5)
     + ' value="' + Text + '"'
     + IIF(ReadOnly, ' readonly', '')
//     + ' style="width:'+inttostr(width-50)+';'
//     + IIF(FFlat, ' border-style:solid;border-width:'+inttostr(FBorderWidth)+ 'px;border-color:'+HTMLClr(FBorderColor)+';')
//     + '"'
     + ' onKeyPress="return false;"'
     + IIF(FAlignment <> taLeftJustify,MakeAlign(FAlignment))
     + ' onFocus="'+HTMLName+'Focus(this)";'
     + iif(FFocusColor <> FColor,' this.style.backgroundColor = '#39+HTMLClr(FColor)+#39';','')
     + '"'#13
     + '>';

  if (AContext.WebApplication.Browser = brIE) then
    buttonval := '<font face="webdings">6</font>'
  else
    buttonval := '...';

  htmlres := htmlres
     + '<BUTTON type="button" onclick="'+HTMLName+'showdiv()"'
     + ' style="width:18px;height:22px;vertical-align:middle;text-align:center;padding:0px;top:-3;position:relative;">'
     + '<DIV style=" font-family:Webdings;font-size:9pt;margin:0px;padding:0px;border:0px;margin-top:-0.2em;margin-right:-0.1em;">'
     + buttonval + '</DIV></BUTTON>'
     + '<br></td></tr></table>';

    htmlres := htmlres
     + '<div  '
     {$IFNDEF TMSIW9}
     + 'class="'+font.CSSStyle+'"'
     {$ENDIF}
     +' onMouseDown="'+HTMLName+'resetdoc();" id="'+HTMLName
     +'checklist" style="zIndex:1000;visibility:hidden; position:absolute; '+gradient
     + 'border-color:#000000;border-width:1px;border-bottom:2 outset;border-right:2 outset;border-style:solid;padding:2px;'
     + IIF(PopUpWidth > 0, ' width:'+IntToStr(PopUpWidth)+'px;', '')
     + IIF(PopUpHeight > 0, ' height:'+IntToStr(PopUpHeight)+'px;overflow:auto;', '')
//     + fontstyle
     + fontstyleDD
     + '">';

 		if ( (CheckAllBox) and (Items.Count > 0)) then
		begin
			redscroll := Height - 20;
			scrollh := IntToStr(redscroll);
		  caLabeltext := '';
			caHinttext := '';
			if (allchecked = 'false') then
			begin
				if (CheckAllHelp = htLabel) then
					caLabeltext := CheckAllText
				else if (CheckAllHelp = htHint) then
					caHinttext := CheckAllText;
			end
			else
			begin
				if (CheckAllHelp = htLabel) then
					caLabeltext := UnCheckAllText
				else if (CheckAllHelp = htHint) then
					caHinttext := UnCheckAllText;
			end;

    	htmlres := htmlres
      + '<div>'
			+ '<input type="checkbox" '+ iif(Editable and not ReadOnly, '', 'disabled') +' id="'+HTMLName+'boxCA" value="checkall" title="'+caHinttext+'"';

			if (allchecked = 'true') then
        htmlres := htmlres + ' checked ';

			htmlres := htmlres +
      '  onClick="'+HTMLName+'CheckAll();" > <span id="'+HTMLName+'labeltext">'+caLabeltext+' </span><br>'
			+ '</div>';
		end;

    for i := 0 to Flist.Count-1 do
    begin
      htmlres := htmlres + '<input type="checkbox" '+ iif(Editable and not ReadOnly, '', 'disabled') +' id="'+HTMLName+'box'+IntToStr(i)+'" value="'
        + FList.Strings[i] + '"'
        + IIF(Selected[i],' CHECKED','')+ ' onMouseDown="'+HTMLName+'resetdoc();" onClick="'+HTMLName+'UpdateList(true,this);"> '+ FList.Strings[i] + '<br>';
    end;

      htmlres := htmlres
     + '</div>'#13;
     //+ ' </td>'#13
     //+ ' </tr></table>';

    if SupportsInput and Required then
    begin
      {$IFDEF TMSIW6}

        {$IFDEF TMSIW9}
        TIWComponent40Context(AContext).AddValidation('getSubmitForm().' + HTMLName + '.value.length==0'
         , Format(RSValidationRequeredField, [FriendlyName]));
        {$ELSE}
        TIWComponent40Context(AContext).AddValidation('GSubmitter.' + HTMLName + '.value.length==0'
         , Format(RSValidationRequeredField, [FriendlyName]));
        {$ENDIF}

      {$ELSE}
      TIWAppForm(Owner).AddValidation('GSubmitter.' + HTMLName + '.value.length==0'
       , Format(RSValidationRequeredField, [FriendlyName]));
      {$ENDIF}
    end;


    {$IFDEF TMSIW6}
    TIWComponent40Context(AContext).AddToInitProc(HTMLName+'UpdateList(false);');
    {$ELSE}
    TIWAppForm(Form).AddToInitProc(HTMLName+'UpdateList(false);');
    {$ENDIF}



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


procedure TTIWCheckList.SetAlignment(const Value: TAlignment);
begin
  FAlignment := Value;
  Invalidate;
end;

procedure TTIWCheckList.SetBorderColor(const Value: TColor);
begin
  FBorderColor := Value;
  Invalidate;
end;

procedure TTIWCheckList.SetBorderWidth(const Value: Integer);
begin
  FBorderWidth := Value;
  Invalidate;
end;

procedure TTIWCheckList.SetColor(const Value: TColor);
begin
  FColor := Value;
  Invalidate;
end;

procedure TTIWCheckList.SetFlat(const Value: Boolean);
begin
  FFlat := Value;
  Invalidate;
end;


procedure TTIWCheckList.SetList(const Value: TStringList);
begin                                                               
  FList.Assign(Value);

end;

{ TTIWCheckListBox }
{$IFDEF TMSIW9}
procedure TTIWCheckListBox.DoOnAsyncCheckClick(AParams: TStringList);
begin
  if Assigned(OnAsyncCheckClick) then
    OnAsyncCheckClick(Self, AParams, FCheckClick, Selected[FCheckClick]);
end;

procedure TTIWCheckListBox.DoOnAsyncCheckAllClick(AParams: TStringList);
var
  allchecked: bool;
  checkflag: bool;
  i: integer;
begin

  allchecked := false;
  checkflag := true;
  for i:=1 to FSelected.Count do
  begin
    if not (Selected[i-1]) then
      checkflag := false;
  end;

  if (FSelected.Count >= FList.Count) and (checkflag) then
    allchecked := true;

  if Assigned(OnAsyncCheckClick) then
    OnAsyncCheckClick(Self, AParams, -1, allchecked);
end;

function TTIWCheckListBox.RenderAsync(
  AContext: TIWBaseHTMLComponentContext): TIWXMLTag;
var
  i: integer;
  htmlres: string;
begin
  Result := TIWXMLTag.CreateTag('control');
  try
    Result.AddStringParam('id', HTMLName);
    Result.AddStringParam('type', 'TTIWCHECKLISTBOX');
    RenderAsyncCommonProperties( AContext, Result );

    htmlres := '';
    if FList.Count > 0 then
    begin
      htmlres := htmlres
      + 'var myel = document.getElementById("' + HTMLName + 'checklist");'#13
      + 'if (myel){'#13
      + '  myel.innerHTML = "";'#13
      ;

      for i := 0 to Flist.Count-1 do
      begin
        htmlres := htmlres + 'myel.innerHTML += '
          + '"<input type=\"checkbox\" '+ iif(Editable, '', 'disabled')
          +' id=\"'+HTMLName+'box'+IntToStr(i)+'\" value=\"' + FList.Strings[i] + '\"'
          + IIF(Selected[i],'CHECKED','')+ ' onClick=\"'+HTMLName+'UpdateList(true,this,' + IntToStr(FList.Count) + ');\"> '
          + FList.Strings[i] + '<br>";'#13
          ;
      end;

      htmlres := htmlres
      + '}'#13;

      GGetWebApplicationThreadVar.CallBackResponse.AddJavaScriptToExecute('<![CDATA[' + htmlres + ']]>');

    end;

  except
    FreeAndNil(Result);
  end;

end;
{$ENDIF}

constructor TTIWCheckListBox.Create(AOwner: TComponent);
begin
  inherited;
  FBorderWidth := 1;
  FBorderColor := clBlack;
  {$IFDEF TMSIW6}
  SetRenderSize(True);
  {$ELSE}
  FRenderSize := True;
  {$ENDIF}
  FNeedsFormTag := True;
  {$IFDEF TMSIW6}
  {TODO}
  {$ELSE}
  FSupportedScriptEvents := 'OnChange,OnSelect';
  FSupportsInput := true;
  FSupportsSubmit := true;
  {$ENDIF}

  FList := TStringList.Create;
  FSelected := TList.Create;
  FBGColor := clWhite;
  FBGColorTo := clNone;
  Text := '';

  FCheckAllBox := false;
  FCheckAllHelp := htNone;
  FCheckAllText := 'Check All';
  FUnCheckAllText := 'UnCheck All';

  Font.FontName := 'Arial';
  Font.Size := 10;
end;

destructor TTIWCheckListBox.Destroy;
begin
  FList.Free;
  FSelected.Free;
  inherited;
end;

procedure TTIWCheckListBox.SetSelected(i: Integer; value: boolean);
begin
  while (i >= FSelected.Count) do
    FSelected.Add(TObject(false));

  FSelected.Items[i] := TObject(value);
end;

function TTIWCheckListBox.GetSelected(i: Integer): boolean;
begin
  if (i < FSelected.Count) then
    Result := boolean(FSelected.Items[i])
  else
    Result := False;
end;

{$IFDEF TMSIW6}
procedure TTIWCheckListBox.IWPaint;
{$ELSE}
procedure TTIWCheckListBox.Paint;
{$ENDIF}
var
  R: TRect;
begin
    Canvas.Font.Assign(Self.Font);
    Canvas.Brush.Color := clWhite;
    Canvas.Pen.Color := BorderColor;
    Canvas.Pen.Width := BorderWidth;
    Draw3DCtrl(Canvas,0, 0, Width - 1, Height - 1);
    R := Rect(2, 2, Width - 2, Height - 2);


  InflateRect(R,-1,-1);
  {$IFNDEF LINUX}
  SetBKMode(Canvas.Handle,TRANSPARENT);
DrawText(Canvas.Handle,PChar(Text),Length(Text),R,DT_LEFT);

  {$ENDIF}
end;


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

procedure TTIWCheckListBox.SetValue(const value:string);
var
  s:string;
  i: Integer;
begin
  inherited SetValue(value);
  FSelected.Clear;
  FCheckClick := 1;

  s := ',' + value + ',';

  for  i := 1 to FList.Count do
  begin
    if pos(','+ IntToStr(i - 1) +',',s) > 0 then
      Selected[i - 1] := true;

    if pos(','+ HTMLName +'box' + IntToStr(i - 1) + ',',s) > 0 then
      FCheckClick := (i - 1);
  end;
end;

{$IFDEF TMSIW6}
function TTIWCheckListBox.RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag;
{$ELSE}
function TTIWCheckListBox.RenderHTML: TIWHTMLTag;
{$ENDIF}
var
  htmlres: string;
  i: Integer;
  gradient, fontstyle: string;
  _sChkAll, _sUnChkAll, _hsChkAll, _hsUnChkAll, caScript, caLabeltext, caHinttext, scrollh: string;
  allchecked: string;
  redscroll: integer;
  checkflag: boolean;
  ajaxcall: string;

   function MakeAlign(AAlignment: TAlignment):string;
   begin
     Result := '';
     case AAlignment of
     taRightJustify: Result := ' style=text-align:right;';
     taCenter: Result := ' style=text-align:center;';
     end;
   end;

   function MakeScript(Name:string): string;
   begin

      {$IFDEF TMSIW9}
      //make sure SetValue is called on check click

      //if Assigned(FOnAsyncCheckClick) then
      //begin
        ajaxcall := 'if(doasync) processAjaxEvent(''onCheckClick'', '
          + HTMLControlImplementation.IWCLName
          + ',''' + HTMLName + '.' + 'DoOnAsyncCheckClick' + ''','
          + 'true' + ', null, '
          + 'true' + ');';

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

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

        + 'function '+HTMLName+'UpdateList(doasync,ctrl,cbCount){'#13
        + '	var editor = document.getElementById("'+HTMLName+'ED");'#13
        + '	editor.value = "";'#13
        + ' var listcount = '+IntToStr(FList.Count)+';'#13
        + ' if (cbCount)'#13
        + '   listcount = cbCount;'#13
        + '  for (i=0;i<listcount;i++){'#13
        + '	  boxid = "'+HTMLName+'box" + i;'#13
        + '	  if (document.getElementById(boxid).checked)'#13
//        + '	   editor.value += document.getElementById(boxid).value + ", ";'#13
        + '	   editor.value += i + ",";'#13
        + '  }'#13
//        + '	editor.value = editor.value.substr(0,editor.value.length-2);'#13
        + ' if (ctrl)'#13
        + '  editor.value += ctrl.id;'#13
        + ajaxcall
        + '}'#13
        + '</Script>';
  end;

  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;


begin

  allchecked := 'false';
  checkflag := true;
  for i:=1 to FSelected.Count do
  begin
    if not (Selected[i-1]) then
      checkflag := false;
  end;

  if (FSelected.Count >= FList.Count) and (checkflag) then
    allchecked := 'true';

	_sChkAll := '';
	_sUnChkAll := '';
	_hsChkAll := '';
	_hsUnChkAll := '';
  scrollh := IntToStr(Height);

	if ((CheckAllBox) and (CheckAllHelp = htLabel)) then
	begin
    _sChkAll := CheckAllText;
  	_sUnChkAll := UnCheckAllText;
 	end;

	if ((CheckAllBox) and (CheckAllHelp = htHint)) then
	begin
		_hsChkAll := CheckAllText;
		_hsUnChkAll := UnCheckAllText;
	end;

      {$IFDEF TMSIW9}  
      //if Assigned(FOnAsyncCheckClick) then
      //begin
        ajaxcall := ' processAjaxEvent(''onCheckClick'', '
          + HTMLControlImplementation.IWCLName
          + ',''' + HTMLName + '.' + 'DoOnAsyncCheckAllClick' + ''','
          + 'true' + ', null, '
          + 'true' + ');';

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

  		caScript := '<Script Language="JavaScript">'#13
				+ ' var '+HTMLName+'allchecked = '+allchecked+'; '#13
				+ 'function '+HTMLName+'CheckAll(){'#13
				+ ' if ('+HTMLName+'allchecked){ '#13
				+ '   for (var i=0;i<'+IntToStr(Items.Count)+';i++){ '#13
				+ '	    boxid = "'+HTMLName+'box" + i;'#13
				+ '      document.getElementById(boxid).checked = false;  }'#13
				+ '   '+HTMLName+'allchecked = false; '#13
				+ '   document.getElementById("'+HTMLName+'labeltext").innerHTML = "'+_sChkAll+'"; '#13
				+ '   document.getElementById("'+HTMLName+'boxCA").title = "'+_hsChkAll+'"; '#13
				+ '   } else { '#13
				+ '   for (var i=0;i<'+IntToStr(Items.Count)+';i++){ '#13
				+ '	    boxid = "'+HTMLName+'box" + i;'#13
				+ '      document.getElementById(boxid).checked = true; }'#13
				+ '   document.getElementById("'+HTMLName+'labeltext").innerHTML = "'+_sUnChkAll+'"; '#13
				+ '   document.getElementById("'+HTMLName+'boxCA").title = "'+_hsUnChkAll+'"; '#13
				+ '   '+HTMLName+'allchecked = true; '#13
				+ '   } '#13
				+ ' '+HTMLName+'UpdateList(false); '#13
        + ajaxcall        
				+ '} '#13
  			+ '</Script>'#13;

  if (BGColor <> clNone) and (BGColorTo <> clNone) then
    gradient := ' background:'+HTMLClr(FBGColor)+';FILTER: progid:DXImageTransform.Microsoft.Gradient(GradientType=' +
        IntToStr(1 - Integer(BGColorGradientDirection)) + ', StartColorStr='#39
        + HTMLClr(BGColor) + #39', EndColorStr='#39 + HTMLClr(BGColorTo) + #39');'
  else
    gradient := ' background:'+HTMLClr(FBGColor)+';';

     {$IFNDEF TMSIW9}
     if (Font.CSSStyle = '') then
     {$ENDIF}
     begin
       {$IFDEF TMSIW6}
       fontstyle := ' ' + Font.FontToStringStyle(AContext.WebApplication.Browser) + ';';
       {$ELSE}
       fontstyle := ' ' + Font.FontToStringStyle(WebApplication.Browser) + ';';
       {$ENDIF}
     end;

  htmlres :=  caScript + MakeScript(HTMLName);

		if ( (CheckAllBox) and (Items.Count > 0)) then
		begin
			redscroll := Height - 20;
			scrollh := IntToStr(redscroll);
		  caLabeltext := '';
			caHinttext := '';
			if (allchecked = 'false') then
			begin
				if (CheckAllHelp = htLabel) then
					caLabeltext := CheckAllText
				else if (CheckAllHelp = htHint) then
					caHinttext := CheckAllText;
			end
			else
			begin
				if (CheckAllHelp = htLabel) then
					caLabeltext := UnCheckAllText
				else if (CheckAllHelp = htHint) then
					caHinttext := UnCheckAllText;
			end;
                                                                                                               
    	htmlres := htmlres
      + '<div>'
			+ '<input tabindex="' + IntToStr(TabOrder) + '" type="checkbox" '+ iif(Editable and not ReadOnly, '', 'disabled')
      +' id="'+HTMLName+'boxCA" value="checkall" title="'+caHinttext+'"';

			if (allchecked = 'true') then
        htmlres := htmlres + ' checked ';

			htmlres := htmlres +
      '  onClick="'+HTMLName+'CheckAll();" > <span id="'+HTMLName+'labeltext">'+caLabeltext+' </span><br>'
			+ '</div>';
		end;

  htmlres := htmlres
     + '<style>.testmystyle {font-family:"Verdana";font-size:15pt}</style>'#13
     + '    <input type="hidden" name="' + HTMLName +'"'
     {$IFNDEF TMSIW9}
     + ' class="'+font.CSSStyle+'"'
     {$ENDIF}
     +' id="' + HTMLName +'ED"'
     + ' value="' + Text + '"'
     + '>'#13
     + '<div '
     {$IFNDEF TMSIW9}
     + 'class="'+font.CSSStyle+'"'
     {$ENDIF}
     + ' id="'+HTMLName + 'checklist"'
     //changed when set explicitly as visible, the control stays visible when inside a region that's invisible
//     + ' style="visibility:visible; position:absolute; '+gradient
     + ' style=" position:absolute; '+gradient
     + 'border-color:'+HTMLClr(BorderColor)+';border-width:'+IntToStr(BorderWidth)+';border-style:solid;padding:2px;'
     + ' width:'+IntToStr(Width)+'px;'
     + ' height:'+IntToStr(Height)+'px;overflow:auto;'
     + fontstyle
     + '">';

  for i := 0 to Flist.Count-1 do
  begin
    htmlres := htmlres + '<input tabindex="' + IntToStr(TabOrder) + '" type="checkbox" '+ iif(Editable and not ReadOnly, '', 'disabled')
      +' id="'+HTMLName+'box'+IntToStr(i)+'" value="'
      + FList.Strings[i] + '"'
      + IIF(Selected[i],'CHECKED','')+ ' onClick="'+HTMLName+'UpdateList(true,this);"> '+ FList.Strings[i] + '<br>'#13;
  end;

  htmlres := htmlres
    + '</div>';


  {$IFDEF TMSIW6}
  TIWComponent40Context(AContext).AddToInitProc(HTMLName+'UpdateList(false);');
  {$ELSE}
  TIWAppForm(Form).AddToInitProc(HTMLName+'UpdateList(false);');
  {$ENDIF}   


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


procedure TTIWCheckListBox.SetBorderColor(const Value: TColor);
begin
  FBorderColor := Value;
  Invalidate;
end;

procedure TTIWCheckListBox.SetBorderWidth(const Value: Integer);
begin
  FBorderWidth := Value;
  Invalidate;
end;


procedure TTIWCheckListBox.SetList(const Value: TStringList);
begin
  FList.Assign(Value);

end;



end.
