/*
 * dlg_us.vdl $Header: dlg_us.vdl, 3, 29.09.2009 15:28:41, Esther Bergter$ $Date: 29.09.2009 15:28:41$
 *
 */

// *********************************************************************
// Adapt local language configuration here ...
//
// What's my Dialoge Language   0=de, 1=us, 2=it, 3=fr, 4=es
#define MY_LANGUAGE 1

// What additional languages can I speak (excluding MY_LANGUAGE)
// CAN_de, CAN_us, CAN_it, CAN_fr, CAN_es
#define CAN_de

// Adapt local language configuration above
// *********************************************************************

#include "lib.h"

int nr_of_additional_languages;      // Number of defined CAN_xx
int additional_language;  // if nr_of_languages = 1 the code of this Language
                          // otherwise -1

/***********************************************
 * HERE IS THE IMAP/LDAP CONFIGURATION SECTION *
 ***********************************************/

#include "extension.vdl"

/****************
 * some helpers *
 ****************/

void SayNumber(char *number)
{
  if ((strcmp(number, "0") == 0) || (strcmp(number, "00") == 0))
  {
    SayTTSC("zero"); return;
  }
  if ((strcmp(number, "1") == 0) || (strcmp(number, "01") == 0))
  {
    SayTTSC("one"); return;
  }
  if ((strcmp(number, "2") == 0) || (strcmp(number, "02") == 0))
  {
    SayTTSC("two"); return;
  }
  if ((strcmp(number, "3") == 0) || (strcmp(number, "03") == 0))
  {
    SayTTSC("three"); return;
  }
  if ((strcmp(number, "4") == 0) || (strcmp(number, "04") == 0))
  {
    SayTTSC("four"); return;
  }
  if ((strcmp(number, "5") == 0) || (strcmp(number, "05") == 0))
  {
    SayTTSC("five"); return;
  }
  if ((strcmp(number, "6") == 0) || (strcmp(number, "06") == 0))
  {
    SayTTSC("six"); return;
  }
  if ((strcmp(number, "7") == 0) || (strcmp(number, "07") == 0))
  {
    SayTTSC("seven"); return;
  }
  if ((strcmp(number, "8") == 0) || (strcmp(number, "08") == 0))
  {
    SayTTSC("eight"); return;
  }
  if ((strcmp(number, "9") == 0) || (strcmp(number, "09") == 0))
  {
    SayTTSC("nine"); return;
  }
  if (strcmp(number, "10") == 0)
  {
    SayTTSC("ten"); return;
  }
  if (strcmp(number, "11") == 0)
  {
    SayTTSC("eleven"); return;
  }
  if (strcmp(number, "12") == 0)
  {
    SayTTSC("twelve"); return;
  }
  if (strcmp(number, "13") == 0)
  {
    SayTTSC("thirteen"); return;
  }
  if (strcmp(number, "14") == 0)
  {
    SayTTSC("fourteen"); return;
  }
  if (strcmp(number, "15") == 0)
  {
    SayTTSC("fifteen"); return;
  }
  if (strcmp(number, "16") == 0)
  {
    SayTTSC("sixteen"); return;
  }
  if (strcmp(number, "17") == 0)
  {
    SayTTSC("seventeen"); return;
  }
  if (strcmp(number, "18") == 0)
  {
    SayTTSC("eighteen"); return;
  }
  if (strcmp(number, "19") == 0)
  {
    SayTTSC("nineteen"); return;
  }
  if (strcmp(number, "20") == 0)
  {
    SayTTSC("twenty"); return;
  }
  if (strcmp(number, "21") == 0)
  {
    SayTTSC("twenty-one"); return;
  }
  if (strcmp(number, "22") == 0)
  {
    SayTTSC("twenty-two"); return;
  }
  if (strcmp(number, "23") == 0)
  {
    SayTTSC("twenty-three"); return;
  }
  if (strcmp(number, "24") == 0)
  {
    SayTTSC("twenty-four"); return;
  }
  if (strcmp(number, "25") == 0)
  {
    SayTTSC("twenty-five"); return;
  }
  if (strcmp(number, "26") == 0)
  {
    SayTTSC("twenty-six"); return;
  }
  if (strcmp(number, "27") == 0)
  {
    SayTTSC("twenty-seven"); return;
  }
  if (strcmp(number, "28") == 0)
  {
    SayTTSC("twenty-eight"); return;
  }
  if (strcmp(number, "29") == 0)
  {
    SayTTSC("twenty-nine"); return;
  }
  if (strcmp(number, "30") == 0)
  {
    SayTTSC("thirty"); return;
  }
  if (strcmp(number, "31") == 0)
  {
    SayTTSC("thirty-one"); return;
  }
  if (strcmp(number, "32") == 0)
  {
    SayTTSC("thirty-two"); return;
  }
  if (strcmp(number, "33") == 0)
  {
    SayTTSC("thirty-three"); return;
  }
  if (strcmp(number, "34") == 0)
  {
    SayTTSC("thirty-four"); return;
  }
  if (strcmp(number, "35") == 0)
  {
    SayTTSC("thirty-five"); return;
  }
  if (strcmp(number, "36") == 0)
  {
    SayTTSC("thirty-six"); return;
  }
  if (strcmp(number, "37") == 0)
  {
    SayTTSC("thirty-seven"); return;
  }
  if (strcmp(number, "38") == 0)
  {
    SayTTSC("thirty-eight"); return;
  }
  if (strcmp(number, "39") == 0)
  {
    SayTTSC("thirty-nine"); return;
  }
  if (strcmp(number, "40") == 0)
  {
    SayTTSC("fourty"); return;
  }
  if (strcmp(number, "41") == 0)
  {
    SayTTSC("fourty-one"); return;
  }
  if (strcmp(number, "42") == 0)
  {
    SayTTSC("fourty-two"); return;
  }
  if (strcmp(number, "43") == 0)
  {
    SayTTSC("fourty-three"); return;
  }
  if (strcmp(number, "44") == 0)
  {
    SayTTSC("fourty-four"); return;
  }
  if (strcmp(number, "45") == 0)
  {
    SayTTSC("fourty-five"); return;
  }
  if (strcmp(number, "46") == 0)
  {
    SayTTSC("fourty-six"); return;
  }
  if (strcmp(number, "47") == 0)
  {
    SayTTSC("fourty-seven"); return;
  }
  if (strcmp(number, "48") == 0)
  {
    SayTTSC("fourty-eight"); return;
  }
  if (strcmp(number, "49") == 0)
  {
    SayTTSC("fourty-nine"); return;
  }
  if (strcmp(number, "50") == 0)
  {
    SayTTSC("fifty"); return;
  }
  if (strcmp(number, "51") == 0)
  {
    SayTTSC("fifty-one"); return;
  }
  if (strcmp(number, "52") == 0)
  {
    SayTTSC("fifty-two"); return;
  }
  if (strcmp(number, "53") == 0)
  {
    SayTTSC("fifty-three"); return;
  }
  if (strcmp(number, "54") == 0)
  {
    SayTTSC("fifty-four"); return;
  }
  if (strcmp(number, "55") == 0)
  {
    SayTTSC("fifty-five"); return;
  }
  if (strcmp(number, "56") == 0)
  {
    SayTTSC("fifty-six"); return;
  }
  if (strcmp(number, "57") == 0)
  {
    SayTTSC("fifty-seven"); return;
  }
  if (strcmp(number, "58") == 0)
  {
    SayTTSC("fifty-eight"); return;
  }
  if (strcmp(number, "59") == 0)
  {
    SayTTSC("fifty-nine"); return;
  }
  if (strcmp(number, "2000") == 0)
  {
    SayTTSC("two thousand"); return;
  }
  if (strcmp(number, "2001") == 0)
  {
    SayTTSC("two thousand one"); return;
  }
  if (strcmp(number, "2002") == 0)
  {
    SayTTSC("two thousand two"); return;
  }
  if (strcmp(number, "2003") == 0)
  {
    SayTTSC("two thousand three"); return;
  }
  if (strcmp(number, "2004") == 0)
  {
    SayTTSC("two thousand four"); return;
  }
  if (strcmp(number, "2005") == 0)
  {
    SayTTSC("two thousand five"); return;
  }
  if (strcmp(number, "2006") == 0)
  {
    SayTTSC("two thousand six"); return;
  }
  if (strcmp(number, "2007") == 0)
  {
    SayTTSC("two thousand seven"); return;
  }
  if (strcmp(number, "2008") == 0)
  {
    SayTTSC("two thousand eight"); return;
  }
  if (strcmp(number, "2009") == 0)
  {
    SayTTSC("two thousand nine"); return;
  }
  if (strcmp(number, "2010") == 0)
  {
    SayTTSC("two thousand ten"); return;
  }

  SayTTS(number);
}

