// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
Survey = {
  init: function()
  { 
    if ($('section'))
	{
  	els = $('section').getElementsByClassName('hasSubsection');
    for (i=0; i< els.length; i++)
	{
		els[i].observe('click',Survey.elementHandler);	
	}
	radio_els = $('section').getElementsByClassName('radioGroup');
    for (i=0; i< radio_els.length; i++)
	{
		radio_els[i].observe('click',Survey.elementHandler);	
	}
	}
  },
  elementHandler: function(event)
  {
  		el = Event.element(event);	
  		//alert(el.id);
		//alert(el.type);
		if(el.type)
		{
          type = el.type;
		}	
		subsection = $('subsection_'+el.id);				
			switch (type)
			{
				case 'checkbox':
				if (el.checked)
				{			
				   if(subsection.hasClassName('subsection'))				   
					{
						subsection.removeClassName('subsection');						
						new Effect.Highlight(subsection);
						chk_els = $('section').getElementsByClassName('chkMatrix');
						if (chk_els != null){
							for (i=0;i<chk_els.length;i++){
								if (chk_els[i].name == el.name && chk_els[i].id == el.id){
									if (chk_els[i].hasClassName('hasSubsection')){												
										c_subsection = $('subsection_'+ +chk_els[i].id);
										if (c_subsection != null){
											if(!c_subsection.hasClassName('subsection')){
											c_subsection.addClassName('subsection');
											}
										}									
									}
								}
							}
						}
									
					}
				}else
				{
					if(!subsection.hasClassName('subsection'))
					{
						subsection.addClassName('subsection');	
					}
				}
				break;
				
				case 'radio':
				if (el.checked)
				{  
				   if(subsection)
				   {
				   if(subsection.hasClassName('subsection'))
					{
						subsection.removeClassName('subsection');
						new Effect.Highlight(subsection);
						r_els = $('section').getElementsByClassName('radioGroup');
						for (i=0; i< r_els.length; i++)
						{
							if(r_els[i].name == el.name&&r_els[i].id!==el.id)
							{
								if(r_els[i].hasClassName('hasSubsection'))
								{
									r_subsection = $('subsection_'+r_els[i].id);
									if(!r_subsection.hasClassName('subsection'))
										{
											r_subsection.addClassName('subsection');	
										}
								}
							}
						}
					}
					}else
					{   
					    r_els = $('section').getElementsByClassName('radioGroup');
						for (i=0; i< r_els.length; i++)
						{
							if(r_els[i].name == el.name)
							{
								if(r_els[i].hasClassName('hasSubsection'))
								{
									//alert(r_els[i].id)
									r_subsection = $('subsection_'+r_els[i].id);
									if(!r_subsection.hasClassName('subsection'))
										{
											r_subsection.addClassName('subsection');	
										}
								}
							}
						}
					}
				}
				break;
			}
  },
  renderRows: function()
  {
  	num_rows = $('num_rows').value;
  	//alert(num_rows);
	$('rows').style.display ='';
	$('row_titles').innerHTML = '';
	for (i=1; i<=num_rows;i++)
	{
	my_input = document.createElement('input');
	Element.extend(my_input);
	my_input.id = "row_"+i;
	my_input.name = "rows[]";
	// insert it in the document
	$('row_titles').appendChild(my_input);
	my_br =  document.createElement('br');
	Element.extend(my_br);	
	$('row_titles').appendChild(my_br);
	}
  },
  renderCols: function()
  {
    num_cols = $('num_cols').value;
  	//alert(num_cols);
	$('cols').style.display ='';
	$('col_titles').innerHTML = '';
	for (i=1; i<=num_cols;i++)
	{
	my_input = document.createElement('input');
	Element.extend(my_input);
	my_input.id = "col_"+i;
	my_input.name = "cols[]";
	// insert it in the document
	$('col_titles').appendChild(my_input);
	my_br =  document.createElement('br');
	Element.extend(my_br);	
	$('col_titles').appendChild(my_br);
	}
  }
  
}
Event.observe(window, 'load',
      function() { Survey.init() }
   );
