static void cyan_bar(GtkWidget *widget);
static void cyan_bar_que(GtkWidget *widget);
static void clear_cyan_bar(GtkWidget *widget, ESVars *esd);
static void scroll_log(GtkWidget *widget);
static void next_field(guint ikey, gint qnum, ESVars *esd, gpointer da);
static gint valid_field(gint qnum);
static gint valid_call(gint qnum);
static gint valid_locus(const gchar *locus_in);
static gint valid_precedence(guint prec);
static gint valid_report(gint qnum);
static gint valid_key(guint ikey, guint istate);
static gint check_field_line(gint qnum);
static void increment_que(void);
static void set_esd_flp(gint qnum, ESVars *esd);
static void edit_line(ESVars *esd);

static void cyan_bar(GtkWidget *widget)
{
  GdkRectangle update_rect;
  gint i;
  gint cyan_bar_color, text_color, band_color, mode_color, qso_color;
  gint dupe_color, sent_color, mult_color[4], on_off_color, rc_color;
  gint px_color;
  gchar date[3], time[5], zone[3];
  gint row1=24, col1=1, row2=24, col2=80;
  
  update_rect.x = (col1-1)*wcol;
  update_rect.y = (row1-1)*hrow;
  update_rect.width = (col2-col1+1)*wcol;
  update_rect.height = (row2-row1+1)*hrow;
  if(ialt_de)
   {
    cyan_bar_color = RED;
    qso_color = WHITE;
    sent_color = WHITE;
    dupe_color = WHITE;
    text_color = WHITE;
    on_off_color = WHITE;
    band_color = WHITE;
    mode_color = WHITE;
    for(i=0; i<4; i++)
     mult_color[i] = WHITE;
    rc_color = WHITE;
    px_color = WHITE;
   }
  else
   {
    cyan_bar_color = CYAN;
    qso_color = BLACK;
    sent_color = BLACK;
    dupe_color = RED;
    if(iscroll_log==icall_count)
     text_color = WHITE;
    else
     text_color = BLACK;
    if(!strcmp(cr_array[icall_count+1].on_off, on_off_char[2]))
     on_off_color = RED;
    else if(!strcmp(cr_array[icall_count+1].on_off, on_off_char[1]))
     on_off_color = HI_GREEN;
    else
     on_off_color = text_color;
    if(!iscroll_log ||
     (cr_array[iscroll_log+1].iband != cr_array[iscroll_log].iband))
     band_color = RED;
    else
     band_color = BLACK;
    if(!iscroll_log ||
     (cr_array[iscroll_log+1].mode != cr_array[iscroll_log].mode))
     mode_color = RED;
    else
     mode_color = BLACK;
    for(i=0; i<4; i++)
     if(cr_array[iscroll_log+1].mult[i]==1)
      mult_color[i] = RED;
     else
      mult_color[i] = BLACK;
    switch (isel_contest)
     {
      case WPX :
       rc_color = BLACK;
       px_color = mult_color[1];
       break;
      case FD :
       rc_color = iscroll_log==icall_count ? WHITE : mult_color[0];
       px_color = BLACK;
       break;
      case SS :
       rc_color = iscroll_log==icall_count ? WHITE : mult_color[0];
       px_color = WHITE;
       break;
      case WAA :
       rc_color = iscroll_log==icall_count ? WHITE : mult_color[0];
       px_color = BLACK;
       break;
      default :
       rc_color = mult_color[0];
       px_color = BLACK;
     }
   }
  draw_rectangle (&update_rect, cyan_bar_color, SAVEPIX);
  if(cbv.col1[FT_IBAND])
   write_line (cbv.row1-1, cbv.col1[FT_IBAND]-1,
    bandf[cr_array[iscroll_log+1].iband], band_color, SAVEPIX);
  if(cbv.col1[FT_MODE])
   write_line (cbv.row1-1, cbv.col1[FT_MODE]-1,
    mode_char[cr_array[iscroll_log+1].mode], mode_color, SAVEPIX);
  if(cbv.col1[FT_RC])
   write_line (cbv.row1-1, cbv.col1[FT_RC]-1,
    cr_array[iscroll_log+1].rec_county, rc_color, SAVEPIX);
  if(cbv.col1[FT_CALL])
   write_line (cbv.row1-1, cbv.col1[FT_CALL]-1,
    cr_array[iscroll_log+1].call, text_color, SAVEPIX);
  if(cbv.col1[FT_RN])
   {
    write_line (cbv.row1-1, cbv.col1[FT_RN]-1,
     cr_array[iscroll_log+1].rec_num, text_color, SAVEPIX);
    if(isel_contest==WW && mult_color[1]==RED)
     {
      concat(zone, cr_array[iscroll_log+1].rec_num,
             3-cr_array[iscroll_log+1].mode,
             4-cr_array[iscroll_log+1].mode, COPY);
      write_line (cbv.row1-1,
                  cbv.col1[FT_RN]+2-cr_array[iscroll_log+1].mode,
                  zone, mult_color[1], SAVEPIX);
     }
   }
  if(cbv.col1[FT_DATE])
   {
    concat(date, cr_array[iscroll_log+1].date, 2, 3, COPY);
    write_line (cbv.row1-1, cbv.col1[FT_DATE]-1,
     date, text_color, SAVEPIX);
   }
  if(cbv.col1[FT_TIME])
   {
    concat(time, cr_array[iscroll_log+1].time, 0, 3, COPY);
    write_line (cbv.row1-1, cbv.col1[FT_TIME]-1,
     time, text_color, SAVEPIX);
   }
  if(cbv.col1[FT_SENT])
   {
    switch (isel_contest)
     {
      case FD :
      case SS :
      case WAA :
       write_line (cbv.row1-1, cbv.col1[FT_SENT]-1,
        cr_array[iscroll_log+1].sent, sent_color, SAVEPIX);
       break;
      default :
       write_line (cbv.row1-1,
        cbv.col1[FT_SENT]-1+cr_array[iscroll_log+1].mode,
        cr_array[iscroll_log+1].sent, sent_color, SAVEPIX);
     }
   }
  if(cbv.col1[FT_PX])
   write_line (cbv.row1-1, cbv.col1[FT_PX]-1,
    cr_array[iscroll_log+1].prefix, px_color, SAVEPIX);
  if(cbv.col1[FT_QSO])
   write_line (cbv.row1-1, cbv.col1[FT_QSO]-1,
    cr_array[iscroll_log+1].qso, qso_color, SAVEPIX);
  if(cbv.col1[FT_ONOFF])
   write_line (cbv.row1-1, cbv.col1[FT_ONOFF]-1,
    cr_array[iscroll_log+1].on_off, on_off_color, SAVEPIX);
  if(cbv.col1[FT_DUPE])
   write_line (cbv.row1-1, cbv.col1[FT_DUPE]-1,
    cr_array[iscroll_log+1].dupe, dupe_color, SAVEPIX);
  if(cbv.col1[FT_PTS])
   write_line (cbv.row1-1, cbv.col1[FT_PTS]-1,
    cr_array[iscroll_log+1].points, text_color, SAVEPIX);
  if(cbv.col1[FT_TX])
   write_line (cbv.row1-1, cbv.col1[FT_TX]-1,
    cr_array[iscroll_log+1].tx, text_color, SAVEPIX);
  gtk_widget_queue_draw_area (widget,
                              update_rect.x,
                              update_rect.y,
                              update_rect.width,
                              update_rect.height);
}

