function passwordTest(settings, password) {
    var result = {};
    result.rules = "";
    result.testString = "^";
    result.result = true;
    if(settings.rule.upperCase) {
        var testString = /^(?=.*[A-Z])/;
        if(!testString.test(password)) {
            result.rules += "Password must contain an UPPERCASE character \n\n";
            result.result = false;
        }
    }
    if(settings.rule.lowerCase) {
        var testString = /^(?=.*[a-z])/;
        if(!testString.test(password)) {
            result.rules += "Password must contain a lowercase character \n\n";
            result.result = false;
        }
    }
    if(settings.rule.numeric) {
        var testString = /^(?=.*[0-9])/;
        if(!testString.test(password)) {
            result.rules += "Password must contain a numeric character \n\n";
            result.result = false;
        }
    }
    if(settings.rule.specialChar) {
        var testString = /^(?=.*[!@#\$%\^&\*\+])/;
        if(!testString.test(password)) {
            result.rules += "Password must contain a special character !@#$%^&*+ \n\n";
            result.result = false;
        }
    }
    if(settings.rule.passwordLen) {
        var testString = "^(?=.{"+settings.rule.passwordLen+",})";
        var testRegex = new RegExp(testString, "gi");
        if(!testRegex.test(password)) {
            result.rules += "Password must contain at least "+settings.rule.passwordLen+" character(s) \n";
            result.result = false;
        }
    }
    return result;
}

(function($) {
    $.fn.validateInput = function(options) {
        // var id = '#'+this.attr('id')+"+div em";
        var id = this.parent().find("div.error em");
        var theLabel = this.parent().find("label[for='"+this.attr("id")+"']");
        var autoLabel = 'You have not completed ';
        var fieldName = '';
        if(theLabel.length > 0) {
            autoLabel = theLabel[0].innerText + ' is ';
            fieldName = theLabel[0].innerText;
        } else {
            var the_ph = this.attr('placeholder');
            if (typeof the_ph !== 'undefined') {
                if(the_ph.length > 0) {
                    autoLabel = the_ph + ' is ';
                    fieldName = the_ph;
                }
            }
        }
        var settings = $.extend({
            for: 'empty',
            error: 'This Field is Required',
            alert: autoLabel + 'an important field'
        }, options);
        // var passwordRule = 'password must contain at least 1 alphabetical character, 1 numeric character and must be six (6) characters or longer';

        $(settings.formid+' #alert').css('display', 'none');
        // $(id).html('');
        // $(id).css({
        //     'color': '#fff',
        //     'background': 'none',
        //     'padding-left': '0',
        //     'padding-right': '0'
        // });

        if(settings.for == 'empty') {
            if((this).val() == '') {
            $(this).notify(settings.error, "error");
            this.focus();
            $.notify(settings.alert);
            // $(id).css({
            //     'color': '#fff',
            //     'background': '#ea3b3b',
            //     'padding-left': '10px',
            //     'padding-right': '10px'
            // });
            return false;
            } return true;
        }

        if(settings.for == 'email') {
            var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
            if(!re.test(String(this.val()).toLowerCase())) {
            $(this).notify(settings.error, "error");
            this.focus();
            $.notify(settings.alert);
            return false;
            } return true;
        }

        if(settings.for == 'phone') {
            var re = /^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,7}$/im;
            if(!re.test(this.val())) {
            $(this).notify(settings.error, "error");
            this.focus();
            $.notify(settings.alert);
            return false;
            } return true;
        }

        if(settings.for == 'password') {
            var passTest = passwordTest(settings, this.val());
            if(!passTest.result) {
                $(this).notify(passTest.rules, {position: "top"});
                this.focus();
                $.notify(passTest.rules);
                return false;
            } return true;
        }

        if(settings.for == 'confirmPassword') {
            var passwordField = $(settings.formid+" #password");
            if(typeof settings.password !== 'undefined') {
                passwordField = $(settings.formid+' #'+settings.password);
            }
            if(passwordField.val() !== this.val()) {
            $(this).notify(settings.error, "error");
            this.focus();
            $.notify(settings.alert);
            return false;
            } return true;
        }

        if(settings.for == 'number') {
            if(parseFloat(this.val()) > parseFloat(this.attr('max'))) {
            // $(id).html("You entered a number higher than the maximum acceptable for the field which is "+this.attr('max'));
            $(this).notify("You entered a number higher than the maximum acceptable for the field which is "+this.attr('max'), "error");
            this.focus();
            $.notify("You entered a number higher than the maximum acceptable for "+fieldName+" field which is "+this.attr('max'));
            return false;
            } 
            else if(parseFloat(this.val()) < parseFloat(this.attr('min'))) {
            // $(id).html("You entered a number lesser than the minimum acceptable for the field which is "+this.attr('min'));
            $(this).notify("You entered a number lesser than the minimum acceptable for the field which is "+this.attr('min'), "error");
            this.focus();
            $.notify("You entered a number lesser than the minimum acceptable for "+fieldName+" field which is "+this.attr('min'));
            return false;
            }
            return true;
        }

        if(settings.for == 'checkbox') {
            if(this[0].checked == false) {
            $(this).notify(settings.error, "error");
            $(settings.formid+' #alert').css('display', '');
            this.focus();
            $.notify(settings.alert);
            return false;
            } return true;
        }

        if(settings.for == 'multiple') {
            if((this).val() == null) {
            $(this).notify(settings.error, "error");
            $(settings.formid+' #alert').css('display', '');
            this.focus();
            $.notify(settings.alert);
            return false;
            } return true;
        }
    }

    $.fn.validateNoContact = function(options) {
        var id = this.parent().find("div.error em");
        var theLabel = this.parent().find("label[for='"+this.attr("id")+"']");
        var fieldName = "";
        if(theLabel.length > 0) {
            fieldName = theLabel[0].innerText + " ";
        } else {
            var the_ph = this.attr('placeholder');
            if (typeof the_ph !== 'undefined') {
                if(the_ph.length > 0) {
                    fieldName = the_ph + " ";
                }
            }
        }

        var re = /^.*[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,7}.*$/im; //phone number
        if(re.test(this.val())) {
            $(this).notify('This field contains a phone number. Please remove all contacts.', "error");
            this.focus();
            $.notify(fieldName + 'contains a phone number. Please remove all contacts.', "error");
            return false;
        }

        var re = /^.*(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,})).*$/;
        if(re.test(this.val())) {
            $(this).notify('This field contains an email address. Please remove all contacts.', "error");
            this.focus();
            $.notify(fieldName + 'contains an email address. Please remove all contacts.', "error");
            return false;
        }

        var re = $("#validationRegex45456ghk").val();
        re = new RegExp(re);
        if(re.test(this.val())) {
            $(this).notify('You are trying to spell a number. Any further attempt will get you suspended.', "error");
            this.focus();
            $.notify('You are trying to spell a number at ' + fieldName + ' Any further attempt will get you suspended.', "error");
            return false;
        } 
        return true;
    }
}(jQuery));


