﻿/*
 * StartupValidation
 *
 * This method performs validation when the page is loaded. This is to ensure that all
 * validators display the proper state when user for example has clicked "back" in the
 * web browser.
 *
 * 090216   Håkan Edling
 * 090810   Håkan Edling    Added support for ValidateEmpty to RegExp validators
 */
function StartupValidation() {
    if (typeof(Core_Validators) != 'undefined') {
        for (var i = 0; i < Core_Validators.length; i++) {
            // Get the validator control
            var validator  = document.all ? document.all[Core_Validators[i].Validator] : document.getElementById(Core_Validators[i].Validator) ;
            var msgcontrol = document.all ? document.all[Core_Validators[i].MessageControl] : document.getElementById(Core_Validators[i].MessageControl) ;
            
            // Perform validation
            if (validator != null) {
                // Hide validator according to ValidateEmpty
                if (!Core_Validators[i].ValidateEmpty && (Core_Validators[i].Type == "TextBox" || Core_Validators[i].Type == "RegExp") && IsEmptyTextBox(Core_Validators[i].Controls)) {
                    validator.style.display = "none" ;
                } else {
                    validator.style.display = "block" ;
                }        
            
                // Set validator style and update validator message control
                if (ValidateObject(Core_Validators[i])) {
                    validator.className = "validator-valid";
                    if (msgcontrol != null)
                        msgcontrol.style.display = "none" ;
                } else {
                    validator.className = "validator-unvalid";
                    if (msgcontrol != null) 
                        msgcontrol.style.display = "inline";     
                }
            }
        }
    }
}

/*
 * EventValidation
 *
 * This method performs validation when an event occurs for a control associated with
 * a CoreValidator, i.e OnKeyUp, OnClick, OnChange or OnBlur. It performs validation
 * on the controls associated with the validator, updates the css-class of the validator
 * according to the result and finally updates the ValidationSummary panel if available
 * on the current page.
 *
 * 081111   Håkan Edling
 * 090810   Håkan Edling    Added support for ValidateEmpty to RegExp validators
 */
function EventValidation(valObj) {
    // Get document controls
    var validator  = document.all ? document.all[valObj.Validator] : document.getElementById(valObj.Validator) ;
    var msgcontrol = document.all ? document.all[valObj.MessageControl] : document.getElementById(valObj.MessageControl) ;

    // Perform event validation
    if (validator != null) {
        // Hide validator according to ValidateEmpty
        if (!valObj.ValidateEmpty && (valObj.Type == "TextBox" || valObj.Type == "RegExp") && IsEmptyTextBox(valObj.Controls)) {
            validator.style.display = "none";
        } else {
            validator.style.display = "block" ;
        }
    
        // Set validator style and update validator message control
        if (ValidateObject(valObj)) {
            validator.className = "validator-valid";
            if (msgcontrol != null)
                msgcontrol.style.display = "none" ;
        } else {
            validator.className = "validator-unvalid";
            if (msgcontrol != null) 
                msgcontrol.style.display = "inline"; 
        }        
    }
    
    // Update ValidationSummary, if available
    if (typeof(ValidationSummary) != 'undefined' && typeof(ValidationSummaryCount) == "function")
        ValidationSummaryCount();
}

/*
 * SubmitValidation
 *
 * This method performs validation when ASP.NET calls DoPostBackWithOptions. If all validators are 
 * valid the method returns true and .NET continues with it's own validation before posting. If any
 * CoreValidators are unvalid the postback will cancel.
 *
 * 081111   Håkan Edling
 */
function SubmitValidation() {
    for (var i = 0; i < Core_Validators.length; i++) {
        // Get the validator control
        var validator = document.all ? document.all[Core_Validators[i].Validator] : document.getElementById(Core_Validators[i].Validator) ;

        if (validator != null) {
            // Hide validator according to ValidateEmpty
            if (!Core_Validators[i].ValidateEmpty && (Core_Validators[i].Type == "TextBox" || Core_Validators[i].Type == "RegExp") && IsEmptyTextBox(Core_Validators[i].Controls))
                continue;

            // Since all validators need to be valid for a submit we can break and return first
            // time we hit an unvalid validator.
            if (ValidateObject(Core_Validators[i]) != true)
                return false ;
        }
    }    
    // All validators was valid
    return true ;
}