static void cyan_bar_que(GtkWidget *widget)
{
  GdkRectangle update_rect;
  gint i;
  gint col1, col2;
  gint row1=25, row2=25;
  
  col1 = cbv.col1[FT_NEXT1];
  col2 = col1+40;
  update_rect.x = (col1-1)*wcol;
  update_rect.y = (row1-1)*hrow;
  update_rect.width = (col2-col1+1)*wcol;
  update_rect.height = (row2-row1+1)*hrow;
  draw_rectangle (&update_rect, CYAN, SAVEPIX);
  for(i=0; i<3; i++)
   write_line (row1-1, cbv.col1[FT_NEXT1+i]-1,
               qued.call[i], WHITE, SAVEPIX);
  gtk_widget_queue_draw_area (widget,
                              update_rect.x,
                              update_rect.y,
                              update_rect.width,
                              update_rect.height);
}

static void clear_cyan_bar(GtkWidget *widget, ESVars *esd)
{
  strcpy(cr_array[icall_count+1].call, qued.call[0]);
  strcpy(cr_array[icall_count+1].dupe, "");
  if(isel_contest==FD || isel_contest==WAA)
   {
    strcpy(cr_array[icall_count+1].rec_num, "");
    strcpy(cr_array[icall_count+1].rec_county, "");
   }
  else if(isel_contest==SS)
   {
    strcpy(cr_array[icall_count+1].rec_num, "");
    strcpy(cr_array[icall_count+1].rec_county, "");
    strcpy(cr_array[icall_count+1].prefix, "");
    strcpy(cr_array[icall_count+1].tx, "");
   }
  else
   strcpy(cr_array[icall_count+1].rec_num, fnn_char[mode]);
  cbv.item = FT_CALL;
  cbv.cursor_col = cbv.col1[cbv.item]+
                   strlen(cr_array[icall_count+1].call);
  cyan_bar (widget);
  if(insert_mode)
   {
    esd->cursor_colp = &cbv.cursor_col;
    esd->field_col = cbv.col1[cbv.item];
    esd->field_length = cbv.field_length[cbv.item];
    esd->field_linep = cr_array[icall_count+1].call;
    esd->ikey = 65507;
    edit_line (esd);
   }
}

static void scroll_log(GtkWidget *widget)
{
  GdkRectangle update_rect;
  gint i, j, row1, nrows;
  gint text_color, band_color, mode_color, dupe_color, mult_color[4];
  gint rc_color, px_color;
  gchar date[3], time[5], zone[3];
  gint col1=1, row2=23, col2=80;
  
  if(iscroll_log>log_lines[idisplay_type])
   nrows = log_lines[idisplay_type];
  else
   nrows = iscroll_log;
  row1 = row2-nrows+1;
  if(nrows<log_lines[idisplay_type])
   black_bar (widget, row2-log_lines[idisplay_type]+1, col1,
              row2-nrows, col2);
  update_rect.x = (col1-1)*wcol;
  update_rect.y = (row1-1)*hrow;
  update_rect.width = (col2-col1+1)*wcol;
  update_rect.height = (row2-row1+1)*hrow;
  draw_rectangle (&update_rect, LT_GRAY, SAVEPIX);
  text_color = BLUE;
  dupe_color = RED;
  for(i=0; i<nrows; i++)
   {
    if(iscroll_log-i==1 ||
     (cr_array[iscroll_log-i].iband != cr_array[iscroll_log-i-1].iband))
     band_color = RED;
    else
     band_color = BLUE;
    if(iscroll_log-i==1 ||
     (cr_array[iscroll_log-i].mode != cr_array[iscroll_log-i-1].mode))
     mode_color = RED;
    else
     mode_color = BLUE;
  for(j=0; j<4; j++)
   if(cr_array[iscroll_log-i].mult[j]==1)
    mult_color[j] = RED;
   else
    mult_color[j] = BLUE;
  if(isel_contest==WPX)
   {
    rc_color = BLUE;
    px_color = mult_color[1];
   }
  else
   {
    rc_color = mult_color[0];
    px_color = BLUE;
   }
    if(cbv.col1[FT_IBAND])
     write_line (row2-i-1, cbv.col1[FT_IBAND]-1,
      bandf[cr_array[iscroll_log-i].iband], band_color, SAVEPIX);
    if(cbv.col1[FT_MODE])
     write_line (row2-i-1, cbv.col1[FT_MODE]-1,
      mode_char[cr_array[iscroll_log-i].mode], mode_color, SAVEPIX);
    if(cbv.col1[FT_RC])
     write_line (row2-i-1, cbv.col1[FT_RC]-1,
      cr_array[iscroll_log-i].rec_county, rc_color, SAVEPIX);
    if(cbv.col1[FT_CALL])
     write_line (row2-i-1, cbv.col1[FT_CALL]-1,
      cr_array[iscroll_log-i].call, text_color, SAVEPIX);
    if(cbv.col1[FT_RN])
     {
      write_line (row2-i-1, cbv.col1[FT_RN]-1,
       cr_array[iscroll_log-i].rec_num, text_color, SAVEPIX);
      if(isel_contest==WW && mult_color[1]==RED)
       {
        concat(zone, cr_array[iscroll_log-i].rec_num,
               3-cr_array[iscroll_log-i].mode,
               4-cr_array[iscroll_log-i].mode, COPY);
        write_line (row2-i-1,
                    cbv.col1[FT_RN]+2-cr_array[iscroll_log-i].mode,
                    zone, mult_color[1], SAVEPIX);
       }
     }
    if(cbv.col1[FT_DATE])
     {
      concat(date, cr_array[iscroll_log-i].date, 2, 3, COPY);
      write_line (row2-i-1, cbv.col1[FT_DATE]-1,
       date, text_color, SAVEPIX);
     }
    if(cbv.col1[FT_TIME])
     {
      concat(time, cr_array[iscroll_log-i].time, 0, 3, COPY);
      write_line (row2-i-1, cbv.col1[FT_TIME]-1,
       time, text_color, SAVEPIX);
     }
    if(cbv.col1[FT_SENT])
     {
      switch (isel_contest)
       {
        case FD :
        case SS :
        case WAA :
         write_line (row2-i-1, cbv.col1[FT_SENT]-1,
          cr_array[iscroll_log-i].sent, text_color, SAVEPIX);
         break;
        default :
         write_line (row2-i-1,
          cbv.col1[FT_SENT]-1+cr_array[iscroll_log-i].mode,
          cr_array[iscroll_log-i].sent, text_color, SAVEPIX);
       }
     }
    if(cbv.col1[FT_PX])
     write_line (row2-i-1, cbv.col1[FT_PX]-1,
      cr_array[iscroll_log-i].prefix, px_color, SAVEPIX);
    if(cbv.col1[FT_QSO])
     write_line (row2-i-1, cbv.col1[FT_QSO]-1,
      cr_array[iscroll_log-i].qso, text_color, SAVEPIX);
    if(cbv.col1[FT_ONOFF])
     write_line (row2-i-1, cbv.col1[FT_ONOFF]-1,
      cr_array[iscroll_log-i].on_off, text_color, SAVEPIX);
    if(cbv.col1[FT_DUPE])
     write_line (row2-i-1, cbv.col1[FT_DUPE]-1,
      cr_array[iscroll_log-i].dupe, dupe_color, SAVEPIX);
    if(cbv.col1[FT_PTS])
     write_line (row2-i-1, cbv.col1[FT_PTS]-1,
      cr_array[iscroll_log-i].points, text_color, SAVEPIX);
    if(cbv.col1[FT_TX])
     write_line (row2-i-1, cbv.col1[FT_TX]-1,
      cr_array[iscroll_log-i].tx, text_color, SAVEPIX);
   }
  gtk_widget_queue_draw_area (widget,
                              update_rect.x,
                              update_rect.y,
                              update_rect.width,
                              update_rect.height);
}

