﻿if (!SIGI) throw ("SIGI.ComboBox depends on the following scripts. Please make sure these are included.\nSIGI.js");

// === classes ===
SIGI.TextArea = function() {
    this._initialDisplay = "";
    this._SHIFT = false; //code 16
    this._CTRL = false;  //code 17
    this._ALT = false;  //code 18
};
SIGI.TextArea.inherits(SIGI.UI.Control);

SIGI.TextArea.prototype.get_text = function() {
    return this.get_element().value;
};
SIGI.TextArea.prototype.set_text = function(pValue) {
    this.get_element().value = pValue;
};
SIGI.TextArea.prototype.get_maxLength = function() {
    var iReturn = this.get_element().getAttribute("maxlength");
    if (!iReturn)
        iReturn = -1;
    return SIGI.Convert.toInteger(iReturn);
};
SIGI.TextArea.prototype.set_maxLength = function(pValue) {
    if (SIGI.Convert.toInteger(pValue) > 0) {
        pValue = SIGI.Convert.toInteger(pValue) + 1;
    }
    this.get_element().setAttribute("maxlength", pValue)
    this.get_element().setAttribute("maxlen", pValue)
};
SIGI.TextArea.prototype.get_enabled = function() {
    return !this.get_element().disabled;
};
SIGI.TextArea.prototype.set_enabled = function(pValue) {
    SIGI.changeState(this.get_element(), !pValue);
    if (this.get_element().simpleValidator) {
        //document.getElementById(this.get_element().simpleValidator).style.display = this.get_element().style.display;
        document.getElementById(this.get_element().simpleValidator).style.display = "none";
    }
};
SIGI.TextArea.prototype.get_allowKeys = function() {
    return this.get_element().getAttribute("allowKeys");
};
SIGI.TextArea.prototype.set_allowKeys = function(pValue) {
    this.get_element().setAttribute("allowKeys", pValue)
};
SIGI.TextArea.prototype.get_toolTip = function() {
    return this.get_element().getAttribute("tooltip");
};
SIGI.TextArea.prototype.set_toolTip = function(pValue) {
    this.get_element().setAttribute("tooltip", pValue);
};
SIGI.TextArea.prototype.get_statusText = function() {
    return this.get_element().getAttribute("statustext");
};
SIGI.TextArea.prototype.set_statusText = function(pValue) {
    this.get_element().setAttribute("statustext", pValue);
};
SIGI.TextArea.prototype.get_visible = function() {
    return this.get_element().getAttribute("helpID");
};
SIGI.TextArea.prototype.set_visible = function(pValue) {
    if (pValue) {
        if (this._initialDisplay.toLowerCase() == "none") {
            this.get_element().style.display = "block";
        }
        else {
            this.get_element().style.display = this._initialDisplay;
        }
    }
    else {
        this.get_element().style.display = "none";
    }
    if (this.get_element().simpleValidator) {
        //document.getElementById(this.get_element().simpleValidator).style.display = this.get_element().style.display;
        document.getElementById(this.get_element().simpleValidator).style.display = "none";
    }
    if (this.get_element().label) {
        if (SIGI.Validation.hideLabelWithElement(this.get_element())) {
            try {
                this.get_element().label.style.display = this.get_element().style.display;
                this.get_element().label.className = "FormLabel";
            }
            catch (e) {
                document.getElementById(this.get_element().label).style.display = this.get_element().style.display;
                document.getElementById(this.get_element().label).className = "FormLabel";
            } 
        }
    }
};

SIGI.TextArea.prototype._onChange = function(e) {
    this._enforceMaxLength();
}

SIGI.TextArea.prototype._onKeyUp = function(e) {
    this.get_element().innerText += window.event.keyCode + " ";
}
SIGI.TextArea.prototype._enforceMaxLength = function(e) {
    //Removing this - no need to set overstrike  mode
    //if ((ie) && (document)) document.execCommand("OverWrite", false, true);

    var keychar

    if (e) {
        if (e.keyCode)
            keychar = e.keyCode;
        else
            keychar = e.which;
    }
    else {
        keychar = 0;
    }
    //if some text is selected right now, that text will be overwritten by the keystroke,
    //making it impossible to overflow the box, so only try to prevent the keystroke if nothing is selected

    if ((!e) || ((!document.selection.createRange()) || (document.selection.createRange() && document.selection.createRange().text == ""))) {
        //debugger;
        //checking maxLength against 1, and subtracting 2 in the next part, because set_maxLength adds 1 to the value
        //It's unclear why this is but I don't want to break other apps by changing it, so I'll accomodate it here
        if ((this.get_maxLength() > 1) && (this.get_element().value.length > (this.get_maxLength() - 2)) && (keychar != 8) && (keychar != 46)) {
            if (e) {
                if (e.preventDefault) e.preventDefault();
                if (e.stopPropagation) e.stopPropagation();

                if (ie) e.keyCode = 0;
                e.returnValue = false;
                e.cancelBubble = true;
            }
            else {
                var actualMaxLength = (this.get_maxLength() - 1).toString();
                //var msg = "Entry too long, total length cut to maximum size of " + actualMaxLength;
                var msg = "Total number of characters entered exceeds maximum allowed of " + actualMaxLength + ".  Only the first " + actualMaxLength + " characters in your entry will be saved.";
                alert(msg);
                this.get_element().value = this.get_element().value.substring(0, this.get_maxLength() - 1);
            }
        }
    }

};

SIGI.TextArea.prototype.focus = function() {
    SIGI.setFocus(this.get_element());
};
SIGI.TextArea.prototype.initialize = function() {
    this._initialDisplay = this.get_element().style.display;
    this.set_maxLength(this.get_maxLength());

    SIGI.attachEvent(this.get_element(), "onkeypress", this.context(this._enforceMaxLength));
    SIGI.attachEvent(this.get_element(), "onchange", this.context(this._onChange));
};

