/*
* jQuery CheckList LimitChildrenNumber validation
*/

/*
* Changes
* ID            Date            User        Description
* 23857155      Feb 23, 2012    Jeyong      File created
* 23857155      Feb 29, 2011    Jeyong      validation was not properly handling forms with multiple pages.
*/

function ValidateLimitNumber(id, limit) {
    var checkedCount = 0;
    if (limit != 0) {
        /*
        Count number of rows that are checked
        */
        $("#" + id).children().each(function() {
            var checked = $(this).children().first().find("input[type='checkbox']").attr("checked");
            if (checked == "checked") {
                checkedCount++;
            }
        });

        /*
        Stylize each rows with checkboxes with error
        */
        $("#" + id).children().each(function() {
        var checkbox = $(this).children().first().find("input[type='checkbox']");
        if (checkedCount > limit) {
        if (checkbox.attr("checked") == "checked") {
        checkbox.addClass("iFormInvalid");
        checkbox.next().addClass("iFormCheckBoxError");
        }
        else {
        checkbox.removeClass("iFormInvalid");
        checkbox.next().removeClass("iFormCheckBoxError");
        }
        }
        else {
        checkbox.removeClass("iFormInvalid");
        checkbox.next().removeClass("iFormCheckBoxError");
        }
        });
        
        /*
        Hide or show error messages at header of the check list
        */
        if (checkedCount > limit) {
            $("#" + id + "_limitNumberError").show();
        }
        else {
            $("#" + id + "_limitNumberError").hide();
        }
    }
    UpdateFinishButtonStatus();
}
function UpdateFinishButtonStatus() {
    var container = $('div.validationError');
    var links = $('.noValidationLinks');
    
    var totalCheckError = $(".iFormInvalid").filter(":visible").length;
    if (totalCheckError > 0) {
        links.hide();
    }
}
   