void SayDigit(char cDigit)
{
  char szDigit[2];

  szDigit[0] = cDigit;
  szDigit[1] = 0;
  SayNumber(szDigit);
}

void SayDay(int day)
{
  switch (day)
  {
  case 1  : SayTTSC("first");
     break;
  case 2  : SayTTSC("second");
     break;
  case 3  : SayTTSC("third");
     break;
  case 4  : SayTTSC("forth");
     break;
  case 5  : SayTTSC("fifth");
     break;
  case 6  : SayTTSC("sixth");
     break;
  case 7  : SayTTSC("seventh");
     break;
  case 8  : SayTTSC("eighth");
     break;
  case 9  : SayTTSC("ninth");
     break;
  case 10 : SayTTSC("tenth");
     break;
  case 11 : SayTTSC("eleventh");
     break;
  case 12 : SayTTSC("twelfth");
     break;
  case 13 : SayTTSC("thirteenth");
     break;
  case 14 : SayTTSC("fourteenth");
     break;
  case 15 : SayTTSC("fifteenth");
     break;
  case 16 : SayTTSC("sixteenth");
     break;
  case 17 : SayTTSC("seventeenth");
     break;
  case 18 : SayTTSC("eighteenths");
     break;
  case 19 : SayTTSC("nineteenths");
     break;
  case 20 : SayTTSC("twentieth");
     break;
  case 21 : SayTTSC("twentyfirst");
     break;
  case 22 : SayTTSC("twentysecond");
     break;
  case 23 : SayTTSC("twentythird");
     break;
  case 24 : SayTTSC("twentyforth");
     break;
  case 25 : SayTTSC("twentyfifth");
     break;
  case 26 : SayTTSC("twentysixth");
     break;
  case 27 : SayTTSC("twentyseventh");
     break;
  case 28 : SayTTSC("twentyeighth");
     break;
  case 29 : SayTTSC("twentyninth");
     break;
  case 30 : SayTTSC("thirtieth");
     break;
  case 31 : SayTTSC("thirtyfirst");
     break;
  }
}

void SayMonth(int mon)
{
  switch (mon)
  {
  case 1  : SayTTSC("January");
     break;
  case 2  : SayTTSC("February");
     break;
  case 3  : SayTTSC("March");
     break;
  case 4  : SayTTSC("April");
     break;
  case 5  : SayTTSC("May");
     break;
  case 6  : SayTTSC("June");
     break;
  case 7  : SayTTSC("July");
     break;
  case 8  : SayTTSC("August");
     break;
  case 9  : SayTTSC("September");
     break;
  case 10 : SayTTSC("October");
     break;
  case 11 : SayTTSC("November");
     break;
  case 12 : SayTTSC("December");
     break;
  }
}

void SayDate(long my_time)
{

  char szDate[128];
  char szYear[5];
  int  day, month;

  FormatTime(szYear, sizeof(szYear), "%Y", my_time);
  FormatTime(szDate, sizeof(szDate), "%d", my_time);
  day = atoi(szDate);
  FormatTime(szDate, sizeof(szDate), "%m", my_time);
  month = atoi(szDate);
  SayDay(day);
  SayTTSC("of");
  SayMonth(month);
  SayNumber(szYear);
}

void SayTime (long my_time)
{
  char szTime[128];

  SayTTSC("at");
  FormatTime(szTime, sizeof(szTime), "%H", my_time);
  SayNumber(szTime);
  //SayTTSC("xxxxx");
  FormatTime(szTime, sizeof(szTime), "%M", my_time);
  SayNumber(szTime);
}

char *spell(char *szNumber)
{
  static char szRet[256];
  char *pi, *po;

  pi = szNumber;
  po = szRet;

  while (*pi)
  {
    *po++ = *pi++;
    *po++ = ' ';
  }

  *po = 0;

  return szRet;
}

void spell2(char *szNumber)
{
  char *pi;

  pi = szNumber;

  while (*pi)
  {
    switch(*pi++)
    {
    case '1': SayTTSC("one");
      break;
    case '2': SayTTSC("two");
      break;
    case '3': SayTTSC("three");
      break;
    case '4': SayTTSC("four");
      break;
    case '5': SayTTSC("five");
      break;
    case '6': SayTTSC("six");
      break;
    case '7': SayTTSC("seven");
      break;
    case '8': SayTTSC("eight");
      break;
    case '9': SayTTSC("nine");
      break;
    case '0': SayTTSC("zero");
      break;
    case '+': SayTTSC("plus");
      break;
    case ' ': /**/
      break;

    case 'V':
      if(*pi++ == 'o')
      {
        SayTTSC("Voice message from");
      }

      do
      {
        pi++;
        if(*pi++ == '+')
        {
          SayTTSC("plus");
        }
      } while(isalpha(*pi++));

      break;

    case 'F':
      if(*pi++ == 'a')
      {
        SayTTSC("Fax from");
      }

      do
      {
        pi++;
        if(*pi++ == '+')
        {
          SayTTSC("plus");
        }
      } while(isalpha(*pi++));

      break;

    case 'S':
      if(*pi++ == 'M')
      {
        SayTTSC("SMS from");
      }

      do
      {
        pi++;
        if(*pi++ == '+')
        {
          SayTTSC("plus");
        }
      } while(isalpha(*pi++));

      break;

    default:
      pi++;
      break;

    }
  }
}

/***********************************************
 ***********************************************
 ***********************************************/

/*
 * UserSelection()
 *
 * select a user by extension
 *
 */

BOOL UserSelection(USER *pUser)
{
  BOOL          bLoop;
  char          szNumber[32];
  EXTENSIONDATA data;

  for (;;)
  {
    SayTTSC("Please enter the extension of the user you want to select.");
    SayTTSC("Confirm with a hash sign");

    GetNumber(szNumber, sizeof(szNumber));
    GetExtensionData(szNumber, &data);
    if (data.bIsUser)
    {
      *pUser = data.User;

      for (bLoop = TRUE; bLoop;)
      {
        SayTTSC("You made the selection");
        sprintf(szMsg, "%s", pUser->szName);
        SayTTS(szMsg);
        SayTTSC("Press one to change the selection or two to confirm.");

        switch (GetEvent())
        {
        case '1':
          bLoop = FALSE;
          break;

        case '2':
          return TRUE;
          break;

        case '0':
          return FALSE;
          break;
        }
      }
    }
    else
    {
      for (bLoop = TRUE; bLoop;)
      {
        SayTTSC("I am sorry, the extension is not assigned.");
        SayTTSC("Press one to try again.");

        switch (GetEvent())
        {
        case '1':
          bLoop = FALSE;
          break;

        case '0':
          return FALSE;
          break;
        }
      }
    }
  }
  return FALSE;
}

