/* High Top Mountain Cottages Calendar Script
   Version 3.1 ~ With Laurel Cottage and Frames
   Original JavaScript (c) January 2003 by 
   John Zachary Cannon
   High Top Mountain Cottages
   PO Box 216
   Dillsboro, NC  28725  USA
   (828) 586-3383

   http://www.mountaindog.net/
   jzcndd@earthlink.net                           

This version of the script has been tested and works with
Netscape 4.06, MSIE 3.02 with JScript.dll versions 1 & 3
and MSIE 5.0 with JScript.dll version 5.  */

var bodyline = '<body bgcolor="#f8f8ff" text="#000000" link="#008080" vlink="#609090"\n'
             + '      background="pp/back.jpg">\n';
var cutoffhr = 15 + HTMTZO;    // 3 pm.
var cutoffday = 2;
var picfolder = "cp/";
var SharedAt = "/";
var Stylesheet = "pp/htmstyle.css";

if (typeof(D) == "undefined")
{
   D = new makeArray ();       // in case date information not given in availability.js
   Updated = 'unknown';
}

var pics = new Array ("000", "001", "010", "011",
                      "100", "101", "110", "111", 
                      "1000", "1001", "1010", "1011");

var alts = new Array ("All three cottages are available.",
                      "Azalea and Dogwood are available.",
                      "Azalea and Laurel are available.",
                      "Azalea is available.",
                      "Dogwood and Laurel are available.",
                      "Dogwood is available.",
                      "Laurel is available.",
                      "Sorry, no vacancies!",
                      "This date has passed...",
                      "Today!  Sorry, we need more advance notice...",
                      "Sorry, we need more advance notice...",
                      "Sorry, this date is too far in advance...");
var daywidth = 24;
var picwidth = 24;
var picheight = 25;       // all pics must be this size.

var backpic = '<img src="cp/back.gif" border="0" width="15" height="15" alt="back one month">';
var fwdpic = '<img src="cp/next.gif" border="0" width="15" height="15" alt="ahead one month">';
var legendpic = '<img src="cp/legend3.gif" border="0" width="190" height="80" alt="legend">';
var legendplus = '<img src="cp/legend3a.gif" border="0" width="190" height="26" alt="legend">';
var todaypic = "1001";
var pastpic = "1000";
var noticepic = "1010";
var futurepic = "1011";
var vacancy = "0";
var novacancy = "1";
var notavail = "1";       // no difference with novacancy, for now.

var moNames = new makeArray("January","February","March","April","May","June","July","August","September","October","November","December");
var moDayct = new makeArray (31,28,31,30,31,30,31,31,30,31,30,31);

var today, thisyear, thismonth, thisday, thisGMThr, thisdom, 
    laureldom, lastdom, pagetop, emptypage, data, followtext, daysoweek;


function mouseOO (str)
{
   var s = 'onMouseOver="status=\'' + str + '\'; return true" '
   + 'onMouseOut="status=\'\'; return true"';
   return s;
}


function getAvail (Year, Month)
{
//   document.write (Year + '  ' + Month);
   var moMatch = moNames[Month];
   moMatch = moMatch.substring (0, 3);
   var yrMatch = "!" + Year;            // assure string 
   yrMatch = yrMatch.substring (3);     // two digits as string
   var mA = 'A' + moMatch + yrMatch;
   var mD = 'D' + moMatch + yrMatch;
   var mL = 'L' + moMatch + yrMatch;
   var blank = '                               ';
   var AA = '', DD = '', LL = '';
   for (i=0; i < D.length; i++) {
       var p = D[i];
       var q = p.substring (0, 6);
       if (q == mA) AA = p.substring (6);
       if (q == mD) DD = p.substring (6);
       if (q == mL) LL = p.substring (6);
   }
   AA += blank;
   DD += blank;
   LL += blank;
   AA = AA.substring (0, moDayct[Month]);
   DD = DD.substring (0, moDayct[Month]);
   LL = LL.substring (0, moDayct[Month]);
   var out = new makeArray (AA, DD, LL);
   return out;
}


function isLeap (Year)
{
   if (((Year % 4 == 0) && (Year % 100 != 0)) || (Year % 400 == 0)) return 1;
   else return 0;
}


function setLeap (Year)
{
   moDayct[1] = 28 + isLeap (Year); 
}


function dom (year, month, day)
{
   // day of the millennium...

   var ans = 0;
   for (var y=2000; y<year; y++) {
       ans += 365 + isLeap(y);
   }
   setLeap (year);
   for (var m=0; m<month; m++) {
       ans += moDayct[m];
   }
   ans += day;
   return ans;
}


