﻿jQuery.ketchup

.validation('toBeAccepted', 'E\'necessario accettare per proseguire.', function(form, el, value) {
    var type = el.attr('type').toLowerCase();
    if (type == 'checkbox' || type == 'radio') {
        return (el.attr('checked') == true);
    } else {
        return (value.length != 0);
    }
})

.validation('required', 'Questo campo è obbligatorio.', function(form, el, value) {
    var type = el.attr('type').toLowerCase();

    if (type == 'checkbox' || type == 'radio') {
        return (el.attr('checked') == true);
    } else {
        return (value.length != 0);
    }
})

.validation('minlength', 'Il campo deve avere una lunghezza minima di {arg1}.', function(form, el, value, min) {
    return (value.length >= +min);
})

.validation('maxlength', 'Il campo deve avere una lunghezza massima di {arg1}.', function(form, el, value, max) {
    return (value.length <= +max);
})

.validation('rangelength', 'Questo campo deve avere una lunghezza compresa tra {arg1} e {arg2}.', function(form, el, value, min, max) {
    return (value.length >= min && value.length <= max);
})

.validation('min', 'Deve essere almeno di {arg1}.', function(form, el, value, min) {
    return (this.isNumber(value) && +value >= +min);
})

.validation('max', 'Non può essere superiore a {arg1}.', function(form, el, value, max) {
    return (this.isNumber(value) && +value <= +max);
})

.validation('range', 'Deve essere compreso tra {arg1} e {arg2}.', function(form, el, value, min, max) {
    return (this.isNumber(value) && +value >= +min && +value <= +max);
})

.validation('number', 'Deve essere un numero.', function(form, el, value) {
    return this.isNumber(value);
})

.validation('digits', 'Deve essere una cifra.', function(form, el, value) {
    return /^\d+$/.test(value);
})

.validation('email', 'Deve essere una e-mail valida.', function(form, el, value) {
    return this.isEmail(value);
})

.validation('url', 'Deve essere un URL valido.', function(form, el, value) {
    return this.isUrl(value);
})

.validation('username', 'Deve essere un nome utente valido.', function(form, el, value) {
    return this.isUsername(value);
})

.validation('match', 'Deve essere {arg1}.', function(form, el, value, word) {
    return (el.val() == word);
})

.validation('contain', 'Deve contenere {arg1}', function(form, el, value, word) {
    return this.contains(value, word);
})

.validation('date', 'Deve essere una data valida.', function(form, el, value) {
    return this.isDate(value);
})

.validation('minselect', 'Selezionare almeno {arg1} opzioni.', function(form, el, value, min) {
    return (min <= this.inputsWithName(form, el).filter(':checked').length);
}, function(form, el) {
    this.bindBrothers(form, el);
})

.validation('maxselect', 'Seleziona non più di {arg1} opzioni.', function(form, el, value, max) {
    return (max >= this.inputsWithName(form, el).filter(':checked').length);
}, function(form, el) {
    this.bindBrothers(form, el);
})

.validation('rangeselect', 'Seleziona tra {arg1} e {arg2} opzioni.', function(form, el, value, min, max) {
    var checked = this.inputsWithName(form, el).filter(':checked').length;

    return (min <= checked && max >= checked);
}, function(form, el) {
    this.bindBrothers(form, el);
});
