{***************************************************************************}
{ 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 IWTMSEdit;

interface

{$I TMSDEFS.INC}

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

type
  TTIWDateFormat = (dfDayMonthYear, dfMonthDayYear);

  {$IFDEF TMSIW9}
  TIWAsyncClickEvent = procedure (Sender: TObject; EventParams: TStringList; Value: Integer) of object;
  TIWAsyncTimeClickEvent = procedure (Sender: TObject; EventParams: TStringList; Hour, Minutes, Seconds: Integer) of object;
  TIWAsyncDateClickEvent = procedure (Sender: TObject; EventParams: TStringList; Year, Month, Day: Integer) of object;  
  {$ENDIF}

  TTIWAdvSpinEdit = class(TIWCustomEdit)
  private
    FBorderWidth: Integer;
    FColor: TColor;
    FBorderColor: TColor;
    FFlat: Boolean;
    FAlignment: TAlignment;
    FFocusColor: TColor;
    FSelectAll: Boolean;
    FSigned: Boolean;
    FDecimalPoint: char;
    FAdvanceOnReturn: Boolean;
    FSubmitOnReturn: Boolean;
    FMaxValue: Integer;
    FMinValue: Integer;
    FAutoSpin: Boolean;
    FButtonDownImage: TIPicture;
    FButtonDownImageURL: string;
    FButtonUpImage: TIPicture;
    FButtonUpImageURL: string;
    {$IFDEF TMSIW9}
    FOnAsyncUpClick: TIWAsyncClickEvent;
    FOnAsyncDownClick: TIWAsyncClickEvent;
    procedure DoOnAsyncUpClick(AParams: TStringList);
    procedure DoOnAsyncDownClick(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 SetSpinValue(const Value: Integer);
    function GetSpinValue: Integer;
    procedure SetButtonDownImage(const Value: TIPicture);
    procedure SetButtonUpImage(const Value: TIPicture);
  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 AdvanceOnReturn: Boolean read FAdvanceOnReturn write FAdvanceOnReturn;
    property Alignment: TAlignment read FAlignment write SetAlignment;
    property AutoSpin: Boolean read FAutoSpin write FAutoSpin default true;
    property BorderColor: TColor read FBorderColor write SetBorderColor;
    property BorderWidth: Integer read FBorderWidth write SetBorderWidth;
    property ButtonDownImage: TIPicture read FButtonDownImage write SetButtonDownImage;
    property ButtonDownImageURL: string read FButtonDownImageURL write FButtonDownImageURL;
    property ButtonUpImage: TIPicture read FButtonUpImage write SetButtonUpImage;
    property ButtonUpImageURL: string read FButtonUpImageURL write FButtonUpImageURL;
    property Color: TColor read FColor write SetColor;
    property DecimalPoint: char read FDecimalPoint write FDecimalPoint;
    property Enabled;
    property FocusColor: TColor read FFocusColor write FFocusColor;
    property Flat: Boolean read FFlat write SetFlat;
    property MaxValue: Integer read FMaxValue write FMaxValue default 100;
    property MinValue: Integer read FMinValue write FMinValue default 0;
    property SelectAll: Boolean read FSelectAll write FSelectAll;
    property Signed: Boolean read FSigned write FSigned;
    property SubmitOnReturn: Boolean read FSubmitOnReturn write FSubmitOnReturn;
    property Value: Integer read GetSpinValue write SetSpinValue;
    {$IFDEF TMSIW9}
    property OnAsyncUpClick: TIWAsyncClickEvent read FOnAsyncUpClick write FOnAsyncUpClick;
    property OnAsyncDownClick: TIWAsyncClickEvent read FOnAsyncDownClick write FOnAsyncDownClick;
    {$ENDIF}
  end;

  
  TTIWAdvTimeEdit = class(TIWCustomEdit)
   private
    FBorderWidth: Integer;
    FColor: TColor;
    FBorderColor: TColor;
    FFlat: Boolean;
    FAlignment: TAlignment;
    FFocusColor: TColor;
    FSelectAll: Boolean;
    FShowSeconds: Boolean;
    FTimeSeparator: char;
    FAdvanceOnReturn: Boolean;
    FSubmitOnReturn: Boolean;
    FAutoSpin: Boolean;
    FHourVal: Integer;
    FMinVal: Integer;
    FSecVal: Integer;
    FButtonDownImage: TIPicture;
    FButtonDownImageURL: string;
    FButtonUpImage: TIPicture;
    FButtonUpImageURL: string;
    {$IFDEF TMSIW9}
    FOnAsyncUpClick: TIWAsyncTimeClickEvent;
    FOnAsyncDownClick: TIWAsyncTimeClickEvent;
    procedure DoOnAsyncUpClick(AParams: TStringList);
    procedure DoOnAsyncDownClick(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 SetHour(const Value: Integer);
    procedure SetMinutes(const Value: Integer);
    procedure SetSeconds(const Value: Integer);
    procedure SetShowSeconds(const Value: Boolean);
    procedure SetTime(const Value: TDateTime);
    procedure SetButtonDownImage(const Value: TIPicture);
    procedure SetButtonUpImage(const Value: TIPicture);
    function GetTime: TDateTime;
  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;     
    property Time: TDateTime read GetTime write SetTime;
  published
    property AdvanceOnReturn: Boolean read FAdvanceOnReturn write FAdvanceOnReturn;
    property Alignment: TAlignment read FAlignment write SetAlignment;
    property AutoSpin: Boolean read FAutoSpin write FAutoSpin default true;
    property BorderColor: TColor read FBorderColor write SetBorderColor;
    property BorderWidth: Integer read FBorderWidth write SetBorderWidth;
    property ButtonDownImage: TIPicture read FButtonDownImage write SetButtonDownImage;
    property ButtonDownImageURL: string read FButtonDownImageURL write FButtonDownImageURL;
    property ButtonUpImage: TIPicture read FButtonUpImage write SetButtonUpImage;
    property ButtonUpImageURL: string read FButtonUpImageURL write FButtonUpImageURL;
    property Color: TColor read FColor write SetColor;
    property Enabled;    
    property FocusColor: TColor read FFocusColor write FFocusColor;
    property Flat: Boolean read FFlat write SetFlat;
    property SelectAll: Boolean read FSelectAll write FSelectAll;
    property ShowSeconds: Boolean read FShowSeconds write SetShowSeconds;
    property SubmitOnReturn: Boolean read FSubmitOnReturn write FSubmitOnReturn;
    property Hour: Integer read FHourVal write SetHour;
    property Minutes: Integer read FMinVal write SetMinutes;
    property Seconds: Integer read FSecVal write SetSeconds;
    property TimeSeparator: char read FTimeSeparator write FTimeSeparator;
    {$IFDEF TMSIW9}
    property OnAsyncUpClick: TIWAsyncTimeClickEvent read FOnAsyncUpClick write FOnAsyncUpClick;
    property OnAsyncDownClick: TIWAsyncTimeClickEvent read FOnAsyncDownClick write FOnAsyncDownClick;
    {$ENDIF}
  end;

 TTIWAdvDateEdit = class(TIWCustomEdit)
   private
    FBorderWidth: Integer;
    FColor: TColor;
    FBorderColor: TColor;
    FFlat: Boolean;
    FAlignment: TAlignment;
    FFocusColor: TColor;
    FSelectAll: Boolean;
    FDateSeparator: char;
    FAdvanceOnReturn: Boolean;
    FSubmitOnReturn: Boolean;
    FAutoSpin: Boolean;
    FDayVal: Integer;
    FMonthVal: Integer;
    FYearVal: Integer;
    FDateFormat: TTIWDateFormat;
    FButtonDownImage: TIPicture;
    FButtonDownImageURL: string;
    FButtonUpImage: TIPicture;
    FButtonUpImageURL: string;
    {$IFDEF TMSIW9}
    FOnAsyncUpClick: TIWAsyncDateClickEvent;
    FOnAsyncDownClick: TIWAsyncDateClickEvent;
    procedure DoOnAsyncUpClick(AParams: TStringList);
    procedure DoOnAsyncDownClick(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 SetDay(const Value: Integer);
    procedure SetMonth(const Value: Integer);
    procedure SetYear(const Value: Integer);
    procedure SetDate(const Value: TDateTime);
    procedure SetButtonDownImage(const Value: TIPicture);
    procedure SetButtonUpImage(const Value: TIPicture);    
    function GetDate: TDateTime;
  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;       
    property Date: TDateTime read GetDate write SetDate;
  published
    property AdvanceOnReturn: Boolean read FAdvanceOnReturn write FAdvanceOnReturn;
    property Alignment: TAlignment read FAlignment write SetAlignment;
    property AutoSpin: Boolean read FAutoSpin write FAutoSpin default true;
    property BorderColor: TColor read FBorderColor write SetBorderColor;
    property BorderWidth: Integer read FBorderWidth write SetBorderWidth;
    property ButtonDownImage: TIPicture read FButtonDownImage write SetButtonDownImage;
    property ButtonDownImageURL: string read FButtonDownImageURL write FButtonDownImageURL;
    property ButtonUpImage: TIPicture read FButtonUpImage write SetButtonUpImage;
    property ButtonUpImageURL: string read FButtonUpImageURL write FButtonUpImageURL;    
    property Color: TColor read FColor write SetColor;
    property DateSeparator: char read FDateSeparator write FDateSeparator;
    property Enabled;    
    property FocusColor: TColor read FFocusColor write FFocusColor;
    property Flat: Boolean read FFlat write SetFlat;
    property SelectAll: Boolean read FSelectAll write FSelectAll;
    property SubmitOnReturn: Boolean read FSubmitOnReturn write FSubmitOnReturn;
    property Day: Integer read FDayVal write SetDay;
    property Month: Integer read FMonthVal write SetMonth;
    property Year: Integer read FYearVal write SetYear;
    property DateFormat: TTIWDateFormat read FDateFormat write FDateFormat;
    {$IFDEF TMSIW9}
    property OnAsyncUpClick: TIWAsyncDateClickEvent read FOnAsyncUpClick write FOnAsyncUpClick;
    property OnAsyncDownClick: TIWAsyncDateClickEvent read FOnAsyncDownClick write FOnAsyncDownClick;
    {$ENDIF}
  end;

implementation

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

{$IFNDEF DELPHI7_LVL}
function BoolToStr(B: Boolean; UseBoolStrs: Boolean = False): string;
const
  cSimpleBoolStrs: array [boolean] of String = ('0', '-1');
begin
  Result := cSimpleBoolStrs[B];
end;
{$ENDIF}
  

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;


{ TTIWAdvSpinEdit }
{$IFDEF TMSIW9}
procedure TTIWAdvSpinEdit.DoOnAsyncUpClick(AParams: TStringList);
begin
  if Assigned(OnAsyncUpClick) then
    OnAsyncUpClick(Self, AParams, Value);
end;

procedure TTIWAdvSpinEdit.DoOnAsyncDownClick(AParams: TStringList);
begin
  if Assigned(OnAsyncDownClick) then
    OnAsyncDownClick(Self, AParams, Value);
end;

function TTIWAdvSpinEdit.RenderAsync(
  AContext: TIWBaseHTMLComponentContext): TIWXMLTag;
var
  js: string;
begin

  Result := TIWXMLTag.CreateTag('control');
  try
    Result.AddStringParam('id', HTMLName);
    Result.AddStringParam('type', 'TTIWADVSPINEDIT');
    RenderAsyncCommonProperties( AContext, Result );

    js :=
      '<![CDATA['
      + 'document.getElementById("' + HTMLName + 'ED").disabled = ' + iif(enabled,'false','true') + ';'#13
      + HTMLName + 'enabled = ' + BoolToStr(Enabled) +';'#13
      + 'var upIE = document.getElementById("' + HTMLName + 'upbtnIE");'#13
      + 'var dnIE = document.getElementById("' + HTMLName + 'dnbtnIE");'#13
      + 'var upNS = document.getElementById("' + HTMLName + 'upbtnNS");'#13
      + 'var dnNS = document.getElementById("' + HTMLName + 'dnbtnNS");'#13
      + 'if (upIE)'
      + '  upIE.disabled = ' + iif(enabled,'false','true') +';'
      + 'if (dnIE)'
      + '  dnIE.disabled = ' + iif(enabled,'false','true') +';'
      + 'if (upNS)'
      + '  upNS.disabled = ' + iif(enabled,'false','true') +';'
      + 'if (dnNS)'
      + '  dnNS.disabled = ' + iif(enabled,'false','true') +';'
      +']]>';

    GGetWebApplicationThreadVar.CallBackResponse.AddJavaScriptToExecute(js);

  except
    FreeAndNil(Result);
  end;

end;
{$ENDIF}

constructor TTIWAdvSpinEdit.Create(AOwner: TComponent);
begin
  inherited;
  FBorderWidth := 1;
  FBorderColor := clBlack;
  FColor := clWhite;
  FFocusColor := clWhite;
  {$IFDEF TMSIW6}
  SetRenderSize(True);
  {$ELSE}
  FRenderSize := True;
  {$ENDIF}
  FNeedsFormTag := True;
  {$IFDEF TMSIW6}
  {TODO}
  {$ELSE}
  FSupportedScriptEvents := 'OnChange,OnSelect';
  {$ENDIF}
  FDecimalPoint := ',';
  FMaxValue := 100;
  FMinValue := 0;
  FAutoSpin := true;
  Text := '0';
  
  FButtonDownImage := TIPicture.Create;
  FButtonUpImage := TIPicture.Create;
end;

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

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

{$IFDEF TMSIW6}
procedure TTIWAdvSpinEdit.IWPaint;
{$ELSE}
procedure TTIWAdvSpinEdit.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,11);
  {$IFNDEF LINUX}
  DrawFrameControl(Canvas.Handle,r,DFC_SCROLL,DFCS_SCROLLUP);
  {$ENDIF}

  R := rect(width-18,11,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;

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


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

procedure TTIWAdvSpinEdit.SetSpinValue(const Value: Integer);
begin
  Text := IntToStr(value);
end;

destructor TTIWAdvSpinEdit.Destroy;
begin
  FButtonDownImage.Free;
  FButtonUpImage.Free;
  inherited;
end;

function TTIWAdvSpinEdit.GetSpinValue: Integer;
var
  err,i: Integer;
begin
  val(Text, i, err);
  Result := i;
end;

{$IFDEF TMSIW6}
function TTIWAdvSpinEdit.RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag;
{$ELSE}
function TTIWAdvSpinEdit.RenderHTML: TIWHTMLTag;
{$ENDIF}
var
  htmlres, dbtnval, ubtnval: string;
  iwlbl: TIWLabel;
  ajaxup, ajaxdown: string;

   //  Advanced edit control with following types:
   //  TTIWAdvSpinEditType = (etString,etNumeric,etFloat,etUppercase,etMixedCase,etLowerCase,etHex);
   //                    0        1         2       3           4            5          6

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

   function MakeScript(Name:string): string;
   begin

      {$IFDEF TMSIW9}
      if Assigned(FOnAsyncUpClick) then
      begin
        ajaxup := 'processAjaxEvent(''onUpClick'', '
          + HTMLControlImplementation.IWCLName
          + ',''' + HTMLName + '.' + 'DoOnAsyncUpClick' + ''','
          + 'true' + ', null, '
          + 'true' + ');';

        Acontext.WebApplication.RegisterCallBack(HTMLName+'.DoOnAsyncUpClick', DoOnAsyncUpClick);
      end;

      if Assigned(FOnAsyncDownClick) then
      begin
        ajaxdown := 'processAjaxEvent(''onDownClick'', '
          + HTMLControlImplementation.IWCLName
          + ',''' + HTMLName + '.' + 'DoOnAsyncDownClick' + ''','
          + 'true' + ', null, '
          + 'true' + ');';

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


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

        {$IFDEF TMSIW9}
        + 'function AsyncUpdateSE(id)'#13
        + '{'#13
        + '   if (!containsName(id)) {'#13
        + ' 	 window.ChangedControls += id + ",";'#13
        + '   }'#13
        + '}'#13
        {$ENDIF}

        + 'var '+HTMLName + 'decrement;'#13
        + 'var '+HTMLName + 'increment;'#13
        + 'var '+HTMLName + 'enabled = ' + BoolToStr(Enabled) +';'#13

        + 'function '+HTMLName+'UpdateVal(ctrl){'#13
        + '  if (ctrl.value > '+IntToStr(MaxValue)+')'#13
        + '  ctrl.value = '+inttostr(MaxValue)+';'#13
        + '  if (ctrl.value < '+IntToStr(MinValue)+')'#13
        + '  ctrl.value = '+inttostr(MinValue)+';'#13
        + ' ' + HTMLName + 'hi.value = ctrl.value;'#13
        {$IFDEF TMSIW9}
        +	'  AsyncUpdateSE("'+HTMLName+'");'#13
        {$ENDIF}
        + '}'#13

        + 'function ' + HTMLName + 'HandleMin(ctrl)'#13
        + ' {'#13
        + '  s = ctrl.value'#13
        + '  if (s.indexOf("-") == -1)'#13
        + '    ctrl.value = "-" + s;'#13
        + '  else'#13
        + '    ctrl.value = s.substring(1);'#13
        + ' ' + HTMLName + 'hi.value = ctrl.value;'#13
        + '}'#13

        + 'function ' + HTMLName + 'StartSpinUp(el)'#13
        + ' {'#13
        + '  if (el.value < '+IntToStr(MaxValue)+')'#13
        + '   el.value = parseInt(el.value) + 1;'#13
        + ' ' + HTMLName + 'hi.value = el.value;'#13
        + ajaxup
        + '}'#13

        + 'function ' + HTMLName + 'StartSpinDown(el)'#13
        + ' {'#13
        + '  if (el.value > '+IntToStr(MinValue)+')'#13
        + '   el.value = parseInt(el.value) - 1;'#13
        + ' ' + HTMLName + 'hi.value = el.value;'#13
        + ajaxdown
        + '}'#13

        + ' function '+HTMLName+'StopSpin()'#13
        + ' {'#13
        + ' clearInterval('+HTMLName+'increment);'#13
        + ' clearInterval('+HTMLName+'decrement);'#13
        + '}'#13

        + 'function '+HTMLName+'ClickProc(editctrl,ctrl) {'#13
        + ' if (' + HTMLName +'enabled != -1) return false;'#13
        + ' el = document.getElementById("'+HTMLName+'ED")'#13
        + ' if (el.value == "")'#13
        + '   el.value = '+IntToStr(MinValue) + #13
        + ' if (ctrl==1){'#13
        + ' '+HTMLName+'StartSpinUp(el)'#13
        + IIF (AutoSpin, HTMLName+'increment = setInterval("'+HTMLName+'StartSpinUp(el)",200);', '')
        + '  } else{'#13
        + ' '+HTMLName+'StartSpinDown(el)'#13
        + IIF (AutoSpin, HTMLName+'decrement = setInterval("'+HTMLName+'StartSpinDown(el)",200);', '')
         + '  }'#13
        + ' el.focus();'#13
        + '}'#13

        + 'var isIE = navigator.appName.indexOf("Microsoft") != -1;'#13        

        + 'function ' + HTMLName + 'KeyPress(ctrl,edittype,e) '#13
        + '{'#13
        + 'var key;'#13
        + 'if (isIE) key = event.keyCode; else key = e.which;'#13
        + ' if ((key == 13) && '+IIF(FAdvanceOnReturn,'(1>0))','(1<0))')+'{'#13
        + ' var i;'#13
        + 'for (i = 0; i < ctrl.form.elements.length; i++)'#13
        + ' if (ctrl == ctrl.form.elements[i])'#13
        + '  break;'#13
        + ' i = (i + 1) % ctrl.form.elements.length;'#13

        + '  ctrl.form.elements[i].focus();'#13
        + '    return false;}'#13
        + ' if ((key == 13) && '+IIF(FSubmitOnReturn,'(1>0))','(1<0))')+'{'#13
        + ' ' + HTMLName + 'hi.value = ctrl.value;'#13
        + '   return SubmitClick('+#39+HTMLName+#39','+'ctrl.value,'+#39'false'#39');'+#13
        + '}'#13
        + ' if (edittype == 1) '#13
        + '  { '#13
        + '  if (key < 48 || key > 57) {'#13
        + '    if (isIE)'#13
        + '      event.returnValue = false;'#13
        + '    else'#13
        + '      return false;'#13
        + '  }'#13
        + IIF(Signed,'  if (key == 45) '+HTMLName+'HandleMin(ctrl)'#13,'')
        + '  } '#13
        + ' if (edittype == 2) '#13
        + '  { '#13
        + '  if ((key < 48 || key > 57) && (key != '+IntToStr(Ord(FDecimalPoint))+')) event.returnValue = false;'#13
        + '  if (key == '+IntToStr(Ord(FDecimalPoint))+')'#13
        + '    if (ctrl.value.indexOf("'+FDecimalPoint+'") != -1) event.returnValue = false;'#13
        + IIF(Signed,'  if (key == 45) '+HTMLName+'HandleMin(ctrl)'#13,'')
        + '  } '#13
        + ' ' + HTMLName + 'hi.value = ctrl.value;'#13
        + '}'#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
  if Editable then
  begin

  if FButtonUpImageURL <> EmptyStr then
    begin
      ubtnval := '<img src="' + FButtonUpImageURL + '" '
        + iif(enabled,' onMouseUp="'+HTMLName+'StopSpin();" onMouseOut="'
        + HTMLName+'StopSpin();" onMouseDown="'+HTMLName+'ClickProc('+HTMLName+',1);"', '')
        + iif(AContext.WebApplication.Browser = brIE, ' style="position:relative;top:-4px;"', 'style="position:relative;top:-1px;"')
        + '>';
    end
    else if FButtonUpImage.Empty then
    begin

      if (AContext.WebApplication.Browser = brIE) then
      begin
       ubtnval := ubtnval
       + '<BUTTON id="'+HTMLName+'upbtnIE" '+iif(enabled,'',' DISABLED')+ ' onMouseUp="'+HTMLName+'StopSpin();" onMouseOut="'+HTMLName+'StopSpin();" onMouseDown="'+HTMLName+'ClickProc('+HTMLName
       +',1);" style="display:;width:18px;height:14px;vertical-align:middle;text-align:center;padding:0px;top:-4px;position:relative;">'
       + ' <DIV style=" font-family:Webdings;font-size:9pt;margin:0px;padding:0px;border:0px;margin-top:-0.4em;margin-right:-0.1em;">'
       + ' 5</DIV></BUTTON>'#13;
      end
      else
      begin
       ubtnval := ubtnval
       + '<BUTTON id="'+HTMLName+'upbtnNS" '+iif(enabled,'',' DISABLED')+ ' onMouseUp="'+HTMLName+'StopSpin();" onMouseOut="'+HTMLName+'StopSpin();" onMouseDown="'+HTMLName+'ClickProc('+
       HTMLName+',1);return false;" style="width:18px;height:14px;vertical-align:middle;text-align:center;padding:0px;top:-1px;position:relative;">'
       +' <DIV style="font-size:9pt;margin:-2px;padding:-1px;position:relative;top:-5;border:0px;margin-top:-0em;margin-right:0em;">'
       +'+</DIV></BUTTON>'#13;
      end;

    end
    else
    begin

      if FButtonUpImage.IsGIF then
      {$IFDEF TMSIW71}
        ubtnval := TIWServerControllerBase.NewCacheFile('gif', true)
      {$ELSE}
        ubtnval := TIWServerControllerBase.NewCacheFile('gif')
      {$ENDIF}
      else
      {$IFDEF TMSIW71}
        ubtnval := TIWServerControllerBase.NewCacheFile('jpg', true);
      {$ELSE}
        ubtnval := TIWServerControllerBase.NewCacheFile('jpg');
      {$ENDIF}

      FButtonUpImage.SaveToFile(ubtnval);

      ubtnval := '<img src="' + GetCacheDir(Self) + ExtractFilename(ubtnval)+ '" '
        +iif(enabled,' onMouseUp="'+HTMLName+'StopSpin();" onMouseOut="'
        +HTMLName+'StopSpin();" onMouseDown="'+HTMLName+'ClickProc('+HTMLName+',1);"', '')
        + iif(AContext.WebApplication.Browser = brIE, ' style="position:relative;top:-4px;"', 'style="position:relative;top:-1px;"')        
        + '>';

    end;

    if FButtonDownImageURL <> EmptyStr then
    begin
      dbtnval := '<img src="' + FButtonDownImageURL + '" '
        + iif(enabled,' onMouseUp="'+HTMLName+'StopSpin();" onMouseOut="'
        + HTMLName+'StopSpin();" onMouseDown="'+HTMLName+'ClickProc('+HTMLName+',2);"', '')
        + iif(AContext.WebApplication.Browser = brIE, ' style="position:relative;top:-13px;"', 'style="position:relative;top:-1px;"')        
        + '>';
    end
    else if FButtonDownImage.Empty then
    begin

      if (AContext.WebApplication.Browser = brIE) then
      begin
       dbtnval := dbtnval
       + ' <BUTTON id="'+HTMLName+'dnbtnIE" '+iif(enabled,'',' DISABLED')+ ' onMouseUp="'+HTMLName+'StopSpin();" onMouseOut="'+HTMLName+'StopSpin();" onMouseDown="'+HTMLName+'ClickProc('+HTMLName+
       ',2);" style="display:;width:18px;height:14px;vertical-align:middle;text-align:center;padding:0px;top:-11px;position:relative;">'
       + ' <DIV style=" font-family:Webdings;font-size:9pt;margin:0px;padding:0px;border:0px;margin-top:-0.4em;margin-right:-0.1em;">'
       + ' 6</DIV></BUTTON>'#13;
      end
      else
      begin
       dbtnval := dbtnval
       + '<BUTTON id="'+HTMLName+'dnbtnNS" '+iif(enabled,'',' DISABLED')+ ' onMouseUp="'+HTMLName+'StopSpin();" onMouseOut="'+HTMLName+'StopSpin();" onMouseDown="'+HTMLName+'ClickProc('
       +HTMLName+',2);return false;" style="width:18px;height:14px;vertical-align:middle;text-align:center;padding:0px;top:-5px;position:relative;">'
       +' <DIV style="font-size:9pt;margin:-2px;padding:-5px;position:relative;top:-1;border:0px;margin-top:-0.4em;margin-right:0em;">'
       +'-</DIV></BUTTON>'#13;
      end;

    end
    else
    begin

      if FButtonDownImage.IsGIF then
      {$IFDEF TMSIW71}
        dbtnval := TIWServerControllerBase.NewCacheFile('gif', true)
      {$ELSE}
        dbtnval := TIWServerControllerBase.NewCacheFile('gif')
      {$ENDIF}
      else
      {$IFDEF TMSIW71}
        dbtnval := TIWServerControllerBase.NewCacheFile('jpg', true);
      {$ELSE}
        dbtnval := TIWServerControllerBase.NewCacheFile('jpg');
      {$ENDIF}

      FButtonDownImage.SaveToFile(dbtnval);

      dbtnval := '<img src="' + GetCacheDir(Self) + ExtractFilename(dbtnval)+ '" '
        +iif(enabled,' onMouseUp="'+HTMLName+'StopSpin();" onMouseOut="'
        +HTMLName+'StopSpin();" onMouseDown="'+HTMLName+'ClickProc('+HTMLName+',2);"', '')
        + iif(AContext.WebApplication.Browser = brIE, ' style="position:relative;top:-13px;"', 'style="position:relative;top:-1px;"')        
        + '>';

    end;

    htmlres :=
     MakeScript(HTMLName)
     + '<table cellspacing="0" cellpadding="0" border="0" style="border-collapse: collapse"><tr>'#13
     + '  <td rowspan="2" valign="top">'#13
     + '    <input type="hidden" name="' + HTMLName +'"' + ' id="' + HTMLName +'_INPUT" value="' + Text + '"><input type=' + IIF(FPasswordPrompt, 'password', 'text') + iif(Enabled,' ',' DISABLED')+' name="' + HTMLName +'ED"' + ' id="' + HTMLName +'ED"'
     + IIF(MaxLength > 0, ' maxlength=' + IntToStr(MaxLength))
     {$IFDEF TMSIW6}
     + ' style="' + Font.FontToStringStyle(AContext.WebApplication.Browser) + ';'
     {$ELSE}
     + ' style="' + Font.FontToStringStyle(WebApplication.Browser) + ';'
     {$ENDIF}
     + 'position:relative;top:-1px;width:'+inttostr(width-18)+';height:'+IntToStr(Height)+'px;'
     + IIF((FColor <> clWindow ) and (FColor <> clWhite), ' background-color:'+HTMLClr(FColor)+';')
     + IIF(FFlat, ' width:'+inttostr(width-18)+';border-style:solid;border-width:'+inttostr(FBorderWidth)+ 'px;border-color:'+HTMLClr(FBorderColor)+';')
     + IIF(FAlignment <> taLeftJustify,MakeAlign(FAlignment))     
     + '"'
//     + ' size=' + IntToStr(Width div 7)
     + ' value="' + Text + '"'
     + IIF(ReadOnly, ' readonly', '')
     {$IFDEF TMSIW9}
     + IIF((Css <> EmptyStr),' class="' + Css + '"','')
     {$ENDIF}
     + ' onKeyPress="return '+HTMLName+'KeyPress('+HTMLName+','+IntTostr(1)+',event);"'
     + ' onFocus="'+HTMLName+'Focus(this);";'
     + ' onBlur="'+HTMLName+'UpdateVal(this);'
     + iif(FFocusColor <> FColor,' this.style.backgroundColor = '#39+HTMLClr(FColor)+#39';','')
     + '"'#13
     + '></td>'#13
     + '  <td valign="bottom">'#13;

     htmlres := htmlres + ubtnval;

     htmlres := htmlres
     + ' </td>'#13
     + '  </tr>'#13
     + '  <tr>'#13
     + '  <td valign="top">'#13;

     htmlres := htmlres + dbtnval;

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

     htmlres := htmlres
		 + '<Script>'
     + 'var '+HTMLName + 'hi = document.getElementById("'+HTMLName+'_INPUT")'#13     
		 + 'var isIE = navigator.appName.indexOf("Microsoft") != -1;'
//		 + '  if (! isIE){'
		 //+ '    document.getElementById("'+HTMLName+'upbtnIE").style.display=''none'';'
		 //+ '    document.getElementById("'+HTMLName+'dnbtnIE").style.display=''none'';'
		 //+ '    document.getElementById("'+HTMLName+'upbtnNS").style.display=''inline'';'
		 //+ '    document.getElementById("'+HTMLName+'dnbtnNS").style.display=''inline'';'
		 //+ '    document.getElementById("'+HTMLName+'ED").style.top=''0px'';'
//		 + '  } '
		 + '</Script>'#13;

    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;
  end
  else
  begin
    iwlbl := TIWLabel.Create(Owner);
    with iwlbl do
    try
      Caption := Self.Text;
      Font.Assign(Self.Font);
      {$IFDEF TMSIW6}
      htmlres := GetControlHTML(iwlbl,AContext);
      {$ELSE}
      htmlres := GetControlHTML(iwlbl);
      {$ENDIF}
    finally
      Free;
    end;
  end;

  Result := TIWHTMLTag.CreateTag('DIV');
  //if css class is defined remove it from the outer div.
  //the class should only be set on the input tag
  {$IFDEF TMSIW9}
  if Css <> EmptyStr then
    Result.AddStringParam('class', ' ');
  {$ENDIF}
  
  Result.Contents.AddText(htmlres);
end;


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

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

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

procedure TTIWAdvSpinEdit.SetButtonDownImage(const Value: TIPicture);
begin
  FButtonDownImage.Assign(Value);
end;

procedure TTIWAdvSpinEdit.SetButtonUpImage(const Value: TIPicture);
begin
  FButtonUpImage.Assign(Value);
end;

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

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


{ TTIWAdvTimeEdit }
{$IFDEF TMSIW9}
procedure TTIWAdvTimeEdit.DoOnAsyncUpClick(AParams: TStringList);
begin
  if Assigned(OnAsyncUpClick) then
    OnAsyncUpClick(Self, AParams, Hour, Minutes, Seconds);
end;

procedure TTIWAdvTimeEdit.DoOnAsyncDownClick(AParams: TStringList);
begin
  if Assigned(OnAsyncDownClick) then
    OnAsyncDownClick(Self, AParams, Hour, Minutes, Seconds);
end;

function TTIWAdvTimeEdit.RenderAsync(
  AContext: TIWBaseHTMLComponentContext): TIWXMLTag;
var
  js: string;
begin
  Result := TIWXMLTag.CreateTag('control');
  try
    Result.AddStringParam('id', HTMLName);
    Result.AddStringParam('type', 'TTIWADVTIMEEDIT');
    RenderAsyncCommonProperties( AContext, Result );

    js :=
      '<![CDATA['
      + 'document.getElementById("' + HTMLName + 'ED").disabled = ' + iif(enabled,'false','true') + ';'#13
      + HTMLName + 'enabled = ' + BoolToStr(Enabled) +';'#13
      + 'document.getElementById("' + HTMLName + 'EDMIN").disabled = ' + iif(enabled,'false','true') + ';'#13
      + HTMLName + 'enabled = ' + BoolToStr(Enabled) +';'#13
      + 'el = document.getElementById("'+ HTMLName + 'EDSEC"); if (el) el.disabled = ' + iif(enabled,'false','true') +';'#13
      + 'var upIE = document.getElementById("' + HTMLName + 'upbtnIE");'#13
      + 'var dnIE = document.getElementById("' + HTMLName + 'dnbtnIE");'#13
      + 'var upNS = document.getElementById("' + HTMLName + 'upbtnNS");'#13
      + 'var dnNS = document.getElementById("' + HTMLName + 'dnbtnNS");'#13
      + 'if (upIE)'
      + '  upIE.disabled = ' + iif(enabled,'false','true') +';'
      + 'if (dnIE)'
      + '  dnIE.disabled = ' + iif(enabled,'false','true') +';'
      + 'if (upNS)'
      + '  upNS.disabled = ' + iif(enabled,'false','true') +';'
      + 'if (dnNS)'
      + '  dnNS.disabled = ' + iif(enabled,'false','true') +';'
      +']]>';

    GGetWebApplicationThreadVar.CallBackResponse.AddJavaScriptToExecute(js);

  except
    FreeAndNil(Result);
  end;

end;
{$ENDIF}

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

  FButtonDownImage := TIPicture.Create;
  FButtonUpImage := TIPicture.Create;

  {$IFNDEF TMSIW6}
  FSupportsSubmit := true;
  FSupportsInput := True;
  {$ENDIF}
end;

procedure TTIWAdvTimeEdit.SetHour(const Value: Integer);
begin
  if (Value >= 0) and (Value <24) then
     FHourVal := value;
  Invalidate;
end;

procedure TTIWAdvTimeEdit.SetMinutes(const Value: Integer);
begin
  if (Value >= 0) and (Value <60) then
     FMinVal := value;
  Invalidate;
end;

procedure TTIWAdvTimeEdit.SetSeconds(const Value: Integer);
begin
  if (Value >= 0) and (Value <100) then
     FSecVal := value;
  Invalidate;     
end;

procedure TTIWAdvTimeEdit.SetShowSeconds(const Value: Boolean);
begin
  FShowSeconds := value;
  Invalidate;
end;


{$IFDEF TMSIW6}
procedure TTIWAdvTimeEdit.IWPaint;
{$ELSE}
procedure TTIWAdvTimeEdit.Paint;
{$ENDIF}
var
  R: TRect;
  s: string;
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,11);
  {$IFNDEF LINUX}
  DrawFrameControl(Canvas.Handle,r,DFC_SCROLL,DFCS_SCROLLUP);
  {$ENDIF}

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

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

  s := IntToStr(FHourval)+ TimeSeparator + IntToStr(FMinval);

  if ShowSeconds then
    s := s + TimeSeparator + IntToStr(FSecVal);

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

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

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

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


{$IFDEF TMSIW6}
function TTIWAdvTimeEdit.RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag;
{$ELSE}
function TTIWAdvTimeEdit.RenderHTML: TIWHTMLTag;
{$ENDIF}
var
  htmlres, dbtnval, ubtnval: string;
  tabidx: string;
  ajaxup, ajaxdown: string;

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

   function MakeScript(Name:string): string;
   begin

      {$IFDEF TMSIW9}
      if Assigned(FOnAsyncUpClick) then
      begin
        ajaxup := 'processAjaxEvent(''onUpClick'', '
          + HTMLControlImplementation.IWCLName
          + ',''' + HTMLName + '.' + 'DoOnAsyncUpClick' + ''','
          + 'true' + ', null, '
          + 'true' + ');';

        Acontext.WebApplication.RegisterCallBack(HTMLName+'.DoOnAsyncUpClick', DoOnAsyncUpClick);
      end;

      if Assigned(FOnAsyncDownClick) then
      begin
        ajaxdown := 'processAjaxEvent(''onDownClick'', '
          + HTMLControlImplementation.IWCLName
          + ',''' + HTMLName + '.' + 'DoOnAsyncDownClick' + ''','
          + 'true' + ', null, '
          + 'true' + ');';

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


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

       + 'var '+HTMLName+'decrement;'#13
       + 'var '+HTMLName+'increment;'#13
       + 'var '+HTMLName+'enabled = ' + BoolToStr(Enabled) + ';'#13

       + 'function '+HTMLName +'changeBGColor() {'#13
       + '  document.getElementById('''+HTMLName+'EDSUR'').style.backgroundColor = '#39+HTMLClr(FColor)+#39';'#13
       + '}'#13

       + 'function '+HTMLName+'UpdateVal(ctrl){'#13
       + ' if (ctrl.id == "'+HTMLName+'ED"){'#13
       + '  if (ctrl.value > 23)'#13
       + '  ctrl.value = 1;'#13
       + ' }'#13
       + ' if (ctrl.id == "'+HTMLName+'EDMIN"){'#13
       + '  if (ctrl.value > 59)'#13
       + '  ctrl.value = 1;'#13
       + ' }'#13
       + ' if (ctrl.id == "'+HTMLName+'EDSEC"){'#13
       + '  if (ctrl.value > 59)'#13
       + '  ctrl.value = 1;'#13
       + ' }'#13
       //added - check for undefined
       + ' if (ctrl.value != undefined) {'#13
       + '   if (ctrl.value.length == 1)'#13
       + '     ctrl.value = "0" + ctrl.value;'#13
       + ' }'#13
       + ' ho = document.getElementById("'+HTMLName + 'ED").value;'#13
       + ' mi = document.getElementById("'+HTMLName + 'EDMIN").value;'#13
       +  IIF(ShowSeconds,'se = document.getElementById("'+HTMLName + 'EDSEC").value;'#13,'se = "0";'#13)
       + 'document.getElementById("'+HTMLName+'_INPUT").value = ho + "," + mi + "," + se'#13
       + '}'#13

       + 'function '+HTMLName+'autotab(original,destination,e){'#13
        + 'var key;'#13
        + 'if (isIE) key = event.keyCode; else key = e.which;'#13
       + ' if ((key >= 96) && (key < 105)) {'#13
       + ' if (original.getAttribute && original.value.length==original.getAttribute("maxlength")) {'#13
       + ' el = document.getElementById(destination);'#13
       + ' if (el != null)  { el.focus(); el.select(); '#13
       + '  }'#13
       + ' }'#13
       + ' }'#13
       + '}'#13

       + 'function ' + HTMLName + 'StartSpinUp(el)'#13
       + ' {'#13
       + ' s = el.value;'#13
       + ' if ((s.substr(0,1) == "0") && (s.length == 2)) s = s.substr(1,1);'#13
       + ' i = parseInt(s);'#13
       + '  if (i < 59)'#13
       + '   el.value = i + 1;'#13
       + ' '+HTMLName+'UpdateVal(el);'#13
       + ajaxup
       + '}'#13

       + 'function ' + HTMLName + 'StartSpinUpH(el)'#13
       + ' {'#13
       + ' s = el.value;'#13
       + ' if ((s.substr(0,1) == "0") && (s.length == 2)) s = s.substr(1,1);'#13
       + ' i = parseInt(s);'#13
       + ' if (i < 23)'#13
       + '   el.value = i + 1;'#13
       + ' '+HTMLName+'UpdateVal(el);'#13
       + ajaxup
       + '}'#13

       + 'function ' + HTMLName + 'StartSpinDown(el)'#13
       + ' {'#13
       + ' s = el.value;'#13
       + ' if ((s.substr(0,1) == "0") && (s.length == 2)) s = s.substr(1,1);'#13
       + ' i = parseInt(s);'#13
       + ' if (i > 0)'#13
       + '   el.value = i - 1;'#13
       + ' '+HTMLName+'UpdateVal(el);'#13
       + ajaxdown
       + '}'#13

       + ' function '+HTMLName+'StopSpin()'#13
       + ' {'#13
       + ' clearInterval('+HTMLName+'increment);'#13
       + ' clearInterval('+HTMLName+'decrement);'#13
        + 'if (!document.hasFocus)'#13
        + ' document.hasFocus=document.getElementById("'+HTMLName+'ED");'#13
        + 'if ((document.hasFocus.id != "'+HTMLName+'ED") && (document.hasFocus.id != "'
          +HTMLName+'EDMIN") && (document.hasFocus.id != "'+HTMLName+'EDSEC"))'#13
        + ' document.hasFocus=document.getElementById("'+HTMLName+'ED");'#13
        + ' el = document.getElementById(document.hasFocus.id);'#13
        + ' el.focus();'#13
        + '}'#13

        + 'function '+HTMLName+'ClickProc(editctrl,ctrl) {'#13
        + 'if (' + HTMLName + 'enabled != -1) return false;'#13
        + 'if (!document.hasFocus)'#13
        + ' document.hasFocus=document.getElementById("'+HTMLName+'ED");'#13        
        + 'if ((document.hasFocus.id != "'+HTMLName+'ED") && (document.hasFocus.id != "'
          +HTMLName+'EDMIN") && (document.hasFocus.id != "'+HTMLName+'EDSEC"))'#13
        + ' document.hasFocus=document.getElementById("'+HTMLName+'ED");'#13

        + ' el = document.getElementById(document.hasFocus.id);'#13
        + '  if (el.value.length == 0)'#13
        + '   el.value = 0;'#13
        + ' if (ctrl==1){'#13
        + ' if (document.hasFocus.id == "'+HTMLName+'ED"){'#13
        + ' '+HTMLName+'StartSpinUpH(el);'#13
        + IIF (AutoSpin, HTMLName+'increment = setInterval("'+HTMLName+'StartSpinUpH(el)",200);')
        + '  }else{'#13
        + ' '+HTMLName+'StartSpinUp(el);'#13
        + IIF (AutoSpin, HTMLName+'increment = setInterval("'+HTMLName+'StartSpinUp(el)",200);')
        + '  } '#13
        + '  } else{'#13
        + ' '+HTMLName+'StartSpinDown(el);'#13
        + IIF (AutoSpin, HTMLName+'decrement = setInterval("'+HTMLName+'StartSpinDown(el)",200);')
        + '  }'#13
        + ' el.focus();'#13
        + '}'#13

        + 'var isIE = navigator.appName.indexOf("Microsoft") != -1;'#13

        + 'function ' + HTMLName + 'KeyPress(ctrl,edittype,e) '#13
        + '{'#13
        + 'var key;'#13
        + 'if (isIE) key = event.keyCode; else key = e.which;'#13

        + IIF(Editable, '','if (isIE) event.returnValue=false; else return false;')

        + ' if ((key == 0) || (key == 8))'#13 //tabkey / arrow keys & backspace in FF
        + '   return;'#13

        + ' if ((key == 13) && '+IIF(FAdvanceOnReturn,'(1>0))','(1<0))')+'{'#13
        + ' var i;'#13
        + 'for (i = 0; i < ctrl.form.elements.length; i++)'#13
        + ' if (ctrl == ctrl.form.elements[i])'#13
        + '  break;'#13
        + ' i = (i + 1) % ctrl.form.elements.length;'#13

        + '  ctrl.form.elements[i].focus();'#13
        + '    return false;}'#13
        + ' if (key == 13) '+HTMLName+'UpdateVal(ctrl);'#13
        + ' if ((key == 13) && '+IIF(FSubmitOnReturn,'(1>0))','(1<0))')+'{'#13
        + '   return SubmitClick('+#39+HTMLName+#39','+'ctrl.value,'+#39'false'#39');'+#13
        + '}'#13

        + ' if (key < 48 || key > 57) {'#13
        + '  if(isIE)'#13
        + '    event.returnValue = false;'#13
        + '  else'#13
        + '    return false;'#13
        + '  }'#13
        + '}'#13
        + 'function ' + HTMLName + 'Focus(ctrl) {'#13;

      if FFocusColor <> FColor then
        Result := Result
        + 'document.getElementById('''+HTMLName+'EDSUR'').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
         + '}'
         + 'function ' + HTMLName + 'FocusDIV() {'#13;

       if FFocusColor <> FColor then
         Result := Result
          + 'document.getElementById("'+HTMLName+'EDSUR").style.backgroundColor = '#39+HTMLClr(FFocusColor)+#39''#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
  tabidx := '';
  {$IFDEF TMSIW72_NEW}
  if (TabOrder >= 0) then
    tabidx := ' tabindex="'+IntToStr(TabOrder+1)+'" ';
  {$ENDIF}

  htmlres := MakeScript(HTMLName)
    + '<table cellspacing="0" cellpadding="0" border="0" style="border-collapse: collapse"><tr>'#13
    + '  <td rowspan="2">'#13

    + '<div id="'+HTMLName+'EDSUR" '
    + ' style="width:'+inttostr(width-18-2)+';height:100%;position:relative;border-style:solid;border-width:'
    + inttostr(FBorderWidth)+ 'px;border-color:'+HTMLClr(FBorderColor)+';'
    + IIF(FAlignment <> taLeftJustify,MakeAlign(FAlignment)) + ';'
    + IIF((FColor <> clNone) and (FColor <> clWindow), ' background-color:'+HTMLClr(FColor)+';')
    + '"'
    {$IFDEF TMSIW9}
    + IIF((Css <> EmptyStr),' class="' + Css + '"','')
    {$ENDIF}
    + ' onFocus="'+HTMLName+'FocusDIV();"' + iif(FFocusColor <> FColor,' onBlur="' + HTMLName + 'UpdateVal(this);' + HTMLName + 'changeBGColor()"','')
    + '>'#13

    + ' <input type="text" '+tabidx+' name="' + HTMLName +'H"' + iif(enabled,'',' DISABLED')+ ' id="' + HTMLName +'ED"'
    + ' maxlength=2 size=1'
    + IIF ((FHourVal < 10), ' value="0' + IntToStr(FHourVal) + '"', ' value="' + IntToStr(FHourVal) + '"')
    + IIF(ReadOnly, ' readonly', '')
    + ' style="width:18;border-style:none;border-width:0px;'
    {$IFDEF TMSIW6}
    + Font.FontToStringStyle(AContext.WebApplication.Browser)
    {$ELSE}
    + Font.FontToStringStyle(WebApplication.Browser)
    {$ENDIF}
    + ' background-color:transparent;'
    + '"'
    + ' onKeyPress="return '+HTMLName+'KeyPress(this,1,event);"'
    + IIF(FAlignment <> taLeftJustify,MakeAlign(FAlignment))
    {$IFDEF TMSIW9}
    + IIF((Css <> EmptyStr),' class="' + Css + '"','')
    {$ENDIF}
    + ' onFocus="'+HTMLName+'Focus(this);document.hasFocus=this;"'
    + ' onBlur="'+HTMLName+'UpdateVal(this);'+ iif(FFocusColor <> FColor,' document.getElementById('''+HTMLName+'EDSUR'').style.backgroundColor = '#39 + HTMLClr(FColor)+#39';','') + '"'
    + ' onKeyup="return '+HTMLName+'autotab(this, '''+HTMLName+'EDMIN'',event)"'
    + '>'#13

    + TimeSeparator +' <input type="text" '+tabidx+' name="' + HTMLName +'M"' + iif(enabled,'',' DISABLED')+ ' id="' + HTMLName +'EDMIN"'
    + ' maxlength=2 size=1'
    + IIF ((FMinVal < 10), ' value="0' + IntToStr(FMinVal) + '"', ' value="' + IntToStr(FMinVal) + '"')
    + IIF(ReadOnly, ' readonly', '')
    + ' style="width:18;border-style:none;border-width:0px;'
    {$IFDEF TMSIW6}
    + Font.FontToStringStyle(AContext.WebApplication.Browser)
    {$ELSE}
    + Font.FontToStringStyle(WebApplication.Browser)
    {$ENDIF}
    + 'background-color:transparent;'
    + '"'
    + ' onKeyPress="return '+HTMLName+'KeyPress(this,1,event);"'
    + IIF(FAlignment <> taLeftJustify,MakeAlign(FAlignment))
    {$IFDEF TMSIW9}
    + IIF((Css <> EmptyStr),' class="' + Css + '"','')
    {$ENDIF}
    + ' onFocus="'+HTMLName+'Focus(this);document.hasFocus=this;"'
    + ' onBlur="'+HTMLName+'UpdateVal(this);'
    + iif(FFocusColor <> FColor,' document.getElementById('''+HTMLName+'EDSUR'').style.backgroundColor = '#39
    + HTMLClr(FColor)+#39';','')
    + '"'#13
    + ' onKeyup="return '+HTMLName+'autotab(this,'''+HTMLName+'EDSEC'',event)"'
    + '>'#13;

  if ShowSeconds then
  begin
    htmlres := htmlres
      + TimeSeparator +' <input type=' + IIF(FPasswordPrompt, 'password', 'text') + iif(enabled,'',' DISABLED') + tabidx+' name="'
      + HTMLName +'S"' + ' id="' + HTMLName +'EDSEC"'
      + ' maxlength=2 size=1'
      + IIF ((FSecVal < 10), ' value="0' + IntToStr(FSecVal) + '"', ' value="' + IntToStr(FSecVal) + '"')
      + IIF(ReadOnly, ' readonly', '')
      + ' style="width:18;border-style:none;border-width:0px;'
    {$IFDEF TMSIW6}
    + Font.FontToStringStyle(AContext.WebApplication.Browser)
    {$ELSE}
    + Font.FontToStringStyle(WebApplication.Browser)
    {$ENDIF}
      + ' background-color:transparent;'
      + '"'
      + ' onKeyPress="return '+HTMLName+'KeyPress(this,1,event);"'
      + IIF(FAlignment <> taLeftJustify,MakeAlign(FAlignment))
      {$IFDEF TMSIW9}
      + IIF((Css <> EmptyStr),' class="' + Css + '"','')
      {$ENDIF}
      + ' onFocus="'+HTMLName+'Focus(this);document.hasFocus=this;"'
      + ' onBlur="'+HTMLName+'UpdateVal(this);'
      +  iif(FFocusColor <> FColor,' document.getElementById('''+HTMLName+'EDSUR'').style.backgroundColor = '#39
      + HTMLClr(FColor)+#39';','')
      + '"'#13
      + '>'#13;
  end;
                                                    
  htmlres := htmlres
    + '</div>'#13
    + '</td>'#13
    + '<td valign="bottom"><div style="height:12px;overflow:hidden;">'#13;

  if FButtonUpImageURL <> EmptyStr then
    begin
      ubtnval := '<img src="' + FButtonUpImageURL + '" '
        +iif(enabled,' onMouseUp="'+HTMLName+'StopSpin();" onMouseOut="'
        +HTMLName+'StopSpin();" onMouseDown="'+HTMLName+'ClickProc('+HTMLName+',1);"', '')
        + iif(AContext.WebApplication.Browser = brIE, ' style="position:relative;top:-4px;"', 'style="position:relative;top:2px;"')
        + '>';
    end
    else if FButtonUpImage.Empty then
    begin

      if (AContext.WebApplication.Browser = brIE) then
      begin
       ubtnval := ubtnval
       + '<BUTTON id="'+HTMLName+'upbtnIE" '+iif(enabled,'',' DISABLED')+ ' onMouseUp="'+HTMLName+'StopSpin();" onMouseOut="'+HTMLName+'StopSpin();" onMouseDown="'+HTMLName+'ClickProc('+HTMLName
       +',1);" style="display:;width:18px;height:14px;vertical-align:middle;text-align:center;padding:0px;top:-3px;position:relative;">'
       + ' <DIV style=" font-family:Webdings;font-size:9pt;margin:0px;padding:0px;border:0px;margin-top:-0.4em;margin-right:-0.1em;">'
       + ' 5</DIV></BUTTON>'#13;
      end
      else
      begin
       ubtnval := ubtnval
       + '<BUTTON id="'+HTMLName+'upbtnNS" '+iif(enabled,'',' DISABLED')+ ' onMouseUp="'+HTMLName+'StopSpin();" onMouseOut="'+HTMLName+'StopSpin();" onMouseDown="'+HTMLName+'ClickProc('+
       HTMLName+',1);return false;" style="width:18px;height:14px;vertical-align:middle;text-align:center;padding:0px;top:-1px;position:relative;">'
       +' <DIV style="font-size:9pt;margin:-2px;padding:-1px;position:relative;top:-5;border:0px;margin-top:-0em;margin-right:0em;">'
       +'+</DIV></BUTTON>'#13;
      end;

    end
    else
    begin

      if FButtonUpImage.IsGIF then
      {$IFDEF TMSIW71}
        ubtnval := TIWServerControllerBase.NewCacheFile('gif', true)
      {$ELSE}
        ubtnval := TIWServerControllerBase.NewCacheFile('gif')
      {$ENDIF}
      else
      {$IFDEF TMSIW71}
        ubtnval := TIWServerControllerBase.NewCacheFile('jpg', true);
      {$ELSE}
        ubtnval := TIWServerControllerBase.NewCacheFile('jpg');
      {$ENDIF}

      FButtonUpImage.SaveToFile(ubtnval);

      ubtnval := '<img src="' + GetCacheDir(Self) + ExtractFilename(ubtnval)+ '" '
        +iif(enabled,' onMouseUp="'+HTMLName+'StopSpin();" onMouseOut="'
        +HTMLName+'StopSpin();" onMouseDown="'+HTMLName+'ClickProc('+HTMLName+',1);"', '')
        + iif(AContext.WebApplication.Browser = brIE, ' style="position:relative;top:-4px;"', 'style="position:relative;top:2px;"')
        + '>';

    end;

    if FButtonDownImageURL <> EmptyStr then
    begin
      dbtnval := '<img src="' + FButtonDownImageURL + '" '
        +iif(enabled,' onMouseUp="'+HTMLName+'StopSpin();" onMouseOut="'
        +HTMLName+'StopSpin();" onMouseDown="'+HTMLName+'ClickProc('+HTMLName+',2);"', '')
        + iif(AContext.WebApplication.Browser = brIE, ' style="position:relative;top:-6px;"', 'style="position:relative;top:0px;"')
        + '>';
    end
    else if FButtonDownImage.Empty then
    begin

      if (AContext.WebApplication.Browser = brIE) then
      begin
       dbtnval := dbtnval
       + ' <BUTTON id="'+HTMLName+'dnbtnIE" '+iif(enabled,'',' DISABLED')+ ' onMouseUp="'+HTMLName+'StopSpin();" onMouseOut="'+HTMLName+'StopSpin();" onMouseDown="'+HTMLName+'ClickProc('+HTMLName+
       ',2);" style="display:;width:18px;height:14px;vertical-align:middle;text-align:center;padding:0px;top:-4px;position:relative;">'
       + ' <DIV style=" font-family:Webdings;font-size:9pt;margin:0px;padding:0px;border:0px;margin-top:-0.4em;margin-right:-0.1em;">'
       + ' 6</DIV></BUTTON>'#13;
      end
      else
      begin
       dbtnval := dbtnval
       + '<BUTTON id="'+HTMLName+'dnbtnNS" '+iif(enabled,'',' DISABLED')+ ' onMouseUp="'+HTMLName+'StopSpin();" onMouseOut="'+HTMLName+'StopSpin();" onMouseDown="'+HTMLName+'ClickProc('
       +HTMLName+',2);return false;" style="width:18px;height:14px;vertical-align:middle;text-align:center;padding:0px;top:-2px;position:relative;">'
       +' <DIV style="font-size:9pt;margin:-2px;padding:-5px;position:relative;top:-1;border:0px;margin-top:-0.4em;margin-right:0em;">'
       +'-</DIV></BUTTON>'#13;
      end;

    end
    else
    begin

      if FButtonDownImage.IsGIF then
      {$IFDEF TMSIW71}
        dbtnval := TIWServerControllerBase.NewCacheFile('gif', true)
      {$ELSE}
        dbtnval := TIWServerControllerBase.NewCacheFile('gif')
      {$ENDIF}
      else
      {$IFDEF TMSIW71}
        dbtnval := TIWServerControllerBase.NewCacheFile('jpg', true);
      {$ELSE}
        dbtnval := TIWServerControllerBase.NewCacheFile('jpg');
      {$ENDIF}

      FButtonDownImage.SaveToFile(dbtnval);

      dbtnval := '<img src="' + GetCacheDir(Self) + ExtractFilename(dbtnval)+ '" '
        +iif(enabled,' onMouseUp="'+HTMLName+'StopSpin();" onMouseOut="'
        +HTMLName+'StopSpin();" onMouseDown="'+HTMLName+'ClickProc('+HTMLName+',2);"', '')
        + iif(AContext.WebApplication.Browser = brIE, ' style="position:relative;top:-6px;"', 'style="position:relative;top:0px;"')
        + '>';

    end;

  htmlres := htmlres + ubtnval;

  htmlres := htmlres
     + ' </div></td>'#13
     + '  </tr>'#13
     + '  <tr>'#13
     + '  <td valign="top"><div style="height:12px;overflow:hidden;">'#13;

  htmlres := htmlres + dbtnval;

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

  htmlres := htmlres
		 + '<Script>'
		 + 'var isIE = navigator.appName.indexOf("Microsoft") != -1;'
//		 + '  if (! isIE){'
		 //+ '    document.getElementById("'+HTMLName+'upbtnIE").style.display=''none'';'
		 //+ '    document.getElementById("'+HTMLName+'dnbtnIE").style.display=''none'';'
		 //+ '    document.getElementById("'+HTMLName+'upbtnNS").style.display=''inline'';'
		 //+ '    document.getElementById("'+HTMLName+'dnbtnNS").style.display=''inline'';'
//		 + '  } '
		 + '</Script>'#13;

  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;

  Result := TIWHTMLTag.CreateTag('DIV');
  Result.Contents.AddText(htmlres);
  //if css class is defined remove it from the outer div.
  //the class should only be set on the input tag
  {$IFDEF TMSIW9}
  if Css <> EmptyStr then
    Result.AddStringParam('class', ' ');
  {$ENDIF}  
  
  with Result.Contents.AddTag('INPUT') do
  begin
    AddStringParam('TYPE','hidden');
    AddStringParam('NAME',HTMLName);
    AddStringParam('ID',HTMLName+'_INPUT');
  end;
end;

procedure TTIWAdvTimeEdit.SetTime(const Value: TDateTime);
var
  msec:word;
  ho,mi,se:word;
begin
  DecodeTime(Value,ho,mi,se,Msec);
  FHourVal := ho;
  FMinVal := mi;
  FSecVal := se;
end;

destructor TTIWAdvTimeEdit.Destroy;
begin
  FButtonDownImage.Free;
  FButtonUpImage.Free;
  inherited;
end;

function TTIWAdvTimeEdit.GetTime: TDateTime;
begin
  Result := encodetime(FHourVal,FMinVal,FSecVal,0);
end;


procedure TTIWAdvTimeEdit.SetValue(const AValue: string);
var
  sp: Integer;
  ss, su: string;
begin
  inherited;
  if AValue = '' then
    Exit;

  su :=  AValue;


  sp := pos(',', su);
  
  if sp > 0 then
  begin
    ss := copy(su, 1, sp - 1);
    Delete(su, 1, sp);
  end
  else
    ss := su;

  FHourVal := StrToInt(ss);

  sp := pos(',', su);
  
  if sp > 0 then
  begin
    ss := copy(su, 1, sp - 1);
    Delete(su, 1, sp);
  end
  else
    ss := su;

  FMinVal := StrToInt(ss);

  if ShowSeconds then
  begin
    sp := pos(',', su);

    if sp > 0 then
    begin
      ss := copy(su, 1, sp - 1);
      Delete(su, 1, sp);
    end
    else
      ss := su;

    FSecVal := StrToInt(ss);
  end;

end;



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

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

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

procedure TTIWAdvTimeEdit.SetButtonDownImage(const Value: TIPicture);
begin
  FButtonDownImage.Assign(Value);
end;

procedure TTIWAdvTimeEdit.SetButtonUpImage(const Value: TIPicture);
begin
  FButtonUpImage.Assign(Value);
end;

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

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




{ TTIWAdvDateEdit }
{$IFDEF TMSIW9}
procedure TTIWAdvDateEdit.DoOnAsyncUpClick(AParams: TStringList);
begin
  if Assigned(OnAsyncUpClick) then
    OnAsyncUpClick(Self, AParams, Year, Month, Day);
end;

procedure TTIWAdvDateEdit.DoOnAsyncDownClick(AParams: TStringList);
begin
  if Assigned(OnAsyncDownClick) then
    OnAsyncDownClick(Self, AParams, Year, Month, Day);
end;

function TTIWAdvDateEdit.RenderAsync(
  AContext: TIWBaseHTMLComponentContext): TIWXMLTag;
var
  js: string;
begin
  Result := TIWXMLTag.CreateTag('control');
  try
    Result.AddStringParam('id', HTMLName);
    Result.AddStringParam('type', 'TTIWADVDATEEDIT');
    RenderAsyncCommonProperties( AContext, Result );

    js :=
      '<![CDATA['
      + 'document.getElementById("' + HTMLName + 'ED").disabled = ' + iif(enabled,'false','true') + ';'#13
      + HTMLName + 'enabled = ' + BoolToStr(Enabled) +';'#13
      + 'document.getElementById("' + HTMLName + 'EDMIN").disabled = ' + iif(enabled,'false','true') + ';'#13
      + HTMLName + 'enabled = ' + BoolToStr(Enabled) +';'#13
      + 'el = document.getElementById("'+ HTMLName + 'EDSEC"); if (el) el.disabled = ' + iif(enabled,'false','true') +';'#13
      + 'var upIE = document.getElementById("' + HTMLName + 'upbtnIE");'#13
      + 'var dnIE = document.getElementById("' + HTMLName + 'dnbtnIE");'#13
      + 'var upNS = document.getElementById("' + HTMLName + 'upbtnNS");'#13
      + 'var dnNS = document.getElementById("' + HTMLName + 'dnbtnNS");'#13
      + 'if (upIE)'
      + '  upIE.disabled = ' + iif(enabled,'false','true') +';'
      + 'if (dnIE)'
      + '  dnIE.disabled = ' + iif(enabled,'false','true') +';'
      + 'if (upNS)'
      + '  upNS.disabled = ' + iif(enabled,'false','true') +';'
      + 'if (dnNS)'
      + '  dnNS.disabled = ' + iif(enabled,'false','true') +';'
      +']]>';

    GGetWebApplicationThreadVar.CallBackResponse.AddJavaScriptToExecute(js);
          
  except
    FreeAndNil(Result);
  end;

end;
{$ENDIF}

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

  FDayVal := 1;
  FMonthVal := 1;
  FYearVal := 1;

  FDateFormat := dfDayMonthYear;
  FButtonDownImage := TIPicture.Create;
  FButtonUpImage := TIPicture.Create;  
end;


{$IFDEF TMSIW6}
procedure TTIWAdvDateEdit.IWPaint;
{$ELSE}
procedure TTIWAdvDateEdit.Paint;
{$ENDIF}
var
  R: TRect;
  s: string;
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,11);
  {$IFNDEF LINUX}
  DrawFrameControl(Canvas.Handle,r,DFC_SCROLL,DFCS_SCROLLUP);
  {$ENDIF}

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

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

  if FDateFormat = dfDayMonthYear then
    s := IntToStr(FDayVal) + DateSeparator + IntToStr(FMonthVal) + DateSeparator + IntToStr(FYearVal)
  else
    s := IntToStr(FMonthVal) + DateSeparator + IntToStr(FDayVal) + DateSeparator + IntToStr(FYearVal);

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

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

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

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


{$IFDEF TMSIW6}
function TTIWAdvDateEdit.RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag;
{$ELSE}
function TTIWAdvDateEdit.RenderHTML: TIWHTMLTag;
{$ENDIF}
var
  htmlres, daycode, monthcode, dbtnval, ubtnval: string;
  tabidx: string;
  ajaxup, ajaxdown: string;

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

  function MakeScript(Name:string): string;
  begin
  
    {$IFDEF TMSIW9}
    if Assigned(FOnAsyncUpClick) then
    begin
      ajaxup := 'processAjaxEvent(''onUpClick'', '
        + HTMLControlImplementation.IWCLName
        + ',''' + HTMLName + '.' + 'DoOnAsyncUpClick' + ''','
        + 'true' + ', null, '
        + 'true' + ');';

      Acontext.WebApplication.RegisterCallBack(HTMLName+'.DoOnAsyncUpClick', DoOnAsyncUpClick);
    end;

    if Assigned(FOnAsyncDownClick) then
    begin
      ajaxdown := 'processAjaxEvent(''onDownClick'', '
        + HTMLControlImplementation.IWCLName
        + ',''' + HTMLName + '.' + 'DoOnAsyncDownClick' + ''','
        + 'true' + ', null, '
        + 'true' + ');';

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

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

        + 'var decrement;'#13
        + 'var increment;'#13
        + 'var '+HTMLName+'enabled = ' + BoolToStr(Enabled) + ';'#13

        + 'function '+HTMLName +'changeBGColor() {'#13
        + '  document.getElementById('''+HTMLName+'EDSUR'').style.backgroundColor = '#39+HTMLClr(FColor)+#39';'#13
        + '}'#13

        + 'function '+HTMLName+'UpdateVal(ctrl){'#13
        + ' if (ctrl.id == "'+HTMLName+'ED"){'#13
        + '  if ((ctrl.value > 31) || (ctrl.value < 1))'#13
        + '  ctrl.value = 1;'#13
        + ' }'#13
        + ' if (ctrl.id == "'+HTMLName+'EDMIN"){'#13
        + '  if ((ctrl.value > 12) || (ctrl.value < 1))'#13
        + '  ctrl.value = 1;'#13
        + ' }'#13
        + ' if (ctrl.id == "'+HTMLName+'EDSEC"){'#13
        + '  if ((ctrl.value > 9999) || (ctrl.value < 1))'#13
        + '  ctrl.value = 1;'#13
        + ' }'#13
        + ' if (ctrl.id != "'+HTMLName+'EDSEC"){'#13
        + '  if (ctrl.value.length < 2)'#13
        + '  ctrl.value = "0" + ctrl.value;'#13
        + ' }'#13
        + ' da = document.getElementById("'+HTMLName + 'ED").value;'#13
        + ' mo = document.getElementById("'+HTMLName + 'EDMIN").value;'#13
        + ' ye = document.getElementById("'+HTMLName + 'EDSEC").value;'#13
        + 'document.getElementById("'+HTMLName+'_INPUT").value = da + "," + mo + "," + ye'#13
        //+ ' window.status = da + "," + mo + "," + ye'#13
        + '}'#13

        + 'function '+HTMLName+'autotab(original,destination,e){'#13
        + ' var key;'#13
        + ' if (isIE) key = event.keyCode; else key = e.which;'#13
        + ' if ((key >= 96) && (key < 105)) {'#13
        + ' if (original.getAttribute&&original.value.length==original.getAttribute("maxlength")) {'#13
        + ' el = document.getElementById(destination);'#13
        + ' if (el != null)  { el.focus(); el.select(); '#13
        + '  }'#13
        + '  }'#13
        + ' }'#13
        + '}'#13

        + 'function ' + HTMLName + 'StartSpinUp(el)'#13
        + ' {'#13
        + ' s = el.value;'#13
        + ' if ((s.substr(0,1) == "0") && (s.length == 2)) s = s.substr(1,1);'#13
        + ' i = parseInt(s);'#13
        + ' if (i < 31)'#13
        + '   el.value = i + 1;'#13
        + ' '+HTMLName+'UpdateVal(el);'#13
        + ajaxup
        + '}'#13

        + 'function ' + HTMLName + 'StartSpinUpM(el)'#13
        + ' {'#13
        + ' s = el.value;'#13
        + ' if ((s.substr(0,1) == "0") && (s.length == 2)) s = s.substr(1,1);'#13
        + ' i = parseInt(s);'#13
        + ' if (i < 12)'#13
        + '   el.value = i + 1;'#13
        + ' '+HTMLName+'UpdateVal(el);'#13
        + ajaxup
        + '}'#13

        + 'function ' + HTMLName + 'StartSpinUpY(el)'#13
        + ' {'#13
        + '  if (el.value < 9999)'#13
        + '   el.value = parseInt(el.value) + 1;'#13
        + ' '+HTMLName+'UpdateVal(el);'#13
        + ajaxup
        + '}'#13


        + 'function ' + HTMLName + 'StartSpinDown(el)'#13
        + ' {'#13
        + ' s = el.value;'#13
        + ' if ((s.substr(0,1) == "0") && (s.length == 2)) s = s.substr(1,1);'#13
        + ' i = parseInt(s);'#13
        + ' if (i > 1)'#13
        + '   el.value = i - 1;'#13
        + ' '+HTMLName+'UpdateVal(el);'#13        
        + ajaxdown
        + '}'#13

        + ' function '+HTMLName+'StopSpin()'#13
        + ' {'#13
        + ' clearInterval(increment);'#13
        + ' clearInterval(decrement);'#13
        + 'if (!document.hasFocus)'#13
        + ' document.hasFocus=document.getElementById("'+HTMLName+'ED");'#13        
        + 'if ((document.hasFocus.id != "'+HTMLName+'ED") && (document.hasFocus.id != "'
          +HTMLName+'EDMIN") && (document.hasFocus.id != "'+HTMLName+'EDSEC"))'#13
        + ' document.hasFocus=document.getElementById("'+HTMLName+'ED");'#13
        + ' el = document.getElementById(document.hasFocus.id);'#13
        + ' el.focus();'#13
        + '}'#13

        + 'function '+HTMLName+'ClickProc(editctrl,ctrl) {'#13
        + 'if (' + HTMLName + 'enabled != -1) return false;'#13
        + 'if (!document.hasFocus)'#13
        + ' document.hasFocus=document.getElementById("'+HTMLName+'ED");'#13        
        + 'if ((document.hasFocus.id != "'+HTMLName+'ED") && (document.hasFocus.id != "'
          +HTMLName+'EDMIN") && (document.hasFocus.id != "'+HTMLName+'EDSEC"))'#13
        + ' document.hasFocus=document.getElementById("'+HTMLName+'ED");'#13

        + ' el = document.getElementById(document.hasFocus.id);'#13
        + '  if (el.value.length == 0)'#13
        + '   el.value = 0;'#13
        + ' if (ctrl==1){'#13
        + ' if (document.hasFocus.id == "'+HTMLName+'ED"){'#13
        + ' '+HTMLName+'StartSpinUp(el);'#13
        + IIF (AutoSpin, 'increment = setInterval("'+HTMLName+'StartSpinUp(el)",200);')
         + '  }else'#13
        + ' if (document.hasFocus.id == "'+HTMLName+'EDMIN"){'#13
        + ' '+HTMLName+'StartSpinUpM(el);'#13
        + IIF (AutoSpin, 'increment = setInterval("'+HTMLName+'StartSpinUpM(el)",200);')
         + '  }else'#13
        + ' if (document.hasFocus.id == "'+HTMLName+'EDSEC"){'#13
        + ' '+HTMLName+'StartSpinUpY(el);'#13
        + IIF (AutoSpin, 'increment = setInterval("'+HTMLName+'StartSpinUpY(el)",200);')
        + '  } '#13
        + '  } else{'#13
        + ' '+HTMLName+'StartSpinDown(el);'#13
        + IIF (AutoSpin, 'decrement = setInterval("'+HTMLName+'StartSpinDown(el)",200);')
         + '  }'#13
        + ' el.focus();'#13
        + '}'#13

        + 'var isIE = navigator.appName.indexOf("Microsoft") != -1;'#13

        + 'function ' + HTMLName + 'KeyPress(ctrl,edittype,e) '#13
        + '{'#13

	      + ' var key;'#13
        +	' if (isIE) key = event.keyCode; else key = e.which;'#13

        + IIF(Editable, '','if (isIE) event.returnValue=false; else return false;')

        + ' if ((key == 0) || (key == 8))'#13 //tabkey / arrow keys & backspace in FF
        + '   return;'#13

        + ' if ((key == 13) && '+IIF(FAdvanceOnReturn,'(1>0))','(1<0))')+'{'#13
        + ' var i;'#13
        + 'for (i = 0; i < ctrl.form.elements.length; i++)'#13
        + ' if (ctrl == ctrl.form.elements[i])'#13
        + '  break;'#13
        + ' i = (i + 1) % ctrl.form.elements.length;'#13

        + '  ctrl.form.elements[i].focus();'#13
        + '    return false;}'#13
        + ' if (key == 13) '+HTMLName+'UpdateVal(ctrl);'#13
        + ' if ((key == 13) && '+IIF(FSubmitOnReturn,'(1>0))','(1<0))')+'{'#13
        + '   return SubmitClick('+#39+HTMLName+#39','+'ctrl.value,'+#39'false'#39');'+#13
        + '}'#13

        + ' if (key < 48 || key > 57) {'#13
        + '  if (isIE) event.returnValue = false; else return false;'#13
        + ' }'#13
        + '}'#13

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

      if FFocusColor <> FColor then
        Result := Result
        + 'document.getElementById('''+HTMLName+'EDSUR'').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
        + '}'
        + 'function ' + HTMLName + 'FocusDIV(ctrl) {'#13;

        if FFocusColor <> FColor then
          Result := Result
           + 'document.getElementById('''+HTMLName+'EDSUR'').style.backgroundColor = '#39+HTMLClr(FFocusColor)+#39''#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
  daycode := '';
  monthcode := '';
  
  tabidx := '';
  {$IFDEF TMSIW72_NEW}
  if (TabOrder >= 0) then
    tabidx := ' tabindex="'+IntToStr(TabOrder+1)+'" ';
  {$ENDIF}

  htmlres := MakeScript(HTMLName)
    + '<table cellspacing="0" cellpadding="0" border="0" style="border-collapse: collapse"><tr>'#13
    + '<td rowspan="2">'#13

    + '<div id="'+HTMLName+'EDSUR" '
    + 'style="width:'+inttostr(width-18-2)+'px;height:100%;position:relative;border-style:solid;border-width:'
    + IntToStr(FBorderWidth)+ 'px;border-color:'+HTMLClr(FBorderColor)+';'
    + IIF((FColor <> clNone) and (FColor <> clWindow), ' background-color:'+HTMLClr(FColor)+';')
    + IIF(FAlignment <> taLeftJustify,MakeAlign(FAlignment))
    + '"'
    {$IFDEF TMSIW9}
    + IIF((Css <> EmptyStr),' class="' + Css + '"','')
    {$ENDIF}
    + '" onFocus="'+HTMLName+'FocusDIV(this);"'
    + iif(FFocusColor <> FColor,' onBlur="'+HTMLName+'changeBGColor();"','')
    + '>'#13;

    daycode := daycode
     + '    <input type=' + IIF(FPasswordPrompt, 'password', 'text') + tabidx + ' name="' + HTMLName +'D"' + iif(enabled,'',' DISABLED') +' id="' + HTMLName +'ED"'
     + ' maxlength=2 size=1'
     + IIF ((FDayVal < 10), ' value="0' + IntToStr(FDayVal) + '"', ' value="' + IntToStr(FDayVal) + '"')
     + IIF(ReadOnly, ' readonly', '')
    {$IFDEF TMSIW9}
     + IIF((Css <> EmptyStr),' class="' + Css + '"','')
     {$ENDIF}
     + ' style="width:18;border-style:none;border-width:0px;'
    {$IFDEF TMSIW6}
     + Font.FontToStringStyle(AContext.WebApplication.Browser)
    {$ELSE}
     + Font.FontToStringStyle(WebApplication.Browser)
    {$ENDIF}
     + ' background-color:transparent;'
     + '"'
     + ' onKeyPress="return '+HTMLName+'KeyPress(this,'+IntTostr(1)+',event);"'
     + IIF(FAlignment <> taLeftJustify,MakeAlign(FAlignment))
     + ' onFocus="'+HTMLName+'Focus(this);document.hasFocus=this;"'
     + ' onBlur="'+HTMLName+'UpdateVal(this);'
     + iif(FFocusColor <> FColor,' document.getElementById('''+HTMLName+'EDSUR'').style.backgroundColor = '#39
     + HTMLClr(FColor)+#39';','')
     + '"'#13
     + ' onKeyup="return '+HTMLName+'autotab(this, '''+HTMLName+'EDMIN'',event)"'
     + '>'#13;

     monthcode := monthcode
     +' <input type=' + IIF(FPasswordPrompt, 'password', 'text') + iif(enabled,'',' DISABLED') +tabidx + ' name="'
     + HTMLName +'M"' + ' id="' + HTMLName +'EDMIN"'
     + ' maxlength=2 size=1'
     + IIF ((FMonthVal < 10), ' value="0' + IntToStr(FMonthVal) + '"', ' value="' + IntToStr(FMonthVal) + '"')
     + IIF(ReadOnly, ' readonly', '')
     {$IFDEF TMSIW9}
     + IIF((Css <> EmptyStr),' class="' + Css + '"','')
     {$ENDIF}
     + ' style="width:18;border-style:none;border-width:0px;'
    {$IFDEF TMSIW6}
     + Font.FontToStringStyle(AContext.WebApplication.Browser)
    {$ELSE}
     + Font.FontToStringStyle(WebApplication.Browser)
    {$ENDIF}
     + ' background-color:transparent;'
     + '"'
     + ' onKeyPress="return '+HTMLName+'KeyPress(this,'+IntTostr(1)+',event);"'
     + IIF(FAlignment <> taLeftJustify,MakeAlign(FAlignment))
     + ' onFocus="'+HTMLName+'Focus(this);document.hasFocus=this;"'
     + ' onBlur="'+HTMLName+'UpdateVal(this);'
     + iif(FFocusColor <> FColor,' document.getElementById('''+HTMLName+'EDSUR'').style.backgroundColor = '#39
     + HTMLClr(FColor)+#39';','')
     + '"'#13
     + ' onKeyup="return '+HTMLName+'autotab(this, '''+HTMLName+'EDSEC'',event)"'
     + '>'#13;

     if DateFormat = dfDayMonthYear then
      htmlres := htmlres + DayCode + DateSeparator + MonthCode
     else
      htmlres := htmlres + MonthCode + DateSeparator + DayCode;

     htmlres := htmlres
     + DateSeparator +' <input type=' + IIF(FPasswordPrompt, 'password', 'text') + iif(enabled,'',' DISABLED') +tabidx + ' name="'
     + HTMLName +'Y"' + ' id="' + HTMLName +'EDSEC"'
     + ' maxlength=4 size=3'
     + IIF ((FYearVal < 1), ' value="000' + IntToStr(FYearVal) + '"', ' value="' + IntToStr(FYearVal) + '"')
     + IIF(ReadOnly, ' readonly', '')
     {$IFDEF TMSIW9}
     + IIF((Css <> EmptyStr),' class="' + Css + '"','')
     {$ENDIF}
     + ' style="width:36;border-style:none;border-width:0px;'
    {$IFDEF TMSIW6}
     + Font.FontToStringStyle(AContext.WebApplication.Browser)
    {$ELSE}
     + Font.FontToStringStyle(WebApplication.Browser)
    {$ENDIF}
     + ' background-color:transparent;'
     + '"'
     + ' onKeyPress="return '+HTMLName+'KeyPress(this,'+IntTostr(1)+',event);"'
     + IIF(FAlignment <> taLeftJustify,MakeAlign(FAlignment))
     + ' onFocus="'+HTMLName+'Focus(this);document.hasFocus=this;"'
     + ' onBlur="'+HTMLName+'UpdateVal(this);'
     + iif(FFocusColor <> FColor,' document.getElementById('''+HTMLName+'EDSUR'').style.backgroundColor = '#39 + HTMLClr(FColor)+#39'";','') + '"'#13
     + '>'#13;

     htmlres := htmlres
       + '</div>'#13
       + '</td>'#13
       + '  <td valign="bottom"><div style="height:12px;overflow:hidden;">'#13;

    if FButtonUpImageURL <> EmptyStr then
      begin
        ubtnval := '<img src="' + FButtonUpImageURL + '" '
          +iif(enabled,' onMouseUp="'+HTMLName+'StopSpin();" onMouseOut="'
          +HTMLName+'StopSpin();" onMouseDown="'+HTMLName+'ClickProc('+HTMLName+',1);"', '')
          + iif(AContext.WebApplication.Browser = brIE, ' style="position:relative;top:-4px;"', 'style="position:relative;top:2px;"')          
          + '>';
      end
      else if FButtonUpImage.Empty then
      begin

        if (AContext.WebApplication.Browser = brIE) then
        begin
         ubtnval := ubtnval
         + '<BUTTON id="'+HTMLName+'upbtnIE" '+iif(enabled,'',' DISABLED')+ ' onMouseUp="'+HTMLName+'StopSpin();" onMouseOut="'+HTMLName+'StopSpin();" onMouseDown="'+HTMLName+'ClickProc('+HTMLName
         +',1);" style="display:;width:18px;height:14px;vertical-align:middle;text-align:center;padding:0px;top:-3px;position:relative;">'
         + ' <DIV style=" font-family:Webdings;font-size:9pt;margin:0px;padding:0px;border:0px;margin-top:-0.4em;margin-right:-0.1em;">'
         + ' 5</DIV></BUTTON>'#13;
        end
        else
        begin
         ubtnval := ubtnval
         + '<BUTTON id="'+HTMLName+'upbtnNS" '+iif(enabled,'',' DISABLED')+ ' onMouseUp="'+HTMLName+'StopSpin();" onMouseOut="'+HTMLName+'StopSpin();" onMouseDown="'+HTMLName+'ClickProc('+
         HTMLName+',1);return false;" style="width:18px;height:14px;vertical-align:middle;text-align:center;padding:0px;top:-1px;position:relative;">'
         +' <DIV style="font-size:9pt;margin:-2px;padding:-1px;position:relative;top:-5;border:0px;margin-top:-0em;margin-right:0em;">'
         +'+</DIV></BUTTON>'#13;
        end;

      end
      else
      begin

        if FButtonUpImage.IsGIF then
        {$IFDEF TMSIW71}
          ubtnval := TIWServerControllerBase.NewCacheFile('gif', true)
        {$ELSE}
          ubtnval := TIWServerControllerBase.NewCacheFile('gif')
        {$ENDIF}
        else
        {$IFDEF TMSIW71}
          ubtnval := TIWServerControllerBase.NewCacheFile('jpg', true);
        {$ELSE}
          ubtnval := TIWServerControllerBase.NewCacheFile('jpg');
        {$ENDIF}

        FButtonUpImage.SaveToFile(ubtnval);

        ubtnval := '<img src="' + GetCacheDir(Self) + ExtractFilename(ubtnval)+ '" '
          +iif(enabled,' onMouseUp="'+HTMLName+'StopSpin();" onMouseOut="'
          +HTMLName+'StopSpin();" onMouseDown="'+HTMLName+'ClickProc('+HTMLName+',1);"', '')
          + iif(AContext.WebApplication.Browser = brIE, ' style="position:relative;top:-4px;"', 'style="position:relative;top:2px;"')
          + '>';

      end;

      if FButtonDownImageURL <> EmptyStr then
      begin
        dbtnval := '<img src="' + FButtonDownImageURL + '" '
          +iif(enabled,' onMouseUp="'+HTMLName+'StopSpin();" onMouseOut="'
          +HTMLName+'StopSpin();" onMouseDown="'+HTMLName+'ClickProc('+HTMLName+',2);"', '')
          + iif(AContext.WebApplication.Browser = brIE, ' style="position:relative;top:-6px;"', 'style="position:relative;top:0px;"')
          + '>';
      end
      else if FButtonDownImage.Empty then
      begin

        if (AContext.WebApplication.Browser = brIE) then
        begin
         dbtnval := dbtnval
         + ' <BUTTON id="'+HTMLName+'dnbtnIE" '+iif(enabled,'',' DISABLED')+ ' onMouseUp="'+HTMLName+'StopSpin();" onMouseOut="'+HTMLName+'StopSpin();" onMouseDown="'+HTMLName+'ClickProc('+HTMLName+
         ',2);" style="display:;width:18px;height:14px;vertical-align:middle;text-align:center;padding:0px;top:-4px;position:relative;">'
         + ' <DIV style=" font-family:Webdings;font-size:9pt;margin:0px;padding:0px;border:0px;margin-top:-0.4em;margin-right:-0.1em;">'
         + ' 6</DIV></BUTTON>'#13;
        end
        else
        begin
         dbtnval := dbtnval
         + '<BUTTON id="'+HTMLName+'dnbtnNS" '+iif(enabled,'',' DISABLED')+ ' onMouseUp="'+HTMLName+'StopSpin();" onMouseOut="'+HTMLName+'StopSpin();" onMouseDown="'+HTMLName+'ClickProc('
         +HTMLName+',2);return false;" style="width:18px;height:14px;vertical-align:middle;text-align:center;padding:0px;top:-2px;position:relative;">'
         +' <DIV style="font-size:9pt;margin:-2px;padding:-5px;position:relative;top:-1;border:0px;margin-top:-0.4em;margin-right:0em;">'
         +'-</DIV></BUTTON>'#13;
        end;

      end
      else
      begin

        if FButtonDownImage.IsGIF then
        {$IFDEF TMSIW71}
          dbtnval := TIWServerControllerBase.NewCacheFile('gif', true)
        {$ELSE}
          dbtnval := TIWServerControllerBase.NewCacheFile('gif')
        {$ENDIF}
        else
        {$IFDEF TMSIW71}
          dbtnval := TIWServerControllerBase.NewCacheFile('jpg', true);
        {$ELSE}
          dbtnval := TIWServerControllerBase.NewCacheFile('jpg');
        {$ENDIF}

        FButtonDownImage.SaveToFile(dbtnval);

        dbtnval := '<img src="' + GetCacheDir(Self) + ExtractFilename(dbtnval)+ '" '
          +iif(enabled,' onMouseUp="'+HTMLName+'StopSpin();" onMouseOut="'
          +HTMLName+'StopSpin();" onMouseDown="'+HTMLName+'ClickProc('+HTMLName+',2);"', '')
          + iif(AContext.WebApplication.Browser = brIE, ' style="position:relative;top:-6px;"', 'style="position:relative;top:0px;"')
          + '>';

      end;

     htmlres := htmlres + ubtnval;

     htmlres := htmlres
     + ' </div></td>'#13
     + '  </tr>'#13
     + '  <tr>'#13
     + '  <td valign="top"><div style="height:12px;overflow:hidden;">'#13;

     htmlres := htmlres + dbtnval;

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

     htmlres := htmlres
		 + '<Script>'
		 + 'var isIE = navigator.appName.indexOf("Microsoft") != -1;'
//		 + '  if (! isIE){'
//		 + '    document.getElementById("'+HTMLName+'upbtnIE").style.display=''none'';'
//		 + '    document.getElementById("'+HTMLName+'dnbtnIE").style.display=''none'';'
//		 + '    document.getElementById("'+HTMLName+'upbtnNS").style.display=''inline'';'
//		 + '    document.getElementById("'+HTMLName+'dnbtnNS").style.display=''inline'';'
//		 + '  } '
		 + '</Script>'#13;
    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;

  Result := TIWHTMLTag.CreateTag('DIV');
  Result.Contents.AddText(htmlres);
  //if css class is defined remove it from the outer div.
  //the class should only be set on the input tag
  {$IFDEF TMSIW9}
  if Css <> EmptyStr then
    Result.AddStringParam('class', ' ');
  {$ENDIF}  
  
  with Result.Contents.AddTag('INPUT') do
  begin
    AddStringParam('TYPE','hidden');
    AddStringParam('NAME',HTMLName);
    AddStringParam('ID',HTMLName+'_INPUT');
  end;
end;


procedure TTIWAdvDateEdit.SetValue(const AValue: string);
var
  sp: Integer;
  ss, su: string;
begin
  inherited;
  if AValue = '' then
    Exit;

  su :=  AValue;

  sp := pos(',', su);
  if sp > 0 then
  begin
    ss := copy(su, 1, sp - 1);
    Delete(su, 1, sp);
  end
  else
    ss := su;

  FDayVal := StrToInt(ss);

  sp := pos(',', su);
  if sp > 0 then
  begin
    ss := copy(su, 1, sp - 1);
    Delete(su, 1, sp);
  end
  else
    ss := su;

  FMonthVal := StrToInt(ss);

  sp := pos(',', su);
  if sp > 0 then
  begin
    ss := copy(su, 1, sp - 1);
    Delete(su, 1, sp);
  end
  else
    ss := su;

  FYearVal := StrToInt(ss);
end;

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

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

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

procedure TTIWAdvDateEdit.SetButtonDownImage(const Value: TIPicture);
begin
  FButtonDownImage.Assign(Value);
end;

procedure TTIWAdvDateEdit.SetButtonUpImage(const Value: TIPicture);
begin
  FButtonUpImage.Assign(Value);
end;

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

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

procedure TTIWAdvDateEdit.SetDay(const Value: Integer);
begin
  if (Value > 0) and (Value <= 31) then
    FDayVal := Value;
  Invalidate;
end;

procedure TTIWAdvDateEdit.SetMonth(const Value: Integer);
begin
  if (Value > 0) and (Value <= 12) then
    FMonthVal := Value;
  Invalidate;
end;

procedure TTIWAdvDateEdit.SetYear(const Value: Integer);
begin
  FYearVal := Value;
  Invalidate;
end;

procedure TTIWAdvDateEdit.SetDate(const Value: TDateTime);
var
  ye,mo,da:word;
begin
  decodedate(value,ye,mo,da);
  FYearVal := ye;
  FMonthVal := mo;
  FDayVal := da;
end;

destructor TTIWAdvDateEdit.Destroy;
begin
  FButtonDownImage.Free;
  FButtonUpImage.Free;
  inherited;
end;

function TTIWAdvDateEdit.GetDate: TDateTime;
begin
  Result := EncodeDate(FYearVal,FMonthVal,FDayVal);
end;


end.
