static void get_country(gint qnum, gchar *call_px)
{
  gint i, j, n, comma, comma_match, colon, colon_match, slash, lcl, imc;
  gint isci, ipartp, lzic, izn, addit;
  guint answer;
  gint matching_country_index[20][2], lcp[2];
  gchar call_in[14], c_syn[14], call_part[2][14], msg[51], zone[3];
  gint cstart=0, match=-1, ici=0, icant_tell=TRUE;
  struct pick_box_vars pbv_hold;

  countries.icountry_index = 0;
  countries.iplus_country_index = 0;
  countries.izone_index = 0;
  pbv.num_match = 0;
  for(i=0; i<20; i++)
   {
    pbv.pick_index[i] = 0;
    for(j=0; j<2; j++)
     matching_country_index[i][j] = 0;
   }
  strcpy(call_in, cr_array[qnum].call);
  lcl = strlen(call_in);
  isci = kb_index (call_in, "/");
  if(isci>0 && isci<lcl-1)
   {
    concat(call_part[0], call_in, 0, isci-1, COPY);
    concat(call_part[1], call_in, isci+1, lcl-1, COPY);
    lcp[0] = strlen(call_part[0]);
    lcp[1] = strlen(call_part[1]);
    if(lcp[0]!=lcp[1])
     {
      if(lcp[0]<lcp[1])
       ipartp = 0;
      else
       ipartp = 1;
      switch (lcp[ipartp]) 
       {
        case 1 :
/* xxxxx/(n,y)
   (n,y)/xxxxx */
         if(lcl>4)
          {
           icant_tell = FALSE;
           if(isdigit(call_part[ipartp][0]))
            {
             for(i=lcp[!ipartp]-1; i>0; i--)
              if(isdigit(call_part[!ipartp][i]))
               {
                call_part[!ipartp][i] = call_part[ipartp][0];
                strcpy(call_in, call_part[!ipartp]);
               }
            }
           else
            switch (call_part[ipartp][0])
             {
              case 'F' :
              case 'I' :
              case 'K' :
              case 'N' :
              case 'W' :
               strcpy(call_in, call_part[ipartp]);
               break;
              case 'G' :
               if(kb_index (call_part[!ipartp], "FR"))
                strcpy(call_in, call_part[ipartp]);
               else
                {
                 strcpy(call_in, call_part[!ipartp]);
                 strcat(call_in, "/");
                 strcat(call_in, call_part[ipartp]);
                }
               break;
              case 'M' :
               play_bells (DUPE_SOUND);
               strcpy(msg, "Is station mobile? (Y/N)");
               answer = boxed_message (msg, 0, LT_GRAY);
               if(toupper_ui(answer)=='Y')
                strcpy(call_in, call_part[!ipartp]);
               else
                strcpy(call_in, call_part[ipartp]);
               break;
              default :
               strcpy(call_in, call_part[!ipartp]);
               strcat(call_in, "/");
               strcat(call_in, call_part[ipartp]);
             }
          }
         break;
        case 2 :
/* xxxxx/(yy,yn,ny)nn
   (yy,yn,ny)nn/xxxxx */
         if(lcl>5)
          {
           icant_tell = FALSE;
           if(isdigit(call_part[ipartp][0]) &&
              isdigit(call_part[ipartp][1]))
            strcpy(call_in, call_part[!ipartp]);
           else if(!strcmp(call_part[ipartp], "AM"))
            {
             play_bells (DUPE_SOUND);
             strcpy(msg, "Is station aeronautical mobile? (Y/N)");
             answer = boxed_message (msg, 0, LT_GRAY);
             if(toupper_ui(answer)=='Y')
              {
               if(isel_contest==WPX)
                strcpy(call_in, call_part[!ipartp]);
               else
                strcpy(call_in, "0");
              } 
             else
              strcpy(call_in, call_part[ipartp]);
            }
           else if(!strcmp(call_part[ipartp], "MM"))
            {
             play_bells (DUPE_SOUND);
             strcpy(msg, "Is station maritime mobile? (Y/N)");
             answer = boxed_message (msg, 0, LT_GRAY);
             if(toupper_ui(answer)=='Y')
              {
               if(isel_contest==WPX)
                strcpy(call_in, call_part[!ipartp]);
               else
                strcpy(call_in, "0");
              } 
             else
              strcpy(call_in, call_part[ipartp]);
            }
           else
            strcpy(call_in, call_part[ipartp]);
          }
         break;
        case 3 :
/* xxxxx/(nyn,yyn,nyy,yny,ynn)nnn,yyy,nny
   (nyn,yyn,nyy,yny,ynn)nnn,yyy,nny/xxxxx */
         if(lcl>7)
          {
           icant_tell = FALSE;
           if((isdigit(call_part[ipartp][0]) &&
               isdigit(call_part[ipartp][1]) &&
               isdigit(call_part[ipartp][2])) ||
              (isalpha(call_part[ipartp][0]) &&
               isalpha(call_part[ipartp][1]) &&
               isalpha(call_part[ipartp][2])) ||
              (isdigit(call_part[ipartp][0]) &&
               isdigit(call_part[ipartp][1]) &&
               isalpha(call_part[ipartp][2])))
            strcpy(call_in, call_part[!ipartp]);
           else if((!strcmp(call_part[ipartp], "AM1")) ||
                   (!strcmp(call_part[ipartp], "AM2")) ||
                   (!strcmp(call_part[ipartp], "AM3")))
            {
             play_bells (DUPE_SOUND);
             strcpy(msg, "Is station aeronautical mobile? (Y/N)");
             answer = boxed_message (msg, 0, LT_GRAY);
             if(toupper_ui(answer)=='Y')
              {
               if(isel_contest==WPX)
                strcpy(call_in, call_part[!ipartp]);
               else if(isel_contest==TEN || isel_contest==A160)
                {
                 concat(call_in, call_part[ipartp], 0, 1, COPY);
                 strcat(call_in, "-");
                 concat(call_in, call_part[ipartp], 2, 2, !COPY);
                }
               else
                strcpy(call_in, "0");
              } 
             else
              strcpy(call_in, call_part[ipartp]);
            }
           else if((!strcmp(call_part[ipartp], "MM1")) ||
                   (!strcmp(call_part[ipartp], "MM2")) ||
                   (!strcmp(call_part[ipartp], "MM3")))
            {
             play_bells (DUPE_SOUND);
             strcpy(msg, "Is station maritime mobile? (Y/N)");
             answer = boxed_message (msg, 0, LT_GRAY);
             if(toupper_ui(answer)=='Y')
              {
               if(isel_contest==WPX)
                strcpy(call_in, call_part[!ipartp]);
               else if(isel_contest==TEN || isel_contest==A160)
                {
                 concat(call_in, call_part[ipartp], 0, 1, COPY);
                 strcat(call_in, "-");
                 concat(call_in, call_part[ipartp], 2, 2, !COPY);
                }
               else
                strcpy(call_in, "0");
              } 
             else
              strcpy(call_in, call_part[ipartp]);
            }
           else
            {
             strcpy(call_in, call_part[ipartp]);
             if(!strcmp(call_in, "KC4"))
              strcpy(call_in, "KC4AA?");
             else if(!strcmp(call_in, "KG4"))
              strcpy(call_in, "KG4??");
             else if(!strcmp(call_in, "KC6"))
              strcpy(call_in, "KC6??");
             else if(!strcmp(call_in, "KX6"))
              strcpy(call_in, "KX6??");
            }
          }
         break;
        case 4 :
/* xxxxx/(yyny)
   (yyny)/xxxxx */
          if(isalpha(call_part[ipartp][0]) &&
             isalpha(call_part[ipartp][1]) &&
             isdigit(call_part[ipartp][2]) &&
             isalpha(call_part[ipartp][3]))
           {
            icant_tell = FALSE;
            strcpy(call_in, call_part[ipartp]);
           }
         break;
       }
     }
    else if(lcp[0]==4 && lcp[1]==4)
     for(n=1; n<=countries.number_of_countries; n++)
      {
       if(!strcmp(countries.country_abv[n], call_part[0]))
        {
         icant_tell = FALSE;
         strcpy(call_in, call_part[0]);
         break;
        }
       else if(!strcmp(countries.country_abv[n], call_part[1]))
        {
         icant_tell = FALSE;
         strcpy(call_in, call_part[1]);
         break;
        }
      }
    if(icant_tell)
     {
      play_bells (DUPE_SOUND);
      strcpy(msg, "Is country prefix ");
      strcat(msg, call_part[0]);
      strcat(msg, "? (Y/N)");
      answer = boxed_message (msg, 0, LT_GRAY);
      if(toupper_ui(answer)=='Y')
       strcpy(call_in, call_part[0]);
      else
       strcpy(call_in, call_part[1]);
     }
    lcl = strlen(call_in);
    isci = kb_index (call_in, "/");
   }
  strcpy(call_px, call_in);
  for(n=1; n<=countries.number_of_countries; n++)
   {
    comma = cstart-1;
    for(i=cstart; i<strlen(countries.country_syn[n]); i++)
     {
      if(countries.country_syn[n][i]==',')
       {
        concat(c_syn, countries.country_syn[n], comma+1, i-1, COPY);
        colon = kb_index (c_syn, ":");
        slash = kb_index (c_syn, "/");
        if(colon>-1)
         {
          colon_match = 0;
          if(lcl>=colon)
           for(j=0; j<colon; j++)
            {
             if(j<colon-2 || isci==-1 || slash==-1)
              {
               if((call_in[j]>=c_syn[j] &&
                call_in[j]<=c_syn[j+colon+1]) ||
                (lcl==colon && c_syn[j]=='?'))
                colon_match++;
               else
                break;
              }
             else if(call_in[isci+1]==c_syn[slash+1])
              {
               colon_match +=2;
               break;
              }
            }
          if(colon_match==colon)
           {
            match++;
            matching_country_index[match][0] = n;
            matching_country_index[match][1] = colon_match;
            if(match>0)
             {
              if(matching_country_index[match-1][0]==n)
               {
                if(matching_country_index[match-1][1]<
                 matching_country_index[match][1])
                 matching_country_index[match-1][1] =
                 matching_country_index[match][1];
                matching_country_index[match][0] = 0;
                matching_country_index[match][1] = 0;
                match--;
               }
             }
           }
         }
        else
         {
          comma_match = 0;
          imc = i-comma-1;
          if(lcl>=imc)
           for(j=0; j<imc; j++)
            {
             if(j<imc-2 || isci==-1 || slash==-1)
              {
               if(call_in[j]==c_syn[j] || (lcl==imc && c_syn[j]=='?'))
                comma_match++;
               else
                break;
              }
             else if(call_in[isci+1]==c_syn[slash+1])
              {
               comma_match +=2;
               break;
              }
            }
          if(comma_match==imc)
           {
            match++;
            matching_country_index[match][0] = n;
            matching_country_index[match][1] = imc;
            if(match>0)
             {
              if(matching_country_index[match-1][0]==n)
               {
                if(matching_country_index[match-1][1]<
                 matching_country_index[match][1])
                 matching_country_index[match-1][1] =
                 matching_country_index[match][1];
                matching_country_index[match][0] = 0;
                matching_country_index[match][1] = 0;
                match--;
               }
             }
           }
         }
        comma = i;
       }
     }
   }
  pbv.pick_index[0] = matching_country_index[0][0];
  for(n=1; n<=match; n++)
   {
    if(countries.country_abv[matching_country_index[n][0]][0]=='+')
     countries.iplus_country_index = matching_country_index[n][0];
    else if(matching_country_index[n][1]>matching_country_index[ici][1])
     {
      ici = n;
      pbv.num_match = 0;
      pbv.pick_index[pbv.num_match] = matching_country_index[n][0];
     }
    else if(matching_country_index[n][1]==matching_country_index[ici][1])
     {
      pbv.num_match++;
      pbv.pick_index[pbv.num_match] = matching_country_index[n][0];
     }
   }
  if(isel_contest==WW)
   {
    pbv_hold = pbv;
    pbv.num_match = -1;
    if(countries.iplus_country_index)
     {
      lzic =
       strlen(countries.zones_in_country[countries.iplus_country_index]);
      for(j=0; j<=lzic/3; j++)
       {
        pbv.num_match++;
        concat(zone,
               countries.zones_in_country[countries.iplus_country_index],
               j*3, j*3+1, COPY);
        pbv.pick_index[pbv.num_match] = atoi(zone);
       }
     }
    else
     {
      for(i=0; i<=pbv_hold.num_match; i++)
       {
        lzic = strlen(countries.zones_in_country[pbv_hold.pick_index[i]]);
        for(j=0; j<=lzic/3; j++)
         {
          if(!i)
           {
            pbv.num_match++;
            concat(zone,
                   countries.zones_in_country[pbv_hold.pick_index[i]],
                   j*3, j*3+1, COPY);
            pbv.pick_index[pbv.num_match] = atoi(zone);
           }
          else
           {
            concat(zone,
                   countries.zones_in_country[pbv_hold.pick_index[i]],
                   j*3, j*3+1, COPY);
            izn = atoi(zone);
            addit = TRUE;
            for(n=0; n<=pbv.num_match; n++)
             if(pbv.pick_index[n]==izn)
              {
               addit = FALSE;
               break;
              }
            if(addit)
             {
              pbv.num_match++;
              pbv.pick_index[pbv.num_match] = izn;
             }
           }
         }
       }
     }
    countries.izone_index = get_zi_from_rn (qnum);
    if(countries.izone_index==-1)
     {
      if(!pbv.num_match)
       countries.izone_index = pbv.pick_index[0];
      else
       {
        pick_zone_box (main_drawing_area, REDRAW);
        countries.izone_index = pbv.pick_index[pbv.item];
       }
     }
    int_to_char (countries.izone_index, zone);
    left_zero_pad (zone, sizeof(zone));
    cr_array[qnum].rec_num[3-mode] = zone[0];
    cr_array[qnum].rec_num[4-mode] = zone[1];
    cyan_bar (main_drawing_area);
    pbv = pbv_hold;
    n = -1;
    for(i=0; i<=pbv.num_match; i++)
     if(kb_index (countries.zones_in_country[pbv.pick_index[i]],
        zone)>-1)
      {
       n++;
       pbv_hold.pick_index[n] = pbv.pick_index[i];
      }
    pbv.num_match = n;
    if(pbv.num_match==-1)
     {
      pbv.num_match = 0;
      pbv.pick_index[0] = 0;
     }
    else
     for(i=0; i<=pbv.num_match; i++)
      pbv.pick_index[i] = pbv_hold.pick_index[i];
    if(pbv.num_match)
     {
      pbv.num_match++;
      pbv.pick_index[pbv.num_match] = 0;
      pick_country_box (main_drawing_area, REDRAW);
      countries.icountry_index = pbv.pick_index[pbv.item];
     }
    else
     countries.icountry_index = pbv.pick_index[0];
   }
  else
   {
    if(pbv.num_match)
     {
      pbv.num_match++;
      pbv.pick_index[pbv.num_match] = 0;
      pick_country_box (main_drawing_area, REDRAW);
      countries.icountry_index = pbv.pick_index[pbv.item];
     }
    else
     countries.icountry_index = matching_country_index[ici][0];
   }
}

