function VDWTextControl(obj)
{
   this.control = obj;
}

/* selection functions */
VDWTextControl.prototype.selectRange = function (selectionStart, selectionEnd)
{
  window.focus();
  if (this.control.setSelectionRange) 
  {
    this.control.setSelectionRange(selectionStart, selectionEnd);
  }
  else if (this.control.createTextRange) 
  {
    var range = this.control.createTextRange();
    range.collapse(true);
    range.moveStart('character', selectionStart);
    range.moveEnd('character', selectionEnd);
    range.select();
  }
  this.control.focus();
}

VDWTextControl.prototype.setSelectionRange = function(iStart, iLength)
{
   if (this.control.createTextRange) {
        var oRange = this.control.createTextRange(); 
        oRange.moveStart("character", iStart); 
        oRange.moveEnd("character", iLength - this.control.value.length); 
        oRange.select();
    } else if (this.control.setSelectionRange) {
        this.control.setSelectionRange(iStart, iLength);
    } 
    this.SetFocus(); 
}

VDWTextControl.prototype.selectString = function (string) {
  var match = new RegExp(string, "i").exec(this.control.value);
  if (match) {
    this.selectRange (match.index, match.index + match[0].length);
  }
}

VDWTextControl.prototype.replaceSelection = function (replaceString) {
  if (this.control.setSelectionRange) {
    var selectionStart = this.control.selectionStart;
    var selectionEnd = this.control.selectionEnd;
    this.control.value = this.control.value.substring(0, selectionStart) + replaceString + this.control.value.substring(selectionEnd);
    if (selectionStart != selectionEnd) // has there been a selection
      setSelectionRange(this.control, selectionStart, selectionStart + replaceString.length);
    else // set caret
      setCaretToPos(this.control, selectionStart + replaceString.length);
  }
  else if (document.selection) {
    var range = document.selection.createRange();
    if (range.parentElement() == this.control) {
      var isCollapsed = range.text == '';
      range.text = replaceString;
      if (!isCollapsed)  { // there has been a selection
        //it appears range.select() should select the newly 
        //inserted text but that fails with IE
        range.moveStart('character', -replaceString.length);
        range.select();
      }
    }
  }
}

/* caret functions */
VDWTextControl.prototype.setCaretToEnd = function () 
{
  this.selectRange(this.control, this.control.value.length, this.control.value.length);
}
VDWTextControl.prototype.setCaretToBegin = function () 
{
  this.selectRange(this.control, 0, 0);
}
VDWTextControl.prototype.setCaretToPos = function (pos) 
{
  this.selectRange(this.control, pos, pos);
}

/* normal functions */
VDWTextControl.prototype.SetFocus = function()
{
   window.focus();/*blur on all other objects that got the focus */
   this.control.focus(); /* take the focus on my control */
}

VDWTextControl.prototype.RemoveEvent = function(event,handler,capture)
{
   RemoveEvent(this.control,event,handler,capture);
}

VDWTextControl.prototype.SetEvent = function(event,handler,capture)
{
   SetEvent(this.control,event,handler,capture);
}

VDWTextControl.prototype.ActivateEvent = function(event)
{
   ActivateEvent(this.control,event);
}

/*
if(VDWTextControl..prototype.__defineSetter__)
{
   VDWTextControl.prototype.__defineSetter__("value",function(sText) { this.control.value = sText; });
   VDWTextControl.prototype.__defineGetter__("value",function() { return this.control.value; });
} 
*/

