/* ResultsWindow.c */
/* Contains all code for displaying results window */

#include <exec/types.h>
#include <exec/memory.h>
#include <clib/asl_protos.h>
#include <clib/dos_protos.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/graphics_protos.h>
#include <graphics/gfxmacros.h>
#include <graphics/GfxBase.h>
#include <intuition/gadgetclass.h>
#include <intuition/intuition.h>
#include <intuition/IntuitionBase.h>
#include <libraries/asl.h>
#include <libraries/dos.h>
#include <libraries/gadtools.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include "ResultsWindow.h"
#include "CycleDBase.h"

/*
** Here is where all the initialization and creation of GadTools gadgets
** take place.  This function requires a pointer to a NULL-initialized
** gadget list pointer.  It returns a pointer to the last created gadget,
** which can be checked for success/failure.
*/
struct Gadget *createResultsGadgets(struct Gadget **glistptr, void *vi,
    UWORD topborder, struct Gadget *my_gads[])
{
struct NewGadget ng;
struct Gadget *gad;

/* All the gadget creation calls accept a pointer to the previous gadget, and
** link the new gadget to that gadget's NextGadget field.  Also, they exit
** gracefully, returning NULL, if any previous gadget was NULL.  This limits
** the amount of checking for failure that is needed.  You only need to check
** before you tweak any gadget structure or use any of its fields, and finally
** once at the end, before you add the gadgets.
*/

/* The following operation is required of any program that uses GadTools.
** It gives the toolkit a place to stuff context data.
*/
gad = CreateContext(glistptr);

/* Since the NewGadget structure is unmodified by any of the CreateGadget()
** calls, we need only change those fields which are different.
*/

ng.ng_LeftEdge   = 40;
ng.ng_TopEdge    = 10+topborder;
ng.ng_Width      = 20;
ng.ng_Height     = 10;
ng.ng_GadgetText = "<";
ng.ng_TextAttr   = &Topaz80;
ng.ng_VisualInfo = vi;
ng.ng_GadgetID   = MYGAD_SPREVIOUS;
ng.ng_Flags      = NG_HIGHLABEL;
my_gads[MYGAD_SPREVIOUS] = gad = CreateGadget(BUTTON_KIND, gad, &ng,
                    GA_Disabled, TRUE,
                    TAG_END);

ng.ng_LeftEdge   = 70;
ng.ng_GadgetText = "";
ng.ng_GadgetID   = MYGAD_SPREVTEXT;
my_gads[MYGAD_SPREVTEXT] = gad = CreateGadget(TEXT_KIND, gad, &ng,
                    GTTX_Text,   "Previous record",
                    TAG_END);

ng.ng_LeftEdge   = 215;
ng.ng_Width      = 50;
ng.ng_GadgetText = "";
ng.ng_GadgetID   = MYGAD_SRECCOUNT;
my_gads[MYGAD_SRECCOUNT] = gad = CreateGadget(NUMBER_KIND, gad, &ng,
                    GTNM_Number,        1,
                    GTNM_Border,        TRUE,
                    GTNM_Justification, GTJ_LEFT,
                    TAG_END);

ng.ng_LeftEdge   = 290;
ng.ng_GadgetText = "";
ng.ng_GadgetID   = MYGAD_SNEXTTEXT;
my_gads[MYGAD_SNEXTTEXT] = gad = CreateGadget(TEXT_KIND, gad, &ng,
                    GTTX_Text,   "Next record",
                    GT_Underscore, '_',
                    TAG_END);

ng.ng_LeftEdge   = 390;
ng.ng_Width      = 20;
ng.ng_Height     = 10;
ng.ng_GadgetText = ">";
ng.ng_GadgetID   = MYGAD_SNEXT;
my_gads[MYGAD_SNEXT] = gad = CreateGadget(BUTTON_KIND, gad, &ng,
                    TAG_END);

ng.ng_TopEdge   += 20;
ng.ng_LeftEdge   = 120;
ng.ng_Width      = 150;
ng.ng_Height     = 12;
ng.ng_GadgetText = "_Destination:";
ng.ng_GadgetID   = MYGAD_SDESTGAD;
my_gads[MYGAD_SDESTGAD] = gad = CreateGadget(STRING_KIND, gad, &ng,
                    GTST_String,   "",
                    GTST_MaxChars, 100,
                    GT_Underscore,    '_',
                    TAG_END);

ng.ng_LeftEdge  += 210;
ng.ng_Width      = 90;
ng.ng_GadgetText = "Date:";
ng.ng_GadgetID   = MYGAD_SDATEGAD;
my_gads[MYGAD_SDATEGAD] = gad = CreateGadget(TEXT_KIND, gad, &ng,
                    GTTX_Text, "",
                    GT_Underscore, '_',
                    TAG_END);

ng.ng_LeftEdge   = 144;
ng.ng_Width      = 60;
ng.ng_TopEdge   += 20;
ng.ng_GadgetText = "Cycle Distance:";
ng.ng_GadgetID   = MYGAD_SDIST1;
my_gads[MYGAD_SDIST1] = gad = CreateGadget(NUMBER_KIND, gad, &ng,
                    GT_Underscore, '_',
                    TAG_END);

ng.ng_Width      = 10;
ng.ng_LeftEdge   = 204;
ng.ng_GadgetText = "";
ng.ng_GadgetID   = MYGAD_SDISTPOINT;
my_gads[MYGAD_SDISTPOINT] = gad = CreateGadget(TEXT_KIND, gad, &ng,
                    GTTX_Text,             ".",
                    GT_Underscore,         '_',
                    TAG_END);

ng.ng_LeftEdge   = 214;
ng.ng_Width      = 60;
ng.ng_GadgetText = "";
ng.ng_GadgetID   = MYGAD_SDIST2;
my_gads[MYGAD_SDIST2] = gad = CreateGadget(NUMBER_KIND, gad, &ng,
                    GT_Underscore, '_',
                    TAG_END);

ng.ng_TopEdge   += 20;
ng.ng_LeftEdge   = 144;
ng.ng_GadgetText = "Cycle Out Time:";
ng.ng_GadgetID   = MYGAD_SCYCLEOUTH;
my_gads[MYGAD_SCYCLEOUTH] = gad = CreateGadget(NUMBER_KIND, gad, &ng,
                    GT_Underscore, '_',
                    TAG_END);

ng.ng_LeftEdge   = 214;
ng.ng_GadgetText = "";
ng.ng_GadgetID   = MYGAD_SCYCLEOUTM;
my_gads[MYGAD_SCYCLEOUTM] = gad = CreateGadget(NUMBER_KIND, gad, &ng,
                    GT_Underscore, '_',
                    TAG_END);

ng.ng_TopEdge   += 20;
ng.ng_LeftEdge   = 152;
ng.ng_GadgetText = "Cycle Back Time:";
ng.ng_GadgetID   = MYGAD_SCYCLEBACKH;
my_gads[MYGAD_SCYCLEBACKH] = gad = CreateGadget(NUMBER_KIND, gad, &ng,
                    GT_Underscore, '_',
                    TAG_END);

ng.ng_LeftEdge   = 222;
ng.ng_GadgetText = "";
ng.ng_GadgetID   = MYGAD_SCYCLEBACKM;
my_gads[MYGAD_SCYCLEBACKM] = gad = CreateGadget(NUMBER_KIND, gad, &ng,
                    GT_Underscore, '_',
                    TAG_END);

ng.ng_TopEdge   += 20;
ng.ng_LeftEdge   = 72;
ng.ng_GadgetText = "_Train?";
ng.ng_GadgetID   = MYGAD_STRAINTICK;
my_gads[MYGAD_STRAINTICK] = gad = CreateGadget(CHECKBOX_KIND, gad, &ng,
                    GTCB_Checked,  FALSE,
                    GA_Disabled, TRUE,
                    GT_Underscore, '_',
                    TAG_END);

ng.ng_LeftEdge   = 190;
ng.ng_GadgetText = "Out Time:";
ng.ng_GadgetID   = MYGAD_STRAINOUTH;
my_gads[MYGAD_STRAINOUTH] = gad = CreateGadget(NUMBER_KIND, gad, &ng,
                    GT_Underscore,  '_',
                    TAG_END);

ng.ng_LeftEdge   = 260;
ng.ng_GadgetText = "";
ng.ng_GadgetID   = MYGAD_STRAINOUTM;
my_gads[MYGAD_STRAINOUTM] = gad = CreateGadget(NUMBER_KIND, gad, &ng,
                    GT_Underscore,  '_',
                    TAG_END);

ng.ng_TopEdge   += 20;
ng.ng_LeftEdge   = 198;
ng.ng_GadgetText = "Back Time:";
ng.ng_GadgetID   = MYGAD_STRAINBACKH;
my_gads[MYGAD_STRAINBACKH] = gad = CreateGadget(NUMBER_KIND, gad, &ng,
                    GT_Underscore,  '_',
                    TAG_END);

ng.ng_LeftEdge   = 268;
ng.ng_GadgetText = "";
ng.ng_GadgetID   = MYGAD_STRAINBACKM;
my_gads[MYGAD_STRAINBACKM] = gad = CreateGadget(NUMBER_KIND, gad, &ng,
                    GT_Underscore,  '_',
                    TAG_END);

ng.ng_TopEdge   += 20;
ng.ng_LeftEdge   = 112;
ng.ng_GadgetText = "Total Time:";
ng.ng_GadgetID   = MYGAD_STOTTIMEH;
my_gads[MYGAD_STOTTIMEH] = gad = CreateGadget(NUMBER_KIND, gad, &ng,
                    GT_Underscore, '_',
                    TAG_END);

ng.ng_LeftEdge   = 180;
ng.ng_GadgetText = "";
ng.ng_GadgetID   = MYGAD_STOTTIMEM;
my_gads[MYGAD_STOTTIMEM] = gad = CreateGadget(NUMBER_KIND, gad, &ng,
                    GT_Underscore, '_',
                    TAG_END);

return(gad);
}