static gint get_packet_country(const gchar *pbm_line, gchar *call_px)
{
  gint i, j, n, comma, comma_match, colon, colon_match, slash, lcl, imc;
  gint isci, ipartp;
  gint icountry_index, iplus_country_index;
  gint matching_country_index[20][2], lcp[2];
  gchar call_in[14], c_syn[14], call_part[2][14];
  gint cstart=0, match=-1, ici=0, icant_tell=TRUE, num_match=0;

  for(i=0; i<20; i++)
   for(j=0; j<2; j++)
    matching_country_index[i][j] = 0;
  concat(call_in, pbm_line, 8, strlen(pbm_line)-1, COPY);
  lcl = strlen(call_in);
  isci = kb_index (call_in, "/");
  if(isci>0 && isci<lcl-1)
   {
    concat(call_part[0], call_in, 0, isci-1, COPY);
    concat(call_part[1], call_in, isci+1, lcl-1, COPY);
    lcp[0] = strlen(call_part[0]);
    lcp[1] = strlen(call_part[1]);
    if(lcp[0]!=lcp[1])
     {
      if(lcp[0]<lcp[1])
       ipartp = 0;
      else
       ipartp = 1;
      switch (lcp[ipartp]) 
       {
        case 1 :
/* xxxxx/(n,y)
   (n,y)/xxxxx */
         if(lcl>4)
          {
           icant_tell = FALSE;
           if(isdigit(call_part[ipartp][0]))
            {
             for(i=lcp[!ipartp]-1; i>0; i--)
              if(isdigit(call_part[!ipartp][i]))
               {
                call_part[!ipartp][i] = call_part[ipartp][0];
                strcpy(call_in, call_part[!ipartp]);
               }
            }
           else
            switch (call_part[ipartp][0])
             {
              case 'F' :
              case 'I' :
              case 'K' :
              case 'N' :
              case 'W' :
               strcpy(call_in, call_part[ipartp]);
               break;
              case 'G' :
               if(kb_index (call_part[!ipartp], "FR"))
                strcpy(call_in, call_part[ipartp]);
               else
                {
                 strcpy(call_in, call_part[!ipartp]);
                 strcat(call_in, "/");
                 strcat(call_in, call_part[ipartp]);
                }
               break;
              case 'M' :
               strcpy(call_in, "0");
               break;
              default :
               strcpy(call_in, call_part[!ipartp]);
               strcat(call_in, "/");
               strcat(call_in, call_part[ipartp]);
             }
          }
         break;
        case 2 :
/* xxxxx/(yy,yn,ny)nn
   (yy,yn,ny)nn/xxxxx */
         if(lcl>5)
          {
           icant_tell = FALSE;
           if(isdigit(call_part[ipartp][0]) &&
              isdigit(call_part[ipartp][1]))
            strcpy(call_in, call_part[!ipartp]);
           else if(!strcmp(call_part[ipartp], "AM"))
            strcpy(call_in, "0");
           else if(!strcmp(call_part[ipartp], "MM"))
            strcpy(call_in, "0");
           else
            strcpy(call_in, call_part[ipartp]);
          }
         break;
        case 3 :
/* xxxxx/(nyn,yyn,nyy,yny,ynn)nnn,yyy,nny
   (nyn,yyn,nyy,yny,ynn)nnn,yyy,nny/xxxxx */
         if(lcl>7)
          {
           icant_tell = FALSE;
           if((isdigit(call_part[ipartp][0]) &&
               isdigit(call_part[ipartp][1]) &&
               isdigit(call_part[ipartp][2])) ||
              (isalpha(call_part[ipartp][0]) &&
               isalpha(call_part[ipartp][1]) &&
               isalpha(call_part[ipartp][2])) ||
              (isdigit(call_part[ipartp][0]) &&
               isdigit(call_part[ipartp][1]) &&
               isalpha(call_part[ipartp][2])))
            strcpy(call_in, call_part[!ipartp]);
           else if((!strcmp(call_part[ipartp], "AM1")) ||
                   (!strcmp(call_part[ipartp], "AM2")) ||
                   (!strcmp(call_part[ipartp], "AM3")))
            strcpy(call_in, "0");
           else if((!strcmp(call_part[ipartp], "MM1")) ||
                   (!strcmp(call_part[ipartp], "MM2")) ||
                   (!strcmp(call_part[ipartp], "MM3")))
            strcpy(call_in, "0");
           else
            {
             strcpy(call_in, call_part[ipartp]);
             if(!strcmp(call_in, "KC4"))
              strcpy(call_in, "KC4AA?");
             else if(!strcmp(call_in, "KG4"))
              strcpy(call_in, "KG4??");
             else if(!strcmp(call_in, "KC6"))
              strcpy(call_in, "KC6??");
             else if(!strcmp(call_in, "KX6"))
              strcpy(call_in, "KX6??");
            }
          }
         break;
        case 4 :
/* xxxxx/(yyny)
   (yyny)/xxxxx */
          if(isalpha(call_part[ipartp][0]) &&
             isalpha(call_part[ipartp][1]) &&
             isdigit(call_part[ipartp][2]) &&
             isalpha(call_part[ipartp][3]))
           {
            icant_tell = FALSE;
            strcpy(call_in, call_part[ipartp]);
           }
         break;
       }
     }
    else if(lcp[0]==4 && lcp[1]==4)
     for(n=1; n<=countries.number_of_countries; n++)
      {
       if(!strcmp(countries.country_abv[n], call_part[0]))
        {
         icant_tell = FALSE;
         strcpy(call_in, call_part[0]);
         break;
        }
       else if(!strcmp(countries.country_abv[n], call_part[1]))
        {
         icant_tell = FALSE;
         strcpy(call_in, call_part[1]);
         break;
        }
      }
    if(icant_tell)
     strcpy(call_in, "0");
    lcl = strlen(call_in);
    isci = kb_index (call_in, "/");
   }
  strcpy(call_px, call_in);
  for(n=1; n<=countries.number_of_countries; n++)
   {
    comma = cstart-1;
    for(i=cstart; i<strlen(countries.country_syn[n]); i++)
     {
      if(countries.country_syn[n][i]==',')
       {
        concat(c_syn, countries.country_syn[n], comma+1, i-1, COPY);
        colon = kb_index (c_syn, ":");
        slash = kb_index (c_syn, "/");
        if(colon>-1)
         {
          colon_match = 0;
          if(lcl>=colon)
           for(j=0; j<colon; j++)
            {
             if(j<colon-2 || isci==-1 || slash==-1)
              {
               if((call_in[j]>=c_syn[j] &&
                call_in[j]<=c_syn[j+colon+1]) ||
                (lcl==colon && c_syn[j]=='?'))
                colon_match++;
               else
                break;
              }
             else if(call_in[isci+1]==c_syn[slash+1])
              {
               colon_match +=2;
               break;
              }
            }
          if(colon_match==colon)
           {
            match++;
            matching_country_index[match][0] = n;
            matching_country_index[match][1] = colon_match;
            if(match>0)
             {
              if(matching_country_index[match-1][0]==n)
               {
                if(matching_country_index[match-1][1]<
                 matching_country_index[match][1])
                 matching_country_index[match-1][1] =
                 matching_country_index[match][1];
                matching_country_index[match][0] = 0;
                matching_country_index[match][1] = 0;
                match--;
               }
             }
           }
         }
        else
         {
          comma_match = 0;
          imc = i-comma-1;
          if(lcl>=imc)
           for(j=0; j<imc; j++)
            {
             if(j<imc-2 || isci==-1 || slash==-1)
              {
               if(call_in[j]==c_syn[j] || (lcl==imc && c_syn[j]=='?'))
                comma_match++;
               else
                break;
              }
             else if(call_in[isci+1]==c_syn[slash+1])
              {
               comma_match +=2;
               break;
              }
            }
          if(comma_match==imc)
           {
            match++;
            matching_country_index[match][0] = n;
            matching_country_index[match][1] = imc;
            if(match>0)
             {
              if(matching_country_index[match-1][0]==n)
               {
                if(matching_country_index[match-1][1]<
                 matching_country_index[match][1])
                 matching_country_index[match-1][1] =
                 matching_country_index[match][1];
                matching_country_index[match][0] = 0;
                matching_country_index[match][1] = 0;
                match--;
               }
             }
           }
         }
        comma = i;
       }
     }
   }
  for(n=1; n<=match; n++)
   {
    if(countries.country_abv[matching_country_index[n][0]][0]=='+')
     iplus_country_index = matching_country_index[n][0];
    else if(matching_country_index[n][1]>matching_country_index[ici][1])
     {
      ici = n;
      num_match = 0;
     }
    else if(matching_country_index[n][1]==matching_country_index[ici][1])
     num_match++;
   }
  if(num_match)
   icountry_index = 0;
  else
   icountry_index = matching_country_index[ici][0];
  return icountry_index;
}