/*
 * SayMsgPropeties()
 *
 * says the properties
 * of a document
 *
 */

void SayMsgProperties(DOCUMENT *pDoc)
{
  char szDate[128];

  if (pDoc->DocID)
  {

    switch(pDoc->Type)
    {
    case MSG_FAX:
      SayTTSC("The fax");
      break;

    case MSG_VOICE:
      SayTTSC("The voice-mail");
      break;

    case MSG_MAIL:
      SayTTSC("The e-mail");
      break;

    }
 //   sprintf(szMsg, "%ld", pDoc->DocID);
 //   spell2(szMsg);

    if (pDoc->Folder == FOLDER_RECEIVE)
    {
      SayTTSC("was received");
    } else if (pDoc->Folder == FOLDER_POOL || pDoc->Folder == FOLDER_PUBLIC)
    {
      SayTTSC("was created");
    }
    else
    {
      SayTTSC("was created");
    }

    SayDate(pDoc->CreationTime);
    SayTime(pDoc->CreationTime);

    if (strlen(pDoc->szSenderName))
    {
      SayTTSC("Was sent from");
      if (isalpha(pDoc->szSenderName[0]))
      {
        sprintf(szMsg, "%s", pDoc->szSenderName);
        SayTTS(szMsg);
      }
      else
      {
        spell2(pDoc->szSenderName);
      }
    }

    if ((pDoc->Type == MSG_MAIL) && strlen(pDoc->szComment))
    {
      SayTTSC("has the subject");
      sprintf(szMsg, "%s", pDoc->szComment);
      SayTTS(szMsg);
    }
  }
}

/*
 * GotoMsg()
 *
 * seek to a message by number
 *
 */

BOOL GotoMsg(DOCUMENT *pDoc)
{
  BOOL     bLoop;
  char     szNumber[32];
  DOCUMENT doc;

  for (;;)
  {
    SayTTSC("Please enter the number of the message");
    SayTTSC("Confirm with a hash sign");

    if (GetNumber(szNumber, sizeof(szNumber))    &&
      (SeekMsg(atoi(szNumber), &doc) == NOERR)    )
    {
      *pDoc = doc;
      return TRUE;
    }

    for (bLoop = TRUE; bLoop;)
    {
      SayTTSC("There is no message with that number");
      SayTTSC("Press one to try again.");

      switch (GetEvent())
      {
      case '1':
        bLoop = FALSE;
        break;

      case '0':
        return FALSE;
        break;
      }
    }
  }
  return FALSE;
}

/*
 * MakeAnnotation()
 *
 * record a annotation
 *
 */

BOOL MakeAnnotation()
{
  BOOL bLoopRecord;
  BOOL bLoopMenu;

  for (bLoopRecord = TRUE; bLoopRecord;)
  {
    SayTTSC("Please speak your annotation after the beep");

    RecordVoice();

    SayTTSC("Your annotation is recorded");

    for (bLoopMenu = TRUE; bLoopMenu;)
    {
      SayTTSC("Press one to hear your annotation");
      SayTTSC("Press two to record it again");
      SayTTSC("Press three to forward the message with your annotation");

      switch (GetEvent())
      {
      case '1':
        PlayRecordedVoice();
        break;

      case '2':
        bLoopMenu = FALSE;
        break;

      case '3':
        return TRUE;
        break;

      case '0':
        return FALSE;
        break;
      }
    }
  }
  return FALSE;
}

/*
 * MakeReply()
 *
 * record a reply
 *
 */

BOOL MakeReply()
{
  BOOL bLoopRecord;
  BOOL bLoopMenu;

  for (bLoopRecord = TRUE; bLoopRecord;)
  {
    SayTTSC("Leave your reply after the beep");

    RecordVoice();

    SayTTSC("Your message is recorded");

    for (bLoopMenu = TRUE; bLoopMenu;)
    {
      SayTTSC("Press one to hear it again");
      SayTTSC("Press two to record another");
      SayTTSC("Press three to send");

      switch (GetEvent(3000, FALSE))
      {
      case '1':
        PlayRecordedVoice();
        break;

      case '2':
        bLoopMenu = FALSE;
        break;

      case '3':
      case EVENT_HANGUP:
        return TRUE;
        break;

      case '0':
        return FALSE;
        break;
      }
    }
  }
  return FALSE;
}

/*
 * ReplyToMsg()
 *
 * manage message reply
 *
 */

void ReplyToMsg(DOCUMENT *pDoc)
{
  if (MakeReply())
  {
    SayTTSC("One moment, your message will be transmitted");
    if (ReplyMsgByRecordedVoice(pDoc) == 0)
    {
      SayTTSC("Your message is transmitted");
    }
  }
}

/* AdditionalLanguageMenu ()
 *
 * read the current Mail Message in an other Language
 */

void AdditionalLanguageMenu (DOCUMENT *cur) {

  BOOL bLoop;
  char c = '0';

  for (bLoop = TRUE;bLoop;) {
#ifdef CAN_de
    SayTTSC("Press one for German");
#endif
#ifdef CAN_us
    SayTTSC("Press two for English");
#endif
#ifdef CAN_it
    SayTTSC("Press three for Italian");
#endif
#ifdef CAN_fr
    SayTTSC("Press four for French");
#endif
#ifdef CAN_es
    SayTTSC("Press five for Spanish");
#endif

    c = GetEvent (5000);

    switch (c) {
#ifdef CAN_de
    case '1':
#endif
#ifdef CAN_us
    case '2':
#endif
#ifdef CAN_it
    case '3':
#endif
#ifdef CAN_fr
    case '4':
#endif
#ifdef CAN_es
    case '5':
#endif
      if (strlen(cur->szComment)) {
        SayTTSC("has the subject");
        sprintf(szMsg, "%s", cur->szComment);
        SetLanguage(c - '1');
        SayTTS(szMsg);
      } else {
        SetLanguage(c - '1');
      }
      PlayMailMsg(cur);
      SetLanguage(MY_LANGUAGE);
      bLoop = FALSE;
      break;
      //case '1','2' ...
    } // switch (c)
  } // for bLoop
} // AdditionalLanguageMenu


/*
 * MailMsgMenu()
 *
 * manages mail messages
 *
 */