/*
** Function to handle a GADGETUP or GADGETDOWN event.
*/
BOOL handleResultsGadgetEvent(struct Screen *mysc, struct Window *win, struct Gadget *gad, UWORD code,
    struct Gadget *my_gads[])
{
switch (gad->GadgetID)
    {
    case MYGAD_SPREVIOUS:
        return(TRUE);
        break;
    case MYGAD_SNEXT:
        return(TRUE);
        break;
    }
    return(FALSE);
}

/*
** Function to handle vanilla keys.
*/
BOOL handleResultsVanillaKey(struct Window *win, UWORD code,
    struct Gadget *my_gads[], int index, int& direction)
{
switch (code)
    {
    case ',':
        direction = -1;
        return(TRUE);
        break;
    case '.':
        direction = 1;
        return(TRUE);
        break;
    }
    return(FALSE);
}

/*
** Standard message handling loop with GadTools message handling functions
** used (GT_GetIMsg() and GT_ReplyIMsg()).
*/
void interaction(struct Screen *mysc, struct Window *mywin,
    struct Gadget *my_gads[], int searchrecords, data searchdata[])
{
    struct IntuiMessage *imsg;
    ULONG imsgClass;
    UWORD imsgCode;
    BOOL terminated = FALSE, cont = FALSE, move_record = FALSE;
    int direction = 0;
    struct Gadget *gad;
    int index = 0;