static void get_your_country(void)
{
  gint lci;
  gchar station_line[22], station_px[14];

  if(countries.number_of_countries)
   {
    strcpy(station_line, "        ");
    strcat(station_line, init_screen_choices[0]);
    lci = get_packet_country (station_line, station_px);
    strcpy(score_mb.your_country, countries.country_abv[lci]);
   }
}

static gint get_locus_index(gint iqnum)
{
  gint n;
  gint cindex=-1;

  for(n=0; n<=loci.number_of_loci; n++)
   if(!strcmp(cr_array[iqnum].rec_county, loci.locus_abv[n]))
    {
     cindex = n;
     break;
    }
  return cindex;
}

static gint get_locus_index_cr(void)
{
  gint n;
  gchar locus_hold[8];
  gint cindex=-1;

  switch (isel_contest)
   {
    case FD :
    case SS :
    case WAA :
     strcpy(locus_hold, cr_array[icall_count+1].rec_county);
     break;
    default :
     concat(locus_hold, cr_array[icall_count+1].rec_num,
            strlen(fnn_char[mode]),
            strlen(cr_array[icall_count+1].rec_num)-1, COPY);
   }
  if(strlen(locus_hold))
   for(n=0; n<=loci.number_of_loci; n++)
    if(!strcmp(locus_hold, loci.locus_abv[n]))
     {
      cindex = n;
      break;
     }
  if(cindex==-1)
   for(n=0; n<=loci.number_of_loci; n++)
    if(!strcmp(cr_array[icall_count+1].call, loci.locus_abv[n]))
     {
      cindex = n;
      break;
     }
  if(cindex==-1)
   cindex = get_worked_locus ();
  return cindex;
}

