static gint packet_search_tree(const gchar *pbm_line, gchar *pbm_rec_county,
                               gchar *pbm_prefix);
static void insert_tree(gint qnum, gint icc);
static void search_px_tree(gint qnum);
static gint packet_search_px_tree(gint ipci, const gchar *call_px);
static void insert_px_tree(gint qnum);
static void update_call_library(gint qnum);
static void remove_old_entries(void);
static guint check_for_dupe(gint qnum, gint icc, gint iask, gpointer da);
static gint check_for_mult(gint qnum, gint rstrt);
static void new_mult_message(gpointer da);
static void new_mult_sound(void);
static void fill_cr_array_dt(gint qnum);
static void fill_cr_array_pts(gint qnum);
static void fill_cr_array_px(gint qnum, const gchar *call_px);
static void fill_empty_fields(void);
static void increment_totals(gint iqnum);
static void reset_arrays(void);

static void search_tree(gint qnum, gint icc)
{
  gint i, cval, mid, high;
  gint low=1;

  call_tree.mbmatch = FALSE;
  high = icc;
  while(low<=high)
   {
    mid = (low+high+1)/2;
    cval = strcmp(cr_array[qnum].call,
                  cr_array[call_tree.sort_order[mid]].call);
    if(cval<0)
     high = mid-1;
    else if(cval>0)
     low = mid+1;
    else
     {
      low = mid;
      for(i=mid-1; i>0; i--)
       {
        cval = strcmp(cr_array[qnum].call,
                      cr_array[call_tree.sort_order[i]].call);
        if(cval)
         break;
        else
         low--;
       }
      call_tree.clow = low;
      high = mid;
      for(i=mid+1; i<=icc; i++)
       {
        cval = strcmp(cr_array[qnum].call,
                      cr_array[call_tree.sort_order[i]].call);
        if(cval)
         break;
        else
         high++;
       }
      call_tree.chigh = high;
      mid = low;
      for(i=low; i<=high; i++)
       {
        cval = cr_array[qnum].mode*6+cr_array[qnum].iband-
               (cr_array[call_tree.sort_order[i]].mode*6+
               cr_array[call_tree.sort_order[i]].iband);
        if(cval<0)
         break;
        else if(cval>0)
         mid++;
        else
         {
          call_tree.mbmatch = TRUE;
          break;
         }
       }
      call_tree.cmatch = mid;
      return; /* found */
     }
   }
  call_tree.cmatch = -(low+high+1)/2;
  call_tree.clow = call_tree.cmatch;
  call_tree.chigh = call_tree.cmatch;
}

static gint packet_search_tree(const gchar *pbm_line, gchar *pbm_rec_county,
                               gchar *pbm_prefix)
{
  gint i, cval, mid, high;
  gchar call_line[14];
  gint mbmatch=FALSE, cmatch=FALSE, idupe=FALSE;
  gint low=1;

  concat(call_line, pbm_line, 8, strlen(pbm_line)-1, COPY);
  strcpy(pbm_rec_county, "");
  strcpy(pbm_prefix, "");
  high = icall_count;
  while(low<=high)
   {
    mid = (low+high+1)/2;
    cval = strcmp(call_line, cr_array[call_tree.sort_order[mid]].call);
    if(cval<0)
     high = mid-1;
    else if(cval>0)
     low = mid+1;
    else
     {
      low = mid;
      for(i=mid-1; i>0; i--)
       {
        cval = strcmp(call_line, cr_array[call_tree.sort_order[i]].call);
        if(cval)
         break;
        else
         low--;
       }
      high = mid;
      for(i=mid+1; i<=icall_count; i++)
       {
        cval = strcmp(call_line, cr_array[call_tree.sort_order[i]].call);
        if(cval)
         break;
        else
         high++;
       }
      mid = low;
      for(i=low; i<=high; i++)
       {
        cval = mode*6+packet_band-
               (cr_array[call_tree.sort_order[i]].mode*6+
               cr_array[call_tree.sort_order[i]].iband);
        if(cval<0)
         break;
        else if(cval>0)
         mid++;
        else
         {
          mbmatch = TRUE;
          break;
         }
       }
      cmatch = TRUE;
      strcpy(pbm_rec_county, cr_array[call_tree.sort_order[low]].rec_county);
      strcpy(pbm_prefix, cr_array[call_tree.sort_order[low]].prefix);
      break; /* found */
     }
   }
  switch (isel_contest)
   {
    case DX :
    case WPX :
    case FD :
    case NYQP :
    case WW :
    case TEN :
    case WAA :
     if(mbmatch)
      idupe = TRUE;
     break;
    case SS :
    case A160 :
     if(cmatch)
      idupe = TRUE;
     break;
   }
  return idupe;
}