static void next_field(guint ikey, gint qnum, ESVars *esd, gpointer da)
{
  gint i, m3, isntmt, nonmt;
  gint from_call_field=FALSE;

  if(cbv.item==FT_CALL)
   from_call_field = TRUE;
  for(i=0; i<NUMBER_OF_FIELDS; i++)
   if(cbv.item_order[i]==cbv.item)
    break;
  switch (ikey)
   {
    case ',' :
     switch (cbv.item)
      {
       case FT_NEXT1 :
       case FT_NEXT2 :
        do
         {
          i++;
          if(i>NUMBER_OF_FIELDS-1)
           i = 0;
          cbv.item = cbv.item_order[i];
         } while(!valid_field (qnum));
        cbv.cursor_col = cbv.col1[cbv.item];
        break;
       case FT_NEXT3 :
        cbv.cursor_col = cbv.col1[cbv.item];
        cbv.cursor_row = 25;
        play_bells (BELL_SOUND);
        break;
       default :
        cbv.item = FT_NEXT1;
        cbv.cursor_col = cbv.col1[cbv.item];
        cbv.cursor_row = 25;
      }
     break;
    case 65289 : /*tab*/
     if(qued.inq && !ialt_de)
      m3 = 0;
     else
      m3 = 3;
     do
      {
       i++;
       if(i>NUMBER_OF_FIELDS-1-m3)
        i = 0;
       cbv.item = cbv.item_order[i];
      } while(!valid_field (qnum));
     cbv.cursor_col = cbv.col1[cbv.item];
     break;
    case 65056 : /*shift_tab*/
     if(qued.inq && !ialt_de)
      m3 = 0;
     else
      m3 = 3;
     do
      {
       i--;
       if(i<0)
        i = NUMBER_OF_FIELDS-1-m3;
       cbv.item = cbv.item_order[i];
      } while(!valid_field (qnum));
     cbv.cursor_col = cbv.col1[cbv.item];
     break;
    case 65293 : /*enter*/
     nonmt = 0;
     do
      {
       do
        {
         i++;
         if(i>NUMBER_OF_FIELDS-4)
          i = 0;
         cbv.item = cbv.item_order[i];
        } while(!valid_field (qnum));
       if(isel_contest==SS && iscroll_log==icall_count)
        switch (cbv.item)
         {
          case FT_CALL :
           isntmt = strlen(cr_array[qnum].call);
           nonmt++;
           break;
          case FT_RN :
           isntmt = strlen(cr_array[qnum].rec_num);
           nonmt++;
           break;
          case FT_TX :
           isntmt = strlen(cr_array[qnum].tx);
           nonmt++;
           break;
          case FT_PX :
           isntmt = strlen(cr_array[qnum].prefix);
           nonmt++;
           break;
          case FT_RC :
           isntmt = strlen(cr_array[qnum].rec_county);
           nonmt++;
           break;
          default :
           isntmt = FALSE;
         }
       else
        isntmt = FALSE;
      } while(isntmt && nonmt<5);
     cbv.cursor_col = cbv.col1[cbv.item];
     break;
   }
  switch (cbv.item)
   {
    case FT_IBAND :
    case FT_MODE :
    case FT_DATE :
    case FT_TIME :
    case FT_QSO :
    case FT_ONOFF :
    case FT_DUPE :
    case FT_PTS :
     main_cursor_type = CURSOR_OFF;
     break;
    case FT_RC :
     main_cursor_type = LINE_CURSOR;
     cbv.cursor_col += strlen(cr_array[qnum].rec_county);
     break;
    case FT_CALL :
     main_cursor_type = LINE_CURSOR;
     cbv.cursor_col += strlen(cr_array[qnum].call);
     break;
    case FT_RN :
     main_cursor_type = LINE_CURSOR;
     cbv.cursor_col += strlen(cr_array[qnum].rec_num);
     break;
    case FT_SENT :
     main_cursor_type = LINE_CURSOR;
     cbv.cursor_col += strlen(cr_array[qnum].sent);
     if(isel_contest!=FD && isel_contest!=SS && isel_contest!=WAA)
      cbv.cursor_col += cr_array[qnum].mode;
     break;
    case FT_PX :
     main_cursor_type = LINE_CURSOR;
     cbv.cursor_col += strlen(cr_array[qnum].prefix);
     break;
    case FT_TX :
     main_cursor_type = LINE_CURSOR;
     cbv.cursor_col += strlen(cr_array[qnum].tx);
     break;
    case FT_NEXT1 :
     main_cursor_type = LINE_CURSOR;
     cbv.cursor_col += strlen(qued.call[0]);
     break;
    case FT_NEXT2 :
     main_cursor_type = LINE_CURSOR;
     cbv.cursor_col += strlen(qued.call[1]);
     break;
    case FT_NEXT3 :
     main_cursor_type = LINE_CURSOR;
     cbv.cursor_col += strlen(qued.call[2]);
     break;
   }
  if(from_call_field)
   super_partial_box (da);
  cyan_bar (da);
  if(qued.inq)
   cyan_bar_que (da);
  if(insert_mode || ialt_de=='E')
   {
    esd->cursor_row = cbv.cursor_row;
    esd->cursor_colp = &cbv.cursor_col;
    esd->field_col = cbv.col1[cbv.item];
    if(cbv.item==FT_SENT)
     if(isel_contest!=FD && isel_contest!=SS && isel_contest!=WAA)
      esd->field_col += cr_array[qnum].mode;
    esd->field_length = cbv.field_length[cbv.item];
    set_esd_flp (qnum, esd);
    edit_line (esd);
   }
}