/* 
 * IsEmptyTextBox
 *
 * Checks weather the given controls are empty
 *
 * 090324   Håkan Edling
 */
function IsEmptyTextBox(ctl) {
    var empty = true ;
    for (var i = 0; i < ctl.length; i++) {
        var control = document.all ? document.all[ctl[i]] : document.getElementById(ctl[i]) ;
        if (control.value.length > 0) {
            empty = false ;
            break ;
        }
    }
    return empty;
}


/*
 * CountInvalidFields
 *
 * This method performs the same action as SubmitValidation, but instead of returning false when hitting
 * an unvalid CoreValidator it counts the sum of all unvalid validators. This method is used by the
 * ValidationSummary to display how many fields are left to fill out.
 *
 * 081111   Håkan Edling
 */
function CountInvalidFields() {
    var count = 0;
    if (typeof(Core_Validators) != 'undefined') {
        for (var i = 0; i < Core_Validators.length; i++) {
            // Get the validator control
            var validator = document.all ? document.all[Core_Validators[i].Validator] : document.getElementById(Core_Validators[i].Validator) ;

            if (validator != null) {
                // Hide validator according to ValidateEmpty
                if (!Core_Validators[i].ValidateEmpty && (Core_Validators[i].Type == "TextBox" || Core_Validators[i].Type == "RegExp") && IsEmptyTextBox(Core_Validators[i].Controls))
                    continue;
                
                if (ValidateObject(Core_Validators[i]) != true)
                    count++;            
            }
        }
    }
    return count;
}

/*
 * ValidateObject
 *
 * Validates the given validator object by calling it's validation function according to the Type
 * set in the javascript validation object.
 *
 * 081111   Håkan Edling
 * 091118   Håkan Edling    Added support for ValidateEmpty
 */
function ValidateObject(valObj) {
    // Validate the control according to type
    if (valObj.Type == "CheckBox") {
        return ValidateRequiredCheckBox(valObj.Controls, valObj.MinSize, valObj.MaxSize) ;
    } else if (valObj.Type == "TextBox") {
        return ValidateRequiredTextBox(valObj.Controls, valObj.MinSize, valObj.MaxSize, valObj.ValidateEmpty) ;
    } else if (valObj.Type == "DropDown") {
        return ValidateRequiredDropDown(valObj.Controls) ;
    } else if (valObj.Type == "RegExp") {
    return ValidateRegExpTextBox(valObj.Controls, valObj.RegExp, valObj.ValidateEmpty);
    } else if (valObj.Type == "Custom") {
        return ValidateCustomValidator(valObj.Validator) ;
    }
    return false ;
}

/* 
 * ValidateRequiredTextBox
 *
 * Validates a single or a set of textboxes
 *
 * 081010   Håkan Edling
 * 091118   Håkan Edling    Added support for ValidateEmpty
 */
function ValidateRequiredTextBox(ctl, min, max, empty) {
    var valid = true ;
    for (var i = 0; i < ctl.length; i++) {
        var control = document.all ? document.all[ctl[i]] : document.getElementById(ctl[i]);
        if (control.value.length > 0 || empty) {
            if ((control.value.length < min) || (max > 0 && control.value.length > max)) {
                valid = false;
                break;
            }
        }
    }
    return valid ;
}

/*
 * ValidateRequiredCheckBox
 *
 * Validates a single or a set of checkboxes 
 *
 * 081010   Håkan Edling
 */
function ValidateRequiredCheckBox(ctl, min, max) {
    var count = 0;
    for (var i = 0; i < ctl.length; i++) {
        var control = document.all ? document.all[ctl[i]] : document.getElementById(ctl[i]) ;
        if (control.checked)
            count++ ;
    }
    if ((count >= min) && (max <= 0 || count <= max))
        return true ;
    return false ;
}

