/*
 * Note: include before script that deals with forms (eg. dedicated/script.js)
 * Sander v.d. Merwe <sander.m@prioserve.net>
 */

$(document).ready(function()
{
	// Change the way jQuery's .change() function works.
    // - change and keyup are now aliases of each other
    // - change event is delayed (500ms)
    
    var change = $.fn["change"];
    
    $.fn["change"] = function(fn){
        var timerId = -1;
        var timerFor = "";
        function delayed() {
            var ctx = this;
            if (-1 != timerId && timerFor == this.name) {
                clearTimeout(timerId);
                timerId = -1;
            }
            timerFor = this.name;
            timerId = setTimeout(function() { fn.call(ctx); }, 500);
        }
        change.call(this, delayed);
        this.keyup(delayed);
	};
});

var Validators =
{
    regexps:
    {
        "Huisnummer"        : /^[0-9]+[\s]*[a-z]?$/i,
        "Postcode"          : /^[\w\d\s.]{4,}$/,
        "Telefoonnummer"    : /(\d.*?){10,}/, 
        "Faxnummer"         : /(\d.*?){10,}/, 
        "E-mail"             : /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/,
    },

    /**
     * Applies given rule to given value and returns true if value is acceptable. 
     */
    validate: function(name, value)
    {
        if (! (/^[^\f\n\r\t\v]+$/.test(value))) {
            return false;
        }
        
        if ("undefined" == typeof Validators.regexps[name]) {
            return true;
        }
        
        return Validators.regexps[name].test(value);
    }
};