static gint get_worked_locus(void)
{
  gint i, imin, qso_num;
  gint cindex=-1, qso_min=10000;

  search_tree (icall_count+1, icall_count);
  if((call_tree.chigh-call_tree.clow)>0 ||
    (call_tree.cmatch>0 && !ialt_de))
   {
    for(i=call_tree.clow; i<=call_tree.chigh; i++)
     {
      qso_num = atoi(cr_array[call_tree.sort_order[i]].qso);
      if(qso_num<qso_min)
       {
        qso_min = qso_num;
        imin = call_tree.sort_order[i];
       }
     }
    cindex = get_locus_index (imin);
   }
  return cindex;
}

static gint get_country_index(gint iqnum)
{
  gint n;
  gint cindex=-1;

  for(n=0; n<=countries.number_of_countries; n++)
   if(!strcmp(cr_array[iqnum].rec_county, countries.country_abv[n]))
    {
     cindex = n;
     break;
    }
  return cindex;
}

static gint get_packet_country_index(const gchar *pbm_country)
{
  gint n;
  gint cindex=0;

  for(n=0; n<=countries.number_of_countries; n++)
   if(!strcmp(pbm_country, countries.country_abv[n]))
    {
     cindex = n;
     break;
    }
  return cindex;
}

static gint get_zone_index(gint iqnum)
{
  gint zindex;
  gchar zone[3];

  concat(zone, cr_array[iqnum].rec_num, 3-mode, 4-mode, COPY);
  zindex = atoi(zone);
  return zindex;
}