void MailMsgMenu(int Folder, int bMarked)
{
  BOOL     bLoop;
  DOCUMENT cur, doc;
  long     lLast = -1;
  char     c = EVENT_TIMEOUT;

  SetFolderSelection(Folder, MSG_MAIL, bMarked);

  if (GetFirstMsg(&cur) == ERR_NOTFOUND)
  {
    SayTTSC("There are no messages of the selected type");
    return;
  }

  for (bLoop = TRUE; bLoop;)
  {
    if (cur.DocID != lLast)
    {
      SayMsgProperties(&cur);
      PlayMailMsg(&cur);
      lLast = cur.DocID;
    }

    if (c == EVENT_TIMEOUT)
    {
      SayTTSC("Press one to hear the message again");
      if (bMarked)
      {
        SayTTSC("press two to delete or three to mark it as read");
      }
      else
      {
        SayTTSC("press two to delete");
      }

      if (nr_of_additional_languages) {
        if ((nr_of_additional_languages == 1) && (additional_language == 1)) {
          SayTTSC("press four to read it in German");
        } else if (nr_of_additional_languages) {
          SayTTSC("press four to read it in a different language");
        }
      }

      if (cur.CanReply)
      {
        SayTTSC("press five to reply");
      }

      SayTTSC("press the hash sign to proceed to the next message");
      SayTTSC("with the star you can go to the previous");
      SayTTSC("or press eight to enter a message number");
    }

    c = GetEvent(5000);

    switch (c)
    {
    case '1':
      PlayMailMsg(&cur);
      break;

    case '2':
      DeleteMsg(&cur);
      SayTTSC("The message is deleted");
      if (GetNextMsg(&doc))
      {
        if (GetPreviousMsg(&doc))
        {
          SayTTSC("All messages of the selected type are deleted");
          bLoop = FALSE;
        }
        else
        {
          cur = doc;
        }
      }
      else
      {
        cur = doc;
      }
      break;

    case '3':
      if (bMarked)
      {
        MarkMsg(&cur, FALSE);
        if (GetNextMsg(&doc))
        {
          if (GetPreviousMsg(&doc))
          {
            SayTTSC("All messages of the selected type are marked as read");
            bLoop = FALSE;
          }
          else
          {
            cur = doc;
          }
        }
        else
        {
          cur = doc;
        }
      }
      break;

    case '4':
      if (additional_language != 1) {
        AdditionalLanguageMenu(&cur);
        break;
      } // Additional Language Menu
      if (strlen(cur.szComment)) {
        SayTTSC("has the subject");
        sprintf(szMsg, "%s", cur.szComment);
        SetLanguage(1);
        SayTTS(szMsg);
      } else {
        SetLanguage(1);
      }
      PlayMailMsg(&cur);
      SetLanguage(MY_LANGUAGE);
      break;

    case '5':
      if (cur.CanReply)
      {
        ReplyToMsg(&cur);
      }
      break;

    case '8':
      GotoMsg(&cur);
      break;

    case '*':
      if (GetPreviousMsg(&doc) == NOERR)
      {
        cur = doc;
      }
      else
      {
        SayTTSC("There is no previous message");
      }
      break;

    case '#':
      if (GetNextMsg(&doc) == NOERR)
      {
        cur = doc;
      }
      else
      {
        SayTTSC("There is no next message");
      }
      break;

    case '0':
      bLoop = FALSE;
      break;

    default:
      c = EVENT_TIMEOUT;
      break;
    }
  }
}

/*
 * ForwardVoiceMsg()
 *
 * forwards a voice message, optional with annotation
 *
 */

void ForwardVoiceMsg(DOCUMENT *pDoc)
{
  BOOL bLoop;
  USER user;

  if (UserSelection(&user))
  {
    for (bLoop = TRUE; bLoop;)
    {
      SayTTSC("Press one to forward the message");
      SayTTSC("Press two to forward with an annotation");

      switch (GetEvent())
      {
      case '1':
        if (ForwardMsg(pDoc, &user, FOLDER_RECEIVE, FALSE) == NOERR)
        {
          SayTTSC("The message is forwarded");
          bLoop = FALSE;
        }
        break;

      case '2':
        if (MakeAnnotation())
        {
          if (ForwardMsg(pDoc, &user, FOLDER_RECEIVE, TRUE) == NOERR)
          {
            SayTTSC("The message is forwarded");
          }
          bLoop = FALSE;
        }
        DeleteRecordedVoice();
        break;

      case '0':
        bLoop = FALSE;
        break;
      }
    }
  }
}

/*
 * VoiceMsgMenu()
 *
 * manages voice messages
 *
 */

void VoiceMsgMenu(int Folder, int bMarked)
{
  BOOL     bLoop;
  DOCUMENT cur, doc;
  long     lLast = -1;
  char     c = EVENT_TIMEOUT;

  SetFolderSelection(Folder, MSG_VOICE, bMarked);

  if (GetFirstMsg(&cur) == ERR_NOTFOUND)
  {
    SayTTSC("There are no messages of the selected type");
    return;
  }

  for (bLoop = TRUE; bLoop;)
  {
    if (cur.DocID != lLast)
    {
      SayMsgProperties(&cur);
      PlayVoiceMsg(&cur);
      lLast = cur.DocID;
    }

    if (c == EVENT_TIMEOUT)
    {
      SayTTSC("Press one to hear the message again, press two to delete it");
      if (bMarked)
      {
        SayTTSC("press three to forward or four to mark as read");
      }
      else
      {
        SayTTSC("press three to forward");
      }

      if (cur.CanReply)
      {
        SayTTSC("press five to reply");
      }

      SayTTSC("press the hash sign to proceed to the next message");
      SayTTSC("with the star you can go to the previous");
      SayTTSC("or press eight to enter a message number");
    }

    c = GetEvent(5000);

    switch (c)
    {
    case '1':
      PlayVoiceMsg(&cur);
      break;

    case '2':
      DeleteMsg(&cur);
      SayTTSC("The message is deleted");
      if (GetNextMsg(&doc))
      {
        if (GetPreviousMsg(&doc))
        {
          TDataLight(0);
          SayTTSC("All messages of the selected type are deleted");
          bLoop = FALSE;
        }
        else
        {
          cur = doc;
        }
      }
      else
      {
        cur = doc;
      }
      break;

    case '3':
      ForwardVoiceMsg(&cur);
      if (GetNextMsg(&doc) == NOERR)
      {
        cur = doc;
      }
      else
      {
        SayTTSC("There are no further messages");
      }
      break;

    case '4':
      if (bMarked)
      {
        MarkMsg(&cur, FALSE);
        if (GetNextMsg(&doc))
        {
          if (GetPreviousMsg(&doc))
          {
            TDataLight(0);
            SayTTSC("All messages of the selected type are marked as read");
            bLoop = FALSE;
          }
          else
          {
            cur = doc;
          }
        }
        else
        {
          cur = doc;
        }
      }
      break;

    case '5':
      if (cur.CanReply)
      {
        ReplyToMsg(&cur);
      }
      break;

    case '8':
      GotoMsg(&cur);
      break;

    case '*':
      if (GetPreviousMsg(&doc) == NOERR)
      {
        cur = doc;
      }
      else
      {
        SayTTSC("There are no previous messages");
      }
      break;

    case '#':
      if (GetNextMsg(&doc) == NOERR)
      {
        cur = doc;
      }
      else
      {
        SayTTSC("There are no further messages");
      }
      break;

    case '0':
      bLoop = FALSE;
      break;

    default:
      c = EVENT_TIMEOUT;
      break;
    }
  }
}

/*
 * ForwardFaxMsg()
 *
 * forwards a fax message
 *
 */

void ForwardFaxMsg(DOCUMENT *pDoc)
{
  USER user;

  if (UserSelection(&user))
  {
    if (ForwardMsg(pDoc, &user, FOLDER_RECEIVE, FALSE) == NOERR)
    {
      SayTTSC("The message is forwarded");
    }
  }
}

/*
 * SendFaxMsg()
 *
 * creates a new sendjob
 *
 */

void SendFaxMsg(DOCUMENT *pDoc)
{
  BOOL bLoop;
  char szNumber[21];
  char     c = EVENT_TIMEOUT;

  for (;;)
  {
    SayTTSC("Please enter the faxnumber. Terminate with a hash sign");

    GetNumber(szNumber, sizeof(szNumber));

    for (bLoop = TRUE; bLoop;)
    {
      SayTTSC("You entered the number");
      spell2(szNumber);

      SayTTSC("Press one to enter the number again, press two to send the fax");

      switch (GetEvent())
      {
      case '1':
        bLoop = FALSE;
        break;

      case '2':
        if (SendFax(pDoc, szNumber) == NOERR)
        {
          SayTTSC("The message will be transmitted");
        }
        return;

      case '0':
        return;

      default:
        c = EVENT_TIMEOUT;
        break;
      }
    }
  }
}