static void insert_tree(gint qnum, gint icc)
{
  gint i;

  call_tree.cmatch = abs(call_tree.cmatch);
  call_tree.clow = abs(call_tree.clow);
  call_tree.chigh = abs(call_tree.chigh);
  for(i=icc; i>=call_tree.cmatch; i--)
   call_tree.sort_order[i+1] = call_tree.sort_order[i];
  call_tree.sort_order[call_tree.cmatch] = qnum;
}

static void search_px_tree(gint qnum)
{
  gint cval, mid, high;
  gint low=1;

  high = px_tree.npx;
  while(low<=high)
   {
    mid = (low+high+1)/2;
    cval = strcmp(cr_array[qnum].prefix, px_tree.px_array[mid]);
    if(cval<0)
     high = mid-1;
    else if(cval>0)
     low = mid+1;
    else
     {
      px_tree.pmatch = mid;
      return;
     }
   }
  px_tree.pmatch = -(low+high+1)/2;
}

static gint packet_search_px_tree(gint ipci, const gchar *call_px)
{
  gint cval, mid, high;
  gchar call_hold[14], px_hold[6];
  gint low=1, i=0, ipx=-1, new_px=TRUE;

  while(call_px[i])
   {
    if(isdigit(call_px[i]))
     ipx = i;
    i++;
   }
  if(!ipx)
   {
    ipx = strlen(call_px);
    ipx = ipx<5 ? ipx-1 : 3;
    concat(px_hold, call_px, 0, ipx, COPY);
    strcat(px_hold, "0");
   }
  else if(ipx<0)
   {
    ipx = strlen(call_px)<2 ? 0 : 1;
    concat(px_hold, call_px, 0, ipx, COPY);
    strcat(px_hold, "0");
   }
  else
   {
    if(ipx>4)
     ipx = 4;
    if(ipx==2 &&
     strlen(countries.country_abv[ipci])==4)
     {
      switch (call_px[0])
       {
        case 'A' :
        case 'K' :
        case 'W' :
         strcpy(call_hold, call_px);
         call_hold[0] = 'K';
         if(!kb_index (call_hold, countries.country_abv[ipci]))
          ipx = 3;
         break;
        default :
         if(!kb_index (call_px, countries.country_abv[ipci]))
          ipx = 3;
       }
     }
    concat(px_hold, call_px, 0, ipx, COPY);
   }
  high = px_tree.npx;
  while(low<=high)
   {
    mid = (low+high+1)/2;
    cval = strcmp(px_hold, px_tree.px_array[mid]);
    if(cval<0)
     high = mid-1;
    else if(cval>0)
     low = mid+1;
    else
     {
      new_px = FALSE;
      break;
     }
   }
  return new_px;
}

static void insert_px_tree(gint qnum)
{
  gint i;

  px_tree.pmatch = abs(px_tree.pmatch);
  for(i=px_tree.npx; i>=px_tree.pmatch; i--)
   strcpy(px_tree.px_array[i+1], px_tree.px_array[i]);
  strcpy(px_tree.px_array[px_tree.pmatch], cr_array[qnum].prefix);
  px_tree.npx++;
}

