awk script to calculate total

Hi
First field is the Record Type. A Record Type 5 can have multiple Record Type 6's before another Record Type 5 appears.

I want to calculate the total of fields at position 8-11 on Record type 6 when Record Type 5 has a field at position 11-14 equals to '2222'. then it should delete the lines of Record Type 6 except the the first Record Type 6 for that Record Type 5 only. Please help

1 101 201 301
5 test 4444 1233
6 1212 1222 3434 33
7 6464 122
8 3664 123
5 test 2222 1233
6 1212 1222 3434 33
7 4464 322
6 1212 4222 3434 33
7 4464 522
6 1212 6222 3434 33
6 1212 7222 3434 33
8 8664 823
5 test 4444 1233
6 1212 1222 3434 33
7 2464 122
8 3664 123
5 test 4444 1233
6 1212 1222 3434 33
7 7464 122
8 5664 123
5 test 2222 1233
6 61212 1222 3434 33
7 6764 122
8 9664 123
9 3332434 334344

so the output should be

1 101 201 301
5 test 4444 1233
6 1212 1222 3434 33
7 6464 122
8 3664 123
5 test 2222 1233
6 1212 18888 3434 33
7 4464 322
7 4464 522
8 8664 823
5 test 4444 1233
6 1212 1222 3434 33
7 2464 122
8 3664 123
5 test 4444 1233
6 1212 1222 3434 33
7 7464 122
8 5664 123
5 test 2222 1233
6 61212 1222 3434 33
7 6764 122
8 9664 123
9 3332434 334344

If the data is as shown in your sample data, the following works

BEGIN { state=sum=0 }
{
   if (state == 0) {
      print
      if ( $1 == "5" && $3 == "2222" ) {
          state=1
          sum=0
      }
   } else {
      if ( $1 == "6" && state == 1 ) {
          tmp6=$0
          tmp7="XXX"
          state=2
      }
      if ( state == 2 ) {
         if ( $1 == "6" ) { sum = sum + $3 }
         if ( $1 == "7" ) {
            if (tmp7 == "XXX")
                tmp7=$0
            else
                tmp7=tmp7"\n"$0
         }
      }
      if ( $1 == "8" ) {
        split(tmp6, a ," ")
        print a[1], a[2], sum, a[4], a[5]
        print tmp7
        print
        state=0
      }
   }
}

hi
The script should be irrespective of record Type 7,8. It should depend on Record Type 5 and 6. so basically every Record Type 5 can have multiple Record Type 6's. So I want to calculate the total for each group of Record Type 6's when field on Record Type 5 is equal to '2222'

I appreciate ur help.
Please help