function addNewlines(str) {
    var result = '';
    while (str.length > 0) {
        result += str.substring(0, 176) + '\n';
        str = str.substring(176);
    }
    return result;
}

var keynum, lines = 1,
    limitNum = 22;

function limitText(obj) {
    if (obj.value.length > limitNum) {
        obj.value = obj.value.substring(0, limitNum);
    }
}


function limitLines(obj, e) {
    // IE
    if (window.event) {
        keynum = e.keyCode;
        // Netscape/Firefox/Opera
    } else if (e.which) {
        keynum = e.which;
    }

    if (keynum == 13) {
        if (lines == obj.rows) {
            return false;
        } else {
            lines++;
        }

        if (obj.value.length > 176) {
            alert("De persoonlijke boodschap kan maximaal 176 tekens bevatten. (8 regels x 22 karakters per regel)");
            return false;
        } else {
            return true;
        }
    }
}

function isNumberKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57)) return false;

    return true;
}

function createTextAreaWithLines(id) {
    var el = document.createElement('TEXTAREA');
    var ta = document.getElementById(id);
    var string = '';
    for (var no = 1; no < 300; no++) {
        if (string.length > 0) string += '\n';
        string += no;
    }
    el.className = 'textAreaWithLines';
    el.style.height = (ta.offsetHeight - 3) + "px";
    el.style.width = "25px";
    el.style.position = "absolute";
    el.style.overflow = 'hidden';
    el.style.textAlign = 'right';
    el.style.paddingRight = '0.2em';
    el.innerHTML = string; //Firefox renders \n linebreak
    el.innerText = string; //IE6 renders \n line break
    el.style.zIndex = 0;
    ta.style.zIndex = 1;
    ta.style.position = "relative";
    ta.parentNode.insertBefore(el, ta.nextSibling);
    setLine();
    ta.focus();

    ta.onkeydown = function () {
        setLine();
    }
    ta.onmousedown = function () {
        setLine();
    }
    ta.onscroll = function () {
        setLine();
    }
    ta.onblur = function () {
        setLine();
    }
    ta.onfocus = function () {
        setLine();
    }
    ta.onmouseover = function () {
        setLine();
    }
    ta.onmouseup = function () {
        setLine();
    }

    function setLine() {
        el.scrollTop = ta.scrollTop;
        el.style.top = (ta.offsetTop) + "px";
        el.style.left = (ta.offsetLeft - 27) + "px";
    }
}