/*
 * ValidateRequiredDropDown
 *
 * Validates a single or a set of dropdowns 
 *
 * 081010   Håkan Edling
 */
function ValidateRequiredDropDown(ctl, min, max) {
    var valid = true ;
    for (var i = 0; i < ctl.length; i++) {
        var control = document.all ? document.all[ctl[i]] : document.getElementById(ctl[i]) ;
        if (control.selectedIndex <= 0) {
            valid = false ;
            break ;
        }
    }
    return valid;
}

/*
 * ValidateRegExpTextBox
 *
 * Validates a single or a set of textboxes against a regular expression 
 *
 * 081010   Håkan Edling
 * 091118   Håkan Edling    Added support for ValidateEmpty
 */
function ValidateRegExpTextBox(ctl, expr, empty) {
    var valid = true ;
    for (var i = 0; i < ctl.length; i++) {
        var control = document.all ? document.all[ctl[i]] : document.getElementById(ctl[i]) ;
        if (control.value.length > 0 || empty) {
            var reg = new RegExp(expr[i], "i");
            if (reg.test(control.value) == false) {
                valid = false;
                break;
            }
        }
    }
    return valid ;
}

/*
 * ValidateRegExpTextBox
 *
 * Validates a custom validator by looking at it's current css-class 
 *
 * 090205   Håkan Edling
 */
function ValidateCustomValidator(ctl) {
    var valid = true ;
    if (ctl != "") {
        var control = document.all ? document.all[ctl] : document.getElementById(ctl) ;
        
        if (control.className == "validator-unvalid")
            valid = false ;
    }
    return valid ;
}

/* 
 * ValidationSummaryCount
 *
 * Handles the ValidationSummary panel. Counts the unvalid validators, presents the result in the panel
 * and enables/disables the current submit button linked to the panel.
 *
 * 081013   Håkan Edling
 * 081111   Håkan Edling    Updated compability
 * 110202   Robert Svallin  Added singular/plural text functionality
 */
function ValidationSummaryCount() {
    var count = CountInvalidFields();
    if (count > 0) {
        var obj = document.all ? document.all[ValidationSummary.PreSingular] : document.getElementById(ValidationSummary.PreSingular);
        if (obj != null)
            obj.style.display = count == 1 ? "inline" : "none";
        obj = document.all ? document.all[ValidationSummary.PrePlural] : document.getElementById(ValidationSummary.PrePlural);
        if (obj != null)
            obj.style.display = count == 1 ? "none" : "inline";
        obj = document.all ? document.all[ValidationSummary.PostSingular] : document.getElementById(ValidationSummary.PostSingular);
        if (obj != null)
            obj.style.display = count == 1 ? "inline" : "none";
        obj = document.all ? document.all[ValidationSummary.PostPlural] : document.getElementById(ValidationSummary.PostPlural);
        if (obj != null)
            obj.style.display = count == 1 ? "none" : "inline";
        obj = document.all ? document.all[ValidationSummary.CountID] : document.getElementById(ValidationSummary.CountID);
        if (obj.innerText != null) /* For IE */
            obj.innerText = count;
        else if (obj.textContent != null) /* For Firefox */
            obj.textContent = count;
        var ctrl = document.all ? document.all[ValidationSummary.ControlID] : document.getElementById(ValidationSummary.ControlID);
        ctrl.style.display = "inline";
        var subm = document.all ? document.all[ValidationSummary.SubmitID] : document.getElementById(ValidationSummary.SubmitID);
        subm.className = "btn-disabled";
        subm.disabled = true;
    } else {
        var ctrl = document.all ? document.all[ValidationSummary.ControlID] : document.getElementById(ValidationSummary.ControlID);
        ctrl.style.display = "none";
        var subm = document.all ? document.all[ValidationSummary.SubmitID] : document.getElementById(ValidationSummary.SubmitID);
        subm.className = "btn";
        subm.disabled = false;
    }
}

