

//------------------------------------------------------------------------------
function Set_Object_Display(_obj_id, _set, mode)
{
 var obj = document.getElementById(_obj_id);

 if (obj == null)
   return;

 if (_set == 'none') {
   obj.style.display = 'none';
 } else if (_set == 'auto') {
   if ((obj.style.display == 'none') || (obj.style.display == ''))
     obj.style.display = mode;
   else
     obj.style.display = 'none';
 } else
   obj.style.display = _set;
}


//------------------------------------------------------------------------------
// Polls

//--------------------------------------------
function CheckPollQuestionType()
{
  var question = document.forms['question'];

  if ((question.question_type.selectedIndex == 8) || (question.question_type.selectedIndex == 7)) {
    Set_Object_Display('questions_answers_div', 'none');
  } else
    Set_Object_Display('questions_answers_div', 'block');

  if ((question.question_type.selectedIndex == 5) || (question.question_type.selectedIndex == 6)) {
    Set_Object_Display('matrix_question_input', 'block');
  } else
    Set_Object_Display('matrix_question_input', 'none');

  if ((question.question_type.selectedIndex >= 2) && (question.question_type.selectedIndex <= 8)) {
    Set_Object_Display('add_open_question_input', 'block');
  } else
    Set_Object_Display('add_open_question_input', 'none');

  if (question.question_type.selectedIndex == 7) {
    Set_Object_Display('questions_answers_div', 'none');
    Set_Object_Display('multi_radio_div', 'block');
  } else {
    Set_Object_Display('multi_radio_div', 'none');
  }
  
  // tryb wyświetlania odpowiedzi w pytaniu
  if ((question.question_type.selectedIndex == 2) || (question.question_type.selectedIndex == 3)) {
    Set_Object_Display('answers_show_mode_block', 'block');
  } else
    Set_Object_Display('answers_show_mode_block', 'none');
  
  // tryb wyświetlania odpowiedzi w pytaniu
  if (question.question_type.selectedIndex == 8) {
    Set_Object_Display('slider_params', 'block');
  } else
    Set_Object_Display('slider_params', 'none');
}


//--------------------------------------------
function AddOpenQuestionClick(el)
{
  if (el.checked)
    Set_Object_Display('open_answer', 'block');
  else
    Set_Object_Display('open_answer', 'none');
}


//--------------------------------------------
function CheckSitesCheckboxes(el)
{
  var tmp = el.name.split('_');
  var el_id = tmp[2], id;
  var state = el.checked;

  var inputs = document.body.getElementsByTagName('input');
  for (var i = 0; i < inputs.length; i++) {
    id = inputs[i].name.split('_')[2];
    if (id == el_id)
      inputs[i].checked = state;
  }
}


function ObjToString(obj) {
 var ret = '';
 for (var prop in obj)
   ret += "  " + prop + " is " + obj[prop] + ";\n";
 return ret;
}


