objSurveyGroup = function() {
  this.name = null;
  this.max = (arguments.length>0 ? arguments[0] : 9999999);
  this.elements = new Array();
  if( arguments.length>1 ){
    for( var i=1; i<arguments.length; i++ ) {
      var element = arguments[i];
      this.elements[this.elements.length] = new function() { this.name="a"+element; this.value=element }
    }
  }
  this.getElementIndex = function(groupName) {
    for( var z=0; z<this.elements.length; z++ ) if( this.elements[z].name == groupName ) return z;
    return -1;
  }//getElementIndex
  this.getTextName = function(groupIndex) {
    return String( this.name+'_'+this.elements[groupIndex].value );
  }//getTextName
}// objSurveyGroup


var Survey = new function() {
  this.groups = new Array();

  this.add = function(name) {
    this.groups[name] = (arguments.length==2 ? arguments[1] : new objSurveyGroup());
    this.groups[name].name = name;
  }

  this.addM = function() {
    for(i=0;i<arguments.length;i++) {
      this.groups[arguments[i]] = new objSurveyGroup();
      this.groups[arguments[i]].name = arguments[i];
    }
  }

  this.objRef = function(groupName) { return groupName ? eval("document.forms['surveyForm']."+groupName) : null }

  this.checkGroup  = function(groupName,groupIndex) {//sprawdza grupe - jej element (groupIndex) po wywolaniu zdarzenia
    var groupRef = this.objRef(groupName);

    if( groupRef[groupIndex] ) { //grupa elementow
      if( groupRef[groupIndex].checked && groupRef[groupIndex].type=='radio')
        for(var i=0; i<this.groups[groupName].elements.length; i++ )
          if( i!=groupIndex ) { this.clearText(groupName,i); this.deselectBranch(this.groups[groupName].elements[i].name); }

      if( !groupRef[groupIndex].checked && groupRef[groupIndex].type=='checkbox' ) {
        this.clearText(groupName,groupIndex); this.deselectBranch(this.groups[groupName].elements[groupIndex].name);
      }

      if( !this.checkMax(groupName) ) return false;
      if( groupRef[groupIndex].checked ) return this.selectPath(groupName);
    } else { //tylko jeden element
      if( !groupRef.checked && groupRef.type=='checkbox' ) {
        this.clearText(groupName,groupIndex); this.deselectBranch(this.groups[groupName].elements[groupIndex].name);
      }

      if( !this.checkMax(groupName) ) return false;
      if( groupRef.checked ) return this.selectPath(groupName);
    }
    return true;
  }//checkGroup

  this.deselectBranch = function(groupName) {//odznacza cala grupe i wszystkie podgrupy
    if( !groupName ) return;
    var groupRef = this.objRef(groupName);
    for( var i=0; i<this.groups[groupName].elements.length; i++ ) {
      if( groupRef ) groupRef[i].checked = false;
      this.clearText( groupName, i );
      this.deselectBranch( this.groups[groupName].elements[i].name );
    }
  }//deselectBranch

  this.selectPath = function(childName) {//zaznacza sciezke od podanego dziecka do najwyzszego rodzica
    for(var name in this.groups)
      for(var i=0; i<this.groups[name].elements.length; i++) {
        var parentIndex = this.groups[name].getElementIndex(childName);
        if( parentIndex>-1 ) { //znalazl rodzica dla childName
          var parentRef = this.objRef(name);
          if( parentRef && !parentRef[parentIndex].checked ) {
            parentRef[parentIndex].checked = true;
            parentRef[parentIndex].checked = this.checkGroup(name,parentIndex);
            return parentRef[parentIndex].checked;
          } else return this.selectPath(name);//if( parentRef && !parentRef[parentIndex].checked )
        } //if( parentIndex>-1 )
      }//for
    return true;
  }//selectPath

  this.clearText = function(groupName,groupIndex) {
    var textRef = this.objRef( this.groups[groupName].getTextName(groupIndex) );
    if( textRef ) textRef.value = '';
    return true;
  }//clearText

  this.checkMax = function(groupName) {//sprawdzenie maxa
    if( !this.groups[groupName].max ) return true;
    var groupRef = this.objRef(groupName);
    var choiceCount = 0;
    for( var i = 0; i < groupRef.length; i++ ) if( groupRef[i].checked ) choiceCount++;

    if( choiceCount > this.groups[groupName].max ) {
      alert('Zaznaczono zbyt wiele odpowiedzi.\nDopuszczalna liczba odpowiedzi na to pytanie wynosi: '+this.groups[groupName].max+'.' );
      return false;
    }
    return true;
  }//checkMax

  this.checkText = function(groupName,groupIndex) {//przy wpisaniu do pola zaznacza przed nim kontrolke odznacza przy pustej wartosci
    var groupRef = this.objRef(groupName);
    var textRef = this.objRef( this.groups[groupName].getTextName(groupIndex) );
    if( textRef.value.length ) {
      if( groupRef ) {
        if( groupRef[groupIndex] ) { //grupa elementow
          groupRef[groupIndex].checked = true;
          groupRef[groupIndex].checked = this.checkGroup(groupName,groupIndex);
          if( !groupRef[groupIndex].checked ) textRef.value='';
        } else { //tylko jeden element
          groupRef.checked = true;
          groupRef.checked = this.checkGroup(groupName,groupIndex);
          if( !groupRef.checked ) textRef.value='';
        }
      } else if( !this.selectPath(groupName) ) textRef.value='';
    } else {
      if( groupRef ) {
        if( groupRef[groupIndex] ) { //grupa elementow
          groupRef[groupIndex].checked = false;
          this.checkGroup(groupName,groupIndex);
        } else { //tylko jeden element
          groupRef.checked = false;
          this.checkGroup(groupName,groupIndex);
        }
      }//if( groupRef )
    }//if( textRef.value.length )
  }//checkText


  this.checkFakeText = function(groupName,groupIndex) {//przy wpisaniu do pola zaznacza przed nim kontrolke odznacza przy pustej wartosci
    var groupRef = this.objRef(groupName);

      var textRef = this.objRef( this.groups[groupName].getTextName(groupIndex) );

    if( textRef.length ) {
      if( groupRef ) {
        if( groupRef[groupIndex] ) { //grupa elementow
          groupRef[groupIndex].checked = true;
          groupRef[groupIndex].checked = this.checkGroup(groupName,groupIndex);
          //if( !groupRef[groupIndex].checked ) textRef.value='';
        } else { //tylko jeden element
          groupRef.checked = true;
          groupRef.checked = this.checkGroup(groupName,groupIndex);
          //if( !groupRef.checked ) textRef.value='';
        }
      } //else if( !this.selectPath(groupName) ) textRef.value='';
    } else {
      if( groupRef ) {
        if( groupRef[groupIndex] ) { //grupa elementow
          groupRef[groupIndex].checked = false;
          this.checkGroup(groupName,groupIndex);
        } else { //tylko jeden element
          groupRef.checked = false;
          this.checkGroup(groupName,groupIndex);
        }
      }//if( groupRef )
    }//if( textRef.length )
  }//checkFakeText
}//Survey