static gint valid_field(gint qnum)
{
  gint i;
  gint vf=TRUE;

  if(!cbv.col1[cbv.item])
   vf = FALSE;
  else if(cbv.item==FT_ONOFF || cbv.item==FT_DATE ||
          cbv.item==FT_DUPE || cbv.item==FT_PTS || cbv.item==FT_QSO)
   vf = FALSE;
  else if(qnum==icall_count+1)
   {
    vf = FALSE;
    for(i=0; i<8; i++)
     if(cbv.item==cbv.vf_w[i])
      {
       vf = TRUE;
       if(cbv.item<FT_NEXT1)
        cbv.cursor_row = 24;
       else
        cbv.cursor_row = 25;
       break;
      }
   }
  else
   switch (isel_contest)
    {
     case WPX :
      if(cbv.item==FT_PX)
       vf = FALSE;
      break;
     case NYQP :
      if(cbv.item==FT_RC)
       vf = FALSE;
      break;
    }
  return vf;
}

static gint valid_call(gint qnum)
{
  gint i, lcall;
  gint vc=FALSE;

  lcall = strlen(cr_array[qnum].call);
  if(lcall>2)
   if(kb_index (cr_array[qnum].call, "?")==-1 &&
      kb_index (cr_array[qnum].call, "*")==-1 &&
      kb_index (cr_array[qnum].call, "~")==-1)
    for(i=0; i<lcall; i++)
     if(isalpha(cr_array[qnum].call[i]))
      {
       vc = TRUE;
       break;
      }
  return vc;
}

static gint valid_locus(const gchar *locus_in)
{
  gint i;
  gint vl=FALSE;

  for(i=0; i<=loci.number_of_loci; i++)
   if(!strcmp(locus_in, loci.locus_abv[i]))
    {
     vl = TRUE;
     break;
    }
  return vl;
}

static gint valid_precedence(guint prec)
{
  gint vp=FALSE;

  if(prec=='A' || prec=='B' || prec=='Q' ||
     prec=='M' || prec=='S' || prec=='U')
   vp = TRUE;
  return vp;
}

static gint valid_report(gint qnum)
{
  gint i, digit[3];
  gchar digit_char[2], rpt_hold[8];
  gint vr=TRUE;

  if(cbv.item==FT_RN)
   strcpy(rpt_hold, cr_array[qnum].rec_num);
  else
   strcpy(rpt_hold, cr_array[qnum].sent);
  for(i=0; i<3-cr_array[qnum].mode; i++)
   {
    concat(digit_char, rpt_hold, i, i, COPY);
    if(isdigit(digit_char[0]))
     digit[i] = atoi(digit_char);
    else
     {
      vr = FALSE;
      return vr;
     }
   }
  if(digit[0]<1 || digit[0]>5)
   {
    vr = FALSE;
    return vr;
   }
  for(i=1; i<3-cr_array[qnum].mode; i++)
   if(!digit[i])
    {
     vr = FALSE;
     break;
    }
  return vr;
}