/*
 * FaxMsgMenu()
 *
 * manages fax messages
 *
 */

void FaxMsgMenu(int Folder, int bMarked)
{
  BOOL     bLoop;
  DOCUMENT cur, doc;
  long     lLast = -1;
  char     c = EVENT_TIMEOUT;

  SetFolderSelection(Folder, MSG_FAX, bMarked);

  if (GetFirstMsg(&cur) == ERR_NOTFOUND)
  {
    SayTTSC("There are no messages of the selected type");
    return;
  }

  for (bLoop = TRUE; bLoop;)
  {
    if (cur.DocID != lLast)
    {
      SayMsgProperties(&cur);
      lLast = cur.DocID;
    }

    if (c == EVENT_TIMEOUT)
    {
      SayTTSC("Press one to send, press two to delete, press three to forward");
      if (bMarked)
      {
        SayTTSC("Press four to send, or five to mark as read");
      }
      else
      {
        SayTTSC("or press four to send");
      }
      SayTTSC("press the hash sign to proceed to the next message");
      SayTTSC("with the star you can go to the previous");
      SayTTSC("press eight to enter a message number");
    }

    c = GetEvent(5000);

    switch (c)
    {
    case '1':
      TransmitFax(&cur, FALSE);
      if (GetNextMsg(&doc) == NOERR)
      {
        cur = doc;
      }
      else
      {
        SayTTSC("There are no further messages");
      }
      break;

    case '2':
      DeleteMsg(&cur);
      SayTTSC("The message is deleted");
      if (GetNextMsg(&doc))
      {
        if (GetPreviousMsg(&doc))
        {
          SayTTSC("All messages of the selected type are deleted");
          bLoop = FALSE;
        }
        else
        {
          cur = doc;
        }
      }
      else
      {
        cur = doc;
      }
      break;

    case '3':
      ForwardFaxMsg(&cur);
      if (GetNextMsg(&doc) == NOERR)
      {
        cur = doc;
      }
      else
      {
        SayTTSC("There are no further messages");
      }
      break;

    case '4':
      SendFaxMsg(&cur);
      if (GetNextMsg(&doc) == NOERR)
      {
        cur = doc;
      }
      else
      {
        SayTTSC("There are no further messages");
      }
      break;

    case '5':
      if (bMarked)
      {
        MarkMsg(&cur, FALSE);
        if (GetNextMsg(&doc))
        {
          if (GetPreviousMsg(&doc))
          {
            SayTTSC("All messages of the selected type are marked as read");
            bLoop = FALSE;
          }
          else
          {
            cur = doc;
          }
        }
        else
        {
          cur = doc;
        }
      }
      break;

    case '8':
      GotoMsg(&cur);
      break;

    case '*':
      if (GetPreviousMsg(&doc) == NOERR)
      {
        cur = doc;
      }
      else
      {
        SayTTSC("There are no previous messages");
      }
      break;

    case '#':
      if (GetNextMsg(&doc) == NOERR)
      {
        cur = doc;
      }
      else
      {
        SayTTSC("There are no further messages");
      }
      break;

    case '0':
      bLoop = FALSE;
      break;

    }
  }
}

/*
 * MessageMenu()
 *
 * manages the messages
 *
 */

void NewMessageMenu()
{
  NEWMESSAGESTAT st;
  BOOL           bLoop;

  GetNewMessageStat(&st);

  if (st.nFax == 0 && st.nVoice == 0 && st.nMail == 0)
  {
    SayTTSC("There are no new messages");
    return;
  }

  if (ExtensionData.bIMAP)
  {
    for (bLoop = TRUE; bLoop;)
    {
      SayTTSC("Press one for new voicemails, press two for new faxes, press three for new e-mails");
      switch (GetEvent())
      {
      case '1':
        VoiceMsgMenu(FOLDER_RECEIVE, TRUE);
        break;

      case '2':
        FaxMsgMenu(FOLDER_RECEIVE, TRUE);
        break;

      case '3':
        MailMsgMenu(FOLDER_RECEIVE, TRUE);
        break;

      case '0':
        bLoop = FALSE;
        break;
      }
    }
  }
  else
  {
    for (bLoop = TRUE; bLoop;)
    {
      SayTTSC("Press one for new voicemails, press two for new faxes");
      switch (GetEvent())
      {
      case '1':
        VoiceMsgMenu(FOLDER_RECEIVE, TRUE);
        break;

      case '2':
        FaxMsgMenu(FOLDER_RECEIVE, TRUE);
        break;

      case '0':
        bLoop = FALSE;
        break;
      }
    }
  }
}

/*
 * FolderMsgMenu()
 *
 * select the XXMsgMenu() by type.
 *
 */

void FolderMsgMenu(int Folder, int Type)
{
  if (Type == MSG_VOICE)
  {
    VoiceMsgMenu(Folder, FALSE);
  }
  else if (Type == MSG_FAX)
  {
    FaxMsgMenu(Folder, FALSE);
  }
  else
  {
    MailMsgMenu(Folder, FALSE);
  }
}

/*
 * FolderMenu()
 *
 * select the folder.
 *
 */

void FolderMenu(int Type)
{
  BOOL bLoop;

  if (ExtensionData.bIMAP)
  {
    // This is the only option.

    FolderMsgMenu(FOLDER_RECEIVE, Type);
    return;
  }

  for (bLoop = TRUE; bLoop;)
  {
    SayTTSC("Press one for received messages, press two for the desktop");
    SayTTSC("Press three for sent messages, press four for public documents");

    switch (GetEvent())
    {
    case '1':
      FolderMsgMenu(FOLDER_RECEIVE, Type);
      break;

    case '2':
      FolderMsgMenu(FOLDER_POOL, Type);
      break;

    case '3':
      FolderMsgMenu(FOLDER_SEND, Type);
      break;

    case '4':
      FolderMsgMenu(FOLDER_PUBLIC, Type);
      break;

    case '0':
      bLoop = FALSE;
      break;
    }
  }
}

/*
 * AllMessageMenu()
 *
 * manage all messages.
 *
 */

void AllMessageMenu()
{
  BOOL bLoop;

  for (bLoop = TRUE; bLoop;)
  {
    if (ExtensionData.bIMAP)
    {
      SayTTSC("Press one for voicemails, press two for faxes, press three for e-mails");
      switch (GetEvent())
      {
      case '1':
        FolderMenu(MSG_VOICE);
        break;

      case '2':
        FolderMenu(MSG_FAX);
        break;

      case '3':
        FolderMenu(MSG_MAIL);
        break;

      case '0':
        bLoop = FALSE;
        break;
      }
    }
    else
    {
      SayTTSC("Press one for voicemails, press two for faxes");
      switch (GetEvent())
      {
      case '1':
        FolderMenu(MSG_VOICE);
        break;

      case '2':
        FolderMenu(MSG_FAX);
        break;

      case '0':
        bLoop = FALSE;
        break;
      }
    }
  }
}

/*
 * MessageMenu()
 *
 * manage the users messages
 *
 */

void MessageMenu()
{
  BOOL bLoop;

  for (bLoop = TRUE; bLoop;)
  {
    SayTTSC("Press one for new messages, press two for all documents");
    switch (GetEvent())
    {
    case '1':
      NewMessageMenu();
      break;

    case '2':
      AllMessageMenu();
      break;

    case '0':
      bLoop = FALSE;
      break;
    }
  }
}

/*
 * NewGoodBye()
 *
 * record a new personal good bye.
 *
 */

