/*
    Copyright 2005 Rolando Gonzalez (rolosworld@gmail.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

// Constants
var months = [ "January",
	       "February",
	       "March",
	       "April",
	       "May",
	       "June",
	       "July",
	       "August",
	       "September",
	       "October",
	       "November",
	       "December" ];

var wdays = [ "Sun",
	      "Mon",
	      "Tue",
	      "Wed",
	      "Thu",
	      "Fri",
	      "Sat" ];

// returns the last day of the month
function getLastDay(date)
{
  var start = new Date(date);
  start.setMonth(start.getMonth()+1);
  start.setDate(0);
  return start.getDate();
}

// compare month, date and year from 2 dates
function sameDates(date1, date2)
{
  if(date1.getMonth() == date2.getMonth() &&
     date1.getDate() == date2.getDate() &&
     date1.getFullYear() == date2.getFullYear())
    return true;
  return false;
}

// Calendar Object
function Calendar(fromYear, toYear)
{
  var me = this;
  this.win = null;

  this.from_year = fromYear;
  this.to_year = toYear;

  this.selected_date = new Date();
  this.bold_dates = new Array();
  this.browse_date = new Date();

  this.select_month = document.createElement("select");
  this.select_month.className = "cmy";
  this.select_month.id = "month_list";
  this.select_year = document.createElement("select");
  this.select_year.className = "cmy";  
  this.select_year.id = "year_list";

  this.tbody = document.createElement("table");
  this.tbody.className = "caltbody";
  this.tbody.cellSpacing = 0;
  this.tbody.cellPadding = 0;
  this.thead = document.createElement("table");
  this.thead.className = "calthead";
  this.thead.cellSpacing = 0;
  this.thead.cellPadding = 0;

  // callback called when day's are clicked,
  // called as function(date)
  this.callback = null;

  this.select_month.onchange = function()
  {
    me.browse_date.setMonth(me.select_month.value);
    me.body();
  };

  this.select_year.onchange = function()
  {
    me.browse_date.setFullYear(me.select_year.value);
    me.body();
  };
  
  this.setBoldDays = function(myDays)
  {
    me.bold_dates = myDays;
  }

  this.newDay = function(d) {
      var c = "cd";
      var td = document.createElement("td");
      var date = new Date(me.browse_date);
      date.setDate(d);

      var cnt = 0;
      chkDate = new Date();
      for (cnt = 0; cnt < me.bold_dates.length; cnt++) {
          chkDate = new Date(Date.parse(me.bold_dates[cnt]));
          if (sameDates(date, chkDate))
              c = "bolded";
      }

      if (c == "bolded") {
          if (sameDates(date, me.selected_date))
              c = "csd";
      }

      td.className = c;
      td.day = d;

      if (c == "bolded") {
          td.onclick = function() 
          {
              me.selected_date = new Date(date);
              me.body();
              me.callback(date);
          }
      }

      td.onmouseover = function() {
          if ((c != "bolded") && (c != "csd")) {
              td.className = "cod";
          }
          else {
              td.className = "bolded2";
          }
      }
      td.onmouseout = function() {
          td.className = c;
      }

      td.appendChild(document.createTextNode(d));
      return td;
  };

  // sets the selected date, its displayed on different color
  this.setSelectedDate = function(date)
  {
    me.selected_date = new Date(date);
    me.browse_date = new Date(date);
  };

  //Draw head of calendar,  month , year,  week days.
  this.head = function()
  {
    var tbody = document.createElement("tbody");
    var tr = document.createElement("tr");
    var td = document.createElement("td");
    var form = document.createElement("form");
    var option;// = document.createElement("option");

    form.className = "cmy";

    td.className = "cmy";
    td.colSpan = "7";
      
    //draw months
    for( i = 0; i < months.length; i++ )
    {  
      option = document.createElement("option");
      option.value = i;
      option.appendChild(document.createTextNode(months[i]));
      me.select_month.appendChild(option);
      option.style.zindex = -100;
    }

    // draw years
    for( i = me.from_year; i <= me.to_year; i++ )
    {
      option = document.createElement("option");
      option.value = i;
      option.appendChild(document.createTextNode(i));
      me.select_year.appendChild(option);
    }
      
    me.select_month = form.appendChild(me.select_month);
    me.select_year = form.appendChild(me.select_year);
    td.appendChild(form);
    tr.appendChild(td);
    tbody.appendChild(tr);
    me.thead.appendChild(tbody);

    me.select_month.selectedIndex = me.browse_date.getMonth();
    me.select_year.value = me.browse_date.getFullYear();
  };

  // Draw days
  this.body = function()
  {
    while(me.tbody.firstChild) me.tbody.removeChild(me.tbody.firstChild);
    var tbody = document.createElement("tbody");
    var tr = document.createElement("tr");
    var td = new Array();
    var th;
    var date = new Date(me.browse_date);
    var day;
    var i;

    date.setDate(1);

    // draw week days
    for(i = 0; i < wdays.length; i++ )
    {
      th = document.createElement("th");
      th.className = "cwd";
      th.appendChild(document.createTextNode(wdays[i]));
      tr.appendChild(th);
    }
    tbody.appendChild(tr);
    tbody = me.tbody.appendChild(tbody);

    // draw initial blank days
    for(i = 0; i < date.getDay(); i++ )
      td[i] = document.createElement("td");

    var last_day = getLastDay(date);
    // draw days
    for(day = 1; day < last_day+1; day++)
      td[i++] = me.newDay(day);

    // draw last blank days
    for(i; i < 42; i++ )
      td[i] = document.createElement("td");

    // insert days in rows
    for(i = 0; i < 6; i++)
    {
      tr = document.createElement("tr");
      for(var j = i*7; j < (i*7)+7; j++)
        tr.appendChild(td[j]);
      tbody.appendChild(tr);
    }
  };

  // returns calendar table element
  this.get = function()
  {
    me.head();
    me.body();

    var conf = new Array();
    conf["topic"] = "JSCalendar";
    conf["class"] = "cal";

    me.win = new cssWindow(conf);

    me.thead = me.win.setHead(me.thead);
    //this.win.setFoot();
    me.tbody = me.win.setBody(me.tbody);

    return me.win.get();
  };
}
