function CalendarWidget(rootId, tgtEl)
{
  if (rootId && document.getElementById)
  {
    CalendarWidget.output(rootId);
    CalendarWidget.widgets[rootId] = this;
    this.root = document.getElementById(rootId);
    this.date = new Date();
    this.selectedDIV = null;
    this.selectedDate = "";
    this.tgtEl = tgtEl;
    if (tgtEl.value.length)
    // this.dateInputOnChange() will call init once done
    {
      this.dateInputOnChange();
    }
    else
    {
      this.init();
    }
  }
}
new CalendarWidget(null, null);

CalendarWidget.widgets = new Array();
CalendarWidget.months = new Array('JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC');
CalendarWidget.days = new Array('S','M','T','W','T','F','S');
CalendarWidget.dateFmt = "m/d/y";
CalendarWidget.prefixImg = "/images/universal-images";

CalendarWidget.dateCellOver = function()
{
  this.className = IFAWUtil.classNameAdd(this.className, "over");
}

CalendarWidget.dateCellOut = function()
{
  this.className = IFAWUtil.classNameDel(this.className, "over");
}

CalendarWidget.dateCellOnClick = function()
{
  var parent, widget;
  for (parent = this.parentNode; parent; parent = parent.parentNode)
  {
    if (parent.id &&
        (widget = CalendarWidget.widgets[parent.id]))
    {
      widget.onClick(this);
    }
  }
}

CalendarWidget.output = function(rootId)
{
  var i;
  document.write(
    '<table class="date_container" id="'+rootId+'" cellpadding="0" cellspacing="0" border="0">'+
    '  <tr class="header_row">'+
    '    <td colspan="7">'+
          '<div class="prev_month"><a href="#" onclick="return CalendarWidget.prev(\''+rootId+'\');"><img src="'+CalendarWidget.prefixImg+'/date-selector-arrow-l.gif" alt="Previous Month" width="15" height="16" /></a></div>'+
          '<div class="next_month"><a href="#" onclick="return CalendarWidget.next(\''+rootId+'\');"><img src="'+CalendarWidget.prefixImg+'/date-selector-arrow-r.gif" alt="Next Month" width="15" height="16" /></a></div>'+
          '<div class="month_name"></div>'+
         '</td>'+
    '  </tr>'+
    '  <tr class="date_grid">'+
    '    <td class="day_abbr">'+CalendarWidget.days[0]+'</td>'+
    '    <td class="day_abbr">'+CalendarWidget.days[1]+'</td>'+
    '    <td class="day_abbr">'+CalendarWidget.days[2]+'</td>'+
    '    <td class="day_abbr">'+CalendarWidget.days[3]+'</td>'+
    '    <td class="day_abbr">'+CalendarWidget.days[4]+'</td>'+
    '    <td class="day_abbr">'+CalendarWidget.days[5]+'</td>'+
    '    <td class="day_abbr">'+CalendarWidget.days[6]+'</td>'+
    '  </tr>'
  );
  for (i = 0; i < 6; i++)
  {
    document.write(
      '  <tr class="date_cells">'+
      '    <td class="date_cell"></td>'+
      '    <td class="date_cell"></td>'+
      '    <td class="date_cell"></td>'+
      '    <td class="date_cell"></td>'+
      '    <td class="date_cell"></td>'+
      '    <td class="date_cell"></td>'+
      '    <td class="date_cell"></td>'+
      '  </tr>'
    );
  }
  document.write('</table>');
}

CalendarWidget.initXV = function(node)
{
  var m, d, dt;
  if (node.nodeType == "1")
  {
    if (IFAWUtil.classNameMatch(node.className, "month_name"))
    {
      node.innerHTML = CalendarWidget.months[this.monthNumber]+" "+this.date.getFullYear();
    }
    else if (IFAWUtil.classNameMatch(node.className, "date_cell"))
    {
    
      node.className = IFAWUtil.classNameDel(node.className, "selected");
      if (!this.done)
      {
        if (!this.xOffset)
        {
          d = new Date();
          d.setFullYear(this.date.getFullYear(), this.date.getMonth(), this.dateCounter);
          dt = CalendarWidget.formatDate(d);
          if (dt == this.widget.selectedDate)
          {
            if (this.widget.selectedDIV)
            {
              this.widget.selectedDIV.className = IFAWUtil.classNameDel(this.widget.selectedDIV.className, "selected");
            }
            this.widget.selectedDIV = node;
            node.className = IFAWUtil.classNameAdd(node.className, "selected");
          }

          node.innerHTML = this.dateCounter++;
          node.onmouseover = CalendarWidget.dateCellOver;
          node.onmouseout = CalendarWidget.dateCellOut;
          node.onclick = CalendarWidget.dateCellOnClick;          

          this.date.setTime(this.date.getTime() + (1000 * 86400));
          if (this.date.getMonth() != this.monthNumber)
          {
            this.done = true;
          }
        }
        else
        {
          node.innerHTML = "";
          node.onmouseover = null;
          node.onmouseout = null;
          node.onclick = null;
          this.xOffset--;
        }
      }
      else
      {
        node.innerHTML = "";
        node.onmouseover = null;
        node.onmouseout = null;
        node.onclick = null;
      }
    }
  }
  return true;
}