static void update_call_library(gint qnum)
{
  gint i, cval, mid, high, cmatch, igli;
  gchar year[5], zone[3];
  gint low=0, no_match=TRUE, numxes=32;

  high = clib.number_of_calls-1;
  while(low<=high)
   {
    mid = (low+high+1)/2;
    cval = strcmp(cr_array[qnum].call, clib.call[mid]);
    if(cval<0)
     high = mid-1;
    else if(cval>0)
     low = mid+1;
    else
     {
      cmatch = mid;
      no_match = FALSE;
      break;
     }
   }
  if(no_match)
   cmatch = -(low+high+1)/2;
  if(no_match && clib.number_of_calls<9998)
   {
    cmatch = abs(cmatch);
    for(i=clib.number_of_calls-1; i>=cmatch; i--)
     {
      strcpy(clib.call[i+1], clib.call[i]);
      strcpy(clib.year[i+1], clib.year[i]);
      strcpy(clib.country[i+1], clib.country[i]);
      strcpy(clib.cqzone[i+1], clib.cqzone[i]);
      strcpy(clib.iaruzone[i+1], clib.iaruzone[i]);
      strcpy(clib.state[i+1], clib.state[i]);
      strcpy(clib.section[i+1], clib.section[i]);
      strcpy(clib.county[i+1], clib.county[i]);
      strcpy(clib.check[i+1], clib.check[i]);
     }
    strcpy(clib.call[cmatch], cr_array[qnum].call);
    strcpy(clib.country[cmatch], "");
    strcpy(clib.cqzone[cmatch], "");
    strcpy(clib.iaruzone[cmatch], "");
    strcpy(clib.state[cmatch], "");
    strcpy(clib.section[cmatch], "");
    strcpy(clib.county[cmatch], "");
    strcpy(clib.check[cmatch], "");
    clib.number_of_calls++;
   }
  strcpy(year, "20");
  concat(year, cr_array[qnum].date, 4, 5, !COPY);
  strcpy(clib.year[cmatch], year);
  switch (isel_contest)
   {
    case DX :
     break;
    case WPX:
     if(!strcmp(cr_array[qnum].rec_county, "W") ||
        !strcmp(cr_array[qnum].rec_county, "VE"))
      strcpy(clib.country[cmatch], cr_array[qnum].rec_county);
     break;
    case FD:
     if(strcmp(cr_array[qnum].rec_county, "DX"))
      strcpy(clib.section[cmatch], cr_array[qnum].rec_county);
     break;
    case NYQP :
     if(strcmp(cr_array[qnum].rec_county, "DX"))
      {
       igli = get_locus_index (qnum);
       if(igli<=loci.number_of_countyloci)
        {
         strcpy(clib.country[cmatch], "W");
         strcpy(clib.cqzone[cmatch], "05");
         strcpy(clib.state[cmatch], "NY");
         strcpy(clib.county[cmatch], cr_array[qnum].rec_county);
        }
       else if(igli<=loci.number_of_usloci)
        {
         strcpy(clib.country[cmatch], "W");
         strcpy(clib.state[cmatch], cr_array[qnum].rec_county);
        }
       else
        {
         strcpy(clib.country[cmatch], "VE");
         strcpy(clib.state[cmatch], cr_array[qnum].rec_county);
        }
      }
     break;
    case WW:
     if(!strcmp(cr_array[qnum].rec_county, "W") ||
        !strcmp(cr_array[qnum].rec_county, "VE"))
      {
       strcpy(clib.country[cmatch], cr_array[qnum].rec_county);
       concat(zone, cr_array[qnum].rec_num, 3-mode, 4-mode, COPY);
       strcpy(clib.cqzone[cmatch], zone);
      }
     break;
    case SS :
     strcpy(clib.section[cmatch], cr_array[qnum].rec_county);
     strcpy(clib.check[cmatch], cr_array[qnum].prefix);
     break;
    case A160 :
     for(i=1; i<=loci.number_of_loci; i++)
      if(!strcmp(cr_array[qnum].rec_county, loci.locus_abv[i]))
       {
        strcpy(clib.section[cmatch], cr_array[qnum].rec_county);
        if(i>loci.number_of_usloci)
         strcpy(clib.country[cmatch], "VE");
        else if(strcmp(cr_array[qnum].rec_county, "PR") &&
                strcmp(cr_array[qnum].rec_county, "VI") &&
                strcmp(cr_array[qnum].rec_county, "PAC") &&
                strcmp(cr_array[qnum].rec_county, "AK"))
         strcpy(clib.country[cmatch], "W");
        break;
       }
     break;
    case TEN :
     for(i=1; i<=loci.number_of_loci-numxes; i++)
      if(!strcmp(cr_array[qnum].rec_county, loci.locus_abv[i]))
       {
        strcpy(clib.state[cmatch], cr_array[qnum].rec_county);
        if(i>loci.number_of_usloci-numxes)
         strcpy(clib.country[cmatch], "VE");
        else if(strcmp(cr_array[qnum].rec_county, "HI") &&
                strcmp(cr_array[qnum].rec_county, "AK"))
         strcpy(clib.country[cmatch], "W");
        break;
       }
     break;
    case WAA :
     break;
   }
}