    // Enter record one data into gadgets
    GT_SetGadgetAttrs(my_gads[MYGAD_SDESTGAD], mywin, NULL, GTST_String, searchdata[0].destination, TAG_END);
    GT_SetGadgetAttrs(my_gads[MYGAD_SDATEGAD], mywin, NULL, GTTX_Text, searchdata[0].date, TAG_END);
    GT_SetGadgetAttrs(my_gads[MYGAD_SDIST1], mywin, NULL, GTNM_Number, searchdata[0].cycledist1, TAG_END);
    GT_SetGadgetAttrs(my_gads[MYGAD_SDIST2], mywin, NULL, GTNM_Number, searchdata[0].cycledist2, TAG_END);
    GT_SetGadgetAttrs(my_gads[MYGAD_SCYCLEOUTH], mywin, NULL, GTNM_Number, searchdata[0].cycleouth, TAG_END);
    GT_SetGadgetAttrs(my_gads[MYGAD_SCYCLEOUTM], mywin, NULL, GTNM_Number, searchdata[0].cycleoutm, TAG_END);
    GT_SetGadgetAttrs(my_gads[MYGAD_SCYCLEBACKH], mywin, NULL, GTNM_Number, searchdata[0].cyclebackh, TAG_END);
    GT_SetGadgetAttrs(my_gads[MYGAD_SCYCLEBACKM], mywin, NULL, GTNM_Number, searchdata[0].cyclebackm, TAG_END);
    GT_SetGadgetAttrs(my_gads[MYGAD_STRAINTICK], mywin, NULL, GTCB_Checked, searchdata[0].train, TAG_END);
    GT_SetGadgetAttrs(my_gads[MYGAD_STRAINOUTH], mywin, NULL, GTNM_Number, searchdata[0].trainouth, TAG_END);
    GT_SetGadgetAttrs(my_gads[MYGAD_STRAINOUTM], mywin, NULL, GTNM_Number, searchdata[0].trainoutm, TAG_END);
    GT_SetGadgetAttrs(my_gads[MYGAD_STRAINBACKH], mywin, NULL, GTNM_Number, searchdata[0].trainbackh, TAG_END);
    GT_SetGadgetAttrs(my_gads[MYGAD_STRAINBACKM], mywin, NULL, GTNM_Number, searchdata[0].trainbackm, TAG_END);
    GT_SetGadgetAttrs(my_gads[MYGAD_STOTTIMEH], mywin, NULL, GTNM_Number, searchdata[0].totalh, TAG_END);
    GT_SetGadgetAttrs(my_gads[MYGAD_STOTTIMEM], mywin, NULL, GTNM_Number, searchdata[0].totalm, TAG_END);