void NewGoodBye()
{
  BOOL bLoop;

  for (;;)
  {
    SayTTSC("Please speak your new good by after the beep");

    RecordVoice();

    SayTTSC("Your good by is recorded");

    for (bLoop = TRUE; bLoop;)
    {
      SayTTSC("Press one to hear it");
      SayTTSC("Press two to record it again, press three to store");

      switch (GetEvent())
      {
      case '1':
        PlayRecordedVoice();
        break;

      case '2':
        bLoop = FALSE;
        break;

      case '3':
        if (StoreRecordedVoiceAsGoodBye() == NOERR)
        {
          ExtensionData.bHasGoodBye = TRUE;
          SayTTSC("Your good by is stored");
        }
        DeleteRecordedVoice();
        return;

      case '0':
        DeleteRecordedVoice();
        return;
      }
    }
  }
}

/*
 * GoodByeMenu()
 *
 * Management of the personal good bye.
 *
 */

void GoodByeMenu()
{
  BOOL bLoop;

  for (bLoop = TRUE; bLoop;)
  {
    SayTTSC("Press one to hear your good by, press two to delete it");
    SayTTSC("Press three to record a new good by message");
    switch (GetEvent())
    {
    case '1':
      if (ExtensionData.bHasGoodBye)
      {
        PlayVoiceGoodBye();
      }
      else
      {
        SayTTSC("You have not recorded a good by message");
      }
      break;

    case '2':
      if (DeleteVoiceGoodBye() == NOERR)
      {
        ExtensionData.bHasGoodBye = FALSE;
        SayTTSC("Your good by message is deleted");
      }
      break;

    case '3':
      NewGoodBye();
      break;

    case '0':
      bLoop = FALSE;
      break;
    }
  }
}

/*
 * NewVideoGoodBye()
 *
 * record a new personal good bye.
 *
 */

void NewVideoGoodBye()
{
  BOOL bLoop;

  for (;;)
  {
    SayTTSC("Please speak your new video good by after the beep");

    RecordVideo();

    SayTTSC("Your video good bye is recorded");

    for (bLoop = TRUE; bLoop;)
    {
      SayTTSC("Press one to play your video good bye");
      SayTTSC("Press two to record it again");
      SayTTSC("or press three to store your new video good by");

      switch (GetEvent())
      {
      case '1':
        PlayRecordedVideo();
        break;

      case '2':
        bLoop = FALSE;
        break;

      case '3':
        if (StoreRecordedVideoAsGoodBye() == NOERR)
        {
          ExtensionData.bHasVideoGoodBye = TRUE;
          SayTTSC("Your video good bye is recorded");
        }
        DeleteRecordedVideo();
        return;

      case '0':
        DeleteRecordedVideo();
        return;
      }
    }
  }
}

/*
 * GoodByeVideoMenu()
 *
 * Management of the personal good bye.
 *
 */

void GoodByeVideoMenu()
{
  BOOL bLoop;

  for (bLoop = TRUE; bLoop;)
  {
    SayTTSC("Press one to play your video good bye");
    SayTTSC("Press two to delete it, press three to record a new one");
    switch (GetEvent())
    {
    case '1':
      if (ExtensionData.bHasVideoGoodBye)
      {
        PlayVideoGoodBye();
      }
      else
      {
        SayTTSC("You have not recorded a video good by");
      }
      break;

    case '2':
      if (DeleteVideoGoodBye() == NOERR)
      {
        ExtensionData.bHasVideoGoodBye = FALSE;
        SayTTSC("Your video good by is deleted");
      }
      break;

    case '3':
      NewVideoGoodBye();
      break;

    case '0':
      bLoop = FALSE;
      break;
    }
  }
}

/*
 * NewGreeting()
 *
 * record a new personal greeting.
 *
 */

void NewGreeting()
{
  BOOL bLoop;

  for (;;)
  {
    SayTTSC("Please enter your greeting after the beep");

    RecordVoice();

    SayTTSC("Your greeting is stored");

    for (bLoop = TRUE; bLoop;)
    {
      SayTTSC("Press one to hear your greeting");
      SayTTSC("Press two to record it again");
      SayTTSC("Press three to store it");

      switch (GetEvent())
      {
      case '1':
        PlayRecordedVoice();
        break;

      case '2':
        bLoop = FALSE;
        break;

      case '3':
        if (StoreRecordedVoiceAsGreeting() == NOERR)
        {
          ExtensionData.bHasGreeting = TRUE;
          SayTTSC("Your greeting is stored");
        }
        DeleteRecordedVoice();
        return;

      case '0':
        DeleteRecordedVoice();
        return;
      }
    }
  }
}

/*
 * GreetingMenu()
 *
 * Management of the personal greeting.
 *
 */

void GreetingMenu()
{
  BOOL bLoop;

  for (bLoop = TRUE; bLoop;)
  {
    SayTTSC("Press one to hear your greeting");
    SayTTSC("Press two to delete it, press three to record a new one");

    switch (GetEvent())
    {
    case '1':
      if (ExtensionData.bHasGreeting)
      {
        PlayVoiceGreeting();
      }
      else
      {
        SayTTSC("You have not recorded a greeting message");
      }
      break;

    case '2':
      if (DeleteVoiceGreeting() == NOERR)
      {
        ExtensionData.bHasGreeting = FALSE;
        SayTTSC("Your greeting is deleted");
      }
      break;

    case '3':
      NewGreeting();
      break;

    case '0':
      bLoop = FALSE;
      break;
    }
  }
}

/*
 * NewVideoGreeting()
 *
 * record a new personal greeting.
 *
 */

void NewVideoGreeting()
{
  BOOL bLoop;

  for (;;)
  {
    SayTTSC("Please speak your new video greeting after the beep");

    RecordVideo();

    SayTTSC("Your video greeting is stored");

    for (bLoop = TRUE; bLoop;)
    {
      SayTTSC("Press one to play your video greeting");
      SayTTSC("Press two to record it again");
      SayTTSC("or press three to store your video greeting");

      switch (GetEvent())
      {
      case '1':
        PlayRecordedVideo();
        break;

      case '2':
        bLoop = FALSE;
        break;

      case '3':
        if (StoreRecordedVideoAsGreeting() == NOERR)
        {
          ExtensionData.bHasVideoGreeting = TRUE;
          SayTTSC("Your video greeting is stored");
        }
        DeleteRecordedVideo();
        return;

      case '0':
        DeleteRecordedVideo();
        return;
      }
    }
  }
}

/*
 * GreetingVideoMenu()
 *
 * Management of the personal video greeting.
 *
 */

void GreetingVideoMenu()
{
  BOOL bLoop;

  if(!bVideo)
  {
    SayTTSC("Sorry! You are not calling from a video terminal. I can not record a video message.");
    return;
  }

  for (bLoop = TRUE; bLoop;)
  {
    SayTTSC("Press one to play your video greeting");
    SayTTSC("Press two to delete it, press three to record a new one");

    switch (GetEvent())
    {
    case '1':
      if (HasGreeting())
      {
        PlayGreeting();
      }
      else
      {
        SayTTSC("You have not recorded a video greeting");
      }
      break;

    case '2':
      if (DeleteVideoGreeting() == NOERR)
      {
        ExtensionData.bHasVideoGreeting = FALSE;
        SayTTSC("Your video greeting is deleted");
      }
      break;

    case '3':
      NewVideoGreeting();
      break;

    case '0':
      bLoop = FALSE;
      break;
    }
  }
}

/*
 *
 * AnnounementsMenu()
 *
 * selection of the announcements to manage
 *
 */
