/*
3-month calendar script- Ada Shimar (adashimar@chalktv.com)
Script featured on and available at:
http://www.javascriptkit.com/
*/

var check_available = true;
var showing_rates = false;
var old_rate_idx = -1;

//Specify bgcolor of calendar
var bg="#FFFFE1"

// Specify bgcolor of reserved days
var reserved_color = "red";

//Specify font size
var fs=2

var flg = 0;
//var fs = 1;
M = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
D = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");

var pdy = new Date();        // today
var pmo = pdy.getMonth();    // present month
var pyr = pdy.getYear();     // present year
if (pyr < 2000)    // Y2K Fix, Isaac Powell
  pyr = pyr + 1900; // http://onyx.idbsu.edu/~ipowell

function getBgn(month, year) 
{
  if (month != null)
    pmo = month;

  if (year != null)
    pyr = year;

  if (pyr < 2000)    // Y2K Fix, Isaac Powell
    pyr = pyr + 1900; // http://onyx.idbsu.edu/~ipowell

  yr = (pmo==0 ? pyr-1 : pyr); // last month's year
  mo = (pmo==0 ? 11 : pmo-1);  // last month

  bgn = new Date(M[mo]+" 1," + yr); // assign to date

  var html = '<TABLE align=center BORDER=0 cellspacing=5><TR><TD VALIGN=TOP>';
  html += Calendar(-1);           // Send last month to screen
  html += '</TD><TD VALIGN=TOP>';
  yr = pyr;                // present year
  mo = pmo;                // present month
  bgn = new Date(M[mo]+" 1,"+yr); // assign to date
  html += Calendar(0);           // Send this month to screen
  html += '</TD><TD VALIGN=TOP>';
  yr = (pmo==11?pyr+1:pyr); // next month's year
  mo = (pmo==11?0:pmo+1);   // next month
  bgn = new Date(M[mo]+" 1,"+yr); // assign to date
  html += Calendar(1);           // Send next month to screen
  html += '</TD></TR>';

  if (check_available)
  {
    html += "<TR><TD COLSPAN=2></TD><TD align=right><FONT SIZE=" + fs + "><SPAN style='border: 1px solid black; background-color:" + reserved_color + ";'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN> Unavailable</FONT></TD></TR>";
  }
  html += '</TABLE>'; // Finish up
  document.getElementById("calendar_div").innerHTML = html;
}

function Calendar(num)
{
  dy = bgn.getDay();
  yr = eval(yr);
  d = "312831303130313130313031";
  if (yr / 4 == Math.floor(yr / 4)) 
  {
    d = d.substring(0, 2) + "29" + d.substring(4, d.length);
  }
  pos = (mo * 2);
  ld = d.substring(pos, pos + 2);
  var html =     "<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=1 BGCOLOR='" + bg + "'>" +
                 "  <TR>" +
                 "    <TD>";
  if (num == -1)
    html +=      "     <a href='javascript: cal_month(-1)'>&lt;&lt;</a>";
  html +=        "    </TD>" +
                 "    <TD ALIGN=CENTER COLSPAN=5>" +
                 "      <FONT SIZE=" + fs + "><B>" + M[mo] + " " + yr + "</B></FONT>" +
                 "    </TD>" +
                 "    <TD>";
  if (num == 1)
    html +=      "     <a href='javascript: cal_month(1)'>&gt;&gt;</a>";
  html +=        "    </TD>" +
                 "  </TR>" +
                 "  <TR>" +
                 "  <TR>";
  for (var i = 0; i < 7; i++) 
  {
    html +=        "  <TD width='27px' ALIGN=CENTER>" +
                   "    <FONT SIZE=" + fs + ">" + D[i] + "</FONT>" + 
                   "  </TD>";
  }
  html +=        "  </TR>" + 
                 "  <TR>";
  ctr = 0;
  for (var i = 0; i < 7; i++)
  {
    if (i < dy) 
    {
      html +=        "<TD ALIGN=CENTER style='border-bottom: 1px solid black;'>" +
                     "  <FONT SIZE=" + fs + ">&nbsp;</FONT>" +
                     "</TD>";
    }
    else 
    {
      ctr++;
      html +=        "<TD ALIGN=CENTER style='border-left: 1px solid black; border-top: 1px solid black; border-bottom: 1px solid black;'";
      if (i == 6)
        html += "style='border-right: 1px solid black;'";
      if (check_available && is_date_reserved(new Date((mo+1) + "/" + ctr + "/" + yr)))
        html += " BGCOLOR=" + reserved_color;
      var d = (mo+1).toString() + "/" + ctr.toString() + "/" + yr.toString();
      html += " onMouseOver='if (showing_rates) show_rate(\"" + d + "\")'";
      html +=        ">  <FONT SIZE=" + fs + ">" + ctr + "</FONT>" +
                     "</TD>";
    }
  }
  html +=        "  </TR>";
  while (ctr < ld) 
  {
    html +=      "  <TR>";
    for (var i = 0; i < 7; i++)
    {
      ctr++;
      if (ctr - 1 == ld && i > 0)
      {
        html +=        "<TD ALIGN=CENTER style='border-left: 1px solid black;'><FONT SIZE=" + fs + ">&nbsp;</FONT></TD>";
      }
      else if (ctr > ld)
      {
        html +=        "<TD ALIGN=CENTER><FONT SIZE=" + fs + ">&nbsp;</FONT></TD>";
      }
      else
      {
        html +=        "<TD ALIGN=CENTER style='border-left: 1px solid black; border-bottom: 1px solid black;'";
        if (i == 6)
          html += "style='border-right: 1px solid black;'";
        if (check_available && is_date_reserved(new Date((mo+1) + "/" + ctr + "/" + yr)))
          html += " BGCOLOR=" + reserved_color;
        var d = (mo+1).toString() + "/" + ctr.toString() + "/" + yr.toString();
        html += " onMouseOver='if (showing_rates) show_rate(\"" + d + "\")'";
        html +=        ">  <FONT SIZE=" + fs + ">" + ctr + "</FONT>" +
                       "</TD>";
      }
    }
    html +=        "</TR>";
  }
  html += "</TABLE>";
  return html;
}

