﻿var _Validators =
{
    is_email: function(email) {
        var supported = 0;
        var result = false;

        if (window.RegExp) {
            var tempStr = "a";
            var tempReg = new RegExp(tempStr);
            if (tempReg.test(tempStr)) supported = 1;
        }

        if (!supported)
            result = (email.indexOf(".") > 2) && (email.indexOf("@") > 0);
        else {
            var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
            var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
            result = (!r1.test(email) && r2.test(email));
        }

        return (result);
    }

    , is_vat: function(value) {
        var valid = false;

        if (value.match(/[^0-9]/g) > 0 || value.length != 9)
            valid = false;
        else {
            var total = 0;

            for (var i = 0; i < 8; i++)
                total += value.substr(i, 1) * (1 << (9 - i - 1));

            valid = true;

            if (!(((total % 11) % 10) == value.substr(9 - 1)) || (value == "000000000"))
                valid = false;
        }

        return (valid);
    }

    , is_identity: function(value) {
        var valid = false;

        var r = new RegExp(/^[Α-Ω]{1,2}[0-9]{6}$/);

        valid = r.test(value.toUpperCase());

        return (valid);
    }

    , customValidators: {}
};


_Validators.customValidators =
{
    validateEmail: function(sender, args) {
        args.IsValid = _Validators.is_email(args.Value);
    }

	, validateVAT: function(sender, args) {
	    args.IsValid = _Validators.is_vat(args.Value);
	}

	, validateIdentity: function(sender, args) {
	    args.IsValid = _Validators.is_identity(args.Value);
	}
}