static gint valid_key(guint ikey, guint istate)
{
  gint ivk=FALSE;

  if(istate==20 || istate==24)
   {
    if(!ialt_de && isupper_ui(ikey))
     ivk = TRUE;
    return ivk;
   }
  switch (cbv.item)
  {
   case FT_RC :
    switch (isel_contest)
     {
      case FD :
      case SS :
       if(isupper_ui(ikey))
        ivk = TRUE;
       break;
      default :
       if(isupper_ui(ikey) || isdigit_ui(ikey) || ikey=='/' || ikey=='-')
        ivk = TRUE;
     }
    break;
   case FT_CALL :
    if(isupper_ui(ikey) || isdigit_ui(ikey) || ikey=='/' ||
       ikey=='?' || ikey=='*' || ikey=='~' ||
       (ikey==' ' &&  kb_index (cr_array[iscroll_log+1].call, " ")==-1))
     ivk = TRUE;
    break;
   case FT_RN :
    switch (isel_contest)
     {
      case DX :
       if(isdigit_ui(ikey) || ikey=='C' || ikey=='K' || ikey=='R')
        ivk = TRUE;
       break;
      case WPX :
       if(isdigit_ui(ikey))
        ivk = TRUE;
       break;
      case FD :
       if(isdigit_ui(ikey) || (ikey>='A' && ikey<='F'))
        ivk = TRUE;
       break;
      case NYQP :
       if(isupper_ui(ikey) || isdigit_ui(ikey))
        ivk = TRUE;
       break;
      case WW :
       if(isdigit_ui(ikey))
        ivk = TRUE;
       break;
      case SS :
       if(isdigit_ui(ikey) || valid_precedence (ikey))
        ivk = TRUE;
       break;
      case A160 :
       if(isupper_ui(ikey) || isdigit_ui(ikey))
        ivk = TRUE;
       break;
      case TEN :
       if(isupper_ui(ikey) || isdigit_ui(ikey))
        ivk = TRUE;
       break;
      case WAA :
       if(isupper_ui(ikey) || isdigit_ui(ikey))
        ivk = TRUE;
       break;
     }
    break;
   case FT_SENT :
    switch (isel_contest)
     {
      case DX :
       if(isupper_ui(ikey) || isdigit_ui(ikey))
        ivk = TRUE;
       break;
      case WPX :
       if(isdigit_ui(ikey))
        ivk = TRUE;
       break;
      case FD :
       if(isupper_ui(ikey) || isdigit_ui(ikey))
        ivk = TRUE;
       break;
      case NYQP :
       if(isupper_ui(ikey) || isdigit_ui(ikey))
        ivk = TRUE;
       break;
      case WW :
       if(isdigit_ui(ikey))
        ivk = TRUE;
       break;
      case SS :
       if(isupper_ui(ikey) || isdigit_ui(ikey))
        ivk = TRUE;
       break;
      case A160 :
       if(isupper_ui(ikey) || isdigit_ui(ikey))
        ivk = TRUE;
       break;
      case TEN :
       if(isupper_ui(ikey) || isdigit_ui(ikey))
        ivk = TRUE;
       break;
      case WAA :
       if(isupper_ui(ikey) || isdigit_ui(ikey))
        ivk = TRUE;
       break;
     }
    break;
   case FT_TX :
    switch (isel_contest)
     {
      case SS :
       ivk = valid_precedence (ikey);
       break;
     }
    break;
   case FT_PX :
    switch (isel_contest)
     {
      case WPX :
       if(isupper_ui(ikey) || isdigit_ui(ikey))
        ivk = TRUE;
       break;
      case SS :
       if(isupper_ui(ikey) || isdigit_ui(ikey))
        ivk = TRUE;
       break;
     }
    break;
   default :
    if(isupper_ui(ikey) || isdigit_ui(ikey) || ikey=='/' ||
      (ikey==' ' && cbv.item==FT_CALL && 
      kb_index (cr_array[iscroll_log+1].call, " ")==-1) ||
      (ikey=='-' && cbv.item==FT_RC))
     ivk = TRUE;
  }
  return ivk;
}

