function HTMLMemo(sys, formID, code, posX, posY, width, height, description, value) {
  this.create(sys, formID, code, posX, posY, width, height, description, value);
  this.type = 1;
  this.report = false;
  this.wrap = false;
}

HTMLMemo.inherits(HTMLEdit);

HTMLMemo.prototype.name = 'HTMLMemo';
HTMLMemo.prototype.tabable = true;
HTMLMemo.prototype.tagName = 'edit';
HTMLMemo.prototype.tabKeys = [9];

HTMLMemo.prototype.designReport = function() {
  var div = this.getDiv('WFRMemo'+this.code, this.posX, this.posY, this.width+(IE?2:0), this.height+(IE?2:0), 3, true);
  div.id  = "ReportMemo" + this.code;
  div.innerHTML = this.value;

  var mainDiv = MM_findObj("lay");
  mainDiv.appendChild(div);
}

HTMLMemo.prototype.setReadOnly = function(v) {
  if (this.isRichText()) {
	this.readonly = v;
    visibleDiv(this.readonlyDiv, this.readonly);
  } else {
    this.callMethod(HTMLEdit, "setReadOnly", v);
  }
}

HTMLMemo.prototype.designInput = function(doc, name, value) {  
  if (IE) {
    this.input = document.createElement('<textarea '+(this.readonly?'readonly':'')+'>');
  } else {
    this.input = document.createElement("TEXTAREA");
  }

  this.input.className = 'edit';
  this.input.setAttribute('wrap', (this.wrap) ? 'on':'off');
  
  if (!name) {
    this.input.name  = 'WFRInput'+this.code;
    this.input.id  = 'WFRInput'+this.code;
  } else {
    this.input.name  = name;
    this.input.id  = name;
  }
  
  this.input.style.height = this.height;
  this.input.style.width = this.width;
  if (!value) {
    this.input.value = this.value;
  } else {
    this.input.value = value;
  }
}

HTMLMemo.prototype.beforeComponentDesign = function(doc) { 
}

HTMLMemo.prototype.design = function(doc, tabControl) {
  this.doc = doc;
  if (this.description != '') {
    this.div      = this.getDiv('WFRComponent'+this.code, this.posX, this.posY, this.width, this.height+this.labelDivHeight, this.zindex, !this.isRichText());
    this.labeldiv = this.getDiv('WFRComponentLabel'+this.code, 0, -1*this.labelDivHeight, 0, this.labelDivHeight, 2, !this.isRichText());

    this.label = document.createElement("font");
    this.label.className = 'label';
    this.label.innerHTML = this.decorateRequired(this.description.replace(/\s/g, '&nbsp;'), this.required);
    this.label.style.whiteSpace = "nowrap";
    this.label.style.cursor = 'default';
    this.label.onmousedown = function() { return false; };
    this.label.onselectstart = function() { return false; };

    this.labeldiv.appendChild(this.label);
    this.context = this.getDiv('WFRComponentContext'+this.code, 0, 0, this.width, this.height, 2, true);
  } else
    this.div = this.getDiv('WFRComponent'+this.code, this.posX, this.posY, this.width, this.height, this.zindex, !this.isRichText());

  if (this.labeldiv) this.div.appendChild(this.labeldiv);
  if (this.context)
    this.div.appendChild(this.context);
  else
    this.context = this.div;

  this.designComponent(doc);
  doc.appendChild(this.div);

  if (this.label) {
    this.label.draggable = false;
    this.label.resizable = false;
  }

  this.init(tabControl);
}

HTMLMemo.prototype.afterComponentDesign = function(doc) {
  if (this.isRichText()) {
    this.timeout(this.richTextLoad, 100);
    
    var minWidth = (this.richText == '1' ? 224 : 432);
    var minHeight = (this.richText == '1' ? 127 : 156);
    
    var readonlyWidth;
    if (this.width <= minWidth) {
      readonlyWidth = minWidth;
    } else {
      readonlyWidth = (this.width + (IE ? 2 : 0) + 1);
    }
    
    var readonlyHeight;
    if (this.height <= minHeight) {
      readonlyHeight = minHeight;
    } else {
      readonlyHeight = (this.height + (IE ? 2 : 0) + 10);
    }
    
    this.readonlyDiv = this.getDiv('ReadonlyDiv' + this.code, 0, 0, readonlyWidth, readonlyHeight, this.zindex + 1, false);
    this.readonlyDiv.innerHTML = "<table width=100% height=100%><tr><td>&nbsp;</td</tr></table>";
    this.context.appendChild(this.readonlyDiv);
    visibleDiv(this.readonlyDiv, this.readonly);
  }
}

