function appendHandler( elem, handler, action )
{
  if( typeof(handler) == 'string' )
  {
    handler = new Array(handler);
  }
  for( var i = 0; i < handler.length; i++ )
  {
    if( elem.attachEvent )
    {
      elem.attachEvent(handler[i], new Function('', action));
    }
    else
    {
      elem.setAttribute(handler[i], action);
    }
  }
}



/** Call:
 radioGaga({'input':'id_of_input_field', 'radio':'id_of_radio_button', 'radioIfEmpty':'id_of_default_radio_button'});
 'radioIfEmpty' is optional. The input field and the radio button(s) will be connected, so that the correct
 radio button gets checked depending on whether there is data in the input field or not.
 */
function radioGaga(params) {
    //var radio = document.getElementById(params['radio']);
    Log.trace('GLOBAL', 'radioGaga(' + Log.objectToString(params, 'v'));
    
    var input = document.getElementById(params['input']);
    appendHandler(input, new Array('onkeyup', 'onchange', 'onblur'),
        "document.getElementById('" + params['radio'] + "').checked=true;"
        + (params['radioIfEmpty']
        ? "if (document.getElementById('" + params['input'] + "').value=='') { document.getElementById('"
            + params['radioIfEmpty'] + "').checked=true; }"
        : '')
    );
    if (params['radioIfEmpty']) {
        appendHandler(document.getElementById(params['radioIfEmpty']), new Array('onkeyup', 'onchange', 'onfocus', 'onblur', 'onclick'),
            "if (document.getElementById('"+params['radioIfEmpty']+"').checked) { document.getElementById('"+params['input']+"').value = ''; }"
        );
    }
}



function checkFrequencies(form, days) {
    var els = form.elements;
    for (var i=0; i<els.length; i++) {
        var el = els[i];
        if (el.name.startsWith('frequencies_recurrent_type_weekly[')) {
            el.checked = days.hasValue(el.value);
        }
    }
}

function selectFrequencyType(form) {
    var els   = form.elements;
    var type  = 'weekly';
    var combs = {
        'daily':    new Array('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'),
        'weekdays': new Array('mon', 'tue', 'wed', 'thu', 'fri')
    };
    var values = new Array();
    for (var i=0; i<combs.daily.length; i++) {
        if (els['frequencies_recurrent_type_weekly['+combs.daily[i]+']'].checked) {
            values.push(combs.daily[i]);
        }
    }
    for (var t in combs) {
        if (t in Object.prototype) { continue; }
        if (values.equalsValues(combs[t])) {
            type = t;
        }
    }
    var s = els['frequencies_recurrent_type'];
    for (i=0; i<s.length; i++) {
        s[i].checked = s[i].value == type;
    }
}
