﻿
//Zinicializuje oEditText ---------------------------------------------------------------------------------------------------------
function Initialize_oEditText(inputID, MaxLength, TextAreaWarrningClass, TextAreaDisabledClass, WarrningLength)
{
    //Init	    
    var input = new Object();
    
    input.MainInput = document.getElementById(inputID);
   
    input.MainInput.maxTxtLength = MaxLength * 1;
    input.MainInput.TextAreaWarrningClass = TextAreaWarrningClass;
    input.MainInput.TextAreaDisabledClass = TextAreaDisabledClass;
    input.MainInput.WarrningLength = WarrningLength;
    
    var lblName = inputID.replace("e", "l");
    input.MainInput.Label = document.getElementById(lblName);
    if (input.MainInput.Label != undefined && input.MainInput.rows > 0) {
        input.MainInput.Label.innerHTML = input.MainInput.Label.innerHTML.substr(0, input.MainInput.Label.innerHTML.lastIndexOf(":"));
        input.MainInput.LabelLength = input.MainInput.Label.innerHTML.length;
        var tempStr = input.MainInput.maxTxtLength + "";
        input.MainInput.LabelValueLength = tempStr.length;  
        input.MainInput.Label.innerHTML += " (" + input.MainInput.maxTxtLength + "):";
    }

    input.MainInput.KeyDown.AddHandler(function(e) { oEditText_OnKeyDown(e, inputID); return true; });
    input.MainInput.KeyPress.AddHandler(function (e) { oEditText_OnKeyPress(e, inputID); return true; });
    
    oEdiText_WatchText(inputID);
    return true;
}

function oEditText_OnKeyDown(e, ID)
{
    var code = "oEdiText_WatchText('" + ID + "')";
    setTimeout(code, 100);

}

function oEditText_OnKeyPress(e, ID) {
    if (e.keyCode == 13) {
        var code = "oEdiText_WatchText('" + ID + "')";
        setTimeout(code, 100);
    }
}

function oEdiText_WatchText(ID)
{
    var input = document.getElementById(ID);
    if (input.maxTxtLength > 0 && input.maxTxtLength < 10000) {
        var indexWarr = index = input.className.search(input.TextAreaWarrningClass);
        var indexDisabled = index = input.className.search(input.TextAreaDisabledClass);
        //alert(input.className);
        if ((input.value.length > input.maxTxtLength - input.WarrningLength) && input.rows > 0) {
            if (indexWarr == -1) {
                input.className += " " + input.TextAreaWarrningClass;
            }
        } else if (indexWarr != -1) {
            input.className = input.className.substr(0, indexWarr - 1);
        }

        if (input.maxTxtLength <= input.value.length && input.rows > 0) {
            input.value = input.value.substring(0, input.maxTxtLength);
            if (indexDisabled == -1) {
                input.className += " " + input.TextAreaDisabledClass;
            }
        } else if (indexDisabled != -1) {
            input.className = input.className.substr(0, indexDisabled - 1);
        }
        if (input.Label != undefined && input.rows > 0) {
            var zbytek = input.maxTxtLength - input.value.length;
            var zbytekStr = zbytek + "";
            var numberSetChar = input.LabelValueLength - zbytekStr.length;
            var setChars = "";
            while (numberSetChar > 0) {
                setChars += "&nbsp;";
                numberSetChar--;
            }
            zbytek = ((setChars != "") ? setChars + zbytek : zbytek);
            input.Label.innerHTML = input.Label.innerHTML.substr(0, input.LabelLength) + " (" + zbytek + "):";
        }
    } else {
        input.Label.innerHTML = input.Label.innerHTML.substr(0, input.LabelLength) + ":";   
    }
    
    
}
