HTML file generated by SIMPOL Editor sbiscalendar.sma
// sbiscalendar.sma
//
// This program contains the calendar table sample using SIMPOL similarly to the sample provided
// with the SBL-based Superbase Internet Server product.
//
// © 2001-2002 Superbase Developers plc
// Author: Neil Robinson
// Date: 05.Nov.2001
// Modified: 09.May.2002, Neil Robinson
//   Updated to not be part of one giant program but as part of a suite of compiled programs
// Modified: 05.Sep.2003, Marco Gagliardi
//   Changed to use an included top and bottom component for the HTML header and footer.
// Modified: 22.Sep.2003, Neil Robinson
//   Updated the variables, program documentation and design (to allow for calling from
//   (CGI, ISAPI, and FastCGI).
//
// Modules Required:
// =================
// sbislib.sml, SBLDateLib.sml
////////////////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////
//          Constant Declarations         //
////////////////////////////////////////////

constant CRLF                "{0D}{0A}"
constant sQUERY_STRING       "QUERY_STRING"
constant sSSCALENDARTABLE    "sbiscalendar.smp"

constant iROWCOUNT           6
constant iDAYCOUNT           7
constant iSUNDAY             7
constant iBASE10             10


////////////////////////////////////////////
//              Main Program              //
////////////////////////////////////////////

function main(cgicall cgi)
  string sReturnval

  fcgiinit()
  sReturnval = fcgi(cgi)
  fcgiterm()
end function sReturnval


////////////////////////////////////////////////////////////////////
// fcgiinit()
//
// Parameters:
// ===========
// None
//
// Return value:
// =============
// If desired, a single object reference can be returned from this
// function.
//
// Description:
// ============
// This function is called the first time that the program is loaded
// by the FastCGI loader. It should be used to do essential
// initialization of the program, such as opening database files.
// A user-defined type should be created that carries a reference
// to each of the resources initialized. An object of that type
// should be the return value. If initialization fails, then return
// .nul and test for .nul in the fcgi() function.
////////////////////////////////////////////////////////////////////
//
function fcgiinit()
end function


