void funCheckCalendarEvents(void){
    idata unsigned int i = 0;
    idata static unsigned char relay_bits = 0;
    idata unsigned char relay_num = 0;
    idata unsigned char on_off= 0;
    



    for (i=0; i<=CALENDAR_RECORDS; i++){ //check all records for a match
        funCalendarRead (i);
        if (c.reg.control != 0xFF){          //if the control field is unset then ditch the whole record.
            if (c.reg.year == Year || c.reg.year == CALENDAR_MASK){
                if (c.reg.month == Month || c.reg.month == CALENDAR_MASK){                
                    if (c.reg.date == Date || c.reg.date == CALENDAR_MASK){
                        if (c.reg.dayofweek == Day || c.reg.dayofweek == CALENDAR_MASK){
                            if (c.reg.hours == Hours || c.reg.hours == CALENDAR_MASK){
                                if (c.reg.minutes == Minutes || c.reg.minutes == CALENDAR_MASK){
                                    printf("Match @ entry %04d ", (unsigned int) i);
                                    printf("%02d:%02d ",(unsigned int)Hours, (unsigned int)Minutes);
                                    printf("on %02d.%02d.20%02d", (unsigned int)Date,(unsigned int)Month, (unsigned int)Year);

                                    printf("tMask is ");
                                    if (c.reg.hours == CALENDAR_MASK) printf("--:");
                                    else printf("%02d:",(unsigned int)c.reg.hours);

                                    if (c.reg.minutes == CALENDAR_MASK) printf("-- on ");
                                    else printf("%02d on ",(unsigned int)c.reg.minutes);

                                    if (c.reg.dayofweek == CALENDAR_MASK) printf("--- ");
                                    else printf("%s ",day_names[c.reg.dayofweek]);
                                

                                    if (c.reg.date == CALENDAR_MASK) printf("--.");
                                    else printf("%02d.",(unsigned int)c.reg.date);

                                    if (c.reg.month == CALENDAR_MASK) printf("--.");
                                    else printf("%02d." ,(unsigned int)c.reg.month);

                                    if (c.reg.year == CALENDAR_MASK) printf("20-- ");
                                    else printf("20%02d " , (unsigned int)c.reg.year); 


                                    relay_num = c.reg.control & 0x0F; 
                                    on_off    = (c.reg.control & 0xC0) >> 6;


                                    printf(" relay %02d " , relay_num);
                                    if (on_off == 1)  printf("On");
                                    if (on_off == 0)  printf("Off");
                                    if (on_off == 2)  printf("Toggle");
                                    printf("rn");

                                    if (relay_num <=8) {
                                        on_off = on_off << (relay_num -1);
                                        if (on_off != 0){
                                            //then shift it the correct number of places then OR it  
                                            relay_bits |= on_off;

                                        }
                                        else {
                                            //shift if the correct number of places then AND it
                                            relay_bits &= on_off;

                                        }


                                    }

                                }
                            }
                        }
                    }
                }
            }
        }
    }

    //only after we have checked all the records should we start the control functions
    //in this manner we are not inadvertently turning the outputs on and off in the same 
    //minute. 
    funActivateControl(relay_bits);
}