    // If only one record then disable navigation gadgets
    if(searchrecords == 1)
    {
//        GT_SetGadgetAttrs(my_gads[MYGAD_SPREVIOUS], mywin, NULL, GA_Disabled, TRUE, TAG_END);
        GT_SetGadgetAttrs(my_gads[MYGAD_SNEXT], mywin, NULL, GA_Disabled, TRUE, TAG_END);
    }
while (!terminated)
    {
    Wait (1 << mywin->UserPort->mp_SigBit);

    /* GT_GetIMsg() returns an IntuiMessage with more friendly information for
    ** complex gadget classes.  Use it wherever you get IntuiMessages where
    ** using GadTools gadgets.
    */

    while ((!terminated) &&
           (imsg = GT_GetIMsg(mywin->UserPort)))
        {
        /* Presuming a gadget, of course, but no harm...
        ** Only dereference this value (gad) where the Class specifies
        ** that it is a gadget event.
        */
        gad = (struct Gadget *)imsg->IAddress;

        imsgClass = imsg->Class;
        imsgCode = imsg->Code;


        /* Use the toolkit message-replying function here... */
        GT_ReplyIMsg(imsg);

        switch (imsgClass)
            {
            /*  --- WARNING --- WARNING --- WARNING --- WARNING --- WARNING ---
            ** GadTools puts the gadget address into IAddress of IDCMP_MOUSEMOVE
            ** messages.  This is NOT true for standard Intuition messages,
            ** but is an added feature of GadTools.
            */
            case IDCMP_VANILLAKEY:
                cont = handleResultsVanillaKey(mywin, imsgCode, my_gads, index, direction);
                if(cont == TRUE)
                {
                    if(direction < 0)
                    {
                        // Go back one record
                        if(index > 0)
                        {
                            // If moving backwards then enable forward gadget
                            if(index == searchrecords-1)
                            {
                                GT_SetGadgetAttrs(my_gads[MYGAD_SNEXT], mywin, NULL, GA_Disabled, FALSE, TAG_END);
                            }
                            index--;
                            // Load new gadget values
                            GT_SetGadgetAttrs(my_gads[MYGAD_SRECCOUNT], mywin, NULL, GTNM_Number, index+1, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SDESTGAD], mywin, NULL, GTST_String, searchdata[index].destination, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SDATEGAD], mywin, NULL, GTTX_Text, searchdata[index].date, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SDIST1], mywin, NULL, GTNM_Number, searchdata[index].cycledist1, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SDIST2], mywin, NULL, GTNM_Number, searchdata[index].cycledist2, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SCYCLEOUTH], mywin, NULL, GTNM_Number, searchdata[index].cycleouth, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SCYCLEOUTM], mywin, NULL, GTNM_Number, searchdata[index].cycleoutm, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SCYCLEBACKH], mywin, NULL, GTNM_Number, searchdata[index].cyclebackh, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SCYCLEBACKM], mywin, NULL, GTNM_Number, searchdata[index].cyclebackm, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_STRAINTICK], mywin, NULL, GTCB_Checked, searchdata[index].train, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_STRAINOUTH], mywin, NULL, GTNM_Number, searchdata[index].trainouth, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_STRAINOUTM], mywin, NULL, GTNM_Number, searchdata[index].trainoutm, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_STRAINBACKH], mywin, NULL, GTNM_Number, searchdata[index].trainbackh, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_STRAINBACKM], mywin, NULL, GTNM_Number, searchdata[index].trainbackm, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_STOTTIMEH], mywin, NULL, GTNM_Number, searchdata[index].totalh, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_STOTTIMEM], mywin, NULL, GTNM_Number, searchdata[index].totalm, TAG_END);
                        }
                        // Disable back gadget if cannot move further back
                        if(index == 0)
                        {
                            GT_SetGadgetAttrs(my_gads[MYGAD_SPREVIOUS], mywin, NULL, GA_Disabled, TRUE, TAG_END);
                        }
                    }
                    if(direction > 0)
                    {
                        // Go forward one record
                        if(index < searchrecords-1)
                        {
                            // If moving forward then enable back gadget
                            if(index == 0)
                            {
                                GT_SetGadgetAttrs(my_gads[MYGAD_SPREVIOUS], mywin, NULL, GA_Disabled, FALSE, TAG_END);
                            }
                            index++;
                            // Load new gadget values
                            GT_SetGadgetAttrs(my_gads[MYGAD_SRECCOUNT], mywin, NULL, GTNM_Number, index+1, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SDESTGAD], mywin, NULL, GTST_String, searchdata[index].destination, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SDATEGAD], mywin, NULL, GTTX_Text, searchdata[index].date, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SDIST1], mywin, NULL, GTNM_Number, searchdata[index].cycledist1, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SDIST2], mywin, NULL, GTNM_Number, searchdata[index].cycledist2, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SCYCLEOUTH], mywin, NULL, GTNM_Number, searchdata[index].cycleouth, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SCYCLEOUTM], mywin, NULL, GTNM_Number, searchdata[index].cycleoutm, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SCYCLEBACKH], mywin, NULL, GTNM_Number, searchdata[index].cyclebackh, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SCYCLEBACKM], mywin, NULL, GTNM_Number, searchdata[index].cyclebackm, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_STRAINTICK], mywin, NULL, GTCB_Checked, searchdata[index].train, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_STRAINOUTH], mywin, NULL, GTNM_Number, searchdata[index].trainouth, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_STRAINOUTM], mywin, NULL, GTNM_Number, searchdata[index].trainoutm, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_STRAINBACKH], mywin, NULL, GTNM_Number, searchdata[index].trainbackh, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_STRAINBACKM], mywin, NULL, GTNM_Number, searchdata[index].trainbackm, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_STOTTIMEH], mywin, NULL, GTNM_Number, searchdata[index].totalh, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_STOTTIMEM], mywin, NULL, GTNM_Number, searchdata[index].totalm, TAG_END);
                        }
                        // Disable forward gadget if cannot move further forward
                        if(index == searchrecords-1)
                        {
                            GT_SetGadgetAttrs(my_gads[MYGAD_SNEXT], mywin, NULL, GA_Disabled, TRUE, TAG_END);
                        }
                    }
                }
                break;
            case IDCMP_GADGETUP:
                move_record = handleResultsGadgetEvent(mysc, mywin, gad, imsgCode, my_gads);

                if(move_record == TRUE)
                {
                    if(gad->GadgetID == MYGAD_SPREVIOUS)
                    {
                        if(index > 0)
                        {
                            // If moving backwards then enable forward gadget
                            if(index == searchrecords-1)
                            {
                                GT_SetGadgetAttrs(my_gads[MYGAD_SNEXT], mywin, NULL, GA_Disabled, FALSE, TAG_END);
                            }
                            index--;
                            // Load new gadget values
                            GT_SetGadgetAttrs(my_gads[MYGAD_SRECCOUNT], mywin, NULL, GTNM_Number, index+1, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SDESTGAD], mywin, NULL, GTST_String, searchdata[index].destination, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SDATEGAD], mywin, NULL, GTTX_Text, searchdata[index].date, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SDIST1], mywin, NULL, GTNM_Number, searchdata[index].cycledist1, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SDIST2], mywin, NULL, GTNM_Number, searchdata[index].cycledist2, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SCYCLEOUTH], mywin, NULL, GTNM_Number, searchdata[index].cycleouth, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SCYCLEOUTM], mywin, NULL, GTNM_Number, searchdata[index].cycleoutm, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SCYCLEBACKH], mywin, NULL, GTNM_Number, searchdata[index].cyclebackh, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SCYCLEBACKM], mywin, NULL, GTNM_Number, searchdata[index].cyclebackm, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_STRAINTICK], mywin, NULL, GTCB_Checked, searchdata[index].train, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_STRAINOUTH], mywin, NULL, GTNM_Number, searchdata[index].trainouth, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_STRAINOUTM], mywin, NULL, GTNM_Number, searchdata[index].trainoutm, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_STRAINBACKH], mywin, NULL, GTNM_Number, searchdata[index].trainbackh, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_STRAINBACKM], mywin, NULL, GTNM_Number, searchdata[index].trainbackm, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_STOTTIMEH], mywin, NULL, GTNM_Number, searchdata[index].totalh, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_STOTTIMEM], mywin, NULL, GTNM_Number, searchdata[index].totalm, TAG_END);
                        }
                        // Disable back gadget if cannot move further back
                        if(index == 0)
                        {
                            GT_SetGadgetAttrs(my_gads[MYGAD_SPREVIOUS], mywin, NULL, GA_Disabled, TRUE, TAG_END);
                        }
                    }
                    if(gad->GadgetID == MYGAD_SNEXT)
                    {
                        if(index < searchrecords-1)
                        {
                            // If moving forward then enable back gadget
                            if(index == 0)
                            {
                                GT_SetGadgetAttrs(my_gads[MYGAD_SPREVIOUS], mywin, NULL, GA_Disabled, FALSE, TAG_END);
                            }
                            index++;
                            // Load new gadget values
                            GT_SetGadgetAttrs(my_gads[MYGAD_SRECCOUNT], mywin, NULL, GTNM_Number, index+1, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SDESTGAD], mywin, NULL, GTST_String, searchdata[index].destination, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SDATEGAD], mywin, NULL, GTTX_Text, searchdata[index].date, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SDIST1], mywin, NULL, GTNM_Number, searchdata[index].cycledist1, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SDIST2], mywin, NULL, GTNM_Number, searchdata[index].cycledist2, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SCYCLEOUTH], mywin, NULL, GTNM_Number, searchdata[index].cycleouth, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SCYCLEOUTM], mywin, NULL, GTNM_Number, searchdata[index].cycleoutm, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SCYCLEBACKH], mywin, NULL, GTNM_Number, searchdata[index].cyclebackh, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_SCYCLEBACKM], mywin, NULL, GTNM_Number, searchdata[index].cyclebackm, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_STRAINTICK], mywin, NULL, GTCB_Checked, searchdata[index].train, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_STRAINOUTH], mywin, NULL, GTNM_Number, searchdata[index].trainouth, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_STRAINOUTM], mywin, NULL, GTNM_Number, searchdata[index].trainoutm, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_STRAINBACKH], mywin, NULL, GTNM_Number, searchdata[index].trainbackh, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_STRAINBACKM], mywin, NULL, GTNM_Number, searchdata[index].trainbackm, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_STOTTIMEH], mywin, NULL, GTNM_Number, searchdata[index].totalh, TAG_END);
                            GT_SetGadgetAttrs(my_gads[MYGAD_STOTTIMEM], mywin, NULL, GTNM_Number, searchdata[index].totalm, TAG_END);
                        }
                        // Disable forward gadget if cannot move further forward
                        if(index == searchrecords-1)
                        {
                            GT_SetGadgetAttrs(my_gads[MYGAD_SNEXT], mywin, NULL, GA_Disabled, TRUE, TAG_END);
                        }
                    }
                }
                // Redraw gadgets to show updates
                GT_BeginRefresh(mywin);
                GT_EndRefresh(mywin, TRUE);

                break;
            case IDCMP_CLOSEWINDOW:
                terminated = TRUE;
                break;
            case IDCMP_REFRESHWINDOW:
                /* With GadTools, the application must use GT_BeginRefresh()
                ** where it would normally have used BeginRefresh()
                */
                GT_BeginRefresh(mywin);
                GT_EndRefresh(mywin, TRUE);
                break;
            }
        }
    }
}

