function controleerInvoer(frmForm, negeerVelden, controleerMail, txtMail, mailMagLeeg) {
    var gelukt = true;
    txtMail = document.getElementById(txtMail);
    for (i = 0; i < frmForm.length; i++) {
        var object = frmForm.elements[i];
        if ((object.type == 'text' || object.type == 'textarea') && (object.value == '' || trimString(object.value) == '')) {
            var tmpNegeer = '|' + object.id + '|';
            if (negeerVelden.indexOf(tmpNegeer) == -1) {
                gelukt = false;
                break;
            }
        }
    }
    if (gelukt == false) {
        var color = object.parentNode.style.backgroundColor;
        object.onkeydown = function() { this.parentNode.style.backgroundColor = color; };
        object.parentNode.style.backgroundColor = '#FF0000';
        if (object.select) object.select();
        if (object.focus) object.focus();
        return false;
    } else {
        if (controleerMail) {
            return controleerEmail(txtMail, mailMagLeeg);
        } else {
            return true;
        }
    }
}

function controleerEmail(txtMail, mailMagLeeg) {
    if (txtMail.value == '' || trimString(txtMail.value) == '') {
        if (mailMagLeeg) {
            return true;
        } else {
            var color = txtMail.parentNode.style.backgroundColor;
            txtMail.onkeydown = function() { this.parentNode.style.backgroundColor = color; };
            txtMail.parentNode.style.backgroundColor = '#FF0000';
            if (txtMail.select) txtMail.select();
            if (txtMail.focus) txtMail.focus();
            return false;
        }
    } else {
        if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(txtMail.value)) {
            return true;
        } else {
            var color = txtMail.parentNode.style.backgroundColor;
            txtMail.onkeydown = function() { this.parentNode.style.backgroundColor = color; };
            txtMail.parentNode.style.backgroundColor = '#FF 0000';
            if (txtMail.select) txtMail.select();
            if (txtMail.focus) txtMail.focus();
            return false;
        }
    }
}

function trimString(tekst) {
    var tmpLeeg = new String(" \t\n\r");
    var tmpString = new String(tekst);
    if (tmpLeeg.indexOf(tmpString.charAt(0)) != -1) {
        var j = 0;
        var i = tmpString.length;
        while (j < i && tmpLeeg.indexOf(tmpString.charAt(j)) != -1) {
            j++;
        }
        tmpString = tmpString.substring(j, i);
   }
   if (tmpLeeg.indexOf(tmpString.charAt(tmpString.length-1)) != -1) {
        var i = tmpString.length - 1;
        while (i >= 0 && tmpLeeg.indexOf(tmpString.charAt(i)) != -1) {
            i--;
        }
        tmpString = tmpString.substring(0, i + 1);
   }
   return tmpString;
}