static void remove_old_entries(void)
{
  gint i, j, fy;

  fy = atoi(full_year);
  for(i=0; i<clib.number_of_calls; i++)
   if(fy>atoi(clib.year[i])+4)
    {
     for(j=i; j<clib.number_of_calls-1; j++)
      {
       strcpy(clib.call[j], clib.call[j+1]);
       strcpy(clib.year[j], clib.year[j+1]);
       strcpy(clib.country[j], clib.country[j+1]);
       strcpy(clib.cqzone[j], clib.cqzone[j+1]);
       strcpy(clib.iaruzone[j], clib.iaruzone[j+1]);
       strcpy(clib.state[j], clib.state[j+1]);
       strcpy(clib.section[j], clib.section[j+1]);
       strcpy(clib.county[j], clib.county[j+1]);
       strcpy(clib.check[j], clib.check[j+1]);
      }
     i--;
     clib.number_of_calls--;
    }
}

static guint check_for_dupe(gint qnum, gint icc, gint iask, gpointer da)
{
  gint idt_lsave;
  gchar msg[57];
  gint idupe=FALSE;
  guint answer=0;

  search_tree (qnum, icc);
  switch (isel_contest)
   {
    case DX :
    case WPX :
    case FD :
    case NYQP :
    case WW :
    case TEN :
    case WAA :
     if(call_tree.mbmatch)
      idupe = TRUE;
     break;
    case SS :
    case A160 :
     if(call_tree.cmatch>0)
      idupe = TRUE;
     break;
   }
  if(!strcmp(cr_array[qnum].call, "NOQSO"))
   idupe = TRUE;
  if(idupe)
   {
    if(!strcmp(set_screen_choices[1], "<ASK>") && iask)
     {
      idt_lsave = idisplay_type;
      idisplay_type = IDT_INFO6;
      ireset_display = FALSE;
      update_display (da, REDRAW);
      play_bells (DUPE_SOUND);
      strcpy(msg, cr_array[qnum].call);
      strcat(msg, " has already been worked. Work again? (Y/N)");
      answer = boxed_message (msg, 0, YELLOW);
      if(idt_lsave!=IDT_INFO6)
       {
        idisplay_type = idt_lsave;
        update_display (da, REDRAW);
       }
     }
    else
     answer = 'Y';
   }
  return answer;
}

