var comments=Array();

function first_match(str, re)
{
    var res = null;
    str.scan(re, function(match) { res = match[1]});
    return res;
}

handlers = {
    'radio' : function() {
	var qid = first_match(this.name, /question_(\d+)/);
	comments[qid].enable();
    },

    'checkbox' : function() {
	var qid = first_match(this.name, /question_(\d+)/);
	var checked = 0;
	$$('input[name^=question_'+qid+']').each(function(el){if(el.checked) checked++});
	if (checked > 0) {
	    comments[qid].enable();
	} else {
	    comments[qid].value = '';
	    comments[qid].disable();
	}
    }
  
};

function init_comment_checker()
{
    var areas = $$('textarea:not([id])');
    var q_name = '';
    areas.each(
	       function(element) {
		   var qid = first_match(element.name, /extras\[(\d*?)\]/);
		   comments[qid] = element;
		   element.disable();
		   $$('input[name^=question_'+qid+']').each(
							    function(el){
								el.onclick = handlers[el.type];
							    }
							    )
	       }
	       );
}

