function fourdigits(number)  {
   return (number < 1000) ? number + 1900 : number;
}

function HTMLEdit(sys, formID, code, posX, posY, width, height, description, value) {
  this.create(sys, formID, code, posX, posY, width, height, description, value);
  this.tagName = 'edit';
  this.type = 1;
  this.month = this.actualMonth + 1;
  this.realMonth = this.actualMonth;
  this.year = this.actualYear;
  this.name = 'HTMLEdit';
  this.tabable = true;
  this.maskSuport = true;
  this.report = false;
}

HTMLEdit.inherits(HTMLLabeledComponent);

HTMLEdit.prototype.now = new Date();
HTMLEdit.prototype.actualMonth = HTMLEdit.prototype.now.getMonth();
HTMLEdit.prototype.actualYear = fourdigits(HTMLEdit.prototype.now.getYear());

HTMLEdit.prototype.getValue = function() {
  var value = this.input.value;
  if (this.textMask) {
    if (this.textMask == "U>") {
      value = value.toUpperCase();
    } else if (this.textMask == "l>") {
      value = value.toLowerCase();
    }
  }
  return value;
}

HTMLEdit.prototype.setWidth = function(width) {
  width = parseInt(width);
  this.callMethod(HTMLLabeledComponent, 'setWidth', width);
  var x = this.width+1;
  if (this.toGrid) x = this.width - 17;
  if (this.btdiv) this.btdiv.style.left = x;
}

HTMLEdit.prototype.setType = function(type) {
  this.type = type;
  if (this.btdiv) {
    this.context.removeChild(this.btdiv);
  this.btdiv = null;
  this.img = null;
  }
}

HTMLEdit.prototype.setMonth = function(m) {
  this.month = m + 1;
  this.realMonth = m;
}

HTMLEdit.prototype.setYear = function(y) {
  this.year = y;
}

HTMLEdit.prototype.designInput = function(doc, name, value) {
  var edtType = 'text';
  if (this.password)
    edtType = 'password';

  if (IE) {
    this.input = document.createElement('<input type="'+edtType+'" '+(this.readonly?'readonly':'')+'>');
  } else {
    this.input = document.createElement("input");
    this.input.type = edtType;
  }

  this.input.className = 'edit';

  if (!name)
    this.input.name  = 'WFRInput'+this.code;
  else
    this.input.name  = name;

  if (this.maxlength) this.input.maxLength = this.maxlength;
  this.input.style.height = this.height;
  this.input.style.width = this.width;
  if (this.align) this.input.style.textAlign = this.align;
  if (!value)
    this.input.value = this.value;
  else
    this.input.value = value;
}

HTMLEdit.prototype.designReport = function() {
  var labelReport = new HTMLLabel(this.sys, this.formID, this.code, 0, 0, this.width, this.height, this.value);
  labelReport.id  = "ReportLabel" + this.code;
  labelReport.design(this.context, false);
}

HTMLEdit.prototype.designComponent = function(doc) {
  if (this.beforeComponentDesign) {
    this.beforeComponentDesign(doc);
  }

  // Testa se é relatório
  if (this.report) {
    this.designReport();
    return "";
  }

  this.designInput(doc);
  this.context.appendChild(this.input);

  if (this.type == 2 && !this.readonly) {
    var img = new ImageObject();
    var x = this.width+1;
    if (this.toGrid) x = this.width - 17;
    this.btdiv = this.getDiv('WFRComponentDate'+this.code, x, ((this.height/2)-8)+(IE?1:0), 16, 16, 1, true);
    this.img = img.getImage(skin+'date.gif', '', this.getAction('openDate'));
    this.onF5press = this.getAction('openDate');
    this.btdiv.appendChild(this.img);
    this.context.appendChild(this.btdiv);
  }
  
  if (this.afterComponentDesign) {
    this.afterComponentDesign(doc);
  }
}

HTMLEdit.prototype.setReadOnly = function(v) {
  this.readonly = v;
  if (this.input) {
    if (this.readonly) this.input.setAttribute('readonly', 'readonly');
    if (this.readonly && !this.bgColor) {
      this.input.style.backgroundColor = '#FFFFE1';
    }
  }
  if (this.btdiv) visibleDiv(this.btdiv, !this.readonly);
}

HTMLEdit.prototype.setEnabled = function(v) {
  this.enabled = v;
  this.setReadOnlyDiv(!v);
  if (this.btdiv) visibleDiv(this.btdiv, this.enabled);
  if (!this.enabled) {
    this.input.style.backgroundColor = '#CCCCCC';
  } else {
    this.input.style.backgroundColor = this.bgColor || '#FFFFFF';
  }
}

HTMLEdit.prototype.openDate = function() {
  var x = tempX;
  var y = tempY+20;
  if (x+250 > screen.width)
    x = screen.width - 260;
  if (y+150 > screen.height-70)
    y = screen.height - 220;

  MM_openBrWindow('getdate?sys='+this.sys+'&month='+this.realMonth+'&year='+this.year+'&field='+this.input.name,'WFRDate','toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=250,height=150,top='+y+',left='+x);
}

HTMLEdit.prototype.beforeSubmit = function() {
  if (this.textMask && this.input) {
    if (this.textMask == "U>") {
      this.input.value = this.input.value.toUpperCase();
    } else if (this.textMask == "l>") {
      this.input.value = this.input.value.toLowerCase();
    }
  }
}