﻿/// <reference path="../jQuery/jquery-1.3.js" />

//$.fn.validatetify = function() {
//    this.find('.required').keyup(function() {
//    if ($(this).val() != '') {
//        $(this).css('border', 'solid 1px #000000');
//        $(this).next('verlicht veld').remove();
//    }
//    else {
//        $(this).css('border', 'solid 2px #990000');
//    }
//});
//}

$.fn.validate = function() {
    var isFormValid = true;
    var firstControl;
    this.find('.required').each(function(i) {
        if ($(this).val() == '') {
            isFormValid = false;
            if (!firstControl) firstControl = $(this);
            //$(this).css('border', 'solid 2px #990000');
            if (!$(this).hasClass('invalid')) {
                $(this).addClass('invalid');
                $(this).after("<span class='validationMsg'> *</span>");


                $(this).keyup(function() {
                    if ($(this).val() != '') {
                        //$(this).css('border', '');
                        $(this).removeClass('invalid');
                        $(this).next('.validationMsg').remove();
                    }
                    else if (!$(this).hasClass('invalid')) {
                        //$(this).css('border', 'solid 2px #990000');
                        $(this).addClass('invalid');
                        $(this).after("<span class='validationMsg'> *</span>");
                    }
                });
            }
        }
    });

    this.find('.numeric').each(function(i) {
        if (!isNumeric($(this).val())) {
            isFormValid = false;
            if (!firstControl) firstControl = $(this);
            //$(this).css('border', 'solid 2px #990000');
            if (!$(this).hasClass('invalid')) {
                $(this).addClass('invalid');
                $(this).after("<span class='validationMsg'> alleen nummers</span>");
                $(this).focus();
                $(this).keyup(function() {
                    if (isNumeric($(this).val())) {
                        $(this).removeClass('invalid');
                        //$(this).css('border', '');
                        $(this).next('.validationMsg').remove();
                    }
                    else if (!$(this).hasClass('invalid')) {
                        $(this).addClass('invalid');
                        //$(this).css('border', 'solid 2px #990000');
                        $(this).after("<span class='validationMsg'> alleen nummers</span>");
                    }
                });
            }
        }
    });

    this.find('.email').each(function(i) {
        var email = $(this).val();
        if (email != '') {
            apos = email.indexOf("@");
            dotpos = email.lastIndexOf(".");
            if (apos < 1 || dotpos - apos < 2) {
                isFormValid = false;
                if (!firstControl) firstControl = $(this);
                if (!$(this).hasClass('invalid')) {
                    $(this).addClass('invalid');
                    $(this).after("<span class='validationMsg'> ongeldig emailadres</span>");
                    $(this).focus();
                }
            }
        }

    });
    if (firstControl != null) {
        firstControl.focus();
    }
    return isFormValid;
}


function isNumeric(x) {
    // I use this function like this: if (isNumeric(myVar)) { } 
    // regular expression that validates a value is numeric 
    var RegExp = /^(-)?(\d*)(\.?)(\d*)$/; // Note: this WILL allow a number that ends in a decimal: -452. 
    // compare the argument to the RegEx 
    // the 'match' function returns 0 if the value didn't match 
    var result = x.match(RegExp);
    return result;
}