static gint check_field_line(gint qnum)
{
  gint i, lfield, ickr[3], lpart2, iwatts;
  gchar rn_hold[8], sent_hold[7], part2[5], part3[4];
  gint cfl=FALSE, icont=-1;

  switch (cbv.item)
   {
    case FT_RC :
     switch (isel_contest)
      {
       case DX :
       case WPX :
        cfl = TRUE;
        for(i=0; i<=countries.number_of_countries; i++)
         if(!strcmp(cr_array[qnum].rec_county, countries.country_abv[i]))
          {
           cfl = FALSE;
           break;
          }
        if(cfl)
         {
          idisplay_type = IDT_COUNTRY;
          if(qnum>1)
           icont = get_continent_index (qnum-1);
          if(icont<0)
           icont = 4;
          ipage_screen[0][idisplay_type] = icont;
          update_display (main_drawing_area, REDRAW);
         }
        break;
       case FD :
       case NYQP :
       case SS :
        cfl = !valid_locus (cr_array[qnum].rec_county);
        if(cfl)
         {
          idisplay_type = IDT_LOCUS;
          update_display (main_drawing_area, REDRAW);
         }
        break;
       case WW :
        cfl = TRUE;
        for(i=0; i<=countries.number_of_countries; i++)
         if(!strcmp(cr_array[qnum].rec_county, countries.country_abv[i]))
          {
           cfl = FALSE;
           break;
          }
        if(cfl)
         {
          idisplay_type = IDT_COUZON;
          ipage_screen[0][idisplay_type] = get_zone_index (qnum);
          update_display (main_drawing_area, REDRAW);
         }
        break;
       case A160 :
       case TEN :
        cfl = TRUE;
        for(i=0; i<=countries.number_of_countries; i++)
         if(!strcmp(cr_array[qnum].rec_county, countries.country_abv[i]))
          {
           cfl = FALSE;
           break;
          }
        if(cfl)
         cfl = !valid_locus (cr_array[qnum].rec_county);
        if(cfl)
         {
          idisplay_type = IDT_LOCUS;
          update_display (main_drawing_area, REDRAW);
         }
        break;
       case WAA :
        break;
      }
     break;
    case FT_CALL :
      cfl = !valid_call (qnum);
     break;
    case FT_RN :
     switch (isel_contest)
      {
       case DX :
        lfield = strlen(cr_array[qnum].rec_num);
        if(lfield<4-mode)
         {
          cfl = TRUE;
          break;
         }
        if(!valid_report (qnum))
         {
          cfl = TRUE;
          break;
         }
        concat(part2, cr_array[qnum].rec_num, 3-mode, lfield-1, COPY);
        lpart2 = strlen(part2);
        ickr[0] = kb_index (part2, "C");
        ickr[1] = kb_index (part2, "K");
        ickr[2] = kb_index (part2, "R");
        if((ickr[0]>-1 && ickr[1]>-1) || (ickr[0]>-1 && ickr[2]>-1) ||
           (ickr[1]>-1 && ickr[2]>-1))
         {
          cfl = TRUE;
          break;
         }
        if((ickr[0]>-1 && (ickr[0]!=lpart2-1 || lpart2>3)) ||
           (ickr[1]>-1 && (ickr[1]!=lpart2-1 || lpart2>2)) ||
           (ickr[2]>-1 && ickr[2]==lpart2-1))
         {
          cfl = TRUE;
          break;
         }
        iwatts = atoi(part2);
        if(iwatts==0)
         {
          if(ickr[2]>-1)
           {
            concat(part3, part2, ickr[2]+1, lpart2-1, COPY);
            if(atoi(part3)==0)
             {
              cfl = TRUE;
              break;
             }
           }
          else if(ickr[0] && ickr[1])
           {
            cfl = TRUE;
            break;
           }
         }
        if(ickr[0]>-1)
         {
          if(ickr[0]==0)
           iwatts = 100;
          else
           iwatts *= 100;
          int_to_char (iwatts, part2);
          concat(rn_hold, cr_array[qnum].rec_num, 0, 2-mode, COPY);
          strcat(rn_hold, part2);
          strcpy(cr_array[qnum].rec_num, rn_hold);
         }
        else if(ickr[1]>-1)
         {
          if(ickr[1]==0)
           iwatts = 1000;
          else
           iwatts *= 1000;
          int_to_char (iwatts, part2);
          concat(rn_hold, cr_array[qnum].rec_num, 0, 2-mode, COPY);
          strcat(rn_hold, part2);
          strcpy(cr_array[qnum].rec_num, rn_hold);
         }
        else if(!ickr[2])
         {
          concat(rn_hold, cr_array[qnum].rec_num, 0, 2-mode, COPY);
          strcat(rn_hold, "0");
          part2[3] = '\0';
          strcat(rn_hold, part2);
          strcpy(cr_array[qnum].rec_num, rn_hold);
         }
        break;
       case WPX :
        lfield = strlen(cr_array[qnum].rec_num);
        if(lfield<4-mode)
         {
          cfl = TRUE;
          break;
         }
        if(!valid_report (qnum))
         {
          cfl = TRUE;
          break;
         }
        concat(part2, cr_array[qnum].rec_num, 3-mode, lfield-1, COPY);
        if(atoi(part2)<1)
         cfl = TRUE;
        break;
       case FD :
        lfield = strlen(cr_array[qnum].rec_num);
        if(lfield<2 || lfield>3)
         {
          cfl = TRUE;
          break;
         }
        if(!isalpha(cr_array[qnum].rec_num[lfield-1]))
         {
          cfl = TRUE;
          break;
         }
        for(i=0; i<lfield-1; i++)
         if(!isdigit(cr_array[qnum].rec_num[i]))
          {
           cfl = TRUE;
           break;
          }
        break;
       case NYQP :
        lfield = strlen(cr_array[qnum].rec_num);
        if(lfield<4-cr_array[qnum].mode)
         {
          cfl = TRUE;
          break;
         }
        if(!valid_report (qnum))
         {
          cfl = TRUE;
          break;
         }
        concat(part2, cr_array[qnum].rec_num,
               3-cr_array[qnum].mode, lfield-1, COPY);
        cfl = !valid_locus (part2);
        if(cfl)
         {
          idisplay_type = IDT_LOCUS;
          update_display (main_drawing_area, REDRAW);
         }
        break;
       case WW :
        lfield = strlen(cr_array[qnum].rec_num);
        if(lfield<4-mode)
         {
          cfl = TRUE;
          break;
         }
        if(!valid_report (qnum))
         {
          cfl = TRUE;
          break;
         }
        concat(part2, cr_array[qnum].rec_num, 3-mode, lfield-1, COPY);
        if(atoi(part2)<1 || atoi(part2)>40)
         {
          cfl = 2;
          break;
         }
        if(lfield==4-mode)
         {
          if(cr_array[qnum].rec_num[2-mode]=='9')
           {
            strcpy(rn_hold, cr_array[qnum].rec_num);
            concat(cr_array[qnum].rec_num, rn_hold, 0, 3-mode, COPY);
            cr_array[qnum].rec_num[3-mode] = '0';
            cr_array[qnum].rec_num[4-mode] = rn_hold[3-mode];
            cr_array[qnum].rec_num[5-mode] = '\0';
           }
          else
           cfl = 3;
         }
        break;
       case SS :
        if(atoi(cr_array[qnum].rec_num)<1 ||
           atoi(cr_array[qnum].rec_num)>9999 ||
           cr_array[qnum].rec_num[0]=='0')
         {
          cfl = TRUE;
          break;
         }
        lfield = strlen(cr_array[qnum].rec_num);
        for(i=0; i<lfield-1; i++)
         if(!isdigit(cr_array[qnum].rec_num[i]))
          {
           cfl = TRUE;
           break;
          }
        if(cfl) break;
        if(isalpha(cr_array[qnum].rec_num[lfield-1]))
         {
          concat(rn_hold, cr_array[qnum].rec_num, 0, lfield-2, COPY);
          concat(cr_array[qnum].tx, cr_array[qnum].rec_num,
                 lfield-1, lfield-1, COPY);
          strcpy(cr_array[qnum].rec_num, rn_hold);
         }
        break;
       case A160 :
        lfield = strlen(cr_array[qnum].rec_num);
        if(lfield<4-mode)
         {
          cfl = TRUE;
          break;
         }
        if(!valid_report (qnum))
         {
          cfl = TRUE;
          break;
         }
        concat(part2, cr_array[qnum].rec_num, 3-mode, lfield-1, COPY);
        cfl = !valid_locus (part2);
        if(cfl)
         {
          cfl = FALSE;
          for(i=0; i<strlen(part2); i++)
           if(!isdigit(part2[i]))
            {
             cfl = TRUE;
             idisplay_type = IDT_LOCUS;
             update_display (main_drawing_area, REDRAW);
             break;
            }
         }
        break;
       case TEN :
        lfield = strlen(cr_array[qnum].rec_num);
        if(lfield<4-cr_array[qnum].mode)
         {
          cfl = TRUE;
          break;
         }
        if(!valid_report (qnum))
         {
          cfl = TRUE;
          break;
         }
        concat(part2, cr_array[qnum].rec_num,
               3-cr_array[qnum].mode, lfield-1, COPY);
        cfl = !valid_locus (part2);
        if(cfl)
         {
          cfl = FALSE;
          for(i=0; i<strlen(part2); i++)
           if(!isdigit(part2[i]))
            {
             cfl = TRUE;
             idisplay_type = IDT_LOCUS;
             update_display (main_drawing_area, REDRAW);
             break;
            }
         }
        break;
       case WAA :
        break;
      }
     break;
    case FT_SENT :
     switch (isel_contest)
      {
       case DX :
        lfield = strlen(cr_array[qnum].sent);
        if(lfield<5-mode)
         {
          cfl = TRUE;
          break;
         }
        if(!valid_report (qnum))
         {
          cfl = TRUE;
          break;
         }
        for(i=3-mode; i<lfield; i++)
         if(!isupper(cr_array[qnum].sent[i]))
          {
           cfl = TRUE;
           break;
          }
        break;
       case WPX :
        lfield = strlen(cr_array[qnum].sent);
        if(lfield!=3-mode)
         {
          cfl = TRUE;
          break;
         }
        if(!valid_report (qnum))
         {
          cfl = TRUE;
          break;
         }
        break;
       case FD :
        lfield = strlen(cr_array[qnum].sent);
        if(lfield<4)
         {
          cfl = TRUE;
          break;
         }
        if(!isdigit(cr_array[qnum].sent[0]))
         {
          cfl = TRUE;
          break;
         }
         if(!isalpha(cr_array[qnum].sent[1]))
          {
           cfl = TRUE;
           break;
          }
        concat(part3, cr_array[qnum].sent, 2, lfield-1, COPY);
        cfl = !valid_locus (part3);
        if(cfl)
         {
          idisplay_type = IDT_LOCUS;
          update_display (main_drawing_area, REDRAW);
         }
        break;
       case WW :
        lfield = strlen(cr_array[qnum].sent);
        if(lfield<4-mode)
         {
          cfl = TRUE;
          break;
         }
        if(!valid_report (qnum))
         {
          cfl = TRUE;
          break;
         }
        concat(part2, cr_array[qnum].sent, 3-mode, lfield-1, COPY);
        if(atoi(part2)<1 || atoi(part2)>40)
         {
          cfl = 2;
          break;
         }
        if(lfield==4-mode)
         {
          if(cr_array[qnum].sent[2-mode]=='9')
           {
            strcpy(sent_hold, cr_array[qnum].sent);
            concat(cr_array[qnum].sent, sent_hold, 0, 3-mode, COPY);
            cr_array[qnum].sent[3-mode] = '0';
            cr_array[qnum].sent[4-mode] = sent_hold[3-mode];
            cr_array[qnum].sent[5-mode] = '\0';
           }
          else
           cfl = 3;
         }
        break;
       case SS :
        lfield = strlen(cr_array[qnum].sent);
        if(lfield<5)
         {
          cfl = TRUE;
          break;
         }
        cfl = !valid_precedence (cr_array[qnum].sent[0]);
        if(cfl) break;
        for(i=1; i<3; i++)
         if(!isdigit(cr_array[qnum].sent[i]))
          {
           cfl = TRUE;
           break;
          }
        if(cfl) break;
        concat(part3, cr_array[qnum].sent, 3, lfield-1, COPY);
        cfl = !valid_locus (part3);
        if(cfl)
         {
          idisplay_type = IDT_LOCUS;
          update_display (main_drawing_area, REDRAW);
         }
        break;
       case NYQP :
       case A160 :
       case TEN :
        lfield = strlen(cr_array[qnum].sent);
        if(lfield<5-cr_array[qnum].mode)
         {
          cfl = TRUE;
          break;
         }
        if(!valid_report (qnum))
         {
          cfl = TRUE;
          break;
         }
        concat(part3, cr_array[qnum].sent,
               3-cr_array[qnum].mode, lfield-1, COPY);
        cfl = !valid_locus (part3);
        if(isel_contest==NYQP && cfl)
         if(strcmp(init_screen_choices[6], "NY"))
          {
           cfl = FALSE;
           for(i=0; i<strlen(part3); i++)
            if(!isalpha(part3[i]))
             {
              cfl = TRUE;
              break;
             }
          }
        break;
       case WAA :
        break;
      }
     break;
    case FT_TX :
     if(isel_contest==SS)
      cfl = !valid_precedence (cr_array[qnum].tx[0]);
     break;
    case FT_PX :
     if(isel_contest==SS)
      {
       lfield = strlen(cr_array[qnum].prefix);
       if(lfield<2)
        {
         cfl = TRUE;
         break;
        }
       for(i=0; i<2; i++)
        if(!isdigit(cr_array[qnum].prefix[i]))
         {
          cfl = TRUE;
          break;
         }
       if(cfl) break;
       if(lfield>2)
        {
         concat(part3, cr_array[qnum].prefix, 2, lfield-1, COPY);
         cfl = !valid_locus (part3);
         if(cfl)
          {
           idisplay_type = IDT_LOCUS;
           update_display (main_drawing_area, REDRAW);
          }
         else
          {
           strcpy(cr_array[qnum].rec_county, part3);
           concat(rn_hold, cr_array[qnum].prefix, 0, 1, COPY);
           strcpy(cr_array[qnum].prefix, rn_hold);
          }
        }
      }
     break;
   }
  return cfl;
}

