﻿function ValidatorUpdateDisplay(val) {
    var ctrlId = val.controltovalidate;
    if (!ctrlId) {
        ctrlId = val.getAttribute('controlid');
    }

    var flag = 0;
    var idx = 1;
    for (var i = 0; i < Page_Validators.length; i++) {
        if ((!Page_Validators[i].controltovalidate) || (Page_Validators[i].controltovalidate == ctrlId)) {
            idx++;
        }
        if (Page_Validators[i] == val) {
            flag = Math.pow(2, idx);
            break;
        }
    }

    var ctrl = document.getElementById(ctrlId);
    if (ctrl) {
        ValidatorStyleControl(val.isvalid, ctrl, flag);
    }
    else {
        var ctrls = document.getElementsByName(ctrlId);
        for (var i = 0; i < ctrls.length; i++) {
            ValidatorStyleControl(val.isvalid, ctrls[i], flag);
        }
    }
}

function ValidatorStyleControl(isvalid, ctrl, flg) {
    var state = ctrl.getAttribute('validationState');
    if (!state) {
        state = 0;
    }
    if (isvalid) {
        if ((state & flg) == flg) {
            state = state ^ flg;
            if (state == 0) {
                ctrl.className = ctrl.className.replace(' validationerror', '');
            }
        }
    }
    else {
        state |= flg;
        if (ctrl.className.indexOf(' validationerror') < 0) {
            ctrl.className += ' validationerror';
        }
    }

    ctrl.setAttribute('validationState', state);
}
