﻿function Validator(attr) {
    this.name = ((attr && attr.name) ? attr.name : "");
    this.errorMessage = ((attr && attr.errorMessage) ? attr.errorMessage : "");
    this.validationGroup = ((attr && attr.validationGroup) ? attr.validationGroup : "");
}
Validator.validate = function(validators, validationGroup) {
    var result = { errors: [], isValid: true };
    for (var num = 0; num < validators.length; num++) {
        var validator = validators[num];
        if ((validator instanceof Validator) && (!validationGroup || (validationGroup == validator.validationGroup))) {
            if (validator.evaluateIsValid && !validator.evaluateIsValid()) {
                result.isValid = false;
                result.errors.push({ name: validator.name, errorMessage: validator.errorMessage });
            }
        }
    }
    return result;
};
Validator.compare = function(op1, op2, operator) {
    switch (operator) {
        case "!=":
            return (op1 != op2);
        case ">":
            return (op1 > op2);
        case ">=":
            return (op1 >= op2);
        case "<":
            return (op1 < op2);
        case "<=":
            return (op1 <= op2);
    }
    return (op1 == op2);
};
function CompareValidator(attr) {
    if (attr === undefined || attr === null) {
        throw new Error("Parameter 'attr' cannot be null.");
    }
    Validator.call(this, attr);
    this.value = attr.value;
    this.operator = (attr.operator || "=");
    this.valueToCompare = attr.valueToCompare;
}
CompareValidator.prototype = new Validator();
CompareValidator.prototype.evaluateIsValid = function() {
    return Validator.compare(this.value, this.valueToCompare, this.operator);
};
function RangeValidator(attr) {
    if (attr === undefined || attr === null) {
        throw new Error("Parameter 'attr' cannot be null.");
    }
    Validator.call(this, attr);
    this.value = attr.value;
    this.maximumValue = attr.maximumValue;
    this.minimumValue = attr.minimumValue;
}
RangeValidator.prototype = new Validator();
RangeValidator.prototype.evaluateIsValid = function() {
    return (Validator.compare(this.value, this.minimumValue, ">=") && Validator.compare(this.value, this.maximumValue, "<="));
};
function RegularExpressionValidator(attr) {
    if (attr === undefined || attr === null) {
        throw new Error("Parameter 'attr' cannot be null.");
    }
    Validator.call(this, attr);
    this.value = (attr.value || "");
    this.validationExpression = (attr.validationExpression || "");
}
RegularExpressionValidator.prototype = new Validator();
RegularExpressionValidator.prototype.evaluateIsValid = function() {
    if (!this.value.length) {
        return true;
    }
    var matches = (new RegExp(this.validationExpression)).exec(this.value);
    return ((matches != null) && (this.value == matches[0]));
};
function RequiredFieldValidator(attr) {
    if (attr === undefined || attr === null) {
        throw new Error("Parameter 'attr' cannot be null.");
    }
    Validator.call(this, attr);
    this.value = (attr.value || "");
    this.initialValue = (attr.initialValue || "");
}
RequiredFieldValidator.prototype = new Validator();
RequiredFieldValidator.prototype.evaluateIsValid = function() {
    return (this.value != this.initialValue);
};