(function($) {
    $.fn.validateForm = function(options) {
        var id = '#'+this.attr('id');
        var valid = true;
        var check = false;
        var alert = "";
        $(id+" #alert span").html("");

        var settings = $.extend({
            password: 'password'
        }, options);

        $.each($(id)[0], function( index, e ) {
            if($(e).hasClass("no-contact") || ($(e).data("noContact") != undefined)) {
                check = $(e).validateNoContact({formid: id});
                if(valid) {
                    valid = check;
                }
            }
            if(e.required) {
                switch(e.type) {
                    case "text":
                        check = $(id+" #"+e.id).validateInput({formid: id});
                        if(valid) {
                            valid = check;
                        }
                        if($(e).hasClass("numbers-only")) {
                            check = $(id+" #"+e.id).validateInput({
                                formid: id,
                                for: 'number'
                            });
                            if(valid) {
                                valid = check;
                            }
                        }
                        break;

                    case "checkbox":
                    case "multiple":
                        check = $(id+" #"+e.id).validateInput({
                            formid: id,
                            for: e.type,
                            alert: e.dataset.alert
                        });
                        if(valid) {
                            valid = check;
                        }
                        break;

                    case "email":
                        check = $(id+" #"+e.id).validateInput({
                            formid: id,
                            for: 'email',
                            error: 'Invalid email format',
                            alert: 'Invalid email format'
                        });
                        if(valid) {
                            valid = check;
                        }
                        break;

                    case "tel":
                        check = $(id+" #"+e.id).validateInput({
                            formid: id,
                            for: 'phone',
                            error: 'Invalid phone number format',
                            alert: 'Invalid phone number format'
                        });
                        if(valid) {
                            valid = check;
                        }
                        break;

                    case "password":
                        if(e.id == settings.password) {
                            var rule = {};
                            rule.upperCase = (e.dataset.upperCase !== undefined) ? true : false;
                            rule.lowerCase = (e.dataset.lowerCase !== undefined) ? true : false;
                            rule.numeric = (e.dataset.numeric !== undefined) ? true : false;
                            rule.passwordLen = (e.dataset.passwordLen !== undefined) ? parseInt(e.dataset.passwordLen) : 6;
                            rule.specialChar = (e.dataset.specialChar !== undefined) ? true : false;
                            check = $(id+" #"+e.id).validateInput({
                                formid: id,
                                for: 'password',
                                error: 'Please observe the password rules',
                                rule: rule
                            });
                        } else {
                            check = $(id+" #"+e.id).validateInput({
                                formid: id,
                                for: 'confirmPassword',
                                password: settings.password,
                                alert: 'Password do not match',
                                error: 'Password do not match'
                            });
                        }
                        if(valid) {
                            valid = check;
                        }
                        break;

                    case "number":
                        if($(id+" #"+e.id).validateInput({formid: id})) {
                            check = $(id+" #"+e.id).validateInput({
                                formid: id,
                                for: 'number'
                            });
                        } else {
                            check = false;
                        }
                        if(valid) {
                            valid = check;
                        }
                        break;
                        
                    default:
                        check = $(id+" #"+e.id).validateInput({formid: id});
                        if(valid) {
                            valid = check;
                        }
                        break;

                }
                if($(id+" #alert span").html() !== "") {
                    alert += $(id+" #alert span").html()+"</br>";
                    $(id+" #alert span").html("");
                }
            }
        }); 
        $(id+" #alert span").html(alert);
        if(alert !== "") {
            $(id+" #alert").css("display", "");
        }
        return valid;       
    }
}(jQuery));

