﻿function ValidateAtLeastOneOf(val)
{
    // For controls who's client-side validation is non-trivial (namely the calendar). 
    // If we can't find at least one good value and do have a calendar control, we'll
    // have to send to the server to finish validation.
    var allowpostback=false; 
    var controls = val.controlstovalidate.split(',');
    
    for (var i = 0;i<controls.length;i++)
    {
        // how to do calendar?
        if (controls[i].replace(' ', '').length > 0)
        {
            var control = document.getElementById(controls[i].replace(' ', ''));
            if (control.type == "checkbox" || control.type == "radio") 
                {if (control.checked) return true;}
            else if (control.type) // regular type (textbox, select[listbox|dropdownlist])
                {if (control.value.replace(/^\s+|\s+$/, '').length > 0) return true;}
            else //checkboxlist, radioboxlist, calendar
            {
                var chkidx = 0, subctrl = document.getElementById(control.id + "_" + chkidx++);
                if (subctrl) //checkboxlist, radioboxlist
                {
                    while (subctrl)  
                    {  
                        if (subctrl.checked) return true;
                        subctrl = document.getElementById(control.id + "_" + chkidx++);
                    }
                }
                // calendar or bad input- have to finish validation server side if we don't find any values here
                else if (control) allowpostback = true; // allow postback
            }
        }
    }
    return allowpostback;
}