////////////////////////////////////////////////////////////////////
// fcgi()
//
// Parameters:
// ===========
// cgicall cgi - This is the cgicall object representing the call
// type(*) x   - (optional) This is an optional parameter to be used
//               if required by the application. This parameter is
//               the return value of the fcgiinit() function.
//
// Return value:
// =============
// string - This can be the entire page. If the page is output in the
//          body of the function then this one should return "".
//
// Description:
// ============
// This function is called each time that the program is called. It
// is the main body of the program and can make use of the initial-
// ization of the program that was done in fcgiinit(). If an error
// occurred during initialization then the return value of the
// fcgiinit() call should have been .nul, which can be tested for
// at the start of this function. There is no way to cause the
// fcgiterm() call to be made, this is done by the web server when
// it's configuration determines the FastCGI program should be shut
// down.
////////////////////////////////////////////////////////////////////
//
function fcgi(cgicall cgi)
  string sBGColor, sTargetDate
  date dt, dtTmp, dtToday
  integer iTmp, i, j
  integer iYear, iMonth, iDay
  SBLlocaledateinfo ldiLocale

  ldiLocale =@ SBLlocaledateinfo.new()
  sTargetDate = cgi.getvariable(sQUERY_STRING)

  iTmp = 0
  i = 0
  j = 0

  // Initialize and set today's date for later use
  dtToday =@ date.new()
  dtToday.setnow()

  cgi.output("Content-type:  text/HTML{d}{a}{d}{a}", 1)
  cgi.output("<html><head><title>CGI Calendar Example</title>" + CRLF, 1)

  ///////////////////////////////////////
  //         External HTML File        //
  //    Include the header and css     //
  ///////////////////////////////////////
  HTML_Include(cgi, "header.htm")

  //////////////////////////////////////
  //          Program title           //
  //////////////////////////////////////
  cgi.output( CRLF, 1)
  cgi.output('<tr><td><center><h3 class="titledblue">Table Sample</h3></center></td>'+ CRLF, 1)
  cgi.output('</tr>'+ CRLF, 1)  

  /////////////////////////////////
  //      Center the table       //
  /////////////////////////////////
  cgi.output('<tr><td align="center"><span class="textitalic">The following is a sample of building a calendar using a table in SIMPOL.</span>'+ CRLF, 1)
  
  /////////////////////////////////////////////////
  //  Evaluate the parameter sTargetDate and if  //
  //  it is empty, assign the date from today    //
  //  to dt. Then reset dt to hold the first of  //
  //  the month.                                 //
  /////////////////////////////////////////////////
  if sTargetDate > ""
    i = .toval(sTargetDate, .nul, iBASE10)

    dt =@ date.new(i)
  else
    dt =@ date.new()
    dt = dtToday
  end if

  iYear = dt.year()
  iMonth = dt.month(.false)
  iDay = 1
  dt.set(year=iYear, month=iMonth, dayinmonth=iDay)

  ///////////////////////////////////////////////
  //  Generate the calendar header and create  //
  //  the table for the calendar elements.     //
  ///////////////////////////////////////////////
  cgi.output('<p class="drkGreyMedium">Calendar for ' + MONTHSTR(dt, ldiLocale) + " " + .tostr(dt.year(), iBASE10) + '</p>' + CRLF, 1)
  
  cgi.output('<table border=1 bgcolor=#C0C0C0 width="433">' + CRLF, 1)
  cgi.output('<TR>' + CRLF, 1)
  cgi.output('<TD WIDTH=50 ALIGN=center><B>Sun</TD>' + CRLF, 1)
  cgi.output('<TD WIDTH=50 ALIGN=center><B>Mon</TD>' + CRLF, 1)
  cgi.output('<TD WIDTH=50 ALIGN=center><B>Tue</TD>' + CRLF, 1)
  cgi.output('<TD WIDTH=50 ALIGN=center><B>Wed</TD>' + CRLF, 1)
  cgi.output('<TD WIDTH=50 ALIGN=center><B>Thu</TD>' + CRLF, 1)
  cgi.output('<TD WIDTH=50 ALIGN=center><B>Fri</TD>' + CRLF, 1)
  cgi.output('<TD WIDTH=50 ALIGN=center><B>Sat</TD>' + CRLF, 1)
  cgi.output('</TR><TR></TR><TR></TR>' + CRLF, 1)

  //////////////////////////////////////////
  //  Now find the date that starts on a  //
  //  Sunday                              //
  //////////////////////////////////////////
  dtTmp =@ date.new(dt)
  iTmp = dtTmp.dayinweek()
  if iTmp != iSUNDAY
    dtTmp =@ date.new(dtTmp - iTmp)
  end if

  /////////////////////////////////////////////
  //          Generate the calendar          //
  /////////////////////////////////////////////
  i = 1
  while i <= iROWCOUNT
    cgi.output('<tr>' + CRLF, 1)
    j = 1
    while j <= iDAYCOUNT
      sBGColor = ""
      if dtToday.month() == dtTmp.month() and dtToday.dayinmonth() == dtTmp.dayinmonth()
        sBGColor = " bgcolor=#00F0F0"
      else if dt.month() == dtTmp.month()
        sBGColor = " bgcolor=#FFFFFF"
      else
        sBGColor = " bgcolor=#C0C0C0"
      end if
      cgi.output('<td width=50 align=center ' + sBGColor + ">" + .tostr(dtTmp.dayinmonth(), iBASE10) + '</td>' + CRLF, 1)
      dtTmp =@ date.new(dtTmp + 1)
      j = j + 1
    end while 
    cgi.output('</tr>' + CRLF, 1)
    i = i + 1
  end while 

  /////////////////////////////////////////////
  //            Close the table              //
  /////////////////////////////////////////////
  cgi.output('</table><br>' + CRLF, 1)

  //////////////////////////////////////////////
  // Create the buttons for going forward and //
  //           and back one month.            //
  //////////////////////////////////////////////
  cgi.output('<table><tr><td height="40" valign="top">' + CRLF, 1)
  cgi.output('<form method="post" action="' + HTML_Page(sSSCALENDARTABLE) + "?" + .tostr((dt - 1), iBASE10) + '">' + CRLF, 1)
  cgi.output('<input type="submit" value="Prev" name="submit"></form></td>' + CRLF, 1)
  cgi.output('<td height="40"> <form method="post" action="' + HTML_Page(sSSCALENDARTABLE) + "?" + .tostr(dtTmp, 10) + '">' + CRLF, 1)
  cgi.output('<input type="submit" value="Next" name="submit2"></form></td></tr></table>' + CRLF, 1)
  cgi.output('</td></tr>', 1)

  ///////////////////////////////////////
  //    Include external HTML file     //
  //           as the footer           //
  /////////////////////////////////////// 
  HTML_Include(cgi, "footer.htm")
end function ""


////////////////////////////////////////////////////////////////////
// fcgiterm()
//
// Parameters:
// ===========
// type(*) x
//
// Return value:
// =============
// None.
//
// Description:
// ============
// This function is called to clean up the program when it is being
// terminated. Normally there isn't much that needs to be done in a
// SIMPOL program, since once the program closes the resources are
// automatically freed. Unless it is absolutely necessary, do not
// set various bits of the user-defined type to .nul when this
// function is called, since some threads may still be executing and
// to do this would be like ripping the carpet out from under them.
////////////////////////////////////////////////////////////////////
//
function fcgiterm(type(*) x)
end function