// $Revision: 1195 $

// Used in the Program Form

////////////////Areas Served//////////////////
// Areas Served is a set of radio buttons, some of which have
// a nested checkbox group. The radio button group is named area_type,
// with each radio button having a value like TOWN. Each nested
// checkbox group has a name of cboxarea_<radio value>, e.g. cboxarea_TOWN.
//
//
function check_greater_towns(towns_regex) {
    var form = document.program_form;
    var towns = form.cboxarea_TOWN;
    var aCheckedTown;
    for (var i = 0; i < towns.length; i++) {
        if (towns[i].value.match(towns_regex)) {
            towns[i].checked = true;
            aCheckedTown = towns[i];
        }
    }
    updateAreasServed(aCheckedTown);
}

// The following 3 functions all deal with maintaining the mutual exclusivity
// of the radio/nested-checkbox groups within areas served, such that:
//    - clicking any checkbox, makes its parent radio button the selected one.
//    - clicking any radio or checkbox clears all checkboxes in any other
//      nested-checkbox group.

// updateAreasServed must be called by each of the radios and checkboxes within
// areas served (via onClick('updateAreasServed(this)').
//
// 'element' is the calling form element. It must be either a radio button with
// a value (e.g. TOWN) or a checkbox with a name like cboxarea_TOWN
function updateAreasServed(element) {
    if (element.type == 'checkbox' && element.name.match(/\bcboxarea_/)) {
        _switchAreaTypeRadioSelection(element);
        return _clearAreaCheckboxGroupsExceptFor(element.form, element.name)
    } else if (element.type == 'radio') {
        // the name of the checkboxes to exclude (if any):
        var checkboxName = "cboxarea_" + element.value;
        return _clearAreaCheckboxGroupsExceptFor(element.form, checkboxName);
    }else {
       alert ("Bad element argument (named " + element.name + ")");
       return false;
    }
}

// Private. Ensure that when a nested checkbox is selected, its parent radio
// button also gets selected.
function _switchAreaTypeRadioSelection(checkbox) {
   if (checkbox.checked) {
      // Get the value of the radio button we want to select
      // (e.g. TOWN) from the checkbox's group name (e.g. cboxarea_TOWN)
      var radioValue = checkbox.name.replace(/cboxarea_/,"");

      for (var i = 0; i < checkbox.form.area_type.length; i++) {
         if (checkbox.form.area_type[i].value == radioValue) {
             checkbox.form.area_type[i].checked = true;
             return;
         }
      }
      // Should never get here.
      alert("Missing area_type radio button with value: " + radioValue);
   }
}

// Private. Clear the values of all checkboxes in the given form that begin
// with cboxarea_ except for those with the given checkboxName.
function _clearAreaCheckboxGroupsExceptFor(form, checkboxName) {
    if (!checkboxName.match(/\bcboxarea_/)) {
       alert ("Argument " + checkboxName + " does not begin with cboxarea_");
       return false;
    }

    for (var i = 0; i < form.elements.length; i++) {
        var element = form.elements[i];
        if (element.type == 'checkbox' && element.name.match(/\bcboxarea_/) &&
            element.name != checkboxName )
        {
            element.checked = false;
        }
     }
     return true;
}

/////////////End Areas Served////////////////
