﻿/*
* This is de example for how the 
* mandatory fields can be checked in a simple way.
* the method returns a boolean
 
function checkMandatoryFields() {
var result = true;

result = validateTextField($get("<%= Field-1 %>")) && result;
result = validateEmailField($get("<%= Field-2 %>")) && result;

return result;
}

*/


var CONST_ERRORCSS = 'inputError';
var CONST_OKCSS = 'input';


// Validate if a mandatory textfield is filled
function validateTextField(which) {
    var value = new String(which.value);
    which.className = CONST_OKCSS;
    
    if (value.trim() != "")
        return true;

    which.className = CONST_ERRORCSS;
    return false;
}

// validate if an email address is valid
function validateEmailField(which, dontChangeCssClass) {
    var value = new String(which.value);
    which.className = (dontChangeCssClass) ? which.className : CONST_OKCSS;

    var reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (reg.test(value))
        return true;

    which.className = (dontChangeCssClass) ? which.className : CONST_ERRORCSS;
    return false;
}


function validateMinimalAge(birthDate, minimalAgeInYears) {
    var today = new Date;
    var theirDate = new Date((birthDate.getFullYear() + minimalAgeInYears), birthDate.getMonth(), birthDate.getDate());
    if ((today.getTime() - theirDate.getTime()) < 0) {
        return false;
    }
    return true;
}
// Add a function to the string object to be able to 
// trim the spaces on the left and right side.
String.prototype.trim = function() {
    return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}


function validateIntegerField(which, minimalValue) {
    which.className = CONST_OKCSS;

    var value = new String(which.value);
    if (value.trim() != "") {
        var val = parseInt(value);
        if (val >= minimalValue)
            return true;
    }

    which.className = CONST_ERRORCSS;
    return false;
}

function validatePostalCode(which) {
    which.className = CONST_OKCSS;

    var regEx = /^[0-9]{4}[a-zA-Z]{2}$/;
    var value = new String(which.value);
    if (value.match(regEx)) {
        return true;
    }


    which.className = CONST_ERRORCSS;
    return false;
}

function validateEmailFields(email1, email2) {
    if (!validateEmailField(email1))
        return false;
        
    email2.className = CONST_OKCSS;

    if (email1.value == email2.value) {
        return true;
    }

    email2.className = CONST_ERRORCSS;
    return false;
}

function bankAccountCheck11Proef(which, whichType) {
    which.className = CONST_OKCSS;
    validRegExp = /\D/;
    var value = which.value;
    if (value.search(validRegExp) >= 0) {
        return false;
    }


    var bankaccount = which.value;
    var length = bankaccount.length;
    if (length != 9) {
        if ((whichType.value.toLowerCase() == "p") && (length < 8))
            return true;

        which.className = CONST_ERRORCSS;
        return false;
    }

    var total = 0;
    for (var i = 0; i < length; i++) {
        var n = bankaccount.charAt(i);
        total += parseInt(n) * (9 - i);
    }

    var result = ((total % 11) == 0);

    which.className = result ? CONST_OKCSS : CONST_ERRORCSS;
    return result;
}

function validateDateField(which) {
    which.className = CONST_OKCSS;
    var val = which.value;
    var result = false;
    var regEx = /(\d{1,2})\D?(\d{1,2})\D?(\d{4})/;
    if ((val.match(regEx) != null) && (val.match(regEx).length == 4)) {
        var day = makeInt(val.match(regEx)[1]);
        var month = makeInt(val.match(regEx)[2]) - 1;
        var year = parseInt(val.match(regEx)[3]);
        var date = new Date(year, month, day);

        result = ((day == date.getDate()) && (month == date.getMonth()) && (year == date.getFullYear()));
        if (result) {
            which.value = day + "-" + (month+1) + "-" + year;
        }
    }

    which.className = result ? CONST_OKCSS : CONST_ERRORCSS;
    return result;
}

function makeInt(str) {
    var len = new String(str).length;
    var newStr = new String(str);
    while ((len > 0) && (newStr.substr(0, 1) == "0")) {
        newStr = newStr.substr(1, len - 1);
    }
    var result = parseInt(newStr);
    return result;
}