static void increment_que(void)
{
  gint i;

  qued.inq = FALSE;
  cbv.cursor_row = 24;
  for(i=0; i<2; i++)
   {
    strcpy(qued.call[i], qued.call[i+1]);
    if(strlen(qued.call[i]))
     qued.inq = TRUE;
   }
  strcpy(qued.call[2], "");
}

static void set_esd_flp(gint qnum, ESVars *esd)
{
  static gchar mbt_char[5];

  switch (cbv.item)
   {
    case FT_IBAND :
     strcpy(mbt_char, bandf[cr_array[qnum].iband]);
     esd->field_linep = mbt_char;
     break;
    case FT_MODE :
     strcpy(mbt_char, mode_char[cr_array[qnum].mode]);
     esd->field_linep = mbt_char;
     break;
    case FT_RC :
     esd->field_linep = cr_array[qnum].rec_county;
     break;
    case FT_CALL :
     esd->field_linep = cr_array[qnum].call;
     break;
    case FT_RN :
     esd->field_linep = cr_array[qnum].rec_num;
     break;
    case FT_DATE :
     esd->field_linep = cr_array[qnum].date;
     break;
    case FT_TIME :
     concat(mbt_char, cr_array[qnum].time, 0, 3, COPY);
     esd->field_linep = mbt_char;
     break;
    case FT_SENT :
     esd->field_linep = cr_array[qnum].sent;
     break;
    case FT_PX :
     esd->field_linep = cr_array[qnum].prefix;
     break;
    case FT_QSO :
     esd->field_linep = cr_array[qnum].qso;
     break;
    case FT_ONOFF :
     esd->field_linep = cr_array[qnum].on_off;
     break;
    case FT_DUPE :
     esd->field_linep = cr_array[qnum].dupe;
     break;
    case FT_PTS :
     esd->field_linep = cr_array[qnum].points;
     break;
    case FT_TX :
     esd->field_linep = cr_array[qnum].tx;
     break;
    case FT_NEXT1 :
     esd->field_linep = qued.call[0];
     break;
    case FT_NEXT2 :
     esd->field_linep = qued.call[1];
     break;
    case FT_NEXT3 :
     esd->field_linep = qued.call[2];
     break;
   }
}

