function GroupForm(obj, group_var, selall_var, multiact_enable)
{
   this.obj = obj;
   this.group_obj = this.obj[group_var];
   this.selall_obj = this.obj[selall_var];
   this.multiact_enable = multiact_enable;

   this.timeout = 500;

   this.onSelectAllClick = GroupForm_onSelectAllClick;
   this.onClick = GroupForm_onClick;
   this.setAll = GroupForm_setAll;
   this.selectAll = GroupForm_selectAll;
   this.deselectAll = GroupForm_deselectAll;
   this.validateSelection = GroupForm_validateSelection;
   this.leaveOneSelected = GroupForm_leaveOneSelected;
   this.reachSelected = GroupForm_reachSelected;
   this.navtoEach = GroupForm_navtoEach;
   this.submitEach = GroupForm_submitEach;
   this.submitGroup = GroupForm_submitGroup;
   this.submit = GroupForm_submit;
}

function GroupForm_onSelectAllClick()
{
   if ( this.group_obj && this.selall_obj ) 
   {
      if ( this.group_obj.length ) 
      {
         for(i=0; i<this.group_obj.length; i++) 
         {
            this.group_obj[i].checked = this.selall_obj.checked;
         }
      }
      else 
      {
         this.group_obj.checked = this.selall_obj.checked;
      }
   }
}

function GroupForm_onClick()
{  
   var AllChecked = true;
   
   if ( this.group_obj && this.selall_obj ) 
   {
      if ( this.group_obj.length ) 
      {
         for(i=0; i<this.group_obj.length; i++) 
         {
            if ( !this.group_obj[i].checked ) 
            {
               AllChecked = false;

               break;
            }
         }
      }
      else 
      {
         AllChecked = this.group_obj.checked;
      }

      this.selall_obj.checked = AllChecked;
   }
}

function GroupForm_setAll(state)
{
   if ( this.group_obj && this.selall_obj ) 
   {
      if ( this.group_obj.length ) 
      {
         for(i=0; i<this.group_obj.length; i++) 
         {
            this.group_obj[i].checked = state;
         }
      }
      else 
      {
         this.group_obj.checked = state;
      }

      this.selall_obj.checked = state;
   }
}

function GroupForm_selectAll()
{
   this.setAll(true);
}

function GroupForm_deselectAll()
{
   this.setAll(false);
}

function GroupForm_validateSelection(do_alert)
{ 
   var check_found = false;

   if ( this.group_obj ) 
   {
      if ( this.group_obj.length ) 
      {
         for(i=0; i<this.group_obj.length; i++) 
         {
            if ( this.group_obj[i].checked ) 
            {
               check_found = true;
               break;
            }
         }
      }
      else 
      {
         if ( this.group_obj.checked ) 
         {
            check_found = true;
         }
      }
   }

   if ( check_found ) 
   {
      return true;
   }
   else 
   { 
      if ( do_alert )
      {
         alert("Ни одной записи не отмечено! Отметьте хотя бы одну запись и повторите действие.");
      }
      return false;
   }
}

function GroupForm_leaveOneSelected()
{ 
   var retval = -1;
   var multi = false;

   if ( this.group_obj && this.selall_obj ) 
   {
      if ( this.group_obj.length ) 
      {
         for(i=0; i<this.group_obj.length; i++) 
         {
            if ( this.group_obj[i].checked ) 
            {
               retval = this.group_obj[i].value;
               break;
            }
         }

         for( i++; i<this.group_obj.length; i++) 
         {
            if ( this.group_obj[i].checked )
            {
               multi = true;
            }
            this.group_obj[i].checked = false;
         }
      }
      else 
      {
         if ( this.group_obj.checked ) 
         {
            retval = this.group_obj.value;
         }
      }

      this.selall_obj.checked = false;
   }

   if ( multi )
   {
      setTimeout("tmp = 1", this.timeout);
   }

   return retval;
}

function GroupForm_reachSelected(nav)
{
   var first_index = -1;

   if ( this.group_obj && this.validateSelection(true) ) 
   {
      if ( this.multiact_enable )
      {
         if ( this.group_obj.length ) 
         {
            for (i = 0; i < this.group_obj.length; i++) 
            {
               if ( this.group_obj[i].checked )
               {
                  if ( first_index == -1 )
                  {
                     first_index = i;
                     continue;
                  }
                  else
                  {
                     nav.go(this.group_obj[i].value, "_blank");
                  }
               }
            }

            nav.go(this.group_obj[first_index].value, "_self");
         }
         else
         {
            if ( this.group_obj.checked ) 
            {
               nav.go(this.group_obj.value, "_self");
            }
         }
      }
      else
      {
         nav.go(this.leaveOneSelected(), "_self");
      }
   }
}

function GroupForm_navtoEach(uri_templ, arg_name)
{
   var nav = new Nav(null, uri_templ, arg_name);
   this.reachSelected(nav);
}
 
function GroupForm_submitEach(uri_templ, arg_name, todo_value)
{
   var nav = new Nav(this.obj, uri_templ, arg_name);
   this.obj.todo.value = todo_value;
   this.reachSelected(nav);
}

function GroupForm_submitGroup(todo_value, action_value)
{
   if ( this.validateSelection(true) ) 
   {
      this.submit(todo_value, action_value);
   }
}

function GroupForm_submit(todo_value, action_value)
{
   this.obj.todo.value = todo_value;
   if ( typeof(action_value) != "undefined" )
   {
      this.obj.action = action_value;
   }
   this.obj.submit();
}