void AnnouncementsMenu()
{
  BOOL bLoop;

  for (bLoop = TRUE; bLoop;)
  {
    SayTTSC("Press one to manage your greeting message");
    SayTTSC("Press two to manage your good bye message");
    if (bVideo)
    {
      SayTTSC("Press three to manage your video greeting");
      SayTTSC("Press four to manage your video good bye");
    }

    switch (GetEvent())
    {
    case '1':
      GreetingMenu();
      break;

    case '2':
      GoodByeMenu();
      break;

    case '3':
      if(bVideo) GreetingVideoMenu();
      break;

    case '4':
      if(bVideo) GoodByeVideoMenu();
      break;

    case '0':
      bLoop = FALSE;
      break;
    }
  }
}

/*
 * SaySubstitute()
 *
 * tells who is the substitute
 *
 */

void SaySubstitute()
{
  USER user;
  int  bKeepCopy;

  GetSubstitute(&user, &bKeepCopy);
  if (user.szID[0])
  {
    SayTTSC("The name of your substitute is");
    sprintf(szMsg, "%s", user.szName);
    SayTTS(szMsg);
    if (bKeepCopy)
    {
      SayTTSC("You get a copy of all incoming messages");
    }
  }
  else
  {
    SayTTSC("You have not defined a substitute");
  }
}

/*
 * SubstituteSelection()
 *
 * select a new substitute
 *
 */

void SubstituteSelection()
{
  BOOL bLoop;
  USER user;

  if (UserSelection(&user))
  {
    for (bLoop = TRUE;bLoop;)
    {
      SayTTSC("Press one to select a substitute, no message copies for you");
      SayTTSC("Press two to select a substitute and get copies from incoming messages");

      switch (GetEvent())
      {
      case '1':
        if (SetSubstitute(&user, FALSE) == NOERR)
        {
          SayTTSC("You have selected a substitute. You will not get copies.");
        }
        bLoop = FALSE;
        break;

      case '2':
        if (SetSubstitute(&user, TRUE) == NOERR)
        {
          SayTTSC("You have selected a substitute. You will get copies of new messages.");
        }
        bLoop = FALSE;
        break;

      case '0':
        bLoop = FALSE;
        break;
      }
    }
  }
}

/*
 * SubstituteMenu()
 *
 * manages the substitute
 *
 */

void SubstituteMenu()
{
  BOOL bLoop;

  SaySubstitute();

  for (bLoop = TRUE; bLoop;)
  {
    SayTTSC("Press one to check your substitute selection");
    SayTTSC("Press two to delete the selection");
    SayTTSC("Press three to select a substitute");

    switch (GetEvent())
    {
    case '1':
      SaySubstitute();
      break;

    case '2':
      if (DeleteSubstitute() == NOERR)
      {
        SayTTSC("The selection of your substitute is deleted");
      }
      break;

    case '3':
      SubstituteSelection();
      break;

    case '0':
      bLoop = FALSE;
      break;
    }
  }
}

/*
 * LanguageMenu()
 *
 * Selection of the default Language for the menu
 *
 */

void LanguageMenu()
{
  BOOL bLoop;
  char c;

  SayTTSC("Select the dialog language");

  for (bLoop = TRUE; bLoop;)
  {
    SayTTSC("your options are");

    SetLanguage(0);
    SayTTSC("Press one for German");

    SetLanguage(1);
    SayTTSC("Press two for English");

    SetLanguage(2);
    SayTTSC("Press three for Italian");

    SetLanguage(3);
    SayTTSC("Press four for French");

    SetLanguage(4);
    SayTTSC("Press five for Spanish");


    c = GetEvent(3000);

    switch (c)
    {
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
      SetLanguage(c - '1');
      bLoop = FALSE;
      break;

    case '0':
      bLoop = FALSE;
      break;
    }
  }
}

// ChangePincodeMenu

void ChangePincodeMenu() {

  char OldPincode[21];
  char NewPincode1[21];
  char NewPincode2[21];
  int  minlength;
  char pinlenmin[81];
  BOOL passt;

  if (GetSystemProperty("PINLENMIN",pinlenmin))
  {
    TraceS ("Error reading PINLENMIN");
    minlength=0;
  }
  else
  {
    minlength=atoi(pinlenmin);
  }

  SayTTSC("Please enter your old pincode");
  SayTTSC("Confirm with a hash sign");

  if (!GetNumber(OldPincode, sizeof(OldPincode)))
  {
    return;
  }

  do
  {
    do
    {
      SayTTSC("Please enter your new pincode");
      SayTTSC("Confirm with a hash sign");

      if (!GetNumber(NewPincode1, sizeof(NewPincode1)))
      {
        return;
      }

      if (strlen(NewPincode1) < minlength)
      {
        char sztmp[32];

        SayTTSC("Your pincode is too short");
        SayTTSC("The minimum length is");
        sprintf(sztmp, "%d", minlength);
        SayNumber(sztmp);
        passt = FALSE;
      }
      else
      {
        passt = TRUE;
      }

    } while (!passt);

    SayTTSC("Please repeat your new pincode");
    SayTTSC("Confirm with a hash sign");

    if (!GetNumber(NewPincode2, sizeof(NewPincode2)))
    {
      return;
    }

    if (strcmp(NewPincode1, NewPincode2))
    {
      SayTTSC("Sorry! the pincodes differ");
      passt = FALSE;
    }
    else
    {
      passt = TRUE;
    }
  } while (!passt);

  if (!ChangePincode(OldPincode, NewPincode1))
  {
    SayTTSC("The pincode is modified");
  }
  else
  {
    SayTTSC("Error modifying the pincode");
  }

} // ChangePincodeMenu

/*
 * AdministrationMenu()
 *
 * Selection of administrative functions,
 * e.g. the greeting.
 *
 */

void AdministrationMenu()
{
  BOOL bLoop;

  if (ExtensionData.bIMAP)
  {

    // This is the only option.

    AnnouncementsMenu();
    return;
  }

  for (bLoop = TRUE; bLoop;)
  {
    SayTTSC("Press one to manage your announcements");
    SayTTSC("Press two to manage your substitute selection");
    SayTTSC("Press three to change your pincode");

    switch (GetEvent())
    {
    case '1':
      AnnouncementsMenu();
      break;

    case '2':
      SubstituteMenu();
      break;

    case '3':
      ChangePincodeMenu();
      break;

    case '0':
      bLoop = FALSE;
      break;
    }
  }
}

/*
 * NewMessageOverview()
 *
 * gives a short overview of new messages
 *
 */

void NewMessageOverview()
{
  NEWMESSAGESTAT st;
  BOOL           dok_exists = FALSE;

  GetNewMessageStat(&st);

  if (!st.nFax && !st.nVoice && !st.nMail)
  {
    SayTTSC("There are no new messages");
  }
  else
  {
    SayTTSC("You have got");
    if (st.nFax == 1)
    {
      SayTTSC("a new fax");
      dok_exists = TRUE;
    }
    if (st.nFax > 1)
    {
      if(st.nFax < 11)
      {
        sprintf(szMsg, "%d", st.nFax);
        SayNumber(szMsg);
      }
      else
      {
        SayTTSC("more then ten");
      }
      SayTTSC("new faxes");
      dok_exists = TRUE;
    }
    if ((st.nVoice > 0) && (dok_exists)) {SayTTSC("and");}
    if (st.nVoice == 1)
    {
      SayTTSC("a new voicemail");
      dok_exists = TRUE;
    }
    if (st.nVoice > 1)
    {
      if(st.nVoice < 11)
      {
        sprintf(szMsg, "%d", st.nVoice);
        SayNumber(szMsg);
      }
      else
      {
        SayTTSC("more then ten");
      }
      SayTTSC("new voicemails");
      dok_exists = TRUE;
    }
    if ((st.nMail > 0) && (dok_exists)) {SayTTSC("and");}
    if (st.nMail == 1)
    {
      SayTTSC("a new e-mail");
    }
    if (st.nMail > 1)
    {
      if(st.nMail < 11)
      {
        sprintf(szMsg, "%d", st.nMail);
        SayNumber(szMsg);
      }
      else
      {
        SayTTSC("more then ten");
      }
      SayTTSC("new e-mails");
    }
  }
}

