﻿// ### Trim
function trim(value) {
    value = value.replace(/^\s+/, '');
    value = value.replace(/\s+$/, '');
    return value;
}

// ### Left
function Left(str, n) {
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else
        return String(str).substring(0, n);
}

// ### Host controleren.
function IsHTTPS() {
    var _url = location.href;
    if (_url != "") {
        var _temp = Left(_url, 5);

        if (_temp != "https") {
            // Nieuwe pagina laden in een HTTPS omgeving.
            redirectHTTPS();
        }
    }
}

// ### Huidige pagina ophalen.
function redirectHTTPS() {
    var _url = location.href;
    if (_url != "") {
        _url = _url.replace("http://", "https://");

        // Redirect.
        location.href = _url;
    }
}

// Datum in de toekomst maximaal een jaar.
function ValidateDateFutureJaar(source, clientside_arguments) {
    var bValid = true;
    if (clientside_arguments.Value != '') {
        var datum = clientside_arguments.Value.split("-");
        var dag = datum[0];
        var maand = datum[1];
        var jaar = datum[2];
        invoer = new Date(jaar, maand * 1 - 1, dag);
        jaarlater = Date.today().next().year();
        if (datum.length == 3) {
            if (invoer > jaarlater) {
                bValid = false;
            }
        }
    }
    clientside_arguments.IsValid = bValid;
}

// Datum in de toekomst.
function ValidateDateFuture(source, clientside_arguments) {
    var bValid = true;
    if (clientside_arguments.Value != '') {
        var datum = clientside_arguments.Value.split("-");
        var dag = datum[0];
        var maand = datum[1];
        var jaar = datum[2];
        //c_now = new Date(jaar, maand * 1 - 1, dag);
        c_now = jaar + langecijfer(maand) + langecijfer(dag);
        now = new Date();
        if (datum.length == 3) {
            if (c_now < now.toString("yyyyMMdd")) {
                bValid = false;
            }
        }
    }
    clientside_arguments.IsValid = bValid;
}

/**
*  Make all characters uppercase.
*  skyliner -> SKYLINER
*/
function MakeCharactersUppercase(element) {
    if (element != null && element.value != '') {
        var new_string = "";
        for (i = 0; i < element.value.length; i++) {
            new_string += element.value.substr(i, 1).toUpperCase();
        }
        element.value = new_string;
    }
}

// Elf proef.
function ValidateElfProef(source, client_arguments) {
    var bValid = false;
    if (client_arguments.Value != '') {
        if (client_arguments.Value.length == 9) {
            nummer = 0;
            for (i = 0; i < 9; i++) {
                nummer = nummer + parseInt(client_arguments.Value.substr(i, 1)) * (9 - i);
            }
            nummer = nummer % 11;
            bValid = (nummer != 0) ? false : true;
        } else {
            if (client_arguments.Value.length >= 3 && client_arguments.Value.length <= 7) {
                bValid = true;
            } else {
                bValid = false;
            }
        }
    }
    client_arguments.IsValid = bValid;
}

// Alleen numerieke waarden toestaan als input.
function OnlyNumeric(e) {
    var charCode = (e.which) ? e.which : event.keyCode;
    return (charCode >= 48 && charCode <= 57) ? true : false;
}

// Wanneer er op enter gedrukt wordt dan het formulier verzenden.
function HandleKeyPressWithPostback(e, ctrl, key) {
    var charCode = (e.which) ? e.which : event.keyCode;
    if (charCode == key) {
        // Control ophalen waarop we het postback event willen doen.
        c = document.getElementById(ctrl);
        if (ctrl != null) {
            __doPostBack(c.name, null);
            return false;
        }
    }
    return true;
}

/** 
*  Validate Age.
*  Check if the birthdate not older then 75.
*/
function ValidateDatePast(source, clientside_arguments) {
    var bValid = true;
    if (clientside_arguments.Value != '') {
        // Datum maken.
        var datum = clientside_arguments.Value.split("-");
        var dag = datum[0];
        var maand = datum[1];
        var jaar = datum[2];
        now = new Date()
        if (datum.length == 3) {
            born = new Date(jaar, maand * 1 - 1, dag);
            years = Math.floor((now.getTime() - born.getTime()) / (365.25 * 24 * 60 * 60 * 1000));
            if (years > 75) {
                bValid = false;
            }
        }
    }
    clientside_arguments.IsValid = bValid;
}

/** 
*  Validate Age.
*  Check if the birthdate not younger then 18.
*/
function ValidateDateEighteen(source, clientside_arguments) {
    var bValid = true;
    if (clientside_arguments.Value != '') {
        // Datum maken.
        var datum = clientside_arguments.Value.split("-");
        var dag = datum[0];
        var maand = datum[1];
        var jaar = datum[2];
        now = new Date()
        if (datum.length == 3) {
            born = new Date(jaar, maand * 1 - 1, dag);
            years = Math.floor((now.getTime() - born.getTime()) / (365.25 * 24 * 60 * 60 * 1000));
            if (years < 18) {
                bValid = false;
            }
        }
    }
    clientside_arguments.IsValid = bValid;
}

function langecijfer(cijfer) {
    if (cijfer.length < 2 && cijfer < 10) {
        cijfer = "0" + cijfer;
    }
    return cijfer;
}