// Function to create and open search window
void
resultswindow(struct Screen *mysc, int searchrecords, data searchdata[])
{
    struct Window   *resultswin;
    struct Gadget   *glist, *results_gads[NO_OF_GADS];
    void            *vi;
    UWORD           topborder;

if (NULL == (vi = GetVisualInfo(mysc, TAG_END)))
{
    EasyRequest(NULL, &novisinfo, NULL);
}
else
{
    /* Here is how we can figure out ahead of time how tall the  */
    /* window's title bar will be:                               */
    topborder = mysc->WBorTop + (mysc->Font->ta_YSize + 1);

    if (NULL == createResultsGadgets(&glist, vi, topborder,
                                         results_gads))
    {
        EasyRequest(NULL, &nogadgets, NULL);
    }
    else
    {

        if (NULL == (resultswin = OpenWindowTags(NULL,
            WA_Title,     "Advanced Search Results - Close window to end search",
            WA_Gadgets,   glist,      WA_AutoAdjust,    TRUE,
            WA_Width,       450,      WA_MinWidth,       450,
            WA_InnerHeight, 200,      WA_MinHeight,      200,
            WA_DragBar,    TRUE,      WA_DepthGadget,   TRUE,
            WA_Activate,   TRUE,      WA_CloseGadget,   TRUE,
            WA_SimpleRefresh, TRUE,
            WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW |
                IDCMP_VANILLAKEY | BUTTONIDCMP,
                WA_PubScreen, mysc,
            TAG_END)))
            {
                EasyRequest(NULL, &nowindow, NULL);
            }
            else
            {
                /* After window is open, gadgets must be refreshed with a
                ** call to the GadTools refresh window function.
                */
                GT_RefreshWindow(resultswin, NULL);

                // Deal with interaction
                interaction(mysc, resultswin, results_gads, searchrecords, searchdata);

                CloseWindow(resultswin);
            }
        }
        /* FreeGadgets() even if createAllGadgets() fails, as some
        ** of the gadgets may have been created...If glist is NULL
        ** then FreeGadgets() will do nothing.
        */
        FreeGadgets(glist);
        FreeVisualInfo(vi);
    }
}