static gint get_zi_from_rn(gint qnum)
{
  gint i, lrn;
  guint answer;
  gchar rn_hold[8], zn_hold[3], msg[51];
  gint zindex=-1, wrong_zone=TRUE;

  lrn = strlen(cr_array[qnum].rec_num);
  if(lrn>3-mode)
   {
    if(lrn==4-mode)
     {
      concat(rn_hold, cr_array[qnum].rec_num, 0, 2-mode, COPY);
      strcat(rn_hold, "0");
      concat(rn_hold, cr_array[qnum].rec_num, 3-mode, 3-mode, !COPY);
      strcpy(cr_array[qnum].rec_num, rn_hold);
      concat(zn_hold, rn_hold, 3-mode, 4-mode, COPY);
     }
    else if(lrn==5-mode)
     concat(zn_hold, cr_array[qnum].rec_num, 3-mode, 4-mode, COPY);
    zindex = atoi(zn_hold);
    if(!pbv.pick_index[0])
     wrong_zone = FALSE;
    else
     for(i=0; i<=pbv.num_match; i++)
      if(zindex==pbv.pick_index[i])
       {
        wrong_zone = FALSE;
        break;
       }
    if(wrong_zone)
     {
      strcpy(msg, "Country is not in zone ");
      strcat(msg, zn_hold);
      strcat(msg, "! Fix it? (Y/N)");
      play_bells (BAD_KEY_SOUND);
      answer = boxed_message (msg, 0, LT_GRAY);
      if(answer=='y' || answer=='Y')
       zindex = -1;
     }
   }
  else if(!pbv.pick_index[0])
   zindex = enter_zone_box ();
  return zindex;
}