function DayCell (indx, day, backcolor)
{
   var adom = data[indx].domoff + day;
   var pic = '', alt = '';

   if (adom < thisdom) pic = pastpic;
   else if (adom == thisdom) pic = todaypic;
   else if ((adom - thisdom) < cutoffday) pic = noticepic;
   else if ((adom - thisdom) == cutoffday) {
        if (thisGMThr >= cutoffhr) {
            pic = noticepic;
        } else {
            alt = ' if reserved before 3 pm ET today.'
        }
   } else if (adom > lastdom) {
      pic = futurepic;
   }

   if (pic == '') {
      var dogkey = vacancy, azakey = vacancy, laukey = vacancy, q = day-1;

      var d = data[indx].dogwood.charAt(q);
      if (d == 'x') dogkey = notavail;
      else if (d != ' ') dogkey = novacancy;

      var a = data[indx].azalea.charAt(q);
      if (a == 'x') azakey = notavail;
      else if (a != ' ') azakey = novacancy;

      var l = data[indx].laurel.charAt(q);
      if (l == 'x') laukey = notavail;
      else if (l != ' ') laukey = novacancy;
      if (adom < laureldom) laukey = notavail;

      pic = azakey + dogkey + laukey;
   }

   var i = parseInt (pic,2);
   if (alt != '' && i <= 6) {
      var piece = alts[i];
      var l = piece.length - 1;
      alt = piece.substring (0,l) + alt;
   } else alt = alts[i];

   var s = '<td width="' + daywidth + '" bgcolor="' + backcolor + '"><b>' + day + '</b><br>'
         + '<img src="' + picfolder + pic + '.gif" border="0" '
         + 'width="' + picwidth + '" height="' + picheight 
         + '" alt="' + alt + '"></td>\n';
   return s;
}

function moAnch (indx)
{
   var s = 'href="javascript:parent.showMonth(' + (indx) + ')" ';
   var t = 'View availability for ' + data[indx].monthname + ', ' + data[indx].year + '.';
   s += mouseOO (t) + '>';
   return s;
}


function monthTable (indx)
{

   var column = 0;
   var i, vpic, alti, alt;
   var s = '<table cellspacing="1" cellpadding="0" border="1">\n<tr>\n'
       + '<td align="center" width="' + daywidth + '">\n';
   if (indx <= 1) s += '&nbsp;';
   else s += '<a ' + moAnch(indx-1) + backpic + '</a>';
   s += '</td>\n<th colspan="5">' + data[indx].monthname + ' '
       + data[indx].year + '</th>\n'
       + '<td align="center" width="' + daywidth + '">\n';
   if (indx >= 14) s += '&nbsp;';
   else s += '<a ' + moAnch(indx+1) + fwdpic + '</a>';
   s += '</td>\n</tr>\n' 
       + daysoweek
       + '<tr align="right">\n';

   for (i=data[indx].pmstart; i<=data[indx].pmend; i++)
   {
     s += DayCell (indx-1, i, '#CCCCCC');
     column++;
     if (column == 7) {s += '\n</tr><tr align="right">\n'; column = 0;}
   }
   for (i=1; i<=data[indx].dayct; i++)
   {
     s += DayCell (indx, i, '#FFFFFF');
     column++;
     if (column == 7) {s += '\n</tr><tr align="right">\n'; column = 0;}
   }
   for (i=data[indx].nmstart; i<=data[indx].nmend; i++)
   {
     s += DayCell (indx+1, i, '#CCCCCC');
     column++;
     if (column == 7) {s += '\n</tr><tr align="right">\n'; column = 0;}
   }
   s += '\n</tr></table>\n';
   return s;
}


function monthPage (indx)
{

   var s = pagetop
     + '<table width="760">\n<tr>\n<td width="180" align="center" valign="top">\n'
     + '<P class="titler">'
     + 'Click on the month<BR>you are interested in...</P>';

   for (var i=1; i<=14; i++)
   {
     if (i == indx) s += '<a class="mocurr" ';
     else s += '<a class="monext" ';
     s += moAnch (i) + data[i].monthname + ' ' + data[i].year + '</a><br>\n';
   }

   s += '</td>\n<td width="360" align="center" valign="top">\n'
     + monthTable (indx)
     + '</td>\n<td width="220" align="center" valign="top">\n'
     + followtext + '\n'
     + '<P class="little">' + legendpic;
   if (data[indx].firstdom <= thisdom) s += '<br>' + legendplus;
   s += '<br>Place your mouse over any color key in the calendar for details.</P>';

   s += '\n</td>\n</tr>\n</table>\n'
     + '</center>\n</body>\n</html>\n';
   return s;
}



function showMonth (indx)
{
   var s = monthPage (indx);
   MAIN.document.open ();
   MAIN.document.write (s);
   MAIN.document.close ();
}


