﻿

$.extend({ ML: {} });
$.extend($.ML, {
    animationSpeed: 500,
    debug: false,
    'dumpObject': function(obj) {
        var msg = '';

        if (typeof obj[prop] === 'object' || typeof obj[prop] === 'Object') {
            for (var prop in obj) {
                msg += prop + ': ' + obj[prop] + '\n';

                if (typeof obj[prop] === 'object' || typeof obj[prop] === 'Object') {

                    for (var childProp in obj[prop]) {
                        msg += '    ' + childProp + ': ' + obj[prop][childProp] + '\n';
                    }
                }
            }
        }
        else {
            for (var prop in obj) {
                msg += '    ' + prop + ': ' + obj[prop] + '\n';
            }


        }
        return msg;

    },
    IsIE6: function() { return ($.browser.msie && $.browser.version == "6.0"); },
    LTrim: function(value) {

        var re = /\s*((\S+\s*)*)/;
        return value.replace(re, "$1");

    },

    // Removes ending whitespaces
    RTrim: function(value) {

        var re = /((\s*\S+)*)\s*/;
        return value.replace(re, "$1");

    },

    // Removes leading and ending whitespaces
    trim: function(value) {

        return this.LTrim(this.RTrim(value));

    },

    isEmail: function(val) {
        return this.trim(val).match(/^([\w._%-]+@(?:[\w_-]+\.)+[\w_%-]{2,7})([\s,;]+([\w._%-]+@(?:[\w_-]+\.)+[\w_%-]{2,7}))*$/);
    }


});

$.namespace("ML", {
    test :function(elems){
    alert(elems.attr("id"));},
    // Removes leading whitespaces
    isEmail: function(elems) {
    return trim(elems.val()).match(/^([\w._%-]+@(?:[\w_-]+\.)+[\w_%-]{2,7})([\s,;]+([\w._%-]+@(?:[\w_-]+\.)+[\w_%-]{2,7}))*$/);
    }
});

if (jQuery.validator) { //validator rules
    jQuery.validator.addMethod(
        "regex",
        function(value, element, regexp) {
            if (regexp.constructor != RegExp)
                regexp = new RegExp(regexp);
            else if (regexp.global)
                regexp.lastIndex = 0;
            return this.optional(element) || regexp.test(value);
        },
        "Please check your input."
    );
        jQuery.validator.addMethod("emails", function(value, element) {
        return this.optional(element) || /^([\w._%-]+@(?:[\w_-]+\.)+[\w_%-]{2,7})([\s,;]+([\w._%-]+@(?:[\w_-]+\.)+[\w_%-]{2,7}))*$/i.test($.ML.trim(value));
        }, "Email address is invalid.");

    jQuery.validator.addMethod("postalCode", function(value, element) {
        return this.optional(element) || /^\d{5}|[A-Za-z]\d[A-Za-z]( )?\d[A-Za-z]\d$/i.test(value);
    }, "Postal Code is invalid.");

    jQuery.validator.addMethod("wordsLimitation", function(value, element, limitation) {
        return $(element).wordsCount() <= limitation;
    }, "Too many words input.");
}
jQuery.fn.wordsCount = function() {
    //var val = $.ML.trim(this.val());
    var val = this.val();
    return val.split(/\b[\s,\.\?:;]*/).length - 1;

};