/**
 * sotarQUERY
 *
 * The sotarQUERY is an accessible web questionary management system that
 * specializes in accessibility and generates W3C-compliant HTML code.
 *
 * PHP version 5
 * @copyright  sotarsoft 2007
 * @author     sotarsoft <info@sotarsoft.pl>
 * @package    Frontend
 * @filesource $Source: c:\cvsroot/educentrum/ankieta/js/script.js,v $
 * @version    file: $Revision: 1.4 $
 * @version    system: sotarQUERY v.0.8
 */

  var CheckboxWatcher = Class.create();
  var CheckboxSubWatcher = Class.create();
  var SubTypeWatcher = Class.create();
  var SubTypeArrWatcher = Class.create();
  
  CheckboxWatcher.prototype = {
  
      initialize: function(radiobox, groupbox, count, onChange) {

		  this.radio_name = radiobox;
 	  	  this.chkbox_name = groupbox;
 	  	  this.count = count;

		  this.radio_no = $(radiobox+"_Nie");
		  this.radio_yes = $(radiobox+"_Tak");
 	  	  this.chkbox = new Array(count);
 	  	  this.onChange = onChange;

          this.radio_no.onclick =
                this.update.bindAsEventListener(this);
          this.radio_yes.onclick =
                this.update.bindAsEventListener(this);

		  var n = count;
		  var _this = this;
		  n.times(function(value, index) {
		  	 var text = groupbox+"_"+(index+1);
		  	 _this.chkbox[index] = $(text);
		  }); 
		  
		  this.update();

	  },
	  
	  update: function(evt) {

		  var on_off = false;   //TAK
		  if (($F(this.radio_name+"_Nie")==null && $F(this.radio_name+"_Tak")==null) || $F(this.radio_name+"_Nie")=="Nie") {
			  on_off = true;
		  }
		  
		  var n = this.count;
		  var _this = this;
		  if (on_off) {
	 	     n.times(function(value, index) {
		   	    _this.chkbox[index].disabled = true;
		   	    _this.chkbox[index].addClassName = "disabled";
		   	    if (_this.chkbox[index].type == "checkbox") {
					_this.chkbox[index].checked = false;
				} else if (_this.chkbox[index].type == "text") {
					_this.chkbox[index].value = "";
				}
		     });
		     if (typeof(this.onChange) == "function") {
				this.onChange("off");
			 }
		  } else {
	 	     n.times(function(value, index) {
		   	    _this.chkbox[index].disabled = false;
		   	    _this.chkbox[index].removeClassName = "disabled";
		     });			
		     if (typeof(this.onChange) == "function") {
				this.onChange("on");
			 }
		  }
		  
	  }
  };  

  CheckboxSubWatcher.prototype = {
  
      initialize: function(checkbox, text) {

		  this.checkbox_name = checkbox;
 	  	  this.text_name = text;

 	  	  this.checkbox = $(checkbox);
 	  	  this.text = $(text);

          this.checkbox.onclick =
                this.update.bindAsEventListener(this);

		  this.update();

	  },
	  
	  update: function(evt) {		  
		  if (this.checkbox.checked) {
		  	this.text.disabled = false;
		   	this.text.removeClassName = "disabled";
		  } else {
		  	this.text.disabled = true;
		   	this.text.addClassName = "disabled";
		  }		  
	  }
  };  

  SubTypeWatcher.prototype = {
	
	 initialize: function(field, subfield) {

		  this.field_name = field;
 	  	  this.subfield_name = subfield;

		  this.field = $(field);
		  this.subfield = $(subfield);

          this.field.onchange =
                this.update.bindAsEventListener(this);
          this.field.onkeypress =
                this.update.bindAsEventListener(this);

		  this.update();
		  		
	 },
	 
	 update: function(evt) {
	 	  var value = $F(this.field_name);
		  if (value && value.length > 0) {
			  this.subfield.disabled = false;
		      this.subfield.removeClassName = "disabled";
		  } else {
			  this.subfield.disabled = true;			
		      this.subfield.addClassName = "disabled";
		  }
	 }
	
  };


  SubTypeArrWatcher.prototype = {
	
	 initialize: function(field, subfields, onChange) {

		  this.field_name = field;
 	  	  this.subfields_name = subfields;
 	  	  this.onChange = onChange;

		  this.field = $(field);
		  this.subfields = new Array();
		  for (i=0; i<subfields.length; i++) {
			  this.subfields[i] = $(subfields[i]);
		  }

          this.field.onchange =
                this.update.bindAsEventListener(this);
          this.field.onkeypress =
                this.update.bindAsEventListener(this);

		  this.update();
		  		
	 },
	 
	 update: function(evt) {
	 	  var value = $F(this.field_name);
		  if (value && value.length > 0) {
		  	  for (i=0; i<this.subfields.length; i++) {
		      	 this.subfields[i].removeClassName = "disabled";
			     this.subfields[i].disabled = false;
			  }
			  if (typeof(this.onChange) == 'function') {
			     this.onChange('on');	
			  }
		  } else {
		  	  for (i=0; i<this.subfields.length; i++) {
		      	 this.subfields[i].addClassName = "disabled";
			     this.subfields[i].disabled = true;
			  }
			  if (typeof(this.onChange) == 'function') {
			     this.onChange('off');	
			  }
		  }
	 }
	
  };