static gint check_for_mult(gint qnum, gint rstrt)
{
  gint i, n, mb, mbl, mbu, new_mult, double_mult;
  gchar call_px[14];

  new_mult = 0;
  switch (isel_contest)
   {
    case FD :
    case WAA :
     mb = 6*cr_array[qnum].mode+cr_array[qnum].iband;
     mbl = mbu = mb;
     break;
    case NYQP :
     mb = 6*cr_array[qnum].mode+cr_array[qnum].iband;
     mbl = 0;
     mbu = 11;
     break;
    case A160 :
    case TEN :
     mb = cr_array[qnum].mode;
     mbl = mbu = mb;
     break;
    case SS :
     mb = cr_array[qnum].iband;
     mbl = 0;
     mbu = 5;
     break;
    default :
     mb = cr_array[qnum].iband;
     mbl = mbu = mb;
   }
  if(((atol(cr_array[qnum].rec_num)>atoi(fnn_char[cr_array[qnum].mode])) &&
     (isel_contest!=FD && isel_contest!=SS && isel_contest!=WAA &&
      isel_contest!=NYQP)) || isel_contest==WW)
   {
    if(rstrt)
     {
      countries.icountry_index = get_country_index (qnum);
      if(isel_contest==WW)
       countries.izone_index = get_zone_index (qnum);
     }
    else
     {
      if(!(isel_contest==WW && cbv.item==FT_RN))
       {
        get_worked_country (qnum, call_px);
        if(countries.icountry_index==-1)
         get_country (qnum, call_px);
       }
      strcpy(cr_array[qnum].rec_county,
             countries.country_abv[countries.icountry_index]);
     }
    if(!countries.icountry_worked[mb][countries.icountry_index])
     {
      countries.icountry_worked[mb][countries.icountry_index] = 1;
      if(countries.icountry_index)
       new_mult += 1;
     }
    knowledge_index = countries.icountry_index;
    if(isel_contest==WW)
     {
      if(!countries.izone_worked[mb][countries.izone_index])
       {
        countries.izone_worked[mb][countries.izone_index] = 1;
        if(countries.izone_index)
         new_mult += 2;
       }
      knowledge_zindex = countries.izone_index;
     }
   }
  else
   {
    new_mult = 1;
    if(isel_contest!=FD && isel_contest!=SS && isel_contest!=WAA)
     concat(cr_array[qnum].rec_county, cr_array[qnum].rec_num,
            strlen(fnn_char[cr_array[qnum].mode]),
            strlen(cr_array[qnum].rec_num)-1, COPY);
    loci.locus_index = get_locus_index (qnum);
    if(loci.locus_index>-1)
     {
      for(i=mbl; i<=mbu; i++)
       {
        if(loci.locus_worked[i][loci.locus_index])
         {
          new_mult = 0;
          break;
         }
       }
     }
    else
     new_mult = 0;
    if(new_mult)
     {     
      loci.locus_worked[mb][loci.locus_index] = 1;
      if(!loci.locus_index)
       new_mult = 0;
      if(isel_contest==NYQP)
       {
        if(loci.locus_index==loci.number_of_loci &&
           !strcmp(init_screen_choices[6], "NY"))
         new_mult = 0;
        if(new_mult && loci.locus_index<=loci.number_of_countyloci)
         {
          for(n=loci.number_of_countyloci+1; n<=loci.number_of_loci; n++)
           if(!strcmp(loci.locus_abv[n], "NY"))
            {
             double_mult = 1;
             for(i=mbl; i<=mbu; i++)
              {
               if(loci.locus_worked[i][n])
                {
                 double_mult = 0;
                 break;
                }
              }
             if(double_mult)
              {
               loci.locus_worked[mb][n] = 1;
               new_mult+=2;
               break;
              }
            }
         }
       }
     }
    knowledge_index = loci.locus_index+1000;
   }
  cr_array[qnum].mult[0] = new_mult%2;
  cr_array[qnum].mult[1] = (new_mult%4)/2;
  cr_array[qnum].mult[2] = (new_mult%8)/4;
  cr_array[qnum].mult[3] = new_mult/8;
  if(isel_contest==WPX)
   {
    new_mult = 0;
    if(!rstrt)
     fill_cr_array_px (qnum, call_px);
    strcpy(knowledge_prefix, cr_array[qnum].prefix);
    search_px_tree (qnum);
    if(px_tree.pmatch<0 && strcmp(cr_array[qnum].call, "NOQSO"))
     {
      insert_px_tree (qnum);
      new_mult = 1;
      cr_array[qnum].mult[1] = new_mult;
     }
   }
  return new_mult;
}