HTMLMemo.prototype.validateDataType = function(focus) {
	var r = this.callMethod(HTMLElementBase, "validateDataType", focus);

	if (r && this.maxlength) {
		if (this.getValue().length > this.maxlength) {
			interactionError(getLocaleMessage("INFO.MEMO_MAXIMUM_CHARACTERS",(this.description ? this.description : this.id),this.maxlength), focus ? (function(e) {e.getFocus(true);}) : null , focus ? [this] : null);
			return false;
		}
	}
	
  return r;
}

HTMLMemo.prototype.keyupAction = function(e){
  var r = this.callMethod(HTMLElementBase, "keyupAction", e);
  
  if (r && this.maxlength) {
    var value = this.getValue();
    var valueSizeLimited = this.defineTextByLength(e);
    if (value != valueSizeLimited) {
  	  this.setValue(valueSizeLimited);
    }
  }
}

HTMLMemo.prototype.defineTextByLength = function(e) { 
  var value = this.getValue();
  
  if (value.length > this.maxlength) {
    value = value.substring(0, this.maxlength);
  }
  
  return value;
}

HTMLMemo.prototype.richTextLoad = function () {
  window.tinyMCE_GZ = {loaded: true};
  if(resources_locale == "pt_BR") {
    webrun.include("components/tiny_mce/tiny_mce_src_pt_BR.js");
  } else {
    webrun.include("components/tiny_mce/tiny_mce_src.js");
  }
    
  tinymce.documentBaseURL = window.location.href.replace(/[\?#].*$/,  '').replace(/[\/\\][^\/]+$/, '');

  if (!/[\/\\]$/.test(tinymce.documentBaseURL))
    tinymce.documentBaseURL += '/';

  tinymce.documentBaseURL += 'components/tiny_mce/' ; 
  tinymce.baseURL = new tinymce.util.URI(tinymce.documentBaseURL).toAbsolute(tinymce.documentBaseURL);
  tinymce.EditorManager.baseURI = new tinymce.util.URI(tinymce.baseURL);
  tinymce.dom.Event.domLoaded = true;
    
  if (this.richText == '1') {
    tinyMCE.init({ mode : "none", theme : "simple" });
  } 
  
  if (this.richText == '2') {   
    tinyMCE.init({ mode : "none", theme : "advanced",
    theme_advanced_buttons1_add : ",|,bullist,numlist", 
    theme_advanced_buttons2_add : ",|,preview,hr,removeformat,visualaid,|,sub,sup,|,charmap",
    theme_advanced_buttons3 : "",
    theme_advanced_toolbar_location : "top", theme_advanced_disable : "help"
    });
  }
  
  tinyMCE.execCommand('mceAddControl', true, this.input.name);

  timeout(visibleDiv, 200, [this.div, this.visible]);

  if (this.labeldiv) {
    this.timeout(function() {
      this.createLabelDiv();
      this.div.appendChild(this.labeldiv);;
    }, 200);
  }  
}

HTMLMemo.prototype.focus = function() {
  var r = this.enabled && this.visible && !this.readonly && !this.resizable && !this.draggable;
  if (this.doc) r = r && isVisibleDiv(this.doc);
  
  if (r) {
    // O memo, quando definido como RichText, tem um comportamento diferente para focar no componente.
    if ((this instanceof HTMLMemo) && this.isRichText()) {
      var richTextObj = tinyMCE.get(this.input.name);
      if (richTextObj && richTextObj.focus) {
        richTextObj.focus();
        this.focuzed = true;
      }
    } else if (this.input) {
      this.input.focus();
      this.focuzed = true;
    }
  }
  
  return r;
}

HTMLMemo.prototype.setValue = function(value, checkDependences) {
  this.callMethod(HTMLElementBase, "setValue", value, checkDependences);
   
  if (this.isRichText()) { 
    this.setRichTextValue(value);    
  }
}

HTMLMemo.prototype.setRichTextValue = function(value) {
	if (tinyMCE.get(this.input.name)) {
    tinyMCE.get(this.input.name).setContent(value);
	}
}

HTMLMemo.prototype.getValue = function() { 
  if (this.isRichText()) {
    return this.getRichTextValue();
  } else {
    return this.input.value;
  }
}

HTMLMemo.prototype.getRichTextValue = function() {
	if (tinyMCE.get(this.input.name)) {
	  return tinyMCE.get(this.input.name).getContent();
	} else {
		return null;
	}
}

HTMLMemo.prototype.isRichText = function() {
  return (this.richText == "1" || this.richText == "2");
}