var months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var cal = null;

function date_parse(s) {
  if (!s || s=='') return null;
  s = s.split('/');
  return new Date(parseInt(s[2], 10),parseInt(s[0], 10)-1,parseInt(s[1],10));
}

function date_string(d) {
  return date_pad(d.getMonth()+1) + '/' + date_pad(d.getDate()) + '/' + date_year(d);
}

function date_pad(i) {
  if (i<10) return '0' + i;
  return i;
}

function date_year(d) {
  var y = d.getYear() % 100;
  y += (y < 38) ? 2000 : 1900;
  return y;
}

function date_compare(o,t) {
  var o = o.valueOf();
  var t = t.valueOf();
  if (o<t) return -1;
  if (o>t) return 1;
  return 0;
}

function Calendar(ctl) {
  this._ctl = ctl;
  if (ctl.value) {
    this._date = date_parse(ctl.value);
  } else {
    this._date = new Date();
  }
  this._date.setDate(1);
  this.Increment = function() { this._date.setMonth(this._date.getMonth()+1); this.Close(); this.Show(); }
  this.Decrement = function() { this._date.setMonth(this._date.getMonth()-1); this.Close(); this.Show(); }
  this.Show = function () {
    var m = months[this._date.getMonth()] + ' ' + date_year(this._date);
    var s = "<table><tr><td class='monthNav'><a id='prev' href=\"javascript:Prev()\">&laquo;</a></td><td colspan='5' class='month'>" + m + "</td><td class='monthNav'><a class='button close' href='javascript:Close()'>&nbsp;</a></td></tr>";
    s += "<tr class='day'><td>Sun</td><td>Mon</td><td>Tue</td><td>Wed</td><td>Thu</td><td>Fri</td><td>Sat</td></tr>";
    s += this.Days(this._date);
    var d = new Date(date_year(this._date),this._date.getMonth()+1,1);
    m = months[d.getMonth()] + ' ' + date_year(d);
    s += "<tr><td class='monthNav'></td><td colspan='5' class='month'>" + m + "</td><td class='monthNav'><a id='next' href='javascript:Next()'>&raquo;</a></td></tr>";
    s += this.Days(d) + "</table>";
    var e = E('div');
    e.setAttribute('id','smallCalendar');
    if (this._ctl.nextSibling) this._ctl.parentNode.insertBefore(e,this._ctl.nextSibling);
    else this._ctl.parentNode.appendChild(e);
    e.onclick = function(e) { if (cal) cal._show=true; }
    e.innerHTML = s;
    this._div = e;
    if (cal) cal.Close();
    this._show = true;
    cal=this;
  }
  this.Days = function(d) {
    var y = date_year(d);
    var m = d.getMonth();
    var f = new Date(y,m,1).getDay();
    var l = new Date(y,m,new Date(y,++m,0).getDate()).getDate();
    var c = Math.ceil((f+l)/7)*7;
    var t = new Date();
    t = (d.getMonth()==t.getMonth() && d.getYear()==t.getYear()) ? t.getDate() : -1;
    var s = '';
    for (var i=0; i<c; i++) {
      d = i-f+1;
      if (i%7==0) s+= '<tr>';
      if (t>-1 && d==t) s+= "<td class='today'>";
      else s += "<td class='date'>";
      if (d<1 || d>l) s += '&nbsp;';
      else {
        s+="<a href=\"javascript:Set('" + date_pad(m) + '/' + date_pad(d) + '/' + y + "');\">" + d + "</a>";
      }
      s += '</td>';
      if ((i+1)%7==0) s+= '</tr>';
    }
    return s;
  }
  this.Close = function() {
    if (!this._show) {
      if (this._div) {
        this._div.parentNode.removeChild(this._div);
      }
      cal = null;
    }
    this._show = false;
  }
  this.Set = function(d) {
    this._ctl.value = d;
    trigger_event(this._ctl,'change');
    this.Close();
  }
}

function Prev() {
  if (cal) cal.Decrement();
}

function Next() {
  if (cal) cal.Increment();
}

function Set(d) {
  if (cal) cal.Set(d);
}

function Close() {
  if (cal) cal.Close();
}

document.onclick = Close;
document.keypress = Close;