static void new_mult_message(gpointer da)
{
  gint i;
  gchar msg[51], zn[3];
  gint mult_num=0;
  const gchar *mult_messages[6] = {"New Mult! ", "Single Mult! ",
               "Double Mult! ", "Triple Mult! ", "Quadruple Mult! ",
               "New Section! "};

  if(!strcmp(set_screen_choices[4], "<YES>"))
   {
    if(isel_contest==NYQP)
     {
      for(i=0; i<4; i++)
       mult_num += cr_array[icall_count+1].mult[i];
      if(mult_num==1)
       mult_num = 0;
     }
    else if(isel_contest==WW)
     for(i=0; i<4; i++)
      mult_num += cr_array[icall_count+1].mult[i];
    else if(isel_contest==FD)
     mult_num = 5;
    strcpy(msg, mult_messages[mult_num]);
    strcat(msg, cr_array[icall_count+1].call);
    strcat(msg, " (");
    if(isel_contest==WPX)
     strcat(msg, cr_array[icall_count+1].prefix);
    else if(isel_contest==NYQP)
     {
      strcat(msg, cr_array[icall_count+1].rec_county);
      if(cr_array[icall_count+1].mult[1])
       strcat(msg, " - NY");
     }
    else if(isel_contest==WW)
     {
      if(cr_array[icall_count+1].mult[0])
       strcat(msg, cr_array[icall_count+1].rec_county);
      if(cr_array[icall_count+1].mult[1])
       {
        if(cr_array[icall_count+1].mult[0])
         {
          if(strlen(msg)>28)
           strcat(msg, ",");
          else
           strcat(msg, " - ");
         }
        strcpy(zn, zone_chars[countries.izone_index]);
        left_blank_unpad (zn);
        if(strlen(msg)>32)
         strcat(msg, "Zn");
        else if(strlen(msg)>30)
         strcat(msg, "Zn ");
        else
         strcat(msg, "Zone ");
        strcat(msg, zn);
       }
     }
    else
     strcat(msg, cr_array[icall_count+1].rec_county);
    strcat(msg, ")");
    message_box (da, msg, REDRAW, CLEAR);
   }
}

static void new_mult_sound(void)
{
  gint i;
  gint mult_num=0;

  for(i=0; i<4; i++)
   mult_num += cr_array[icall_count+1].mult[i];
  if(isel_contest==WPX)
   mult_num -= cr_array[icall_count+1].mult[0];
  switch (mult_num)
   {
    case 1 :
     play_bells (SINGLE_MULT_SOUND);
     break;
    case 2 :
     play_bells (DOUBLE_MULT_SOUND);
     break;
    case 3 :
     play_bells (DOUBLE_MULT_SOUND);
     play_bells (SINGLE_MULT_SOUND);
     break;
    case 4 :
     play_bells (DOUBLE_MULT_SOUND);
     play_bells (DOUBLE_MULT_SOUND);
     break;
   }
}

static void fill_cr_array_dt(gint qnum)
{
  strftime(cr_array[qnum].date, sizeof(cr_array[qnum].date),
           "%m%d%g", &kb_time);
  strftime(cr_array[qnum].time, sizeof(cr_array[qnum].time),
           "%H%M%S", &kb_time);
}

