﻿if (typeof String.prototype.ElfCheck !== 'function') {
    //validates dutch bank account numbers (except ING) usign the 11-check.
    String.prototype.ElfCheck = function () {
        var arr = this.replace(/\D/g, '').split('');
        var result = 0;
        if (arr.length == 9) { arr.splice(0, 0, 0) }
        for (var z = 0; z < arr.length; z++) {
            result += (arr[z] * (z * 1 + 1));
        }
        result = (result / 11);
        return (Math.floor(result) == result);
    };
}

if (typeof String.prototype.padZero !== 'function') {
    //pads the number to the specified length/
    //useful for dates etc. where a fixed length is required: 02 vs 2.
    String.prototype.padZero = function (length) {
        if (undefined == length || isNaN(length)) { return this; }
        var returnvar = this.toString();
        while (returnvar.length < length) {
            returnvar = '0' + returnvar;
        }
        return returnvar;
    };
}

if (typeof Date.prototype.isValid !== 'function') {
    //checks if date is valid.
    Date.prototype.isValid = function () {
        var d = this.getDate();
        var m = this.getMonth();
        var y = this.getFullYear();
        if (d > 30) {
            if (m == 2 || m == 4 || m == 6 || m == 9 || m == 11) {
                return false;
            } else if (d < 32) {
                return true;
            }
        } else if (m == 2 && d == 29) {
            if (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)) {
                return true;
            }
        } else if (m == 2 && d > 29) {
            return false;
        } else {
            return true;
        }
        return false;
    };
}

function ArrayDiff(array1, array2) {
    //This function takes an array as argument
    //goal:     return the differences between the two arrays.
    //note:     make sure array is unique.
    //remarks:  alters original arrays. To prvent this, call this function with array.slice();
    if (!array1 || undefined == array1) {
        return array2;
    } else if (!array2 || undefined == array2) {
        return array1;
    }
    var result = [];
    var b = false; //boolean indicator: turns true if a match is found.
    if (array1.length < array2.length) {
        var temp = array1;
        array1 = array2;
        array2 = temp;
    }
    var i = 0;
    while (i < array1.length) {
        b = false;
        var j = 0;
        while (j < array2.length) {
            if (array1[i] == array2[j]) {
                array2.splice(j, 1); //remove the item from array2.
                b = true;
                break;
            } else {
                b = false;
            }
            j++;
        }
        if (!b) {
            result.push(array1[i]);
        }
        i++;
    }
    return result.concat(array2);
}

if (typeof String.prototype.trim !== 'function') {
    String.prototype.trim = function () {
        return this.replace(/^\s+|\s+$/, '');
    };
}


//maakt een array uniek, dubbele waardes worden weggegooid
function ArrayUnique(ar1) {
    var a = [];
    var l = ar1.length;
    for (var i = 0; i < l; i++) {
        for (var j = i + 1; j < l; j++) {
            // If this[i] is found later in the array
            if (ar1[i] === ar1[j]) {
                j = i + 1;
                i = i + 1;
            }
        }
        a.push(ar1[i]);
    }
    return a;
};