CalendarWidget.prototype.init = function()
{
  var h, i, c;

  h = new Object();
  h.date = new Date();
  h.date.setDate(1);
  h.date.setMonth(this.date.getMonth());
  h.date.setFullYear(this.date.getFullYear());
  h.dateCounter = 1;
  h.monthNumber = h.date.getMonth();
  h.xOffset = h.date.getDay();
  h.done = false;
  h.widget = this;
  h.handleXVerse = CalendarWidget.initXV;
  IFAWUtil.xVerseNode(this.root, h);
}

CalendarWidget.prev = function(rootId)
{
  CalendarWidget.widgets[rootId].prev();
  return false;
}

CalendarWidget.prototype.prev = function()
{
  var m = this.date.getMonth();
  if (m == 0)
  {
    this.date.setMonth(11);
    this.date.setFullYear(this.date.getFullYear() - 1);
  }
  else
  {
    this.date.setMonth(--m);
  }
  this.init();
}

CalendarWidget.next = function(rootId)
{
  CalendarWidget.widgets[rootId].next();
  return false;
}

CalendarWidget.prototype.next = function()
{
  var m = this.date.getMonth();
  if (m == 11)
  {
    this.date.setMonth(0);
    this.date.setFullYear(this.date.getFullYear() + 1);
  }
  else
  {
    this.date.setMonth(++m);
  }
  this.init();
}

CalendarWidget.formatDate = function(d)
{
  var i, c, df, t;
  
  df = "";
  for (i = 0, c = CalendarWidget.dateFmt.length; i < c; i++)
  {
    ch = CalendarWidget.dateFmt.substr(i, 1);
    switch (ch)
    {
      case "y":
        df += d.getFullYear();
break;
      case "m":
        t = ""+(d.getMonth() + 1);
        df += (t.length == 1) ? "0"+t : t;
        break;
      case "d":
        t = ""+d.getDate();
        df += (t.length == 1) ? "0"+t : t;
        break;
      default:
        df += ch;
break;
    }
  }

  return df;
}


CalendarWidget.prototype.onClick = function(div)
{
  var d;
  if (this.selectedDIV)
  {
    this.selectedDIV.className = IFAWUtil.classNameDel(this.selectedDIV.className, "selected");
  }
  this.selectedDIV = div;
div.className = IFAWUtil.classNameAdd(div.className, "selected");

  d = new Date();
  d.setFullYear(this.date.getFullYear(), this.date.getMonth(), parseInt(div.innerHTML));
  this.selectedDate = CalendarWidget.formatDate(d);

this.tgtEl.value = this.selectedDate;
}

CalendarWidget.prototype.clear = function()
{
  if (this.selectedDIV)
  {
    this.selectedDIV.className = IFAWUtil.classNameDel(this.selectedDIV.className, "selected");
    this.selectedDIV = null;
}
  this.tgtEl.value = "";
}

CalendarWidget.dateInputOnChange = function(rootId)
{
  CalendarWidget.widgets[rootId].dateInputOnChange();
}

CalendarWidget.prototype.dateInputOnChange = function()
{
  var d = CalendarWidget.fixStrDate(this.tgtEl.value);
if (!d)
  {
    this.clear();
    this.date = new Date();
  }
  else
  {
    this.date = d;
this.tgtEl.value = CalendarWidget.formatDate(d);
this.selectedDate = this.tgtEl.value;
}
  this.init();
}

CalendarWidget.fixStrDate = function(value)
{
  var rgs, d, i, c, ch, t;

  value = value.replace(/^\s+/, "");
  value = value.replace(/\s+$/, "");

  if (!(rgs = value.match(/^([0-9]{1,4})[^0-9]([0-9]{1,4})[^0-9]([0-9]{1,4})$/)))
{
    return null;
  }
  
  d = new Date();
  pos = 1;
for (i = 0, c = CalendarWidget.dateFmt.length; i < c; i++)
  {
    ch = CalendarWidget.dateFmt.substr(i, 1);
    switch (ch)
    {
      case "y":
        t = parseInt(rgs[pos++].replace(/^0+/, ""));
        if ((t > 90) && (t < 100))
        {
          t += 1900;
}
        else if (t < 100)
        {
          t += 2000;
}
        d.setFullYear(t);
        break;
      case "m":
        t = parseInt(rgs[pos++].replace(/^0+/, "")) - 1;
        d.setMonth((t > 0) ? t : 0);
        break;
      case "d":
        d.setDate(parseInt(rgs[pos++].replace(/^0+/, "")));
        break;
      default:
        break;
    }
  }

  return d;
}