static void fill_cr_array_pts(gint qnum)
{
  if(!strcmp(cr_array[qnum].dupe, "*"))
   strcpy(cr_array[qnum].points, score_mb.values_char[0]);
  else
   switch (isel_contest)
    {
     case WW :
      if(!strcmp(countries.country_abv[countries.icountry_index],
         score_mb.your_country))
       strcpy(cr_array[qnum].points, score_mb.values_char[0]);
      else if(countries.izone_index<9)
       strcpy(cr_array[qnum].points, score_mb.values_char[1]);
      else
       strcpy(cr_array[qnum].points, score_mb.values_char[2]);
      break;
     case WPX :
      if(!strcmp(countries.country_abv[countries.icountry_index],
         score_mb.your_country))
       strcpy(cr_array[qnum].points, score_mb.values_char[1]);
      else if(!strcmp(countries.continent_abv[countries.icountry_index],
              continent_abvs[4]))
       strcpy(cr_array[qnum].points,
              score_mb.values_char[2+2*((5-cr_array[qnum].iband)/3)]);
      else
       strcpy(cr_array[qnum].points,
              score_mb.values_char[3+2*((5-cr_array[qnum].iband)/3)]);
      break;
     case FD :
     case NYQP :
     case TEN :
      strcpy(cr_array[qnum].points,
             score_mb.values_char[2-cr_array[qnum].mode]);
      break;
     case A160 :
      if(atol(cr_array[qnum].rec_num)>atoi(fnn_char[cr_array[qnum].mode]))
       strcpy(cr_array[qnum].points, score_mb.values_char[2]);
      else
       strcpy(cr_array[qnum].points, score_mb.values_char[1]);
      break;
     default :
      strcpy(cr_array[qnum].points, score_mb.values_char[1]);
    }
}

static void fill_cr_array_px(gint qnum, const gchar *call_px)
{
  gint i, ipx;
  gchar call_hold[14];

  i = 0;
  ipx = -1;
  while(call_px[i])
   {
    if(isdigit(call_px[i]))
     ipx = i;
    i++;
   }
  if(!ipx)
   {
    ipx = strlen(call_px);
    ipx = ipx<5 ? ipx-1 : 3;
    concat(cr_array[qnum].prefix, call_px, 0, ipx, COPY);
    strcat(cr_array[qnum].prefix, "0");
   }
  else if(ipx<0)
   {
    ipx = strlen(call_px)<2 ? 0 : 1;
    concat(cr_array[qnum].prefix, call_px, 0, ipx, COPY);
    strcat(cr_array[qnum].prefix, "0");
   }
  else
   {
    if(ipx>4)
     ipx = 4;
    if(ipx==2 &&
     strlen(countries.country_abv[countries.icountry_index])==4)
     {
      switch (call_px[0])
       {
        case 'A' :
        case 'K' :
        case 'W' :
         strcpy(call_hold, call_px);
         call_hold[0] = 'K';
         if(!kb_index (call_hold,
          countries.country_abv[countries.icountry_index]))
          ipx = 3;
         break;
        default :
         if(!kb_index (call_px,
          countries.country_abv[countries.icountry_index]))
          ipx = 3;
       }
     }
    concat(cr_array[qnum].prefix, call_px, 0, ipx, COPY);
   }
}