static void get_worked_country(gint qnum, gchar *call_px)
{
  gint i, imin, qso_num;
  gchar zone[3];
  gint qso_min=10000;

  countries.icountry_index = -1;
  if((call_tree.chigh-call_tree.clow)>0 ||
    (call_tree.cmatch>0 && !ialt_de))
   {
    for(i=call_tree.clow; i<=call_tree.chigh; i++)
     {
      qso_num = atoi(cr_array[call_tree.sort_order[i]].qso);
      if(qso_num<qso_min)
       {
        qso_min = qso_num;
        imin = call_tree.sort_order[i];
       }
     }
    countries.icountry_index = get_country_index (imin);
//    if(countries.icountry_index<countries.number_of_countries)
//     if(countries.country_abv[countries.icountry_index+1][0]=='+')
//      countries.icountry_index = -1;
    if(countries.icountry_index>-1)
     {
      strcpy(call_px, cr_array[imin].prefix);
      if(isel_contest==WW)
       {
        countries.izone_index = get_zone_index (imin);
        int_to_char (countries.izone_index, zone);
        left_zero_pad (zone, sizeof(zone));
        cr_array[qnum].rec_num[3-mode] = zone[0];
        cr_array[qnum].rec_num[4-mode] = zone[1];
        cyan_bar (main_drawing_area);
       }
     }
   }
}

