/**
Origins of these calendar routines can be found at...
http://www.nsftools.com/tips/NotesTips.htm#datepicker
*/

function bbh_Calendar () {
  var self = this;

  this.SetFieldID = function (a_FieldID) {
    if (document.getElementById(a_FieldID)) {
      self.FieldID = a_FieldID;
      self.Field = document.getElementById(a_FieldID);
      return true; }
    return false;
  };

  this.SetAnchorID = function(a_AnchorID) {
    if (document.getElementById(a_AnchorID)) {
      self.AnchorID = a_AnchorID;
      self.Anchor = document.getElementById(a_AnchorID);
      return true; }
    return false;
  };

  this.SetOnChange = function(a_OnChange) {
    self.OnChange = a_OnChange;
  };

  this.SetDate = function(a_Date) {
    self.Date = a_Date.Copy();
    self.SetDisplayedMonth(self.Date);
  };

  this.LoadFieldDate = function() {
    if (self.Field == null) { return; }
    self.Date.FromText(self.Field.value);
  };

  this.SetDisplayedMonth = function(a_Date) {
    self.DisplayedMonth = a_Date.Copy();
    self.DisplayedMonth.setDate(1);
  };

  this.UpdateField = function() {
    if (self.Field != null) {
      self.Field.value = self.Date.ToText(); }
  };

  this.Close = function (a_Event) {
    if (self.Visible == false) { return false; }
    self.NodeCover.style.display = "none";
    self.Visible = false;
    return false;
  };

  this.CoverClicked = function (a_Event) {
    self.Close();
    return false;
  };

  this.ContainerClicked = function (a_Event) {
    var i_Event = a_Event ? a_Event : event;
    if (i_Event.stopPropagation) {
      i_Event.stopPropagation(); }
    else {
      i_Event.cancelBubble = true; } 
    return false;
  };

  this.ButtonClicked = function (a_Event) {
    var i_Event = a_Event ? a_Event : event;
    var i_Target = i_Event.target ? i_Event.target : i_Event.srcElement;
    var i_ID = i_Target.id;
    
    if (self.Visible == false) { return false; }
    i_ID = i_ID.substr(i_ID.lastIndexOf("-") + 1);

//    alert("ID=" + i_ID);

    switch (i_ID) {
      case "PrevMonth":
        self.DisplayedMonth.AddMonths(-1);
        self.RefreshCalendar();
        break;
      case "NextMonth":
        self.DisplayedMonth.AddMonths(1);
        self.RefreshCalendar();
        break;
      case "Close":
        self.Close();
        break; }
    return false;
  };

  this.DayClicked = function (a_Event) {
    var i_Event = a_Event ? a_Event : event;
    var i_Target = i_Event.target ? i_Event.target : i_Event.srcElement;
    var i_ID = i_Target.id;
    var i_ThisDate = self.DisplayedMonth.Copy();
    var i_ThisDay = i_ThisDate.getDay();
    
    if (self.Visible == false) { return false; }
    i_ID = i_ID.substr(i_ID.lastIndexOf("-") + 1);
    if (i_ID.length < 4) { return false; }
    i_ID = parseInt(i_ID.substr(3));

    i_ThisDate.AddDays(0 - i_ThisDay + i_ID);

    self.Date.setTime(i_ThisDate.getTime());
    self.UpdateField();
    self.Close();
    self.OnChange();
    return false;
  };

  this.CreateHtml = function() {
    var i_Group = null;
    var i_Group2 = null;
    var i_Node = null;
    var i_ButtonPrev = null;
    var i_ButtonNext = null;
    var i_ButtonClose = null;

//alert("Create In");

    self.NodeCover = bbh_DomElementCreate("div", "", "class", "cal_Cover", "style", "display:none;");
    self.NodeContainer = bbh_DomElementCreate("div", "", "class", "cal_Container", "~Parent", self.NodeCover);
    i_Group = bbh_DomElementCreate("div", "", "class", "cal_HeadMonth", "~Parent", self.NodeContainer);
    i_Group2 = bbh_DomElementCreate("div", "", "class", "cal_HeadMonthPrev", "~Parent", i_Group);
    i_ButtonPrev = bbh_DomElementCreate("button", "&lt;", "id", self.FieldID + "-PrevMonth", "~Parent", i_Group2);
    i_Group2 = bbh_DomElementCreate("div", "", "class", "cal_HeadMonthNext", "~Parent", i_Group);
    i_ButtonNext = bbh_DomElementCreate("button", "&gt;", "id", self.FieldID + "-NextMonth", "~Parent", i_Group2);
    self.NodeTitle = bbh_DomElementCreate("span", "", "~Parent", i_Group);

    i_Group = bbh_DomElementCreate("div", "", "class", "cal_HeadDays", "~Parent", self.NodeContainer);
    for (i_Sub = 0; i_Sub < 7; i_Sub++) {
      i_Class = "cal_HeadDay";
      if (i_Sub == 0) {
        i_Class = "cal_HeadDayFirst"; }
      i_Node = bbh_DomElementCreate("div", bd_DaysShort[i_Sub], "class", i_Class, "~Parent", i_Group); }

    for (i_Sub = 0; i_Sub < 6; i_Sub++) {
      i_Group = bbh_DomElementCreate("div", "", "class", "cal_CellsLine", "~Parent", self.NodeContainer);
      for (i_Sub2 = 0; i_Sub2 < 7; i_Sub2++) {
        i_Sub3 = (i_Sub * 7) + i_Sub2;
        i_Class = "cal_Cell";
        if (i_Sub == 0) {
          i_Class = "cal_CellFirst"; }
        self.NodeCells[i_Sub3] = bbh_DomElementCreate("div", "", "class", i_Class, "~Parent", i_Group);
        self.NodeLinks[i_Sub3] = bbh_DomElementCreate("a", "", "id", self.FieldID + "-Day" + i_Sub3, "href", "javascript:void(0)", "~Parent", self.NodeCells[i_Sub3]); } }

    i_Group = bbh_DomElementCreate("div", "", "class", "cal_Footer", "~Parent", self.NodeContainer);
    i_ButtonClose = bbh_DomElementCreate("button", "close", "id", self.FieldID + "-Close", "~Parent", i_Group);

    document.body.appendChild(self.NodeCover);

    bbh_EventAdd(self.NodeCover, "click", self.CoverClicked);
    bbh_EventAdd(self.NodeContainer, "click", self.ContainerClicked);
    bbh_EventAdd(i_ButtonPrev, "click", self.ButtonClicked);
    bbh_EventAdd(i_ButtonNext, "click", self.ButtonClicked);
    bbh_EventAdd(i_ButtonClose, "click", self.ButtonClicked);
    for (i_Sub = 0; i_Sub < 42; i_Sub++) {
//      bbh_EventAdd(self.NodeCells[i_Sub], "mouseover", self.CellMouseOver);
//      bbh_EventAdd(self.NodeCells[i_Sub], "mouseout", self.CellMouseOut);
      bbh_EventAdd(self.NodeLinks[i_Sub], "click", self.DayClicked); }

//alert("Create Out");
  };

  this.RefreshCalendar = function() {
    var i_ThisDate = self.DisplayedMonth.Copy();
    var i_ThisMonth = i_ThisDate.getMonth();
    var i_ThisDay = i_ThisDate.getDay();
    var i_Class = "";
    var i_Sub = 0;

//alert("Refresh In");

    self.NodeTitle.innerHTML = bd_MonthsLong[i_ThisMonth] + " " + i_ThisDate.getFullYear();
    
    i_ThisDate.AddDays(0 - i_ThisDay);
    for (i_Sub = 0; i_Sub < 42; i_Sub++) {
      i_Class = "cal_CellOut";
      if (i_ThisDate.getMonth() == i_ThisMonth) {
        i_Class = "cal_Cell"; }
      if (self.Date.DaysDiff(i_ThisDate) == 0) {
        i_Class = "cal_CellCur"; }
      if (i_Sub % 7 == 0) {
        i_Class = i_Class + "First"; }
      self.NodeCells[i_Sub].className = i_Class;
      self.NodeLinks[i_Sub].innerHTML = i_ThisDate.getDate();
      i_ThisDate.AddDays(1); }

    self.NodeCover.style.display = "block";

//alert("Refresh Out");
  };

  this.Show = function() {
    var i_Anchor = null;
    var i_Parent = null;
    var i_Top = 0;
    var i_Left = 0;
    var i_Sub = 0;

//alert("Show In");

    if (self.Visible == true) { return; }

    if (self.Anchor == null) {
      if (self.Field == null) { return; }
      i_Anchor = self.Field; }
    else {
      i_Anchor = self.Anchor; }

    if (self.NodeContainer == null) {
      self.CreateHtml(); }

    i_Top = i_Anchor.offsetTop;
    i_Left = i_Anchor.offsetLeft;
    i_Parent = i_Anchor;
    while (i_Parent.offsetParent) {
      i_Parent = i_Parent.offsetParent;
      i_Top += i_Parent.offsetTop ;
      i_Left += i_Parent.offsetLeft; }

//i_Top=i_Top+100;
    self.NodeCover.style.width = bbh_PageWidth() + "px";
    self.NodeCover.style.height = bbh_PageHeight() + "px";
    self.NodeContainer.style.position = "absolute";
    self.NodeContainer.style.left = i_Left + "px";
    self.NodeContainer.style.top = i_Top + "px";
 
    self.RefreshCalendar();
    self.Visible = true;

//alert("Class=" + self.NodeContainer.className);

//alert("Show Out " + i_Top + "/" + i_Left);
  };


  this.Date = new Date();
  this.Date.SetToMidnight();
  this.DisplayedMonth = null;
  this.SetDisplayedMonth(this.Date);
  this.FieldID = "";
  this.Field = null;
  this.AnchorID = "";
  this.Anchor = null;
  this.OnChange = function () {};
  this.NodeCover = null;
  this.NodeContainer = null;
  this.NodeTitle = null;
  this.NodeCells = new Array();
  this.NodeLinks = new Array();
  this.Visible = false;
}