static void fill_empty_fields(void)
{
  gint i, imin, qso_num, lrn;
  gint qso_min=10000;

  if(call_tree.cmatch>0)
   {
    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];
       }
     }
    switch (isel_contest)
     {
      case DX :
       if(strlen(cr_array[icall_count+1].rec_num)==3-mode)
        strcpy(cr_array[icall_count+1].rec_num, cr_array[imin].rec_num);
       break;
      case FD :
       if(!strlen(cr_array[icall_count+1].rec_num))
        strcpy(cr_array[icall_count+1].rec_num, cr_array[imin].rec_num);
       if(!strlen(cr_array[icall_count+1].rec_county))
        strcpy(cr_array[icall_count+1].rec_county,
               cr_array[imin].rec_county);
       break;
      case NYQP :
       lrn = strlen(cr_array[imin].rec_num);
       if(strlen(cr_array[icall_count+1].rec_num)==3-mode)
        if(isalpha(cr_array[imin].rec_num[lrn-1]))
         concat(cr_array[icall_count+1].rec_num, cr_array[imin].rec_num,
                3-cr_array[imin].mode, lrn-1, !COPY);
       break;
      case WW :
       if(strlen(cr_array[icall_count+1].rec_num)==3-mode)
        strcpy(cr_array[icall_count+1].rec_num, cr_array[imin].rec_num);
       break;
      case SS :
       if(!strlen(cr_array[icall_count+1].tx))
        strcpy(cr_array[icall_count+1].tx, cr_array[imin].tx);
       if(!strlen(cr_array[icall_count+1].prefix))
        strcpy(cr_array[icall_count+1].prefix, cr_array[imin].prefix);
       if(!strlen(cr_array[icall_count+1].rec_county))
        strcpy(cr_array[icall_count+1].rec_county,
               cr_array[imin].rec_county);
       break;
      case A160 :
       lrn = strlen(cr_array[imin].rec_num);
       if(strlen(cr_array[icall_count+1].rec_num)==3-mode)
        if(isalpha(cr_array[imin].rec_num[lrn-1]))
         concat(cr_array[icall_count+1].rec_num, cr_array[imin].rec_num,
                3-mode, lrn-1, !COPY);
       break;
      case TEN :
       lrn = strlen(cr_array[imin].rec_num);
       if(strlen(cr_array[icall_count+1].rec_num)==3-mode)
        if(isalpha(cr_array[imin].rec_num[lrn-1]))
         concat(cr_array[icall_count+1].rec_num, cr_array[imin].rec_num,
                lrn-2, lrn-1, !COPY);
       break;
      case WAA :
       if(!strlen(cr_array[icall_count+1].rec_num))
        strcpy(cr_array[icall_count+1].rec_num, cr_array[imin].rec_num);
       if(!strlen(cr_array[icall_count+1].rec_county))
        strcpy(cr_array[icall_count+1].rec_county,
               cr_array[imin].rec_county);
       break;
     }
   }
}

static void increment_totals(gint iqnum)
{
  gint i, m, ib, iz;

  m = cr_array[iqnum].mode;
  ib = cr_array[iqnum].iband;
  score_mb.qsos[m][ib] += 1;
  if(!strcmp(cr_array[iqnum].dupe, "*"))
   score_mb.dupes[m][ib] += 1;
  score_mb.points[m][ib] += atoi(cr_array[iqnum].points);
  if(isel_contest==WPX)
   iz = 1;
  else
   iz = 0;
  if(isel_contest==WW)
   {
    score_mb.mults[m][ib] += cr_array[iqnum].mult[0];
    score_mb.zones[m][ib] += cr_array[iqnum].mult[1];
   }
  else
   for(i=iz; i<4; i++)
    score_mb.mults[m][ib] += cr_array[iqnum].mult[i];
}

static void reset_arrays(void)
{
  gint i, j, m, ib;

  for(m=0; m<2; m++)
   for(ib=0; ib<6; ib++)
    {
     score_mb.qsos[m][ib] = 0;
     score_mb.dupes[m][ib] = 0;
     score_mb.mults[m][ib] = 0;
     score_mb.zones[m][ib] = 0;
     score_mb.points[m][ib] = 0;
    }
  for(i=0; i<12; i++)
   for(j=0; j<MAX_LOCI; j++)
    loci.locus_worked[i][j] = 0;
  for(i=0; i<6; i++)
   {
    for(j=0; j<MAX_COUNTRIES; j++)
     countries.icountry_worked[i][j] = 0;
    for(j=0; j<MAX_ZONES; j++)
     countries.izone_worked[i][j] = 0;
   }
  for(i=0; i<icall_count+2; i++)
   {
    cr_array[i].dupe[0] = '\0';
    cr_array[i].points[0] = '\0';
    for(j=0; j<4; j++)
     cr_array[i].mult[j] = 0;
   }
  px_tree.npx = 0;
}