(function($) {
    $.fn.clearForm = function(options) {
        var id = '#'+this.attr('id');
        $.each($(id)[0], function( index, e ) {
            if($(e).attr("type") != 'hidden' && $(e).attr("id") != 'alert' && $(e).attr("id") != 'submit' && $(e).data("noclear") === undefined) {
                $(e).val("");
                if($(e).hasClass("croppie-file")) {
                    var img = $("#croppie-output-"+ $(e).data("controlId"));
                    $(img).attr("src", $(img).data("defaultSrc"));
                }
                if($(e).hasClass("standardSelect")) {
                    $(e).trigger("chosen:updated");
                }
                if($(e).hasClass("trumbowyg-textarea")) {
                    $(".trumbowyg-editor").html("");
                }
            }
        });   
    }
}(jQuery));

jQuery(document).ready(function($){
    // 
    $(document).on("keyup", "input[type='password'][required='required']", function(e){
        var rule = {};
        rule.upperCase = ($(this).data('upperCase') !== undefined) ? true : false;
        rule.lowerCase = ($(this).data('lowerCase') !== undefined) ? true : false;
        rule.numeric = ($(this).data('numeric') !== undefined) ? true : false;
        rule.passwordLen = ($(this).data('passwordLen') !== undefined) ? parseInt($(this).data('passwordLen')) : 6;
        rule.specialChar = ($(this).data('specialChar') !== undefined) ? true : false;
        var settings = {rule: rule}
        var val = $(this).val();
        var test = passwordTest(settings, val);
        if(!test.result) {
            $(this).notify(test.rules);
        } else {
            $(this).notify("Good Password", {
                className: "success",
                position: "top"
            });
        }
        
    })
})