static gint get_continent_index(gint iqnum)
{
  gint n, gci;
  gint cindex=-1;

  gci = get_country_index (iqnum);
  if(gci>-1)
   for(n=0; n<7; n++)
    if(!strcmp(countries.continent_abv[gci], continent_abvs[n]))
     {
      cindex = n;
      break;
     }
  return cindex;
}

static void pick_country_box(GtkWidget *widget, gint idraw)
{
  GdkRectangle update_rect;
  static gint idt_slsave, kbi_save;
  static gchar kbp_save[6];
  gint i, pick_color, mb, row1, lpx;
  gint col1=60, row2=23, col2=71;
  const gchar *title = "";
  const gchar *subtitle = "";

  row1 = row2-pbv.num_match-2;
  update_rect.x = col1*wcol;
  update_rect.y = row1*hrow;
  update_rect.width = (col2-col1-1)*wcol;
  update_rect.height = (row2-row1-1)*hrow;
  switch (isel_contest)
   {
    case A160 :
    case TEN :
     mb = mode;
     break;
    default :
     mb = iband;
   }
  if(idraw)
   {
    ialt_box = 'C';
    idt_slsave = idisplay_type;
    kbi_save = knowledge_index;
    if(isel_contest==WPX)
     strcpy(kbp_save, knowledge_prefix);
    if(idisplay_type!=IDT_SUMMARY || ireset_display)
     {
      idisplay_type = IDT_SUMMARY;
      ireset_display = FALSE;
      update_display (widget, REDRAW);
     }
    gdk_window_process_updates (main_window->window, TRUE);
    draw_box (row1, col1, row2, col2, 1, BLACK, CYAN,
              title, WHITE, subtitle, LT_GRAY, !SAVEPIX);
    pbv.item = 0;
    pbv.done = FALSE;
    pbv.mouse_cols[0] = col1;
    pbv.mouse_cols[1] = col2;
    main_cursor_type = CURSOR_OFF;
    if(ialt_de=='E')
     g_signal_handler_block (main_window, pec_key_handler_id);
    else
     g_signal_handler_block (main_window, main_key_handler_id);
    pbv.key_handler_id = g_signal_connect (G_OBJECT (main_window),
                         "key_press_event",
                         G_CALLBACK (pcb_key_handler), widget);
   }
  if(pbv.done)
   {
    g_signal_handler_disconnect (main_window, pbv.key_handler_id);
    if(ialt_de=='E')
     g_signal_handler_unblock (main_window, pec_key_handler_id);
    else
     g_signal_handler_unblock (main_window, main_key_handler_id);
    main_cursor_type = LINE_CURSOR;
    gtk_widget_queue_draw_area (widget,
                                update_rect.x-wcol,
                                update_rect.y-hrow,
                                update_rect.width+2*wcol,
                                update_rect.height+2*hrow);
//    knowledge_index = kbi_save;
    if(isel_contest==WPX)
     strcpy(knowledge_prefix, kbp_save);
    idisplay_type = idt_slsave;
    if(idisplay_type==IDT_SUMMARY)
     knowledge_box (widget, REDRAW);
    else
     update_display (widget, REDRAW);
    ialt_box = FALSE;
    gtk_main_quit ();
   }
  else
   {
    draw_rectangle (&update_rect, CYAN, !SAVEPIX);
    for(i=0; i<=pbv.num_match; i++)
     {
      if(i==pbv.num_match)
       pick_color = RED;
      else if(countries.icountry_worked[mb][pbv.pick_index[i]])
       pick_color = BLACK;
      else
       pick_color = WHITE;
      write_line (row1+i, col1+2,
       countries.country_abv[pbv.pick_index[i]], pick_color, !SAVEPIX);
     }
    write_line (row1+pbv.item, col1, "->", YELLOW, !SAVEPIX);
    knowledge_index = pbv.pick_index[pbv.item];
    if(isel_contest==WPX)
     {
      lpx = strlen(cr_array[icall_count+1].call);
      lpx = lpx<5 ? lpx : 5;
      concat(knowledge_prefix, cr_array[icall_count+1].call,
             0, lpx-1, COPY);
     }
    else if(isel_contest==WW)
     knowledge_zindex = countries.izone_index;
    knowledge_box (widget, REDRAW);
    if(idraw) gtk_main ();
   }
}