function MonthData (Y, M)
{
   setLeap (Y);
   var pm = M - 1;
   var py = Y;
   if (pm < 0) {py--; pm = 11}      // pm, py are previous month and year
   var nm = M + 1;
   var ny = Y;
   if (nm >= 12) {ny++; nm = 0}     // nm, ny are next month and year

   this.month = M;
   this.year = Y;
   this.monthname = moNames[M];

   var firstDay = new Date (Y, M, 1);
   this.dayct = moDayct[M];
   var lastDay = new Date (Y, M, this.dayct);
   this.startday = firstDay.getDay();
   this.endday = lastDay.getDay();

   var showpreviousmo = this.startday;   // 7 + this.startday;
   this.pmend = moDayct[pm];
   this.pmstart = this.pmend - showpreviousmo + 1;
   var shownextmo = 6 - this.endday;    // 7 + (6 - this.endday);
   this.nmstart = 1;
   this.nmend = this.nmstart + shownextmo - 1;
   this.domoff = dom (Y, M, 1) - 1;
   this.firstdom = this.domoff - showpreviousmo + 1;

   var avail = getAvail (Y, M);
   this.azalea = avail[0];
   this.dogwood = avail[1];
   this.laurel = avail[2];

   return this;
}


function DataArray ()
{
   var M = thismonth - 1;             // start one month before current month.
   var Y = thisyear;
   if (M < 0) {Y --; M = 11}          // adjust start year if necessary.
   for (var i=0; i<=15; i++) {
       this[i] = new MonthData (Y, M);
       M++;
       if (M==12) {Y++; M=0;}
   }
   this.length = i;
   return this;
}


function calsetup()
{

   var nb = new cbar();
   today = new Date ();
   thisyear = today.getYear();
   if (thisyear < 1900) thisyear += 1900;
   thismonth = today.getMonth();
   thisday = today.getDate();
   thisGMThr = (today.getTimezoneOffset() + today.getMinutes())/60 + today.getHours();
   thisdom = dom (thisyear, thismonth, thisday);

   var fy = thisyear + 1;              // calculate last date for which reservations will
   var fm = thismonth + 1;             // be taken: 13 months ahead and through end of
   if (fm >= 12) {fy += 1, fm = 0}     // that week.
   var fd = thisday;
   if (fd > moDayct[fm]) fd = moDayct[fm];
   var fdat = new Date (fy, fm, fd);   // do this to get day of week of future date
   lastdom = dom (fy, fm, fd) + (6 - fdat.getDay());  // last day reservations taken for.

   laureldom = dom (2004, 3, 1);                     // laurel available April 1 2004.

   pagetop = '<html>\n<head>\n<title>High Top Mountain Cottages - Availability</title>\n'
    + '<base href="' + top.location.href + '">\n';
   if (Stylesheet != '') {
      pagetop += '<link rel="stylesheet" href="' + Stylesheet + '" type="text/css">\n';
   }
   pagetop += '</head>\n' + bodyline + '<center>\n'
    + nb.bar + '<hr>\n';

   emptypage = '<html><head><title></title></head>\n' + bodyline + '</body></html>\n';

   followtext = '<P class="little">Calendar was last updated ' + Updated + '.</P>\n'
      + '<P class="little">'
      + 'If you made a reservation prior to the last update, and the '
      + 'calendar does not indicate it, please '
      + '<A HREF="mail'
      + 'to:mtndog@ver'
      + 'izon.net?subj'
      + 'ect=Reservations" '
      + mouseOO ('Email us with reservation questions!') + '><B>email us</B></A>!</P>\n'
      + '<P class="little">We cannot take reservations more than '
      + '13 months in advance.</P>';

   daysoweek = '<tr><td align="center"><font size=1>Sun</font></td>'
      + '<td align="center"><font size=1>Mon</font></td>'
      + '<td align="center"><font size=1>Tue</font></td>'
      + '<td align="center"><font size=1>Wed</font></td>'
      + '<td align="center"><font size=1>Thu</font></td>'
      + '<td align="center"><font size=1>Fri</font></td>'
      + '<td align="center"><font size=1>Sat</font></td></tr>\n';

   data = new DataArray();

}


function writeframes()
{

   var f = '</head>\n'
    + '<frameset cols="1,*" border="0" frameborder="0" framespacing="0">\n'
    + '<frame name="DUMB" src="javascript:parent.emptypage" scrolling="no" noresize border="0" frameborder="0" marginwidth="0" marginheight="0"></frame>\n'
    + '<frame name="MAIN" src="javascript:parent.monthPage(1)" scrolling="auto" noresize border="0" frameborder="0" marginwidth="4" marginheight="4"></frame>\n'
    + '<noframes>\n'
    + bodyline
    + '<center>\n';

   var nb = new parent.cbar();
   f += nb.bar + '<hr>\n'
    + '<p>Sorry! To view the availability calendar, your browser must support frames...</p>\n'
    + '<p>Please select a different page.</p>\n'
    + '<img src="' + SharedAt + 'probframes.gif" border="0" width="1" height="1" alt="">\n'
    + '</center>\n</body>\n</noframes>\n'
    + '</frameset>\n';

   document.write (f);
}

