///////////////////////////// ML //////////////////////////

var errorValidateFocusOut = false;
var focusOutValidateList = [];

///////////////////////////////////////////////////////////

(function ($) {
    if (!$.validator) {
        throw '$.validator is missing.';
    }

    var custom = function (value, element, methodName) {
        if (!methodName) {
            throw 'Invalid method.';
        }

        var method = window[methodName];
        if (!method) {
            throw 'Invalid method ' + methodName + '.';
        }
        ///////////////////////////// ML //////////////////////////
        //return method.apply(this, [element, '-1', 1]);
        return method.apply(this, [element, this.validatorObject.sectionScope ? this.validatorObject.sectionScope.page - 1 : '-1', 1]);
        ///////////////////////////////////////////////////////////
    };
    $.validator.addMethod('customE', function (value, element, methodName) {
        return custom(value, element, methodName);
    });

    $.validator.addMethod('customW', function (value, element, methodName) {
        return custom(value, element, methodName);
    });

    $.validator.addMethod("date", function (value, element) {
        var check = false;
        if ($("#" + element.id).hasClass("datepicker")) {
            var re = /^\d{1,2}\.\d{1,2}\.\d{4}$/;
            if (re.test(value)) {
                var adata = value.split('.');
                var day = parseInt(adata[0], 10);
                var month = parseInt(adata[1], 10);
                var year = parseInt(adata[2], 10);

                check = isValidDate(day, month, year);
            }
        }
        else if ($("#" + element.id).hasClass("datetimepicker")) {
            var re2 = /^\d{1,2}\.\d{1,2}\.\d{4}\s\d{1,2}:\d{1,2}$/;
            if (re2.test(value)) {
                var adata = value.split(' ');
                var dateParts = adata[0].split('.');

                var day = parseInt(dateParts[0], 10);
                var month = parseInt(dateParts[1], 10);
                var year = parseInt(dateParts[2], 10);

                var timeParts = adata[1].split(':');

                var hours = parseInt(timeParts[0], 10);
                var minutes = parseInt(timeParts[1], 10);

                check = isValidDateTime(day, month, year, hours, minutes);
            }
        }
        else if ($("#" + element.id).hasClass("datetimewithsecondspicker")) {
            var re2 = /^\d{1,2}\.\d{1,2}\.\d{4}\s\d{1,2}:\d{1,2}:\d{1,2}$/;
            if (re2.test(value)) {
                var adata = value.split(' ');
                var dateParts = adata[0].split('.');

                var day = parseInt(dateParts[0], 10);
                var month = parseInt(dateParts[1], 10);
                var year = parseInt(dateParts[2], 10);

                var timeParts = adata[1].split(':');

                var hours = parseInt(timeParts[0], 10);
                var minutes = parseInt(timeParts[1], 10);
                var seconds = parseInt(timeParts[2], 10);

                check = isValidDateTimeWithSeconds(day, month, year, hours, minutes, seconds);
            }
        }

        return this.optional(element) || check;
    }, "Please enter a correct date");


    $.validator.addMethod("maxValue", function (value, element, arg) {
        var check = true;
        var max = parseFloat(arg);
        if (value > max)
            check = false;

        return this.optional(element) || check;
    }, "Bad value");

    $.validator.addMethod("minValue", function (value, element, arg) {
        var check = true;
        var min = parseFloat(arg);
        if (value < min)
            check = false;

        return this.optional(element) || check;
    }, "Bad value");

}(jQuery));

function isValidDate(day, month, year) {
    if (month < 1 || month > 12) {
        return false;
    }

    if (day < 1 || day > 31) {
        return false;
    }

    if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) {
        return false;
    }

    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day == 29 && !isleap)) {
            return false;
        }
    }

    return true;  // date is valid
}

function isValidDateTime(day, month, year, hours, minutes) {
    if (!isValidDate(day, month, year)) {
        return false;
    }

    if (hours < 0 || hours > 23) {
        return false;
    }

    if (minutes < 0 || minutes > 59) {
        return false;
    }

    return true;  // date is valid
}

function isValidDateTimeWithSeconds(day, month, year, hours, minutes, seconds) {
    if (!isValidDate(day, month, year)) {
        return false;
    }

    if (hours < 0 || hours > 23) {
        return false;
    }

    if (minutes < 0 || minutes > 59) {
        return false;
    }

    if (seconds < 0 || seconds > 59) {
        return false;
    }

    return true;  // date is valid
}