/*
 * UserMenu()
 *
 * First get pincode, then offer the user menu.
 *
 */

int ValidatePincode()
{
  char szPincode[21];

  TraceS("Validate Pincode"); /* stabilizes cint, don't remove */
  if (!GetNumber(szPincode, sizeof(szPincode)))
  {
    return FALSE;
  }
  return CheckPincode(szPincode) == NOERR ? TRUE : FALSE;
}

void UserMenu()
{
  int  nRetries;
  BOOL bLoop;
  char szPincode[128];

  if (ExtensionData.bIMAPError)
  {
    SayTTSC("Sorry! We have problems in the network or faulty system settings");
    SayTTSC("This service is temporary not available");
    return;
  }

  nRetries = 0;

  for (bLoop = TRUE; bLoop;)
  {
    SayTTSC("Please enter your PIN code. Confirm with a hash sign.");

    if (!ValidatePincode())
    {
      if (++nRetries == 3)
      {
        SayTTSC("Sorry! Too many attempts.");
        exit(0);
      }
      else
      {
        SayTTSC("Invalid PIN code");
      }
    }
    else
    {
      SayTTSC("Hello");
      sprintf(szMsg, "%s", ExtensionData.User.szName);
      SayTTS(szMsg);
      bLoop = FALSE;
    }
  }

  NewMessageOverview();

  for (bLoop = TRUE; bLoop;)
  {
    SayTTSC("Press one for the message menue, press two for the administrations menue");

    switch (GetEvent())
    {
    case '1':
      MessageMenu();
      break;

    case '2':
      AdministrationMenu();
      break;

    case '0':
      SayTTSC("Good bye");
      bLoop = FALSE;
      break;
    }
  }
}


void InitMyLanguages () {
  nr_of_additional_languages=0;
  #ifdef CAN_de
  nr_of_additional_languages++;
  additional_language=0;
  #endif // CAN_de
  #ifdef CAN_us
  nr_of_additional_languages++;
  additional_language=1;
  #endif // CAN_us
  #ifdef CAN_it
  nr_of_additional_languages++;
  additional_language=2;
  #endif // CAN_it
  #ifdef CAN_fr
  nr_of_additional_languages++;
  additional_language=3;
  #endif // CAN_fr
  #ifdef CAN_es
  nr_of_additional_languages++;
  additional_language=4;
  #endif // CAN_es
  if (nr_of_additional_languages != 1) {
    additional_language=-1;
  }
} // InitMyLanguages


/**********
 * Main() *
 **********/

void Main()
{
  BOOL bLoop;

  bVideo = FALSE;
  bSMS   = FALSE;
  bFax   = FALSE;

  GetConnectionData(&ConnectionData);
  GetExtensionData(ConnectionData.szDialedExtension, &ExtensionData, EXT_IMAP);
  ApplyExtensionData(&ExtensionData);
  
  if (CanFax())
    TraceS("CanFax");
  if (CanVoice())
    TraceS("CanVoice");
  if (CanSMS())
    TraceS("CanSMS");
   
  switch(GetEvent(1))
  {
  case EVENT_VIDEO:
    bVideo = TRUE;
    break;

  case EVENT_SMS:
    bSMS = TRUE;
    break;
   
  }

  if (bSMS)
  {
    if (CanSMS() && (ExtensionData.bIsUser || ExtensionData.bIsGroup || ExtensionData.bAcceptAll)) {
        AcceptCall();
        ReceiveSMS();
    }
    exit(0);
  }

  InitLanguages();
  InitMyLanguages ();
  SetLanguage (MY_LANGUAGE);

  AcceptCall();  

  DoSleep(1000); // Pause fuer H.323 - Telefone

  if (ExtensionData.bIMAPError)
  {
    SayTTSC("L D A P or I M A P error.");
  }

/*********
 * video *
 *********/
  if(bVideo)
  {
    char szExtension[20];

    SayTTSC("Please enter the extension of the party"); // new
    SayTTSC("Confirm with a hash sign");
    while(!GetNumber(szExtension, sizeof(szExtension)))
    {
      SayTTSC("Please enter the extension of the party"); // new
      SayTTSC("Confirm with a hash sign");
    }
    SayNumber(szExtension);
    strcpy(ExtensionData.szExtension,szExtension);
    GetExtensionData(szExtension, &ExtensionData);
    ApplyExtensionData(&ExtensionData);
  }
/*********
 *********/

  if (!HasGreeting())
  {
    SayTTSC("Hello");
    SayTTSC("You have dialed the extension");

    if (atoi(ExtensionData.szExtension)<60)
    {
      sprintf(szMsg, "%s", ExtensionData.szExtension);
      SayNumber(szMsg);
    }
    else
    {
      spell2(ExtensionData.szExtension);
    }

    //SayTTSC("xxxxx");
  }

  if (ExtensionData.bIsUser)
  {
    if (HasGreeting())
    {
      PlayGreeting();
    }
    else
    {
      SayTTSC("You are connected with Papageno the electronic messaging assistant of");
      sprintf(szMsg,"%s", ExtensionData.User.szName);
      SayTTS(szMsg);

      if (ExtensionData.bIsGroup)
      {
        SayTTSC("the message you leave will also be sent to the team");
        sprintf(szMsg,"%s", ExtensionData.Group.szName);
        SayTTS(szMsg);
        //SayTTSC("xxxxx");
      }
      SayTTSC("Please leave your message after the beep");
    }

  }
  else if (ExtensionData.bIsGroup)
  {
    SayTTSC("You are connected with Papageno the electronic messaging assistant of the team");
    sprintf(szMsg, "%s", ExtensionData.Group.szName);
    SayTTS(szMsg);
    SayTTSC("Please leave your message after the beep");
  }
  else if (ExtensionData.bIsPolling)
  {
    SayTTSC("We switch to the polling mode");
    ReceiveFax();
    exit(0);
  }
  else
  {
    if (ExtensionData.bAcceptAll) {
      SayTTSC("You are connected with Papageno the electronic messaging assistant of");
      sprintf(szMsg,"%s", ExtensionData.szAcceptAll);
      SayTTS(szMsg);
      SayTTSC("Please leave your message after the beep");
    }
    else {
        SayTTSC("The number you dialed is not assigned");
        SayTTSC("Good bye");
        exit(0);
    }
  }

  for (bLoop = TRUE; bLoop;)
  {
    switch (GetEvent(1000)) // assume fax tone comes within this time.
    {
    case '1':
    case EVENT_TIMEOUT:
      if (bVideo)
      {
        ReceiveVideo();
      }
      else
      {
        ReceiveVoice();
        TDataLight(1);
      }
      bLoop = FALSE;
      break;

    case '2':
    case EVENT_FAX:
      if (CanFax()) {
          ReceiveFax();
      } 
      else { 
          // Einfach auflegen fuehrt zu 'besetzt'
          DoSleep(5000); // Pause fuer Gegenstelle -> HW3311
      }
      exit(0);
      break;

    case '#':
      if (ExtensionData.bIsUser)
      {
        UserMenu();
        exit(0);
      }
      break;
    }
  }

  if (HasGoodBye())
  {
    PlayGoodBye();
  }
  else
  {
    SayTTSC("Good by and thank you for calling");
  }
}