function cal_month(dir)
{
  var new_mo = pmo + dir;
  var new_yr = pyr;
  if (new_mo == -1)
  {
    new_mo = 11;
    new_yr--;
  }
  if (new_mo == 12)
  {
    new_mo = 0;
    new_yr++;
  }
  getBgn(new_mo, new_yr);
}

function create_calendar_here(show_rates)
{
  document.write("<div id='calendar_div'></div>");
  showing_rates = show_rates;
  if (showing_rates)
  {
    document.write("<div id='rates_div'>");
    document.write("<table width='50%' align='center'>");
    document.write("  <tr>");
    document.write("    <td colspan='3' class='tdbluebld' align='center'>Weekly Rental Rates</td>");
    document.write("  </tr>");
    document.write("  <tr>");
    document.write("    <td class='tdbluebld' align='center'>Start Date</td>");
    document.write("    <td class='tdbluebld' align='center'>End Date</td>");
    document.write("    <td class='tdbluebld' align='center'>Price($)</td>");
    document.write("  </tr>");

    var c = 'tdblue';
    for (var i = 0; i < rates.length; i++)
    {
      if (c == 'tdblue')
        c = 'tdgry';
      else
        c = 'tdblue';
      document.write("  <tr>");
      document.write("    <td id='rates" + i + "0' class='" + c + "' align='center'>" + my_to_date(rates[i].start_date) + "</td>");
      document.write("    <td id='rates" + i + "1' class='" + c + "' align='center'>" + my_to_date(rates[i].end_date) + "</td>");
      document.write("    <td id='rates" + i + "2' class='" + c + "' align='center'>$" + rates[i].price + "</td>");
      document.write("  </tr>");
    }
    document.write("</table>");
    document.write("</div>");
  }
}

function show_rate(d)
{
  var idx = get_rate_idx(d);
  set_rate_row_color(old_rate_idx, "black", "0px solid black");
  set_rate_row_color(idx, "blue", "1px solid blue");
  old_rate_idx = idx;
}

function set_rate_row_color(idx, color, border)
{
  if (idx == -1)
    return;
  for (var i = 0; i < 3; i++)
  {
    var rowcol = document.getElementById("rates" + idx.toString() + i.toString());
    rowcol.style.color = color;
    rowcol.style.border = border;
  }
}

function my_to_date(d)
{
  return two_digits(d.getMonth()+1) + "/" + two_digits(d.getDate()) + "/" + d.getFullYear();
}

function two_digits(n)
{
  if (n < 10)
    return "0" + n.toString();
  else
    return n.toString();
}