//------------------------------------
var myMnt = {
  base: '',
  sufix: '',

  //---------------------------------------------------------
  // Ajax.Updater
  aU: function (id, target)
  {
    new Ajax.Updater(id, target, {asynchronous:true, evalScripts:true, requestHeaders:['X-Update', id]});
  },

  //---------------------------------------------------------
  // Ajax.Updater - Form
  aUF: function (id, target, form)
  {
    new Ajax.Updater(id, target, {asynchronous:true, evalScripts:true, parameters:Form.serialize(form), requestHeaders:['X-Update', id]});
  },

  //---------------------------------------------------------
  ajaxIcon: function(target)
  {
    Element.update(target, myLang.get('loading') + '... <img src="/files/images/ajax-loader.gif" />');
  },

  // aktualizacja listy otwartych odpowiedzi
  updateOpen: function()
  {
    this.ajaxIcon('data_block');
    this.aUF('data_block', this.base + 'ajax_r_open' + this.sufix, 'settings_form');
    this.hide_settings_block();
  },

  // aktualizacja listy otwartych odpowiedzi
  updateHistogram: function()
  {
    this.ajaxIcon('data_block');
    this.aUF('data_block', this.base + 'ajax_r_report' + this.sufix, 'settings_form');
    this.hide_settings_block();
  },

  // aktualizacja dashboarda
  updateDashboard: function()
  {
    this.ajaxIcon('data_block');
    this.aU('data_block', this.base + 'ajax_r' + this.sufix);
  },

  // aktualizacja raportu o serwisie
  updateSiteReport: function()
  {
    this.ajaxIcon('data_block');
    this.aUF('data_block', this.base + 'ajax_r_site' + this.sufix, 'settings_form');
    this.hide_settings_block();
  },

  // aktualizacja raportu o stronie
  updatePagesReport: function()
  {
    this.ajaxIcon('data_block');
    this.aUF('data_block', this.base + 'ajax_r_pages' + this.sufix, 'settings_form');
    this.hide_settings_block();
  },
  
  // aktualizacja odpowiedzi na pytania klientów
  updateClientQuestionsReport: function()
  {
    this.ajaxIcon('data_block');
    this.aUF('data_block', this.base + 'ajax_r_client' + this.sufix, 'settings_form');
    this.hide_settings_block();
  },
  
  // aktualizacja szczegolowych odpowiedzi - lista wypelnien
  updateDetailReport: function(sufix)
  {
  	this.sufix = sufix || this.sufix;
    this.ajaxIcon('data_block');
    this.aUF('data_block', this.base + 'ajax_r_detail_list' + this.sufix, 'settings_form');
    this.hide_settings_block();
  },

  // aktualizacja raportu o stronie
  updatePrepQuestions: function()
  {
    var select = $('add_question2').elements[ 'prep_groups' ];
    val_s = select.options[ select.selectedIndex ].value; 
    this.ajaxIcon('prep_questions_blk');
    this.aU('prep_questions_blk', this.base + 'ajax_prep_questions/' + val_s);
  },
  
  exportData: function()
  {
  	els = $('settings_form').elements['data_format'];
  	for (var i in els) {
  		if (els[i].checked)
  			return true;
  	}
  	
  	return false;
  },

  show_settings_block: function()
  {
    Element.hide('show_settings_block');
    Element.show('settings_block');
  },

  hide_settings_block: function()
  {
    Element.hide('settings_block');
    Element.show('show_settings_block');
  },

  // INDICATORS FUNCTIONS
  indicatorsCheckboxes: {
    // obserwowanie liczby zaznaczonych checkboxów
    observe: function()
    {
      var indicators = $('settings_form').getInputs('checkbox', 'indicators[]');
      var indicators_a = $A(indicators);

      indicators_a.each(function(inp) {
        new Form.Element.EventObserver(inp, function() { myMnt.indicatorsCheckboxes.check(); });
      });
    },

    check: function()
    {
      var indicators = $('settings_form').getInputs('checkbox', 'indicators[]');
      var indicators_a = $A(indicators);
      var selected = 0;

      indicators_a.each(function(inp) {
        if (inp.checked)
          selected++;
      });

      if (selected >= 4) {
        indicators_a.each(function(inp) {
          if (!inp.checked)
            inp.disabled = true;
        });
      } else {
        indicators_a.each(function(inp) {
          if (inp.disabled)
            inp.disabled = false;
        });
      }
    }
  }
}


//--------------------------------------------------------------------
function myInit()
{
  var inputs = document.getElementsByTagName('input');
       
  for (var i = 0; i < inputs.length; i++) {
    var inp = inputs[i];
    if ((inp.type == 'submit') || (inp.type == 'button')) {
      if ((inp.parentNode != 'null') && (inp.parentNode.parentNode != 'null')) {
      	if (inp.parentNode.parentNode.className == 's2') {
      		inp.onmouseover = function () {
      			this.parentNode.parentNode.className = 's1';  
      		}
        
      		inp.onmouseout = function () {
      			this.parentNode.parentNode.className = 's2';
      		}
      	} else if (inp.parentNode.parentNode.className == 's4') {
      		inp.onmouseover = function () {
      			this.parentNode.parentNode.className = 's3';  
      		}
        
      		inp.onmouseout = function () {
      			this.parentNode.parentNode.className = 's4';
      		}
      	}
      }
    }
  }
}

window.onload = myInit;



//------------------------------------
var myFilterUpdater = {
		names: {
			c: {
				name: myLang.get('filter_screen_color_depth'),
				field: 'filter_colours_val'
			},
			
			r: {
				name: myLang.get('filter_resolution'),
				field: 'filter_resolutions_val'
			},
			
			s: {
				name: myLang.get('filter_operating_system'),
				field: 'filter_systems_val'
			},
			
			b: {
				name: myLang.get('filter_browser'),
				field: 'filter_browsers_val'
			},
			
			sg: {
				name: myLang.get('filter_segment'),
				field: 'filter_segments_val'
			}
		},
		
		//---------------------------------------------------------
		update: function(data)
		{
			var txt = myLang.get('no_filters_defined'), j = 0;
			
			for (var key in this.names) {
				if (typeof(data[key]) != 'undefined') {
					val = data[key];
				
					if (j++)
						txt += '.&nbsp;&nbsp;';
					else
						txt = '';
				
					txt += this.getKeyTxt(key, val)
				}
			}
			
			Element.update('show_settings_filters', txt);
		},
		
		//---------------------------------------------------------
		getKeyTxt: function(key, val)
		{
			txt = this.names[key].name + ': ' 
				
			select = $('settings_form').elements[ this.names[key].field ];
			for (var i=0; i < select.length; i++) {
				if (select.options[i].value == val) {
					txt += select.options[i].text;
						
					break;
				}
			}
			
			return txt;
		}
}


//------------------------------------
var myLanguageManager = {
	base: '',
	file: '',

	update: function()
	{
		select = $('languages_form').elements[ 'base_language' ];
		val_b = select.options[ select.selectedIndex ].value; 
			
		select = $('languages_form').elements[ 'edit_language' ];
		val_e = select.options[ select.selectedIndex ].value;
		
		document.location = this.base + val_b + '/' + val_e + '/' + this.file; 
	}
}

