﻿var EmailRegex = /^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
function checkValidity(FocusOnFails) {
    for (var i = 0; i < Alertes.length; i++) {
        var Field = MandatoryFields[i];
        var Alerte = Alertes[i];
        if (Alerte == null | (CbContact.checked | CbInfos.checked)) {
            if (Field.value.replace(/^\s+/, '').replace(/\s+$/, '').length == 0 || 
               (Field == Email && !EmailRegex.test(Field.value.replace(/^\s+/, '').replace(/\s+$/, '')))) {
                Field.onkeyup = function() { checkValidity(false) };
                if (FocusOnFails) {
                    Field.className = 'bad';
                    Field.focus();
                }
                return false;
            }
            else {
                Field.onkeyup = null;
                Field.className = '';
            }
        }
    }
    return true;
}

function checkMandatoryFields() {
    var IsAlertVisible = (CbContact.checked | CbInfos.checked);
    for (var i = 0; i < Alertes.length; i++) {
        var Alerte = Alertes[i];
        if (Alerte != null) {
            Alertes[i].style.display = IsAlertVisible ? '' : 'none';
            if (!IsAlertVisible) MandatoryFields[i].className = '';
        }
    }
}