﻿/*
    PB/20:20, 22/11/10 Simple client side-validator;
*/
var validator = (function() {

    var _errors = [];

    // validation rules;
    // can be extended by addition of a new _errMsg rule object;
    //
    // the name of each rule maps to a specific input class name,
    // e.g if an input has the class name 'email' it will trigger
    // the method;
    //
    //      _errMsg['email'].test([input value]);
    //
    // upon failure the value of the message property will be 
    // returned; 
    var _errMsg = {
        
        // validate email address;
        email: {
            // message to be returned upon failure;
            msg: '(invalid email address)',
            // test method;
            test: function(val /*String, input value*/) {
                return /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/.test(val);
            }
        },

        // validate field maked as valid, basically that it has a value;
        text: {
            msg: '(required field)',
            test: function(val) {
                return val.length > 0;        
            }
        },

        // validate phone number implimentation to be defined;
        phone: {
            msg: 'Invalid phone number',
            test: function(val) {
                return true; //test here!;
            }
        },

        // general validation of credit card numbers;
        char16: {
            msg: 'Invalid credit card number, please enter you number without spaces',
            test: function(val) {
                return val.length !== 16;
            }
        },

        // general validation of credit card CNV numbers;
        char3: {
            msg: 'Invalid CVV number',
            test: function(val) {
                return val.length !== 3;
            }
        },

        // general validation for date dropdowns, that 
        // it has a numeric value prevents submition of
        // the default value;
        dd: {
            msg: '(required field)',
            test: function(val) {
                return /^[0-9]{2}$/.test(val);
            }
        },

        // as dd;
        mm: {
            msg: '(required field)',
            test: function(val) {
                return /^[0-9]{2}$/.test(val);
            }
        },

        // as dd;
        yyyy: {
            msg: '(required field)',
            test: function(val) {
                return /^[0-9]{2}$/.test(val);
            }
        },

        // validate checkbox is checked;
        checkbox:  {
            msg: '(required field)',
            test: function(val, el) {
                // slight deviation for checkbox, 
                //check elements checked attribute;
                return el.checked;
            }
        }

    }

    return {
        
        // required validation rules for an element are defined in the inputs className;
        validate: function(val /*String, input value*/, className /*String, input class name*/, el /* HTMLInputElement, element being validated*/) {

            _errors = [];   // Array, list of validation failures, allow for
                            // an input to fail more than rule;

            // split class name into potential validation rules;
            for (var i = 0, rules = className.split(' '); i<rules.length; i++) {
                // check if a class name maps to a validation rule && 
                // if it pass the test for that rule (see above), if not...;
                if (_errMsg[rules[i]] && !_errMsg[rules[i]].test(val, el)) {
                    // ...push the rules error message to the validation failures array;
                    _errors.push(_errMsg[rules[i]].msg);
                };
            };

            return _errors; 

        } 

    }

})();