static gint pcb_key_handler(GtkWidget *widget, GdkEventKey *event,
                            gpointer da)
{
  if(!pbv.done)
   {
    switch (event->keyval)
     {
      case 65362 : /*up_arrow*/
       pbv.item--;
       if(pbv.item<0)
        pbv.item = pbv.num_match;
       pick_country_box (da, !REDRAW);
       break;
      case 65364 : /*down_arrow*/
       pbv.item++;
       if(pbv.item>pbv.num_match)
        pbv.item = 0;
       pick_country_box (da, !REDRAW);
       break;
      case 65293 : /*enter*/
      case 65421 : /*keypad enter*/
       pbv.done = TRUE;
       pick_country_box (da, !REDRAW);
       break;
      default :
       play_bells (BAD_KEY_SOUND);
     }
   }
  else
   play_bells (BAD_KEY_SOUND);
  return TRUE;
}

static void pick_zone_box(GtkWidget *widget, gint idraw)
{
  GdkRectangle update_rect;
  gint i, pick_color, mb, row1;
  gint col1=49, row2=23, col2=55;
  const gchar *title = "";
  const gchar *subtitle = "";

  row1 = row2-pbv.num_match-2;
  update_rect.x = col1*wcol;
  update_rect.y = row1*hrow;
  update_rect.width = (col2-col1-1)*wcol;
  update_rect.height = (row2-row1-1)*hrow;
  mb = iband;
  if(idraw)
   {
    ialt_box = 'Z';
    gdk_window_process_updates (main_window->window, TRUE);
    draw_box (row1, col1, row2, col2, 1, BLACK, CYAN,
              title, WHITE, subtitle, LT_GRAY, !SAVEPIX);
    pbv.item = 0;
    pbv.done = FALSE;
    pbv.mouse_cols[0] = col1;
    pbv.mouse_cols[1] = col2;
    main_cursor_type = CURSOR_OFF;
    if(ialt_de=='E')
     g_signal_handler_block (main_window, pec_key_handler_id);
    else
     g_signal_handler_block (main_window, main_key_handler_id);
    pbv.key_handler_id = g_signal_connect (G_OBJECT (main_window),
                         "key_press_event",
                         G_CALLBACK (pzb_key_handler), widget);
   }
  if(pbv.done)
   {
    g_signal_handler_disconnect (main_window, pbv.key_handler_id);
    if(ialt_de=='E')
     g_signal_handler_unblock (main_window, pec_key_handler_id);
    else
     g_signal_handler_unblock (main_window, main_key_handler_id);
    main_cursor_type = LINE_CURSOR;
    gtk_widget_queue_draw_area (widget,
                                update_rect.x-wcol,
                                update_rect.y-hrow,
                                update_rect.width+2*wcol,
                                update_rect.height+2*hrow);
    ialt_box = FALSE;
    gtk_main_quit ();
   }
  else
   {
    draw_rectangle (&update_rect, CYAN, !SAVEPIX);
    for(i=0; i<=pbv.num_match; i++)
     {
      if(countries.izone_worked[mb][pbv.pick_index[i]])
       pick_color = BLACK;
      else
       pick_color = WHITE;
      write_line (row1+i, col1+2,
       zone_chars[pbv.pick_index[i]], pick_color, !SAVEPIX);
     }
    write_line (row1+pbv.item, col1, "->", YELLOW, !SAVEPIX);
    if(idraw) gtk_main ();
   }
}

static gint pzb_key_handler(GtkWidget *widget, GdkEventKey *event,
                            gpointer da)
{
  if(!pbv.done)
   {
    switch (event->keyval)
     {
      case 65362 : /*up_arrow*/
       pbv.item--;
       if(pbv.item<0)
        pbv.item = pbv.num_match;
       pick_zone_box (da, !REDRAW);
       break;
      case 65364 : /*down_arrow*/
       pbv.item++;
       if(pbv.item>pbv.num_match)
        pbv.item = 0;
       pick_zone_box (da, !REDRAW);
       break;
      case 65293 : /*enter*/
      case 65421 : /*keypad enter*/
       pbv.done = TRUE;
       pick_zone_box (da, !REDRAW);
       break;
      default :
       play_bells (BAD_KEY_SOUND);
     }
   }
  else
   play_bells (BAD_KEY_SOUND);
  return TRUE;
}

static void check_for_conflict(GtkWidget *widget)
{
  gint i, j;
  gchar msg[46];

  for(i=0; i<=loci.number_of_loci; i++)
   for(j=0; j<=countries.number_of_countries; j++)
    if(!strcmp(loci.locus_abv[i], countries.country_abv[j]))
     {
      strcpy(msg, "Conflict with country list entry for: ");
      strcat(msg, countries.country_abv[j]);
      message_box (widget, msg, !REDRAW, CLEAR);
      play_bells (MESSAGE_SOUND);
      return;
     }
}
