#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <dos.h>
#include <bios.h>
#include <ctype.h>

#define SCROLL_UP 0x001
#define SCROLL_DOWN 0x002

#define HIDE_CURSOR 0x001
#define NORM_CURSOR 0x002
#define BLOCK_CURSOR 0x003

#define HIGH 0xb
#define LOW 0x53
#define DF_UP 0x001
#define DF_DOWN 0x002

#define KB_ALT_F 0x21           //ALT+F
#define KB_ALT_H 0x23           //ALT+H
#define KB_ALT_I 0x17           //ALT+I
#define KB_ALT_X 0x2d           //ALT+X
#define KB_ALT_F3 0x6a          //ALT+F3
#define KB_ESC 0x1b             //ESC
#define KB_ENTER 0x0d           //ENTER
#define KB_F1 0x3b              //F1
#define KB_F2 0x3c              //F2
#define KB_F3 0x3d              //F3
#define KB_F4 0x3e              //F4
#define KB_F5 0x3f              //F5
#define KB_F6 0x40              //F6
#define KB_F7 0x41              //F7
#define KB_F8 0x42              //F8
#define KB_F9 0x43              //F9
#define KB_F10 0x44             //F10
#define KB_F11 0x48             //F11
#define KB_F12 0x50             //F12
#define KB_BKSPACE 0x08         //Backspace
#define KB_INSERT 0x52          //Insert
#define KB_DELETE 0x53          //Delete
#define KB_LEFT_ARR 0x4b        //<-
#define KB_RIGHT_ARR 0x4d       //->
#define KB_ARR_UP 0x48          //up arrow
#define KB_ARR_DOWN 0x50        //down arrow
#define KB_HOME 0x47            //Home
#define KB_END 0x4f             //End

#define MENU_MSG_FILE_NEW 0x001
#define MENU_MSG_FILE_OPEN 0x002
#define MENU_MSG_FILE_SAVE 0x003
#define MENU_MSG_FILE_SAVEAS 0x004
#define MENU_MSG_FILE_CLOSE 0x005
#define MENU_MSG_FILE_DEBUG 0x006
#define MENU_MSG_FILE_EXIT 0x007
#define MENU_MSG_FIRM_ADD 0x010
#define MENU_MSG_FIRM_DELETE 0x011
#define MENU_MSG_FIRM_FIND 0x012
#define MENU_MSG_HELP_ABOUT 0x20

enum BOOL {FALSE = 0, TRUE = 1};
typedef enum BOOL BOOL;

struct CDistributor
{
 char dTitle[10];
 char dAddr[10];
 char dSchet[5];
 char dName[10];
 char dPrice[10];
 char dNum[5];
 struct CDistributor *next, *prev;
};

struct Items
{
 char iName[25];
 int message;
 int iNumber;
 struct Items *next, *prev;
};

//typedef struct Items Items;

struct CMenu
{
 char menuname[10];
 int mPosition, mHight, mWidth;
 struct Items *fitem, *items;
 struct CMenu *next, *prev;
};

char readchar(void);
void writechar(char ch, char attr, char vp);
char *getstring(int x, int y, char *s, int len);
void cprint(char *str);
void scroll(int dir);
int getKey(void);
void cursor(int cs);
void DrawWindow( char *s );
void DrawBox(char *caption, int left, int top, int right, int bottom, BOOL shadow);
void DrawShadow(int left, int top, int right, int bottom);

void InitMenuBar(BOOL);
void InitStatusBar(BOOL);
void InitWindow(BOOL);
void Helper(void);

int DrawMenu(void);
void CreateMenu(void);
void DeleteMenu(void);
void UpdateMenu(BOOL);
void UpdateItem(BOOL);
void DrawStatusBar(void);

void DeleteSpisok(void);
void AddRecord(void);
void DeleteRecord(void);
void OutputRecord(int);
void FindRecord(void);
void ClearRecordData(void);
void MoveTo(int);

extern int left, top, right, bottom;
extern int event;
extern int cmnum;
extern char scr[512];
extern char winscr[4096];
extern struct CMenu *fmenu, *menu;
extern struct CDistributor *fdist, *dist;
extern BOOL debug;
extern char fname[13];
extern int maxRec, curRec;