static void edit_line(ESVars *esd)
{
  GdkRectangle update_bar;
  gint ccol, lflp, beep;
  gint cyan_bar_color, text_color;
  gchar str_hold[81];

  if(esd->ikey=='\0')
   {
//-----should never get here-----
    play_bells (BELL_SOUND);
    play_bells (BAD_CALL_SOUND);
    play_bells (BELL_SOUND);
    printf("null key encountered! %s\n", esd->field_linep);
    return;
   }

  beep = 1;
  ccol = *esd->cursor_colp-esd->field_col;
  lflp = strlen(esd->field_linep);
  if(insert_mode)
   {
    if(ialt_de=='E')
     {
      cyan_bar_color = YELLOW;
      text_color = RED;
     }
    else if(ialt_box=='G' || ialt_box=='M')
     {
      cyan_bar_color = YELLOW;
      text_color = BLACK;
     }
    else
     {
      cyan_bar_color = LT_GRAY;
      text_color = HI_CYAN;
     }
   }
  else
   {
    if(ialt_de=='E')
     {
      cyan_bar_color = RED;
      text_color = YELLOW;
     }
    else if(ialt_box=='G' || ialt_box=='M')
     {
      cyan_bar_color = BLACK;
      text_color = YELLOW;
     }
    else
     {
      cyan_bar_color = CYAN;
      text_color = WHITE;
     }
   }
  update_bar.x = (esd->field_col-1)*wcol;
  update_bar.y = (esd->cursor_row-1)*hrow;
  update_bar.width = esd->field_length*wcol;
  update_bar.height = hrow;
  switch (esd->ikey)
   {
    case 65507 : /*left ctrl*/
    case 65508 : /*right ctrl*/
    case 65289 : /*tab*/
    case 65056 : /*shift_tab*/
    case 65379 : /*insert*/
    case 65362 : /*up_arrow*/
    case 65364 : /*down_arrow*/
    case 65293 : /*enter*/
    case 65470 : /*F1*/
    case 65471 : /*F2*/
    case 65473 : /*F4*/
    case 65365 : /*page_up*/
    case 65366 : /*page_down*/
     beep = 0;
     draw_rectangle (&update_bar, cyan_bar_color, SAVEPIX);
     write_line (esd->cursor_row-1, esd->field_col-1,
      esd->field_linep, text_color, SAVEPIX);
     break;
    case 65361 : /*left_arrow*/
     if(ccol)
      {
       beep = 0;
       (*esd->cursor_colp)--;
      }
     break;
    case 65363 : /*right_arrow*/
     if(ccol<lflp)
      {
       beep = 0;
       (*esd->cursor_colp)++;
      }
     break;
    case 65360 : /*home*/
     if(ccol)
      {
       beep = 0;
       *esd->cursor_colp = esd->field_col;
      }
     break;
    case 65367 : /*end*/
     if(ccol<lflp)
      {
       beep = 0;
       *esd->cursor_colp = esd->field_col+lflp;
      }
     break;
    case 65535 : /*delete*/
     if(ccol<lflp)
      {
       beep = 0;
       concat(str_hold, esd->field_linep, 0, ccol-1, COPY);
       concat(str_hold, esd->field_linep, ccol+1, lflp-1, !COPY);
       strcpy(esd->field_linep, str_hold);
       draw_rectangle (&update_bar, cyan_bar_color, SAVEPIX);
       write_line (esd->cursor_row-1, esd->field_col-1,
        esd->field_linep, text_color, SAVEPIX);
      }
     break;
    case 65288 : /*backspace*/
     if(ccol)
      {
       beep = 0;
       concat(str_hold, esd->field_linep, 0, ccol-2, COPY);
       concat(str_hold, esd->field_linep, ccol, lflp-1, !COPY);
       strcpy(esd->field_linep, str_hold);
       (*esd->cursor_colp)--;
       draw_rectangle (&update_bar, cyan_bar_color, SAVEPIX);
       write_line (esd->cursor_row-1, esd->field_col-1,
        esd->field_linep, text_color, SAVEPIX);
      }
     break;
    case 65307 : /*escape*/
     beep = 0;
     *esd->cursor_colp = esd->field_col;
     esd->field_linep[0] = '\0';
     draw_rectangle (&update_bar, cyan_bar_color, SAVEPIX);
     break;
    default :
     if(esd->ikey==',' && ialt_box!='M')
      {
       beep = 0;
       draw_rectangle (&update_bar, cyan_bar_color, SAVEPIX);
       write_line (esd->cursor_row-1, esd->field_col-1,
        esd->field_linep, text_color, SAVEPIX);
      }
     else
      {
       if(insert_mode)
        {
         if((lflp<esd->field_length) && (ccol || esd->ikey!=' '))
          {
           beep = 0;
           concat(str_hold, esd->field_linep, 0, ccol-1, COPY);
           str_hold[ccol] = esd->ikey;
           str_hold[ccol+1] = '\0';
           concat(str_hold, esd->field_linep, ccol, lflp-1, !COPY);
           strcpy(esd->field_linep, str_hold);
           (*esd->cursor_colp)++;
           draw_rectangle (&update_bar, cyan_bar_color, SAVEPIX);
           write_line (esd->cursor_row-1, esd->field_col-1,
            esd->field_linep, text_color, SAVEPIX);
          }
        }
       else
        {
         if((lflp<=esd->field_length) && (ccol || esd->ikey!=' ') &&
            (ccol<esd->field_length))
          {
           beep = 0;
           concat(str_hold, esd->field_linep, 0, ccol-1, COPY);
           str_hold[ccol] = esd->ikey;
           str_hold[ccol+1] = '\0';
           concat(str_hold, esd->field_linep, ccol+1, lflp-1, !COPY);
           strcpy(esd->field_linep, str_hold);
           (*esd->cursor_colp)++;
           draw_rectangle (&update_bar, cyan_bar_color, SAVEPIX);
           write_line (esd->cursor_row-1, esd->field_col-1,
            esd->field_linep, text_color, SAVEPIX);
          }
        }
      }
   }
  if(!beep)
   gtk_widget_queue_draw_area (main_drawing_area,
                               update_bar.x,
                               update_bar.y,
                               update_bar.width+wcol,
                               update_bar.height);
  else
   play_bells (BELL_SOUND);
}
